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