blob: 0e4a68b20445d92a4736e463df47a14d28f9edcd [file] [log] [blame]
Hridya Valsarajudea91b42018-07-17 11:14:01 -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 "commands.h"
18
19#include <sys/socket.h>
20#include <sys/un.h>
21
22#include <android-base/logging.h>
23#include <android-base/parseint.h>
24#include <android-base/properties.h>
25#include <android-base/stringprintf.h>
26#include <android-base/strings.h>
27#include <android-base/unique_fd.h>
28#include <cutils/android_reboot.h>
29
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070030#include "constants.h"
Hridya Valsarajudea91b42018-07-17 11:14:01 -070031#include "fastboot_device.h"
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070032#include "utility.h"
33
34using ::android::hardware::hidl_string;
35using ::android::hardware::boot::V1_0::BoolResult;
36using ::android::hardware::boot::V1_0::CommandResult;
37using ::android::hardware::boot::V1_0::Slot;
38
39bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) {
David Anderson856b7ec2018-08-08 17:58:56 -070040 using VariableHandler = std::function<bool(FastbootDevice*, const std::vector<std::string>&)>;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070041 const std::unordered_map<std::string, VariableHandler> kVariableMap = {
42 {FB_VAR_VERSION, GetVersion},
43 {FB_VAR_VERSION_BOOTLOADER, GetBootloaderVersion},
44 {FB_VAR_VERSION_BASEBAND, GetBasebandVersion},
45 {FB_VAR_PRODUCT, GetProduct},
46 {FB_VAR_SERIALNO, GetSerial},
47 {FB_VAR_SECURE, GetSecure},
48 {FB_VAR_UNLOCKED, GetUnlocked},
49 {FB_VAR_MAX_DOWNLOAD_SIZE, GetMaxDownloadSize},
50 {FB_VAR_CURRENT_SLOT, ::GetCurrentSlot},
51 {FB_VAR_SLOT_COUNT, GetSlotCount},
52 {FB_VAR_HAS_SLOT, GetHasSlot},
53 {FB_VAR_SLOT_SUCCESSFUL, GetSlotSuccessful},
54 {FB_VAR_SLOT_UNBOOTABLE, GetSlotUnbootable}};
55
56 // args[0] is command name, args[1] is variable.
57 auto found_variable = kVariableMap.find(args[1]);
58 if (found_variable == kVariableMap.end()) {
59 return device->WriteStatus(FastbootResult::FAIL, "Unknown variable");
60 }
61
62 std::vector<std::string> getvar_args(args.begin() + 2, args.end());
David Anderson856b7ec2018-08-08 17:58:56 -070063 return found_variable->second(device, getvar_args);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070064}
Hridya Valsarajudea91b42018-07-17 11:14:01 -070065
66bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
67 if (args.size() < 2) {
68 return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
69 }
70 // arg[0] is the command name, arg[1] contains size of data to be downloaded
71 unsigned int size;
72 if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) {
73 return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
74 }
75 device->get_download_data().resize(size);
76 if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
77 return false;
78 }
79
80 if (device->HandleData(true, &device->get_download_data())) {
81 return device->WriteStatus(FastbootResult::OKAY, "");
82 }
83
84 PLOG(ERROR) << "Couldn't download data";
85 return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
86}
87
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070088bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
89 if (args.size() < 2) {
90 return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
91 }
92
93 // Slot suffix needs to be between 'a' and 'z'.
94 Slot slot;
95 if (!GetSlotNumber(args[1], &slot)) {
96 return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
97 }
98
99 // Non-A/B devices will not have a boot control HAL.
100 auto boot_control_hal = device->boot_control_hal();
101 if (!boot_control_hal) {
102 return device->WriteStatus(FastbootResult::FAIL,
103 "Cannot set slot: boot control HAL absent");
104 }
105 if (slot >= boot_control_hal->getNumberSlots()) {
106 return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
107 }
108 CommandResult ret;
109 auto cb = [&ret](CommandResult result) { ret = result; };
110 auto result = boot_control_hal->setActiveBootSlot(slot, cb);
111 if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, "");
112 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700113}
114
115bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
116 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
117 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
118 device->CloseDevice();
119 TEMP_FAILURE_RETRY(pause());
120 return result;
121}
122
123bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
124 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
125 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
126 device->CloseDevice();
127 TEMP_FAILURE_RETRY(pause());
128 return result;
129}
130
131bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
132 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
133 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
134 device->CloseDevice();
135 TEMP_FAILURE_RETRY(pause());
136 return result;
137}
138
139bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
140 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
141 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
142 device->CloseDevice();
143 TEMP_FAILURE_RETRY(pause());
144 return result;
145}
146
147static bool EnterRecovery() {
148 const char msg_switch_to_recovery = 'r';
149
150 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
151 if (sock < 0) {
152 PLOG(ERROR) << "Couldn't create sock";
153 return false;
154 }
155
156 struct sockaddr_un addr = {.sun_family = AF_UNIX};
157 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
158 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
159 PLOG(ERROR) << "Couldn't connect to recovery";
160 return false;
161 }
162 // Switch to recovery will not update the boot reason since it does not
163 // require a reboot.
164 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
165 if (ret != sizeof(msg_switch_to_recovery)) {
166 PLOG(ERROR) << "Couldn't write message to switch to recovery";
167 return false;
168 }
169
170 return true;
171}
172
173bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
174 auto status = true;
175 if (EnterRecovery()) {
176 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
177 } else {
178 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
179 }
180 device->CloseDevice();
181 TEMP_FAILURE_RETRY(pause());
182 return status;
183}