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