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