blob: 6e45133f4b237467da5451e47366669e0e904096 [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);
Hridya Valsaraju20bdf892018-10-10 11:02:19 -0700240 if (result.isOk() && ret.success) {
241 // Save as slot suffix to match the suffix format as returned from
242 // the boot control HAL.
243 auto current_slot = "_" + args[1];
244 device->set_active_slot(current_slot);
245 return device->WriteStatus(FastbootResult::OKAY, "");
246 }
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700247 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700248}
249
250bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
251 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
252 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
253 device->CloseDevice();
254 TEMP_FAILURE_RETRY(pause());
255 return result;
256}
257
258bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
259 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
260 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
261 device->CloseDevice();
262 TEMP_FAILURE_RETRY(pause());
263 return result;
264}
265
266bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
267 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
268 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
269 device->CloseDevice();
270 TEMP_FAILURE_RETRY(pause());
271 return result;
272}
273
274bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
275 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
276 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
277 device->CloseDevice();
278 TEMP_FAILURE_RETRY(pause());
279 return result;
280}
281
282static bool EnterRecovery() {
283 const char msg_switch_to_recovery = 'r';
284
285 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
286 if (sock < 0) {
287 PLOG(ERROR) << "Couldn't create sock";
288 return false;
289 }
290
291 struct sockaddr_un addr = {.sun_family = AF_UNIX};
292 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
293 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
294 PLOG(ERROR) << "Couldn't connect to recovery";
295 return false;
296 }
297 // Switch to recovery will not update the boot reason since it does not
298 // require a reboot.
299 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
300 if (ret != sizeof(msg_switch_to_recovery)) {
301 PLOG(ERROR) << "Couldn't write message to switch to recovery";
302 return false;
303 }
304
305 return true;
306}
307
308bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
309 auto status = true;
310 if (EnterRecovery()) {
311 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
312 } else {
313 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
314 }
315 device->CloseDevice();
316 TEMP_FAILURE_RETRY(pause());
317 return status;
318}
David Anderson0d4277d2018-07-31 13:27:37 -0700319
320// Helper class for opening a handle to a MetadataBuilder and writing the new
321// partition table to the same place it was read.
322class PartitionBuilder {
323 public:
324 explicit PartitionBuilder(FastbootDevice* device);
325
326 bool Write();
327 bool Valid() const { return !!builder_; }
328 MetadataBuilder* operator->() const { return builder_.get(); }
329
330 private:
331 std::string super_device_;
332 uint32_t slot_number_;
333 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();
344 slot_number_ = SlotNumberForSlotSuffix(slot);
345 builder_ = MetadataBuilder::New(super_device_, slot_number_);
346}
347
348bool PartitionBuilder::Write() {
349 std::unique_ptr<LpMetadata> metadata = builder_->Export();
350 if (!metadata) {
351 return false;
352 }
353 return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_);
354}
355
356bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
357 if (args.size() < 3) {
358 return device->WriteFail("Invalid partition name and size");
359 }
360
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700361 if (GetDeviceLockStatus()) {
362 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
363 }
364
David Anderson0d4277d2018-07-31 13:27:37 -0700365 uint64_t partition_size;
366 std::string partition_name = args[1];
367 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
368 return device->WriteFail("Invalid partition size");
369 }
370
371 PartitionBuilder builder(device);
372 if (!builder.Valid()) {
373 return device->WriteFail("Could not open super partition");
374 }
375 // TODO(112433293) Disallow if the name is in the physical table as well.
376 if (builder->FindPartition(partition_name)) {
377 return device->WriteFail("Partition already exists");
378 }
379
David Andersone5f2f062018-10-03 13:49:23 -0700380 Partition* partition = builder->AddPartition(partition_name, 0);
David Anderson0d4277d2018-07-31 13:27:37 -0700381 if (!partition) {
382 return device->WriteFail("Failed to add partition");
383 }
384 if (!builder->ResizePartition(partition, partition_size)) {
385 builder->RemovePartition(partition_name);
386 return device->WriteFail("Not enough space for partition");
387 }
388 if (!builder.Write()) {
389 return device->WriteFail("Failed to write partition table");
390 }
391 return device->WriteOkay("Partition created");
392}
393
394bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
395 if (args.size() < 2) {
396 return device->WriteFail("Invalid partition name and size");
397 }
398
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700399 if (GetDeviceLockStatus()) {
400 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
401 }
402
David Anderson0d4277d2018-07-31 13:27:37 -0700403 PartitionBuilder builder(device);
404 if (!builder.Valid()) {
405 return device->WriteFail("Could not open super partition");
406 }
407 builder->RemovePartition(args[1]);
408 if (!builder.Write()) {
409 return device->WriteFail("Failed to write partition table");
410 }
411 return device->WriteOkay("Partition deleted");
412}
413
414bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
415 if (args.size() < 3) {
416 return device->WriteFail("Invalid partition name and size");
417 }
418
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700419 if (GetDeviceLockStatus()) {
420 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
421 }
422
David Anderson0d4277d2018-07-31 13:27:37 -0700423 uint64_t partition_size;
424 std::string partition_name = args[1];
425 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
426 return device->WriteFail("Invalid partition size");
427 }
428
429 PartitionBuilder builder(device);
430 if (!builder.Valid()) {
431 return device->WriteFail("Could not open super partition");
432 }
433
434 Partition* partition = builder->FindPartition(partition_name);
435 if (!partition) {
436 return device->WriteFail("Partition does not exist");
437 }
438 if (!builder->ResizePartition(partition, partition_size)) {
439 return device->WriteFail("Not enough space to resize partition");
440 }
441 if (!builder.Write()) {
442 return device->WriteFail("Failed to write partition table");
443 }
444 return device->WriteOkay("Partition resized");
445}
David Anderson38b3c7a2018-08-15 16:27:42 -0700446
447bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) {
448 if (args.size() < 2) {
449 return device->WriteFail("Invalid arguments");
450 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700451
452 if (GetDeviceLockStatus()) {
453 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
454 }
455
David Anderson38b3c7a2018-08-15 16:27:42 -0700456 bool wipe = (args.size() >= 3 && args[2] == "wipe");
457 return UpdateSuper(device, args[1], wipe);
458}