blob: b7263d94590c9804afbc788b556f1aba7193a38b [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
David Anderson220ddb12019-10-31 18:02:41 -070022#include <unordered_set>
23
Hridya Valsarajudea91b42018-07-17 11:14:01 -070024#include <android-base/logging.h>
25#include <android-base/parseint.h>
26#include <android-base/properties.h>
27#include <android-base/stringprintf.h>
28#include <android-base/strings.h>
29#include <android-base/unique_fd.h>
David Andersonab8f4662019-10-21 16:45:59 -070030#include <android/hardware/boot/1.1/IBootControl.h>
Hridya Valsarajudea91b42018-07-17 11:14:01 -070031#include <cutils/android_reboot.h>
David Anderson12211d12018-07-24 15:21:20 -070032#include <ext4_utils/wipe.h>
David Anderson5cbd2e42018-09-27 10:53:04 -070033#include <fs_mgr.h>
David Anderson1d504e32019-01-15 14:38:20 -080034#include <libgsi/libgsi.h>
David Anderson0d4277d2018-07-31 13:27:37 -070035#include <liblp/builder.h>
36#include <liblp/liblp.h>
David Anderson220ddb12019-10-31 18:02:41 -070037#include <libsnapshot/snapshot.h>
David Anderson0d4277d2018-07-31 13:27:37 -070038#include <uuid/uuid.h>
Hridya Valsarajudea91b42018-07-17 11:14:01 -070039
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070040#include "constants.h"
Hridya Valsarajudea91b42018-07-17 11:14:01 -070041#include "fastboot_device.h"
David Anderson12211d12018-07-24 15:21:20 -070042#include "flashing.h"
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070043#include "utility.h"
44
David Anderson27475322019-06-11 14:00:08 -070045using android::fs_mgr::MetadataBuilder;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070046using ::android::hardware::hidl_string;
47using ::android::hardware::boot::V1_0::BoolResult;
48using ::android::hardware::boot::V1_0::CommandResult;
49using ::android::hardware::boot::V1_0::Slot;
David Andersonab8f4662019-10-21 16:45:59 -070050using ::android::hardware::boot::V1_1::MergeStatus;
Hridya Valsarajua15fe312018-09-14 13:58:21 -070051using ::android::hardware::fastboot::V1_0::Result;
52using ::android::hardware::fastboot::V1_0::Status;
David Anderson220ddb12019-10-31 18:02:41 -070053using android::snapshot::SnapshotManager;
David Andersonab8f4662019-10-21 16:45:59 -070054using IBootControl1_1 = ::android::hardware::boot::V1_1::IBootControl;
Hridya Valsarajua15fe312018-09-14 13:58:21 -070055
David Anderson0f626632018-08-31 16:44:25 -070056struct VariableHandlers {
57 // Callback to retrieve the value of a single variable.
58 std::function<bool(FastbootDevice*, const std::vector<std::string>&, std::string*)> get;
59 // Callback to retrieve all possible argument combinations, for getvar all.
60 std::function<std::vector<std::vector<std::string>>(FastbootDevice*)> get_all_args;
61};
62
David Anderson220ddb12019-10-31 18:02:41 -070063static bool IsSnapshotUpdateInProgress(FastbootDevice* device) {
64 auto hal = device->boot1_1();
65 if (!hal) {
66 return false;
67 }
68 auto merge_status = hal->getSnapshotMergeStatus();
69 return merge_status == MergeStatus::SNAPSHOTTED || merge_status == MergeStatus::MERGING;
70}
71
72static bool IsProtectedPartitionDuringMerge(FastbootDevice* device, const std::string& name) {
73 static const std::unordered_set<std::string> ProtectedPartitionsDuringMerge = {
74 "userdata", "metadata", "misc"};
75 if (ProtectedPartitionsDuringMerge.count(name) == 0) {
76 return false;
77 }
78 return IsSnapshotUpdateInProgress(device);
79}
80
David Anderson0f626632018-08-31 16:44:25 -070081static void GetAllVars(FastbootDevice* device, const std::string& name,
82 const VariableHandlers& handlers) {
83 if (!handlers.get_all_args) {
84 std::string message;
85 if (!handlers.get(device, std::vector<std::string>(), &message)) {
86 return;
87 }
88 device->WriteInfo(android::base::StringPrintf("%s:%s", name.c_str(), message.c_str()));
89 return;
90 }
91
92 auto all_args = handlers.get_all_args(device);
93 for (const auto& args : all_args) {
94 std::string message;
95 if (!handlers.get(device, args, &message)) {
96 continue;
97 }
98 std::string arg_string = android::base::Join(args, ":");
99 device->WriteInfo(android::base::StringPrintf("%s:%s:%s", name.c_str(), arg_string.c_str(),
100 message.c_str()));
101 }
102}
103
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700104bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) {
David Anderson0f626632018-08-31 16:44:25 -0700105 const std::unordered_map<std::string, VariableHandlers> kVariableMap = {
106 {FB_VAR_VERSION, {GetVersion, nullptr}},
107 {FB_VAR_VERSION_BOOTLOADER, {GetBootloaderVersion, nullptr}},
108 {FB_VAR_VERSION_BASEBAND, {GetBasebandVersion, nullptr}},
109 {FB_VAR_PRODUCT, {GetProduct, nullptr}},
110 {FB_VAR_SERIALNO, {GetSerial, nullptr}},
Hridya Valsaraju4af80902018-09-26 13:08:16 -0700111 {FB_VAR_VARIANT, {GetVariant, nullptr}},
David Anderson0f626632018-08-31 16:44:25 -0700112 {FB_VAR_SECURE, {GetSecure, nullptr}},
113 {FB_VAR_UNLOCKED, {GetUnlocked, nullptr}},
114 {FB_VAR_MAX_DOWNLOAD_SIZE, {GetMaxDownloadSize, nullptr}},
115 {FB_VAR_CURRENT_SLOT, {::GetCurrentSlot, nullptr}},
116 {FB_VAR_SLOT_COUNT, {GetSlotCount, nullptr}},
117 {FB_VAR_HAS_SLOT, {GetHasSlot, GetAllPartitionArgsNoSlot}},
118 {FB_VAR_SLOT_SUCCESSFUL, {GetSlotSuccessful, nullptr}},
119 {FB_VAR_SLOT_UNBOOTABLE, {GetSlotUnbootable, nullptr}},
David Anderson27475322019-06-11 14:00:08 -0700120 {FB_VAR_PARTITION_SIZE, {GetPartitionSize, GetAllPartitionArgsWithSlot}},
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -0700121 {FB_VAR_PARTITION_TYPE, {GetPartitionType, GetAllPartitionArgsWithSlot}},
David Anderson0f626632018-08-31 16:44:25 -0700122 {FB_VAR_IS_LOGICAL, {GetPartitionIsLogical, GetAllPartitionArgsWithSlot}},
David Andersonc091c172018-09-04 18:11:03 -0700123 {FB_VAR_IS_USERSPACE, {GetIsUserspace, nullptr}},
Hridya Valsaraju7c9bbe92018-09-27 10:41:01 -0700124 {FB_VAR_OFF_MODE_CHARGE_STATE, {GetOffModeChargeState, nullptr}},
Hridya Valsaraju47658ca2018-09-28 11:41:22 -0700125 {FB_VAR_BATTERY_VOLTAGE, {GetBatteryVoltage, nullptr}},
Hridya Valsarajua534a5a2018-10-03 15:53:22 -0700126 {FB_VAR_BATTERY_SOC_OK, {GetBatterySoCOk, nullptr}},
David Anderson90fe0a42018-11-05 18:01:32 -0800127 {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}},
David Andersonab8f4662019-10-21 16:45:59 -0700128 {FB_VAR_SUPER_PARTITION_NAME, {GetSuperPartitionName, nullptr}},
Bowgo Tsai33da5c92019-11-13 17:13:49 +0800129 {FB_VAR_SNAPSHOT_UPDATE_STATUS, {GetSnapshotUpdateStatus, nullptr}},
130 {FB_VAR_CPU_ABI, {GetCpuAbi, nullptr}}};
David Anderson0f626632018-08-31 16:44:25 -0700131
132 if (args.size() < 2) {
133 return device->WriteFail("Missing argument");
134 }
135
136 // Special case: return all variables that we can.
137 if (args[1] == "all") {
138 for (const auto& [name, handlers] : kVariableMap) {
139 GetAllVars(device, name, handlers);
140 }
141 return device->WriteOkay("");
142 }
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700143
144 // args[0] is command name, args[1] is variable.
145 auto found_variable = kVariableMap.find(args[1]);
146 if (found_variable == kVariableMap.end()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700147 return device->WriteFail("Unknown variable");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700148 }
149
David Anderson1fb3fd72018-08-31 14:40:22 -0700150 std::string message;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700151 std::vector<std::string> getvar_args(args.begin() + 2, args.end());
David Anderson0f626632018-08-31 16:44:25 -0700152 if (!found_variable->second.get(device, getvar_args, &message)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700153 return device->WriteFail(message);
154 }
155 return device->WriteOkay(message);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700156}
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700157
David Anderson12211d12018-07-24 15:21:20 -0700158bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) {
159 if (args.size() < 2) {
160 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
161 }
Hridya Valsarajud1e62312018-10-08 09:13:17 -0700162
163 if (GetDeviceLockStatus()) {
164 return device->WriteStatus(FastbootResult::FAIL, "Erase is not allowed on locked devices");
165 }
166
David Anderson220ddb12019-10-31 18:02:41 -0700167 const auto& partition_name = args[1];
168 if (IsProtectedPartitionDuringMerge(device, partition_name)) {
169 auto message = "Cannot erase " + partition_name + " while a snapshot update is in progress";
170 return device->WriteFail(message);
171 }
172
David Anderson12211d12018-07-24 15:21:20 -0700173 PartitionHandle handle;
David Anderson220ddb12019-10-31 18:02:41 -0700174 if (!OpenPartition(device, partition_name, &handle)) {
David Anderson12211d12018-07-24 15:21:20 -0700175 return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist");
176 }
177 if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) {
178 return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded");
179 }
180 return device->WriteStatus(FastbootResult::FAIL, "Erasing failed");
181}
182
Hridya Valsarajua15fe312018-09-14 13:58:21 -0700183bool OemCmdHandler(FastbootDevice* device, const std::vector<std::string>& args) {
184 auto fastboot_hal = device->fastboot_hal();
185 if (!fastboot_hal) {
186 return device->WriteStatus(FastbootResult::FAIL, "Unable to open fastboot HAL");
187 }
188
189 Result ret;
190 auto ret_val = fastboot_hal->doOemCommand(args[0], [&](Result result) { ret = result; });
191 if (!ret_val.isOk()) {
192 return device->WriteStatus(FastbootResult::FAIL, "Unable to do OEM command");
193 }
194 if (ret.status != Status::SUCCESS) {
195 return device->WriteStatus(FastbootResult::FAIL, ret.message);
196 }
197
198 return device->WriteStatus(FastbootResult::OKAY, ret.message);
199}
200
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700201bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
202 if (args.size() < 2) {
203 return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
204 }
Hridya Valsarajud1e62312018-10-08 09:13:17 -0700205
206 if (GetDeviceLockStatus()) {
207 return device->WriteStatus(FastbootResult::FAIL,
208 "Download is not allowed on locked devices");
209 }
210
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700211 // arg[0] is the command name, arg[1] contains size of data to be downloaded
212 unsigned int size;
Hridya Valsarajuaae84e82018-10-08 13:10:25 -0700213 if (!android::base::ParseUint("0x" + args[1], &size, kMaxDownloadSizeDefault)) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700214 return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
215 }
David Anderson12211d12018-07-24 15:21:20 -0700216 device->download_data().resize(size);
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700217 if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
218 return false;
219 }
220
David Anderson12211d12018-07-24 15:21:20 -0700221 if (device->HandleData(true, &device->download_data())) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700222 return device->WriteStatus(FastbootResult::OKAY, "");
223 }
224
225 PLOG(ERROR) << "Couldn't download data";
226 return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
227}
228
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700229bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
230 if (args.size() < 2) {
231 return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
232 }
233
Hridya Valsarajud1e62312018-10-08 09:13:17 -0700234 if (GetDeviceLockStatus()) {
235 return device->WriteStatus(FastbootResult::FAIL,
236 "set_active command is not allowed on locked devices");
237 }
238
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700239 Slot slot;
240 if (!GetSlotNumber(args[1], &slot)) {
David Anderson220ddb12019-10-31 18:02:41 -0700241 // Slot suffix needs to be between 'a' and 'z'.
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700242 return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
243 }
244
245 // Non-A/B devices will not have a boot control HAL.
246 auto boot_control_hal = device->boot_control_hal();
247 if (!boot_control_hal) {
248 return device->WriteStatus(FastbootResult::FAIL,
249 "Cannot set slot: boot control HAL absent");
250 }
251 if (slot >= boot_control_hal->getNumberSlots()) {
252 return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
253 }
David Anderson220ddb12019-10-31 18:02:41 -0700254
255 // If the slot is not changing, do nothing.
256 if (slot == boot_control_hal->getCurrentSlot()) {
257 return device->WriteOkay("");
258 }
259
260 // Check how to handle the current snapshot state.
261 if (auto hal11 = device->boot1_1()) {
262 auto merge_status = hal11->getSnapshotMergeStatus();
263 if (merge_status == MergeStatus::MERGING) {
264 return device->WriteFail("Cannot change slots while a snapshot update is in progress");
265 }
266 // Note: we allow the slot change if the state is SNAPSHOTTED. First-
267 // stage init does not have access to the HAL, and uses the slot number
268 // and /metadata OTA state to determine whether a slot change occurred.
269 // Booting into the old slot would erase the OTA, and switching A->B->A
270 // would simply resume it if no boots occur in between. Re-flashing
271 // partitions implicitly cancels the OTA, so leaving the state as-is is
272 // safe.
273 if (merge_status == MergeStatus::SNAPSHOTTED) {
274 device->WriteInfo(
275 "Changing the active slot with a snapshot applied may cancel the"
276 " update.");
277 }
278 }
279
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700280 CommandResult ret;
281 auto cb = [&ret](CommandResult result) { ret = result; };
282 auto result = boot_control_hal->setActiveBootSlot(slot, cb);
Hridya Valsaraju20bdf892018-10-10 11:02:19 -0700283 if (result.isOk() && ret.success) {
284 // Save as slot suffix to match the suffix format as returned from
285 // the boot control HAL.
286 auto current_slot = "_" + args[1];
287 device->set_active_slot(current_slot);
288 return device->WriteStatus(FastbootResult::OKAY, "");
289 }
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700290 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700291}
292
293bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
294 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
295 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
296 device->CloseDevice();
297 TEMP_FAILURE_RETRY(pause());
298 return result;
299}
300
301bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
302 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
303 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
304 device->CloseDevice();
305 TEMP_FAILURE_RETRY(pause());
306 return result;
307}
308
309bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
310 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
311 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
312 device->CloseDevice();
313 TEMP_FAILURE_RETRY(pause());
314 return result;
315}
316
317bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
318 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
319 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
320 device->CloseDevice();
321 TEMP_FAILURE_RETRY(pause());
322 return result;
323}
324
325static bool EnterRecovery() {
326 const char msg_switch_to_recovery = 'r';
327
328 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
329 if (sock < 0) {
330 PLOG(ERROR) << "Couldn't create sock";
331 return false;
332 }
333
334 struct sockaddr_un addr = {.sun_family = AF_UNIX};
335 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
336 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
337 PLOG(ERROR) << "Couldn't connect to recovery";
338 return false;
339 }
340 // Switch to recovery will not update the boot reason since it does not
341 // require a reboot.
342 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
343 if (ret != sizeof(msg_switch_to_recovery)) {
344 PLOG(ERROR) << "Couldn't write message to switch to recovery";
345 return false;
346 }
347
348 return true;
349}
350
351bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
352 auto status = true;
353 if (EnterRecovery()) {
354 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
355 } else {
356 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
357 }
358 device->CloseDevice();
359 TEMP_FAILURE_RETRY(pause());
360 return status;
361}
David Anderson0d4277d2018-07-31 13:27:37 -0700362
363// Helper class for opening a handle to a MetadataBuilder and writing the new
364// partition table to the same place it was read.
365class PartitionBuilder {
366 public:
David Andersond25f1c32018-11-09 20:41:33 -0800367 explicit PartitionBuilder(FastbootDevice* device, const std::string& partition_name);
David Anderson0d4277d2018-07-31 13:27:37 -0700368
369 bool Write();
370 bool Valid() const { return !!builder_; }
371 MetadataBuilder* operator->() const { return builder_.get(); }
372
373 private:
David Anderson4d307b02018-12-17 17:07:34 -0800374 FastbootDevice* device_;
David Anderson0d4277d2018-07-31 13:27:37 -0700375 std::string super_device_;
David Andersond25f1c32018-11-09 20:41:33 -0800376 uint32_t slot_number_;
David Anderson0d4277d2018-07-31 13:27:37 -0700377 std::unique_ptr<MetadataBuilder> builder_;
378};
379
David Anderson4d307b02018-12-17 17:07:34 -0800380PartitionBuilder::PartitionBuilder(FastbootDevice* device, const std::string& partition_name)
381 : device_(device) {
David Andersond25f1c32018-11-09 20:41:33 -0800382 std::string slot_suffix = GetSuperSlotSuffix(device, partition_name);
David Anderson27475322019-06-11 14:00:08 -0700383 slot_number_ = android::fs_mgr::SlotNumberForSlotSuffix(slot_suffix);
David Andersond25f1c32018-11-09 20:41:33 -0800384 auto super_device = FindPhysicalPartition(fs_mgr_get_super_partition_name(slot_number_));
David Anderson0d4277d2018-07-31 13:27:37 -0700385 if (!super_device) {
386 return;
387 }
388 super_device_ = *super_device;
David Andersond25f1c32018-11-09 20:41:33 -0800389 builder_ = MetadataBuilder::New(super_device_, slot_number_);
David Anderson0d4277d2018-07-31 13:27:37 -0700390}
391
392bool PartitionBuilder::Write() {
David Anderson27475322019-06-11 14:00:08 -0700393 auto metadata = builder_->Export();
David Anderson0d4277d2018-07-31 13:27:37 -0700394 if (!metadata) {
395 return false;
396 }
David Anderson4d307b02018-12-17 17:07:34 -0800397 return UpdateAllPartitionMetadata(device_, super_device_, *metadata.get());
David Anderson0d4277d2018-07-31 13:27:37 -0700398}
399
400bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
401 if (args.size() < 3) {
402 return device->WriteFail("Invalid partition name and size");
403 }
404
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700405 if (GetDeviceLockStatus()) {
406 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
407 }
408
David Anderson0d4277d2018-07-31 13:27:37 -0700409 uint64_t partition_size;
410 std::string partition_name = args[1];
411 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
412 return device->WriteFail("Invalid partition size");
413 }
414
David Andersond25f1c32018-11-09 20:41:33 -0800415 PartitionBuilder builder(device, partition_name);
David Anderson0d4277d2018-07-31 13:27:37 -0700416 if (!builder.Valid()) {
417 return device->WriteFail("Could not open super partition");
418 }
419 // TODO(112433293) Disallow if the name is in the physical table as well.
420 if (builder->FindPartition(partition_name)) {
421 return device->WriteFail("Partition already exists");
422 }
423
David Anderson27475322019-06-11 14:00:08 -0700424 auto partition = builder->AddPartition(partition_name, 0);
David Anderson0d4277d2018-07-31 13:27:37 -0700425 if (!partition) {
426 return device->WriteFail("Failed to add partition");
427 }
428 if (!builder->ResizePartition(partition, partition_size)) {
429 builder->RemovePartition(partition_name);
430 return device->WriteFail("Not enough space for partition");
431 }
432 if (!builder.Write()) {
433 return device->WriteFail("Failed to write partition table");
434 }
435 return device->WriteOkay("Partition created");
436}
437
438bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
439 if (args.size() < 2) {
440 return device->WriteFail("Invalid partition name and size");
441 }
442
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700443 if (GetDeviceLockStatus()) {
444 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
445 }
446
David Andersond25f1c32018-11-09 20:41:33 -0800447 std::string partition_name = args[1];
448
449 PartitionBuilder builder(device, partition_name);
David Anderson0d4277d2018-07-31 13:27:37 -0700450 if (!builder.Valid()) {
451 return device->WriteFail("Could not open super partition");
452 }
David Andersond25f1c32018-11-09 20:41:33 -0800453 builder->RemovePartition(partition_name);
David Anderson0d4277d2018-07-31 13:27:37 -0700454 if (!builder.Write()) {
455 return device->WriteFail("Failed to write partition table");
456 }
457 return device->WriteOkay("Partition deleted");
458}
459
460bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
461 if (args.size() < 3) {
462 return device->WriteFail("Invalid partition name and size");
463 }
464
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700465 if (GetDeviceLockStatus()) {
466 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
467 }
468
David Anderson0d4277d2018-07-31 13:27:37 -0700469 uint64_t partition_size;
470 std::string partition_name = args[1];
471 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
472 return device->WriteFail("Invalid partition size");
473 }
474
David Andersond25f1c32018-11-09 20:41:33 -0800475 PartitionBuilder builder(device, partition_name);
David Anderson0d4277d2018-07-31 13:27:37 -0700476 if (!builder.Valid()) {
477 return device->WriteFail("Could not open super partition");
478 }
479
David Anderson27475322019-06-11 14:00:08 -0700480 auto partition = builder->FindPartition(partition_name);
David Anderson0d4277d2018-07-31 13:27:37 -0700481 if (!partition) {
482 return device->WriteFail("Partition does not exist");
483 }
David Andersonad970fc2019-08-27 14:01:16 -0700484
485 // Remove the updated flag to cancel any snapshots.
486 uint32_t attrs = partition->attributes();
487 partition->set_attributes(attrs & ~LP_PARTITION_ATTR_UPDATED);
488
David Anderson0d4277d2018-07-31 13:27:37 -0700489 if (!builder->ResizePartition(partition, partition_size)) {
490 return device->WriteFail("Not enough space to resize partition");
491 }
492 if (!builder.Write()) {
493 return device->WriteFail("Failed to write partition table");
494 }
495 return device->WriteOkay("Partition resized");
496}
David Anderson38b3c7a2018-08-15 16:27:42 -0700497
David Andersonad970fc2019-08-27 14:01:16 -0700498void CancelPartitionSnapshot(FastbootDevice* device, const std::string& partition_name) {
499 PartitionBuilder builder(device, partition_name);
500 if (!builder.Valid()) return;
501
502 auto partition = builder->FindPartition(partition_name);
503 if (!partition) return;
504
505 // Remove the updated flag to cancel any snapshots.
506 uint32_t attrs = partition->attributes();
507 partition->set_attributes(attrs & ~LP_PARTITION_ATTR_UPDATED);
508
509 builder.Write();
510}
511
512bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) {
513 if (args.size() < 2) {
514 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
515 }
516
517 if (GetDeviceLockStatus()) {
518 return device->WriteStatus(FastbootResult::FAIL,
519 "Flashing is not allowed on locked devices");
520 }
521
522 const auto& partition_name = args[1];
David Anderson220ddb12019-10-31 18:02:41 -0700523 if (IsProtectedPartitionDuringMerge(device, partition_name)) {
524 auto message = "Cannot flash " + partition_name + " while a snapshot update is in progress";
525 return device->WriteFail(message);
526 }
527
David Andersonad970fc2019-08-27 14:01:16 -0700528 if (LogicalPartitionExists(device, partition_name)) {
529 CancelPartitionSnapshot(device, partition_name);
530 }
531
532 int ret = Flash(device, partition_name);
533 if (ret < 0) {
534 return device->WriteStatus(FastbootResult::FAIL, strerror(-ret));
535 }
536 return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded");
537}
538
David Anderson38b3c7a2018-08-15 16:27:42 -0700539bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) {
540 if (args.size() < 2) {
541 return device->WriteFail("Invalid arguments");
542 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700543
544 if (GetDeviceLockStatus()) {
545 return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices");
546 }
547
David Anderson38b3c7a2018-08-15 16:27:42 -0700548 bool wipe = (args.size() >= 3 && args[2] == "wipe");
549 return UpdateSuper(device, args[1], wipe);
550}
David Anderson1d504e32019-01-15 14:38:20 -0800551
David Anderson3d782d52019-01-29 13:09:49 -0800552bool GsiHandler(FastbootDevice* device, const std::vector<std::string>& args) {
David Anderson1d504e32019-01-15 14:38:20 -0800553 if (args.size() != 2) {
554 return device->WriteFail("Invalid arguments");
555 }
David Anderson3d782d52019-01-29 13:09:49 -0800556
557 AutoMountMetadata mount_metadata;
558 if (!mount_metadata) {
559 return device->WriteFail("Could not find GSI install");
560 }
561
562 if (!android::gsi::IsGsiInstalled()) {
563 return device->WriteStatus(FastbootResult::FAIL, "No GSI is installed");
564 }
565
David Anderson1d504e32019-01-15 14:38:20 -0800566 if (args[1] == "wipe") {
567 if (!android::gsi::UninstallGsi()) {
568 return device->WriteStatus(FastbootResult::FAIL, strerror(errno));
569 }
570 } else if (args[1] == "disable") {
571 if (!android::gsi::DisableGsi()) {
572 return device->WriteStatus(FastbootResult::FAIL, strerror(errno));
573 }
574 }
575 return device->WriteStatus(FastbootResult::OKAY, "Success");
576}
David Andersonab8f4662019-10-21 16:45:59 -0700577
578bool SnapshotUpdateHandler(FastbootDevice* device, const std::vector<std::string>& args) {
579 // Note that we use the HAL rather than mounting /metadata, since we want
580 // our results to match the bootloader.
David Anderson220ddb12019-10-31 18:02:41 -0700581 auto hal = device->boot1_1();
David Andersonab8f4662019-10-21 16:45:59 -0700582 if (!hal) return device->WriteFail("Not supported");
583
David Andersonab8f4662019-10-21 16:45:59 -0700584 // If no arguments, return the same thing as a getvar. Note that we get the
585 // HAL first so we can return "not supported" before we return the less
586 // specific error message below.
587 if (args.size() < 2 || args[1].empty()) {
588 std::string message;
589 if (!GetSnapshotUpdateStatus(device, {}, &message)) {
590 return device->WriteFail("Could not determine update status");
591 }
592 device->WriteInfo(message);
593 return device->WriteOkay("");
594 }
595
David Anderson220ddb12019-10-31 18:02:41 -0700596 MergeStatus status = hal->getSnapshotMergeStatus();
597
598 if (args.size() != 2) {
David Andersonab8f4662019-10-21 16:45:59 -0700599 return device->WriteFail("Invalid arguments");
600 }
David Anderson220ddb12019-10-31 18:02:41 -0700601 if (args[1] == "cancel") {
602 switch (status) {
603 case MergeStatus::SNAPSHOTTED:
604 case MergeStatus::MERGING:
605 hal->setSnapshotMergeStatus(MergeStatus::CANCELLED);
606 break;
607 default:
608 break;
609 }
610 } else if (args[1] == "merge") {
611 if (status != MergeStatus::MERGING) {
612 return device->WriteFail("No snapshot merge is in progress");
613 }
David Andersonab8f4662019-10-21 16:45:59 -0700614
David Anderson220ddb12019-10-31 18:02:41 -0700615 auto sm = SnapshotManager::NewForFirstStageMount();
616 if (!sm) {
617 return device->WriteFail("Unable to create SnapshotManager");
618 }
619 if (!sm->HandleImminentDataWipe()) {
620 return device->WriteFail("Unable to finish snapshot merge");
621 }
622 } else {
623 return device->WriteFail("Invalid parameter to snapshot-update");
David Andersonab8f4662019-10-21 16:45:59 -0700624 }
625 return device->WriteStatus(FastbootResult::OKAY, "Success");
626}