blob: 7eaefe6de6bf68f7955b477dbd6a2a1a33eeec31 [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>
David Anderson12211d12018-07-24 15:21:20 -070029#include <ext4_utils/wipe.h>
Hridya Valsarajudea91b42018-07-17 11:14:01 -070030
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070031#include "constants.h"
Hridya Valsarajudea91b42018-07-17 11:14:01 -070032#include "fastboot_device.h"
David Anderson12211d12018-07-24 15:21:20 -070033#include "flashing.h"
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070034#include "utility.h"
35
36using ::android::hardware::hidl_string;
37using ::android::hardware::boot::V1_0::BoolResult;
38using ::android::hardware::boot::V1_0::CommandResult;
39using ::android::hardware::boot::V1_0::Slot;
40
41bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) {
David Anderson856b7ec2018-08-08 17:58:56 -070042 using VariableHandler = std::function<bool(FastbootDevice*, const std::vector<std::string>&)>;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070043 const std::unordered_map<std::string, VariableHandler> kVariableMap = {
44 {FB_VAR_VERSION, GetVersion},
45 {FB_VAR_VERSION_BOOTLOADER, GetBootloaderVersion},
46 {FB_VAR_VERSION_BASEBAND, GetBasebandVersion},
47 {FB_VAR_PRODUCT, GetProduct},
48 {FB_VAR_SERIALNO, GetSerial},
49 {FB_VAR_SECURE, GetSecure},
50 {FB_VAR_UNLOCKED, GetUnlocked},
51 {FB_VAR_MAX_DOWNLOAD_SIZE, GetMaxDownloadSize},
52 {FB_VAR_CURRENT_SLOT, ::GetCurrentSlot},
53 {FB_VAR_SLOT_COUNT, GetSlotCount},
54 {FB_VAR_HAS_SLOT, GetHasSlot},
55 {FB_VAR_SLOT_SUCCESSFUL, GetSlotSuccessful},
David Anderson12211d12018-07-24 15:21:20 -070056 {FB_VAR_SLOT_UNBOOTABLE, GetSlotUnbootable},
57 {FB_VAR_PARTITION_SIZE, GetPartitionSize}};
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070058
59 // args[0] is command name, args[1] is variable.
60 auto found_variable = kVariableMap.find(args[1]);
61 if (found_variable == kVariableMap.end()) {
62 return device->WriteStatus(FastbootResult::FAIL, "Unknown variable");
63 }
64
65 std::vector<std::string> getvar_args(args.begin() + 2, args.end());
David Anderson856b7ec2018-08-08 17:58:56 -070066 return found_variable->second(device, getvar_args);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070067}
Hridya Valsarajudea91b42018-07-17 11:14:01 -070068
David Anderson12211d12018-07-24 15:21:20 -070069bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) {
70 if (args.size() < 2) {
71 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
72 }
73 PartitionHandle handle;
74 if (!OpenPartition(device, args[1], &handle)) {
75 return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist");
76 }
77 if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) {
78 return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded");
79 }
80 return device->WriteStatus(FastbootResult::FAIL, "Erasing failed");
81}
82
Hridya Valsarajudea91b42018-07-17 11:14:01 -070083bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
84 if (args.size() < 2) {
85 return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
86 }
87 // arg[0] is the command name, arg[1] contains size of data to be downloaded
88 unsigned int size;
89 if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) {
90 return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
91 }
David Anderson12211d12018-07-24 15:21:20 -070092 device->download_data().resize(size);
Hridya Valsarajudea91b42018-07-17 11:14:01 -070093 if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
94 return false;
95 }
96
David Anderson12211d12018-07-24 15:21:20 -070097 if (device->HandleData(true, &device->download_data())) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -070098 return device->WriteStatus(FastbootResult::OKAY, "");
99 }
100
101 PLOG(ERROR) << "Couldn't download data";
102 return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
103}
104
David Anderson12211d12018-07-24 15:21:20 -0700105bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) {
106 if (args.size() < 2) {
107 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
108 }
109 int ret = Flash(device, args[1]);
110 if (ret < 0) {
111 return device->WriteStatus(FastbootResult::FAIL, strerror(-ret));
112 }
113 return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded");
114}
115
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700116bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
117 if (args.size() < 2) {
118 return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
119 }
120
121 // Slot suffix needs to be between 'a' and 'z'.
122 Slot slot;
123 if (!GetSlotNumber(args[1], &slot)) {
124 return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
125 }
126
127 // Non-A/B devices will not have a boot control HAL.
128 auto boot_control_hal = device->boot_control_hal();
129 if (!boot_control_hal) {
130 return device->WriteStatus(FastbootResult::FAIL,
131 "Cannot set slot: boot control HAL absent");
132 }
133 if (slot >= boot_control_hal->getNumberSlots()) {
134 return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
135 }
136 CommandResult ret;
137 auto cb = [&ret](CommandResult result) { ret = result; };
138 auto result = boot_control_hal->setActiveBootSlot(slot, cb);
139 if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, "");
140 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700141}
142
143bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
144 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
145 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
146 device->CloseDevice();
147 TEMP_FAILURE_RETRY(pause());
148 return result;
149}
150
151bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
152 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
153 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
154 device->CloseDevice();
155 TEMP_FAILURE_RETRY(pause());
156 return result;
157}
158
159bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
160 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
161 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
162 device->CloseDevice();
163 TEMP_FAILURE_RETRY(pause());
164 return result;
165}
166
167bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
168 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
169 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
170 device->CloseDevice();
171 TEMP_FAILURE_RETRY(pause());
172 return result;
173}
174
175static bool EnterRecovery() {
176 const char msg_switch_to_recovery = 'r';
177
178 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
179 if (sock < 0) {
180 PLOG(ERROR) << "Couldn't create sock";
181 return false;
182 }
183
184 struct sockaddr_un addr = {.sun_family = AF_UNIX};
185 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
186 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
187 PLOG(ERROR) << "Couldn't connect to recovery";
188 return false;
189 }
190 // Switch to recovery will not update the boot reason since it does not
191 // require a reboot.
192 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
193 if (ret != sizeof(msg_switch_to_recovery)) {
194 PLOG(ERROR) << "Couldn't write message to switch to recovery";
195 return false;
196 }
197
198 return true;
199}
200
201bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
202 auto status = true;
203 if (EnterRecovery()) {
204 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
205 } else {
206 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
207 }
208 device->CloseDevice();
209 TEMP_FAILURE_RETRY(pause());
210 return status;
211}