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 | |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 22 | #include <unordered_set> |
| 23 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 24 | #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 Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 30 | #include <android/hardware/boot/1.1/IBootControl.h> |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 31 | #include <cutils/android_reboot.h> |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 32 | #include <ext4_utils/wipe.h> |
David Anderson | 5cbd2e4 | 2018-09-27 10:53:04 -0700 | [diff] [blame] | 33 | #include <fs_mgr.h> |
David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 34 | #include <libgsi/libgsi.h> |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 35 | #include <liblp/builder.h> |
| 36 | #include <liblp/liblp.h> |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 37 | #include <libsnapshot/snapshot.h> |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 38 | #include <uuid/uuid.h> |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 39 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 40 | #include "constants.h" |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 41 | #include "fastboot_device.h" |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 42 | #include "flashing.h" |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 43 | #include "utility.h" |
| 44 | |
David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 45 | using android::fs_mgr::MetadataBuilder; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 46 | using ::android::hardware::hidl_string; |
| 47 | using ::android::hardware::boot::V1_0::BoolResult; |
| 48 | using ::android::hardware::boot::V1_0::CommandResult; |
| 49 | using ::android::hardware::boot::V1_0::Slot; |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 50 | using ::android::hardware::boot::V1_1::MergeStatus; |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 51 | using ::android::hardware::fastboot::V1_0::Result; |
| 52 | using ::android::hardware::fastboot::V1_0::Status; |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 53 | using android::snapshot::SnapshotManager; |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 54 | using IBootControl1_1 = ::android::hardware::boot::V1_1::IBootControl; |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 55 | |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 56 | struct 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 Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 63 | static 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 | |
| 72 | static 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 Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 81 | static 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 Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 104 | bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 105 | 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}}, |
Bowgo Tsai | 99f9a38 | 2020-01-21 18:31:23 +0800 | [diff] [blame^] | 109 | {FB_VAR_VERSION_OS, {GetOsVersion, nullptr}}, |
| 110 | {FB_VAR_VERSION_VNDK, {GetVndkVersion, nullptr}}, |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 111 | {FB_VAR_PRODUCT, {GetProduct, nullptr}}, |
| 112 | {FB_VAR_SERIALNO, {GetSerial, nullptr}}, |
Hridya Valsaraju | 4af8090 | 2018-09-26 13:08:16 -0700 | [diff] [blame] | 113 | {FB_VAR_VARIANT, {GetVariant, nullptr}}, |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 114 | {FB_VAR_SECURE, {GetSecure, nullptr}}, |
| 115 | {FB_VAR_UNLOCKED, {GetUnlocked, nullptr}}, |
| 116 | {FB_VAR_MAX_DOWNLOAD_SIZE, {GetMaxDownloadSize, nullptr}}, |
| 117 | {FB_VAR_CURRENT_SLOT, {::GetCurrentSlot, nullptr}}, |
| 118 | {FB_VAR_SLOT_COUNT, {GetSlotCount, nullptr}}, |
| 119 | {FB_VAR_HAS_SLOT, {GetHasSlot, GetAllPartitionArgsNoSlot}}, |
| 120 | {FB_VAR_SLOT_SUCCESSFUL, {GetSlotSuccessful, nullptr}}, |
| 121 | {FB_VAR_SLOT_UNBOOTABLE, {GetSlotUnbootable, nullptr}}, |
David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 122 | {FB_VAR_PARTITION_SIZE, {GetPartitionSize, GetAllPartitionArgsWithSlot}}, |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 123 | {FB_VAR_PARTITION_TYPE, {GetPartitionType, GetAllPartitionArgsWithSlot}}, |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 124 | {FB_VAR_IS_LOGICAL, {GetPartitionIsLogical, GetAllPartitionArgsWithSlot}}, |
David Anderson | c091c17 | 2018-09-04 18:11:03 -0700 | [diff] [blame] | 125 | {FB_VAR_IS_USERSPACE, {GetIsUserspace, nullptr}}, |
Hridya Valsaraju | 7c9bbe9 | 2018-09-27 10:41:01 -0700 | [diff] [blame] | 126 | {FB_VAR_OFF_MODE_CHARGE_STATE, {GetOffModeChargeState, nullptr}}, |
Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 127 | {FB_VAR_BATTERY_VOLTAGE, {GetBatteryVoltage, nullptr}}, |
Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 128 | {FB_VAR_BATTERY_SOC_OK, {GetBatterySoCOk, nullptr}}, |
David Anderson | 90fe0a4 | 2018-11-05 18:01:32 -0800 | [diff] [blame] | 129 | {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}}, |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 130 | {FB_VAR_SUPER_PARTITION_NAME, {GetSuperPartitionName, nullptr}}, |
Bowgo Tsai | 33da5c9 | 2019-11-13 17:13:49 +0800 | [diff] [blame] | 131 | {FB_VAR_SNAPSHOT_UPDATE_STATUS, {GetSnapshotUpdateStatus, nullptr}}, |
Bowgo Tsai | 99f9a38 | 2020-01-21 18:31:23 +0800 | [diff] [blame^] | 132 | {FB_VAR_CPU_ABI, {GetCpuAbi, nullptr}}, |
| 133 | {FB_VAR_SYSTEM_FINGERPRINT, {GetSystemFingerprint, nullptr}}, |
| 134 | {FB_VAR_VENDOR_FINGERPRINT, {GetVendorFingerprint, nullptr}}, |
| 135 | {FB_VAR_DYNAMIC_PARTITION, {GetDynamicPartition, nullptr}}, |
| 136 | {FB_VAR_FIRST_API_LEVEL, {GetFirstApiLevel, nullptr}}, |
| 137 | {FB_VAR_SECURITY_PATCH_LEVEL, {GetSecurityPatchLevel, nullptr}}, |
| 138 | {FB_VAR_TREBLE_ENABLED, {GetTrebleEnabled, nullptr}}}; |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 139 | |
| 140 | if (args.size() < 2) { |
| 141 | return device->WriteFail("Missing argument"); |
| 142 | } |
| 143 | |
| 144 | // Special case: return all variables that we can. |
| 145 | if (args[1] == "all") { |
| 146 | for (const auto& [name, handlers] : kVariableMap) { |
| 147 | GetAllVars(device, name, handlers); |
| 148 | } |
| 149 | return device->WriteOkay(""); |
| 150 | } |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 151 | |
| 152 | // args[0] is command name, args[1] is variable. |
| 153 | auto found_variable = kVariableMap.find(args[1]); |
| 154 | if (found_variable == kVariableMap.end()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 155 | return device->WriteFail("Unknown variable"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 156 | } |
| 157 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 158 | std::string message; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 159 | std::vector<std::string> getvar_args(args.begin() + 2, args.end()); |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 160 | if (!found_variable->second.get(device, getvar_args, &message)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 161 | return device->WriteFail(message); |
| 162 | } |
| 163 | return device->WriteOkay(message); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 164 | } |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 165 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 166 | bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 167 | if (args.size() < 2) { |
| 168 | return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments"); |
| 169 | } |
Hridya Valsaraju | d1e6231 | 2018-10-08 09:13:17 -0700 | [diff] [blame] | 170 | |
| 171 | if (GetDeviceLockStatus()) { |
| 172 | return device->WriteStatus(FastbootResult::FAIL, "Erase is not allowed on locked devices"); |
| 173 | } |
| 174 | |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 175 | const auto& partition_name = args[1]; |
| 176 | if (IsProtectedPartitionDuringMerge(device, partition_name)) { |
| 177 | auto message = "Cannot erase " + partition_name + " while a snapshot update is in progress"; |
| 178 | return device->WriteFail(message); |
| 179 | } |
| 180 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 181 | PartitionHandle handle; |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 182 | if (!OpenPartition(device, partition_name, &handle)) { |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 183 | return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist"); |
| 184 | } |
| 185 | if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) { |
| 186 | return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded"); |
| 187 | } |
| 188 | return device->WriteStatus(FastbootResult::FAIL, "Erasing failed"); |
| 189 | } |
| 190 | |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 191 | bool OemCmdHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 192 | auto fastboot_hal = device->fastboot_hal(); |
| 193 | if (!fastboot_hal) { |
| 194 | return device->WriteStatus(FastbootResult::FAIL, "Unable to open fastboot HAL"); |
| 195 | } |
| 196 | |
| 197 | Result ret; |
| 198 | auto ret_val = fastboot_hal->doOemCommand(args[0], [&](Result result) { ret = result; }); |
| 199 | if (!ret_val.isOk()) { |
| 200 | return device->WriteStatus(FastbootResult::FAIL, "Unable to do OEM command"); |
| 201 | } |
| 202 | if (ret.status != Status::SUCCESS) { |
| 203 | return device->WriteStatus(FastbootResult::FAIL, ret.message); |
| 204 | } |
| 205 | |
| 206 | return device->WriteStatus(FastbootResult::OKAY, ret.message); |
| 207 | } |
| 208 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 209 | bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 210 | if (args.size() < 2) { |
| 211 | return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified"); |
| 212 | } |
Hridya Valsaraju | d1e6231 | 2018-10-08 09:13:17 -0700 | [diff] [blame] | 213 | |
| 214 | if (GetDeviceLockStatus()) { |
| 215 | return device->WriteStatus(FastbootResult::FAIL, |
| 216 | "Download is not allowed on locked devices"); |
| 217 | } |
| 218 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 219 | // arg[0] is the command name, arg[1] contains size of data to be downloaded |
| 220 | unsigned int size; |
Hridya Valsaraju | aae84e8 | 2018-10-08 13:10:25 -0700 | [diff] [blame] | 221 | if (!android::base::ParseUint("0x" + args[1], &size, kMaxDownloadSizeDefault)) { |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 222 | return device->WriteStatus(FastbootResult::FAIL, "Invalid size"); |
| 223 | } |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 224 | device->download_data().resize(size); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 225 | if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) { |
| 226 | return false; |
| 227 | } |
| 228 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 229 | if (device->HandleData(true, &device->download_data())) { |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 230 | return device->WriteStatus(FastbootResult::OKAY, ""); |
| 231 | } |
| 232 | |
| 233 | PLOG(ERROR) << "Couldn't download data"; |
| 234 | return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data"); |
| 235 | } |
| 236 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 237 | bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 238 | if (args.size() < 2) { |
| 239 | return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument"); |
| 240 | } |
| 241 | |
Hridya Valsaraju | d1e6231 | 2018-10-08 09:13:17 -0700 | [diff] [blame] | 242 | if (GetDeviceLockStatus()) { |
| 243 | return device->WriteStatus(FastbootResult::FAIL, |
| 244 | "set_active command is not allowed on locked devices"); |
| 245 | } |
| 246 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 247 | Slot slot; |
| 248 | if (!GetSlotNumber(args[1], &slot)) { |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 249 | // Slot suffix needs to be between 'a' and 'z'. |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 250 | return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix"); |
| 251 | } |
| 252 | |
| 253 | // Non-A/B devices will not have a boot control HAL. |
| 254 | auto boot_control_hal = device->boot_control_hal(); |
| 255 | if (!boot_control_hal) { |
| 256 | return device->WriteStatus(FastbootResult::FAIL, |
| 257 | "Cannot set slot: boot control HAL absent"); |
| 258 | } |
| 259 | if (slot >= boot_control_hal->getNumberSlots()) { |
| 260 | return device->WriteStatus(FastbootResult::FAIL, "Slot out of range"); |
| 261 | } |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 262 | |
| 263 | // If the slot is not changing, do nothing. |
| 264 | if (slot == boot_control_hal->getCurrentSlot()) { |
| 265 | return device->WriteOkay(""); |
| 266 | } |
| 267 | |
| 268 | // Check how to handle the current snapshot state. |
| 269 | if (auto hal11 = device->boot1_1()) { |
| 270 | auto merge_status = hal11->getSnapshotMergeStatus(); |
| 271 | if (merge_status == MergeStatus::MERGING) { |
| 272 | return device->WriteFail("Cannot change slots while a snapshot update is in progress"); |
| 273 | } |
| 274 | // Note: we allow the slot change if the state is SNAPSHOTTED. First- |
| 275 | // stage init does not have access to the HAL, and uses the slot number |
| 276 | // and /metadata OTA state to determine whether a slot change occurred. |
| 277 | // Booting into the old slot would erase the OTA, and switching A->B->A |
| 278 | // would simply resume it if no boots occur in between. Re-flashing |
| 279 | // partitions implicitly cancels the OTA, so leaving the state as-is is |
| 280 | // safe. |
| 281 | if (merge_status == MergeStatus::SNAPSHOTTED) { |
| 282 | device->WriteInfo( |
| 283 | "Changing the active slot with a snapshot applied may cancel the" |
| 284 | " update."); |
| 285 | } |
| 286 | } |
| 287 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 288 | CommandResult ret; |
| 289 | auto cb = [&ret](CommandResult result) { ret = result; }; |
| 290 | auto result = boot_control_hal->setActiveBootSlot(slot, cb); |
Hridya Valsaraju | 20bdf89 | 2018-10-10 11:02:19 -0700 | [diff] [blame] | 291 | if (result.isOk() && ret.success) { |
| 292 | // Save as slot suffix to match the suffix format as returned from |
| 293 | // the boot control HAL. |
| 294 | auto current_slot = "_" + args[1]; |
| 295 | device->set_active_slot(current_slot); |
| 296 | return device->WriteStatus(FastbootResult::OKAY, ""); |
| 297 | } |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 298 | return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot"); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 302 | auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down"); |
| 303 | android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot"); |
| 304 | device->CloseDevice(); |
| 305 | TEMP_FAILURE_RETRY(pause()); |
| 306 | return result; |
| 307 | } |
| 308 | |
| 309 | bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 310 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting"); |
| 311 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot"); |
| 312 | device->CloseDevice(); |
| 313 | TEMP_FAILURE_RETRY(pause()); |
| 314 | return result; |
| 315 | } |
| 316 | |
| 317 | bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 318 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader"); |
| 319 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader"); |
| 320 | device->CloseDevice(); |
| 321 | TEMP_FAILURE_RETRY(pause()); |
| 322 | return result; |
| 323 | } |
| 324 | |
| 325 | bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 326 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot"); |
| 327 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot"); |
| 328 | device->CloseDevice(); |
| 329 | TEMP_FAILURE_RETRY(pause()); |
| 330 | return result; |
| 331 | } |
| 332 | |
| 333 | static bool EnterRecovery() { |
| 334 | const char msg_switch_to_recovery = 'r'; |
| 335 | |
| 336 | android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0)); |
| 337 | if (sock < 0) { |
| 338 | PLOG(ERROR) << "Couldn't create sock"; |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | struct sockaddr_un addr = {.sun_family = AF_UNIX}; |
| 343 | strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1); |
| 344 | if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { |
| 345 | PLOG(ERROR) << "Couldn't connect to recovery"; |
| 346 | return false; |
| 347 | } |
| 348 | // Switch to recovery will not update the boot reason since it does not |
| 349 | // require a reboot. |
| 350 | auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery)); |
| 351 | if (ret != sizeof(msg_switch_to_recovery)) { |
| 352 | PLOG(ERROR) << "Couldn't write message to switch to recovery"; |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 360 | auto status = true; |
| 361 | if (EnterRecovery()) { |
| 362 | status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery"); |
| 363 | } else { |
| 364 | status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery"); |
| 365 | } |
| 366 | device->CloseDevice(); |
| 367 | TEMP_FAILURE_RETRY(pause()); |
| 368 | return status; |
| 369 | } |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 370 | |
| 371 | // Helper class for opening a handle to a MetadataBuilder and writing the new |
| 372 | // partition table to the same place it was read. |
| 373 | class PartitionBuilder { |
| 374 | public: |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 375 | explicit PartitionBuilder(FastbootDevice* device, const std::string& partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 376 | |
| 377 | bool Write(); |
| 378 | bool Valid() const { return !!builder_; } |
| 379 | MetadataBuilder* operator->() const { return builder_.get(); } |
| 380 | |
| 381 | private: |
David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 382 | FastbootDevice* device_; |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 383 | std::string super_device_; |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 384 | uint32_t slot_number_; |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 385 | std::unique_ptr<MetadataBuilder> builder_; |
| 386 | }; |
| 387 | |
David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 388 | PartitionBuilder::PartitionBuilder(FastbootDevice* device, const std::string& partition_name) |
| 389 | : device_(device) { |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 390 | std::string slot_suffix = GetSuperSlotSuffix(device, partition_name); |
David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 391 | slot_number_ = android::fs_mgr::SlotNumberForSlotSuffix(slot_suffix); |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 392 | auto super_device = FindPhysicalPartition(fs_mgr_get_super_partition_name(slot_number_)); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 393 | if (!super_device) { |
| 394 | return; |
| 395 | } |
| 396 | super_device_ = *super_device; |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 397 | builder_ = MetadataBuilder::New(super_device_, slot_number_); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | bool PartitionBuilder::Write() { |
David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 401 | auto metadata = builder_->Export(); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 402 | if (!metadata) { |
| 403 | return false; |
| 404 | } |
David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 405 | return UpdateAllPartitionMetadata(device_, super_device_, *metadata.get()); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 409 | if (args.size() < 3) { |
| 410 | return device->WriteFail("Invalid partition name and size"); |
| 411 | } |
| 412 | |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 413 | if (GetDeviceLockStatus()) { |
| 414 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 415 | } |
| 416 | |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 417 | uint64_t partition_size; |
| 418 | std::string partition_name = args[1]; |
| 419 | if (!android::base::ParseUint(args[2].c_str(), &partition_size)) { |
| 420 | return device->WriteFail("Invalid partition size"); |
| 421 | } |
| 422 | |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 423 | PartitionBuilder builder(device, partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 424 | if (!builder.Valid()) { |
| 425 | return device->WriteFail("Could not open super partition"); |
| 426 | } |
| 427 | // TODO(112433293) Disallow if the name is in the physical table as well. |
| 428 | if (builder->FindPartition(partition_name)) { |
| 429 | return device->WriteFail("Partition already exists"); |
| 430 | } |
| 431 | |
David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 432 | auto partition = builder->AddPartition(partition_name, 0); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 433 | if (!partition) { |
| 434 | return device->WriteFail("Failed to add partition"); |
| 435 | } |
| 436 | if (!builder->ResizePartition(partition, partition_size)) { |
| 437 | builder->RemovePartition(partition_name); |
| 438 | return device->WriteFail("Not enough space for partition"); |
| 439 | } |
| 440 | if (!builder.Write()) { |
| 441 | return device->WriteFail("Failed to write partition table"); |
| 442 | } |
| 443 | return device->WriteOkay("Partition created"); |
| 444 | } |
| 445 | |
| 446 | bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 447 | if (args.size() < 2) { |
| 448 | return device->WriteFail("Invalid partition name and size"); |
| 449 | } |
| 450 | |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 451 | if (GetDeviceLockStatus()) { |
| 452 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 453 | } |
| 454 | |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 455 | std::string partition_name = args[1]; |
| 456 | |
| 457 | PartitionBuilder builder(device, partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 458 | if (!builder.Valid()) { |
| 459 | return device->WriteFail("Could not open super partition"); |
| 460 | } |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 461 | builder->RemovePartition(partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 462 | if (!builder.Write()) { |
| 463 | return device->WriteFail("Failed to write partition table"); |
| 464 | } |
| 465 | return device->WriteOkay("Partition deleted"); |
| 466 | } |
| 467 | |
| 468 | bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 469 | if (args.size() < 3) { |
| 470 | return device->WriteFail("Invalid partition name and size"); |
| 471 | } |
| 472 | |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 473 | if (GetDeviceLockStatus()) { |
| 474 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 475 | } |
| 476 | |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 477 | uint64_t partition_size; |
| 478 | std::string partition_name = args[1]; |
| 479 | if (!android::base::ParseUint(args[2].c_str(), &partition_size)) { |
| 480 | return device->WriteFail("Invalid partition size"); |
| 481 | } |
| 482 | |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 483 | PartitionBuilder builder(device, partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 484 | if (!builder.Valid()) { |
| 485 | return device->WriteFail("Could not open super partition"); |
| 486 | } |
| 487 | |
David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 488 | auto partition = builder->FindPartition(partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 489 | if (!partition) { |
| 490 | return device->WriteFail("Partition does not exist"); |
| 491 | } |
David Anderson | ad970fc | 2019-08-27 14:01:16 -0700 | [diff] [blame] | 492 | |
| 493 | // Remove the updated flag to cancel any snapshots. |
| 494 | uint32_t attrs = partition->attributes(); |
| 495 | partition->set_attributes(attrs & ~LP_PARTITION_ATTR_UPDATED); |
| 496 | |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 497 | if (!builder->ResizePartition(partition, partition_size)) { |
| 498 | return device->WriteFail("Not enough space to resize partition"); |
| 499 | } |
| 500 | if (!builder.Write()) { |
| 501 | return device->WriteFail("Failed to write partition table"); |
| 502 | } |
| 503 | return device->WriteOkay("Partition resized"); |
| 504 | } |
David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 505 | |
David Anderson | ad970fc | 2019-08-27 14:01:16 -0700 | [diff] [blame] | 506 | void CancelPartitionSnapshot(FastbootDevice* device, const std::string& partition_name) { |
| 507 | PartitionBuilder builder(device, partition_name); |
| 508 | if (!builder.Valid()) return; |
| 509 | |
| 510 | auto partition = builder->FindPartition(partition_name); |
| 511 | if (!partition) return; |
| 512 | |
| 513 | // Remove the updated flag to cancel any snapshots. |
| 514 | uint32_t attrs = partition->attributes(); |
| 515 | partition->set_attributes(attrs & ~LP_PARTITION_ATTR_UPDATED); |
| 516 | |
| 517 | builder.Write(); |
| 518 | } |
| 519 | |
| 520 | bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 521 | if (args.size() < 2) { |
| 522 | return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments"); |
| 523 | } |
| 524 | |
| 525 | if (GetDeviceLockStatus()) { |
| 526 | return device->WriteStatus(FastbootResult::FAIL, |
| 527 | "Flashing is not allowed on locked devices"); |
| 528 | } |
| 529 | |
| 530 | const auto& partition_name = args[1]; |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 531 | if (IsProtectedPartitionDuringMerge(device, partition_name)) { |
| 532 | auto message = "Cannot flash " + partition_name + " while a snapshot update is in progress"; |
| 533 | return device->WriteFail(message); |
| 534 | } |
| 535 | |
David Anderson | ad970fc | 2019-08-27 14:01:16 -0700 | [diff] [blame] | 536 | if (LogicalPartitionExists(device, partition_name)) { |
| 537 | CancelPartitionSnapshot(device, partition_name); |
| 538 | } |
| 539 | |
| 540 | int ret = Flash(device, partition_name); |
| 541 | if (ret < 0) { |
| 542 | return device->WriteStatus(FastbootResult::FAIL, strerror(-ret)); |
| 543 | } |
| 544 | return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded"); |
| 545 | } |
| 546 | |
David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 547 | bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 548 | if (args.size() < 2) { |
| 549 | return device->WriteFail("Invalid arguments"); |
| 550 | } |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 551 | |
| 552 | if (GetDeviceLockStatus()) { |
| 553 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 554 | } |
| 555 | |
David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 556 | bool wipe = (args.size() >= 3 && args[2] == "wipe"); |
| 557 | return UpdateSuper(device, args[1], wipe); |
| 558 | } |
David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 559 | |
David Anderson | 3d782d5 | 2019-01-29 13:09:49 -0800 | [diff] [blame] | 560 | bool GsiHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 561 | if (args.size() != 2) { |
| 562 | return device->WriteFail("Invalid arguments"); |
| 563 | } |
David Anderson | 3d782d5 | 2019-01-29 13:09:49 -0800 | [diff] [blame] | 564 | |
| 565 | AutoMountMetadata mount_metadata; |
| 566 | if (!mount_metadata) { |
| 567 | return device->WriteFail("Could not find GSI install"); |
| 568 | } |
| 569 | |
| 570 | if (!android::gsi::IsGsiInstalled()) { |
| 571 | return device->WriteStatus(FastbootResult::FAIL, "No GSI is installed"); |
| 572 | } |
| 573 | |
David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 574 | if (args[1] == "wipe") { |
| 575 | if (!android::gsi::UninstallGsi()) { |
| 576 | return device->WriteStatus(FastbootResult::FAIL, strerror(errno)); |
| 577 | } |
| 578 | } else if (args[1] == "disable") { |
| 579 | if (!android::gsi::DisableGsi()) { |
| 580 | return device->WriteStatus(FastbootResult::FAIL, strerror(errno)); |
| 581 | } |
| 582 | } |
| 583 | return device->WriteStatus(FastbootResult::OKAY, "Success"); |
| 584 | } |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 585 | |
| 586 | bool SnapshotUpdateHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 587 | // Note that we use the HAL rather than mounting /metadata, since we want |
| 588 | // our results to match the bootloader. |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 589 | auto hal = device->boot1_1(); |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 590 | if (!hal) return device->WriteFail("Not supported"); |
| 591 | |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 592 | // If no arguments, return the same thing as a getvar. Note that we get the |
| 593 | // HAL first so we can return "not supported" before we return the less |
| 594 | // specific error message below. |
| 595 | if (args.size() < 2 || args[1].empty()) { |
| 596 | std::string message; |
| 597 | if (!GetSnapshotUpdateStatus(device, {}, &message)) { |
| 598 | return device->WriteFail("Could not determine update status"); |
| 599 | } |
| 600 | device->WriteInfo(message); |
| 601 | return device->WriteOkay(""); |
| 602 | } |
| 603 | |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 604 | MergeStatus status = hal->getSnapshotMergeStatus(); |
| 605 | |
| 606 | if (args.size() != 2) { |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 607 | return device->WriteFail("Invalid arguments"); |
| 608 | } |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 609 | if (args[1] == "cancel") { |
| 610 | switch (status) { |
| 611 | case MergeStatus::SNAPSHOTTED: |
| 612 | case MergeStatus::MERGING: |
| 613 | hal->setSnapshotMergeStatus(MergeStatus::CANCELLED); |
| 614 | break; |
| 615 | default: |
| 616 | break; |
| 617 | } |
| 618 | } else if (args[1] == "merge") { |
| 619 | if (status != MergeStatus::MERGING) { |
| 620 | return device->WriteFail("No snapshot merge is in progress"); |
| 621 | } |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 622 | |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 623 | auto sm = SnapshotManager::NewForFirstStageMount(); |
| 624 | if (!sm) { |
| 625 | return device->WriteFail("Unable to create SnapshotManager"); |
| 626 | } |
| 627 | if (!sm->HandleImminentDataWipe()) { |
| 628 | return device->WriteFail("Unable to finish snapshot merge"); |
| 629 | } |
| 630 | } else { |
| 631 | return device->WriteFail("Invalid parameter to snapshot-update"); |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 632 | } |
| 633 | return device->WriteStatus(FastbootResult::OKAY, "Success"); |
| 634 | } |