Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | #include "fastboot.h" |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/properties.h> |
Michael Bestas | 4ef767f | 2022-05-11 06:49:31 +0300 | [diff] [blame] | 28 | #include <android-base/strings.h> |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 29 | #include <bootloader_message/bootloader_message.h> |
| 30 | |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 31 | #include "recovery_ui/ui.h" |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 32 | |
| 33 | static const std::vector<std::pair<std::string, Device::BuiltinAction>> kFastbootMenuActions{ |
Mark Salyzyn | 488cc05 | 2019-05-20 10:36:16 -0700 | [diff] [blame] | 34 | { "Reboot system now", Device::REBOOT_FROM_FASTBOOT }, |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 35 | { "Enter recovery", Device::ENTER_RECOVERY }, |
| 36 | { "Reboot to bootloader", Device::REBOOT_BOOTLOADER }, |
Mark Salyzyn | 488cc05 | 2019-05-20 10:36:16 -0700 | [diff] [blame] | 37 | { "Power off", Device::SHUTDOWN_FROM_FASTBOOT }, |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | Device::BuiltinAction StartFastboot(Device* device, const std::vector<std::string>& /* args */) { |
| 41 | RecoveryUI* ui = device->GetUI(); |
| 42 | |
Michael Bestas | 4ef767f | 2022-05-11 06:49:31 +0300 | [diff] [blame] | 43 | std::string bootloader_version = android::base::GetProperty("ro.bootloader", ""); |
| 44 | std::string baseband_version = android::base::GetProperty("ro.build.expect.baseband", ""); |
Michael Bestas | 66c482b | 2022-05-11 06:39:40 +0300 | [diff] [blame^] | 45 | std::string hw_version = android::base::GetProperty( |
| 46 | "ro.boot.hardware.revision", android::base::GetProperty("ro.revision", "")); |
Richard Hansen | d44512a | 2020-05-12 18:08:56 -0400 | [diff] [blame] | 47 | std::vector<std::string> title_lines; |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 48 | title_lines.push_back("Product name - " + android::base::GetProperty("ro.product.device", "")); |
Michael Bestas | 4ef767f | 2022-05-11 06:49:31 +0300 | [diff] [blame] | 49 | if (!android::base::EqualsIgnoreCase(bootloader_version, "unknown")) { |
| 50 | title_lines.push_back("Bootloader version - " + bootloader_version); |
| 51 | } |
| 52 | if (!baseband_version.empty()) { |
| 53 | title_lines.push_back("Baseband version - " + baseband_version); |
| 54 | } |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 55 | title_lines.push_back("Serial number - " + android::base::GetProperty("ro.serialno", "")); |
| 56 | title_lines.push_back(std::string("Secure boot - ") + |
| 57 | ((android::base::GetProperty("ro.secure", "") == "1") ? "yes" : "no")); |
Michael Bestas | 4ef767f | 2022-05-11 06:49:31 +0300 | [diff] [blame] | 58 | if (!android::base::EqualsIgnoreCase(hw_version, "0")) { |
| 59 | title_lines.push_back("HW version - " + hw_version); |
| 60 | } |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 61 | |
| 62 | ui->ResetKeyInterruptStatus(); |
| 63 | ui->SetTitle(title_lines); |
| 64 | ui->ShowText(true); |
Hongguang Chen | 0426727 | 2020-04-21 20:58:04 -0700 | [diff] [blame] | 65 | device->StartFastboot(); |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 66 | |
| 67 | // Reset to normal system boot so recovery won't cycle indefinitely. |
| 68 | // TODO(b/112277594) Clear only if 'recovery' field of BCB is empty. If not, |
| 69 | // set the 'command' field of BCB to 'boot-recovery' so the next boot is into recovery |
| 70 | // to finish any interrupted tasks. |
| 71 | std::string err; |
| 72 | if (!clear_bootloader_message(&err)) { |
| 73 | LOG(ERROR) << "Failed to clear BCB message: " << err; |
| 74 | } |
| 75 | |
| 76 | std::vector<std::string> fastboot_menu_items; |
| 77 | std::transform(kFastbootMenuActions.cbegin(), kFastbootMenuActions.cend(), |
| 78 | std::back_inserter(fastboot_menu_items), |
| 79 | [](const auto& entry) { return entry.first; }); |
| 80 | |
| 81 | auto chosen_item = ui->ShowMenu( |
| 82 | {}, fastboot_menu_items, 0, false, |
| 83 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
| 84 | |
| 85 | if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) { |
| 86 | return Device::KEY_INTERRUPTED; |
| 87 | } |
| 88 | if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::TIMED_OUT)) { |
| 89 | return Device::BuiltinAction::NO_ACTION; |
| 90 | } |
| 91 | return kFastbootMenuActions[chosen_item].second; |
| 92 | } |