blob: 9784798d117034a3371e3f710452382a0eb85729 [file] [log] [blame]
Hridya Valsaraju20c81b32018-07-27 22:09:12 -07001/*
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 Bestasce7db6c2022-05-11 06:49:31 +030028#include <android-base/strings.h>
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070029#include <bootloader_message/bootloader_message.h>
30
Tianjie Xu8f397302018-08-20 13:40:47 -070031#include "recovery_ui/ui.h"
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070032
33static const std::vector<std::pair<std::string, Device::BuiltinAction>> kFastbootMenuActions{
NurKeinNeidaded1b42022-05-07 11:24:50 +020034 { "Reboot to system", Device::REBOOT_FROM_FASTBOOT },
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070035 { "Enter recovery", Device::ENTER_RECOVERY },
36 { "Reboot to bootloader", Device::REBOOT_BOOTLOADER },
Mark Salyzyn488cc052019-05-20 10:36:16 -070037 { "Power off", Device::SHUTDOWN_FROM_FASTBOOT },
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070038};
39
Abhishek Nigamebc02272023-06-13 22:28:43 +000040void FillDefaultFastbootLines(std::vector<std::string>& title_lines) {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070041
Michael Bestasce7db6c2022-05-11 06:49:31 +030042 std::string bootloader_version = android::base::GetProperty("ro.bootloader", "");
43 std::string baseband_version = android::base::GetProperty("ro.build.expect.baseband", "");
Michael Bestasb7dbb912022-05-11 06:39:40 +030044 std::string hw_version = android::base::GetProperty(
Jis G Jacoba9155282024-04-10 12:01:17 -040045 "ro.boot.hardware.revision", android::base::GetProperty("ro.revision", "")); title_lines.push_back("Product name - " + android::base::GetProperty("ro.product.device", ""));
Michael Bestasce7db6c2022-05-11 06:49:31 +030046 if (!android::base::EqualsIgnoreCase(bootloader_version, "unknown")) {
47 title_lines.push_back("Bootloader version - " + bootloader_version);
48 }
49 if (!baseband_version.empty()) {
50 title_lines.push_back("Baseband version - " + baseband_version);
51 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070052 title_lines.push_back("Serial number - " + android::base::GetProperty("ro.serialno", ""));
53 title_lines.push_back(std::string("Secure boot - ") +
54 ((android::base::GetProperty("ro.secure", "") == "1") ? "yes" : "no"));
Michael Bestasce7db6c2022-05-11 06:49:31 +030055 if (!android::base::EqualsIgnoreCase(hw_version, "0")) {
56 title_lines.push_back("HW version - " + hw_version);
57 }
Abhishek Nigamebc02272023-06-13 22:28:43 +000058}
59
60void FillWearableFastbootLines(std::vector<std::string>& title_lines) {
61 title_lines.push_back("Android Fastboot");
62 title_lines.push_back(android::base::GetProperty("ro.product.device", "") + " - " +
63 android::base::GetProperty("ro.revision", ""));
64 title_lines.push_back(android::base::GetProperty("ro.bootloader", ""));
65
66 const size_t max_baseband_len = 24;
67 const std::string& baseband = android::base::GetProperty("ro.build.expect.baseband", "");
68 title_lines.push_back(baseband.length() > max_baseband_len
69 ? baseband.substr(0, max_baseband_len - 3) + "..."
70 : baseband);
71
72 title_lines.push_back("Serial #: " + android::base::GetProperty("ro.serialno", ""));
73}
74
75Device::BuiltinAction StartFastboot(Device* device, const std::vector<std::string>& /* args */) {
76 RecoveryUI* ui = device->GetUI();
77 std::vector<std::string> title_lines;
78
79 if (ui->IsWearable()) {
80 FillWearableFastbootLines(title_lines);
81 } else {
Ben Fennema4022a1d2023-11-07 16:04:58 -080082 ui->SetEnableFastbootdLogo(true);
Abhishek Nigamebc02272023-06-13 22:28:43 +000083 FillDefaultFastbootLines(title_lines);
84 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070085
86 ui->ResetKeyInterruptStatus();
87 ui->SetTitle(title_lines);
88 ui->ShowText(true);
Hongguang Chen04267272020-04-21 20:58:04 -070089 device->StartFastboot();
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070090
91 // Reset to normal system boot so recovery won't cycle indefinitely.
92 // TODO(b/112277594) Clear only if 'recovery' field of BCB is empty. If not,
93 // set the 'command' field of BCB to 'boot-recovery' so the next boot is into recovery
94 // to finish any interrupted tasks.
95 std::string err;
96 if (!clear_bootloader_message(&err)) {
97 LOG(ERROR) << "Failed to clear BCB message: " << err;
98 }
99
100 std::vector<std::string> fastboot_menu_items;
101 std::transform(kFastbootMenuActions.cbegin(), kFastbootMenuActions.cend(),
102 std::back_inserter(fastboot_menu_items),
103 [](const auto& entry) { return entry.first; });
104
105 auto chosen_item = ui->ShowMenu(
106 {}, fastboot_menu_items, 0, false,
107 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
108
109 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
110 return Device::KEY_INTERRUPTED;
111 }
112 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::TIMED_OUT)) {
113 return Device::BuiltinAction::NO_ACTION;
114 }
115 return kFastbootMenuActions[chosen_item].second;
116}