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