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_FROM_FASTBOOT },
34   { "Enter recovery", Device::ENTER_RECOVERY },
35   { "Reboot to bootloader", Device::REBOOT_BOOTLOADER },
36   { "Power off", Device::SHUTDOWN_FROM_FASTBOOT },
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   device->StartFastboot();
56 
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 }
83