1 //
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"
17
18 #include "fastboot_driver.h"
19
20 #include <android-base/logging.h>
21 #include <android-base/parseint.h>
22
23 #include "fastboot.h"
24 #include "filesystem.h"
25 #include "super_flash_helper.h"
26 #include "util.h"
27
28 using namespace std::string_literals;
FlashTask(const std::string & slot,const std::string & pname,const std::string & fname,const bool apply_vbmeta,const FlashingPlan * fp)29 FlashTask::FlashTask(const std::string& slot, const std::string& pname, const std::string& fname,
30 const bool apply_vbmeta, const FlashingPlan* fp)
31 : pname_(pname), fname_(fname), slot_(slot), apply_vbmeta_(apply_vbmeta), fp_(fp) {}
32
IsDynamicPartition(const ImageSource * source,const FlashTask * task)33 bool FlashTask::IsDynamicPartition(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
Run()42 void FlashTask::Run() {
43 auto flash = [&](const std::string& partition) {
44 if (should_flash_in_userspace(fp_->source.get(), partition) && !is_userspace_fastboot() &&
45 !fp_->force_flash) {
46 die("The partition you are trying to flash is dynamic, and "
47 "should be flashed via fastbootd. Please run:\n"
48 "\n"
49 " fastboot reboot fastboot\n"
50 "\n"
51 "And try again. If you are intentionally trying to "
52 "overwrite a fixed partition, use --force.");
53 }
54 do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_, fp_);
55 };
56 do_for_partitions(pname_, slot_, flash, true);
57 }
58
ToString() const59 std::string FlashTask::ToString() const {
60 std::string apply_vbmeta_string = "";
61 if (apply_vbmeta_) {
62 apply_vbmeta_string = " --apply_vbmeta";
63 }
64 return "flash" + apply_vbmeta_string + " " + pname_ + " " + fname_;
65 }
66
GetPartitionAndSlot() const67 std::string FlashTask::GetPartitionAndSlot() const {
68 auto slot = slot_;
69 if (slot.empty()) {
70 slot = get_current_slot();
71 }
72 if (slot.empty()) {
73 return pname_;
74 }
75 if (slot == "all") {
76 LOG(FATAL) << "Cannot retrieve a singular name when using all slots";
77 }
78 return pname_ + "_" + slot;
79 }
80
RebootTask(const FlashingPlan * fp)81 RebootTask::RebootTask(const FlashingPlan* fp) : fp_(fp){};
RebootTask(const FlashingPlan * fp,const std::string & reboot_target)82 RebootTask::RebootTask(const FlashingPlan* fp, const std::string& reboot_target)
83 : reboot_target_(reboot_target), fp_(fp){};
84
Run()85 void RebootTask::Run() {
86 if (reboot_target_ == "fastboot") {
87 if (!is_userspace_fastboot()) {
88 reboot_to_userspace_fastboot();
89 fp_->fb->WaitForDisconnect();
90 }
91 } else if (reboot_target_ == "recovery") {
92 fp_->fb->RebootTo("recovery");
93 fp_->fb->WaitForDisconnect();
94 } else if (reboot_target_ == "bootloader") {
95 fp_->fb->RebootTo("bootloader");
96 fp_->fb->WaitForDisconnect();
97 } else if (reboot_target_ == "") {
98 fp_->fb->Reboot();
99 fp_->fb->WaitForDisconnect();
100 } else {
101 syntax_error("unknown reboot target %s", reboot_target_.c_str());
102 }
103 }
104
ToString() const105 std::string RebootTask::ToString() const {
106 return "reboot " + reboot_target_;
107 }
108
OptimizedFlashSuperTask(const std::string & super_name,std::unique_ptr<SuperFlashHelper> helper,SparsePtr sparse_layout,uint64_t super_size,const FlashingPlan * fp)109 OptimizedFlashSuperTask::OptimizedFlashSuperTask(const std::string& super_name,
110 std::unique_ptr<SuperFlashHelper> helper,
111 SparsePtr sparse_layout, uint64_t super_size,
112 const FlashingPlan* fp)
113 : super_name_(super_name),
114 helper_(std::move(helper)),
115 sparse_layout_(std::move(sparse_layout)),
116 super_size_(super_size),
117 fp_(fp) {}
118
Run()119 void OptimizedFlashSuperTask::Run() {
120 // Use the reported super partition size as the upper limit, rather than
121 // sparse_file_len, which (1) can fail and (2) is kind of expensive, since
122 // it will map in all of the embedded fds.
123 std::vector<SparsePtr> files;
124 if (int limit = get_sparse_limit(super_size_, fp_)) {
125 files = resparse_file(sparse_layout_.get(), limit);
126 } else {
127 files.emplace_back(std::move(sparse_layout_));
128 }
129
130 // Send the data to the device.
131 flash_partition_files(super_name_, files);
132 }
133
ToString() const134 std::string OptimizedFlashSuperTask::ToString() const {
135 return "optimized-flash-super";
136 }
137
138 // This looks for a block within tasks that has the following pattern [reboot fastboot,
139 // update-super, $LIST_OF_DYNAMIC_FLASH_TASKS] and returns true if this is found.Theoretically
140 // this check is just a pattern match and could break if fastboot-info has a bunch of junk commands
141 // but all devices should pretty much follow this pattern
CanOptimize(const ImageSource * source,const std::vector<std::unique_ptr<Task>> & tasks)142 bool OptimizedFlashSuperTask::CanOptimize(const ImageSource* source,
143 const std::vector<std::unique_ptr<Task>>& tasks) {
144 for (size_t i = 0; i < tasks.size(); i++) {
145 auto reboot_task = tasks[i]->AsRebootTask();
146 if (!reboot_task || reboot_task->GetTarget() != "fastboot") {
147 continue;
148 }
149 // The check for i >= tasks.size() - 2 is because we are peeking two tasks ahead. We need to
150 // check for an update-super && flash {dynamic_partition}
151 if (i >= tasks.size() - 2 || !tasks[i + 1]->AsUpdateSuperTask()) {
152 continue;
153 }
154 auto flash_task = tasks[i + 2]->AsFlashTask();
155 if (!FlashTask::IsDynamicPartition(source, flash_task)) {
156 continue;
157 }
158 return true;
159 }
160 return false;
161 }
162
Initialize(const FlashingPlan * fp,std::vector<std::unique_ptr<Task>> & tasks)163 std::unique_ptr<OptimizedFlashSuperTask> OptimizedFlashSuperTask::Initialize(
164 const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks) {
165 if (!fp->should_optimize_flash_super) {
166 LOG(INFO) << "super optimization is disabled";
167 return nullptr;
168 }
169 if (!supports_AB(fp->fb)) {
170 LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device";
171 return nullptr;
172 }
173 if (fp->slot_override == "all") {
174 LOG(VERBOSE) << "Cannot optimize flashing super for all slots";
175 return nullptr;
176 }
177 if (!CanOptimize(fp->source.get(), tasks)) {
178 return nullptr;
179 }
180
181 // Does this device use dynamic partitions at all?
182 unique_fd fd = fp->source->OpenFile("super_empty.img");
183
184 if (fd < 0) {
185 LOG(VERBOSE) << "could not open super_empty.img";
186 return nullptr;
187 }
188
189 std::string super_name;
190 // Try to find whether there is a super partition.
191 if (fp->fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) {
192 super_name = "super";
193 }
194 uint64_t partition_size;
195 std::string partition_size_str;
196 if (fp->fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) {
197 LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition";
198 return nullptr;
199 }
200 partition_size_str = fb_fix_numeric_var(partition_size_str);
201 if (!android::base::ParseUint(partition_size_str, &partition_size)) {
202 LOG(VERBOSE) << "Could not parse " << super_name << " size: " << partition_size_str;
203 return nullptr;
204 }
205
206 std::unique_ptr<SuperFlashHelper> helper = std::make_unique<SuperFlashHelper>(*fp->source);
207 if (!helper->Open(fd)) {
208 return nullptr;
209 }
210
211 for (const auto& task : tasks) {
212 if (auto flash_task = task->AsFlashTask()) {
213 auto partition = flash_task->GetPartitionAndSlot();
214 if (!helper->AddPartition(partition, flash_task->GetImageName(), false)) {
215 return nullptr;
216 }
217 }
218 }
219
220 auto s = helper->GetSparseLayout();
221 if (!s) return nullptr;
222
223 // Remove tasks that are concatenated into this optimized task
224 auto remove_if_callback = [&](const auto& task) -> bool {
225 if (auto flash_task = task->AsFlashTask()) {
226 return helper->WillFlash(flash_task->GetPartitionAndSlot());
227 } else if (task->AsUpdateSuperTask()) {
228 return true;
229 } else if (auto reboot_task = task->AsRebootTask()) {
230 if (reboot_task->GetTarget() == "fastboot") {
231 return true;
232 }
233 }
234 return false;
235 };
236
237 tasks.erase(std::remove_if(tasks.begin(), tasks.end(), remove_if_callback), tasks.end());
238
239 return std::make_unique<OptimizedFlashSuperTask>(super_name, std::move(helper), std::move(s),
240 partition_size, fp);
241 }
242
UpdateSuperTask(const FlashingPlan * fp)243 UpdateSuperTask::UpdateSuperTask(const FlashingPlan* fp) : fp_(fp) {}
244
Run()245 void UpdateSuperTask::Run() {
246 unique_fd fd = fp_->source->OpenFile("super_empty.img");
247 if (fd < 0) {
248 return;
249 }
250 if (!is_userspace_fastboot()) {
251 reboot_to_userspace_fastboot();
252 }
253
254 std::string super_name;
255 if (fp_->fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
256 super_name = "super";
257 }
258 fp_->fb->Download(super_name, fd, get_file_size(fd));
259
260 std::string command = "update-super:" + super_name;
261 if (fp_->wants_wipe) {
262 command += ":wipe";
263 }
264 fp_->fb->RawCommand(command, "Updating super partition");
265 }
ToString() const266 std::string UpdateSuperTask::ToString() const {
267 return "update-super";
268 }
269
ResizeTask(const FlashingPlan * fp,const std::string & pname,const std::string & size,const std::string & slot)270 ResizeTask::ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size,
271 const std::string& slot)
272 : fp_(fp), pname_(pname), size_(size), slot_(slot) {}
273
Run()274 void ResizeTask::Run() {
275 auto resize_partition = [this](const std::string& partition) -> void {
276 if (is_logical(partition)) {
277 fp_->fb->ResizePartition(partition, size_);
278 }
279 };
280 do_for_partitions(pname_, slot_, resize_partition, false);
281 }
282
ToString() const283 std::string ResizeTask::ToString() const {
284 return "resize " + pname_;
285 }
286
DeleteTask(const FlashingPlan * fp,const std::string & pname)287 DeleteTask::DeleteTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
288
Run()289 void DeleteTask::Run() {
290 fp_->fb->DeletePartition(pname_);
291 }
292
ToString() const293 std::string DeleteTask::ToString() const {
294 return "delete " + pname_;
295 }
296
WipeTask(const FlashingPlan * fp,const std::string & pname)297 WipeTask::WipeTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
298
Run()299 void WipeTask::Run() {
300 std::string partition_type;
301 if (fp_->fb->GetVar("partition-type:" + pname_, &partition_type) != fastboot::SUCCESS) {
302 LOG(ERROR) << "wipe task partition not found: " << pname_;
303 return;
304 }
305 if (partition_type.empty()) return;
306 if (fp_->fb->Erase(pname_) != fastboot::SUCCESS) {
307 LOG(ERROR) << "wipe task erase failed with partition: " << pname_;
308 return;
309 }
310 fb_perform_format(pname_, 1, partition_type, "", fp_->fs_options, fp_);
311 }
312
ToString() const313 std::string WipeTask::ToString() const {
314 return "erase " + pname_;
315 }
316