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 | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 26 | |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 27 | using namespace std::string_literals; |
Daniel Zheng | 403657d | 2023-03-10 07:37:25 +0000 | [diff] [blame] | 28 | FlashTask::FlashTask(const std::string& _slot, const std::string& _pname, const std::string& _fname, |
| 29 | const bool apply_vbmeta) |
| 30 | : pname_(_pname), fname_(_fname), slot_(_slot), apply_vbmeta_(apply_vbmeta) {} |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 31 | |
| 32 | void FlashTask::Run() { |
| 33 | auto flash = [&](const std::string& partition) { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame] | 34 | if (should_flash_in_userspace(partition) && !is_userspace_fastboot()) { |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 35 | die("The partition you are trying to flash is dynamic, and " |
| 36 | "should be flashed via fastbootd. Please run:\n" |
| 37 | "\n" |
| 38 | " fastboot reboot fastboot\n" |
| 39 | "\n" |
| 40 | "And try again. If you are intentionally trying to " |
| 41 | "overwrite a fixed partition, use --force."); |
| 42 | } |
Daniel Zheng | 403657d | 2023-03-10 07:37:25 +0000 | [diff] [blame] | 43 | do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_); |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 44 | }; |
| 45 | do_for_partitions(pname_, slot_, flash, true); |
| 46 | } |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 47 | |
Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 48 | RebootTask::RebootTask(const FlashingPlan* fp) : fp_(fp){}; |
| 49 | RebootTask::RebootTask(const FlashingPlan* fp, const std::string& reboot_target) |
Daniel Zheng | 43987c9 | 2023-03-07 18:20:53 +0000 | [diff] [blame] | 50 | : reboot_target_(reboot_target), fp_(fp){}; |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 51 | |
| 52 | void RebootTask::Run() { |
| 53 | if ((reboot_target_ == "userspace" || reboot_target_ == "fastboot")) { |
| 54 | if (!is_userspace_fastboot()) { |
| 55 | reboot_to_userspace_fastboot(); |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame] | 56 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 57 | } |
| 58 | } else if (reboot_target_ == "recovery") { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame] | 59 | fp_->fb->RebootTo("recovery"); |
| 60 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 61 | } else if (reboot_target_ == "bootloader") { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame] | 62 | fp_->fb->RebootTo("bootloader"); |
| 63 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 64 | } else if (reboot_target_ == "") { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame] | 65 | fp_->fb->Reboot(); |
| 66 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 67 | } else { |
| 68 | syntax_error("unknown reboot target %s", reboot_target_.c_str()); |
| 69 | } |
| 70 | } |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 71 | |
| 72 | FlashSuperLayoutTask::FlashSuperLayoutTask(const std::string& super_name, |
| 73 | std::unique_ptr<SuperFlashHelper> helper, |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 74 | SparsePtr sparse_layout, uint64_t super_size) |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 75 | : super_name_(super_name), |
| 76 | helper_(std::move(helper)), |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 77 | sparse_layout_(std::move(sparse_layout)), |
| 78 | super_size_(super_size) {} |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 79 | |
| 80 | void FlashSuperLayoutTask::Run() { |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 81 | // Use the reported super partition size as the upper limit, rather than |
| 82 | // sparse_file_len, which (1) can fail and (2) is kind of expensive, since |
| 83 | // it will map in all of the embedded fds. |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 84 | std::vector<SparsePtr> files; |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 85 | if (int limit = get_sparse_limit(super_size_)) { |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 86 | files = resparse_file(sparse_layout_.get(), limit); |
| 87 | } else { |
| 88 | files.emplace_back(std::move(sparse_layout_)); |
| 89 | } |
| 90 | |
| 91 | // Send the data to the device. |
| 92 | flash_partition_files(super_name_, files); |
| 93 | } |
| 94 | |
| 95 | std::unique_ptr<FlashSuperLayoutTask> FlashSuperLayoutTask::Initialize( |
| 96 | FlashingPlan* fp, std::vector<ImageEntry>& os_images) { |
| 97 | if (!supports_AB()) { |
| 98 | LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device"; |
| 99 | return nullptr; |
| 100 | } |
Daniel Zheng | 1e6456b | 2023-04-10 09:35:48 -0700 | [diff] [blame] | 101 | if (fp->slot_override == "all") { |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 102 | LOG(VERBOSE) << "Cannot optimize flashing super for all slots"; |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | // Does this device use dynamic partitions at all? |
| 107 | unique_fd fd = fp->source->OpenFile("super_empty.img"); |
| 108 | |
| 109 | if (fd < 0) { |
| 110 | LOG(VERBOSE) << "could not open super_empty.img"; |
| 111 | return nullptr; |
| 112 | } |
| 113 | |
| 114 | std::string super_name; |
| 115 | // Try to find whether there is a super partition. |
| 116 | if (fp->fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) { |
| 117 | super_name = "super"; |
| 118 | } |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 119 | |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 120 | uint64_t partition_size; |
| 121 | std::string partition_size_str; |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 122 | if (fp->fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) { |
| 123 | LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition"; |
| 124 | return nullptr; |
| 125 | } |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 126 | partition_size_str = fb_fix_numeric_var(partition_size_str); |
| 127 | if (!android::base::ParseUint(partition_size_str, &partition_size)) { |
| 128 | LOG(VERBOSE) << "Could not parse " << super_name << " size: " << partition_size_str; |
| 129 | return nullptr; |
| 130 | } |
| 131 | |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 132 | std::unique_ptr<SuperFlashHelper> helper = std::make_unique<SuperFlashHelper>(*fp->source); |
| 133 | if (!helper->Open(fd)) { |
| 134 | return nullptr; |
| 135 | } |
| 136 | |
| 137 | for (const auto& entry : os_images) { |
Kelvin Zhang | 2a4a45f | 2023-04-03 20:44:05 +0000 | [diff] [blame] | 138 | auto partition = GetPartitionName(entry, fp->current_slot); |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 139 | auto image = entry.first; |
| 140 | |
| 141 | if (!helper->AddPartition(partition, image->img_name, image->optional_if_no_image)) { |
| 142 | return nullptr; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | auto s = helper->GetSparseLayout(); |
| 147 | if (!s) return nullptr; |
| 148 | |
| 149 | // Remove images that we already flashed, just in case we have non-dynamic OS images. |
| 150 | auto remove_if_callback = [&](const ImageEntry& entry) -> bool { |
Kelvin Zhang | 2a4a45f | 2023-04-03 20:44:05 +0000 | [diff] [blame] | 151 | return helper->WillFlash(GetPartitionName(entry, fp->current_slot)); |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 152 | }; |
| 153 | os_images.erase(std::remove_if(os_images.begin(), os_images.end(), remove_if_callback), |
| 154 | os_images.end()); |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 155 | return std::make_unique<FlashSuperLayoutTask>(super_name, std::move(helper), std::move(s), |
| 156 | partition_size); |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 157 | } |
Daniel Zheng | 6bb8baa | 2023-03-03 07:14:23 +0000 | [diff] [blame] | 158 | |
Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 159 | UpdateSuperTask::UpdateSuperTask(const FlashingPlan* fp) : fp_(fp) {} |
Daniel Zheng | 6bb8baa | 2023-03-03 07:14:23 +0000 | [diff] [blame] | 160 | |
| 161 | void UpdateSuperTask::Run() { |
| 162 | unique_fd fd = fp_->source->OpenFile("super_empty.img"); |
| 163 | if (fd < 0) { |
| 164 | return; |
| 165 | } |
| 166 | if (!is_userspace_fastboot()) { |
| 167 | reboot_to_userspace_fastboot(); |
| 168 | } |
| 169 | |
| 170 | std::string super_name; |
| 171 | if (fp_->fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) { |
| 172 | super_name = "super"; |
| 173 | } |
| 174 | fp_->fb->Download(super_name, fd, get_file_size(fd)); |
| 175 | |
| 176 | std::string command = "update-super:" + super_name; |
| 177 | if (fp_->wants_wipe) { |
| 178 | command += ":wipe"; |
| 179 | } |
| 180 | fp_->fb->RawCommand(command, "Updating super partition"); |
| 181 | } |
Daniel Zheng | 9f7bf7e | 2023-03-03 07:16:46 +0000 | [diff] [blame] | 182 | |
Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 183 | 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] | 184 | const std::string& slot) |
| 185 | : fp_(fp), pname_(pname), size_(size), slot_(slot) {} |
| 186 | |
| 187 | void ResizeTask::Run() { |
| 188 | auto resize_partition = [this](const std::string& partition) -> void { |
| 189 | if (is_logical(partition)) { |
| 190 | fp_->fb->ResizePartition(partition, size_); |
| 191 | } |
| 192 | }; |
| 193 | do_for_partitions(pname_, slot_, resize_partition, false); |
| 194 | } |
Daniel Zheng | aa70f4c | 2023-03-07 18:59:51 +0000 | [diff] [blame] | 195 | |
Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 196 | 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] | 197 | |
| 198 | void DeleteTask::Run() { |
| 199 | fp_->fb->DeletePartition(pname_); |
Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 202 | 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] | 203 | |
| 204 | void WipeTask::Run() { |
| 205 | std::string partition_type; |
| 206 | if (fp_->fb->GetVar("partition-type:" + pname_, &partition_type) != fastboot::SUCCESS) { |
Daniel Zheng | 6f213b2 | 2023-03-28 22:47:04 +0000 | [diff] [blame] | 207 | LOG(ERROR) << "wipe task partition not found: " << pname_; |
Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | if (partition_type.empty()) return; |
Daniel Zheng | 6f213b2 | 2023-03-28 22:47:04 +0000 | [diff] [blame] | 211 | if (fp_->fb->Erase(pname_) != fastboot::SUCCESS) { |
| 212 | LOG(ERROR) << "wipe task erase failed with partition: " << pname_; |
| 213 | return; |
| 214 | } |
Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 215 | fb_perform_format(pname_, 1, partition_type, "", fp_->fs_options); |
| 216 | } |