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>
28 #include <bootloader_message/bootloader_message.h>
29
30 #include "recovery_ui/ui.h"
31
32 static const std::vector<std::pair<std::string, Device::BuiltinAction>> kFastbootMenuActions{
33 { "Reboot system now", Device::REBOOT },
34 { "Enter recovery", Device::ENTER_RECOVERY },
35 { "Reboot to bootloader", Device::REBOOT_BOOTLOADER },
36 { "Power off", Device::SHUTDOWN },
37 };
38
StartFastboot(Device * device,const std::vector<std::string> &)39 Device::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);
55
56 // Reset to normal system boot so recovery won't cycle indefinitely.
57 // TODO(b/112277594) Clear only if 'recovery' field of BCB is empty. If not,
58 // set the 'command' field of BCB to 'boot-recovery' so the next boot is into recovery
59 // to finish any interrupted tasks.
60 std::string err;
61 if (!clear_bootloader_message(&err)) {
62 LOG(ERROR) << "Failed to clear BCB message: " << err;
63 }
64
65 std::vector<std::string> fastboot_menu_items;
66 std::transform(kFastbootMenuActions.cbegin(), kFastbootMenuActions.cend(),
67 std::back_inserter(fastboot_menu_items),
68 [](const auto& entry) { return entry.first; });
69
70 auto chosen_item = ui->ShowMenu(
71 {}, fastboot_menu_items, 0, false,
72 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
73
74 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
75 return Device::KEY_INTERRUPTED;
76 }
77 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::TIMED_OUT)) {
78 return Device::BuiltinAction::NO_ACTION;
79 }
80 return kFastbootMenuActions[chosen_item].second;
81 }
82