blob: 08744ec0235387921cbe97ec23ac7239c5c817c0 [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 Anderson90fe0a42018-11-05 18:01:32 -0800102 {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}},
103 {FB_VAR_SUPER_PARTITION_NAME, {GetSuperPartitionName, nullptr}}};
David Anderson0f626632018-08-31 16:44:25 -0700104
105 if (args.size() < 2) {
106 return device->WriteFail("Missing argument");
107 }
108
109 // Special case: return all variables that we can.
110 if (args[1] == "all") {
111 for (const auto& [name, handlers] : kVariableMap) {
112 GetAllVars(device, name, handlers);
113 }
114 return device->WriteOkay("");
115 }
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700116
117 // args[0] is command name, args[1] is variable.
118 auto found_variable = kVariableMap.find(args[1]);
119 if (found_variable == kVariableMap.end()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700120 return device->WriteFail("Unknown variable");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700121 }
122
David Anderson1fb3fd72018-08-31 14:40:22 -0700123 std::string message;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700124 std::vector<std::string> getvar_args(args.begin() + 2, args.end());
David Anderson0f626632018-08-31 16:44:25 -0700125 if (!found_variable->second.get(device, getvar_args, &message)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700126 return device->WriteFail(message);
127 }
128 return device->WriteOkay(message);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700129}
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700130
David Anderson12211d12018-07-24 15:21:20 -0700131bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) {
132 if (args.size() < 2) {
133 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
134 }
Hridya Valsarajud1e62312018-10-08 09:13:17 -0700135
136 if (GetDeviceLockStatus()) {
137 return device->WriteStatus(FastbootResult::FAIL, "Erase is not allowed on locked devices");
138 }
139
David Anderson12211d12018-07-24 15:21:20 -0700140 PartitionHandle handle;
141 if (!OpenPartition(device, args[1], &handle)) {
142 return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist");
143 }
144 if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) {
145 return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded");
146 }
147 return device->WriteStatus(FastbootResult::FAIL, "Erasing failed");
148}
149
Hridya Valsarajua15fe312018-09-14 13:58:21 -0700150bool OemCmdHandler(FastbootDevice* device, const std::vector<std::string>& args) {
151 auto fastboot_hal = device->fastboot_hal();
152 if (!fastboot_hal) {
153 return device->WriteStatus(FastbootResult::FAIL, "Unable to open fastboot HAL");
154 }
155
156 Result ret;
157 auto ret_val = fastboot_hal->doOemCommand(args[0], [&](Result result) { ret = result; });
158 if (!ret_val.isOk()) {
159 return device->WriteStatus(FastbootResult::FAIL, "Unable to do OEM command");
160 }
161 if (ret.status != Status::SUCCESS) {
162 return device->WriteStatus(FastbootResult::FAIL, ret.message);
163 }
164
165 return device->WriteStatus(FastbootResult::OKAY, ret.message);
166}
167
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700168bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
169 if (args.size() < 2) {
170 return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
171 }
Hridya Valsarajud1e62312018-10-08 09:13:17 -0700172
173 if (GetDeviceLockStatus()) {
174 return device->WriteStatus(FastbootResult::FAIL,
175 "Download is not allowed on locked devices");
176 }
177
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700178 // arg[0] is the command name, arg[1] contains size of data to be downloaded
179 unsigned int size;
Hridya Valsarajuaae84e82018-10-08 13:10:25 -0700180 if (!android::base::ParseUint("0x" + args[1], &size, kMaxDownloadSizeDefault)) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700181 return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
182 }
David Anderson12211d12018-07-24 15:21:20 -0700183 device->download_data().resize(size);
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700184 if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
185 return false;
186 }
187
David Anderson12211d12018-07-24 15:21:20 -0700188 if (device->HandleData(true, &device->download_data())) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700189 return device->WriteStatus(FastbootResult::OKAY, "");
190 }
191
192 PLOG(ERROR) << "Couldn't download data";
193 return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
194}
195
David Anderson12211d12018-07-24 15:21:20 -0700196bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) {
197 if (args.size() < 2) {
198 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
199 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700200
201 if (GetDeviceLockStatus()) {
202 return device->WriteStatus(FastbootResult::FAIL,
203 "Flashing is not allowed on locked devices");
204 }
205
David Anderson12211d12018-07-24 15:21:20 -0700206 int ret = Flash(device, args[1]);
207 if (ret < 0) {
208 return device->WriteStatus(FastbootResult::FAIL, strerror(-ret));
209 }
210 return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded");
211}
212
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700213bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
214 if (args.size() < 2) {
215 return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
216 }
217
Hridya Valsarajud1e62312018-10-08 09:13:17 -0700218 if (GetDeviceLockStatus()) {
219 return device->WriteStatus(FastbootResult::FAIL,
220 "set_active command is not allowed on locked devices");
221 }
222
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700223 // Slot suffix needs to be between 'a' and 'z'.
224 Slot slot;
225 if (!GetSlotNumber(args[1], &slot)) {
226 return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
227 }
228
229 // Non-A/B devices will not have a boot control HAL.
230 auto boot_control_hal = device->boot_control_hal();
231 if (!boot_control_hal) {
232 return device->WriteStatus(FastbootResult::FAIL,
233 "Cannot set slot: boot control HAL absent");
234 }
235 if (slot >= boot_control_hal->getNumberSlots()) {
236 return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
237 }
238 CommandResult ret;
239 auto cb = [&ret](CommandResult result) { ret = result; };
240 auto result = boot_control_hal->setActiveBootSlot(slot, cb);
Hridya Valsaraju20bdf892018-10-10 11:02:19 -0700241 if (result.isOk() && ret.success) {
242 // Save as slot suffix to match the suffix format as returned from
243 // the boot control HAL.
244 auto current_slot = "_" + args[1];
245 device->set_active_slot(current_slot);
246 return device->WriteStatus(FastbootResult::OKAY, "");
247 }
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700248 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700249}
250
251bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
252 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
253 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
254 device->CloseDevice();
255 TEMP_FAILURE_RETRY(pause());
256 return result;
257}
258
259bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
260 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
261 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
262 device->CloseDevice();
263 TEMP_FAILURE_RETRY(pause());
264 return result;
265}
266
267bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
268 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
269 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
270 device->CloseDevice();
271 TEMP_FAILURE_RETRY(pause());
272 return result;
273}
274
275bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
276 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
277 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
278 device->CloseDevice();
279 TEMP_FAILURE_RETRY(pause());
280 return result;
281}
282
283static bool EnterRecovery() {
284 const char msg_switch_to_recovery = 'r';
285
286 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
287 if (sock < 0) {
288 PLOG(ERROR) << "Couldn't create sock";
289 return false;
290 }
291
292 struct sockaddr_un addr = {.sun_family = AF_UNIX};
293 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
294 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
295 PLOG(ERROR) << "Couldn't connect to recovery";
296 return false;
297 }
298 // Switch to recovery will not update the boot reason since it does not
299 // require a reboot.
300 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
301 if (ret != sizeof(msg_switch_to_recovery)) {
302 PLOG(ERROR) << "Couldn't write message to switch to recovery";
303 return false;
304 }
305
306 return true;
307}
308
309bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
310 auto status = true;
311 if (EnterRecovery()) {
312 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
313 } else {
314 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
315 }
316 device->CloseDevice();
317 TEMP_FAILURE_RETRY(pause());
318 return status;
319}
David Anderson0d4277d2018-07-31 13:27:37 -0700320
321// Helper class for opening a handle to a MetadataBuilder and writing the new
322// partition table to the same place it was read.
323class PartitionBuilder {
324 public:
325 explicit PartitionBuilder(FastbootDevice* device);
326
327 bool Write();
328 bool Valid() const { return !!builder_; }
329 MetadataBuilder* operator->() const { return builder_.get(); }
330
331 private:
332 std::string super_device_;
David Anderson0d4277d2018-07-31 13:27:37 -0700333 std::unique_ptr<MetadataBuilder> builder_;
334};
335
336PartitionBuilder::PartitionBuilder(FastbootDevice* device) {
David Anderson5cbd2e42018-09-27 10:53:04 -0700337 auto super_device = FindPhysicalPartition(fs_mgr_get_super_partition_name());
David Anderson0d4277d2018-07-31 13:27:37 -0700338 if (!super_device) {
339 return;
340 }
341 super_device_ = *super_device;
342
343 std::string slot = device->GetCurrentSlot();
David Anderson63ffb442018-11-01 17:41:29 -0700344 uint32_t slot_number = SlotNumberForSlotSuffix(slot);
345 builder_ = MetadataBuilder::New(super_device_, slot_number);
David Anderson0d4277d2018-07-31 13:27:37 -0700346}
347
348bool PartitionBuilder::Write() {
349 std::unique_ptr<LpMetadata> metadata = builder_->Export();
350 if (!metadata) {
351 return false;
352 }
David Anderson63ffb442018-11-01 17:41:29 -0700353 bool ok = true;
354 for (uint32_t i = 0; i < metadata->geometry.metadata_slot_count; i++) {
355 ok &= UpdatePartitionTable(super_device_, *metadata.get(), i);
356 }
357 return ok;
David Anderson0d4277d2018-07-31 13:27:37 -0700358}
359
360bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
361 if (args.size() < 3) {
362 return device->WriteFail("Invalid partition name and size");
363 }
364
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700365 if (GetDeviceLockStatus()) {
366 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
367 }
368
David Anderson0d4277d2018-07-31 13:27:37 -0700369 uint64_t partition_size;
370 std::string partition_name = args[1];
371 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
372 return device->WriteFail("Invalid partition size");
373 }
374
375 PartitionBuilder builder(device);
376 if (!builder.Valid()) {
377 return device->WriteFail("Could not open super partition");
378 }
379 // TODO(112433293) Disallow if the name is in the physical table as well.
380 if (builder->FindPartition(partition_name)) {
381 return device->WriteFail("Partition already exists");
382 }
383
David Andersone5f2f062018-10-03 13:49:23 -0700384 Partition* partition = builder->AddPartition(partition_name, 0);
David Anderson0d4277d2018-07-31 13:27:37 -0700385 if (!partition) {
386 return device->WriteFail("Failed to add partition");
387 }
388 if (!builder->ResizePartition(partition, partition_size)) {
389 builder->RemovePartition(partition_name);
390 return device->WriteFail("Not enough space for partition");
391 }
392 if (!builder.Write()) {
393 return device->WriteFail("Failed to write partition table");
394 }
395 return device->WriteOkay("Partition created");
396}
397
398bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
399 if (args.size() < 2) {
400 return device->WriteFail("Invalid partition name and size");
401 }
402
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700403 if (GetDeviceLockStatus()) {
404 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
405 }
406
David Anderson0d4277d2018-07-31 13:27:37 -0700407 PartitionBuilder builder(device);
408 if (!builder.Valid()) {
409 return device->WriteFail("Could not open super partition");
410 }
411 builder->RemovePartition(args[1]);
412 if (!builder.Write()) {
413 return device->WriteFail("Failed to write partition table");
414 }
415 return device->WriteOkay("Partition deleted");
416}
417
418bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
419 if (args.size() < 3) {
420 return device->WriteFail("Invalid partition name and size");
421 }
422
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700423 if (GetDeviceLockStatus()) {
424 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
425 }
426
David Anderson0d4277d2018-07-31 13:27:37 -0700427 uint64_t partition_size;
428 std::string partition_name = args[1];
429 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
430 return device->WriteFail("Invalid partition size");
431 }
432
433 PartitionBuilder builder(device);
434 if (!builder.Valid()) {
435 return device->WriteFail("Could not open super partition");
436 }
437
438 Partition* partition = builder->FindPartition(partition_name);
439 if (!partition) {
440 return device->WriteFail("Partition does not exist");
441 }
442 if (!builder->ResizePartition(partition, partition_size)) {
443 return device->WriteFail("Not enough space to resize partition");
444 }
445 if (!builder.Write()) {
446 return device->WriteFail("Failed to write partition table");
447 }
448 return device->WriteOkay("Partition resized");
449}
David Anderson38b3c7a2018-08-15 16:27:42 -0700450
451bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) {
452 if (args.size() < 2) {
453 return device->WriteFail("Invalid arguments");
454 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700455
456 if (GetDeviceLockStatus()) {
457 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
458 }
459
David Anderson38b3c7a2018-08-15 16:27:42 -0700460 bool wipe = (args.size() >= 3 && args[2] == "wipe");
461 return UpdateSuper(device, args[1], wipe);
462}