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