blob: 6233c900f3b630f1f960e85d5a262703461c172d [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 Zheng0d307182023-01-31 21:07:53 +000023
Daniel Zheng43987c92023-03-07 18:20:53 +000024FlashTask::FlashTask(const std::string& slot, const std::string& pname)
25 : pname_(pname), fname_(find_item(pname)), slot_(slot) {
Daniel Zheng0d307182023-01-31 21:07:53 +000026 if (fname_.empty()) die("cannot determine image filename for '%s'", pname_.c_str());
27}
Daniel Zhengbc01da52023-02-23 03:25:52 +000028FlashTask::FlashTask(const std::string& _slot, const std::string& _pname, const std::string& _fname)
29 : pname_(_pname), fname_(_fname), slot_(_slot) {}
Daniel Zheng0d307182023-01-31 21:07:53 +000030
31void FlashTask::Run() {
32 auto flash = [&](const std::string& partition) {
Daniel Zhengbc01da52023-02-23 03:25:52 +000033 if (should_flash_in_userspace(partition) && !is_userspace_fastboot()) {
Daniel Zheng0d307182023-01-31 21:07:53 +000034 die("The partition you are trying to flash is dynamic, and "
35 "should be flashed via fastbootd. Please run:\n"
36 "\n"
37 " fastboot reboot fastboot\n"
38 "\n"
39 "And try again. If you are intentionally trying to "
40 "overwrite a fixed partition, use --force.");
41 }
42 do_flash(partition.c_str(), fname_.c_str());
43 };
44 do_for_partitions(pname_, slot_, flash, true);
45}
Daniel Zheng71b3b432023-01-30 17:43:00 +000046
Daniel Zheng43987c92023-03-07 18:20:53 +000047RebootTask::RebootTask(FlashingPlan* fp) : fp_(fp){};
48RebootTask::RebootTask(FlashingPlan* fp, const std::string& reboot_target)
49 : reboot_target_(reboot_target), fp_(fp){};
Daniel Zheng71b3b432023-01-30 17:43:00 +000050
51void RebootTask::Run() {
52 if ((reboot_target_ == "userspace" || reboot_target_ == "fastboot")) {
53 if (!is_userspace_fastboot()) {
54 reboot_to_userspace_fastboot();
Daniel Zhengbc01da52023-02-23 03:25:52 +000055 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000056 }
57 } else if (reboot_target_ == "recovery") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000058 fp_->fb->RebootTo("recovery");
59 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000060 } else if (reboot_target_ == "bootloader") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000061 fp_->fb->RebootTo("bootloader");
62 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000063 } else if (reboot_target_ == "") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000064 fp_->fb->Reboot();
65 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000066 } else {
67 syntax_error("unknown reboot target %s", reboot_target_.c_str());
68 }
69}
Daniel Zheng47d70a52023-02-14 17:44:40 +000070
71FlashSuperLayoutTask::FlashSuperLayoutTask(const std::string& super_name,
72 std::unique_ptr<SuperFlashHelper> helper,
73 SparsePtr sparse_layout)
74 : super_name_(super_name),
75 helper_(std::move(helper)),
76 sparse_layout_(std::move(sparse_layout)) {}
77
78void FlashSuperLayoutTask::Run() {
79 std::vector<SparsePtr> files;
80 if (int limit = get_sparse_limit(sparse_file_len(sparse_layout_.get(), false, false))) {
81 files = resparse_file(sparse_layout_.get(), limit);
82 } else {
83 files.emplace_back(std::move(sparse_layout_));
84 }
85
86 // Send the data to the device.
87 flash_partition_files(super_name_, files);
88}
89
90std::unique_ptr<FlashSuperLayoutTask> FlashSuperLayoutTask::Initialize(
91 FlashingPlan* fp, std::vector<ImageEntry>& os_images) {
92 if (!supports_AB()) {
93 LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device";
94 return nullptr;
95 }
96 if (fp->slot == "all") {
97 LOG(VERBOSE) << "Cannot optimize flashing super for all slots";
98 return nullptr;
99 }
100
101 // Does this device use dynamic partitions at all?
102 unique_fd fd = fp->source->OpenFile("super_empty.img");
103
104 if (fd < 0) {
105 LOG(VERBOSE) << "could not open super_empty.img";
106 return nullptr;
107 }
108
109 std::string super_name;
110 // Try to find whether there is a super partition.
111 if (fp->fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) {
112 super_name = "super";
113 }
114 std::string partition_size_str;
115
116 if (fp->fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) {
117 LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition";
118 return nullptr;
119 }
120 std::unique_ptr<SuperFlashHelper> helper = std::make_unique<SuperFlashHelper>(*fp->source);
121 if (!helper->Open(fd)) {
122 return nullptr;
123 }
124
125 for (const auto& entry : os_images) {
126 auto partition = GetPartitionName(entry, fp->current_slot);
127 auto image = entry.first;
128
129 if (!helper->AddPartition(partition, image->img_name, image->optional_if_no_image)) {
130 return nullptr;
131 }
132 }
133
134 auto s = helper->GetSparseLayout();
135 if (!s) return nullptr;
136
137 // Remove images that we already flashed, just in case we have non-dynamic OS images.
138 auto remove_if_callback = [&](const ImageEntry& entry) -> bool {
139 return helper->WillFlash(GetPartitionName(entry, fp->current_slot));
140 };
141 os_images.erase(std::remove_if(os_images.begin(), os_images.end(), remove_if_callback),
142 os_images.end());
143 return std::make_unique<FlashSuperLayoutTask>(super_name, std::move(helper), std::move(s));
144}
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000145
146UpdateSuperTask::UpdateSuperTask(FlashingPlan* fp)
147 : fp_(fp) {}
148
149void UpdateSuperTask::Run() {
150 unique_fd fd = fp_->source->OpenFile("super_empty.img");
151 if (fd < 0) {
152 return;
153 }
154 if (!is_userspace_fastboot()) {
155 reboot_to_userspace_fastboot();
156 }
157
158 std::string super_name;
159 if (fp_->fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
160 super_name = "super";
161 }
162 fp_->fb->Download(super_name, fd, get_file_size(fd));
163
164 std::string command = "update-super:" + super_name;
165 if (fp_->wants_wipe) {
166 command += ":wipe";
167 }
168 fp_->fb->RawCommand(command, "Updating super partition");
169}
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000170
171ResizeTask::ResizeTask(FlashingPlan* fp, const std::string& pname, const std::string& size,
172 const std::string& slot)
173 : fp_(fp), pname_(pname), size_(size), slot_(slot) {}
174
175void ResizeTask::Run() {
176 auto resize_partition = [this](const std::string& partition) -> void {
177 if (is_logical(partition)) {
178 fp_->fb->ResizePartition(partition, size_);
179 }
180 };
181 do_for_partitions(pname_, slot_, resize_partition, false);
182}
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000183
184DeleteTask::DeleteTask(FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
185
186void DeleteTask::Run() {
187 fp_->fb->DeletePartition(pname_);
188}