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