Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 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" |
Daniel Zheng | 7627b54 | 2023-04-13 13:43:17 -0700 | [diff] [blame] | 17 | |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 18 | #include <iostream> |
Daniel Zheng | 7627b54 | 2023-04-13 13:43:17 -0700 | [diff] [blame] | 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android-base/parseint.h> |
| 22 | |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 23 | #include "fastboot.h" |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 24 | #include "filesystem.h" |
| 25 | #include "super_flash_helper.h" |
Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 26 | #include "util.h" |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 27 | |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 28 | using namespace std::string_literals; |
Daniel Zheng | 5a9905a | 2023-05-23 10:51:01 -0700 | [diff] [blame^] | 29 | FlashTask::FlashTask(const std::string& slot, const std::string& pname, const std::string& fname, |
Daniel Zheng | 403657d | 2023-03-10 07:37:25 +0000 | [diff] [blame] | 30 | const bool apply_vbmeta) |
Daniel Zheng | 5a9905a | 2023-05-23 10:51:01 -0700 | [diff] [blame^] | 31 | : pname_(pname), fname_(fname), slot_(slot), apply_vbmeta_(apply_vbmeta) {} |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 32 | |
| 33 | void FlashTask::Run() { |
| 34 | auto flash = [&](const std::string& partition) { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame] | 35 | if (should_flash_in_userspace(partition) && !is_userspace_fastboot()) { |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 36 | 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 Zheng | 403657d | 2023-03-10 07:37:25 +0000 | [diff] [blame] | 44 | do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_); |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 45 | }; |
| 46 | do_for_partitions(pname_, slot_, flash, true); |
| 47 | } |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 48 | |
Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 49 | std::string FlashTask::GetPartitionAndSlot() { |
| 50 | auto slot = slot_; |
| 51 | if (slot.empty()) { |
| 52 | slot = get_current_slot(); |
| 53 | } |
| 54 | if (slot.empty()) { |
| 55 | return pname_; |
| 56 | } |
| 57 | if (slot == "all") { |
| 58 | LOG(FATAL) << "Cannot retrieve a singular name when using all slots"; |
| 59 | } |
| 60 | return pname_ + "_" + slot; |
| 61 | } |
| 62 | |
Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 63 | RebootTask::RebootTask(const FlashingPlan* fp) : fp_(fp){}; |
| 64 | RebootTask::RebootTask(const FlashingPlan* fp, const std::string& reboot_target) |
Daniel Zheng | 43987c9 | 2023-03-07 18:20:53 +0000 | [diff] [blame] | 65 | : reboot_target_(reboot_target), fp_(fp){}; |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 66 | |
| 67 | void RebootTask::Run() { |
Daniel Zheng | 77af6af | 2023-04-20 14:30:28 -0700 | [diff] [blame] | 68 | if (reboot_target_ == "fastboot") { |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 69 | if (!is_userspace_fastboot()) { |
| 70 | reboot_to_userspace_fastboot(); |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame] | 71 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 72 | } |
| 73 | } else if (reboot_target_ == "recovery") { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame] | 74 | fp_->fb->RebootTo("recovery"); |
| 75 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 76 | } else if (reboot_target_ == "bootloader") { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame] | 77 | fp_->fb->RebootTo("bootloader"); |
| 78 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 79 | } else if (reboot_target_ == "") { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame] | 80 | fp_->fb->Reboot(); |
| 81 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 82 | } else { |
| 83 | syntax_error("unknown reboot target %s", reboot_target_.c_str()); |
| 84 | } |
| 85 | } |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 86 | |
| 87 | FlashSuperLayoutTask::FlashSuperLayoutTask(const std::string& super_name, |
| 88 | std::unique_ptr<SuperFlashHelper> helper, |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 89 | SparsePtr sparse_layout, uint64_t super_size) |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 90 | : super_name_(super_name), |
| 91 | helper_(std::move(helper)), |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 92 | sparse_layout_(std::move(sparse_layout)), |
| 93 | super_size_(super_size) {} |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 94 | |
| 95 | void FlashSuperLayoutTask::Run() { |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 96 | // Use the reported super partition size as the upper limit, rather than |
| 97 | // sparse_file_len, which (1) can fail and (2) is kind of expensive, since |
| 98 | // it will map in all of the embedded fds. |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 99 | std::vector<SparsePtr> files; |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 100 | if (int limit = get_sparse_limit(super_size_)) { |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 101 | files = resparse_file(sparse_layout_.get(), limit); |
| 102 | } else { |
| 103 | files.emplace_back(std::move(sparse_layout_)); |
| 104 | } |
| 105 | |
| 106 | // Send the data to the device. |
| 107 | flash_partition_files(super_name_, files); |
| 108 | } |
| 109 | |
| 110 | std::unique_ptr<FlashSuperLayoutTask> FlashSuperLayoutTask::Initialize( |
Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 111 | const FlashingPlan* fp, std::vector<ImageEntry>& os_images) { |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 112 | if (!supports_AB()) { |
| 113 | LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device"; |
| 114 | return nullptr; |
| 115 | } |
Daniel Zheng | 1e6456b | 2023-04-10 09:35:48 -0700 | [diff] [blame] | 116 | if (fp->slot_override == "all") { |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 117 | LOG(VERBOSE) << "Cannot optimize flashing super for all slots"; |
| 118 | return nullptr; |
| 119 | } |
| 120 | |
| 121 | // Does this device use dynamic partitions at all? |
| 122 | unique_fd fd = fp->source->OpenFile("super_empty.img"); |
| 123 | |
| 124 | if (fd < 0) { |
| 125 | LOG(VERBOSE) << "could not open super_empty.img"; |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | std::string super_name; |
| 130 | // Try to find whether there is a super partition. |
| 131 | if (fp->fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) { |
| 132 | super_name = "super"; |
| 133 | } |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 134 | |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 135 | uint64_t partition_size; |
| 136 | std::string partition_size_str; |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 137 | if (fp->fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) { |
| 138 | LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition"; |
| 139 | return nullptr; |
| 140 | } |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 141 | partition_size_str = fb_fix_numeric_var(partition_size_str); |
| 142 | if (!android::base::ParseUint(partition_size_str, &partition_size)) { |
| 143 | LOG(VERBOSE) << "Could not parse " << super_name << " size: " << partition_size_str; |
| 144 | return nullptr; |
| 145 | } |
| 146 | |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 147 | std::unique_ptr<SuperFlashHelper> helper = std::make_unique<SuperFlashHelper>(*fp->source); |
| 148 | if (!helper->Open(fd)) { |
| 149 | return nullptr; |
| 150 | } |
| 151 | |
| 152 | for (const auto& entry : os_images) { |
Kelvin Zhang | 2a4a45f | 2023-04-03 20:44:05 +0000 | [diff] [blame] | 153 | auto partition = GetPartitionName(entry, fp->current_slot); |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 154 | auto image = entry.first; |
| 155 | |
| 156 | if (!helper->AddPartition(partition, image->img_name, image->optional_if_no_image)) { |
| 157 | return nullptr; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | auto s = helper->GetSparseLayout(); |
| 162 | if (!s) return nullptr; |
| 163 | |
| 164 | // Remove images that we already flashed, just in case we have non-dynamic OS images. |
| 165 | auto remove_if_callback = [&](const ImageEntry& entry) -> bool { |
Kelvin Zhang | 2a4a45f | 2023-04-03 20:44:05 +0000 | [diff] [blame] | 166 | return helper->WillFlash(GetPartitionName(entry, fp->current_slot)); |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 167 | }; |
| 168 | os_images.erase(std::remove_if(os_images.begin(), os_images.end(), remove_if_callback), |
| 169 | os_images.end()); |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 170 | return std::make_unique<FlashSuperLayoutTask>(super_name, std::move(helper), std::move(s), |
| 171 | partition_size); |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 172 | } |
Daniel Zheng | 6bb8baa | 2023-03-03 07:14:23 +0000 | [diff] [blame] | 173 | |
Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 174 | std::unique_ptr<FlashSuperLayoutTask> FlashSuperLayoutTask::InitializeFromTasks( |
| 175 | const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks) { |
| 176 | if (!supports_AB()) { |
| 177 | LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device"; |
| 178 | return nullptr; |
| 179 | } |
| 180 | if (fp->slot_override == "all") { |
| 181 | LOG(VERBOSE) << "Cannot optimize flashing super for all slots"; |
| 182 | return nullptr; |
| 183 | } |
| 184 | |
| 185 | // Does this device use dynamic partitions at all? |
| 186 | unique_fd fd = fp->source->OpenFile("super_empty.img"); |
| 187 | |
| 188 | if (fd < 0) { |
| 189 | LOG(VERBOSE) << "could not open super_empty.img"; |
| 190 | return nullptr; |
| 191 | } |
| 192 | |
| 193 | std::string super_name; |
| 194 | // Try to find whether there is a super partition. |
| 195 | if (fp->fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) { |
| 196 | super_name = "super"; |
| 197 | } |
| 198 | uint64_t partition_size; |
| 199 | std::string partition_size_str; |
| 200 | if (fp->fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) { |
| 201 | LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition"; |
| 202 | return nullptr; |
| 203 | } |
| 204 | partition_size_str = fb_fix_numeric_var(partition_size_str); |
| 205 | if (!android::base::ParseUint(partition_size_str, &partition_size)) { |
| 206 | LOG(VERBOSE) << "Could not parse " << super_name << " size: " << partition_size_str; |
| 207 | return nullptr; |
| 208 | } |
| 209 | |
| 210 | std::unique_ptr<SuperFlashHelper> helper = std::make_unique<SuperFlashHelper>(*fp->source); |
| 211 | if (!helper->Open(fd)) { |
| 212 | return nullptr; |
| 213 | } |
| 214 | |
| 215 | for (const auto& task : tasks) { |
| 216 | if (auto flash_task = task->AsFlashTask()) { |
| 217 | if (should_flash_in_userspace(flash_task->GetPartitionAndSlot())) { |
| 218 | auto partition = flash_task->GetPartitionAndSlot(); |
| 219 | if (!helper->AddPartition(partition, flash_task->GetImageName(), false)) { |
| 220 | return nullptr; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | auto s = helper->GetSparseLayout(); |
| 227 | if (!s) return nullptr; |
| 228 | // Remove images that we already flashed, just in case we have non-dynamic OS images. |
| 229 | auto remove_if_callback = [&](const auto& task) -> bool { |
| 230 | if (auto flash_task = task->AsFlashTask()) { |
| 231 | return helper->WillFlash(flash_task->GetPartitionAndSlot()); |
| 232 | } else if (auto update_super_task = task->AsUpdateSuperTask()) { |
| 233 | return true; |
| 234 | } else if (auto reboot_task = task->AsRebootTask()) { |
| 235 | return true; |
| 236 | } |
| 237 | return false; |
| 238 | }; |
| 239 | tasks.erase(std::remove_if(tasks.begin(), tasks.end(), remove_if_callback), tasks.end()); |
| 240 | |
| 241 | return std::make_unique<FlashSuperLayoutTask>(super_name, std::move(helper), std::move(s), |
| 242 | partition_size); |
| 243 | } |
| 244 | |
Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 245 | UpdateSuperTask::UpdateSuperTask(const FlashingPlan* fp) : fp_(fp) {} |
Daniel Zheng | 6bb8baa | 2023-03-03 07:14:23 +0000 | [diff] [blame] | 246 | |
| 247 | void UpdateSuperTask::Run() { |
| 248 | unique_fd fd = fp_->source->OpenFile("super_empty.img"); |
| 249 | if (fd < 0) { |
| 250 | return; |
| 251 | } |
| 252 | if (!is_userspace_fastboot()) { |
| 253 | reboot_to_userspace_fastboot(); |
| 254 | } |
| 255 | |
| 256 | std::string super_name; |
| 257 | if (fp_->fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) { |
| 258 | super_name = "super"; |
| 259 | } |
| 260 | fp_->fb->Download(super_name, fd, get_file_size(fd)); |
| 261 | |
| 262 | std::string command = "update-super:" + super_name; |
| 263 | if (fp_->wants_wipe) { |
| 264 | command += ":wipe"; |
| 265 | } |
| 266 | fp_->fb->RawCommand(command, "Updating super partition"); |
| 267 | } |
Daniel Zheng | 9f7bf7e | 2023-03-03 07:16:46 +0000 | [diff] [blame] | 268 | |
Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 269 | ResizeTask::ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size, |
Daniel Zheng | 9f7bf7e | 2023-03-03 07:16:46 +0000 | [diff] [blame] | 270 | const std::string& slot) |
| 271 | : fp_(fp), pname_(pname), size_(size), slot_(slot) {} |
| 272 | |
| 273 | void 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 Zheng | aa70f4c | 2023-03-07 18:59:51 +0000 | [diff] [blame] | 281 | |
Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 282 | DeleteTask::DeleteTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){}; |
Daniel Zheng | aa70f4c | 2023-03-07 18:59:51 +0000 | [diff] [blame] | 283 | |
| 284 | void DeleteTask::Run() { |
| 285 | fp_->fb->DeletePartition(pname_); |
Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 288 | WipeTask::WipeTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){}; |
Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 289 | |
| 290 | void WipeTask::Run() { |
| 291 | std::string partition_type; |
| 292 | if (fp_->fb->GetVar("partition-type:" + pname_, &partition_type) != fastboot::SUCCESS) { |
Daniel Zheng | 6f213b2 | 2023-03-28 22:47:04 +0000 | [diff] [blame] | 293 | LOG(ERROR) << "wipe task partition not found: " << pname_; |
Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | if (partition_type.empty()) return; |
Daniel Zheng | 6f213b2 | 2023-03-28 22:47:04 +0000 | [diff] [blame] | 297 | if (fp_->fb->Erase(pname_) != fastboot::SUCCESS) { |
| 298 | LOG(ERROR) << "wipe task erase failed with partition: " << pname_; |
| 299 | return; |
| 300 | } |
Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 301 | fb_perform_format(pname_, 1, partition_type, "", fp_->fs_options); |
| 302 | } |