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