blob: b02d968c5b4074c423f72afd8b27b0964ce9417b [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>
David Anderson0d4277d2018-07-31 13:27:37 -070030#include <liblp/builder.h>
31#include <liblp/liblp.h>
32#include <uuid/uuid.h>
Hridya Valsarajudea91b42018-07-17 11:14:01 -070033
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070034#include "constants.h"
Hridya Valsarajudea91b42018-07-17 11:14:01 -070035#include "fastboot_device.h"
David Anderson12211d12018-07-24 15:21:20 -070036#include "flashing.h"
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070037#include "utility.h"
38
39using ::android::hardware::hidl_string;
40using ::android::hardware::boot::V1_0::BoolResult;
41using ::android::hardware::boot::V1_0::CommandResult;
42using ::android::hardware::boot::V1_0::Slot;
Hridya Valsarajua15fe312018-09-14 13:58:21 -070043using ::android::hardware::fastboot::V1_0::Result;
44using ::android::hardware::fastboot::V1_0::Status;
45
David Anderson0d4277d2018-07-31 13:27:37 -070046using namespace android::fs_mgr;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070047
David Anderson0f626632018-08-31 16:44:25 -070048struct VariableHandlers {
49 // Callback to retrieve the value of a single variable.
50 std::function<bool(FastbootDevice*, const std::vector<std::string>&, std::string*)> get;
51 // Callback to retrieve all possible argument combinations, for getvar all.
52 std::function<std::vector<std::vector<std::string>>(FastbootDevice*)> get_all_args;
53};
54
55static void GetAllVars(FastbootDevice* device, const std::string& name,
56 const VariableHandlers& handlers) {
57 if (!handlers.get_all_args) {
58 std::string message;
59 if (!handlers.get(device, std::vector<std::string>(), &message)) {
60 return;
61 }
62 device->WriteInfo(android::base::StringPrintf("%s:%s", name.c_str(), message.c_str()));
63 return;
64 }
65
66 auto all_args = handlers.get_all_args(device);
67 for (const auto& args : all_args) {
68 std::string message;
69 if (!handlers.get(device, args, &message)) {
70 continue;
71 }
72 std::string arg_string = android::base::Join(args, ":");
73 device->WriteInfo(android::base::StringPrintf("%s:%s:%s", name.c_str(), arg_string.c_str(),
74 message.c_str()));
75 }
76}
77
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070078bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) {
David Anderson0f626632018-08-31 16:44:25 -070079 const std::unordered_map<std::string, VariableHandlers> kVariableMap = {
80 {FB_VAR_VERSION, {GetVersion, nullptr}},
81 {FB_VAR_VERSION_BOOTLOADER, {GetBootloaderVersion, nullptr}},
82 {FB_VAR_VERSION_BASEBAND, {GetBasebandVersion, nullptr}},
83 {FB_VAR_PRODUCT, {GetProduct, nullptr}},
84 {FB_VAR_SERIALNO, {GetSerial, nullptr}},
Hridya Valsaraju4af80902018-09-26 13:08:16 -070085 {FB_VAR_VARIANT, {GetVariant, nullptr}},
David Anderson0f626632018-08-31 16:44:25 -070086 {FB_VAR_SECURE, {GetSecure, nullptr}},
87 {FB_VAR_UNLOCKED, {GetUnlocked, nullptr}},
88 {FB_VAR_MAX_DOWNLOAD_SIZE, {GetMaxDownloadSize, nullptr}},
89 {FB_VAR_CURRENT_SLOT, {::GetCurrentSlot, nullptr}},
90 {FB_VAR_SLOT_COUNT, {GetSlotCount, nullptr}},
91 {FB_VAR_HAS_SLOT, {GetHasSlot, GetAllPartitionArgsNoSlot}},
92 {FB_VAR_SLOT_SUCCESSFUL, {GetSlotSuccessful, nullptr}},
93 {FB_VAR_SLOT_UNBOOTABLE, {GetSlotUnbootable, nullptr}},
94 {FB_VAR_PARTITION_SIZE, {GetPartitionSize, GetAllPartitionArgsWithSlot}},
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -070095 {FB_VAR_PARTITION_TYPE, {GetPartitionType, GetAllPartitionArgsWithSlot}},
David Anderson0f626632018-08-31 16:44:25 -070096 {FB_VAR_IS_LOGICAL, {GetPartitionIsLogical, GetAllPartitionArgsWithSlot}},
David Andersonc091c172018-09-04 18:11:03 -070097 {FB_VAR_IS_USERSPACE, {GetIsUserspace, nullptr}},
Hridya Valsaraju7c9bbe92018-09-27 10:41:01 -070098 {FB_VAR_OFF_MODE_CHARGE_STATE, {GetOffModeChargeState, nullptr}},
Hridya Valsaraju47658ca2018-09-28 11:41:22 -070099 {FB_VAR_BATTERY_VOLTAGE, {GetBatteryVoltage, nullptr}},
David Andersonc091c172018-09-04 18:11:03 -0700100 {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}}};
David Anderson0f626632018-08-31 16:44:25 -0700101
102 if (args.size() < 2) {
103 return device->WriteFail("Missing argument");
104 }
105
106 // Special case: return all variables that we can.
107 if (args[1] == "all") {
108 for (const auto& [name, handlers] : kVariableMap) {
109 GetAllVars(device, name, handlers);
110 }
111 return device->WriteOkay("");
112 }
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700113
114 // args[0] is command name, args[1] is variable.
115 auto found_variable = kVariableMap.find(args[1]);
116 if (found_variable == kVariableMap.end()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700117 return device->WriteFail("Unknown variable");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700118 }
119
David Anderson1fb3fd72018-08-31 14:40:22 -0700120 std::string message;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700121 std::vector<std::string> getvar_args(args.begin() + 2, args.end());
David Anderson0f626632018-08-31 16:44:25 -0700122 if (!found_variable->second.get(device, getvar_args, &message)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700123 return device->WriteFail(message);
124 }
125 return device->WriteOkay(message);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700126}
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700127
David Anderson12211d12018-07-24 15:21:20 -0700128bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) {
129 if (args.size() < 2) {
130 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
131 }
132 PartitionHandle handle;
133 if (!OpenPartition(device, args[1], &handle)) {
134 return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist");
135 }
136 if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) {
137 return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded");
138 }
139 return device->WriteStatus(FastbootResult::FAIL, "Erasing failed");
140}
141
Hridya Valsarajua15fe312018-09-14 13:58:21 -0700142bool OemCmdHandler(FastbootDevice* device, const std::vector<std::string>& args) {
143 auto fastboot_hal = device->fastboot_hal();
144 if (!fastboot_hal) {
145 return device->WriteStatus(FastbootResult::FAIL, "Unable to open fastboot HAL");
146 }
147
148 Result ret;
149 auto ret_val = fastboot_hal->doOemCommand(args[0], [&](Result result) { ret = result; });
150 if (!ret_val.isOk()) {
151 return device->WriteStatus(FastbootResult::FAIL, "Unable to do OEM command");
152 }
153 if (ret.status != Status::SUCCESS) {
154 return device->WriteStatus(FastbootResult::FAIL, ret.message);
155 }
156
157 return device->WriteStatus(FastbootResult::OKAY, ret.message);
158}
159
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700160bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
161 if (args.size() < 2) {
162 return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
163 }
164 // arg[0] is the command name, arg[1] contains size of data to be downloaded
165 unsigned int size;
166 if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) {
167 return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
168 }
David Anderson12211d12018-07-24 15:21:20 -0700169 device->download_data().resize(size);
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700170 if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
171 return false;
172 }
173
David Anderson12211d12018-07-24 15:21:20 -0700174 if (device->HandleData(true, &device->download_data())) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700175 return device->WriteStatus(FastbootResult::OKAY, "");
176 }
177
178 PLOG(ERROR) << "Couldn't download data";
179 return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
180}
181
David Anderson12211d12018-07-24 15:21:20 -0700182bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) {
183 if (args.size() < 2) {
184 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
185 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700186
187 if (GetDeviceLockStatus()) {
188 return device->WriteStatus(FastbootResult::FAIL,
189 "Flashing is not allowed on locked devices");
190 }
191
David Anderson12211d12018-07-24 15:21:20 -0700192 int ret = Flash(device, args[1]);
193 if (ret < 0) {
194 return device->WriteStatus(FastbootResult::FAIL, strerror(-ret));
195 }
196 return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded");
197}
198
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700199bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
200 if (args.size() < 2) {
201 return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
202 }
203
204 // Slot suffix needs to be between 'a' and 'z'.
205 Slot slot;
206 if (!GetSlotNumber(args[1], &slot)) {
207 return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
208 }
209
210 // Non-A/B devices will not have a boot control HAL.
211 auto boot_control_hal = device->boot_control_hal();
212 if (!boot_control_hal) {
213 return device->WriteStatus(FastbootResult::FAIL,
214 "Cannot set slot: boot control HAL absent");
215 }
216 if (slot >= boot_control_hal->getNumberSlots()) {
217 return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
218 }
219 CommandResult ret;
220 auto cb = [&ret](CommandResult result) { ret = result; };
221 auto result = boot_control_hal->setActiveBootSlot(slot, cb);
222 if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, "");
223 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700224}
225
226bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
227 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
228 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
229 device->CloseDevice();
230 TEMP_FAILURE_RETRY(pause());
231 return result;
232}
233
234bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
235 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
236 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
237 device->CloseDevice();
238 TEMP_FAILURE_RETRY(pause());
239 return result;
240}
241
242bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
243 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
244 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
245 device->CloseDevice();
246 TEMP_FAILURE_RETRY(pause());
247 return result;
248}
249
250bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
251 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
252 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
253 device->CloseDevice();
254 TEMP_FAILURE_RETRY(pause());
255 return result;
256}
257
258static bool EnterRecovery() {
259 const char msg_switch_to_recovery = 'r';
260
261 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
262 if (sock < 0) {
263 PLOG(ERROR) << "Couldn't create sock";
264 return false;
265 }
266
267 struct sockaddr_un addr = {.sun_family = AF_UNIX};
268 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
269 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
270 PLOG(ERROR) << "Couldn't connect to recovery";
271 return false;
272 }
273 // Switch to recovery will not update the boot reason since it does not
274 // require a reboot.
275 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
276 if (ret != sizeof(msg_switch_to_recovery)) {
277 PLOG(ERROR) << "Couldn't write message to switch to recovery";
278 return false;
279 }
280
281 return true;
282}
283
284bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
285 auto status = true;
286 if (EnterRecovery()) {
287 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
288 } else {
289 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
290 }
291 device->CloseDevice();
292 TEMP_FAILURE_RETRY(pause());
293 return status;
294}
David Anderson0d4277d2018-07-31 13:27:37 -0700295
296// Helper class for opening a handle to a MetadataBuilder and writing the new
297// partition table to the same place it was read.
298class PartitionBuilder {
299 public:
300 explicit PartitionBuilder(FastbootDevice* device);
301
302 bool Write();
303 bool Valid() const { return !!builder_; }
304 MetadataBuilder* operator->() const { return builder_.get(); }
305
306 private:
307 std::string super_device_;
308 uint32_t slot_number_;
309 std::unique_ptr<MetadataBuilder> builder_;
310};
311
312PartitionBuilder::PartitionBuilder(FastbootDevice* device) {
313 auto super_device = FindPhysicalPartition(LP_METADATA_PARTITION_NAME);
314 if (!super_device) {
315 return;
316 }
317 super_device_ = *super_device;
318
319 std::string slot = device->GetCurrentSlot();
320 slot_number_ = SlotNumberForSlotSuffix(slot);
321 builder_ = MetadataBuilder::New(super_device_, slot_number_);
322}
323
324bool PartitionBuilder::Write() {
325 std::unique_ptr<LpMetadata> metadata = builder_->Export();
326 if (!metadata) {
327 return false;
328 }
329 return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_);
330}
331
332bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
333 if (args.size() < 3) {
334 return device->WriteFail("Invalid partition name and size");
335 }
336
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700337 if (GetDeviceLockStatus()) {
338 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
339 }
340
David Anderson0d4277d2018-07-31 13:27:37 -0700341 uint64_t partition_size;
342 std::string partition_name = args[1];
343 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
344 return device->WriteFail("Invalid partition size");
345 }
346
347 PartitionBuilder builder(device);
348 if (!builder.Valid()) {
349 return device->WriteFail("Could not open super partition");
350 }
351 // TODO(112433293) Disallow if the name is in the physical table as well.
352 if (builder->FindPartition(partition_name)) {
353 return device->WriteFail("Partition already exists");
354 }
355
356 // Make a random UUID, since they're not currently used.
357 uuid_t uuid;
358 char uuid_str[37];
359 uuid_generate_random(uuid);
360 uuid_unparse(uuid, uuid_str);
361
362 Partition* partition = builder->AddPartition(partition_name, uuid_str, 0);
363 if (!partition) {
364 return device->WriteFail("Failed to add partition");
365 }
366 if (!builder->ResizePartition(partition, partition_size)) {
367 builder->RemovePartition(partition_name);
368 return device->WriteFail("Not enough space for partition");
369 }
370 if (!builder.Write()) {
371 return device->WriteFail("Failed to write partition table");
372 }
373 return device->WriteOkay("Partition created");
374}
375
376bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
377 if (args.size() < 2) {
378 return device->WriteFail("Invalid partition name and size");
379 }
380
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700381 if (GetDeviceLockStatus()) {
382 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
383 }
384
David Anderson0d4277d2018-07-31 13:27:37 -0700385 PartitionBuilder builder(device);
386 if (!builder.Valid()) {
387 return device->WriteFail("Could not open super partition");
388 }
389 builder->RemovePartition(args[1]);
390 if (!builder.Write()) {
391 return device->WriteFail("Failed to write partition table");
392 }
393 return device->WriteOkay("Partition deleted");
394}
395
396bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
397 if (args.size() < 3) {
398 return device->WriteFail("Invalid partition name and size");
399 }
400
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700401 if (GetDeviceLockStatus()) {
402 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
403 }
404
David Anderson0d4277d2018-07-31 13:27:37 -0700405 uint64_t partition_size;
406 std::string partition_name = args[1];
407 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
408 return device->WriteFail("Invalid partition size");
409 }
410
411 PartitionBuilder builder(device);
412 if (!builder.Valid()) {
413 return device->WriteFail("Could not open super partition");
414 }
415
416 Partition* partition = builder->FindPartition(partition_name);
417 if (!partition) {
418 return device->WriteFail("Partition does not exist");
419 }
420 if (!builder->ResizePartition(partition, partition_size)) {
421 return device->WriteFail("Not enough space to resize partition");
422 }
423 if (!builder.Write()) {
424 return device->WriteFail("Failed to write partition table");
425 }
426 return device->WriteOkay("Partition resized");
427}
David Anderson38b3c7a2018-08-15 16:27:42 -0700428
429bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) {
430 if (args.size() < 2) {
431 return device->WriteFail("Invalid arguments");
432 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700433
434 if (GetDeviceLockStatus()) {
435 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
436 }
437
David Anderson38b3c7a2018-08-15 16:27:42 -0700438 bool wipe = (args.size() >= 3 && args[2] == "wipe");
439 return UpdateSuper(device, args[1], wipe);
440}