blob: 863a1e6fdc6f31d2e4f877456abf239f677d0e33 [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 }
134 PartitionHandle handle;
135 if (!OpenPartition(device, args[1], &handle)) {
136 return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist");
137 }
138 if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) {
139 return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded");
140 }
141 return device->WriteStatus(FastbootResult::FAIL, "Erasing failed");
142}
143
Hridya Valsarajua15fe312018-09-14 13:58:21 -0700144bool OemCmdHandler(FastbootDevice* device, const std::vector<std::string>& args) {
145 auto fastboot_hal = device->fastboot_hal();
146 if (!fastboot_hal) {
147 return device->WriteStatus(FastbootResult::FAIL, "Unable to open fastboot HAL");
148 }
149
150 Result ret;
151 auto ret_val = fastboot_hal->doOemCommand(args[0], [&](Result result) { ret = result; });
152 if (!ret_val.isOk()) {
153 return device->WriteStatus(FastbootResult::FAIL, "Unable to do OEM command");
154 }
155 if (ret.status != Status::SUCCESS) {
156 return device->WriteStatus(FastbootResult::FAIL, ret.message);
157 }
158
159 return device->WriteStatus(FastbootResult::OKAY, ret.message);
160}
161
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700162bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
163 if (args.size() < 2) {
164 return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
165 }
166 // arg[0] is the command name, arg[1] contains size of data to be downloaded
167 unsigned int size;
168 if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) {
169 return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
170 }
David Anderson12211d12018-07-24 15:21:20 -0700171 device->download_data().resize(size);
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700172 if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
173 return false;
174 }
175
David Anderson12211d12018-07-24 15:21:20 -0700176 if (device->HandleData(true, &device->download_data())) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700177 return device->WriteStatus(FastbootResult::OKAY, "");
178 }
179
180 PLOG(ERROR) << "Couldn't download data";
181 return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
182}
183
David Anderson12211d12018-07-24 15:21:20 -0700184bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) {
185 if (args.size() < 2) {
186 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
187 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700188
189 if (GetDeviceLockStatus()) {
190 return device->WriteStatus(FastbootResult::FAIL,
191 "Flashing is not allowed on locked devices");
192 }
193
David Anderson12211d12018-07-24 15:21:20 -0700194 int ret = Flash(device, args[1]);
195 if (ret < 0) {
196 return device->WriteStatus(FastbootResult::FAIL, strerror(-ret));
197 }
198 return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded");
199}
200
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700201bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
202 if (args.size() < 2) {
203 return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
204 }
205
206 // Slot suffix needs to be between 'a' and 'z'.
207 Slot slot;
208 if (!GetSlotNumber(args[1], &slot)) {
209 return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
210 }
211
212 // Non-A/B devices will not have a boot control HAL.
213 auto boot_control_hal = device->boot_control_hal();
214 if (!boot_control_hal) {
215 return device->WriteStatus(FastbootResult::FAIL,
216 "Cannot set slot: boot control HAL absent");
217 }
218 if (slot >= boot_control_hal->getNumberSlots()) {
219 return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
220 }
221 CommandResult ret;
222 auto cb = [&ret](CommandResult result) { ret = result; };
223 auto result = boot_control_hal->setActiveBootSlot(slot, cb);
Hridya Valsaraju20bdf892018-10-10 11:02:19 -0700224 if (result.isOk() && ret.success) {
225 // Save as slot suffix to match the suffix format as returned from
226 // the boot control HAL.
227 auto current_slot = "_" + args[1];
228 device->set_active_slot(current_slot);
229 return device->WriteStatus(FastbootResult::OKAY, "");
230 }
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700231 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700232}
233
234bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
235 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
236 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
237 device->CloseDevice();
238 TEMP_FAILURE_RETRY(pause());
239 return result;
240}
241
242bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
243 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
244 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
245 device->CloseDevice();
246 TEMP_FAILURE_RETRY(pause());
247 return result;
248}
249
250bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
251 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
252 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
253 device->CloseDevice();
254 TEMP_FAILURE_RETRY(pause());
255 return result;
256}
257
258bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
259 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
260 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
261 device->CloseDevice();
262 TEMP_FAILURE_RETRY(pause());
263 return result;
264}
265
266static bool EnterRecovery() {
267 const char msg_switch_to_recovery = 'r';
268
269 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
270 if (sock < 0) {
271 PLOG(ERROR) << "Couldn't create sock";
272 return false;
273 }
274
275 struct sockaddr_un addr = {.sun_family = AF_UNIX};
276 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
277 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
278 PLOG(ERROR) << "Couldn't connect to recovery";
279 return false;
280 }
281 // Switch to recovery will not update the boot reason since it does not
282 // require a reboot.
283 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
284 if (ret != sizeof(msg_switch_to_recovery)) {
285 PLOG(ERROR) << "Couldn't write message to switch to recovery";
286 return false;
287 }
288
289 return true;
290}
291
292bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
293 auto status = true;
294 if (EnterRecovery()) {
295 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
296 } else {
297 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
298 }
299 device->CloseDevice();
300 TEMP_FAILURE_RETRY(pause());
301 return status;
302}
David Anderson0d4277d2018-07-31 13:27:37 -0700303
304// Helper class for opening a handle to a MetadataBuilder and writing the new
305// partition table to the same place it was read.
306class PartitionBuilder {
307 public:
308 explicit PartitionBuilder(FastbootDevice* device);
309
310 bool Write();
311 bool Valid() const { return !!builder_; }
312 MetadataBuilder* operator->() const { return builder_.get(); }
313
314 private:
315 std::string super_device_;
316 uint32_t slot_number_;
317 std::unique_ptr<MetadataBuilder> builder_;
318};
319
320PartitionBuilder::PartitionBuilder(FastbootDevice* device) {
David Anderson5cbd2e42018-09-27 10:53:04 -0700321 auto super_device = FindPhysicalPartition(fs_mgr_get_super_partition_name());
David Anderson0d4277d2018-07-31 13:27:37 -0700322 if (!super_device) {
323 return;
324 }
325 super_device_ = *super_device;
326
327 std::string slot = device->GetCurrentSlot();
328 slot_number_ = SlotNumberForSlotSuffix(slot);
329 builder_ = MetadataBuilder::New(super_device_, slot_number_);
330}
331
332bool PartitionBuilder::Write() {
333 std::unique_ptr<LpMetadata> metadata = builder_->Export();
334 if (!metadata) {
335 return false;
336 }
337 return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_);
338}
339
340bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
341 if (args.size() < 3) {
342 return device->WriteFail("Invalid partition name and size");
343 }
344
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700345 if (GetDeviceLockStatus()) {
346 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
347 }
348
David Anderson0d4277d2018-07-31 13:27:37 -0700349 uint64_t partition_size;
350 std::string partition_name = args[1];
351 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
352 return device->WriteFail("Invalid partition size");
353 }
354
355 PartitionBuilder builder(device);
356 if (!builder.Valid()) {
357 return device->WriteFail("Could not open super partition");
358 }
359 // TODO(112433293) Disallow if the name is in the physical table as well.
360 if (builder->FindPartition(partition_name)) {
361 return device->WriteFail("Partition already exists");
362 }
363
David Andersone5f2f062018-10-03 13:49:23 -0700364 Partition* partition = builder->AddPartition(partition_name, 0);
David Anderson0d4277d2018-07-31 13:27:37 -0700365 if (!partition) {
366 return device->WriteFail("Failed to add partition");
367 }
368 if (!builder->ResizePartition(partition, partition_size)) {
369 builder->RemovePartition(partition_name);
370 return device->WriteFail("Not enough space for partition");
371 }
372 if (!builder.Write()) {
373 return device->WriteFail("Failed to write partition table");
374 }
375 return device->WriteOkay("Partition created");
376}
377
378bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
379 if (args.size() < 2) {
380 return device->WriteFail("Invalid partition name and size");
381 }
382
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700383 if (GetDeviceLockStatus()) {
384 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
385 }
386
David Anderson0d4277d2018-07-31 13:27:37 -0700387 PartitionBuilder builder(device);
388 if (!builder.Valid()) {
389 return device->WriteFail("Could not open super partition");
390 }
391 builder->RemovePartition(args[1]);
392 if (!builder.Write()) {
393 return device->WriteFail("Failed to write partition table");
394 }
395 return device->WriteOkay("Partition deleted");
396}
397
398bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
399 if (args.size() < 3) {
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 uint64_t partition_size;
408 std::string partition_name = args[1];
409 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
410 return device->WriteFail("Invalid partition size");
411 }
412
413 PartitionBuilder builder(device);
414 if (!builder.Valid()) {
415 return device->WriteFail("Could not open super partition");
416 }
417
418 Partition* partition = builder->FindPartition(partition_name);
419 if (!partition) {
420 return device->WriteFail("Partition does not exist");
421 }
422 if (!builder->ResizePartition(partition, partition_size)) {
423 return device->WriteFail("Not enough space to resize partition");
424 }
425 if (!builder.Write()) {
426 return device->WriteFail("Failed to write partition table");
427 }
428 return device->WriteOkay("Partition resized");
429}
David Anderson38b3c7a2018-08-15 16:27:42 -0700430
431bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) {
432 if (args.size() < 2) {
433 return device->WriteFail("Invalid arguments");
434 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700435
436 if (GetDeviceLockStatus()) {
437 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
438 }
439
David Anderson38b3c7a2018-08-15 16:27:42 -0700440 bool wipe = (args.size() >= 3 && args[2] == "wipe");
441 return UpdateSuper(device, args[1], wipe);
442}