blob: 9d4cb75b2e485673aca0444102fc0d86a6b35f98 [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 Zheng47d70a52023-02-14 17:44:40 +000017#include <iostream>
Daniel Zheng71b3b432023-01-30 17:43:00 +000018#include "fastboot.h"
Daniel Zheng47d70a52023-02-14 17:44:40 +000019#include "filesystem.h"
20#include "super_flash_helper.h"
Daniel Zheng0d307182023-01-31 21:07:53 +000021
David Anderson74c78072023-03-16 21:21:28 -070022#include <android-base/parseint.h>
23
Daniel Zheng47d70a52023-02-14 17:44:40 +000024using namespace std::string_literals;
Daniel Zheng403657d2023-03-10 07:37:25 +000025FlashTask::FlashTask(const std::string& _slot, const std::string& _pname, const std::string& _fname,
26 const bool apply_vbmeta)
27 : pname_(_pname), fname_(_fname), slot_(_slot), apply_vbmeta_(apply_vbmeta) {}
Daniel Zheng0d307182023-01-31 21:07:53 +000028
29void FlashTask::Run() {
30 auto flash = [&](const std::string& partition) {
Daniel Zhengbc01da52023-02-23 03:25:52 +000031 if (should_flash_in_userspace(partition) && !is_userspace_fastboot()) {
Daniel Zheng0d307182023-01-31 21:07:53 +000032 die("The partition you are trying to flash is dynamic, and "
33 "should be flashed via fastbootd. Please run:\n"
34 "\n"
35 " fastboot reboot fastboot\n"
36 "\n"
37 "And try again. If you are intentionally trying to "
38 "overwrite a fixed partition, use --force.");
39 }
Daniel Zheng403657d2023-03-10 07:37:25 +000040 do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_);
Daniel Zheng0d307182023-01-31 21:07:53 +000041 };
42 do_for_partitions(pname_, slot_, flash, true);
43}
Daniel Zheng71b3b432023-01-30 17:43:00 +000044
Daniel Zheng43987c92023-03-07 18:20:53 +000045RebootTask::RebootTask(FlashingPlan* fp) : fp_(fp){};
46RebootTask::RebootTask(FlashingPlan* fp, const std::string& reboot_target)
47 : reboot_target_(reboot_target), fp_(fp){};
Daniel Zheng71b3b432023-01-30 17:43:00 +000048
49void RebootTask::Run() {
50 if ((reboot_target_ == "userspace" || reboot_target_ == "fastboot")) {
51 if (!is_userspace_fastboot()) {
52 reboot_to_userspace_fastboot();
Daniel Zhengbc01da52023-02-23 03:25:52 +000053 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000054 }
55 } else if (reboot_target_ == "recovery") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000056 fp_->fb->RebootTo("recovery");
57 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000058 } else if (reboot_target_ == "bootloader") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000059 fp_->fb->RebootTo("bootloader");
60 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000061 } else if (reboot_target_ == "") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000062 fp_->fb->Reboot();
63 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000064 } else {
65 syntax_error("unknown reboot target %s", reboot_target_.c_str());
66 }
67}
Daniel Zheng47d70a52023-02-14 17:44:40 +000068
69FlashSuperLayoutTask::FlashSuperLayoutTask(const std::string& super_name,
70 std::unique_ptr<SuperFlashHelper> helper,
David Anderson74c78072023-03-16 21:21:28 -070071 SparsePtr sparse_layout, uint64_t super_size)
Daniel Zheng47d70a52023-02-14 17:44:40 +000072 : super_name_(super_name),
73 helper_(std::move(helper)),
David Anderson74c78072023-03-16 21:21:28 -070074 sparse_layout_(std::move(sparse_layout)),
75 super_size_(super_size) {}
Daniel Zheng47d70a52023-02-14 17:44:40 +000076
77void FlashSuperLayoutTask::Run() {
David Anderson74c78072023-03-16 21:21:28 -070078 // Use the reported super partition size as the upper limit, rather than
79 // sparse_file_len, which (1) can fail and (2) is kind of expensive, since
80 // it will map in all of the embedded fds.
Daniel Zheng47d70a52023-02-14 17:44:40 +000081 std::vector<SparsePtr> files;
David Anderson74c78072023-03-16 21:21:28 -070082 if (int limit = get_sparse_limit(super_size_)) {
Daniel Zheng47d70a52023-02-14 17:44:40 +000083 files = resparse_file(sparse_layout_.get(), limit);
84 } else {
85 files.emplace_back(std::move(sparse_layout_));
86 }
87
88 // Send the data to the device.
89 flash_partition_files(super_name_, files);
90}
91
92std::unique_ptr<FlashSuperLayoutTask> FlashSuperLayoutTask::Initialize(
93 FlashingPlan* fp, std::vector<ImageEntry>& os_images) {
94 if (!supports_AB()) {
95 LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device";
96 return nullptr;
97 }
98 if (fp->slot == "all") {
99 LOG(VERBOSE) << "Cannot optimize flashing super for all slots";
100 return nullptr;
101 }
102
103 // Does this device use dynamic partitions at all?
104 unique_fd fd = fp->source->OpenFile("super_empty.img");
105
106 if (fd < 0) {
107 LOG(VERBOSE) << "could not open super_empty.img";
108 return nullptr;
109 }
110
111 std::string super_name;
112 // Try to find whether there is a super partition.
113 if (fp->fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) {
114 super_name = "super";
115 }
Daniel Zheng47d70a52023-02-14 17:44:40 +0000116
David Anderson74c78072023-03-16 21:21:28 -0700117 uint64_t partition_size;
118 std::string partition_size_str;
Daniel Zheng47d70a52023-02-14 17:44:40 +0000119 if (fp->fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) {
120 LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition";
121 return nullptr;
122 }
David Anderson74c78072023-03-16 21:21:28 -0700123 partition_size_str = fb_fix_numeric_var(partition_size_str);
124 if (!android::base::ParseUint(partition_size_str, &partition_size)) {
125 LOG(VERBOSE) << "Could not parse " << super_name << " size: " << partition_size_str;
126 return nullptr;
127 }
128
Daniel Zheng47d70a52023-02-14 17:44:40 +0000129 std::unique_ptr<SuperFlashHelper> helper = std::make_unique<SuperFlashHelper>(*fp->source);
130 if (!helper->Open(fd)) {
131 return nullptr;
132 }
133
134 for (const auto& entry : os_images) {
135 auto partition = GetPartitionName(entry, fp->current_slot);
136 auto image = entry.first;
137
138 if (!helper->AddPartition(partition, image->img_name, image->optional_if_no_image)) {
139 return nullptr;
140 }
141 }
142
143 auto s = helper->GetSparseLayout();
144 if (!s) return nullptr;
145
146 // Remove images that we already flashed, just in case we have non-dynamic OS images.
147 auto remove_if_callback = [&](const ImageEntry& entry) -> bool {
148 return helper->WillFlash(GetPartitionName(entry, fp->current_slot));
149 };
150 os_images.erase(std::remove_if(os_images.begin(), os_images.end(), remove_if_callback),
151 os_images.end());
David Anderson74c78072023-03-16 21:21:28 -0700152 return std::make_unique<FlashSuperLayoutTask>(super_name, std::move(helper), std::move(s),
153 partition_size);
Daniel Zheng47d70a52023-02-14 17:44:40 +0000154}
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000155
Daniel Zheng15e77832023-03-13 17:09:43 +0000156UpdateSuperTask::UpdateSuperTask(FlashingPlan* fp) : fp_(fp) {}
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000157
158void UpdateSuperTask::Run() {
159 unique_fd fd = fp_->source->OpenFile("super_empty.img");
160 if (fd < 0) {
161 return;
162 }
163 if (!is_userspace_fastboot()) {
164 reboot_to_userspace_fastboot();
165 }
166
167 std::string super_name;
168 if (fp_->fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
169 super_name = "super";
170 }
171 fp_->fb->Download(super_name, fd, get_file_size(fd));
172
173 std::string command = "update-super:" + super_name;
174 if (fp_->wants_wipe) {
175 command += ":wipe";
176 }
177 fp_->fb->RawCommand(command, "Updating super partition");
178}
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000179
180ResizeTask::ResizeTask(FlashingPlan* fp, const std::string& pname, const std::string& size,
181 const std::string& slot)
182 : fp_(fp), pname_(pname), size_(size), slot_(slot) {}
183
184void ResizeTask::Run() {
185 auto resize_partition = [this](const std::string& partition) -> void {
186 if (is_logical(partition)) {
187 fp_->fb->ResizePartition(partition, size_);
188 }
189 };
190 do_for_partitions(pname_, slot_, resize_partition, false);
191}
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000192
193DeleteTask::DeleteTask(FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
194
195void DeleteTask::Run() {
196 fp_->fb->DeletePartition(pname_);
Daniel Zheng15e77832023-03-13 17:09:43 +0000197}
198
199WipeTask::WipeTask(FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
200
201void WipeTask::Run() {
202 std::string partition_type;
203 if (fp_->fb->GetVar("partition-type:" + pname_, &partition_type) != fastboot::SUCCESS) {
Daniel Zheng6f213b22023-03-28 22:47:04 +0000204 LOG(ERROR) << "wipe task partition not found: " << pname_;
Daniel Zheng15e77832023-03-13 17:09:43 +0000205 return;
206 }
207 if (partition_type.empty()) return;
Daniel Zheng6f213b22023-03-28 22:47:04 +0000208 if (fp_->fb->Erase(pname_) != fastboot::SUCCESS) {
209 LOG(ERROR) << "wipe task erase failed with partition: " << pname_;
210 return;
211 }
Daniel Zheng15e77832023-03-13 17:09:43 +0000212 fb_perform_format(pname_, 1, partition_type, "", fp_->fs_options);
213}