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 | |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame^] | 23 | FlashTask::FlashTask(const std::string& _slot, const std::string& _pname) |
| 24 | : pname_(_pname), fname_(find_item(_pname)), slot_(_slot) { |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 25 | if (fname_.empty()) die("cannot determine image filename for '%s'", pname_.c_str()); |
| 26 | } |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame^] | 27 | FlashTask::FlashTask(const std::string& _slot, const std::string& _pname, const std::string& _fname) |
| 28 | : pname_(_pname), fname_(_fname), slot_(_slot) {} |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 29 | |
| 30 | void FlashTask::Run() { |
| 31 | auto flash = [&](const std::string& partition) { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame^] | 32 | if (should_flash_in_userspace(partition) && !is_userspace_fastboot()) { |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 33 | 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 Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 45 | |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame^] | 46 | RebootTask::RebootTask(FlashingPlan* _fp) : fp_(_fp){}; |
| 47 | RebootTask::RebootTask(FlashingPlan* _fp, const std::string& _reboot_target) |
| 48 | : reboot_target_(_reboot_target), fp_(_fp){}; |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 49 | |
| 50 | void RebootTask::Run() { |
| 51 | if ((reboot_target_ == "userspace" || reboot_target_ == "fastboot")) { |
| 52 | if (!is_userspace_fastboot()) { |
| 53 | reboot_to_userspace_fastboot(); |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame^] | 54 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 55 | } |
| 56 | } else if (reboot_target_ == "recovery") { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame^] | 57 | fp_->fb->RebootTo("recovery"); |
| 58 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 59 | } else if (reboot_target_ == "bootloader") { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame^] | 60 | fp_->fb->RebootTo("bootloader"); |
| 61 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 62 | } else if (reboot_target_ == "") { |
Daniel Zheng | bc01da5 | 2023-02-23 03:25:52 +0000 | [diff] [blame^] | 63 | fp_->fb->Reboot(); |
| 64 | fp_->fb->WaitForDisconnect(); |
Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 65 | } else { |
| 66 | syntax_error("unknown reboot target %s", reboot_target_.c_str()); |
| 67 | } |
| 68 | } |