blob: 898d15cb509312012b65dd8ea61df33724a3379b [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 Bestas20544ef2022-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 Bestas20544ef2022-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", "");
45 std::string hw_version = android::base::GetProperty("ro.revision", "");
Richard Hansen07d73332020-05-12 18:08:56 -040046 std::vector<std::string> title_lines;
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070047 title_lines.push_back("Product name - " + android::base::GetProperty("ro.product.device", ""));
Michael Bestas20544ef2022-05-11 06:49:31 +030048 if (!android::base::EqualsIgnoreCase(bootloader_version, "unknown")) {
49 title_lines.push_back("Bootloader version - " + bootloader_version);
50 }
51 if (!baseband_version.empty()) {
52 title_lines.push_back("Baseband version - " + baseband_version);
53 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070054 title_lines.push_back("Serial number - " + android::base::GetProperty("ro.serialno", ""));
55 title_lines.push_back(std::string("Secure boot - ") +
56 ((android::base::GetProperty("ro.secure", "") == "1") ? "yes" : "no"));
Michael Bestas20544ef2022-05-11 06:49:31 +030057 if (!android::base::EqualsIgnoreCase(hw_version, "0")) {
58 title_lines.push_back("HW version - " + hw_version);
59 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070060
61 ui->ResetKeyInterruptStatus();
62 ui->SetTitle(title_lines);
63 ui->ShowText(true);
Hongguang Chen04267272020-04-21 20:58:04 -070064 device->StartFastboot();
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070065
66 // Reset to normal system boot so recovery won't cycle indefinitely.
67 // TODO(b/112277594) Clear only if 'recovery' field of BCB is empty. If not,
68 // set the 'command' field of BCB to 'boot-recovery' so the next boot is into recovery
69 // to finish any interrupted tasks.
70 std::string err;
71 if (!clear_bootloader_message(&err)) {
72 LOG(ERROR) << "Failed to clear BCB message: " << err;
73 }
74
75 std::vector<std::string> fastboot_menu_items;
76 std::transform(kFastbootMenuActions.cbegin(), kFastbootMenuActions.cend(),
77 std::back_inserter(fastboot_menu_items),
78 [](const auto& entry) { return entry.first; });
79
80 auto chosen_item = ui->ShowMenu(
81 {}, fastboot_menu_items, 0, false,
82 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
83
84 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
85 return Device::KEY_INTERRUPTED;
86 }
87 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::TIMED_OUT)) {
88 return Device::BuiltinAction::NO_ACTION;
89 }
90 return kFastbootMenuActions[chosen_item].second;
91}