| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -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 "variables.h" | 
|  | 18 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 19 | #include <inttypes.h> | 
|  | 20 |  | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 21 | #include <android-base/file.h> | 
|  | 22 | #include <android-base/logging.h> | 
|  | 23 | #include <android-base/properties.h> | 
|  | 24 | #include <android-base/stringprintf.h> | 
|  | 25 | #include <android-base/strings.h> | 
| David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 26 | #include <android/hardware/boot/1.1/IBootControl.h> | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 27 | #include <ext4_utils/ext4_utils.h> | 
| David Anderson | 90fe0a4 | 2018-11-05 18:01:32 -0800 | [diff] [blame] | 28 | #include <fs_mgr.h> | 
| Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 29 | #include <healthhalutils/HealthHalUtils.h> | 
| David Anderson | 90fe0a4 | 2018-11-05 18:01:32 -0800 | [diff] [blame] | 30 | #include <liblp/liblp.h> | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | #include "fastboot_device.h" | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 33 | #include "flashing.h" | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 34 | #include "utility.h" | 
|  | 35 |  | 
|  | 36 | using ::android::hardware::boot::V1_0::BoolResult; | 
|  | 37 | using ::android::hardware::boot::V1_0::Slot; | 
| David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 38 | using ::android::hardware::boot::V1_1::MergeStatus; | 
| Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 39 | using ::android::hardware::fastboot::V1_0::FileSystemType; | 
|  | 40 | using ::android::hardware::fastboot::V1_0::Result; | 
|  | 41 | using ::android::hardware::fastboot::V1_0::Status; | 
| David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 42 | using IBootControl1_1 = ::android::hardware::boot::V1_1::IBootControl; | 
| David Anderson | 90fe0a4 | 2018-11-05 18:01:32 -0800 | [diff] [blame] | 43 | using namespace android::fs_mgr; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 44 |  | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 45 | constexpr char kFastbootProtocolVersion[] = "0.4"; | 
|  | 46 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 47 | bool GetVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, | 
|  | 48 | std::string* message) { | 
|  | 49 | *message = kFastbootProtocolVersion; | 
|  | 50 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 53 | bool GetBootloaderVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, | 
|  | 54 | std::string* message) { | 
|  | 55 | *message = android::base::GetProperty("ro.bootloader", ""); | 
|  | 56 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 59 | bool GetBasebandVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, | 
|  | 60 | std::string* message) { | 
|  | 61 | *message = android::base::GetProperty("ro.build.expect.baseband", ""); | 
|  | 62 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 65 | bool GetProduct(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, | 
|  | 66 | std::string* message) { | 
|  | 67 | *message = android::base::GetProperty("ro.product.device", ""); | 
|  | 68 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 71 | bool GetSerial(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, | 
|  | 72 | std::string* message) { | 
|  | 73 | *message = android::base::GetProperty("ro.serialno", ""); | 
|  | 74 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 77 | bool GetSecure(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, | 
|  | 78 | std::string* message) { | 
|  | 79 | *message = android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no"; | 
|  | 80 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Hridya Valsaraju | 4af8090 | 2018-09-26 13:08:16 -0700 | [diff] [blame] | 83 | bool GetVariant(FastbootDevice* device, const std::vector<std::string>& /* args */, | 
|  | 84 | std::string* message) { | 
|  | 85 | auto fastboot_hal = device->fastboot_hal(); | 
|  | 86 | if (!fastboot_hal) { | 
|  | 87 | *message = "Fastboot HAL not found"; | 
|  | 88 | return false; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | Result ret; | 
|  | 92 | auto ret_val = fastboot_hal->getVariant([&](std::string device_variant, Result result) { | 
|  | 93 | *message = device_variant; | 
|  | 94 | ret = result; | 
|  | 95 | }); | 
|  | 96 | if (!ret_val.isOk() || ret.status != Status::SUCCESS) { | 
|  | 97 | *message = "Unable to get device variant"; | 
|  | 98 | return false; | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | return true; | 
|  | 102 | } | 
|  | 103 |  | 
| Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 104 | bool GetBatteryVoltageHelper(FastbootDevice* device, int32_t* battery_voltage) { | 
|  | 105 | using android::hardware::health::V2_0::HealthInfo; | 
|  | 106 | using android::hardware::health::V2_0::Result; | 
|  | 107 |  | 
|  | 108 | auto health_hal = device->health_hal(); | 
|  | 109 | if (!health_hal) { | 
|  | 110 | return false; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | Result ret; | 
|  | 114 | auto ret_val = health_hal->getHealthInfo([&](Result result, HealthInfo info) { | 
|  | 115 | *battery_voltage = info.legacy.batteryVoltage; | 
|  | 116 | ret = result; | 
|  | 117 | }); | 
|  | 118 | if (!ret_val.isOk() || (ret != Result::SUCCESS)) { | 
|  | 119 | return false; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | return true; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | bool GetBatterySoCOk(FastbootDevice* device, const std::vector<std::string>& /* args */, | 
|  | 126 | std::string* message) { | 
|  | 127 | int32_t battery_voltage = 0; | 
|  | 128 | if (!GetBatteryVoltageHelper(device, &battery_voltage)) { | 
|  | 129 | *message = "Unable to read battery voltage"; | 
|  | 130 | return false; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | auto fastboot_hal = device->fastboot_hal(); | 
|  | 134 | if (!fastboot_hal) { | 
|  | 135 | *message = "Fastboot HAL not found"; | 
|  | 136 | return false; | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | Result ret; | 
|  | 140 | auto ret_val = fastboot_hal->getBatteryVoltageFlashingThreshold( | 
|  | 141 | [&](int32_t voltage_threshold, Result result) { | 
|  | 142 | *message = battery_voltage >= voltage_threshold ? "yes" : "no"; | 
|  | 143 | ret = result; | 
|  | 144 | }); | 
|  | 145 |  | 
|  | 146 | if (!ret_val.isOk() || ret.status != Status::SUCCESS) { | 
|  | 147 | *message = "Unable to get battery voltage flashing threshold"; | 
|  | 148 | return false; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | return true; | 
|  | 152 | } | 
|  | 153 |  | 
| Hridya Valsaraju | 7c9bbe9 | 2018-09-27 10:41:01 -0700 | [diff] [blame] | 154 | bool GetOffModeChargeState(FastbootDevice* device, const std::vector<std::string>& /* args */, | 
|  | 155 | std::string* message) { | 
|  | 156 | auto fastboot_hal = device->fastboot_hal(); | 
|  | 157 | if (!fastboot_hal) { | 
|  | 158 | *message = "Fastboot HAL not found"; | 
|  | 159 | return false; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | Result ret; | 
|  | 163 | auto ret_val = | 
|  | 164 | fastboot_hal->getOffModeChargeState([&](bool off_mode_charging_state, Result result) { | 
|  | 165 | *message = off_mode_charging_state ? "1" : "0"; | 
|  | 166 | ret = result; | 
|  | 167 | }); | 
|  | 168 | if (!ret_val.isOk() || (ret.status != Status::SUCCESS)) { | 
|  | 169 | *message = "Unable to get off mode charge state"; | 
|  | 170 | return false; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | return true; | 
|  | 174 | } | 
|  | 175 |  | 
| Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 176 | bool GetBatteryVoltage(FastbootDevice* device, const std::vector<std::string>& /* args */, | 
|  | 177 | std::string* message) { | 
| Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 178 | int32_t battery_voltage = 0; | 
|  | 179 | if (GetBatteryVoltageHelper(device, &battery_voltage)) { | 
|  | 180 | *message = std::to_string(battery_voltage); | 
|  | 181 | return true; | 
| Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 182 | } | 
| Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 183 | *message = "Unable to get battery voltage"; | 
|  | 184 | return false; | 
| Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 187 | bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */, | 
|  | 188 | std::string* message) { | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 189 | std::string suffix = device->GetCurrentSlot(); | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 190 | *message = suffix.size() == 2 ? suffix.substr(1) : suffix; | 
|  | 191 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 194 | bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */, | 
|  | 195 | std::string* message) { | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 196 | auto boot_control_hal = device->boot_control_hal(); | 
|  | 197 | if (!boot_control_hal) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 198 | *message = "0"; | 
|  | 199 | } else { | 
|  | 200 | *message = std::to_string(boot_control_hal->getNumberSlots()); | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 201 | } | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 202 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 203 | } | 
|  | 204 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 205 | bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args, | 
|  | 206 | std::string* message) { | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 207 | if (args.empty()) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 208 | *message = "Missing argument"; | 
|  | 209 | return false; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 210 | } | 
|  | 211 | Slot slot; | 
|  | 212 | if (!GetSlotNumber(args[0], &slot)) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 213 | *message = "Invalid slot"; | 
|  | 214 | return false; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 215 | } | 
|  | 216 | auto boot_control_hal = device->boot_control_hal(); | 
|  | 217 | if (!boot_control_hal) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 218 | *message = "Device has no slots"; | 
|  | 219 | return false; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 220 | } | 
| David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 221 | if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 222 | *message = "no"; | 
|  | 223 | } else { | 
|  | 224 | *message = "yes"; | 
| David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 225 | } | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 226 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 227 | } | 
|  | 228 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 229 | bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args, | 
|  | 230 | std::string* message) { | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 231 | if (args.empty()) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 232 | *message = "Missing argument"; | 
|  | 233 | return false; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 234 | } | 
|  | 235 | Slot slot; | 
|  | 236 | if (!GetSlotNumber(args[0], &slot)) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 237 | *message = "Invalid slot"; | 
|  | 238 | return false; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 239 | } | 
|  | 240 | auto boot_control_hal = device->boot_control_hal(); | 
|  | 241 | if (!boot_control_hal) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 242 | *message = "Device has no slots"; | 
|  | 243 | return false; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 244 | } | 
| David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 245 | if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 246 | *message = "yes"; | 
|  | 247 | } else { | 
|  | 248 | *message = "no"; | 
| David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 249 | } | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 250 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 253 | bool GetMaxDownloadSize(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, | 
|  | 254 | std::string* message) { | 
| David Anderson | 28b81cd | 2018-09-04 16:51:29 -0700 | [diff] [blame] | 255 | *message = android::base::StringPrintf("0x%X", kMaxDownloadSizeDefault); | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 256 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 259 | bool GetUnlocked(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, | 
|  | 260 | std::string* message) { | 
| Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 261 | *message = GetDeviceLockStatus() ? "no" : "yes"; | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 262 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 263 | } | 
|  | 264 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 265 | bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args, | 
|  | 266 | std::string* message) { | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 267 | if (args.empty()) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 268 | *message = "Missing argument"; | 
|  | 269 | return false; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 270 | } | 
|  | 271 | std::string slot_suffix = device->GetCurrentSlot(); | 
|  | 272 | if (slot_suffix.empty()) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 273 | *message = "no"; | 
|  | 274 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 275 | } | 
| David Anderson | 79ab0e3 | 2018-08-14 16:21:50 -0700 | [diff] [blame] | 276 | std::string partition_name = args[0] + slot_suffix; | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 277 | if (FindPhysicalPartition(partition_name) || LogicalPartitionExists(device, partition_name)) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 278 | *message = "yes"; | 
|  | 279 | } else { | 
|  | 280 | *message = "no"; | 
| David Anderson | 79ab0e3 | 2018-08-14 16:21:50 -0700 | [diff] [blame] | 281 | } | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 282 | return true; | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 283 | } | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 284 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 285 | bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args, | 
|  | 286 | std::string* message) { | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 287 | if (args.size() < 1) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 288 | *message = "Missing argument"; | 
|  | 289 | return false; | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 290 | } | 
| David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 291 | // Zero-length partitions cannot be created through device-mapper, so we | 
|  | 292 | // special case them here. | 
|  | 293 | bool is_zero_length; | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 294 | if (LogicalPartitionExists(device, args[0], &is_zero_length) && is_zero_length) { | 
| Hridya Valsaraju | 2a377da | 2018-10-09 10:03:51 -0700 | [diff] [blame] | 295 | *message = "0x0"; | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 296 | return true; | 
| David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 297 | } | 
|  | 298 | // Otherwise, open the partition as normal. | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 299 | PartitionHandle handle; | 
|  | 300 | if (!OpenPartition(device, args[0], &handle)) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 301 | *message = "Could not open partition"; | 
|  | 302 | return false; | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 303 | } | 
|  | 304 | uint64_t size = get_block_device_size(handle.fd()); | 
| David Anderson | 4758967 | 2018-09-04 16:22:41 -0700 | [diff] [blame] | 305 | *message = android::base::StringPrintf("0x%" PRIX64, size); | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 306 | return true; | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 307 | } | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 308 |  | 
| Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 309 | bool GetPartitionType(FastbootDevice* device, const std::vector<std::string>& args, | 
|  | 310 | std::string* message) { | 
|  | 311 | if (args.size() < 1) { | 
|  | 312 | *message = "Missing argument"; | 
|  | 313 | return false; | 
|  | 314 | } | 
| Hridya Valsaraju | 4165e00 | 2018-10-09 10:40:35 -0700 | [diff] [blame] | 315 |  | 
| Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 316 | std::string partition_name = args[0]; | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 317 | if (!FindPhysicalPartition(partition_name) && !LogicalPartitionExists(device, partition_name)) { | 
| Hridya Valsaraju | 4165e00 | 2018-10-09 10:40:35 -0700 | [diff] [blame] | 318 | *message = "Invalid partition"; | 
|  | 319 | return false; | 
|  | 320 | } | 
|  | 321 |  | 
| Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 322 | auto fastboot_hal = device->fastboot_hal(); | 
|  | 323 | if (!fastboot_hal) { | 
|  | 324 | *message = "Fastboot HAL not found"; | 
|  | 325 | return false; | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | FileSystemType type; | 
|  | 329 | Result ret; | 
|  | 330 | auto ret_val = | 
|  | 331 | fastboot_hal->getPartitionType(args[0], [&](FileSystemType fs_type, Result result) { | 
|  | 332 | type = fs_type; | 
|  | 333 | ret = result; | 
|  | 334 | }); | 
|  | 335 | if (!ret_val.isOk() || (ret.status != Status::SUCCESS)) { | 
|  | 336 | *message = "Unable to retrieve partition type"; | 
|  | 337 | } else { | 
|  | 338 | switch (type) { | 
|  | 339 | case FileSystemType::RAW: | 
|  | 340 | *message = "raw"; | 
|  | 341 | return true; | 
|  | 342 | case FileSystemType::EXT4: | 
|  | 343 | *message = "ext4"; | 
|  | 344 | return true; | 
|  | 345 | case FileSystemType::F2FS: | 
|  | 346 | *message = "f2fs"; | 
|  | 347 | return true; | 
|  | 348 | default: | 
|  | 349 | *message = "Unknown file system type"; | 
|  | 350 | } | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | return false; | 
|  | 354 | } | 
|  | 355 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 356 | bool GetPartitionIsLogical(FastbootDevice* device, const std::vector<std::string>& args, | 
|  | 357 | std::string* message) { | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 358 | if (args.size() < 1) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 359 | *message = "Missing argument"; | 
|  | 360 | return false; | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 361 | } | 
|  | 362 | // Note: if a partition name is in both the GPT and the super partition, we | 
|  | 363 | // return "true", to be consistent with prefering to flash logical partitions | 
|  | 364 | // over physical ones. | 
|  | 365 | std::string partition_name = args[0]; | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 366 | if (LogicalPartitionExists(device, partition_name)) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 367 | *message = "yes"; | 
|  | 368 | return true; | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 369 | } | 
|  | 370 | if (FindPhysicalPartition(partition_name)) { | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 371 | *message = "no"; | 
|  | 372 | return true; | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 373 | } | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 374 | *message = "Partition not found"; | 
|  | 375 | return false; | 
| David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 376 | } | 
| David Anderson | d9ba061 | 2018-08-02 11:05:00 -0700 | [diff] [blame] | 377 |  | 
| David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 378 | bool GetIsUserspace(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, | 
|  | 379 | std::string* message) { | 
|  | 380 | *message = "yes"; | 
|  | 381 | return true; | 
| David Anderson | d9ba061 | 2018-08-02 11:05:00 -0700 | [diff] [blame] | 382 | } | 
| David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 383 |  | 
|  | 384 | std::vector<std::vector<std::string>> GetAllPartitionArgsWithSlot(FastbootDevice* device) { | 
|  | 385 | std::vector<std::vector<std::string>> args; | 
|  | 386 | auto partitions = ListPartitions(device); | 
|  | 387 | for (const auto& partition : partitions) { | 
|  | 388 | args.emplace_back(std::initializer_list<std::string>{partition}); | 
|  | 389 | } | 
|  | 390 | return args; | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | std::vector<std::vector<std::string>> GetAllPartitionArgsNoSlot(FastbootDevice* device) { | 
|  | 394 | auto partitions = ListPartitions(device); | 
|  | 395 |  | 
|  | 396 | std::string slot_suffix = device->GetCurrentSlot(); | 
|  | 397 | if (!slot_suffix.empty()) { | 
|  | 398 | auto names = std::move(partitions); | 
|  | 399 | for (const auto& name : names) { | 
|  | 400 | std::string slotless_name = name; | 
|  | 401 | if (android::base::EndsWith(name, "_a") || android::base::EndsWith(name, "_b")) { | 
|  | 402 | slotless_name = name.substr(0, name.rfind("_")); | 
|  | 403 | } | 
|  | 404 | if (std::find(partitions.begin(), partitions.end(), slotless_name) == | 
|  | 405 | partitions.end()) { | 
|  | 406 | partitions.emplace_back(slotless_name); | 
|  | 407 | } | 
|  | 408 | } | 
|  | 409 | } | 
|  | 410 |  | 
|  | 411 | std::vector<std::vector<std::string>> args; | 
|  | 412 | for (const auto& partition : partitions) { | 
|  | 413 | args.emplace_back(std::initializer_list<std::string>{partition}); | 
|  | 414 | } | 
|  | 415 | return args; | 
|  | 416 | } | 
| David Anderson | c091c17 | 2018-09-04 18:11:03 -0700 | [diff] [blame] | 417 |  | 
|  | 418 | bool GetHardwareRevision(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, | 
|  | 419 | std::string* message) { | 
|  | 420 | *message = android::base::GetProperty("ro.revision", ""); | 
|  | 421 | return true; | 
|  | 422 | } | 
| David Anderson | 90fe0a4 | 2018-11-05 18:01:32 -0800 | [diff] [blame] | 423 |  | 
|  | 424 | bool GetSuperPartitionName(FastbootDevice* device, const std::vector<std::string>& /* args */, | 
|  | 425 | std::string* message) { | 
|  | 426 | uint32_t slot_number = SlotNumberForSlotSuffix(device->GetCurrentSlot()); | 
|  | 427 | *message = fs_mgr_get_super_partition_name(slot_number); | 
|  | 428 | return true; | 
|  | 429 | } | 
| David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 430 |  | 
|  | 431 | bool GetSnapshotUpdateStatus(FastbootDevice* device, const std::vector<std::string>& /* args */, | 
|  | 432 | std::string* message) { | 
|  | 433 | // Note that we use the HAL rather than mounting /metadata, since we want | 
|  | 434 | // our results to match the bootloader. | 
| David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 435 | auto hal = device->boot1_1(); | 
| David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 436 | if (!hal) { | 
|  | 437 | *message = "not supported"; | 
|  | 438 | return false; | 
|  | 439 | } | 
|  | 440 |  | 
| David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 441 | MergeStatus status = hal->getSnapshotMergeStatus(); | 
| David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 442 | switch (status) { | 
|  | 443 | case MergeStatus::SNAPSHOTTED: | 
|  | 444 | *message = "snapshotted"; | 
|  | 445 | break; | 
|  | 446 | case MergeStatus::MERGING: | 
|  | 447 | *message = "merging"; | 
|  | 448 | break; | 
|  | 449 | default: | 
|  | 450 | *message = "none"; | 
|  | 451 | break; | 
|  | 452 | } | 
|  | 453 | return true; | 
|  | 454 | } | 
| Bowgo Tsai | 33da5c9 | 2019-11-13 17:13:49 +0800 | [diff] [blame] | 455 |  | 
|  | 456 | bool GetCpuAbi(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, | 
|  | 457 | std::string* message) { | 
|  | 458 | *message = android::base::GetProperty("ro.product.cpu.abi", ""); | 
|  | 459 | return true; | 
|  | 460 | } |