Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 29 | #include <ext4_utils/wipe.h> |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 30 | #include <liblp/builder.h> |
| 31 | #include <liblp/liblp.h> |
| 32 | #include <uuid/uuid.h> |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 33 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 34 | #include "constants.h" |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 35 | #include "fastboot_device.h" |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 36 | #include "flashing.h" |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 37 | #include "utility.h" |
| 38 | |
| 39 | using ::android::hardware::hidl_string; |
| 40 | using ::android::hardware::boot::V1_0::BoolResult; |
| 41 | using ::android::hardware::boot::V1_0::CommandResult; |
| 42 | using ::android::hardware::boot::V1_0::Slot; |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 43 | using ::android::hardware::fastboot::V1_0::Result; |
| 44 | using ::android::hardware::fastboot::V1_0::Status; |
| 45 | |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 46 | using namespace android::fs_mgr; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 47 | |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 48 | struct 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 | |
| 55 | static 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 Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 78 | bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 79 | 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 Valsaraju | 4af8090 | 2018-09-26 13:08:16 -0700 | [diff] [blame] | 85 | {FB_VAR_VARIANT, {GetVariant, nullptr}}, |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 86 | {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 Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 95 | {FB_VAR_PARTITION_TYPE, {GetPartitionType, GetAllPartitionArgsWithSlot}}, |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 96 | {FB_VAR_IS_LOGICAL, {GetPartitionIsLogical, GetAllPartitionArgsWithSlot}}, |
David Anderson | c091c17 | 2018-09-04 18:11:03 -0700 | [diff] [blame] | 97 | {FB_VAR_IS_USERSPACE, {GetIsUserspace, nullptr}}, |
Hridya Valsaraju | 7c9bbe9 | 2018-09-27 10:41:01 -0700 | [diff] [blame^] | 98 | {FB_VAR_OFF_MODE_CHARGE_STATE, {GetOffModeChargeState, nullptr}}, |
David Anderson | c091c17 | 2018-09-04 18:11:03 -0700 | [diff] [blame] | 99 | {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}}}; |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 100 | |
| 101 | if (args.size() < 2) { |
| 102 | return device->WriteFail("Missing argument"); |
| 103 | } |
| 104 | |
| 105 | // Special case: return all variables that we can. |
| 106 | if (args[1] == "all") { |
| 107 | for (const auto& [name, handlers] : kVariableMap) { |
| 108 | GetAllVars(device, name, handlers); |
| 109 | } |
| 110 | return device->WriteOkay(""); |
| 111 | } |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 112 | |
| 113 | // args[0] is command name, args[1] is variable. |
| 114 | auto found_variable = kVariableMap.find(args[1]); |
| 115 | if (found_variable == kVariableMap.end()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 116 | return device->WriteFail("Unknown variable"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 117 | } |
| 118 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 119 | std::string message; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 120 | std::vector<std::string> getvar_args(args.begin() + 2, args.end()); |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 121 | if (!found_variable->second.get(device, getvar_args, &message)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 122 | return device->WriteFail(message); |
| 123 | } |
| 124 | return device->WriteOkay(message); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 125 | } |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 126 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 127 | bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 128 | if (args.size() < 2) { |
| 129 | return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments"); |
| 130 | } |
| 131 | PartitionHandle handle; |
| 132 | if (!OpenPartition(device, args[1], &handle)) { |
| 133 | return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist"); |
| 134 | } |
| 135 | if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) { |
| 136 | return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded"); |
| 137 | } |
| 138 | return device->WriteStatus(FastbootResult::FAIL, "Erasing failed"); |
| 139 | } |
| 140 | |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 141 | bool OemCmdHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 142 | auto fastboot_hal = device->fastboot_hal(); |
| 143 | if (!fastboot_hal) { |
| 144 | return device->WriteStatus(FastbootResult::FAIL, "Unable to open fastboot HAL"); |
| 145 | } |
| 146 | |
| 147 | Result ret; |
| 148 | auto ret_val = fastboot_hal->doOemCommand(args[0], [&](Result result) { ret = result; }); |
| 149 | if (!ret_val.isOk()) { |
| 150 | return device->WriteStatus(FastbootResult::FAIL, "Unable to do OEM command"); |
| 151 | } |
| 152 | if (ret.status != Status::SUCCESS) { |
| 153 | return device->WriteStatus(FastbootResult::FAIL, ret.message); |
| 154 | } |
| 155 | |
| 156 | return device->WriteStatus(FastbootResult::OKAY, ret.message); |
| 157 | } |
| 158 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 159 | bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 160 | if (args.size() < 2) { |
| 161 | return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified"); |
| 162 | } |
| 163 | // arg[0] is the command name, arg[1] contains size of data to be downloaded |
| 164 | unsigned int size; |
| 165 | if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) { |
| 166 | return device->WriteStatus(FastbootResult::FAIL, "Invalid size"); |
| 167 | } |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 168 | device->download_data().resize(size); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 169 | if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) { |
| 170 | return false; |
| 171 | } |
| 172 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 173 | if (device->HandleData(true, &device->download_data())) { |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 174 | return device->WriteStatus(FastbootResult::OKAY, ""); |
| 175 | } |
| 176 | |
| 177 | PLOG(ERROR) << "Couldn't download data"; |
| 178 | return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data"); |
| 179 | } |
| 180 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 181 | bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 182 | if (args.size() < 2) { |
| 183 | return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments"); |
| 184 | } |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 185 | |
| 186 | if (GetDeviceLockStatus()) { |
| 187 | return device->WriteStatus(FastbootResult::FAIL, |
| 188 | "Flashing is not allowed on locked devices"); |
| 189 | } |
| 190 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 191 | int ret = Flash(device, args[1]); |
| 192 | if (ret < 0) { |
| 193 | return device->WriteStatus(FastbootResult::FAIL, strerror(-ret)); |
| 194 | } |
| 195 | return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded"); |
| 196 | } |
| 197 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 198 | bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 199 | if (args.size() < 2) { |
| 200 | return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument"); |
| 201 | } |
| 202 | |
| 203 | // Slot suffix needs to be between 'a' and 'z'. |
| 204 | Slot slot; |
| 205 | if (!GetSlotNumber(args[1], &slot)) { |
| 206 | return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix"); |
| 207 | } |
| 208 | |
| 209 | // Non-A/B devices will not have a boot control HAL. |
| 210 | auto boot_control_hal = device->boot_control_hal(); |
| 211 | if (!boot_control_hal) { |
| 212 | return device->WriteStatus(FastbootResult::FAIL, |
| 213 | "Cannot set slot: boot control HAL absent"); |
| 214 | } |
| 215 | if (slot >= boot_control_hal->getNumberSlots()) { |
| 216 | return device->WriteStatus(FastbootResult::FAIL, "Slot out of range"); |
| 217 | } |
| 218 | CommandResult ret; |
| 219 | auto cb = [&ret](CommandResult result) { ret = result; }; |
| 220 | auto result = boot_control_hal->setActiveBootSlot(slot, cb); |
| 221 | if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, ""); |
| 222 | return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot"); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 226 | auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down"); |
| 227 | android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot"); |
| 228 | device->CloseDevice(); |
| 229 | TEMP_FAILURE_RETRY(pause()); |
| 230 | return result; |
| 231 | } |
| 232 | |
| 233 | bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 234 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting"); |
| 235 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot"); |
| 236 | device->CloseDevice(); |
| 237 | TEMP_FAILURE_RETRY(pause()); |
| 238 | return result; |
| 239 | } |
| 240 | |
| 241 | bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 242 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader"); |
| 243 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader"); |
| 244 | device->CloseDevice(); |
| 245 | TEMP_FAILURE_RETRY(pause()); |
| 246 | return result; |
| 247 | } |
| 248 | |
| 249 | bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 250 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot"); |
| 251 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot"); |
| 252 | device->CloseDevice(); |
| 253 | TEMP_FAILURE_RETRY(pause()); |
| 254 | return result; |
| 255 | } |
| 256 | |
| 257 | static bool EnterRecovery() { |
| 258 | const char msg_switch_to_recovery = 'r'; |
| 259 | |
| 260 | android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0)); |
| 261 | if (sock < 0) { |
| 262 | PLOG(ERROR) << "Couldn't create sock"; |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | struct sockaddr_un addr = {.sun_family = AF_UNIX}; |
| 267 | strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1); |
| 268 | if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { |
| 269 | PLOG(ERROR) << "Couldn't connect to recovery"; |
| 270 | return false; |
| 271 | } |
| 272 | // Switch to recovery will not update the boot reason since it does not |
| 273 | // require a reboot. |
| 274 | auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery)); |
| 275 | if (ret != sizeof(msg_switch_to_recovery)) { |
| 276 | PLOG(ERROR) << "Couldn't write message to switch to recovery"; |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 284 | auto status = true; |
| 285 | if (EnterRecovery()) { |
| 286 | status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery"); |
| 287 | } else { |
| 288 | status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery"); |
| 289 | } |
| 290 | device->CloseDevice(); |
| 291 | TEMP_FAILURE_RETRY(pause()); |
| 292 | return status; |
| 293 | } |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 294 | |
| 295 | // Helper class for opening a handle to a MetadataBuilder and writing the new |
| 296 | // partition table to the same place it was read. |
| 297 | class PartitionBuilder { |
| 298 | public: |
| 299 | explicit PartitionBuilder(FastbootDevice* device); |
| 300 | |
| 301 | bool Write(); |
| 302 | bool Valid() const { return !!builder_; } |
| 303 | MetadataBuilder* operator->() const { return builder_.get(); } |
| 304 | |
| 305 | private: |
| 306 | std::string super_device_; |
| 307 | uint32_t slot_number_; |
| 308 | std::unique_ptr<MetadataBuilder> builder_; |
| 309 | }; |
| 310 | |
| 311 | PartitionBuilder::PartitionBuilder(FastbootDevice* device) { |
| 312 | auto super_device = FindPhysicalPartition(LP_METADATA_PARTITION_NAME); |
| 313 | if (!super_device) { |
| 314 | return; |
| 315 | } |
| 316 | super_device_ = *super_device; |
| 317 | |
| 318 | std::string slot = device->GetCurrentSlot(); |
| 319 | slot_number_ = SlotNumberForSlotSuffix(slot); |
| 320 | builder_ = MetadataBuilder::New(super_device_, slot_number_); |
| 321 | } |
| 322 | |
| 323 | bool PartitionBuilder::Write() { |
| 324 | std::unique_ptr<LpMetadata> metadata = builder_->Export(); |
| 325 | if (!metadata) { |
| 326 | return false; |
| 327 | } |
| 328 | return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_); |
| 329 | } |
| 330 | |
| 331 | bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 332 | if (args.size() < 3) { |
| 333 | return device->WriteFail("Invalid partition name and size"); |
| 334 | } |
| 335 | |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 336 | if (GetDeviceLockStatus()) { |
| 337 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 338 | } |
| 339 | |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 340 | uint64_t partition_size; |
| 341 | std::string partition_name = args[1]; |
| 342 | if (!android::base::ParseUint(args[2].c_str(), &partition_size)) { |
| 343 | return device->WriteFail("Invalid partition size"); |
| 344 | } |
| 345 | |
| 346 | PartitionBuilder builder(device); |
| 347 | if (!builder.Valid()) { |
| 348 | return device->WriteFail("Could not open super partition"); |
| 349 | } |
| 350 | // TODO(112433293) Disallow if the name is in the physical table as well. |
| 351 | if (builder->FindPartition(partition_name)) { |
| 352 | return device->WriteFail("Partition already exists"); |
| 353 | } |
| 354 | |
| 355 | // Make a random UUID, since they're not currently used. |
| 356 | uuid_t uuid; |
| 357 | char uuid_str[37]; |
| 358 | uuid_generate_random(uuid); |
| 359 | uuid_unparse(uuid, uuid_str); |
| 360 | |
| 361 | Partition* partition = builder->AddPartition(partition_name, uuid_str, 0); |
| 362 | if (!partition) { |
| 363 | return device->WriteFail("Failed to add partition"); |
| 364 | } |
| 365 | if (!builder->ResizePartition(partition, partition_size)) { |
| 366 | builder->RemovePartition(partition_name); |
| 367 | return device->WriteFail("Not enough space for partition"); |
| 368 | } |
| 369 | if (!builder.Write()) { |
| 370 | return device->WriteFail("Failed to write partition table"); |
| 371 | } |
| 372 | return device->WriteOkay("Partition created"); |
| 373 | } |
| 374 | |
| 375 | bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 376 | if (args.size() < 2) { |
| 377 | return device->WriteFail("Invalid partition name and size"); |
| 378 | } |
| 379 | |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 380 | if (GetDeviceLockStatus()) { |
| 381 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 382 | } |
| 383 | |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 384 | PartitionBuilder builder(device); |
| 385 | if (!builder.Valid()) { |
| 386 | return device->WriteFail("Could not open super partition"); |
| 387 | } |
| 388 | builder->RemovePartition(args[1]); |
| 389 | if (!builder.Write()) { |
| 390 | return device->WriteFail("Failed to write partition table"); |
| 391 | } |
| 392 | return device->WriteOkay("Partition deleted"); |
| 393 | } |
| 394 | |
| 395 | bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 396 | if (args.size() < 3) { |
| 397 | return device->WriteFail("Invalid partition name and size"); |
| 398 | } |
| 399 | |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 400 | if (GetDeviceLockStatus()) { |
| 401 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 402 | } |
| 403 | |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 404 | uint64_t partition_size; |
| 405 | std::string partition_name = args[1]; |
| 406 | if (!android::base::ParseUint(args[2].c_str(), &partition_size)) { |
| 407 | return device->WriteFail("Invalid partition size"); |
| 408 | } |
| 409 | |
| 410 | PartitionBuilder builder(device); |
| 411 | if (!builder.Valid()) { |
| 412 | return device->WriteFail("Could not open super partition"); |
| 413 | } |
| 414 | |
| 415 | Partition* partition = builder->FindPartition(partition_name); |
| 416 | if (!partition) { |
| 417 | return device->WriteFail("Partition does not exist"); |
| 418 | } |
| 419 | if (!builder->ResizePartition(partition, partition_size)) { |
| 420 | return device->WriteFail("Not enough space to resize partition"); |
| 421 | } |
| 422 | if (!builder.Write()) { |
| 423 | return device->WriteFail("Failed to write partition table"); |
| 424 | } |
| 425 | return device->WriteOkay("Partition resized"); |
| 426 | } |
David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 427 | |
| 428 | bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 429 | if (args.size() < 2) { |
| 430 | return device->WriteFail("Invalid arguments"); |
| 431 | } |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 432 | |
| 433 | if (GetDeviceLockStatus()) { |
| 434 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 435 | } |
| 436 | |
David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 437 | bool wipe = (args.size() >= 3 && args[2] == "wipe"); |
| 438 | return UpdateSuper(device, args[1], wipe); |
| 439 | } |