blob: f13dd55b35bebb0678cedf3fdb45db510eb7d02b [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 Zheng1fff6902023-08-24 09:47:43 -070018#include "fastboot_driver.h"
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
Daniel Zheng1ef66b72023-08-24 15:44:56 -070033bool FlashTask::IsDynamicParitition(const ImageSource* source, const FlashTask* task) {
34 std::vector<char> contents;
35 if (!source->ReadFile("super_empty.img", &contents)) {
36 return false;
37 }
38 auto metadata = android::fs_mgr::ReadFromImageBlob(contents.data(), contents.size());
39 return should_flash_in_userspace(*metadata.get(), task->GetPartitionAndSlot());
40}
41
Daniel Zheng0d307182023-01-31 21:07:53 +000042void FlashTask::Run() {
43 auto flash = [&](const std::string& partition) {
Daniel Zheng376b7042023-08-09 10:52:38 -070044 if (should_flash_in_userspace(partition) && !is_userspace_fastboot() && !fp_->force_flash) {
Daniel Zheng0d307182023-01-31 21:07:53 +000045 die("The partition you are trying to flash is dynamic, and "
46 "should be flashed via fastbootd. Please run:\n"
47 "\n"
48 " fastboot reboot fastboot\n"
49 "\n"
50 "And try again. If you are intentionally trying to "
51 "overwrite a fixed partition, use --force.");
52 }
Daniel Zheng791a83b2023-05-23 10:42:39 -070053 do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_, fp_);
Daniel Zheng0d307182023-01-31 21:07:53 +000054 };
55 do_for_partitions(pname_, slot_, flash, true);
56}
Daniel Zheng71b3b432023-01-30 17:43:00 +000057
Daniel Zheng1ef66b72023-08-24 15:44:56 -070058std::string FlashTask::ToString() const {
Daniel Zheng665a4602023-06-05 11:24:59 -070059 std::string apply_vbmeta_string = "";
60 if (apply_vbmeta_) {
61 apply_vbmeta_string = " --apply_vbmeta";
62 }
63 return "flash" + apply_vbmeta_string + " " + pname_ + " " + fname_;
64}
65
Daniel Zheng1ef66b72023-08-24 15:44:56 -070066std::string FlashTask::GetPartitionAndSlot() const {
Daniel Zheng29a211c2023-03-10 07:23:49 +000067 auto slot = slot_;
68 if (slot.empty()) {
69 slot = get_current_slot();
70 }
71 if (slot.empty()) {
72 return pname_;
73 }
74 if (slot == "all") {
75 LOG(FATAL) << "Cannot retrieve a singular name when using all slots";
76 }
77 return pname_ + "_" + slot;
78}
79
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -070080RebootTask::RebootTask(const FlashingPlan* fp) : fp_(fp){};
81RebootTask::RebootTask(const FlashingPlan* fp, const std::string& reboot_target)
Daniel Zheng43987c92023-03-07 18:20:53 +000082 : reboot_target_(reboot_target), fp_(fp){};
Daniel Zheng71b3b432023-01-30 17:43:00 +000083
84void RebootTask::Run() {
Daniel Zheng77af6af2023-04-20 14:30:28 -070085 if (reboot_target_ == "fastboot") {
Daniel Zheng71b3b432023-01-30 17:43:00 +000086 if (!is_userspace_fastboot()) {
87 reboot_to_userspace_fastboot();
Daniel Zhengbc01da52023-02-23 03:25:52 +000088 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000089 }
90 } else if (reboot_target_ == "recovery") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000091 fp_->fb->RebootTo("recovery");
92 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000093 } else if (reboot_target_ == "bootloader") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000094 fp_->fb->RebootTo("bootloader");
95 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000096 } else if (reboot_target_ == "") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000097 fp_->fb->Reboot();
98 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000099 } else {
100 syntax_error("unknown reboot target %s", reboot_target_.c_str());
101 }
102}
Daniel Zheng47d70a52023-02-14 17:44:40 +0000103
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700104std::string RebootTask::ToString() const {
Daniel Zheng665a4602023-06-05 11:24:59 -0700105 return "reboot " + reboot_target_;
106}
107
Daniel Zheng3e048572023-06-21 15:21:06 -0700108OptimizedFlashSuperTask::OptimizedFlashSuperTask(const std::string& super_name,
109 std::unique_ptr<SuperFlashHelper> helper,
110 SparsePtr sparse_layout, uint64_t super_size,
111 const FlashingPlan* fp)
Daniel Zheng47d70a52023-02-14 17:44:40 +0000112 : super_name_(super_name),
113 helper_(std::move(helper)),
David Anderson74c78072023-03-16 21:21:28 -0700114 sparse_layout_(std::move(sparse_layout)),
Daniel Zheng5769b262023-06-21 14:41:11 -0700115 super_size_(super_size),
116 fp_(fp) {}
Daniel Zheng47d70a52023-02-14 17:44:40 +0000117
Daniel Zheng3e048572023-06-21 15:21:06 -0700118void OptimizedFlashSuperTask::Run() {
David Anderson74c78072023-03-16 21:21:28 -0700119 // Use the reported super partition size as the upper limit, rather than
120 // sparse_file_len, which (1) can fail and (2) is kind of expensive, since
121 // it will map in all of the embedded fds.
Daniel Zheng47d70a52023-02-14 17:44:40 +0000122 std::vector<SparsePtr> files;
Daniel Zheng5769b262023-06-21 14:41:11 -0700123 if (int limit = get_sparse_limit(super_size_, fp_)) {
Daniel Zheng47d70a52023-02-14 17:44:40 +0000124 files = resparse_file(sparse_layout_.get(), limit);
125 } else {
126 files.emplace_back(std::move(sparse_layout_));
127 }
128
129 // Send the data to the device.
130 flash_partition_files(super_name_, files);
131}
Daniel Zheng1fff6902023-08-24 09:47:43 -0700132
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700133std::string OptimizedFlashSuperTask::ToString() const {
Daniel Zheng665a4602023-06-05 11:24:59 -0700134 return "optimized-flash-super";
135}
Daniel Zheng47d70a52023-02-14 17:44:40 +0000136
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700137// This looks for a block within tasks that has the following pattern [reboot fastboot,
138// update-super, $LIST_OF_DYNAMIC_FLASH_TASKS] and returns true if this is found.Theoretically
139// this check is just a pattern match and could break if fastboot-info has a bunch of junk commands
140// but all devices should pretty much follow this pattern
141bool OptimizedFlashSuperTask::CanOptimize(const ImageSource* source,
142 const std::vector<std::unique_ptr<Task>>& tasks) {
143 for (size_t i = 0; i < tasks.size(); i++) {
144 auto reboot_task = tasks[i]->AsRebootTask();
145 if (!reboot_task || reboot_task->GetTarget() != "fastboot") {
146 continue;
Daniel Zheng47d70a52023-02-14 17:44:40 +0000147 }
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700148 // The check for i >= tasks.size() - 2 is because we are peeking two tasks ahead. We need to
149 // check for an update-super && flash {dynamic_partition}
150 if (i >= tasks.size() - 2 || !tasks[i + 1]->AsUpdateSuperTask()) {
151 continue;
152 }
153 auto flash_task = tasks[i + 2]->AsFlashTask();
154 if (!FlashTask::IsDynamicParitition(source, flash_task)) {
155 continue;
156 }
157 return true;
Daniel Zheng47d70a52023-02-14 17:44:40 +0000158 }
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700159 return false;
Daniel Zheng47d70a52023-02-14 17:44:40 +0000160}
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000161
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700162std::unique_ptr<OptimizedFlashSuperTask> OptimizedFlashSuperTask::Initialize(
Daniel Zheng29a211c2023-03-10 07:23:49 +0000163 const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks) {
Daniel Zhengd62002c2023-06-21 14:27:35 -0700164 if (!fp->should_optimize_flash_super) {
165 LOG(INFO) << "super optimization is disabled";
166 return nullptr;
167 }
Daniel Zheng1fff6902023-08-24 09:47:43 -0700168 if (!supports_AB(fp->fb)) {
Daniel Zheng29a211c2023-03-10 07:23:49 +0000169 LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device";
170 return nullptr;
171 }
172 if (fp->slot_override == "all") {
173 LOG(VERBOSE) << "Cannot optimize flashing super for all slots";
174 return nullptr;
175 }
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700176 if (!CanOptimize(fp->source, tasks)) {
177 return nullptr;
178 }
Daniel Zheng29a211c2023-03-10 07:23:49 +0000179
180 // Does this device use dynamic partitions at all?
181 unique_fd fd = fp->source->OpenFile("super_empty.img");
182
183 if (fd < 0) {
184 LOG(VERBOSE) << "could not open super_empty.img";
185 return nullptr;
186 }
187
188 std::string super_name;
189 // Try to find whether there is a super partition.
190 if (fp->fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) {
191 super_name = "super";
192 }
193 uint64_t partition_size;
194 std::string partition_size_str;
195 if (fp->fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) {
196 LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition";
197 return nullptr;
198 }
199 partition_size_str = fb_fix_numeric_var(partition_size_str);
200 if (!android::base::ParseUint(partition_size_str, &partition_size)) {
201 LOG(VERBOSE) << "Could not parse " << super_name << " size: " << partition_size_str;
202 return nullptr;
203 }
204
205 std::unique_ptr<SuperFlashHelper> helper = std::make_unique<SuperFlashHelper>(*fp->source);
206 if (!helper->Open(fd)) {
207 return nullptr;
208 }
209
210 for (const auto& task : tasks) {
211 if (auto flash_task = task->AsFlashTask()) {
Daniel Zheng2775df62023-05-25 15:14:59 -0700212 auto partition = flash_task->GetPartitionAndSlot();
213 if (!helper->AddPartition(partition, flash_task->GetImageName(), false)) {
214 return nullptr;
Daniel Zheng29a211c2023-03-10 07:23:49 +0000215 }
216 }
217 }
218
219 auto s = helper->GetSparseLayout();
220 if (!s) return nullptr;
Daniel Zheng1fff6902023-08-24 09:47:43 -0700221
222 // Remove tasks that are concatenated into this optimized task
Daniel Zheng29a211c2023-03-10 07:23:49 +0000223 auto remove_if_callback = [&](const auto& task) -> bool {
224 if (auto flash_task = task->AsFlashTask()) {
225 return helper->WillFlash(flash_task->GetPartitionAndSlot());
226 } else if (auto update_super_task = task->AsUpdateSuperTask()) {
227 return true;
228 } else if (auto reboot_task = task->AsRebootTask()) {
Daniel Zheng1fff6902023-08-24 09:47:43 -0700229 if (reboot_task->GetTarget() == "fastboot") {
230 return true;
231 }
Daniel Zheng29a211c2023-03-10 07:23:49 +0000232 }
233 return false;
234 };
Daniel Zheng1fff6902023-08-24 09:47:43 -0700235
Daniel Zheng29a211c2023-03-10 07:23:49 +0000236 tasks.erase(std::remove_if(tasks.begin(), tasks.end(), remove_if_callback), tasks.end());
237
Daniel Zheng3e048572023-06-21 15:21:06 -0700238 return std::make_unique<OptimizedFlashSuperTask>(super_name, std::move(helper), std::move(s),
239 partition_size, fp);
Daniel Zheng29a211c2023-03-10 07:23:49 +0000240}
241
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700242UpdateSuperTask::UpdateSuperTask(const FlashingPlan* fp) : fp_(fp) {}
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000243
244void UpdateSuperTask::Run() {
245 unique_fd fd = fp_->source->OpenFile("super_empty.img");
246 if (fd < 0) {
247 return;
248 }
249 if (!is_userspace_fastboot()) {
250 reboot_to_userspace_fastboot();
251 }
252
253 std::string super_name;
254 if (fp_->fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
255 super_name = "super";
256 }
257 fp_->fb->Download(super_name, fd, get_file_size(fd));
258
259 std::string command = "update-super:" + super_name;
260 if (fp_->wants_wipe) {
261 command += ":wipe";
262 }
263 fp_->fb->RawCommand(command, "Updating super partition");
264}
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700265std::string UpdateSuperTask::ToString() const {
Daniel Zheng665a4602023-06-05 11:24:59 -0700266 return "update-super";
267}
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000268
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700269ResizeTask::ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size,
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000270 const std::string& slot)
271 : fp_(fp), pname_(pname), size_(size), slot_(slot) {}
272
273void ResizeTask::Run() {
274 auto resize_partition = [this](const std::string& partition) -> void {
275 if (is_logical(partition)) {
276 fp_->fb->ResizePartition(partition, size_);
277 }
278 };
279 do_for_partitions(pname_, slot_, resize_partition, false);
280}
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000281
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700282std::string ResizeTask::ToString() const {
Daniel Zheng665a4602023-06-05 11:24:59 -0700283 return "resize " + pname_;
284}
285
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700286DeleteTask::DeleteTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000287
288void DeleteTask::Run() {
289 fp_->fb->DeletePartition(pname_);
Daniel Zheng15e77832023-03-13 17:09:43 +0000290}
291
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700292std::string DeleteTask::ToString() const {
Daniel Zheng665a4602023-06-05 11:24:59 -0700293 return "delete " + pname_;
294}
295
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700296WipeTask::WipeTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
Daniel Zheng15e77832023-03-13 17:09:43 +0000297
298void WipeTask::Run() {
299 std::string partition_type;
300 if (fp_->fb->GetVar("partition-type:" + pname_, &partition_type) != fastboot::SUCCESS) {
Daniel Zheng6f213b22023-03-28 22:47:04 +0000301 LOG(ERROR) << "wipe task partition not found: " << pname_;
Daniel Zheng15e77832023-03-13 17:09:43 +0000302 return;
303 }
304 if (partition_type.empty()) return;
Daniel Zheng6f213b22023-03-28 22:47:04 +0000305 if (fp_->fb->Erase(pname_) != fastboot::SUCCESS) {
306 LOG(ERROR) << "wipe task erase failed with partition: " << pname_;
307 return;
308 }
Daniel Zheng5769b262023-06-21 14:41:11 -0700309 fb_perform_format(pname_, 1, partition_type, "", fp_->fs_options, fp_);
Daniel Zheng15e77832023-03-13 17:09:43 +0000310}
Daniel Zheng665a4602023-06-05 11:24:59 -0700311
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700312std::string WipeTask::ToString() const {
Daniel Zheng665a4602023-06-05 11:24:59 -0700313 return "erase " + pname_;
314}