| 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 |  | 
|  | 22 | #include <android-base/logging.h> | 
|  | 23 | #include <android-base/parseint.h> | 
|  | 24 | #include <android-base/properties.h> | 
|  | 25 | #include <android-base/stringprintf.h> | 
|  | 26 | #include <android-base/strings.h> | 
|  | 27 | #include <android-base/unique_fd.h> | 
|  | 28 | #include <cutils/android_reboot.h> | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 29 | #include <ext4_utils/wipe.h> | 
| David Anderson | 5cbd2e4 | 2018-09-27 10:53:04 -0700 | [diff] [blame] | 30 | #include <fs_mgr.h> | 
| David Anderson | 3d782d5 | 2019-01-29 13:09:49 -0800 | [diff] [blame] | 31 | #include <fs_mgr/roots.h> | 
| David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 32 | #include <libgsi/libgsi.h> | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 33 | #include <liblp/builder.h> | 
|  | 34 | #include <liblp/liblp.h> | 
|  | 35 | #include <uuid/uuid.h> | 
| Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 36 |  | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 37 | #include "constants.h" | 
| Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 38 | #include "fastboot_device.h" | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 39 | #include "flashing.h" | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 40 | #include "utility.h" | 
|  | 41 |  | 
| David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 42 | using android::fs_mgr::MetadataBuilder; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 43 | using ::android::hardware::hidl_string; | 
|  | 44 | using ::android::hardware::boot::V1_0::BoolResult; | 
|  | 45 | using ::android::hardware::boot::V1_0::CommandResult; | 
|  | 46 | using ::android::hardware::boot::V1_0::Slot; | 
| Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 47 | using ::android::hardware::fastboot::V1_0::Result; | 
|  | 48 | using ::android::hardware::fastboot::V1_0::Status; | 
|  | 49 |  | 
| David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 50 | struct VariableHandlers { | 
|  | 51 | // Callback to retrieve the value of a single variable. | 
|  | 52 | std::function<bool(FastbootDevice*, const std::vector<std::string>&, std::string*)> get; | 
|  | 53 | // Callback to retrieve all possible argument combinations, for getvar all. | 
|  | 54 | std::function<std::vector<std::vector<std::string>>(FastbootDevice*)> get_all_args; | 
|  | 55 | }; | 
|  | 56 |  | 
|  | 57 | static void GetAllVars(FastbootDevice* device, const std::string& name, | 
|  | 58 | const VariableHandlers& handlers) { | 
|  | 59 | if (!handlers.get_all_args) { | 
|  | 60 | std::string message; | 
|  | 61 | if (!handlers.get(device, std::vector<std::string>(), &message)) { | 
|  | 62 | return; | 
|  | 63 | } | 
|  | 64 | device->WriteInfo(android::base::StringPrintf("%s:%s", name.c_str(), message.c_str())); | 
|  | 65 | return; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | auto all_args = handlers.get_all_args(device); | 
|  | 69 | for (const auto& args : all_args) { | 
|  | 70 | std::string message; | 
|  | 71 | if (!handlers.get(device, args, &message)) { | 
|  | 72 | continue; | 
|  | 73 | } | 
|  | 74 | std::string arg_string = android::base::Join(args, ":"); | 
|  | 75 | device->WriteInfo(android::base::StringPrintf("%s:%s:%s", name.c_str(), arg_string.c_str(), | 
|  | 76 | message.c_str())); | 
|  | 77 | } | 
|  | 78 | } | 
|  | 79 |  | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 80 | bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) { | 
| David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 81 | const std::unordered_map<std::string, VariableHandlers> kVariableMap = { | 
|  | 82 | {FB_VAR_VERSION, {GetVersion, nullptr}}, | 
|  | 83 | {FB_VAR_VERSION_BOOTLOADER, {GetBootloaderVersion, nullptr}}, | 
|  | 84 | {FB_VAR_VERSION_BASEBAND, {GetBasebandVersion, nullptr}}, | 
|  | 85 | {FB_VAR_PRODUCT, {GetProduct, nullptr}}, | 
|  | 86 | {FB_VAR_SERIALNO, {GetSerial, nullptr}}, | 
| Hridya Valsaraju | 4af8090 | 2018-09-26 13:08:16 -0700 | [diff] [blame] | 87 | {FB_VAR_VARIANT, {GetVariant, nullptr}}, | 
| David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 88 | {FB_VAR_SECURE, {GetSecure, nullptr}}, | 
|  | 89 | {FB_VAR_UNLOCKED, {GetUnlocked, nullptr}}, | 
|  | 90 | {FB_VAR_MAX_DOWNLOAD_SIZE, {GetMaxDownloadSize, nullptr}}, | 
|  | 91 | {FB_VAR_CURRENT_SLOT, {::GetCurrentSlot, nullptr}}, | 
|  | 92 | {FB_VAR_SLOT_COUNT, {GetSlotCount, nullptr}}, | 
|  | 93 | {FB_VAR_HAS_SLOT, {GetHasSlot, GetAllPartitionArgsNoSlot}}, | 
|  | 94 | {FB_VAR_SLOT_SUCCESSFUL, {GetSlotSuccessful, nullptr}}, | 
|  | 95 | {FB_VAR_SLOT_UNBOOTABLE, {GetSlotUnbootable, nullptr}}, | 
| David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 96 | {FB_VAR_PARTITION_SIZE, {GetPartitionSize, GetAllPartitionArgsWithSlot}}, | 
| Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 97 | {FB_VAR_PARTITION_TYPE, {GetPartitionType, GetAllPartitionArgsWithSlot}}, | 
| David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 98 | {FB_VAR_IS_LOGICAL, {GetPartitionIsLogical, GetAllPartitionArgsWithSlot}}, | 
| David Anderson | c091c17 | 2018-09-04 18:11:03 -0700 | [diff] [blame] | 99 | {FB_VAR_IS_USERSPACE, {GetIsUserspace, nullptr}}, | 
| Hridya Valsaraju | 7c9bbe9 | 2018-09-27 10:41:01 -0700 | [diff] [blame] | 100 | {FB_VAR_OFF_MODE_CHARGE_STATE, {GetOffModeChargeState, nullptr}}, | 
| Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 101 | {FB_VAR_BATTERY_VOLTAGE, {GetBatteryVoltage, nullptr}}, | 
| Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 102 | {FB_VAR_BATTERY_SOC_OK, {GetBatterySoCOk, nullptr}}, | 
| David Anderson | 90fe0a4 | 2018-11-05 18:01:32 -0800 | [diff] [blame] | 103 | {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}}, | 
|  | 104 | {FB_VAR_SUPER_PARTITION_NAME, {GetSuperPartitionName, nullptr}}}; | 
| David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 105 |  | 
|  | 106 | if (args.size() < 2) { | 
|  | 107 | return device->WriteFail("Missing argument"); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | // Special case: return all variables that we can. | 
|  | 111 | if (args[1] == "all") { | 
|  | 112 | for (const auto& [name, handlers] : kVariableMap) { | 
|  | 113 | GetAllVars(device, name, handlers); | 
|  | 114 | } | 
|  | 115 | return device->WriteOkay(""); | 
|  | 116 | } | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 117 |  | 
|  | 118 | // args[0] is command name, args[1] is variable. | 
|  | 119 | auto found_variable = kVariableMap.find(args[1]); | 
|  | 120 | if (found_variable == kVariableMap.end()) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 121 | return device->WriteFail("Unknown variable"); | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 124 | std::string message; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 125 | std::vector<std::string> getvar_args(args.begin() + 2, args.end()); | 
| David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 126 | if (!found_variable->second.get(device, getvar_args, &message)) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 127 | return device->WriteFail(message); | 
|  | 128 | } | 
|  | 129 | return device->WriteOkay(message); | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 130 | } | 
| Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 131 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 132 | bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) { | 
|  | 133 | if (args.size() < 2) { | 
|  | 134 | return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments"); | 
|  | 135 | } | 
| Hridya Valsaraju | d1e6231 | 2018-10-08 09:13:17 -0700 | [diff] [blame] | 136 |  | 
|  | 137 | if (GetDeviceLockStatus()) { | 
|  | 138 | return device->WriteStatus(FastbootResult::FAIL, "Erase is not allowed on locked devices"); | 
|  | 139 | } | 
|  | 140 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 141 | PartitionHandle handle; | 
|  | 142 | if (!OpenPartition(device, args[1], &handle)) { | 
|  | 143 | return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist"); | 
|  | 144 | } | 
|  | 145 | if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) { | 
|  | 146 | return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded"); | 
|  | 147 | } | 
|  | 148 | return device->WriteStatus(FastbootResult::FAIL, "Erasing failed"); | 
|  | 149 | } | 
|  | 150 |  | 
| Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 151 | bool OemCmdHandler(FastbootDevice* device, const std::vector<std::string>& args) { | 
|  | 152 | auto fastboot_hal = device->fastboot_hal(); | 
|  | 153 | if (!fastboot_hal) { | 
|  | 154 | return device->WriteStatus(FastbootResult::FAIL, "Unable to open fastboot HAL"); | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | Result ret; | 
|  | 158 | auto ret_val = fastboot_hal->doOemCommand(args[0], [&](Result result) { ret = result; }); | 
|  | 159 | if (!ret_val.isOk()) { | 
|  | 160 | return device->WriteStatus(FastbootResult::FAIL, "Unable to do OEM command"); | 
|  | 161 | } | 
|  | 162 | if (ret.status != Status::SUCCESS) { | 
|  | 163 | return device->WriteStatus(FastbootResult::FAIL, ret.message); | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | return device->WriteStatus(FastbootResult::OKAY, ret.message); | 
|  | 167 | } | 
|  | 168 |  | 
| Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 169 | bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) { | 
|  | 170 | if (args.size() < 2) { | 
|  | 171 | return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified"); | 
|  | 172 | } | 
| Hridya Valsaraju | d1e6231 | 2018-10-08 09:13:17 -0700 | [diff] [blame] | 173 |  | 
|  | 174 | if (GetDeviceLockStatus()) { | 
|  | 175 | return device->WriteStatus(FastbootResult::FAIL, | 
|  | 176 | "Download is not allowed on locked devices"); | 
|  | 177 | } | 
|  | 178 |  | 
| Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 179 | // arg[0] is the command name, arg[1] contains size of data to be downloaded | 
|  | 180 | unsigned int size; | 
| Hridya Valsaraju | aae84e8 | 2018-10-08 13:10:25 -0700 | [diff] [blame] | 181 | if (!android::base::ParseUint("0x" + args[1], &size, kMaxDownloadSizeDefault)) { | 
| Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 182 | return device->WriteStatus(FastbootResult::FAIL, "Invalid size"); | 
|  | 183 | } | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 184 | device->download_data().resize(size); | 
| Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 185 | if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) { | 
|  | 186 | return false; | 
|  | 187 | } | 
|  | 188 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 189 | if (device->HandleData(true, &device->download_data())) { | 
| Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 190 | return device->WriteStatus(FastbootResult::OKAY, ""); | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | PLOG(ERROR) << "Couldn't download data"; | 
|  | 194 | return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data"); | 
|  | 195 | } | 
|  | 196 |  | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 197 | bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) { | 
|  | 198 | if (args.size() < 2) { | 
|  | 199 | return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument"); | 
|  | 200 | } | 
|  | 201 |  | 
| Hridya Valsaraju | d1e6231 | 2018-10-08 09:13:17 -0700 | [diff] [blame] | 202 | if (GetDeviceLockStatus()) { | 
|  | 203 | return device->WriteStatus(FastbootResult::FAIL, | 
|  | 204 | "set_active command is not allowed on locked devices"); | 
|  | 205 | } | 
|  | 206 |  | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 207 | // Slot suffix needs to be between 'a' and 'z'. | 
|  | 208 | Slot slot; | 
|  | 209 | if (!GetSlotNumber(args[1], &slot)) { | 
|  | 210 | return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix"); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | // Non-A/B devices will not have a boot control HAL. | 
|  | 214 | auto boot_control_hal = device->boot_control_hal(); | 
|  | 215 | if (!boot_control_hal) { | 
|  | 216 | return device->WriteStatus(FastbootResult::FAIL, | 
|  | 217 | "Cannot set slot: boot control HAL absent"); | 
|  | 218 | } | 
|  | 219 | if (slot >= boot_control_hal->getNumberSlots()) { | 
|  | 220 | return device->WriteStatus(FastbootResult::FAIL, "Slot out of range"); | 
|  | 221 | } | 
|  | 222 | CommandResult ret; | 
|  | 223 | auto cb = [&ret](CommandResult result) { ret = result; }; | 
|  | 224 | auto result = boot_control_hal->setActiveBootSlot(slot, cb); | 
| Hridya Valsaraju | 20bdf89 | 2018-10-10 11:02:19 -0700 | [diff] [blame] | 225 | if (result.isOk() && ret.success) { | 
|  | 226 | // Save as slot suffix to match the suffix format as returned from | 
|  | 227 | // the boot control HAL. | 
|  | 228 | auto current_slot = "_" + args[1]; | 
|  | 229 | device->set_active_slot(current_slot); | 
|  | 230 | return device->WriteStatus(FastbootResult::OKAY, ""); | 
|  | 231 | } | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 232 | return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot"); | 
| Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 233 | } | 
|  | 234 |  | 
|  | 235 | bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { | 
|  | 236 | auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down"); | 
|  | 237 | android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot"); | 
|  | 238 | device->CloseDevice(); | 
|  | 239 | TEMP_FAILURE_RETRY(pause()); | 
|  | 240 | return result; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { | 
|  | 244 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting"); | 
|  | 245 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot"); | 
|  | 246 | device->CloseDevice(); | 
|  | 247 | TEMP_FAILURE_RETRY(pause()); | 
|  | 248 | return result; | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { | 
|  | 252 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader"); | 
|  | 253 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader"); | 
|  | 254 | device->CloseDevice(); | 
|  | 255 | TEMP_FAILURE_RETRY(pause()); | 
|  | 256 | return result; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { | 
|  | 260 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot"); | 
|  | 261 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot"); | 
|  | 262 | device->CloseDevice(); | 
|  | 263 | TEMP_FAILURE_RETRY(pause()); | 
|  | 264 | return result; | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | static bool EnterRecovery() { | 
|  | 268 | const char msg_switch_to_recovery = 'r'; | 
|  | 269 |  | 
|  | 270 | android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0)); | 
|  | 271 | if (sock < 0) { | 
|  | 272 | PLOG(ERROR) << "Couldn't create sock"; | 
|  | 273 | return false; | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | struct sockaddr_un addr = {.sun_family = AF_UNIX}; | 
|  | 277 | strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1); | 
|  | 278 | if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { | 
|  | 279 | PLOG(ERROR) << "Couldn't connect to recovery"; | 
|  | 280 | return false; | 
|  | 281 | } | 
|  | 282 | // Switch to recovery will not update the boot reason since it does not | 
|  | 283 | // require a reboot. | 
|  | 284 | auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery)); | 
|  | 285 | if (ret != sizeof(msg_switch_to_recovery)) { | 
|  | 286 | PLOG(ERROR) << "Couldn't write message to switch to recovery"; | 
|  | 287 | return false; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | return true; | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { | 
|  | 294 | auto status = true; | 
|  | 295 | if (EnterRecovery()) { | 
|  | 296 | status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery"); | 
|  | 297 | } else { | 
|  | 298 | status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery"); | 
|  | 299 | } | 
|  | 300 | device->CloseDevice(); | 
|  | 301 | TEMP_FAILURE_RETRY(pause()); | 
|  | 302 | return status; | 
|  | 303 | } | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 304 |  | 
|  | 305 | // Helper class for opening a handle to a MetadataBuilder and writing the new | 
|  | 306 | // partition table to the same place it was read. | 
|  | 307 | class PartitionBuilder { | 
|  | 308 | public: | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 309 | explicit PartitionBuilder(FastbootDevice* device, const std::string& partition_name); | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 310 |  | 
|  | 311 | bool Write(); | 
|  | 312 | bool Valid() const { return !!builder_; } | 
|  | 313 | MetadataBuilder* operator->() const { return builder_.get(); } | 
|  | 314 |  | 
|  | 315 | private: | 
| David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 316 | FastbootDevice* device_; | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 317 | std::string super_device_; | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 318 | uint32_t slot_number_; | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 319 | std::unique_ptr<MetadataBuilder> builder_; | 
|  | 320 | }; | 
|  | 321 |  | 
| David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 322 | PartitionBuilder::PartitionBuilder(FastbootDevice* device, const std::string& partition_name) | 
|  | 323 | : device_(device) { | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 324 | std::string slot_suffix = GetSuperSlotSuffix(device, partition_name); | 
| David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 325 | slot_number_ = android::fs_mgr::SlotNumberForSlotSuffix(slot_suffix); | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 326 | auto super_device = FindPhysicalPartition(fs_mgr_get_super_partition_name(slot_number_)); | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 327 | if (!super_device) { | 
|  | 328 | return; | 
|  | 329 | } | 
|  | 330 | super_device_ = *super_device; | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 331 | builder_ = MetadataBuilder::New(super_device_, slot_number_); | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 332 | } | 
|  | 333 |  | 
|  | 334 | bool PartitionBuilder::Write() { | 
| David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 335 | auto metadata = builder_->Export(); | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 336 | if (!metadata) { | 
|  | 337 | return false; | 
|  | 338 | } | 
| David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 339 | return UpdateAllPartitionMetadata(device_, super_device_, *metadata.get()); | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 340 | } | 
|  | 341 |  | 
|  | 342 | bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { | 
|  | 343 | if (args.size() < 3) { | 
|  | 344 | return device->WriteFail("Invalid partition name and size"); | 
|  | 345 | } | 
|  | 346 |  | 
| Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 347 | if (GetDeviceLockStatus()) { | 
|  | 348 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); | 
|  | 349 | } | 
|  | 350 |  | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 351 | uint64_t partition_size; | 
|  | 352 | std::string partition_name = args[1]; | 
|  | 353 | if (!android::base::ParseUint(args[2].c_str(), &partition_size)) { | 
|  | 354 | return device->WriteFail("Invalid partition size"); | 
|  | 355 | } | 
|  | 356 |  | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 357 | PartitionBuilder builder(device, partition_name); | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 358 | if (!builder.Valid()) { | 
|  | 359 | return device->WriteFail("Could not open super partition"); | 
|  | 360 | } | 
|  | 361 | // TODO(112433293) Disallow if the name is in the physical table as well. | 
|  | 362 | if (builder->FindPartition(partition_name)) { | 
|  | 363 | return device->WriteFail("Partition already exists"); | 
|  | 364 | } | 
|  | 365 |  | 
| David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 366 | auto partition = builder->AddPartition(partition_name, 0); | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 367 | if (!partition) { | 
|  | 368 | return device->WriteFail("Failed to add partition"); | 
|  | 369 | } | 
|  | 370 | if (!builder->ResizePartition(partition, partition_size)) { | 
|  | 371 | builder->RemovePartition(partition_name); | 
|  | 372 | return device->WriteFail("Not enough space for partition"); | 
|  | 373 | } | 
|  | 374 | if (!builder.Write()) { | 
|  | 375 | return device->WriteFail("Failed to write partition table"); | 
|  | 376 | } | 
|  | 377 | return device->WriteOkay("Partition created"); | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { | 
|  | 381 | if (args.size() < 2) { | 
|  | 382 | return device->WriteFail("Invalid partition name and size"); | 
|  | 383 | } | 
|  | 384 |  | 
| Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 385 | if (GetDeviceLockStatus()) { | 
|  | 386 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); | 
|  | 387 | } | 
|  | 388 |  | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 389 | std::string partition_name = args[1]; | 
|  | 390 |  | 
|  | 391 | PartitionBuilder builder(device, partition_name); | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 392 | if (!builder.Valid()) { | 
|  | 393 | return device->WriteFail("Could not open super partition"); | 
|  | 394 | } | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 395 | builder->RemovePartition(partition_name); | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 396 | if (!builder.Write()) { | 
|  | 397 | return device->WriteFail("Failed to write partition table"); | 
|  | 398 | } | 
|  | 399 | return device->WriteOkay("Partition deleted"); | 
|  | 400 | } | 
|  | 401 |  | 
|  | 402 | bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) { | 
|  | 403 | if (args.size() < 3) { | 
|  | 404 | return device->WriteFail("Invalid partition name and size"); | 
|  | 405 | } | 
|  | 406 |  | 
| Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 407 | if (GetDeviceLockStatus()) { | 
|  | 408 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); | 
|  | 409 | } | 
|  | 410 |  | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 411 | uint64_t partition_size; | 
|  | 412 | std::string partition_name = args[1]; | 
|  | 413 | if (!android::base::ParseUint(args[2].c_str(), &partition_size)) { | 
|  | 414 | return device->WriteFail("Invalid partition size"); | 
|  | 415 | } | 
|  | 416 |  | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 417 | PartitionBuilder builder(device, partition_name); | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 418 | if (!builder.Valid()) { | 
|  | 419 | return device->WriteFail("Could not open super partition"); | 
|  | 420 | } | 
|  | 421 |  | 
| David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 422 | auto partition = builder->FindPartition(partition_name); | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 423 | if (!partition) { | 
|  | 424 | return device->WriteFail("Partition does not exist"); | 
|  | 425 | } | 
| David Anderson | ad970fc | 2019-08-27 14:01:16 -0700 | [diff] [blame] | 426 |  | 
|  | 427 | // Remove the updated flag to cancel any snapshots. | 
|  | 428 | uint32_t attrs = partition->attributes(); | 
|  | 429 | partition->set_attributes(attrs & ~LP_PARTITION_ATTR_UPDATED); | 
|  | 430 |  | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 431 | if (!builder->ResizePartition(partition, partition_size)) { | 
|  | 432 | return device->WriteFail("Not enough space to resize partition"); | 
|  | 433 | } | 
|  | 434 | if (!builder.Write()) { | 
|  | 435 | return device->WriteFail("Failed to write partition table"); | 
|  | 436 | } | 
|  | 437 | return device->WriteOkay("Partition resized"); | 
|  | 438 | } | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 439 |  | 
| David Anderson | ad970fc | 2019-08-27 14:01:16 -0700 | [diff] [blame] | 440 | void CancelPartitionSnapshot(FastbootDevice* device, const std::string& partition_name) { | 
|  | 441 | PartitionBuilder builder(device, partition_name); | 
|  | 442 | if (!builder.Valid()) return; | 
|  | 443 |  | 
|  | 444 | auto partition = builder->FindPartition(partition_name); | 
|  | 445 | if (!partition) return; | 
|  | 446 |  | 
|  | 447 | // Remove the updated flag to cancel any snapshots. | 
|  | 448 | uint32_t attrs = partition->attributes(); | 
|  | 449 | partition->set_attributes(attrs & ~LP_PARTITION_ATTR_UPDATED); | 
|  | 450 |  | 
|  | 451 | builder.Write(); | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) { | 
|  | 455 | if (args.size() < 2) { | 
|  | 456 | return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments"); | 
|  | 457 | } | 
|  | 458 |  | 
|  | 459 | if (GetDeviceLockStatus()) { | 
|  | 460 | return device->WriteStatus(FastbootResult::FAIL, | 
|  | 461 | "Flashing is not allowed on locked devices"); | 
|  | 462 | } | 
|  | 463 |  | 
|  | 464 | const auto& partition_name = args[1]; | 
|  | 465 | if (LogicalPartitionExists(device, partition_name)) { | 
|  | 466 | CancelPartitionSnapshot(device, partition_name); | 
|  | 467 | } | 
|  | 468 |  | 
|  | 469 | int ret = Flash(device, partition_name); | 
|  | 470 | if (ret < 0) { | 
|  | 471 | return device->WriteStatus(FastbootResult::FAIL, strerror(-ret)); | 
|  | 472 | } | 
|  | 473 | return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded"); | 
|  | 474 | } | 
|  | 475 |  | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 476 | bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) { | 
|  | 477 | if (args.size() < 2) { | 
|  | 478 | return device->WriteFail("Invalid arguments"); | 
|  | 479 | } | 
| Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 480 |  | 
|  | 481 | if (GetDeviceLockStatus()) { | 
|  | 482 | return device->WriteStatus(FastbootResult::FAIL, "Command not available on locked devices"); | 
|  | 483 | } | 
|  | 484 |  | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 485 | bool wipe = (args.size() >= 3 && args[2] == "wipe"); | 
|  | 486 | return UpdateSuper(device, args[1], wipe); | 
|  | 487 | } | 
| David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 488 |  | 
| David Anderson | 3d782d5 | 2019-01-29 13:09:49 -0800 | [diff] [blame] | 489 | class AutoMountMetadata { | 
|  | 490 | public: | 
|  | 491 | AutoMountMetadata() { | 
| David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 492 | android::fs_mgr::Fstab proc_mounts; | 
| David Anderson | 3d782d5 | 2019-01-29 13:09:49 -0800 | [diff] [blame] | 493 | if (!ReadFstabFromFile("/proc/mounts", &proc_mounts)) { | 
|  | 494 | LOG(ERROR) << "Could not read /proc/mounts"; | 
|  | 495 | return; | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | auto iter = std::find_if(proc_mounts.begin(), proc_mounts.end(), | 
|  | 499 | [](const auto& entry) { return entry.mount_point == "/metadata"; }); | 
|  | 500 | if (iter != proc_mounts.end()) { | 
|  | 501 | mounted_ = true; | 
|  | 502 | return; | 
|  | 503 | } | 
|  | 504 |  | 
|  | 505 | if (!ReadDefaultFstab(&fstab_)) { | 
|  | 506 | LOG(ERROR) << "Could not read default fstab"; | 
|  | 507 | return; | 
|  | 508 | } | 
|  | 509 | mounted_ = EnsurePathMounted(&fstab_, "/metadata"); | 
|  | 510 | should_unmount_ = true; | 
| David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 511 | } | 
| David Anderson | 3d782d5 | 2019-01-29 13:09:49 -0800 | [diff] [blame] | 512 | ~AutoMountMetadata() { | 
|  | 513 | if (mounted_ && should_unmount_) { | 
|  | 514 | EnsurePathUnmounted(&fstab_, "/metadata"); | 
|  | 515 | } | 
|  | 516 | } | 
|  | 517 | explicit operator bool() const { return mounted_; } | 
|  | 518 |  | 
|  | 519 | private: | 
| David Anderson | 2747532 | 2019-06-11 14:00:08 -0700 | [diff] [blame] | 520 | android::fs_mgr::Fstab fstab_; | 
| David Anderson | 3d782d5 | 2019-01-29 13:09:49 -0800 | [diff] [blame] | 521 | bool mounted_ = false; | 
|  | 522 | bool should_unmount_ = false; | 
|  | 523 | }; | 
|  | 524 |  | 
|  | 525 | bool GsiHandler(FastbootDevice* device, const std::vector<std::string>& args) { | 
| David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 526 | if (args.size() != 2) { | 
|  | 527 | return device->WriteFail("Invalid arguments"); | 
|  | 528 | } | 
| David Anderson | 3d782d5 | 2019-01-29 13:09:49 -0800 | [diff] [blame] | 529 |  | 
|  | 530 | AutoMountMetadata mount_metadata; | 
|  | 531 | if (!mount_metadata) { | 
|  | 532 | return device->WriteFail("Could not find GSI install"); | 
|  | 533 | } | 
|  | 534 |  | 
|  | 535 | if (!android::gsi::IsGsiInstalled()) { | 
|  | 536 | return device->WriteStatus(FastbootResult::FAIL, "No GSI is installed"); | 
|  | 537 | } | 
|  | 538 |  | 
| David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 539 | if (args[1] == "wipe") { | 
|  | 540 | if (!android::gsi::UninstallGsi()) { | 
|  | 541 | return device->WriteStatus(FastbootResult::FAIL, strerror(errno)); | 
|  | 542 | } | 
|  | 543 | } else if (args[1] == "disable") { | 
|  | 544 | if (!android::gsi::DisableGsi()) { | 
|  | 545 | return device->WriteStatus(FastbootResult::FAIL, strerror(errno)); | 
|  | 546 | } | 
|  | 547 | } | 
|  | 548 | return device->WriteStatus(FastbootResult::OKAY, "Success"); | 
|  | 549 | } |