Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 1 | // |
| 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 Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame^] | 17 | #include "fastboot.h" |
| 18 | #include "util.h" |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 19 | |
| 20 | #include "fastboot.h" |
| 21 | #include "util.h" |
| 22 | |
| 23 | FlashTask::FlashTask(const std::string& _slot) : slot_(_slot){}; |
| 24 | FlashTask::FlashTask(const std::string& _slot, bool _force_flash) |
| 25 | : slot_(_slot), force_flash_(_force_flash) {} |
| 26 | FlashTask::FlashTask(const std::string& _slot, bool _force_flash, const std::string& _pname) |
| 27 | : pname_(_pname), fname_(find_item(_pname)), slot_(_slot), force_flash_(_force_flash) { |
| 28 | if (fname_.empty()) die("cannot determine image filename for '%s'", pname_.c_str()); |
| 29 | } |
| 30 | FlashTask::FlashTask(const std::string& _slot, bool _force_flash, const std::string& _pname, |
| 31 | const std::string& _fname) |
| 32 | : pname_(_pname), fname_(_fname), slot_(_slot), force_flash_(_force_flash) {} |
| 33 | |
| 34 | void FlashTask::Run() { |
| 35 | auto flash = [&](const std::string& partition) { |
| 36 | if (should_flash_in_userspace(partition) && !is_userspace_fastboot() && !force_flash_) { |
| 37 | die("The partition you are trying to flash is dynamic, and " |
| 38 | "should be flashed via fastbootd. Please run:\n" |
| 39 | "\n" |
| 40 | " fastboot reboot fastboot\n" |
| 41 | "\n" |
| 42 | "And try again. If you are intentionally trying to " |
| 43 | "overwrite a fixed partition, use --force."); |
| 44 | } |
| 45 | do_flash(partition.c_str(), fname_.c_str()); |
| 46 | }; |
| 47 | do_for_partitions(pname_, slot_, flash, true); |
| 48 | } |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame^] | 49 | |
| 50 | RebootTask::RebootTask(fastboot::FastBootDriver* _fb) : fb_(_fb){}; |
| 51 | RebootTask::RebootTask(fastboot::FastBootDriver* _fb, std::string _reboot_target) |
| 52 | : reboot_target_(std::move(_reboot_target)), fb_(_fb){}; |
| 53 | |
| 54 | void RebootTask::Run() { |
| 55 | if ((reboot_target_ == "userspace" || reboot_target_ == "fastboot")) { |
| 56 | if (!is_userspace_fastboot()) { |
| 57 | reboot_to_userspace_fastboot(); |
| 58 | fb_->WaitForDisconnect(); |
| 59 | } |
| 60 | } else if (reboot_target_ == "recovery") { |
| 61 | fb_->RebootTo("recovery"); |
| 62 | fb_->WaitForDisconnect(); |
| 63 | } else if (reboot_target_ == "bootloader") { |
| 64 | fb_->RebootTo("bootloader"); |
| 65 | fb_->WaitForDisconnect(); |
| 66 | } else if (reboot_target_ == "") { |
| 67 | fb_->Reboot(); |
| 68 | fb_->WaitForDisconnect(); |
| 69 | } else { |
| 70 | syntax_error("unknown reboot target %s", reboot_target_.c_str()); |
| 71 | } |
| 72 | } |