blob: 29befb7104d80df70ca70abcbfaa636cef7f9cdd [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
Daniel Zheng47d70a52023-02-14 17:44:40 +000022using namespace std::string_literals;
Daniel Zheng403657d2023-03-10 07:37:25 +000023FlashTask::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 Zheng0d307182023-01-31 21:07:53 +000026
27void FlashTask::Run() {
28 auto flash = [&](const std::string& partition) {
Daniel Zhengbc01da52023-02-23 03:25:52 +000029 if (should_flash_in_userspace(partition) && !is_userspace_fastboot()) {
Daniel Zheng0d307182023-01-31 21:07:53 +000030 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 Zheng403657d2023-03-10 07:37:25 +000038 do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_);
Daniel Zheng0d307182023-01-31 21:07:53 +000039 };
40 do_for_partitions(pname_, slot_, flash, true);
41}
Daniel Zheng71b3b432023-01-30 17:43:00 +000042
Daniel Zheng43987c92023-03-07 18:20:53 +000043RebootTask::RebootTask(FlashingPlan* fp) : fp_(fp){};
44RebootTask::RebootTask(FlashingPlan* fp, const std::string& reboot_target)
45 : reboot_target_(reboot_target), fp_(fp){};
Daniel Zheng71b3b432023-01-30 17:43:00 +000046
47void RebootTask::Run() {
48 if ((reboot_target_ == "userspace" || reboot_target_ == "fastboot")) {
49 if (!is_userspace_fastboot()) {
50 reboot_to_userspace_fastboot();
Daniel Zhengbc01da52023-02-23 03:25:52 +000051 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000052 }
53 } else if (reboot_target_ == "recovery") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000054 fp_->fb->RebootTo("recovery");
55 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000056 } else if (reboot_target_ == "bootloader") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000057 fp_->fb->RebootTo("bootloader");
58 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000059 } else if (reboot_target_ == "") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000060 fp_->fb->Reboot();
61 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000062 } else {
63 syntax_error("unknown reboot target %s", reboot_target_.c_str());
64 }
65}
Daniel Zheng47d70a52023-02-14 17:44:40 +000066
67FlashSuperLayoutTask::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
74void 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
86std::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 Zheng6bb8baa2023-03-03 07:14:23 +0000141
Daniel Zheng15e77832023-03-13 17:09:43 +0000142UpdateSuperTask::UpdateSuperTask(FlashingPlan* fp) : fp_(fp) {}
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000143
144void 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 Zheng9f7bf7e2023-03-03 07:16:46 +0000165
166ResizeTask::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
170void 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 Zhengaa70f4c2023-03-07 18:59:51 +0000178
179DeleteTask::DeleteTask(FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
180
181void DeleteTask::Run() {
182 fp_->fb->DeletePartition(pname_);
Daniel Zheng15e77832023-03-13 17:09:43 +0000183}
184
185WipeTask::WipeTask(FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
186
187void 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}