2023-01-31 22:07:53 +01:00
|
|
|
//
|
|
|
|
// Copyright (C) 2023 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
#include "task.h"
|
2023-04-13 22:43:17 +02:00
|
|
|
|
2023-08-24 18:47:43 +02:00
|
|
|
#include "fastboot_driver.h"
|
2023-04-13 22:43:17 +02:00
|
|
|
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/parseint.h>
|
|
|
|
|
2023-01-30 18:43:00 +01:00
|
|
|
#include "fastboot.h"
|
2023-02-14 18:44:40 +01:00
|
|
|
#include "filesystem.h"
|
|
|
|
#include "super_flash_helper.h"
|
2023-03-10 08:23:49 +01:00
|
|
|
#include "util.h"
|
2023-01-31 22:07:53 +01:00
|
|
|
|
2023-02-14 18:44:40 +01:00
|
|
|
using namespace std::string_literals;
|
2023-05-23 19:51:01 +02:00
|
|
|
FlashTask::FlashTask(const std::string& slot, const std::string& pname, const std::string& fname,
|
2023-05-23 19:42:39 +02:00
|
|
|
const bool apply_vbmeta, const FlashingPlan* fp)
|
|
|
|
: pname_(pname), fname_(fname), slot_(slot), apply_vbmeta_(apply_vbmeta), fp_(fp) {}
|
2023-01-31 22:07:53 +01:00
|
|
|
|
2023-12-12 20:25:40 +01:00
|
|
|
bool FlashTask::IsDynamicPartition(const ImageSource* source, const FlashTask* task) {
|
2023-08-25 00:44:56 +02:00
|
|
|
std::vector<char> contents;
|
|
|
|
if (!source->ReadFile("super_empty.img", &contents)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto metadata = android::fs_mgr::ReadFromImageBlob(contents.data(), contents.size());
|
|
|
|
return should_flash_in_userspace(*metadata.get(), task->GetPartitionAndSlot());
|
|
|
|
}
|
|
|
|
|
2023-01-31 22:07:53 +01:00
|
|
|
void FlashTask::Run() {
|
|
|
|
auto flash = [&](const std::string& partition) {
|
2023-10-03 02:02:05 +02:00
|
|
|
if (should_flash_in_userspace(fp_->source.get(), partition) && !is_userspace_fastboot() &&
|
2023-09-22 05:37:47 +02:00
|
|
|
!fp_->force_flash) {
|
2023-01-31 22:07:53 +01:00
|
|
|
die("The partition you are trying to flash is dynamic, and "
|
|
|
|
"should be flashed via fastbootd. Please run:\n"
|
|
|
|
"\n"
|
|
|
|
" fastboot reboot fastboot\n"
|
|
|
|
"\n"
|
|
|
|
"And try again. If you are intentionally trying to "
|
|
|
|
"overwrite a fixed partition, use --force.");
|
|
|
|
}
|
2023-05-23 19:42:39 +02:00
|
|
|
do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_, fp_);
|
2023-01-31 22:07:53 +01:00
|
|
|
};
|
|
|
|
do_for_partitions(pname_, slot_, flash, true);
|
|
|
|
}
|
2023-01-30 18:43:00 +01:00
|
|
|
|
2023-08-25 00:44:56 +02:00
|
|
|
std::string FlashTask::ToString() const {
|
2023-06-05 20:24:59 +02:00
|
|
|
std::string apply_vbmeta_string = "";
|
|
|
|
if (apply_vbmeta_) {
|
|
|
|
apply_vbmeta_string = " --apply_vbmeta";
|
|
|
|
}
|
|
|
|
return "flash" + apply_vbmeta_string + " " + pname_ + " " + fname_;
|
|
|
|
}
|
|
|
|
|
2023-08-25 00:44:56 +02:00
|
|
|
std::string FlashTask::GetPartitionAndSlot() const {
|
2023-03-10 08:23:49 +01:00
|
|
|
auto slot = slot_;
|
|
|
|
if (slot.empty()) {
|
|
|
|
slot = get_current_slot();
|
|
|
|
}
|
|
|
|
if (slot.empty()) {
|
|
|
|
return pname_;
|
|
|
|
}
|
|
|
|
if (slot == "all") {
|
|
|
|
LOG(FATAL) << "Cannot retrieve a singular name when using all slots";
|
|
|
|
}
|
|
|
|
return pname_ + "_" + slot;
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:53:08 +02:00
|
|
|
RebootTask::RebootTask(const FlashingPlan* fp) : fp_(fp){};
|
|
|
|
RebootTask::RebootTask(const FlashingPlan* fp, const std::string& reboot_target)
|
2023-03-07 19:20:53 +01:00
|
|
|
: reboot_target_(reboot_target), fp_(fp){};
|
2023-01-30 18:43:00 +01:00
|
|
|
|
|
|
|
void RebootTask::Run() {
|
2023-04-20 23:30:28 +02:00
|
|
|
if (reboot_target_ == "fastboot") {
|
2023-01-30 18:43:00 +01:00
|
|
|
if (!is_userspace_fastboot()) {
|
|
|
|
reboot_to_userspace_fastboot();
|
2023-02-23 04:25:52 +01:00
|
|
|
fp_->fb->WaitForDisconnect();
|
2023-01-30 18:43:00 +01:00
|
|
|
}
|
|
|
|
} else if (reboot_target_ == "recovery") {
|
2023-02-23 04:25:52 +01:00
|
|
|
fp_->fb->RebootTo("recovery");
|
|
|
|
fp_->fb->WaitForDisconnect();
|
2023-01-30 18:43:00 +01:00
|
|
|
} else if (reboot_target_ == "bootloader") {
|
2023-02-23 04:25:52 +01:00
|
|
|
fp_->fb->RebootTo("bootloader");
|
|
|
|
fp_->fb->WaitForDisconnect();
|
2023-01-30 18:43:00 +01:00
|
|
|
} else if (reboot_target_ == "") {
|
2023-02-23 04:25:52 +01:00
|
|
|
fp_->fb->Reboot();
|
|
|
|
fp_->fb->WaitForDisconnect();
|
2023-01-30 18:43:00 +01:00
|
|
|
} else {
|
|
|
|
syntax_error("unknown reboot target %s", reboot_target_.c_str());
|
|
|
|
}
|
|
|
|
}
|
2023-02-14 18:44:40 +01:00
|
|
|
|
2023-08-25 00:44:56 +02:00
|
|
|
std::string RebootTask::ToString() const {
|
2023-06-05 20:24:59 +02:00
|
|
|
return "reboot " + reboot_target_;
|
|
|
|
}
|
|
|
|
|
2023-06-22 00:21:06 +02:00
|
|
|
OptimizedFlashSuperTask::OptimizedFlashSuperTask(const std::string& super_name,
|
|
|
|
std::unique_ptr<SuperFlashHelper> helper,
|
|
|
|
SparsePtr sparse_layout, uint64_t super_size,
|
|
|
|
const FlashingPlan* fp)
|
2023-02-14 18:44:40 +01:00
|
|
|
: super_name_(super_name),
|
|
|
|
helper_(std::move(helper)),
|
2023-03-17 05:21:28 +01:00
|
|
|
sparse_layout_(std::move(sparse_layout)),
|
2023-06-21 23:41:11 +02:00
|
|
|
super_size_(super_size),
|
|
|
|
fp_(fp) {}
|
2023-02-14 18:44:40 +01:00
|
|
|
|
2023-06-22 00:21:06 +02:00
|
|
|
void OptimizedFlashSuperTask::Run() {
|
2023-03-17 05:21:28 +01:00
|
|
|
// Use the reported super partition size as the upper limit, rather than
|
|
|
|
// sparse_file_len, which (1) can fail and (2) is kind of expensive, since
|
|
|
|
// it will map in all of the embedded fds.
|
2023-02-14 18:44:40 +01:00
|
|
|
std::vector<SparsePtr> files;
|
2023-06-21 23:41:11 +02:00
|
|
|
if (int limit = get_sparse_limit(super_size_, fp_)) {
|
2023-02-14 18:44:40 +01:00
|
|
|
files = resparse_file(sparse_layout_.get(), limit);
|
|
|
|
} else {
|
|
|
|
files.emplace_back(std::move(sparse_layout_));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the data to the device.
|
|
|
|
flash_partition_files(super_name_, files);
|
|
|
|
}
|
2023-08-24 18:47:43 +02:00
|
|
|
|
2023-08-25 00:44:56 +02:00
|
|
|
std::string OptimizedFlashSuperTask::ToString() const {
|
2023-06-05 20:24:59 +02:00
|
|
|
return "optimized-flash-super";
|
|
|
|
}
|
2023-02-14 18:44:40 +01:00
|
|
|
|
2023-08-25 00:44:56 +02:00
|
|
|
// This looks for a block within tasks that has the following pattern [reboot fastboot,
|
|
|
|
// update-super, $LIST_OF_DYNAMIC_FLASH_TASKS] and returns true if this is found.Theoretically
|
|
|
|
// this check is just a pattern match and could break if fastboot-info has a bunch of junk commands
|
|
|
|
// but all devices should pretty much follow this pattern
|
|
|
|
bool OptimizedFlashSuperTask::CanOptimize(const ImageSource* source,
|
|
|
|
const std::vector<std::unique_ptr<Task>>& tasks) {
|
|
|
|
for (size_t i = 0; i < tasks.size(); i++) {
|
|
|
|
auto reboot_task = tasks[i]->AsRebootTask();
|
|
|
|
if (!reboot_task || reboot_task->GetTarget() != "fastboot") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// The check for i >= tasks.size() - 2 is because we are peeking two tasks ahead. We need to
|
|
|
|
// check for an update-super && flash {dynamic_partition}
|
|
|
|
if (i >= tasks.size() - 2 || !tasks[i + 1]->AsUpdateSuperTask()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto flash_task = tasks[i + 2]->AsFlashTask();
|
2023-12-12 20:25:40 +01:00
|
|
|
if (!FlashTask::IsDynamicPartition(source, flash_task)) {
|
2023-08-25 00:44:56 +02:00
|
|
|
continue;
|
2023-02-14 18:44:40 +01:00
|
|
|
}
|
2023-08-25 00:44:56 +02:00
|
|
|
return true;
|
2023-02-14 18:44:40 +01:00
|
|
|
}
|
2023-08-25 00:44:56 +02:00
|
|
|
return false;
|
2023-03-10 08:23:49 +01:00
|
|
|
}
|
|
|
|
|
2023-08-25 00:44:56 +02:00
|
|
|
std::unique_ptr<OptimizedFlashSuperTask> OptimizedFlashSuperTask::Initialize(
|
2023-03-10 08:23:49 +01:00
|
|
|
const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks) {
|
2023-06-21 23:27:35 +02:00
|
|
|
if (!fp->should_optimize_flash_super) {
|
|
|
|
LOG(INFO) << "super optimization is disabled";
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-08-24 18:47:43 +02:00
|
|
|
if (!supports_AB(fp->fb)) {
|
2023-03-10 08:23:49 +01:00
|
|
|
LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (fp->slot_override == "all") {
|
|
|
|
LOG(VERBOSE) << "Cannot optimize flashing super for all slots";
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-10-03 02:02:05 +02:00
|
|
|
if (!CanOptimize(fp->source.get(), tasks)) {
|
2023-08-25 00:44:56 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2023-03-10 08:23:49 +01:00
|
|
|
|
|
|
|
// Does this device use dynamic partitions at all?
|
|
|
|
unique_fd fd = fp->source->OpenFile("super_empty.img");
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
LOG(VERBOSE) << "could not open super_empty.img";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string super_name;
|
|
|
|
// Try to find whether there is a super partition.
|
|
|
|
if (fp->fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) {
|
|
|
|
super_name = "super";
|
|
|
|
}
|
|
|
|
uint64_t partition_size;
|
|
|
|
std::string partition_size_str;
|
|
|
|
if (fp->fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) {
|
|
|
|
LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
partition_size_str = fb_fix_numeric_var(partition_size_str);
|
|
|
|
if (!android::base::ParseUint(partition_size_str, &partition_size)) {
|
|
|
|
LOG(VERBOSE) << "Could not parse " << super_name << " size: " << partition_size_str;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<SuperFlashHelper> helper = std::make_unique<SuperFlashHelper>(*fp->source);
|
|
|
|
if (!helper->Open(fd)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& task : tasks) {
|
|
|
|
if (auto flash_task = task->AsFlashTask()) {
|
2023-05-26 00:14:59 +02:00
|
|
|
auto partition = flash_task->GetPartitionAndSlot();
|
|
|
|
if (!helper->AddPartition(partition, flash_task->GetImageName(), false)) {
|
|
|
|
return nullptr;
|
2023-03-10 08:23:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto s = helper->GetSparseLayout();
|
|
|
|
if (!s) return nullptr;
|
2023-08-24 18:47:43 +02:00
|
|
|
|
|
|
|
// Remove tasks that are concatenated into this optimized task
|
2023-03-10 08:23:49 +01:00
|
|
|
auto remove_if_callback = [&](const auto& task) -> bool {
|
|
|
|
if (auto flash_task = task->AsFlashTask()) {
|
|
|
|
return helper->WillFlash(flash_task->GetPartitionAndSlot());
|
2023-12-05 10:35:33 +01:00
|
|
|
} else if (task->AsUpdateSuperTask()) {
|
2023-03-10 08:23:49 +01:00
|
|
|
return true;
|
|
|
|
} else if (auto reboot_task = task->AsRebootTask()) {
|
2023-08-24 18:47:43 +02:00
|
|
|
if (reboot_task->GetTarget() == "fastboot") {
|
|
|
|
return true;
|
|
|
|
}
|
2023-03-10 08:23:49 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2023-08-24 18:47:43 +02:00
|
|
|
|
2023-03-10 08:23:49 +01:00
|
|
|
tasks.erase(std::remove_if(tasks.begin(), tasks.end(), remove_if_callback), tasks.end());
|
|
|
|
|
2023-06-22 00:21:06 +02:00
|
|
|
return std::make_unique<OptimizedFlashSuperTask>(super_name, std::move(helper), std::move(s),
|
|
|
|
partition_size, fp);
|
2023-02-14 18:44:40 +01:00
|
|
|
}
|
2023-03-03 08:14:23 +01:00
|
|
|
|
2023-04-10 18:53:08 +02:00
|
|
|
UpdateSuperTask::UpdateSuperTask(const FlashingPlan* fp) : fp_(fp) {}
|
2023-03-03 08:14:23 +01:00
|
|
|
|
|
|
|
void UpdateSuperTask::Run() {
|
|
|
|
unique_fd fd = fp_->source->OpenFile("super_empty.img");
|
|
|
|
if (fd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!is_userspace_fastboot()) {
|
|
|
|
reboot_to_userspace_fastboot();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string super_name;
|
|
|
|
if (fp_->fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
|
|
|
|
super_name = "super";
|
|
|
|
}
|
|
|
|
fp_->fb->Download(super_name, fd, get_file_size(fd));
|
|
|
|
|
|
|
|
std::string command = "update-super:" + super_name;
|
|
|
|
if (fp_->wants_wipe) {
|
|
|
|
command += ":wipe";
|
|
|
|
}
|
|
|
|
fp_->fb->RawCommand(command, "Updating super partition");
|
|
|
|
}
|
2023-08-25 00:44:56 +02:00
|
|
|
std::string UpdateSuperTask::ToString() const {
|
2023-06-05 20:24:59 +02:00
|
|
|
return "update-super";
|
|
|
|
}
|
2023-03-03 08:16:46 +01:00
|
|
|
|
2023-04-10 18:53:08 +02:00
|
|
|
ResizeTask::ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size,
|
2023-03-03 08:16:46 +01:00
|
|
|
const std::string& slot)
|
|
|
|
: fp_(fp), pname_(pname), size_(size), slot_(slot) {}
|
|
|
|
|
|
|
|
void ResizeTask::Run() {
|
|
|
|
auto resize_partition = [this](const std::string& partition) -> void {
|
|
|
|
if (is_logical(partition)) {
|
|
|
|
fp_->fb->ResizePartition(partition, size_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
do_for_partitions(pname_, slot_, resize_partition, false);
|
|
|
|
}
|
2023-03-07 19:59:51 +01:00
|
|
|
|
2023-08-25 00:44:56 +02:00
|
|
|
std::string ResizeTask::ToString() const {
|
2023-06-05 20:24:59 +02:00
|
|
|
return "resize " + pname_;
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:53:08 +02:00
|
|
|
DeleteTask::DeleteTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
|
2023-03-07 19:59:51 +01:00
|
|
|
|
|
|
|
void DeleteTask::Run() {
|
|
|
|
fp_->fb->DeletePartition(pname_);
|
2023-03-13 18:09:43 +01:00
|
|
|
}
|
|
|
|
|
2023-08-25 00:44:56 +02:00
|
|
|
std::string DeleteTask::ToString() const {
|
2023-06-05 20:24:59 +02:00
|
|
|
return "delete " + pname_;
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:53:08 +02:00
|
|
|
WipeTask::WipeTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
|
2023-03-13 18:09:43 +01:00
|
|
|
|
|
|
|
void WipeTask::Run() {
|
|
|
|
std::string partition_type;
|
|
|
|
if (fp_->fb->GetVar("partition-type:" + pname_, &partition_type) != fastboot::SUCCESS) {
|
2023-03-29 00:47:04 +02:00
|
|
|
LOG(ERROR) << "wipe task partition not found: " << pname_;
|
2023-03-13 18:09:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (partition_type.empty()) return;
|
2023-03-29 00:47:04 +02:00
|
|
|
if (fp_->fb->Erase(pname_) != fastboot::SUCCESS) {
|
|
|
|
LOG(ERROR) << "wipe task erase failed with partition: " << pname_;
|
|
|
|
return;
|
|
|
|
}
|
2023-06-21 23:41:11 +02:00
|
|
|
fb_perform_format(pname_, 1, partition_type, "", fp_->fs_options, fp_);
|
2023-03-13 18:09:43 +01:00
|
|
|
}
|
2023-06-05 20:24:59 +02:00
|
|
|
|
2023-08-25 00:44:56 +02:00
|
|
|
std::string WipeTask::ToString() const {
|
2023-06-05 20:24:59 +02:00
|
|
|
return "erase " + pname_;
|
|
|
|
}
|