blob: 96b952c94521a19f9f80d19a0d7030291e95ebd3 [file] [log] [blame]
Daniel Zheng0d307182023-01-31 21:07:53 +00001//
2// Copyright (C) 2023 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16#include "task.h"
Daniel Zheng7627b542023-04-13 13:43:17 -070017
Daniel Zheng47d70a52023-02-14 17:44:40 +000018#include <iostream>
Daniel Zheng7627b542023-04-13 13:43:17 -070019
20#include <android-base/logging.h>
21#include <android-base/parseint.h>
22
Daniel Zheng71b3b432023-01-30 17:43:00 +000023#include "fastboot.h"
Daniel Zheng47d70a52023-02-14 17:44:40 +000024#include "filesystem.h"
25#include "super_flash_helper.h"
Daniel Zheng29a211c2023-03-10 07:23:49 +000026#include "util.h"
Daniel Zheng0d307182023-01-31 21:07:53 +000027
Daniel Zheng47d70a52023-02-14 17:44:40 +000028using namespace std::string_literals;
Daniel Zheng5a9905a2023-05-23 10:51:01 -070029FlashTask::FlashTask(const std::string& slot, const std::string& pname, const std::string& fname,
Daniel Zheng791a83b2023-05-23 10:42:39 -070030 const bool apply_vbmeta, const FlashingPlan* fp)
31 : pname_(pname), fname_(fname), slot_(slot), apply_vbmeta_(apply_vbmeta), fp_(fp) {}
Daniel Zheng0d307182023-01-31 21:07:53 +000032
33void FlashTask::Run() {
34 auto flash = [&](const std::string& partition) {
Daniel Zhengbc01da52023-02-23 03:25:52 +000035 if (should_flash_in_userspace(partition) && !is_userspace_fastboot()) {
Daniel Zheng0d307182023-01-31 21:07:53 +000036 die("The partition you are trying to flash is dynamic, and "
37 "should be flashed via fastbootd. Please run:\n"
38 "\n"
39 " fastboot reboot fastboot\n"
40 "\n"
41 "And try again. If you are intentionally trying to "
42 "overwrite a fixed partition, use --force.");
43 }
Daniel Zheng791a83b2023-05-23 10:42:39 -070044 do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_, fp_);
Daniel Zheng0d307182023-01-31 21:07:53 +000045 };
46 do_for_partitions(pname_, slot_, flash, true);
47}
Daniel Zheng71b3b432023-01-30 17:43:00 +000048
Daniel Zheng665a4602023-06-05 11:24:59 -070049std::string FlashTask::ToString() {
50 std::string apply_vbmeta_string = "";
51 if (apply_vbmeta_) {
52 apply_vbmeta_string = " --apply_vbmeta";
53 }
54 return "flash" + apply_vbmeta_string + " " + pname_ + " " + fname_;
55}
56
Daniel Zheng29a211c2023-03-10 07:23:49 +000057std::string FlashTask::GetPartitionAndSlot() {
58 auto slot = slot_;
59 if (slot.empty()) {
60 slot = get_current_slot();
61 }
62 if (slot.empty()) {
63 return pname_;
64 }
65 if (slot == "all") {
66 LOG(FATAL) << "Cannot retrieve a singular name when using all slots";
67 }
68 return pname_ + "_" + slot;
69}
70
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -070071RebootTask::RebootTask(const FlashingPlan* fp) : fp_(fp){};
72RebootTask::RebootTask(const FlashingPlan* fp, const std::string& reboot_target)
Daniel Zheng43987c92023-03-07 18:20:53 +000073 : reboot_target_(reboot_target), fp_(fp){};
Daniel Zheng71b3b432023-01-30 17:43:00 +000074
75void RebootTask::Run() {
Daniel Zheng77af6af2023-04-20 14:30:28 -070076 if (reboot_target_ == "fastboot") {
Daniel Zheng71b3b432023-01-30 17:43:00 +000077 if (!is_userspace_fastboot()) {
78 reboot_to_userspace_fastboot();
Daniel Zhengbc01da52023-02-23 03:25:52 +000079 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000080 }
81 } else if (reboot_target_ == "recovery") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000082 fp_->fb->RebootTo("recovery");
83 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000084 } else if (reboot_target_ == "bootloader") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000085 fp_->fb->RebootTo("bootloader");
86 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000087 } else if (reboot_target_ == "") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000088 fp_->fb->Reboot();
89 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000090 } else {
91 syntax_error("unknown reboot target %s", reboot_target_.c_str());
92 }
93}
Daniel Zheng47d70a52023-02-14 17:44:40 +000094
Daniel Zheng665a4602023-06-05 11:24:59 -070095std::string RebootTask::ToString() {
96 return "reboot " + reboot_target_;
97}
98
Daniel Zheng47d70a52023-02-14 17:44:40 +000099FlashSuperLayoutTask::FlashSuperLayoutTask(const std::string& super_name,
100 std::unique_ptr<SuperFlashHelper> helper,
David Anderson74c78072023-03-16 21:21:28 -0700101 SparsePtr sparse_layout, uint64_t super_size)
Daniel Zheng47d70a52023-02-14 17:44:40 +0000102 : super_name_(super_name),
103 helper_(std::move(helper)),
David Anderson74c78072023-03-16 21:21:28 -0700104 sparse_layout_(std::move(sparse_layout)),
105 super_size_(super_size) {}
Daniel Zheng47d70a52023-02-14 17:44:40 +0000106
107void FlashSuperLayoutTask::Run() {
David Anderson74c78072023-03-16 21:21:28 -0700108 // Use the reported super partition size as the upper limit, rather than
109 // sparse_file_len, which (1) can fail and (2) is kind of expensive, since
110 // it will map in all of the embedded fds.
Daniel Zheng47d70a52023-02-14 17:44:40 +0000111 std::vector<SparsePtr> files;
David Anderson74c78072023-03-16 21:21:28 -0700112 if (int limit = get_sparse_limit(super_size_)) {
Daniel Zheng47d70a52023-02-14 17:44:40 +0000113 files = resparse_file(sparse_layout_.get(), limit);
114 } else {
115 files.emplace_back(std::move(sparse_layout_));
116 }
117
118 // Send the data to the device.
119 flash_partition_files(super_name_, files);
120}
Daniel Zheng665a4602023-06-05 11:24:59 -0700121std::string FlashSuperLayoutTask::ToString() {
122 return "optimized-flash-super";
123}
Daniel Zheng47d70a52023-02-14 17:44:40 +0000124
125std::unique_ptr<FlashSuperLayoutTask> FlashSuperLayoutTask::Initialize(
Daniel Zheng29a211c2023-03-10 07:23:49 +0000126 const FlashingPlan* fp, std::vector<ImageEntry>& os_images) {
Daniel Zhengd62002c2023-06-21 14:27:35 -0700127 if (!fp->should_optimize_flash_super) {
128 LOG(INFO) << "super optimization is disabled";
129 return nullptr;
130 }
Daniel Zheng47d70a52023-02-14 17:44:40 +0000131 if (!supports_AB()) {
132 LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device";
133 return nullptr;
134 }
Daniel Zheng1e6456b2023-04-10 09:35:48 -0700135 if (fp->slot_override == "all") {
Daniel Zheng47d70a52023-02-14 17:44:40 +0000136 LOG(VERBOSE) << "Cannot optimize flashing super for all slots";
137 return nullptr;
138 }
139
140 // Does this device use dynamic partitions at all?
141 unique_fd fd = fp->source->OpenFile("super_empty.img");
142
143 if (fd < 0) {
144 LOG(VERBOSE) << "could not open super_empty.img";
145 return nullptr;
146 }
147
148 std::string super_name;
149 // Try to find whether there is a super partition.
150 if (fp->fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) {
151 super_name = "super";
152 }
Daniel Zheng47d70a52023-02-14 17:44:40 +0000153
David Anderson74c78072023-03-16 21:21:28 -0700154 uint64_t partition_size;
155 std::string partition_size_str;
Daniel Zheng47d70a52023-02-14 17:44:40 +0000156 if (fp->fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) {
157 LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition";
158 return nullptr;
159 }
David Anderson74c78072023-03-16 21:21:28 -0700160 partition_size_str = fb_fix_numeric_var(partition_size_str);
161 if (!android::base::ParseUint(partition_size_str, &partition_size)) {
162 LOG(VERBOSE) << "Could not parse " << super_name << " size: " << partition_size_str;
163 return nullptr;
164 }
165
Daniel Zheng47d70a52023-02-14 17:44:40 +0000166 std::unique_ptr<SuperFlashHelper> helper = std::make_unique<SuperFlashHelper>(*fp->source);
167 if (!helper->Open(fd)) {
168 return nullptr;
169 }
170
171 for (const auto& entry : os_images) {
Kelvin Zhang2a4a45f2023-04-03 20:44:05 +0000172 auto partition = GetPartitionName(entry, fp->current_slot);
Daniel Zheng47d70a52023-02-14 17:44:40 +0000173 auto image = entry.first;
174
175 if (!helper->AddPartition(partition, image->img_name, image->optional_if_no_image)) {
176 return nullptr;
177 }
178 }
179
180 auto s = helper->GetSparseLayout();
181 if (!s) return nullptr;
182
183 // Remove images that we already flashed, just in case we have non-dynamic OS images.
184 auto remove_if_callback = [&](const ImageEntry& entry) -> bool {
Kelvin Zhang2a4a45f2023-04-03 20:44:05 +0000185 return helper->WillFlash(GetPartitionName(entry, fp->current_slot));
Daniel Zheng47d70a52023-02-14 17:44:40 +0000186 };
187 os_images.erase(std::remove_if(os_images.begin(), os_images.end(), remove_if_callback),
188 os_images.end());
David Anderson74c78072023-03-16 21:21:28 -0700189 return std::make_unique<FlashSuperLayoutTask>(super_name, std::move(helper), std::move(s),
190 partition_size);
Daniel Zheng47d70a52023-02-14 17:44:40 +0000191}
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000192
Daniel Zheng29a211c2023-03-10 07:23:49 +0000193std::unique_ptr<FlashSuperLayoutTask> FlashSuperLayoutTask::InitializeFromTasks(
194 const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks) {
Daniel Zhengd62002c2023-06-21 14:27:35 -0700195 if (!fp->should_optimize_flash_super) {
196 LOG(INFO) << "super optimization is disabled";
197 return nullptr;
198 }
Daniel Zheng29a211c2023-03-10 07:23:49 +0000199 if (!supports_AB()) {
200 LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device";
201 return nullptr;
202 }
203 if (fp->slot_override == "all") {
204 LOG(VERBOSE) << "Cannot optimize flashing super for all slots";
205 return nullptr;
206 }
207
208 // Does this device use dynamic partitions at all?
209 unique_fd fd = fp->source->OpenFile("super_empty.img");
210
211 if (fd < 0) {
212 LOG(VERBOSE) << "could not open super_empty.img";
213 return nullptr;
214 }
215
216 std::string super_name;
217 // Try to find whether there is a super partition.
218 if (fp->fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) {
219 super_name = "super";
220 }
221 uint64_t partition_size;
222 std::string partition_size_str;
223 if (fp->fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) {
224 LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition";
225 return nullptr;
226 }
227 partition_size_str = fb_fix_numeric_var(partition_size_str);
228 if (!android::base::ParseUint(partition_size_str, &partition_size)) {
229 LOG(VERBOSE) << "Could not parse " << super_name << " size: " << partition_size_str;
230 return nullptr;
231 }
232
233 std::unique_ptr<SuperFlashHelper> helper = std::make_unique<SuperFlashHelper>(*fp->source);
234 if (!helper->Open(fd)) {
235 return nullptr;
236 }
237
238 for (const auto& task : tasks) {
239 if (auto flash_task = task->AsFlashTask()) {
Daniel Zheng2775df62023-05-25 15:14:59 -0700240 auto partition = flash_task->GetPartitionAndSlot();
241 if (!helper->AddPartition(partition, flash_task->GetImageName(), false)) {
242 return nullptr;
Daniel Zheng29a211c2023-03-10 07:23:49 +0000243 }
244 }
245 }
246
247 auto s = helper->GetSparseLayout();
248 if (!s) return nullptr;
249 // Remove images that we already flashed, just in case we have non-dynamic OS images.
250 auto remove_if_callback = [&](const auto& task) -> bool {
251 if (auto flash_task = task->AsFlashTask()) {
252 return helper->WillFlash(flash_task->GetPartitionAndSlot());
253 } else if (auto update_super_task = task->AsUpdateSuperTask()) {
254 return true;
255 } else if (auto reboot_task = task->AsRebootTask()) {
256 return true;
257 }
258 return false;
259 };
260 tasks.erase(std::remove_if(tasks.begin(), tasks.end(), remove_if_callback), tasks.end());
261
262 return std::make_unique<FlashSuperLayoutTask>(super_name, std::move(helper), std::move(s),
263 partition_size);
264}
265
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700266UpdateSuperTask::UpdateSuperTask(const FlashingPlan* fp) : fp_(fp) {}
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000267
268void UpdateSuperTask::Run() {
269 unique_fd fd = fp_->source->OpenFile("super_empty.img");
270 if (fd < 0) {
271 return;
272 }
273 if (!is_userspace_fastboot()) {
274 reboot_to_userspace_fastboot();
275 }
276
277 std::string super_name;
278 if (fp_->fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
279 super_name = "super";
280 }
281 fp_->fb->Download(super_name, fd, get_file_size(fd));
282
283 std::string command = "update-super:" + super_name;
284 if (fp_->wants_wipe) {
285 command += ":wipe";
286 }
287 fp_->fb->RawCommand(command, "Updating super partition");
288}
Daniel Zheng665a4602023-06-05 11:24:59 -0700289std::string UpdateSuperTask::ToString() {
290 return "update-super";
291}
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000292
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700293ResizeTask::ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size,
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000294 const std::string& slot)
295 : fp_(fp), pname_(pname), size_(size), slot_(slot) {}
296
297void ResizeTask::Run() {
298 auto resize_partition = [this](const std::string& partition) -> void {
299 if (is_logical(partition)) {
300 fp_->fb->ResizePartition(partition, size_);
301 }
302 };
303 do_for_partitions(pname_, slot_, resize_partition, false);
304}
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000305
Daniel Zheng665a4602023-06-05 11:24:59 -0700306std::string ResizeTask::ToString() {
307 return "resize " + pname_;
308}
309
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700310DeleteTask::DeleteTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000311
312void DeleteTask::Run() {
313 fp_->fb->DeletePartition(pname_);
Daniel Zheng15e77832023-03-13 17:09:43 +0000314}
315
Daniel Zheng665a4602023-06-05 11:24:59 -0700316std::string DeleteTask::ToString() {
317 return "delete " + pname_;
318}
319
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700320WipeTask::WipeTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
Daniel Zheng15e77832023-03-13 17:09:43 +0000321
322void WipeTask::Run() {
323 std::string partition_type;
324 if (fp_->fb->GetVar("partition-type:" + pname_, &partition_type) != fastboot::SUCCESS) {
Daniel Zheng6f213b22023-03-28 22:47:04 +0000325 LOG(ERROR) << "wipe task partition not found: " << pname_;
Daniel Zheng15e77832023-03-13 17:09:43 +0000326 return;
327 }
328 if (partition_type.empty()) return;
Daniel Zheng6f213b22023-03-28 22:47:04 +0000329 if (fp_->fb->Erase(pname_) != fastboot::SUCCESS) {
330 LOG(ERROR) << "wipe task erase failed with partition: " << pname_;
331 return;
332 }
Daniel Zheng15e77832023-03-13 17:09:43 +0000333 fb_perform_format(pname_, 1, partition_type, "", fp_->fs_options);
334}
Daniel Zheng665a4602023-06-05 11:24:59 -0700335
336std::string WipeTask::ToString() {
337 return "erase " + pname_;
338}