blob: a4aa637ae20296321ae118d47f9a3665e564e1e1 [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 Zheng71b3b432023-01-30 17:43:00 +000017#include "fastboot.h"
18#include "util.h"
Daniel Zheng0d307182023-01-31 21:07:53 +000019
20#include "fastboot.h"
21#include "util.h"
22
Daniel Zhengbc01da52023-02-23 03:25:52 +000023FlashTask::FlashTask(const std::string& _slot, const std::string& _pname)
24 : pname_(_pname), fname_(find_item(_pname)), slot_(_slot) {
Daniel Zheng0d307182023-01-31 21:07:53 +000025 if (fname_.empty()) die("cannot determine image filename for '%s'", pname_.c_str());
26}
Daniel Zhengbc01da52023-02-23 03:25:52 +000027FlashTask::FlashTask(const std::string& _slot, const std::string& _pname, const std::string& _fname)
28 : pname_(_pname), fname_(_fname), slot_(_slot) {}
Daniel Zheng0d307182023-01-31 21:07:53 +000029
30void FlashTask::Run() {
31 auto flash = [&](const std::string& partition) {
Daniel Zhengbc01da52023-02-23 03:25:52 +000032 if (should_flash_in_userspace(partition) && !is_userspace_fastboot()) {
Daniel Zheng0d307182023-01-31 21:07:53 +000033 die("The partition you are trying to flash is dynamic, and "
34 "should be flashed via fastbootd. Please run:\n"
35 "\n"
36 " fastboot reboot fastboot\n"
37 "\n"
38 "And try again. If you are intentionally trying to "
39 "overwrite a fixed partition, use --force.");
40 }
41 do_flash(partition.c_str(), fname_.c_str());
42 };
43 do_for_partitions(pname_, slot_, flash, true);
44}
Daniel Zheng71b3b432023-01-30 17:43:00 +000045
Daniel Zhengbc01da52023-02-23 03:25:52 +000046RebootTask::RebootTask(FlashingPlan* _fp) : fp_(_fp){};
47RebootTask::RebootTask(FlashingPlan* _fp, const std::string& _reboot_target)
48 : reboot_target_(_reboot_target), fp_(_fp){};
Daniel Zheng71b3b432023-01-30 17:43:00 +000049
50void RebootTask::Run() {
51 if ((reboot_target_ == "userspace" || reboot_target_ == "fastboot")) {
52 if (!is_userspace_fastboot()) {
53 reboot_to_userspace_fastboot();
Daniel Zhengbc01da52023-02-23 03:25:52 +000054 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000055 }
56 } else if (reboot_target_ == "recovery") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000057 fp_->fb->RebootTo("recovery");
58 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000059 } else if (reboot_target_ == "bootloader") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000060 fp_->fb->RebootTo("bootloader");
61 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000062 } else if (reboot_target_ == "") {
Daniel Zhengbc01da52023-02-23 03:25:52 +000063 fp_->fb->Reboot();
64 fp_->fb->WaitForDisconnect();
Daniel Zheng71b3b432023-01-30 17:43:00 +000065 } else {
66 syntax_error("unknown reboot target %s", reboot_target_.c_str());
67 }
68}