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