blob: a0930087c7ee4194c82e19f521fa3bbf5eb45a7d [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>
28#include <bootloader_message/bootloader_message.h>
29
Tianjie Xu8f397302018-08-20 13:40:47 -070030#include "recovery_ui/ui.h"
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070031
32static const std::vector<std::pair<std::string, Device::BuiltinAction>> kFastbootMenuActions{
Mark Salyzyn488cc052019-05-20 10:36:16 -070033 { "Reboot system now", Device::REBOOT_FROM_FASTBOOT },
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070034 { "Enter recovery", Device::ENTER_RECOVERY },
35 { "Reboot to bootloader", Device::REBOOT_BOOTLOADER },
Mark Salyzyn488cc052019-05-20 10:36:16 -070036 { "Power off", Device::SHUTDOWN_FROM_FASTBOOT },
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070037};
38
39Device::BuiltinAction StartFastboot(Device* device, const std::vector<std::string>& /* args */) {
40 RecoveryUI* ui = device->GetUI();
41
42 std::vector<std::string> title_lines = { "Android Fastboot" };
43 title_lines.push_back("Product name - " + android::base::GetProperty("ro.product.device", ""));
44 title_lines.push_back("Bootloader version - " + android::base::GetProperty("ro.bootloader", ""));
45 title_lines.push_back("Baseband version - " +
46 android::base::GetProperty("ro.build.expect.baseband", ""));
47 title_lines.push_back("Serial number - " + android::base::GetProperty("ro.serialno", ""));
48 title_lines.push_back(std::string("Secure boot - ") +
49 ((android::base::GetProperty("ro.secure", "") == "1") ? "yes" : "no"));
50 title_lines.push_back("HW version - " + android::base::GetProperty("ro.revision", ""));
51
52 ui->ResetKeyInterruptStatus();
53 ui->SetTitle(title_lines);
54 ui->ShowText(true);
Hongguang Chen4d0df882020-04-21 20:58:04 -070055 device->StartFastboot();
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070056
57 // Reset to normal system boot so recovery won't cycle indefinitely.
58 // TODO(b/112277594) Clear only if 'recovery' field of BCB is empty. If not,
59 // set the 'command' field of BCB to 'boot-recovery' so the next boot is into recovery
60 // to finish any interrupted tasks.
61 std::string err;
62 if (!clear_bootloader_message(&err)) {
63 LOG(ERROR) << "Failed to clear BCB message: " << err;
64 }
65
66 std::vector<std::string> fastboot_menu_items;
67 std::transform(kFastbootMenuActions.cbegin(), kFastbootMenuActions.cend(),
68 std::back_inserter(fastboot_menu_items),
69 [](const auto& entry) { return entry.first; });
70
71 auto chosen_item = ui->ShowMenu(
72 {}, fastboot_menu_items, 0, false,
73 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
74
75 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
76 return Device::KEY_INTERRUPTED;
77 }
78 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::TIMED_OUT)) {
79 return Device::BuiltinAction::NO_ACTION;
80 }
81 return kFastbootMenuActions[chosen_item].second;
82}