blob: 3f663ef8eeed8f28eee02d7683c51c26bb48dbf4 [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);
224 if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, "");
225 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700226}
227
228bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
229 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
230 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
231 device->CloseDevice();
232 TEMP_FAILURE_RETRY(pause());
233 return result;
234}
235
236bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
237 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
238 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
239 device->CloseDevice();
240 TEMP_FAILURE_RETRY(pause());
241 return result;
242}
243
244bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
245 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
246 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
247 device->CloseDevice();
248 TEMP_FAILURE_RETRY(pause());
249 return result;
250}
251
252bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
253 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
254 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
255 device->CloseDevice();
256 TEMP_FAILURE_RETRY(pause());
257 return result;
258}
259
260static bool EnterRecovery() {
261 const char msg_switch_to_recovery = 'r';
262
263 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
264 if (sock < 0) {
265 PLOG(ERROR) << "Couldn't create sock";
266 return false;
267 }
268
269 struct sockaddr_un addr = {.sun_family = AF_UNIX};
270 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
271 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
272 PLOG(ERROR) << "Couldn't connect to recovery";
273 return false;
274 }
275 // Switch to recovery will not update the boot reason since it does not
276 // require a reboot.
277 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
278 if (ret != sizeof(msg_switch_to_recovery)) {
279 PLOG(ERROR) << "Couldn't write message to switch to recovery";
280 return false;
281 }
282
283 return true;
284}
285
286bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
287 auto status = true;
288 if (EnterRecovery()) {
289 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
290 } else {
291 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
292 }
293 device->CloseDevice();
294 TEMP_FAILURE_RETRY(pause());
295 return status;
296}
David Anderson0d4277d2018-07-31 13:27:37 -0700297
298// Helper class for opening a handle to a MetadataBuilder and writing the new
299// partition table to the same place it was read.
300class PartitionBuilder {
301 public:
302 explicit PartitionBuilder(FastbootDevice* device);
303
304 bool Write();
305 bool Valid() const { return !!builder_; }
306 MetadataBuilder* operator->() const { return builder_.get(); }
307
308 private:
309 std::string super_device_;
310 uint32_t slot_number_;
311 std::unique_ptr<MetadataBuilder> builder_;
312};
313
314PartitionBuilder::PartitionBuilder(FastbootDevice* device) {
David Anderson5cbd2e42018-09-27 10:53:04 -0700315 auto super_device = FindPhysicalPartition(fs_mgr_get_super_partition_name());
David Anderson0d4277d2018-07-31 13:27:37 -0700316 if (!super_device) {
317 return;
318 }
319 super_device_ = *super_device;
320
321 std::string slot = device->GetCurrentSlot();
322 slot_number_ = SlotNumberForSlotSuffix(slot);
323 builder_ = MetadataBuilder::New(super_device_, slot_number_);
324}
325
326bool PartitionBuilder::Write() {
327 std::unique_ptr<LpMetadata> metadata = builder_->Export();
328 if (!metadata) {
329 return false;
330 }
331 return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_);
332}
333
334bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
335 if (args.size() < 3) {
336 return device->WriteFail("Invalid partition name and size");
337 }
338
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700339 if (GetDeviceLockStatus()) {
340 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
341 }
342
David Anderson0d4277d2018-07-31 13:27:37 -0700343 uint64_t partition_size;
344 std::string partition_name = args[1];
345 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
346 return device->WriteFail("Invalid partition size");
347 }
348
349 PartitionBuilder builder(device);
350 if (!builder.Valid()) {
351 return device->WriteFail("Could not open super partition");
352 }
353 // TODO(112433293) Disallow if the name is in the physical table as well.
354 if (builder->FindPartition(partition_name)) {
355 return device->WriteFail("Partition already exists");
356 }
357
David Andersone5f2f062018-10-03 13:49:23 -0700358 Partition* partition = builder->AddPartition(partition_name, 0);
David Anderson0d4277d2018-07-31 13:27:37 -0700359 if (!partition) {
360 return device->WriteFail("Failed to add partition");
361 }
362 if (!builder->ResizePartition(partition, partition_size)) {
363 builder->RemovePartition(partition_name);
364 return device->WriteFail("Not enough space for partition");
365 }
366 if (!builder.Write()) {
367 return device->WriteFail("Failed to write partition table");
368 }
369 return device->WriteOkay("Partition created");
370}
371
372bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
373 if (args.size() < 2) {
374 return device->WriteFail("Invalid partition name and size");
375 }
376
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700377 if (GetDeviceLockStatus()) {
378 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
379 }
380
David Anderson0d4277d2018-07-31 13:27:37 -0700381 PartitionBuilder builder(device);
382 if (!builder.Valid()) {
383 return device->WriteFail("Could not open super partition");
384 }
385 builder->RemovePartition(args[1]);
386 if (!builder.Write()) {
387 return device->WriteFail("Failed to write partition table");
388 }
389 return device->WriteOkay("Partition deleted");
390}
391
392bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
393 if (args.size() < 3) {
394 return device->WriteFail("Invalid partition name and size");
395 }
396
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700397 if (GetDeviceLockStatus()) {
398 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
399 }
400
David Anderson0d4277d2018-07-31 13:27:37 -0700401 uint64_t partition_size;
402 std::string partition_name = args[1];
403 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
404 return device->WriteFail("Invalid partition size");
405 }
406
407 PartitionBuilder builder(device);
408 if (!builder.Valid()) {
409 return device->WriteFail("Could not open super partition");
410 }
411
412 Partition* partition = builder->FindPartition(partition_name);
413 if (!partition) {
414 return device->WriteFail("Partition does not exist");
415 }
416 if (!builder->ResizePartition(partition, partition_size)) {
417 return device->WriteFail("Not enough space to resize partition");
418 }
419 if (!builder.Write()) {
420 return device->WriteFail("Failed to write partition table");
421 }
422 return device->WriteOkay("Partition resized");
423}
David Anderson38b3c7a2018-08-15 16:27:42 -0700424
425bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) {
426 if (args.size() < 2) {
427 return device->WriteFail("Invalid arguments");
428 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700429
430 if (GetDeviceLockStatus()) {
431 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
432 }
433
David Anderson38b3c7a2018-08-15 16:27:42 -0700434 bool wipe = (args.size() >= 3 && args[2] == "wipe");
435 return UpdateSuper(device, args[1], wipe);
436}