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 | |
Yifan Hong | a4eb475 | 2021-02-16 19:37:21 -0800 | [diff] [blame] | 19 | #include <inttypes.h> |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 20 | #include <sys/socket.h> |
| 21 | #include <sys/un.h> |
| 22 | |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 23 | #include <unordered_set> |
| 24 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/parseint.h> |
| 27 | #include <android-base/properties.h> |
| 28 | #include <android-base/stringprintf.h> |
| 29 | #include <android-base/strings.h> |
| 30 | #include <android-base/unique_fd.h> |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 31 | #include <android/hardware/boot/1.1/IBootControl.h> |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 32 | #include <cutils/android_reboot.h> |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 33 | #include <ext4_utils/wipe.h> |
David Anderson | 5cbd2e4 | 2018-09-27 10:53:04 -0700 | [diff] [blame] | 34 | #include <fs_mgr.h> |
David Anderson | 2ffc31b | 2020-03-23 23:43:45 -0700 | [diff] [blame] | 35 | #include <fs_mgr/roots.h> |
David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 36 | #include <libgsi/libgsi.h> |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 37 | #include <liblp/builder.h> |
| 38 | #include <liblp/liblp.h> |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 39 | #include <libsnapshot/snapshot.h> |
Yifan Hong | a4eb475 | 2021-02-16 19:37:21 -0800 | [diff] [blame] | 40 | #include <storage_literals/storage_literals.h> |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 41 | #include <uuid/uuid.h> |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 42 | |
Florian Mayer | 4c3c526 | 2022-09-14 16:02:11 -0700 | [diff] [blame] | 43 | #include <bootloader_message/bootloader_message.h> |
| 44 | |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 45 | #include "BootControlClient.h" |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 46 | #include "constants.h" |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 47 | #include "fastboot_device.h" |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 48 | #include "flashing.h" |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 49 | #include "utility.h" |
| 50 | |
Yifan Hong | a4eb475 | 2021-02-16 19:37:21 -0800 | [diff] [blame] | 51 | #ifdef FB_ENABLE_FETCH |
| 52 | static constexpr bool kEnableFetch = true; |
| 53 | #else |
| 54 | static constexpr bool kEnableFetch = false; |
| 55 | #endif |
| 56 | |
David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 57 | using android::fs_mgr::MetadataBuilder; |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 58 | using android::hal::CommandResult; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 59 | using ::android::hardware::hidl_string; |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 60 | using android::snapshot::SnapshotManager; |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 61 | using MergeStatus = android::hal::BootControlClient::MergeStatus; |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 62 | |
Yifan Hong | a4eb475 | 2021-02-16 19:37:21 -0800 | [diff] [blame] | 63 | using namespace android::storage_literals; |
| 64 | |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 65 | struct VariableHandlers { |
| 66 | // Callback to retrieve the value of a single variable. |
| 67 | std::function<bool(FastbootDevice*, const std::vector<std::string>&, std::string*)> get; |
| 68 | // Callback to retrieve all possible argument combinations, for getvar all. |
| 69 | std::function<std::vector<std::vector<std::string>>(FastbootDevice*)> get_all_args; |
| 70 | }; |
| 71 | |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 72 | static bool IsSnapshotUpdateInProgress(FastbootDevice* device) { |
| 73 | auto hal = device->boot1_1(); |
| 74 | if (!hal) { |
| 75 | return false; |
| 76 | } |
| 77 | auto merge_status = hal->getSnapshotMergeStatus(); |
| 78 | return merge_status == MergeStatus::SNAPSHOTTED || merge_status == MergeStatus::MERGING; |
| 79 | } |
| 80 | |
| 81 | static bool IsProtectedPartitionDuringMerge(FastbootDevice* device, const std::string& name) { |
| 82 | static const std::unordered_set<std::string> ProtectedPartitionsDuringMerge = { |
| 83 | "userdata", "metadata", "misc"}; |
| 84 | if (ProtectedPartitionsDuringMerge.count(name) == 0) { |
| 85 | return false; |
| 86 | } |
| 87 | return IsSnapshotUpdateInProgress(device); |
| 88 | } |
| 89 | |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 90 | static void GetAllVars(FastbootDevice* device, const std::string& name, |
| 91 | const VariableHandlers& handlers) { |
| 92 | if (!handlers.get_all_args) { |
| 93 | std::string message; |
| 94 | if (!handlers.get(device, std::vector<std::string>(), &message)) { |
| 95 | return; |
| 96 | } |
| 97 | device->WriteInfo(android::base::StringPrintf("%s:%s", name.c_str(), message.c_str())); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | auto all_args = handlers.get_all_args(device); |
| 102 | for (const auto& args : all_args) { |
| 103 | std::string message; |
| 104 | if (!handlers.get(device, args, &message)) { |
| 105 | continue; |
| 106 | } |
| 107 | std::string arg_string = android::base::Join(args, ":"); |
| 108 | device->WriteInfo(android::base::StringPrintf("%s:%s:%s", name.c_str(), arg_string.c_str(), |
| 109 | message.c_str())); |
| 110 | } |
| 111 | } |
| 112 | |
David Anderson | ebc8fe1 | 2022-06-17 19:17:43 -0700 | [diff] [blame] | 113 | const std::unordered_map<std::string, VariableHandlers> kVariableMap = { |
| 114 | {FB_VAR_VERSION, {GetVersion, nullptr}}, |
| 115 | {FB_VAR_VERSION_BOOTLOADER, {GetBootloaderVersion, nullptr}}, |
| 116 | {FB_VAR_VERSION_BASEBAND, {GetBasebandVersion, nullptr}}, |
| 117 | {FB_VAR_VERSION_OS, {GetOsVersion, nullptr}}, |
| 118 | {FB_VAR_VERSION_VNDK, {GetVndkVersion, nullptr}}, |
| 119 | {FB_VAR_PRODUCT, {GetProduct, nullptr}}, |
| 120 | {FB_VAR_SERIALNO, {GetSerial, nullptr}}, |
| 121 | {FB_VAR_VARIANT, {GetVariant, nullptr}}, |
| 122 | {FB_VAR_SECURE, {GetSecure, nullptr}}, |
| 123 | {FB_VAR_UNLOCKED, {GetUnlocked, nullptr}}, |
| 124 | {FB_VAR_MAX_DOWNLOAD_SIZE, {GetMaxDownloadSize, nullptr}}, |
| 125 | {FB_VAR_CURRENT_SLOT, {::GetCurrentSlot, nullptr}}, |
| 126 | {FB_VAR_SLOT_COUNT, {GetSlotCount, nullptr}}, |
| 127 | {FB_VAR_HAS_SLOT, {GetHasSlot, GetAllPartitionArgsNoSlot}}, |
| 128 | {FB_VAR_SLOT_SUCCESSFUL, {GetSlotSuccessful, nullptr}}, |
| 129 | {FB_VAR_SLOT_UNBOOTABLE, {GetSlotUnbootable, nullptr}}, |
| 130 | {FB_VAR_PARTITION_SIZE, {GetPartitionSize, GetAllPartitionArgsWithSlot}}, |
| 131 | {FB_VAR_PARTITION_TYPE, {GetPartitionType, GetAllPartitionArgsWithSlot}}, |
| 132 | {FB_VAR_IS_LOGICAL, {GetPartitionIsLogical, GetAllPartitionArgsWithSlot}}, |
| 133 | {FB_VAR_IS_USERSPACE, {GetIsUserspace, nullptr}}, |
Yi-Yo Chiang | 38b68c6 | 2022-11-22 17:32:21 +0800 | [diff] [blame^] | 134 | {FB_VAR_IS_FORCE_DEBUGGABLE, {GetIsForceDebuggable, nullptr}}, |
David Anderson | ebc8fe1 | 2022-06-17 19:17:43 -0700 | [diff] [blame] | 135 | {FB_VAR_OFF_MODE_CHARGE_STATE, {GetOffModeChargeState, nullptr}}, |
| 136 | {FB_VAR_BATTERY_VOLTAGE, {GetBatteryVoltage, nullptr}}, |
| 137 | {FB_VAR_BATTERY_SOC_OK, {GetBatterySoCOk, nullptr}}, |
| 138 | {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}}, |
| 139 | {FB_VAR_SUPER_PARTITION_NAME, {GetSuperPartitionName, nullptr}}, |
| 140 | {FB_VAR_SNAPSHOT_UPDATE_STATUS, {GetSnapshotUpdateStatus, nullptr}}, |
| 141 | {FB_VAR_CPU_ABI, {GetCpuAbi, nullptr}}, |
| 142 | {FB_VAR_SYSTEM_FINGERPRINT, {GetSystemFingerprint, nullptr}}, |
| 143 | {FB_VAR_VENDOR_FINGERPRINT, {GetVendorFingerprint, nullptr}}, |
| 144 | {FB_VAR_DYNAMIC_PARTITION, {GetDynamicPartition, nullptr}}, |
| 145 | {FB_VAR_FIRST_API_LEVEL, {GetFirstApiLevel, nullptr}}, |
| 146 | {FB_VAR_SECURITY_PATCH_LEVEL, {GetSecurityPatchLevel, nullptr}}, |
| 147 | {FB_VAR_TREBLE_ENABLED, {GetTrebleEnabled, nullptr}}, |
| 148 | {FB_VAR_MAX_FETCH_SIZE, {GetMaxFetchSize, nullptr}}, |
| 149 | }; |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 150 | |
David Anderson | ebc8fe1 | 2022-06-17 19:17:43 -0700 | [diff] [blame] | 151 | static bool GetVarAll(FastbootDevice* device) { |
| 152 | for (const auto& [name, handlers] : kVariableMap) { |
| 153 | GetAllVars(device, name, handlers); |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
Florian Mayer | 4c3c526 | 2022-09-14 16:02:11 -0700 | [diff] [blame] | 158 | static void PostWipeData() { |
| 159 | std::string err; |
| 160 | // Reset mte state of device. |
| 161 | if (!WriteMiscMemtagMessage({}, &err)) { |
| 162 | LOG(ERROR) << "Failed to reset MTE state: " << err; |
| 163 | } |
| 164 | } |
| 165 | |
David Anderson | ebc8fe1 | 2022-06-17 19:17:43 -0700 | [diff] [blame] | 166 | const std::unordered_map<std::string, std::function<bool(FastbootDevice*)>> kSpecialVars = { |
| 167 | {"all", GetVarAll}, |
| 168 | {"dmesg", GetDmesg}, |
| 169 | }; |
| 170 | |
| 171 | bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 172 | if (args.size() < 2) { |
| 173 | return device->WriteFail("Missing argument"); |
| 174 | } |
| 175 | |
David Anderson | ebc8fe1 | 2022-06-17 19:17:43 -0700 | [diff] [blame] | 176 | // "all" and "dmesg" are multiline and handled specially. |
| 177 | auto found_special = kSpecialVars.find(args[1]); |
| 178 | if (found_special != kSpecialVars.end()) { |
| 179 | if (!found_special->second(device)) { |
| 180 | return false; |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 181 | } |
| 182 | return device->WriteOkay(""); |
| 183 | } |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 184 | |
| 185 | // args[0] is command name, args[1] is variable. |
| 186 | auto found_variable = kVariableMap.find(args[1]); |
| 187 | if (found_variable == kVariableMap.end()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 188 | return device->WriteFail("Unknown variable"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 189 | } |
| 190 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 191 | std::string message; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 192 | std::vector<std::string> getvar_args(args.begin() + 2, args.end()); |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 193 | if (!found_variable->second.get(device, getvar_args, &message)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 194 | return device->WriteFail(message); |
| 195 | } |
| 196 | return device->WriteOkay(message); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 197 | } |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 198 | |
josephjang | 2906975 | 2020-09-23 16:28:03 +0800 | [diff] [blame] | 199 | bool OemPostWipeData(FastbootDevice* device) { |
| 200 | auto fastboot_hal = device->fastboot_hal(); |
| 201 | if (!fastboot_hal) { |
| 202 | return false; |
| 203 | } |
| 204 | |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 205 | auto status = fastboot_hal->doOemSpecificErase(); |
| 206 | if (status.isOk()) { |
josephjang | 2906975 | 2020-09-23 16:28:03 +0800 | [diff] [blame] | 207 | device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded"); |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 208 | return true; |
josephjang | 2906975 | 2020-09-23 16:28:03 +0800 | [diff] [blame] | 209 | } |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 210 | switch (status.getExceptionCode()) { |
| 211 | case EX_UNSUPPORTED_OPERATION: |
| 212 | return false; |
| 213 | case EX_SERVICE_SPECIFIC: |
| 214 | device->WriteStatus(FastbootResult::FAIL, status.getDescription()); |
| 215 | return false; |
| 216 | default: |
| 217 | LOG(ERROR) << "Erase operation failed" << status.getDescription(); |
| 218 | return false; |
| 219 | } |
josephjang | 2906975 | 2020-09-23 16:28:03 +0800 | [diff] [blame] | 220 | } |
| 221 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 222 | bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 223 | if (args.size() < 2) { |
| 224 | return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments"); |
| 225 | } |
Hridya Valsaraju | d1e6231 | 2018-10-08 09:13:17 -0700 | [diff] [blame] | 226 | |
| 227 | if (GetDeviceLockStatus()) { |
| 228 | return device->WriteStatus(FastbootResult::FAIL, "Erase is not allowed on locked devices"); |
| 229 | } |
| 230 | |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 231 | const auto& partition_name = args[1]; |
| 232 | if (IsProtectedPartitionDuringMerge(device, partition_name)) { |
| 233 | auto message = "Cannot erase " + partition_name + " while a snapshot update is in progress"; |
| 234 | return device->WriteFail(message); |
| 235 | } |
| 236 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 237 | PartitionHandle handle; |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 238 | if (!OpenPartition(device, partition_name, &handle)) { |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 239 | return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist"); |
| 240 | } |
| 241 | if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) { |
josephjang | 2906975 | 2020-09-23 16:28:03 +0800 | [diff] [blame] | 242 | //Perform oem PostWipeData if Android userdata partition has been erased |
| 243 | bool support_oem_postwipedata = false; |
| 244 | if (partition_name == "userdata") { |
Florian Mayer | 4c3c526 | 2022-09-14 16:02:11 -0700 | [diff] [blame] | 245 | PostWipeData(); |
josephjang | 2906975 | 2020-09-23 16:28:03 +0800 | [diff] [blame] | 246 | support_oem_postwipedata = OemPostWipeData(device); |
| 247 | } |
| 248 | |
| 249 | if (!support_oem_postwipedata) { |
| 250 | return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded"); |
| 251 | } else { |
| 252 | //Write device status in OemPostWipeData(), so just return true |
| 253 | return true; |
| 254 | } |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 255 | } |
| 256 | return device->WriteStatus(FastbootResult::FAIL, "Erasing failed"); |
| 257 | } |
| 258 | |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 259 | bool OemCmdHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 260 | auto fastboot_hal = device->fastboot_hal(); |
| 261 | if (!fastboot_hal) { |
| 262 | return device->WriteStatus(FastbootResult::FAIL, "Unable to open fastboot HAL"); |
| 263 | } |
| 264 | |
josephjang | ad90b45 | 2020-09-16 16:27:42 +0800 | [diff] [blame] | 265 | //Disable "oem postwipedata userdata" to prevent user wipe oem userdata only. |
| 266 | if (args[0] == "oem postwipedata userdata") { |
| 267 | return device->WriteStatus(FastbootResult::FAIL, "Unable to do oem postwipedata userdata"); |
| 268 | } |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 269 | std::string message; |
| 270 | auto status = fastboot_hal->doOemCommand(args[0], &message); |
| 271 | if (!status.isOk()) { |
| 272 | LOG(ERROR) << "Unable to do OEM command " << args[0].c_str() << status.getDescription(); |
| 273 | return device->WriteStatus(FastbootResult::FAIL, |
| 274 | "Unable to do OEM command " + status.getDescription()); |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 277 | device->WriteInfo(message); |
| 278 | return device->WriteStatus(FastbootResult::OKAY, message); |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 281 | bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 282 | if (args.size() < 2) { |
| 283 | return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified"); |
| 284 | } |
Hridya Valsaraju | d1e6231 | 2018-10-08 09:13:17 -0700 | [diff] [blame] | 285 | |
| 286 | if (GetDeviceLockStatus()) { |
| 287 | return device->WriteStatus(FastbootResult::FAIL, |
| 288 | "Download is not allowed on locked devices"); |
| 289 | } |
| 290 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 291 | // arg[0] is the command name, arg[1] contains size of data to be downloaded |
Keith Mok | 3724bbc | 2021-12-30 20:08:04 +0000 | [diff] [blame] | 292 | // which should always be 8 bytes |
| 293 | if (args[1].length() != 8) { |
| 294 | return device->WriteStatus(FastbootResult::FAIL, |
| 295 | "Invalid size (length of size != 8)"); |
| 296 | } |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 297 | unsigned int size; |
Hridya Valsaraju | aae84e8 | 2018-10-08 13:10:25 -0700 | [diff] [blame] | 298 | if (!android::base::ParseUint("0x" + args[1], &size, kMaxDownloadSizeDefault)) { |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 299 | return device->WriteStatus(FastbootResult::FAIL, "Invalid size"); |
| 300 | } |
Keith Mok | 3724bbc | 2021-12-30 20:08:04 +0000 | [diff] [blame] | 301 | if (size == 0) { |
| 302 | return device->WriteStatus(FastbootResult::FAIL, "Invalid size (0)"); |
| 303 | } |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 304 | device->download_data().resize(size); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 305 | if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) { |
| 306 | return false; |
| 307 | } |
| 308 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 309 | if (device->HandleData(true, &device->download_data())) { |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 310 | return device->WriteStatus(FastbootResult::OKAY, ""); |
| 311 | } |
| 312 | |
| 313 | PLOG(ERROR) << "Couldn't download data"; |
| 314 | return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data"); |
| 315 | } |
| 316 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 317 | bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 318 | if (args.size() < 2) { |
| 319 | return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument"); |
| 320 | } |
| 321 | |
Hridya Valsaraju | d1e6231 | 2018-10-08 09:13:17 -0700 | [diff] [blame] | 322 | if (GetDeviceLockStatus()) { |
| 323 | return device->WriteStatus(FastbootResult::FAIL, |
| 324 | "set_active command is not allowed on locked devices"); |
| 325 | } |
| 326 | |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 327 | int32_t slot = 0; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 328 | if (!GetSlotNumber(args[1], &slot)) { |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 329 | // Slot suffix needs to be between 'a' and 'z'. |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 330 | return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix"); |
| 331 | } |
| 332 | |
| 333 | // Non-A/B devices will not have a boot control HAL. |
| 334 | auto boot_control_hal = device->boot_control_hal(); |
| 335 | if (!boot_control_hal) { |
| 336 | return device->WriteStatus(FastbootResult::FAIL, |
| 337 | "Cannot set slot: boot control HAL absent"); |
| 338 | } |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 339 | if (slot >= boot_control_hal->GetNumSlots()) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 340 | return device->WriteStatus(FastbootResult::FAIL, "Slot out of range"); |
| 341 | } |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 342 | |
| 343 | // If the slot is not changing, do nothing. |
Hridya Valsaraju | 45719a8 | 2020-03-02 13:03:47 -0800 | [diff] [blame] | 344 | if (args[1] == device->GetCurrentSlot()) { |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 345 | return device->WriteOkay(""); |
| 346 | } |
| 347 | |
| 348 | // Check how to handle the current snapshot state. |
| 349 | if (auto hal11 = device->boot1_1()) { |
| 350 | auto merge_status = hal11->getSnapshotMergeStatus(); |
| 351 | if (merge_status == MergeStatus::MERGING) { |
| 352 | return device->WriteFail("Cannot change slots while a snapshot update is in progress"); |
| 353 | } |
| 354 | // Note: we allow the slot change if the state is SNAPSHOTTED. First- |
| 355 | // stage init does not have access to the HAL, and uses the slot number |
| 356 | // and /metadata OTA state to determine whether a slot change occurred. |
| 357 | // Booting into the old slot would erase the OTA, and switching A->B->A |
| 358 | // would simply resume it if no boots occur in between. Re-flashing |
| 359 | // partitions implicitly cancels the OTA, so leaving the state as-is is |
| 360 | // safe. |
| 361 | if (merge_status == MergeStatus::SNAPSHOTTED) { |
| 362 | device->WriteInfo( |
| 363 | "Changing the active slot with a snapshot applied may cancel the" |
| 364 | " update."); |
| 365 | } |
| 366 | } |
| 367 | |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 368 | CommandResult ret = boot_control_hal->SetActiveBootSlot(slot); |
| 369 | if (ret.success) { |
Hridya Valsaraju | 20bdf89 | 2018-10-10 11:02:19 -0700 | [diff] [blame] | 370 | // Save as slot suffix to match the suffix format as returned from |
| 371 | // the boot control HAL. |
| 372 | auto current_slot = "_" + args[1]; |
| 373 | device->set_active_slot(current_slot); |
| 374 | return device->WriteStatus(FastbootResult::OKAY, ""); |
| 375 | } |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 376 | return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot"); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 380 | auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down"); |
| 381 | android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot"); |
| 382 | device->CloseDevice(); |
| 383 | TEMP_FAILURE_RETRY(pause()); |
| 384 | return result; |
| 385 | } |
| 386 | |
| 387 | bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 388 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting"); |
| 389 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot"); |
| 390 | device->CloseDevice(); |
| 391 | TEMP_FAILURE_RETRY(pause()); |
| 392 | return result; |
| 393 | } |
| 394 | |
| 395 | bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 396 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader"); |
| 397 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader"); |
| 398 | device->CloseDevice(); |
| 399 | TEMP_FAILURE_RETRY(pause()); |
| 400 | return result; |
| 401 | } |
| 402 | |
| 403 | bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 404 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot"); |
| 405 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot"); |
| 406 | device->CloseDevice(); |
| 407 | TEMP_FAILURE_RETRY(pause()); |
| 408 | return result; |
| 409 | } |
| 410 | |
| 411 | static bool EnterRecovery() { |
| 412 | const char msg_switch_to_recovery = 'r'; |
| 413 | |
| 414 | android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0)); |
| 415 | if (sock < 0) { |
| 416 | PLOG(ERROR) << "Couldn't create sock"; |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | struct sockaddr_un addr = {.sun_family = AF_UNIX}; |
| 421 | strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1); |
Yifan Hong | 07e947f | 2021-03-22 19:21:34 -0700 | [diff] [blame] | 422 | if (connect(sock.get(), (struct sockaddr*)&addr, sizeof(addr)) < 0) { |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 423 | PLOG(ERROR) << "Couldn't connect to recovery"; |
| 424 | return false; |
| 425 | } |
| 426 | // Switch to recovery will not update the boot reason since it does not |
| 427 | // require a reboot. |
Yifan Hong | 07e947f | 2021-03-22 19:21:34 -0700 | [diff] [blame] | 428 | auto ret = write(sock.get(), &msg_switch_to_recovery, sizeof(msg_switch_to_recovery)); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 429 | if (ret != sizeof(msg_switch_to_recovery)) { |
| 430 | PLOG(ERROR) << "Couldn't write message to switch to recovery"; |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 438 | auto status = true; |
| 439 | if (EnterRecovery()) { |
| 440 | status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery"); |
| 441 | } else { |
| 442 | status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery"); |
| 443 | } |
| 444 | device->CloseDevice(); |
| 445 | TEMP_FAILURE_RETRY(pause()); |
| 446 | return status; |
| 447 | } |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 448 | |
| 449 | // Helper class for opening a handle to a MetadataBuilder and writing the new |
| 450 | // partition table to the same place it was read. |
| 451 | class PartitionBuilder { |
| 452 | public: |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 453 | explicit PartitionBuilder(FastbootDevice* device, const std::string& partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 454 | |
| 455 | bool Write(); |
| 456 | bool Valid() const { return !!builder_; } |
| 457 | MetadataBuilder* operator->() const { return builder_.get(); } |
| 458 | |
| 459 | private: |
David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 460 | FastbootDevice* device_; |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 461 | std::string super_device_; |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 462 | uint32_t slot_number_; |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 463 | std::unique_ptr<MetadataBuilder> builder_; |
| 464 | }; |
| 465 | |
David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 466 | PartitionBuilder::PartitionBuilder(FastbootDevice* device, const std::string& partition_name) |
| 467 | : device_(device) { |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 468 | std::string slot_suffix = GetSuperSlotSuffix(device, partition_name); |
David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 469 | slot_number_ = android::fs_mgr::SlotNumberForSlotSuffix(slot_suffix); |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 470 | auto super_device = FindPhysicalPartition(fs_mgr_get_super_partition_name(slot_number_)); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 471 | if (!super_device) { |
| 472 | return; |
| 473 | } |
| 474 | super_device_ = *super_device; |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 475 | builder_ = MetadataBuilder::New(super_device_, slot_number_); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | bool PartitionBuilder::Write() { |
David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 479 | auto metadata = builder_->Export(); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 480 | if (!metadata) { |
| 481 | return false; |
| 482 | } |
David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 483 | return UpdateAllPartitionMetadata(device_, super_device_, *metadata.get()); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 487 | if (args.size() < 3) { |
| 488 | return device->WriteFail("Invalid partition name and size"); |
| 489 | } |
| 490 | |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 491 | if (GetDeviceLockStatus()) { |
| 492 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 493 | } |
| 494 | |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 495 | uint64_t partition_size; |
| 496 | std::string partition_name = args[1]; |
| 497 | if (!android::base::ParseUint(args[2].c_str(), &partition_size)) { |
| 498 | return device->WriteFail("Invalid partition size"); |
| 499 | } |
| 500 | |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 501 | PartitionBuilder builder(device, partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 502 | if (!builder.Valid()) { |
| 503 | return device->WriteFail("Could not open super partition"); |
| 504 | } |
| 505 | // TODO(112433293) Disallow if the name is in the physical table as well. |
| 506 | if (builder->FindPartition(partition_name)) { |
| 507 | return device->WriteFail("Partition already exists"); |
| 508 | } |
| 509 | |
David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 510 | auto partition = builder->AddPartition(partition_name, 0); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 511 | if (!partition) { |
| 512 | return device->WriteFail("Failed to add partition"); |
| 513 | } |
| 514 | if (!builder->ResizePartition(partition, partition_size)) { |
| 515 | builder->RemovePartition(partition_name); |
| 516 | return device->WriteFail("Not enough space for partition"); |
| 517 | } |
| 518 | if (!builder.Write()) { |
| 519 | return device->WriteFail("Failed to write partition table"); |
| 520 | } |
| 521 | return device->WriteOkay("Partition created"); |
| 522 | } |
| 523 | |
| 524 | bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 525 | if (args.size() < 2) { |
| 526 | return device->WriteFail("Invalid partition name and size"); |
| 527 | } |
| 528 | |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 529 | if (GetDeviceLockStatus()) { |
| 530 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 531 | } |
| 532 | |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 533 | std::string partition_name = args[1]; |
| 534 | |
| 535 | PartitionBuilder builder(device, partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 536 | if (!builder.Valid()) { |
| 537 | return device->WriteFail("Could not open super partition"); |
| 538 | } |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 539 | builder->RemovePartition(partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 540 | if (!builder.Write()) { |
| 541 | return device->WriteFail("Failed to write partition table"); |
| 542 | } |
| 543 | return device->WriteOkay("Partition deleted"); |
| 544 | } |
| 545 | |
| 546 | bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 547 | if (args.size() < 3) { |
| 548 | return device->WriteFail("Invalid partition name and size"); |
| 549 | } |
| 550 | |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 551 | if (GetDeviceLockStatus()) { |
| 552 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 553 | } |
| 554 | |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 555 | uint64_t partition_size; |
| 556 | std::string partition_name = args[1]; |
| 557 | if (!android::base::ParseUint(args[2].c_str(), &partition_size)) { |
| 558 | return device->WriteFail("Invalid partition size"); |
| 559 | } |
| 560 | |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 561 | PartitionBuilder builder(device, partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 562 | if (!builder.Valid()) { |
| 563 | return device->WriteFail("Could not open super partition"); |
| 564 | } |
| 565 | |
David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 566 | auto partition = builder->FindPartition(partition_name); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 567 | if (!partition) { |
| 568 | return device->WriteFail("Partition does not exist"); |
| 569 | } |
David Anderson | ad970fc | 2019-08-27 14:01:16 -0700 | [diff] [blame] | 570 | |
| 571 | // Remove the updated flag to cancel any snapshots. |
| 572 | uint32_t attrs = partition->attributes(); |
| 573 | partition->set_attributes(attrs & ~LP_PARTITION_ATTR_UPDATED); |
| 574 | |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 575 | if (!builder->ResizePartition(partition, partition_size)) { |
| 576 | return device->WriteFail("Not enough space to resize partition"); |
| 577 | } |
| 578 | if (!builder.Write()) { |
| 579 | return device->WriteFail("Failed to write partition table"); |
| 580 | } |
| 581 | return device->WriteOkay("Partition resized"); |
| 582 | } |
David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 583 | |
David Anderson | ad970fc | 2019-08-27 14:01:16 -0700 | [diff] [blame] | 584 | void CancelPartitionSnapshot(FastbootDevice* device, const std::string& partition_name) { |
| 585 | PartitionBuilder builder(device, partition_name); |
| 586 | if (!builder.Valid()) return; |
| 587 | |
| 588 | auto partition = builder->FindPartition(partition_name); |
| 589 | if (!partition) return; |
| 590 | |
| 591 | // Remove the updated flag to cancel any snapshots. |
| 592 | uint32_t attrs = partition->attributes(); |
| 593 | partition->set_attributes(attrs & ~LP_PARTITION_ATTR_UPDATED); |
| 594 | |
| 595 | builder.Write(); |
| 596 | } |
| 597 | |
| 598 | bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 599 | if (args.size() < 2) { |
| 600 | return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments"); |
| 601 | } |
| 602 | |
| 603 | if (GetDeviceLockStatus()) { |
| 604 | return device->WriteStatus(FastbootResult::FAIL, |
| 605 | "Flashing is not allowed on locked devices"); |
| 606 | } |
| 607 | |
| 608 | const auto& partition_name = args[1]; |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 609 | if (IsProtectedPartitionDuringMerge(device, partition_name)) { |
| 610 | auto message = "Cannot flash " + partition_name + " while a snapshot update is in progress"; |
| 611 | return device->WriteFail(message); |
| 612 | } |
| 613 | |
David Anderson | ad970fc | 2019-08-27 14:01:16 -0700 | [diff] [blame] | 614 | if (LogicalPartitionExists(device, partition_name)) { |
| 615 | CancelPartitionSnapshot(device, partition_name); |
| 616 | } |
| 617 | |
| 618 | int ret = Flash(device, partition_name); |
| 619 | if (ret < 0) { |
| 620 | return device->WriteStatus(FastbootResult::FAIL, strerror(-ret)); |
| 621 | } |
Florian Mayer | 4c3c526 | 2022-09-14 16:02:11 -0700 | [diff] [blame] | 622 | if (partition_name == "userdata") { |
| 623 | PostWipeData(); |
| 624 | } |
| 625 | |
David Anderson | ad970fc | 2019-08-27 14:01:16 -0700 | [diff] [blame] | 626 | return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded"); |
| 627 | } |
| 628 | |
David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 629 | bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 630 | if (args.size() < 2) { |
| 631 | return device->WriteFail("Invalid arguments"); |
| 632 | } |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 633 | |
| 634 | if (GetDeviceLockStatus()) { |
| 635 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); |
| 636 | } |
| 637 | |
David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 638 | bool wipe = (args.size() >= 3 && args[2] == "wipe"); |
| 639 | return UpdateSuper(device, args[1], wipe); |
| 640 | } |
David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 641 | |
David Anderson | 3d782d5 | 2019-01-29 13:09:49 -0800 | [diff] [blame] | 642 | bool GsiHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 643 | if (args.size() != 2) { |
| 644 | return device->WriteFail("Invalid arguments"); |
| 645 | } |
David Anderson | 3d782d5 | 2019-01-29 13:09:49 -0800 | [diff] [blame] | 646 | |
| 647 | AutoMountMetadata mount_metadata; |
| 648 | if (!mount_metadata) { |
| 649 | return device->WriteFail("Could not find GSI install"); |
| 650 | } |
| 651 | |
| 652 | if (!android::gsi::IsGsiInstalled()) { |
| 653 | return device->WriteStatus(FastbootResult::FAIL, "No GSI is installed"); |
| 654 | } |
| 655 | |
David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 656 | if (args[1] == "wipe") { |
| 657 | if (!android::gsi::UninstallGsi()) { |
| 658 | return device->WriteStatus(FastbootResult::FAIL, strerror(errno)); |
| 659 | } |
| 660 | } else if (args[1] == "disable") { |
| 661 | if (!android::gsi::DisableGsi()) { |
| 662 | return device->WriteStatus(FastbootResult::FAIL, strerror(errno)); |
| 663 | } |
| 664 | } |
| 665 | return device->WriteStatus(FastbootResult::OKAY, "Success"); |
| 666 | } |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 667 | |
| 668 | bool SnapshotUpdateHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 669 | // Note that we use the HAL rather than mounting /metadata, since we want |
| 670 | // our results to match the bootloader. |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 671 | auto hal = device->boot1_1(); |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 672 | if (!hal) return device->WriteFail("Not supported"); |
| 673 | |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 674 | // If no arguments, return the same thing as a getvar. Note that we get the |
| 675 | // HAL first so we can return "not supported" before we return the less |
| 676 | // specific error message below. |
| 677 | if (args.size() < 2 || args[1].empty()) { |
| 678 | std::string message; |
| 679 | if (!GetSnapshotUpdateStatus(device, {}, &message)) { |
| 680 | return device->WriteFail("Could not determine update status"); |
| 681 | } |
| 682 | device->WriteInfo(message); |
| 683 | return device->WriteOkay(""); |
| 684 | } |
| 685 | |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 686 | MergeStatus status = hal->getSnapshotMergeStatus(); |
| 687 | |
| 688 | if (args.size() != 2) { |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 689 | return device->WriteFail("Invalid arguments"); |
| 690 | } |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 691 | if (args[1] == "cancel") { |
| 692 | switch (status) { |
| 693 | case MergeStatus::SNAPSHOTTED: |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 694 | case MergeStatus::MERGING: { |
| 695 | const auto ret = hal->SetSnapshotMergeStatus(MergeStatus::CANCELLED); |
| 696 | if (!ret.success) { |
| 697 | device->WriteFail("Failed to SetSnapshotMergeStatus(MergeStatus::CANCELLED) " + |
| 698 | ret.errMsg); |
| 699 | } |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 700 | break; |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 701 | } |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 702 | default: |
| 703 | break; |
| 704 | } |
| 705 | } else if (args[1] == "merge") { |
| 706 | if (status != MergeStatus::MERGING) { |
| 707 | return device->WriteFail("No snapshot merge is in progress"); |
| 708 | } |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 709 | |
David Anderson | 565577f | 2021-02-04 20:14:18 -0800 | [diff] [blame] | 710 | auto sm = SnapshotManager::New(); |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 711 | if (!sm) { |
| 712 | return device->WriteFail("Unable to create SnapshotManager"); |
| 713 | } |
David Anderson | 5a0177d | 2020-04-30 18:53:23 -0700 | [diff] [blame] | 714 | if (!sm->FinishMergeInRecovery()) { |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 715 | return device->WriteFail("Unable to finish snapshot merge"); |
| 716 | } |
| 717 | } else { |
| 718 | return device->WriteFail("Invalid parameter to snapshot-update"); |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 719 | } |
| 720 | return device->WriteStatus(FastbootResult::OKAY, "Success"); |
| 721 | } |
Yifan Hong | a4eb475 | 2021-02-16 19:37:21 -0800 | [diff] [blame] | 722 | |
| 723 | namespace { |
| 724 | // Helper of FetchHandler. |
| 725 | class PartitionFetcher { |
| 726 | public: |
| 727 | static bool Fetch(FastbootDevice* device, const std::vector<std::string>& args) { |
| 728 | if constexpr (!kEnableFetch) { |
| 729 | return device->WriteFail("Fetch is not allowed on user build"); |
| 730 | } |
| 731 | |
| 732 | if (GetDeviceLockStatus()) { |
| 733 | return device->WriteFail("Fetch is not allowed on locked devices"); |
| 734 | } |
| 735 | |
| 736 | PartitionFetcher fetcher(device, args); |
| 737 | if (fetcher.Open()) { |
| 738 | fetcher.Fetch(); |
| 739 | } |
| 740 | CHECK(fetcher.ret_.has_value()); |
| 741 | return *fetcher.ret_; |
| 742 | } |
| 743 | |
| 744 | private: |
| 745 | PartitionFetcher(FastbootDevice* device, const std::vector<std::string>& args) |
| 746 | : device_(device), args_(&args) {} |
| 747 | // Return whether the partition is successfully opened. |
| 748 | // If successfully opened, ret_ is left untouched. Otherwise, ret_ is set to the value |
| 749 | // that FetchHandler should return. |
| 750 | bool Open() { |
Yifan Hong | bcd2770 | 2021-02-16 19:37:32 -0800 | [diff] [blame] | 751 | if (args_->size() < 2) { |
| 752 | ret_ = device_->WriteFail("Missing partition arg"); |
Yifan Hong | a4eb475 | 2021-02-16 19:37:21 -0800 | [diff] [blame] | 753 | return false; |
| 754 | } |
| 755 | |
Yifan Hong | bcd2770 | 2021-02-16 19:37:32 -0800 | [diff] [blame] | 756 | partition_name_ = args_->at(1); |
| 757 | if (std::find(kAllowedPartitions.begin(), kAllowedPartitions.end(), partition_name_) == |
| 758 | kAllowedPartitions.end()) { |
| 759 | ret_ = device_->WriteFail("Fetch is only allowed on [" + |
| 760 | android::base::Join(kAllowedPartitions, ", ") + "]"); |
| 761 | return false; |
| 762 | } |
| 763 | |
Konstantin Vyshetsky | 81cc119 | 2021-11-04 10:27:06 -0700 | [diff] [blame] | 764 | if (!OpenPartition(device_, partition_name_, &handle_, O_RDONLY)) { |
Yifan Hong | a4eb475 | 2021-02-16 19:37:21 -0800 | [diff] [blame] | 765 | ret_ = device_->WriteFail( |
| 766 | android::base::StringPrintf("Cannot open %s", partition_name_.c_str())); |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | partition_size_ = get_block_device_size(handle_.fd()); |
| 771 | if (partition_size_ == 0) { |
| 772 | ret_ = device_->WriteOkay(android::base::StringPrintf("Partition %s has size 0", |
| 773 | partition_name_.c_str())); |
| 774 | return false; |
| 775 | } |
| 776 | |
| 777 | start_offset_ = 0; |
| 778 | if (args_->size() >= 3) { |
| 779 | if (!android::base::ParseUint(args_->at(2), &start_offset_)) { |
| 780 | ret_ = device_->WriteFail("Invalid offset, must be integer"); |
| 781 | return false; |
| 782 | } |
| 783 | if (start_offset_ > std::numeric_limits<off64_t>::max()) { |
| 784 | ret_ = device_->WriteFail( |
| 785 | android::base::StringPrintf("Offset overflows: %" PRIx64, start_offset_)); |
| 786 | return false; |
| 787 | } |
| 788 | } |
| 789 | if (start_offset_ > partition_size_) { |
| 790 | ret_ = device_->WriteFail(android::base::StringPrintf( |
| 791 | "Invalid offset 0x%" PRIx64 ", partition %s has size 0x%" PRIx64, start_offset_, |
| 792 | partition_name_.c_str(), partition_size_)); |
| 793 | return false; |
| 794 | } |
| 795 | uint64_t maximum_total_size_to_read = partition_size_ - start_offset_; |
| 796 | total_size_to_read_ = maximum_total_size_to_read; |
| 797 | if (args_->size() >= 4) { |
| 798 | if (!android::base::ParseUint(args_->at(3), &total_size_to_read_)) { |
| 799 | ret_ = device_->WriteStatus(FastbootResult::FAIL, "Invalid size, must be integer"); |
| 800 | return false; |
| 801 | } |
| 802 | } |
| 803 | if (total_size_to_read_ == 0) { |
| 804 | ret_ = device_->WriteOkay("Read 0 bytes"); |
| 805 | return false; |
| 806 | } |
| 807 | if (total_size_to_read_ > maximum_total_size_to_read) { |
| 808 | ret_ = device_->WriteFail(android::base::StringPrintf( |
| 809 | "Invalid size to read 0x%" PRIx64 ", partition %s has size 0x%" PRIx64 |
| 810 | " and fetching from offset 0x%" PRIx64, |
| 811 | total_size_to_read_, partition_name_.c_str(), partition_size_, start_offset_)); |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | if (total_size_to_read_ > kMaxFetchSizeDefault) { |
| 816 | ret_ = device_->WriteFail(android::base::StringPrintf( |
| 817 | "Cannot fetch 0x%" PRIx64 |
| 818 | " bytes because it exceeds maximum transport size 0x%x", |
| 819 | partition_size_, kMaxDownloadSizeDefault)); |
| 820 | return false; |
| 821 | } |
| 822 | |
| 823 | return true; |
| 824 | } |
| 825 | |
| 826 | // Assume Open() returns true. |
| 827 | // After execution, ret_ is set to the value that FetchHandler should return. |
| 828 | void Fetch() { |
| 829 | CHECK(start_offset_ <= std::numeric_limits<off64_t>::max()); |
| 830 | if (lseek64(handle_.fd(), start_offset_, SEEK_SET) != static_cast<off64_t>(start_offset_)) { |
| 831 | ret_ = device_->WriteFail(android::base::StringPrintf( |
| 832 | "On partition %s, unable to lseek(0x%" PRIx64 ": %s", partition_name_.c_str(), |
| 833 | start_offset_, strerror(errno))); |
| 834 | return; |
| 835 | } |
| 836 | |
| 837 | if (!device_->WriteStatus(FastbootResult::DATA, |
| 838 | android::base::StringPrintf( |
| 839 | "%08x", static_cast<uint32_t>(total_size_to_read_)))) { |
| 840 | ret_ = false; |
| 841 | return; |
| 842 | } |
| 843 | uint64_t end_offset = start_offset_ + total_size_to_read_; |
| 844 | std::vector<char> buf(1_MiB); |
| 845 | uint64_t current_offset = start_offset_; |
| 846 | while (current_offset < end_offset) { |
| 847 | // On any error, exit. We can't return a status message to the driver because |
| 848 | // we are in the middle of writing data, so just let the driver guess what's wrong |
| 849 | // by ending the data stream prematurely. |
| 850 | uint64_t remaining = end_offset - current_offset; |
| 851 | uint64_t chunk_size = std::min<uint64_t>(buf.size(), remaining); |
| 852 | if (!android::base::ReadFully(handle_.fd(), buf.data(), chunk_size)) { |
| 853 | PLOG(ERROR) << std::hex << "Unable to read 0x" << chunk_size << " bytes from " |
| 854 | << partition_name_ << " @ offset 0x" << current_offset; |
| 855 | ret_ = false; |
| 856 | return; |
| 857 | } |
| 858 | if (!device_->HandleData(false /* is read */, buf.data(), chunk_size)) { |
| 859 | PLOG(ERROR) << std::hex << "Unable to send 0x" << chunk_size << " bytes of " |
| 860 | << partition_name_ << " @ offset 0x" << current_offset; |
| 861 | ret_ = false; |
| 862 | return; |
| 863 | } |
| 864 | current_offset += chunk_size; |
| 865 | } |
| 866 | |
| 867 | ret_ = device_->WriteOkay(android::base::StringPrintf( |
| 868 | "Fetched %s (offset=0x%" PRIx64 ", size=0x%" PRIx64, partition_name_.c_str(), |
| 869 | start_offset_, total_size_to_read_)); |
| 870 | } |
| 871 | |
Yifan Hong | bcd2770 | 2021-02-16 19:37:32 -0800 | [diff] [blame] | 872 | static constexpr std::array<const char*, 3> kAllowedPartitions{ |
| 873 | "vendor_boot", |
| 874 | "vendor_boot_a", |
| 875 | "vendor_boot_b", |
| 876 | }; |
| 877 | |
Yifan Hong | a4eb475 | 2021-02-16 19:37:21 -0800 | [diff] [blame] | 878 | FastbootDevice* device_; |
| 879 | const std::vector<std::string>* args_ = nullptr; |
| 880 | std::string partition_name_; |
| 881 | PartitionHandle handle_; |
| 882 | uint64_t partition_size_ = 0; |
| 883 | uint64_t start_offset_ = 0; |
| 884 | uint64_t total_size_to_read_ = 0; |
| 885 | |
| 886 | // What FetchHandler should return. |
| 887 | std::optional<bool> ret_ = std::nullopt; |
| 888 | }; |
| 889 | } // namespace |
| 890 | |
| 891 | bool FetchHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 892 | return PartitionFetcher::Fetch(device, args); |
| 893 | } |