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