blob: 6a9d0845fa9f6e9e271819f3fa286fe527e67e77 [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 Bestas4ef767f2022-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{
Mark Salyzyn488cc052019-05-20 10:36:16 -070034 { "Reboot system now", 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
40Device::BuiltinAction StartFastboot(Device* device, const std::vector<std::string>& /* args */) {
41 RecoveryUI* ui = device->GetUI();
42
Michael Bestas4ef767f2022-05-11 06:49:31 +030043 std::string bootloader_version = android::base::GetProperty("ro.bootloader", "");
44 std::string baseband_version = android::base::GetProperty("ro.build.expect.baseband", "");
Michael Bestas66c482b2022-05-11 06:39:40 +030045 std::string hw_version = android::base::GetProperty(
46 "ro.boot.hardware.revision", android::base::GetProperty("ro.revision", ""));
Richard Hansend44512a2020-05-12 18:08:56 -040047 std::vector<std::string> title_lines;
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070048 title_lines.push_back("Product name - " + android::base::GetProperty("ro.product.device", ""));
Michael Bestas4ef767f2022-05-11 06:49:31 +030049 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 Valsaraju20c81b32018-07-27 22:09:12 -070055 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 Bestas4ef767f2022-05-11 06:49:31 +030058 if (!android::base::EqualsIgnoreCase(hw_version, "0")) {
59 title_lines.push_back("HW version - " + hw_version);
60 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070061
62 ui->ResetKeyInterruptStatus();
63 ui->SetTitle(title_lines);
64 ui->ShowText(true);
Hongguang Chen04267272020-04-21 20:58:04 -070065 device->StartFastboot();
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070066
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}