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