blob: e70de1b6d05401cecfd82176e569e4bc878afa88 [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}},
Hridya Valsaraju4af80902018-09-26 13:08:16 -070085 {FB_VAR_VARIANT, {GetVariant, nullptr}},
David Anderson0f626632018-08-31 16:44:25 -070086 {FB_VAR_SECURE, {GetSecure, nullptr}},
87 {FB_VAR_UNLOCKED, {GetUnlocked, nullptr}},
88 {FB_VAR_MAX_DOWNLOAD_SIZE, {GetMaxDownloadSize, nullptr}},
89 {FB_VAR_CURRENT_SLOT, {::GetCurrentSlot, nullptr}},
90 {FB_VAR_SLOT_COUNT, {GetSlotCount, nullptr}},
91 {FB_VAR_HAS_SLOT, {GetHasSlot, GetAllPartitionArgsNoSlot}},
92 {FB_VAR_SLOT_SUCCESSFUL, {GetSlotSuccessful, nullptr}},
93 {FB_VAR_SLOT_UNBOOTABLE, {GetSlotUnbootable, nullptr}},
94 {FB_VAR_PARTITION_SIZE, {GetPartitionSize, GetAllPartitionArgsWithSlot}},
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -070095 {FB_VAR_PARTITION_TYPE, {GetPartitionType, GetAllPartitionArgsWithSlot}},
David Anderson0f626632018-08-31 16:44:25 -070096 {FB_VAR_IS_LOGICAL, {GetPartitionIsLogical, GetAllPartitionArgsWithSlot}},
David Andersonc091c172018-09-04 18:11:03 -070097 {FB_VAR_IS_USERSPACE, {GetIsUserspace, nullptr}},
98 {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}}};
David Anderson0f626632018-08-31 16:44:25 -070099
100 if (args.size() < 2) {
101 return device->WriteFail("Missing argument");
102 }
103
104 // Special case: return all variables that we can.
105 if (args[1] == "all") {
106 for (const auto& [name, handlers] : kVariableMap) {
107 GetAllVars(device, name, handlers);
108 }
109 return device->WriteOkay("");
110 }
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700111
112 // args[0] is command name, args[1] is variable.
113 auto found_variable = kVariableMap.find(args[1]);
114 if (found_variable == kVariableMap.end()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700115 return device->WriteFail("Unknown variable");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700116 }
117
David Anderson1fb3fd72018-08-31 14:40:22 -0700118 std::string message;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700119 std::vector<std::string> getvar_args(args.begin() + 2, args.end());
David Anderson0f626632018-08-31 16:44:25 -0700120 if (!found_variable->second.get(device, getvar_args, &message)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700121 return device->WriteFail(message);
122 }
123 return device->WriteOkay(message);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700124}
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700125
David Anderson12211d12018-07-24 15:21:20 -0700126bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) {
127 if (args.size() < 2) {
128 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
129 }
130 PartitionHandle handle;
131 if (!OpenPartition(device, args[1], &handle)) {
132 return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist");
133 }
134 if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) {
135 return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded");
136 }
137 return device->WriteStatus(FastbootResult::FAIL, "Erasing failed");
138}
139
Hridya Valsarajua15fe312018-09-14 13:58:21 -0700140bool OemCmdHandler(FastbootDevice* device, const std::vector<std::string>& args) {
141 auto fastboot_hal = device->fastboot_hal();
142 if (!fastboot_hal) {
143 return device->WriteStatus(FastbootResult::FAIL, "Unable to open fastboot HAL");
144 }
145
146 Result ret;
147 auto ret_val = fastboot_hal->doOemCommand(args[0], [&](Result result) { ret = result; });
148 if (!ret_val.isOk()) {
149 return device->WriteStatus(FastbootResult::FAIL, "Unable to do OEM command");
150 }
151 if (ret.status != Status::SUCCESS) {
152 return device->WriteStatus(FastbootResult::FAIL, ret.message);
153 }
154
155 return device->WriteStatus(FastbootResult::OKAY, ret.message);
156}
157
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700158bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
159 if (args.size() < 2) {
160 return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
161 }
162 // arg[0] is the command name, arg[1] contains size of data to be downloaded
163 unsigned int size;
164 if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) {
165 return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
166 }
David Anderson12211d12018-07-24 15:21:20 -0700167 device->download_data().resize(size);
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700168 if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
169 return false;
170 }
171
David Anderson12211d12018-07-24 15:21:20 -0700172 if (device->HandleData(true, &device->download_data())) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700173 return device->WriteStatus(FastbootResult::OKAY, "");
174 }
175
176 PLOG(ERROR) << "Couldn't download data";
177 return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
178}
179
David Anderson12211d12018-07-24 15:21:20 -0700180bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) {
181 if (args.size() < 2) {
182 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
183 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700184
185 if (GetDeviceLockStatus()) {
186 return device->WriteStatus(FastbootResult::FAIL,
187 "Flashing is not allowed on locked devices");
188 }
189
David Anderson12211d12018-07-24 15:21:20 -0700190 int ret = Flash(device, args[1]);
191 if (ret < 0) {
192 return device->WriteStatus(FastbootResult::FAIL, strerror(-ret));
193 }
194 return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded");
195}
196
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700197bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
198 if (args.size() < 2) {
199 return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
200 }
201
202 // Slot suffix needs to be between 'a' and 'z'.
203 Slot slot;
204 if (!GetSlotNumber(args[1], &slot)) {
205 return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
206 }
207
208 // Non-A/B devices will not have a boot control HAL.
209 auto boot_control_hal = device->boot_control_hal();
210 if (!boot_control_hal) {
211 return device->WriteStatus(FastbootResult::FAIL,
212 "Cannot set slot: boot control HAL absent");
213 }
214 if (slot >= boot_control_hal->getNumberSlots()) {
215 return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
216 }
217 CommandResult ret;
218 auto cb = [&ret](CommandResult result) { ret = result; };
219 auto result = boot_control_hal->setActiveBootSlot(slot, cb);
220 if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, "");
221 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700222}
223
224bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
225 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
226 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
227 device->CloseDevice();
228 TEMP_FAILURE_RETRY(pause());
229 return result;
230}
231
232bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
233 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
234 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
235 device->CloseDevice();
236 TEMP_FAILURE_RETRY(pause());
237 return result;
238}
239
240bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
241 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
242 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
243 device->CloseDevice();
244 TEMP_FAILURE_RETRY(pause());
245 return result;
246}
247
248bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
249 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
250 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
251 device->CloseDevice();
252 TEMP_FAILURE_RETRY(pause());
253 return result;
254}
255
256static bool EnterRecovery() {
257 const char msg_switch_to_recovery = 'r';
258
259 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
260 if (sock < 0) {
261 PLOG(ERROR) << "Couldn't create sock";
262 return false;
263 }
264
265 struct sockaddr_un addr = {.sun_family = AF_UNIX};
266 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
267 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
268 PLOG(ERROR) << "Couldn't connect to recovery";
269 return false;
270 }
271 // Switch to recovery will not update the boot reason since it does not
272 // require a reboot.
273 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
274 if (ret != sizeof(msg_switch_to_recovery)) {
275 PLOG(ERROR) << "Couldn't write message to switch to recovery";
276 return false;
277 }
278
279 return true;
280}
281
282bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
283 auto status = true;
284 if (EnterRecovery()) {
285 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
286 } else {
287 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
288 }
289 device->CloseDevice();
290 TEMP_FAILURE_RETRY(pause());
291 return status;
292}
David Anderson0d4277d2018-07-31 13:27:37 -0700293
294// Helper class for opening a handle to a MetadataBuilder and writing the new
295// partition table to the same place it was read.
296class PartitionBuilder {
297 public:
298 explicit PartitionBuilder(FastbootDevice* device);
299
300 bool Write();
301 bool Valid() const { return !!builder_; }
302 MetadataBuilder* operator->() const { return builder_.get(); }
303
304 private:
305 std::string super_device_;
306 uint32_t slot_number_;
307 std::unique_ptr<MetadataBuilder> builder_;
308};
309
310PartitionBuilder::PartitionBuilder(FastbootDevice* device) {
311 auto super_device = FindPhysicalPartition(LP_METADATA_PARTITION_NAME);
312 if (!super_device) {
313 return;
314 }
315 super_device_ = *super_device;
316
317 std::string slot = device->GetCurrentSlot();
318 slot_number_ = SlotNumberForSlotSuffix(slot);
319 builder_ = MetadataBuilder::New(super_device_, slot_number_);
320}
321
322bool PartitionBuilder::Write() {
323 std::unique_ptr<LpMetadata> metadata = builder_->Export();
324 if (!metadata) {
325 return false;
326 }
327 return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_);
328}
329
330bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
331 if (args.size() < 3) {
332 return device->WriteFail("Invalid partition name and size");
333 }
334
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700335 if (GetDeviceLockStatus()) {
336 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
337 }
338
David Anderson0d4277d2018-07-31 13:27:37 -0700339 uint64_t partition_size;
340 std::string partition_name = args[1];
341 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
342 return device->WriteFail("Invalid partition size");
343 }
344
345 PartitionBuilder builder(device);
346 if (!builder.Valid()) {
347 return device->WriteFail("Could not open super partition");
348 }
349 // TODO(112433293) Disallow if the name is in the physical table as well.
350 if (builder->FindPartition(partition_name)) {
351 return device->WriteFail("Partition already exists");
352 }
353
354 // Make a random UUID, since they're not currently used.
355 uuid_t uuid;
356 char uuid_str[37];
357 uuid_generate_random(uuid);
358 uuid_unparse(uuid, uuid_str);
359
360 Partition* partition = builder->AddPartition(partition_name, uuid_str, 0);
361 if (!partition) {
362 return device->WriteFail("Failed to add partition");
363 }
364 if (!builder->ResizePartition(partition, partition_size)) {
365 builder->RemovePartition(partition_name);
366 return device->WriteFail("Not enough space for partition");
367 }
368 if (!builder.Write()) {
369 return device->WriteFail("Failed to write partition table");
370 }
371 return device->WriteOkay("Partition created");
372}
373
374bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
375 if (args.size() < 2) {
376 return device->WriteFail("Invalid partition name and size");
377 }
378
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700379 if (GetDeviceLockStatus()) {
380 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
381 }
382
David Anderson0d4277d2018-07-31 13:27:37 -0700383 PartitionBuilder builder(device);
384 if (!builder.Valid()) {
385 return device->WriteFail("Could not open super partition");
386 }
387 builder->RemovePartition(args[1]);
388 if (!builder.Write()) {
389 return device->WriteFail("Failed to write partition table");
390 }
391 return device->WriteOkay("Partition deleted");
392}
393
394bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
395 if (args.size() < 3) {
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 uint64_t partition_size;
404 std::string partition_name = args[1];
405 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
406 return device->WriteFail("Invalid partition size");
407 }
408
409 PartitionBuilder builder(device);
410 if (!builder.Valid()) {
411 return device->WriteFail("Could not open super partition");
412 }
413
414 Partition* partition = builder->FindPartition(partition_name);
415 if (!partition) {
416 return device->WriteFail("Partition does not exist");
417 }
418 if (!builder->ResizePartition(partition, partition_size)) {
419 return device->WriteFail("Not enough space to resize partition");
420 }
421 if (!builder.Write()) {
422 return device->WriteFail("Failed to write partition table");
423 }
424 return device->WriteOkay("Partition resized");
425}
David Anderson38b3c7a2018-08-15 16:27:42 -0700426
427bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) {
428 if (args.size() < 2) {
429 return device->WriteFail("Invalid arguments");
430 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700431
432 if (GetDeviceLockStatus()) {
433 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
434 }
435
David Anderson38b3c7a2018-08-15 16:27:42 -0700436 bool wipe = (args.size() >= 3 && args[2] == "wipe");
437 return UpdateSuper(device, args[1], wipe);
438}