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