blob: b7cafac4a4a492999f27e8a6f176ba82942c46ec [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 Anderson0d4277d2018-07-31 13:27:37 -070030#include <liblp/builder.h>
31#include <liblp/liblp.h>
32#include <uuid/uuid.h>
Hridya Valsarajudea91b42018-07-17 11:14:01 -070033
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070034#include "constants.h"
Hridya Valsarajudea91b42018-07-17 11:14:01 -070035#include "fastboot_device.h"
David Anderson12211d12018-07-24 15:21:20 -070036#include "flashing.h"
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070037#include "utility.h"
38
39using ::android::hardware::hidl_string;
40using ::android::hardware::boot::V1_0::BoolResult;
41using ::android::hardware::boot::V1_0::CommandResult;
42using ::android::hardware::boot::V1_0::Slot;
Hridya Valsarajua15fe312018-09-14 13:58:21 -070043using ::android::hardware::fastboot::V1_0::Result;
44using ::android::hardware::fastboot::V1_0::Status;
45
David Anderson0d4277d2018-07-31 13:27:37 -070046using namespace android::fs_mgr;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070047
David Anderson0f626632018-08-31 16:44:25 -070048struct VariableHandlers {
49 // Callback to retrieve the value of a single variable.
50 std::function<bool(FastbootDevice*, const std::vector<std::string>&, std::string*)> get;
51 // Callback to retrieve all possible argument combinations, for getvar all.
52 std::function<std::vector<std::vector<std::string>>(FastbootDevice*)> get_all_args;
53};
54
55static void GetAllVars(FastbootDevice* device, const std::string& name,
56 const VariableHandlers& handlers) {
57 if (!handlers.get_all_args) {
58 std::string message;
59 if (!handlers.get(device, std::vector<std::string>(), &message)) {
60 return;
61 }
62 device->WriteInfo(android::base::StringPrintf("%s:%s", name.c_str(), message.c_str()));
63 return;
64 }
65
66 auto all_args = handlers.get_all_args(device);
67 for (const auto& args : all_args) {
68 std::string message;
69 if (!handlers.get(device, args, &message)) {
70 continue;
71 }
72 std::string arg_string = android::base::Join(args, ":");
73 device->WriteInfo(android::base::StringPrintf("%s:%s:%s", name.c_str(), arg_string.c_str(),
74 message.c_str()));
75 }
76}
77
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070078bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) {
David Anderson0f626632018-08-31 16:44:25 -070079 const std::unordered_map<std::string, VariableHandlers> kVariableMap = {
80 {FB_VAR_VERSION, {GetVersion, nullptr}},
81 {FB_VAR_VERSION_BOOTLOADER, {GetBootloaderVersion, nullptr}},
82 {FB_VAR_VERSION_BASEBAND, {GetBasebandVersion, nullptr}},
83 {FB_VAR_PRODUCT, {GetProduct, nullptr}},
84 {FB_VAR_SERIALNO, {GetSerial, nullptr}},
85 {FB_VAR_SECURE, {GetSecure, nullptr}},
86 {FB_VAR_UNLOCKED, {GetUnlocked, nullptr}},
87 {FB_VAR_MAX_DOWNLOAD_SIZE, {GetMaxDownloadSize, nullptr}},
88 {FB_VAR_CURRENT_SLOT, {::GetCurrentSlot, nullptr}},
89 {FB_VAR_SLOT_COUNT, {GetSlotCount, nullptr}},
90 {FB_VAR_HAS_SLOT, {GetHasSlot, GetAllPartitionArgsNoSlot}},
91 {FB_VAR_SLOT_SUCCESSFUL, {GetSlotSuccessful, nullptr}},
92 {FB_VAR_SLOT_UNBOOTABLE, {GetSlotUnbootable, nullptr}},
93 {FB_VAR_PARTITION_SIZE, {GetPartitionSize, GetAllPartitionArgsWithSlot}},
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -070094 {FB_VAR_PARTITION_TYPE, {GetPartitionType, GetAllPartitionArgsWithSlot}},
David Anderson0f626632018-08-31 16:44:25 -070095 {FB_VAR_IS_LOGICAL, {GetPartitionIsLogical, GetAllPartitionArgsWithSlot}},
David Andersonc091c172018-09-04 18:11:03 -070096 {FB_VAR_IS_USERSPACE, {GetIsUserspace, nullptr}},
97 {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}}};
David Anderson0f626632018-08-31 16:44:25 -070098
99 if (args.size() < 2) {
100 return device->WriteFail("Missing argument");
101 }
102
103 // Special case: return all variables that we can.
104 if (args[1] == "all") {
105 for (const auto& [name, handlers] : kVariableMap) {
106 GetAllVars(device, name, handlers);
107 }
108 return device->WriteOkay("");
109 }
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700110
111 // args[0] is command name, args[1] is variable.
112 auto found_variable = kVariableMap.find(args[1]);
113 if (found_variable == kVariableMap.end()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700114 return device->WriteFail("Unknown variable");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700115 }
116
David Anderson1fb3fd72018-08-31 14:40:22 -0700117 std::string message;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700118 std::vector<std::string> getvar_args(args.begin() + 2, args.end());
David Anderson0f626632018-08-31 16:44:25 -0700119 if (!found_variable->second.get(device, getvar_args, &message)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700120 return device->WriteFail(message);
121 }
122 return device->WriteOkay(message);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700123}
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700124
David Anderson12211d12018-07-24 15:21:20 -0700125bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) {
126 if (args.size() < 2) {
127 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
128 }
129 PartitionHandle handle;
130 if (!OpenPartition(device, args[1], &handle)) {
131 return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist");
132 }
133 if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) {
134 return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded");
135 }
136 return device->WriteStatus(FastbootResult::FAIL, "Erasing failed");
137}
138
Hridya Valsarajua15fe312018-09-14 13:58:21 -0700139bool OemCmdHandler(FastbootDevice* device, const std::vector<std::string>& args) {
140 auto fastboot_hal = device->fastboot_hal();
141 if (!fastboot_hal) {
142 return device->WriteStatus(FastbootResult::FAIL, "Unable to open fastboot HAL");
143 }
144
145 Result ret;
146 auto ret_val = fastboot_hal->doOemCommand(args[0], [&](Result result) { ret = result; });
147 if (!ret_val.isOk()) {
148 return device->WriteStatus(FastbootResult::FAIL, "Unable to do OEM command");
149 }
150 if (ret.status != Status::SUCCESS) {
151 return device->WriteStatus(FastbootResult::FAIL, ret.message);
152 }
153
154 return device->WriteStatus(FastbootResult::OKAY, ret.message);
155}
156
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700157bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
158 if (args.size() < 2) {
159 return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
160 }
161 // arg[0] is the command name, arg[1] contains size of data to be downloaded
162 unsigned int size;
163 if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) {
164 return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
165 }
David Anderson12211d12018-07-24 15:21:20 -0700166 device->download_data().resize(size);
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700167 if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
168 return false;
169 }
170
David Anderson12211d12018-07-24 15:21:20 -0700171 if (device->HandleData(true, &device->download_data())) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700172 return device->WriteStatus(FastbootResult::OKAY, "");
173 }
174
175 PLOG(ERROR) << "Couldn't download data";
176 return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
177}
178
David Anderson12211d12018-07-24 15:21:20 -0700179bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) {
180 if (args.size() < 2) {
181 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
182 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700183
184 if (GetDeviceLockStatus()) {
185 return device->WriteStatus(FastbootResult::FAIL,
186 "Flashing is not allowed on locked devices");
187 }
188
David Anderson12211d12018-07-24 15:21:20 -0700189 int ret = Flash(device, args[1]);
190 if (ret < 0) {
191 return device->WriteStatus(FastbootResult::FAIL, strerror(-ret));
192 }
193 return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded");
194}
195
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700196bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
197 if (args.size() < 2) {
198 return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
199 }
200
201 // Slot suffix needs to be between 'a' and 'z'.
202 Slot slot;
203 if (!GetSlotNumber(args[1], &slot)) {
204 return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
205 }
206
207 // Non-A/B devices will not have a boot control HAL.
208 auto boot_control_hal = device->boot_control_hal();
209 if (!boot_control_hal) {
210 return device->WriteStatus(FastbootResult::FAIL,
211 "Cannot set slot: boot control HAL absent");
212 }
213 if (slot >= boot_control_hal->getNumberSlots()) {
214 return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
215 }
216 CommandResult ret;
217 auto cb = [&ret](CommandResult result) { ret = result; };
218 auto result = boot_control_hal->setActiveBootSlot(slot, cb);
219 if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, "");
220 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700221}
222
223bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
224 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
225 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
226 device->CloseDevice();
227 TEMP_FAILURE_RETRY(pause());
228 return result;
229}
230
231bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
232 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
233 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
234 device->CloseDevice();
235 TEMP_FAILURE_RETRY(pause());
236 return result;
237}
238
239bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
240 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
241 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
242 device->CloseDevice();
243 TEMP_FAILURE_RETRY(pause());
244 return result;
245}
246
247bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
248 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
249 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
250 device->CloseDevice();
251 TEMP_FAILURE_RETRY(pause());
252 return result;
253}
254
255static bool EnterRecovery() {
256 const char msg_switch_to_recovery = 'r';
257
258 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
259 if (sock < 0) {
260 PLOG(ERROR) << "Couldn't create sock";
261 return false;
262 }
263
264 struct sockaddr_un addr = {.sun_family = AF_UNIX};
265 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
266 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
267 PLOG(ERROR) << "Couldn't connect to recovery";
268 return false;
269 }
270 // Switch to recovery will not update the boot reason since it does not
271 // require a reboot.
272 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
273 if (ret != sizeof(msg_switch_to_recovery)) {
274 PLOG(ERROR) << "Couldn't write message to switch to recovery";
275 return false;
276 }
277
278 return true;
279}
280
281bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
282 auto status = true;
283 if (EnterRecovery()) {
284 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
285 } else {
286 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
287 }
288 device->CloseDevice();
289 TEMP_FAILURE_RETRY(pause());
290 return status;
291}
David Anderson0d4277d2018-07-31 13:27:37 -0700292
293// Helper class for opening a handle to a MetadataBuilder and writing the new
294// partition table to the same place it was read.
295class PartitionBuilder {
296 public:
297 explicit PartitionBuilder(FastbootDevice* device);
298
299 bool Write();
300 bool Valid() const { return !!builder_; }
301 MetadataBuilder* operator->() const { return builder_.get(); }
302
303 private:
304 std::string super_device_;
305 uint32_t slot_number_;
306 std::unique_ptr<MetadataBuilder> builder_;
307};
308
309PartitionBuilder::PartitionBuilder(FastbootDevice* device) {
310 auto super_device = FindPhysicalPartition(LP_METADATA_PARTITION_NAME);
311 if (!super_device) {
312 return;
313 }
314 super_device_ = *super_device;
315
316 std::string slot = device->GetCurrentSlot();
317 slot_number_ = SlotNumberForSlotSuffix(slot);
318 builder_ = MetadataBuilder::New(super_device_, slot_number_);
319}
320
321bool PartitionBuilder::Write() {
322 std::unique_ptr<LpMetadata> metadata = builder_->Export();
323 if (!metadata) {
324 return false;
325 }
326 return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_);
327}
328
329bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
330 if (args.size() < 3) {
331 return device->WriteFail("Invalid partition name and size");
332 }
333
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700334 if (GetDeviceLockStatus()) {
335 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
336 }
337
David Anderson0d4277d2018-07-31 13:27:37 -0700338 uint64_t partition_size;
339 std::string partition_name = args[1];
340 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
341 return device->WriteFail("Invalid partition size");
342 }
343
344 PartitionBuilder builder(device);
345 if (!builder.Valid()) {
346 return device->WriteFail("Could not open super partition");
347 }
348 // TODO(112433293) Disallow if the name is in the physical table as well.
349 if (builder->FindPartition(partition_name)) {
350 return device->WriteFail("Partition already exists");
351 }
352
353 // Make a random UUID, since they're not currently used.
354 uuid_t uuid;
355 char uuid_str[37];
356 uuid_generate_random(uuid);
357 uuid_unparse(uuid, uuid_str);
358
359 Partition* partition = builder->AddPartition(partition_name, uuid_str, 0);
360 if (!partition) {
361 return device->WriteFail("Failed to add partition");
362 }
363 if (!builder->ResizePartition(partition, partition_size)) {
364 builder->RemovePartition(partition_name);
365 return device->WriteFail("Not enough space for partition");
366 }
367 if (!builder.Write()) {
368 return device->WriteFail("Failed to write partition table");
369 }
370 return device->WriteOkay("Partition created");
371}
372
373bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
374 if (args.size() < 2) {
375 return device->WriteFail("Invalid partition name and size");
376 }
377
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700378 if (GetDeviceLockStatus()) {
379 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
380 }
381
David Anderson0d4277d2018-07-31 13:27:37 -0700382 PartitionBuilder builder(device);
383 if (!builder.Valid()) {
384 return device->WriteFail("Could not open super partition");
385 }
386 builder->RemovePartition(args[1]);
387 if (!builder.Write()) {
388 return device->WriteFail("Failed to write partition table");
389 }
390 return device->WriteOkay("Partition deleted");
391}
392
393bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
394 if (args.size() < 3) {
395 return device->WriteFail("Invalid partition name and size");
396 }
397
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700398 if (GetDeviceLockStatus()) {
399 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
400 }
401
David Anderson0d4277d2018-07-31 13:27:37 -0700402 uint64_t partition_size;
403 std::string partition_name = args[1];
404 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
405 return device->WriteFail("Invalid partition size");
406 }
407
408 PartitionBuilder builder(device);
409 if (!builder.Valid()) {
410 return device->WriteFail("Could not open super partition");
411 }
412
413 Partition* partition = builder->FindPartition(partition_name);
414 if (!partition) {
415 return device->WriteFail("Partition does not exist");
416 }
417 if (!builder->ResizePartition(partition, partition_size)) {
418 return device->WriteFail("Not enough space to resize partition");
419 }
420 if (!builder.Write()) {
421 return device->WriteFail("Failed to write partition table");
422 }
423 return device->WriteOkay("Partition resized");
424}
David Anderson38b3c7a2018-08-15 16:27:42 -0700425
426bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) {
427 if (args.size() < 2) {
428 return device->WriteFail("Invalid arguments");
429 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700430
431 if (GetDeviceLockStatus()) {
432 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
433 }
434
David Anderson38b3c7a2018-08-15 16:27:42 -0700435 bool wipe = (args.size() >= 3 && args[2] == "wipe");
436 return UpdateSuper(device, args[1], wipe);
437}