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; |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 43 | using namespace android::fs_mgr; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 44 | |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 45 | struct VariableHandlers { |
| 46 | // Callback to retrieve the value of a single variable. |
| 47 | std::function<bool(FastbootDevice*, const std::vector<std::string>&, std::string*)> get; |
| 48 | // Callback to retrieve all possible argument combinations, for getvar all. |
| 49 | std::function<std::vector<std::vector<std::string>>(FastbootDevice*)> get_all_args; |
| 50 | }; |
| 51 | |
| 52 | static void GetAllVars(FastbootDevice* device, const std::string& name, |
| 53 | const VariableHandlers& handlers) { |
| 54 | if (!handlers.get_all_args) { |
| 55 | std::string message; |
| 56 | if (!handlers.get(device, std::vector<std::string>(), &message)) { |
| 57 | return; |
| 58 | } |
| 59 | device->WriteInfo(android::base::StringPrintf("%s:%s", name.c_str(), message.c_str())); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | auto all_args = handlers.get_all_args(device); |
| 64 | for (const auto& args : all_args) { |
| 65 | std::string message; |
| 66 | if (!handlers.get(device, args, &message)) { |
| 67 | continue; |
| 68 | } |
| 69 | std::string arg_string = android::base::Join(args, ":"); |
| 70 | device->WriteInfo(android::base::StringPrintf("%s:%s:%s", name.c_str(), arg_string.c_str(), |
| 71 | message.c_str())); |
| 72 | } |
| 73 | } |
| 74 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 75 | bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 76 | const std::unordered_map<std::string, VariableHandlers> kVariableMap = { |
| 77 | {FB_VAR_VERSION, {GetVersion, nullptr}}, |
| 78 | {FB_VAR_VERSION_BOOTLOADER, {GetBootloaderVersion, nullptr}}, |
| 79 | {FB_VAR_VERSION_BASEBAND, {GetBasebandVersion, nullptr}}, |
| 80 | {FB_VAR_PRODUCT, {GetProduct, nullptr}}, |
| 81 | {FB_VAR_SERIALNO, {GetSerial, nullptr}}, |
| 82 | {FB_VAR_SECURE, {GetSecure, nullptr}}, |
| 83 | {FB_VAR_UNLOCKED, {GetUnlocked, nullptr}}, |
| 84 | {FB_VAR_MAX_DOWNLOAD_SIZE, {GetMaxDownloadSize, nullptr}}, |
| 85 | {FB_VAR_CURRENT_SLOT, {::GetCurrentSlot, nullptr}}, |
| 86 | {FB_VAR_SLOT_COUNT, {GetSlotCount, nullptr}}, |
| 87 | {FB_VAR_HAS_SLOT, {GetHasSlot, GetAllPartitionArgsNoSlot}}, |
| 88 | {FB_VAR_SLOT_SUCCESSFUL, {GetSlotSuccessful, nullptr}}, |
| 89 | {FB_VAR_SLOT_UNBOOTABLE, {GetSlotUnbootable, nullptr}}, |
| 90 | {FB_VAR_PARTITION_SIZE, {GetPartitionSize, GetAllPartitionArgsWithSlot}}, |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 91 | {FB_VAR_PARTITION_TYPE, {GetPartitionType, GetAllPartitionArgsWithSlot}}, |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 92 | {FB_VAR_IS_LOGICAL, {GetPartitionIsLogical, GetAllPartitionArgsWithSlot}}, |
David Anderson | c091c17 | 2018-09-04 18:11:03 -0700 | [diff] [blame] | 93 | {FB_VAR_IS_USERSPACE, {GetIsUserspace, nullptr}}, |
| 94 | {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}}}; |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 95 | |
| 96 | if (args.size() < 2) { |
| 97 | return device->WriteFail("Missing argument"); |
| 98 | } |
| 99 | |
| 100 | // Special case: return all variables that we can. |
| 101 | if (args[1] == "all") { |
| 102 | for (const auto& [name, handlers] : kVariableMap) { |
| 103 | GetAllVars(device, name, handlers); |
| 104 | } |
| 105 | return device->WriteOkay(""); |
| 106 | } |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 107 | |
| 108 | // args[0] is command name, args[1] is variable. |
| 109 | auto found_variable = kVariableMap.find(args[1]); |
| 110 | if (found_variable == kVariableMap.end()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 111 | return device->WriteFail("Unknown variable"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 112 | } |
| 113 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 114 | std::string message; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 115 | std::vector<std::string> getvar_args(args.begin() + 2, args.end()); |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 116 | if (!found_variable->second.get(device, getvar_args, &message)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 117 | return device->WriteFail(message); |
| 118 | } |
| 119 | return device->WriteOkay(message); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 120 | } |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 121 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 122 | bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 123 | if (args.size() < 2) { |
| 124 | return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments"); |
| 125 | } |
| 126 | PartitionHandle handle; |
| 127 | if (!OpenPartition(device, args[1], &handle)) { |
| 128 | return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist"); |
| 129 | } |
| 130 | if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) { |
| 131 | return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded"); |
| 132 | } |
| 133 | return device->WriteStatus(FastbootResult::FAIL, "Erasing failed"); |
| 134 | } |
| 135 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 136 | bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 137 | if (args.size() < 2) { |
| 138 | return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified"); |
| 139 | } |
| 140 | // arg[0] is the command name, arg[1] contains size of data to be downloaded |
| 141 | unsigned int size; |
| 142 | if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) { |
| 143 | return device->WriteStatus(FastbootResult::FAIL, "Invalid size"); |
| 144 | } |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 145 | device->download_data().resize(size); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 146 | if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) { |
| 147 | return false; |
| 148 | } |
| 149 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 150 | if (device->HandleData(true, &device->download_data())) { |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 151 | return device->WriteStatus(FastbootResult::OKAY, ""); |
| 152 | } |
| 153 | |
| 154 | PLOG(ERROR) << "Couldn't download data"; |
| 155 | return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data"); |
| 156 | } |
| 157 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 158 | bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 159 | if (args.size() < 2) { |
| 160 | return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments"); |
| 161 | } |
| 162 | int ret = Flash(device, args[1]); |
| 163 | if (ret < 0) { |
| 164 | return device->WriteStatus(FastbootResult::FAIL, strerror(-ret)); |
| 165 | } |
| 166 | return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded"); |
| 167 | } |
| 168 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 169 | bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 170 | if (args.size() < 2) { |
| 171 | return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument"); |
| 172 | } |
| 173 | |
| 174 | // Slot suffix needs to be between 'a' and 'z'. |
| 175 | Slot slot; |
| 176 | if (!GetSlotNumber(args[1], &slot)) { |
| 177 | return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix"); |
| 178 | } |
| 179 | |
| 180 | // Non-A/B devices will not have a boot control HAL. |
| 181 | auto boot_control_hal = device->boot_control_hal(); |
| 182 | if (!boot_control_hal) { |
| 183 | return device->WriteStatus(FastbootResult::FAIL, |
| 184 | "Cannot set slot: boot control HAL absent"); |
| 185 | } |
| 186 | if (slot >= boot_control_hal->getNumberSlots()) { |
| 187 | return device->WriteStatus(FastbootResult::FAIL, "Slot out of range"); |
| 188 | } |
| 189 | CommandResult ret; |
| 190 | auto cb = [&ret](CommandResult result) { ret = result; }; |
| 191 | auto result = boot_control_hal->setActiveBootSlot(slot, cb); |
| 192 | if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, ""); |
| 193 | return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot"); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 197 | auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down"); |
| 198 | android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot"); |
| 199 | device->CloseDevice(); |
| 200 | TEMP_FAILURE_RETRY(pause()); |
| 201 | return result; |
| 202 | } |
| 203 | |
| 204 | bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 205 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting"); |
| 206 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot"); |
| 207 | device->CloseDevice(); |
| 208 | TEMP_FAILURE_RETRY(pause()); |
| 209 | return result; |
| 210 | } |
| 211 | |
| 212 | bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 213 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader"); |
| 214 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader"); |
| 215 | device->CloseDevice(); |
| 216 | TEMP_FAILURE_RETRY(pause()); |
| 217 | return result; |
| 218 | } |
| 219 | |
| 220 | bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 221 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot"); |
| 222 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot"); |
| 223 | device->CloseDevice(); |
| 224 | TEMP_FAILURE_RETRY(pause()); |
| 225 | return result; |
| 226 | } |
| 227 | |
| 228 | static bool EnterRecovery() { |
| 229 | const char msg_switch_to_recovery = 'r'; |
| 230 | |
| 231 | android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0)); |
| 232 | if (sock < 0) { |
| 233 | PLOG(ERROR) << "Couldn't create sock"; |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | struct sockaddr_un addr = {.sun_family = AF_UNIX}; |
| 238 | strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1); |
| 239 | if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { |
| 240 | PLOG(ERROR) << "Couldn't connect to recovery"; |
| 241 | return false; |
| 242 | } |
| 243 | // Switch to recovery will not update the boot reason since it does not |
| 244 | // require a reboot. |
| 245 | auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery)); |
| 246 | if (ret != sizeof(msg_switch_to_recovery)) { |
| 247 | PLOG(ERROR) << "Couldn't write message to switch to recovery"; |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 255 | auto status = true; |
| 256 | if (EnterRecovery()) { |
| 257 | status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery"); |
| 258 | } else { |
| 259 | status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery"); |
| 260 | } |
| 261 | device->CloseDevice(); |
| 262 | TEMP_FAILURE_RETRY(pause()); |
| 263 | return status; |
| 264 | } |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 265 | |
| 266 | // Helper class for opening a handle to a MetadataBuilder and writing the new |
| 267 | // partition table to the same place it was read. |
| 268 | class PartitionBuilder { |
| 269 | public: |
| 270 | explicit PartitionBuilder(FastbootDevice* device); |
| 271 | |
| 272 | bool Write(); |
| 273 | bool Valid() const { return !!builder_; } |
| 274 | MetadataBuilder* operator->() const { return builder_.get(); } |
| 275 | |
| 276 | private: |
| 277 | std::string super_device_; |
| 278 | uint32_t slot_number_; |
| 279 | std::unique_ptr<MetadataBuilder> builder_; |
| 280 | }; |
| 281 | |
| 282 | PartitionBuilder::PartitionBuilder(FastbootDevice* device) { |
| 283 | auto super_device = FindPhysicalPartition(LP_METADATA_PARTITION_NAME); |
| 284 | if (!super_device) { |
| 285 | return; |
| 286 | } |
| 287 | super_device_ = *super_device; |
| 288 | |
| 289 | std::string slot = device->GetCurrentSlot(); |
| 290 | slot_number_ = SlotNumberForSlotSuffix(slot); |
| 291 | builder_ = MetadataBuilder::New(super_device_, slot_number_); |
| 292 | } |
| 293 | |
| 294 | bool PartitionBuilder::Write() { |
| 295 | std::unique_ptr<LpMetadata> metadata = builder_->Export(); |
| 296 | if (!metadata) { |
| 297 | return false; |
| 298 | } |
| 299 | return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_); |
| 300 | } |
| 301 | |
| 302 | bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 303 | if (args.size() < 3) { |
| 304 | return device->WriteFail("Invalid partition name and size"); |
| 305 | } |
| 306 | |
| 307 | uint64_t partition_size; |
| 308 | std::string partition_name = args[1]; |
| 309 | if (!android::base::ParseUint(args[2].c_str(), &partition_size)) { |
| 310 | return device->WriteFail("Invalid partition size"); |
| 311 | } |
| 312 | |
| 313 | PartitionBuilder builder(device); |
| 314 | if (!builder.Valid()) { |
| 315 | return device->WriteFail("Could not open super partition"); |
| 316 | } |
| 317 | // TODO(112433293) Disallow if the name is in the physical table as well. |
| 318 | if (builder->FindPartition(partition_name)) { |
| 319 | return device->WriteFail("Partition already exists"); |
| 320 | } |
| 321 | |
| 322 | // Make a random UUID, since they're not currently used. |
| 323 | uuid_t uuid; |
| 324 | char uuid_str[37]; |
| 325 | uuid_generate_random(uuid); |
| 326 | uuid_unparse(uuid, uuid_str); |
| 327 | |
| 328 | Partition* partition = builder->AddPartition(partition_name, uuid_str, 0); |
| 329 | if (!partition) { |
| 330 | return device->WriteFail("Failed to add partition"); |
| 331 | } |
| 332 | if (!builder->ResizePartition(partition, partition_size)) { |
| 333 | builder->RemovePartition(partition_name); |
| 334 | return device->WriteFail("Not enough space for partition"); |
| 335 | } |
| 336 | if (!builder.Write()) { |
| 337 | return device->WriteFail("Failed to write partition table"); |
| 338 | } |
| 339 | return device->WriteOkay("Partition created"); |
| 340 | } |
| 341 | |
| 342 | bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 343 | if (args.size() < 2) { |
| 344 | return device->WriteFail("Invalid partition name and size"); |
| 345 | } |
| 346 | |
| 347 | PartitionBuilder builder(device); |
| 348 | if (!builder.Valid()) { |
| 349 | return device->WriteFail("Could not open super partition"); |
| 350 | } |
| 351 | builder->RemovePartition(args[1]); |
| 352 | if (!builder.Write()) { |
| 353 | return device->WriteFail("Failed to write partition table"); |
| 354 | } |
| 355 | return device->WriteOkay("Partition deleted"); |
| 356 | } |
| 357 | |
| 358 | bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 359 | if (args.size() < 3) { |
| 360 | return device->WriteFail("Invalid partition name and size"); |
| 361 | } |
| 362 | |
| 363 | uint64_t partition_size; |
| 364 | std::string partition_name = args[1]; |
| 365 | if (!android::base::ParseUint(args[2].c_str(), &partition_size)) { |
| 366 | return device->WriteFail("Invalid partition size"); |
| 367 | } |
| 368 | |
| 369 | PartitionBuilder builder(device); |
| 370 | if (!builder.Valid()) { |
| 371 | return device->WriteFail("Could not open super partition"); |
| 372 | } |
| 373 | |
| 374 | Partition* partition = builder->FindPartition(partition_name); |
| 375 | if (!partition) { |
| 376 | return device->WriteFail("Partition does not exist"); |
| 377 | } |
| 378 | if (!builder->ResizePartition(partition, partition_size)) { |
| 379 | return device->WriteFail("Not enough space to resize partition"); |
| 380 | } |
| 381 | if (!builder.Write()) { |
| 382 | return device->WriteFail("Failed to write partition table"); |
| 383 | } |
| 384 | return device->WriteOkay("Partition resized"); |
| 385 | } |
David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 386 | |
| 387 | bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 388 | if (args.size() < 2) { |
| 389 | return device->WriteFail("Invalid arguments"); |
| 390 | } |
| 391 | bool wipe = (args.size() >= 3 && args[2] == "wipe"); |
| 392 | return UpdateSuper(device, args[1], wipe); |
| 393 | } |