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> |
David Anderson | ebc8fe1 | 2022-06-17 19:17:43 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 21 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 22 | #include <android-base/file.h> |
| 23 | #include <android-base/logging.h> |
| 24 | #include <android-base/properties.h> |
| 25 | #include <android-base/stringprintf.h> |
| 26 | #include <android-base/strings.h> |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 27 | #include <android/hardware/boot/1.1/IBootControl.h> |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 28 | #include <ext4_utils/ext4_utils.h> |
David Anderson | 90fe0a4 | 2018-11-05 18:01:32 -0800 | [diff] [blame] | 29 | #include <fs_mgr.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 | |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 32 | #include "BootControlClient.h" |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 33 | #include "fastboot_device.h" |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 34 | #include "flashing.h" |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 35 | #include "utility.h" |
| 36 | |
Yifan Hong | b299cb7 | 2021-02-17 13:44:49 -0800 | [diff] [blame] | 37 | #ifdef FB_ENABLE_FETCH |
| 38 | static constexpr bool kEnableFetch = true; |
| 39 | #else |
| 40 | static constexpr bool kEnableFetch = false; |
| 41 | #endif |
| 42 | |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 43 | using MergeStatus = android::hal::BootControlClient::MergeStatus; |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 44 | using aidl::android::hardware::fastboot::FileSystemType; |
David Anderson | 90fe0a4 | 2018-11-05 18:01:32 -0800 | [diff] [blame] | 45 | using namespace android::fs_mgr; |
David Anderson | ebc8fe1 | 2022-06-17 19:17:43 -0700 | [diff] [blame] | 46 | using namespace std::string_literals; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 47 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 48 | constexpr char kFastbootProtocolVersion[] = "0.4"; |
| 49 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 50 | bool GetVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 51 | std::string* message) { |
| 52 | *message = kFastbootProtocolVersion; |
| 53 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 54 | } |
| 55 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 56 | bool GetBootloaderVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 57 | std::string* message) { |
| 58 | *message = android::base::GetProperty("ro.bootloader", ""); |
| 59 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 60 | } |
| 61 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 62 | bool GetBasebandVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 63 | std::string* message) { |
| 64 | *message = android::base::GetProperty("ro.build.expect.baseband", ""); |
| 65 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Bowgo Tsai | 99f9a38 | 2020-01-21 18:31:23 +0800 | [diff] [blame] | 68 | bool GetOsVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 69 | std::string* message) { |
| 70 | *message = android::base::GetProperty("ro.build.version.release", ""); |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | bool GetVndkVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 75 | std::string* message) { |
| 76 | *message = android::base::GetProperty("ro.vndk.version", ""); |
| 77 | return true; |
| 78 | } |
| 79 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 80 | bool GetProduct(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 81 | std::string* message) { |
| 82 | *message = android::base::GetProperty("ro.product.device", ""); |
| 83 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 84 | } |
| 85 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 86 | bool GetSerial(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 87 | std::string* message) { |
| 88 | *message = android::base::GetProperty("ro.serialno", ""); |
| 89 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 90 | } |
| 91 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 92 | bool GetSecure(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 93 | std::string* message) { |
| 94 | *message = android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no"; |
| 95 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Hridya Valsaraju | 4af8090 | 2018-09-26 13:08:16 -0700 | [diff] [blame] | 98 | bool GetVariant(FastbootDevice* device, const std::vector<std::string>& /* args */, |
| 99 | std::string* message) { |
| 100 | auto fastboot_hal = device->fastboot_hal(); |
| 101 | if (!fastboot_hal) { |
| 102 | *message = "Fastboot HAL not found"; |
| 103 | return false; |
| 104 | } |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 105 | std::string device_variant = ""; |
| 106 | auto status = fastboot_hal->getVariant(&device_variant); |
Hridya Valsaraju | 4af8090 | 2018-09-26 13:08:16 -0700 | [diff] [blame] | 107 | |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 108 | if (!status.isOk()) { |
Hridya Valsaraju | 4af8090 | 2018-09-26 13:08:16 -0700 | [diff] [blame] | 109 | *message = "Unable to get device variant"; |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 110 | LOG(ERROR) << message->c_str() << status.getDescription(); |
Hridya Valsaraju | 4af8090 | 2018-09-26 13:08:16 -0700 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 114 | *message = device_variant; |
Hridya Valsaraju | 4af8090 | 2018-09-26 13:08:16 -0700 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 118 | bool GetBatteryVoltageHelper(FastbootDevice* device, int32_t* battery_voltage) { |
Yifan Hong | 4cb88dc | 2021-12-02 18:58:02 -0800 | [diff] [blame] | 119 | using aidl::android::hardware::health::HealthInfo; |
Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 120 | |
| 121 | auto health_hal = device->health_hal(); |
| 122 | if (!health_hal) { |
| 123 | return false; |
| 124 | } |
| 125 | |
Yifan Hong | 4cb88dc | 2021-12-02 18:58:02 -0800 | [diff] [blame] | 126 | HealthInfo health_info; |
| 127 | auto res = health_hal->getHealthInfo(&health_info); |
| 128 | if (!res.isOk()) return false; |
| 129 | *battery_voltage = health_info.batteryVoltageMillivolts; |
Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
| 133 | bool GetBatterySoCOk(FastbootDevice* device, const std::vector<std::string>& /* args */, |
| 134 | std::string* message) { |
| 135 | int32_t battery_voltage = 0; |
| 136 | if (!GetBatteryVoltageHelper(device, &battery_voltage)) { |
| 137 | *message = "Unable to read battery voltage"; |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | auto fastboot_hal = device->fastboot_hal(); |
| 142 | if (!fastboot_hal) { |
| 143 | *message = "Fastboot HAL not found"; |
| 144 | return false; |
| 145 | } |
| 146 | |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 147 | auto voltage_threshold = 0; |
| 148 | auto status = fastboot_hal->getBatteryVoltageFlashingThreshold(&voltage_threshold); |
| 149 | if (!status.isOk()) { |
Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 150 | *message = "Unable to get battery voltage flashing threshold"; |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 151 | LOG(ERROR) << message->c_str() << status.getDescription(); |
Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 152 | return false; |
| 153 | } |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 154 | *message = battery_voltage >= voltage_threshold ? "yes" : "no"; |
Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
Hridya Valsaraju | 7c9bbe9 | 2018-09-27 10:41:01 -0700 | [diff] [blame] | 159 | bool GetOffModeChargeState(FastbootDevice* device, const std::vector<std::string>& /* args */, |
| 160 | std::string* message) { |
| 161 | auto fastboot_hal = device->fastboot_hal(); |
| 162 | if (!fastboot_hal) { |
| 163 | *message = "Fastboot HAL not found"; |
| 164 | return false; |
| 165 | } |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 166 | bool off_mode_charging_state = false; |
| 167 | auto status = fastboot_hal->getOffModeChargeState(&off_mode_charging_state); |
| 168 | if (!status.isOk()) { |
Hridya Valsaraju | 7c9bbe9 | 2018-09-27 10:41:01 -0700 | [diff] [blame] | 169 | *message = "Unable to get off mode charge state"; |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 170 | LOG(ERROR) << message->c_str() << status.getDescription(); |
Hridya Valsaraju | 7c9bbe9 | 2018-09-27 10:41:01 -0700 | [diff] [blame] | 171 | return false; |
| 172 | } |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 173 | *message = off_mode_charging_state ? "1" : "0"; |
Hridya Valsaraju | 7c9bbe9 | 2018-09-27 10:41:01 -0700 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 177 | bool GetBatteryVoltage(FastbootDevice* device, const std::vector<std::string>& /* args */, |
| 178 | std::string* message) { |
Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 179 | int32_t battery_voltage = 0; |
| 180 | if (GetBatteryVoltageHelper(device, &battery_voltage)) { |
| 181 | *message = std::to_string(battery_voltage); |
| 182 | return true; |
Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 183 | } |
Hridya Valsaraju | a534a5a | 2018-10-03 15:53:22 -0700 | [diff] [blame] | 184 | *message = "Unable to get battery voltage"; |
| 185 | return false; |
Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 186 | } |
| 187 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 188 | bool GetCurrentSlot(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 | std::string suffix = device->GetCurrentSlot(); |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 191 | *message = suffix.size() == 2 ? suffix.substr(1) : suffix; |
| 192 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 193 | } |
| 194 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 195 | bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */, |
| 196 | std::string* message) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 197 | auto boot_control_hal = device->boot_control_hal(); |
| 198 | if (!boot_control_hal) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 199 | *message = "0"; |
| 200 | } else { |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 201 | *message = std::to_string(boot_control_hal->GetNumSlots()); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 202 | } |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 203 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 204 | } |
| 205 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 206 | bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args, |
| 207 | std::string* message) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 208 | if (args.empty()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 209 | *message = "Missing argument"; |
| 210 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 211 | } |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 212 | int32_t slot = -1; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 213 | if (!GetSlotNumber(args[0], &slot)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 214 | *message = "Invalid slot"; |
| 215 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 216 | } |
| 217 | auto boot_control_hal = device->boot_control_hal(); |
| 218 | if (!boot_control_hal) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 219 | *message = "Device has no slots"; |
| 220 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 221 | } |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 222 | if (boot_control_hal->IsSlotMarkedSuccessful(slot).value_or(false)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 223 | *message = "no"; |
| 224 | } else { |
| 225 | *message = "yes"; |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 226 | } |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 227 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 228 | } |
| 229 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 230 | bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args, |
| 231 | std::string* message) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 232 | if (args.empty()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 233 | *message = "Missing argument"; |
| 234 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 235 | } |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 236 | int32_t slot = -1; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 237 | if (!GetSlotNumber(args[0], &slot)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 238 | *message = "Invalid slot"; |
| 239 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 240 | } |
| 241 | auto boot_control_hal = device->boot_control_hal(); |
| 242 | if (!boot_control_hal) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 243 | *message = "Device has no slots"; |
| 244 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 245 | } |
Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 246 | if (!boot_control_hal->IsSlotBootable(slot).value_or(false)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 247 | *message = "yes"; |
| 248 | } else { |
| 249 | *message = "no"; |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 250 | } |
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 GetMaxDownloadSize(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 255 | std::string* message) { |
David Anderson | 28b81cd | 2018-09-04 16:51:29 -0700 | [diff] [blame] | 256 | *message = android::base::StringPrintf("0x%X", kMaxDownloadSizeDefault); |
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 GetUnlocked(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 261 | std::string* message) { |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 262 | *message = GetDeviceLockStatus() ? "no" : "yes"; |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 263 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 264 | } |
| 265 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 266 | bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args, |
| 267 | std::string* message) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 268 | if (args.empty()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 269 | *message = "Missing argument"; |
| 270 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 271 | } |
| 272 | std::string slot_suffix = device->GetCurrentSlot(); |
| 273 | if (slot_suffix.empty()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 274 | *message = "no"; |
| 275 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 276 | } |
David Anderson | 79ab0e3 | 2018-08-14 16:21:50 -0700 | [diff] [blame] | 277 | std::string partition_name = args[0] + slot_suffix; |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 278 | if (FindPhysicalPartition(partition_name) || LogicalPartitionExists(device, partition_name)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 279 | *message = "yes"; |
| 280 | } else { |
| 281 | *message = "no"; |
David Anderson | 79ab0e3 | 2018-08-14 16:21:50 -0700 | [diff] [blame] | 282 | } |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 283 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 284 | } |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 285 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 286 | bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args, |
| 287 | std::string* message) { |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 288 | if (args.size() < 1) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 289 | *message = "Missing argument"; |
| 290 | return false; |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 291 | } |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 292 | // Zero-length partitions cannot be created through device-mapper, so we |
| 293 | // special case them here. |
| 294 | bool is_zero_length; |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 295 | if (LogicalPartitionExists(device, args[0], &is_zero_length) && is_zero_length) { |
Hridya Valsaraju | 2a377da | 2018-10-09 10:03:51 -0700 | [diff] [blame] | 296 | *message = "0x0"; |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 297 | return true; |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 298 | } |
| 299 | // Otherwise, open the partition as normal. |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 300 | PartitionHandle handle; |
| 301 | if (!OpenPartition(device, args[0], &handle)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 302 | *message = "Could not open partition"; |
| 303 | return false; |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 304 | } |
| 305 | uint64_t size = get_block_device_size(handle.fd()); |
David Anderson | 4758967 | 2018-09-04 16:22:41 -0700 | [diff] [blame] | 306 | *message = android::base::StringPrintf("0x%" PRIX64, size); |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 307 | return true; |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 308 | } |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 309 | |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 310 | bool GetPartitionType(FastbootDevice* device, const std::vector<std::string>& args, |
| 311 | std::string* message) { |
| 312 | if (args.size() < 1) { |
| 313 | *message = "Missing argument"; |
| 314 | return false; |
| 315 | } |
Hridya Valsaraju | 4165e00 | 2018-10-09 10:40:35 -0700 | [diff] [blame] | 316 | |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 317 | std::string partition_name = args[0]; |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 318 | if (!FindPhysicalPartition(partition_name) && !LogicalPartitionExists(device, partition_name)) { |
Hridya Valsaraju | 4165e00 | 2018-10-09 10:40:35 -0700 | [diff] [blame] | 319 | *message = "Invalid partition"; |
| 320 | return false; |
| 321 | } |
| 322 | |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 323 | auto fastboot_hal = device->fastboot_hal(); |
| 324 | if (!fastboot_hal) { |
LuK1337 | 9dd073e | 2022-03-02 14:18:27 +0100 | [diff] [blame] | 325 | *message = "raw"; |
| 326 | return true; |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | FileSystemType type; |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 330 | auto status = fastboot_hal->getPartitionType(args[0], &type); |
| 331 | |
| 332 | if (!status.isOk()) { |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 333 | *message = "Unable to retrieve partition type"; |
Sandeep Dhavale | 2534d48 | 2022-11-08 22:30:05 +0000 | [diff] [blame] | 334 | LOG(ERROR) << message->c_str() << status.getDescription(); |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 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]; |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 364 | if (LogicalPartitionExists(device, partition_name)) { |
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 | |
Yi-Yo Chiang | 38b68c6 | 2022-11-22 17:32:21 +0800 | [diff] [blame^] | 382 | bool GetIsForceDebuggable(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 383 | std::string* message) { |
| 384 | *message = android::base::GetBoolProperty("ro.force.debuggable", false) ? "yes" : "no"; |
| 385 | return true; |
| 386 | } |
| 387 | |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 388 | std::vector<std::vector<std::string>> GetAllPartitionArgsWithSlot(FastbootDevice* device) { |
| 389 | std::vector<std::vector<std::string>> args; |
| 390 | auto partitions = ListPartitions(device); |
| 391 | for (const auto& partition : partitions) { |
| 392 | args.emplace_back(std::initializer_list<std::string>{partition}); |
| 393 | } |
| 394 | return args; |
| 395 | } |
| 396 | |
| 397 | std::vector<std::vector<std::string>> GetAllPartitionArgsNoSlot(FastbootDevice* device) { |
| 398 | auto partitions = ListPartitions(device); |
| 399 | |
| 400 | std::string slot_suffix = device->GetCurrentSlot(); |
| 401 | if (!slot_suffix.empty()) { |
| 402 | auto names = std::move(partitions); |
| 403 | for (const auto& name : names) { |
| 404 | std::string slotless_name = name; |
| 405 | if (android::base::EndsWith(name, "_a") || android::base::EndsWith(name, "_b")) { |
| 406 | slotless_name = name.substr(0, name.rfind("_")); |
| 407 | } |
| 408 | if (std::find(partitions.begin(), partitions.end(), slotless_name) == |
| 409 | partitions.end()) { |
| 410 | partitions.emplace_back(slotless_name); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | std::vector<std::vector<std::string>> args; |
| 416 | for (const auto& partition : partitions) { |
| 417 | args.emplace_back(std::initializer_list<std::string>{partition}); |
| 418 | } |
| 419 | return args; |
| 420 | } |
David Anderson | c091c17 | 2018-09-04 18:11:03 -0700 | [diff] [blame] | 421 | |
| 422 | bool GetHardwareRevision(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 423 | std::string* message) { |
| 424 | *message = android::base::GetProperty("ro.revision", ""); |
| 425 | return true; |
| 426 | } |
David Anderson | 90fe0a4 | 2018-11-05 18:01:32 -0800 | [diff] [blame] | 427 | |
| 428 | bool GetSuperPartitionName(FastbootDevice* device, const std::vector<std::string>& /* args */, |
| 429 | std::string* message) { |
| 430 | uint32_t slot_number = SlotNumberForSlotSuffix(device->GetCurrentSlot()); |
| 431 | *message = fs_mgr_get_super_partition_name(slot_number); |
| 432 | return true; |
| 433 | } |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 434 | |
| 435 | bool GetSnapshotUpdateStatus(FastbootDevice* device, const std::vector<std::string>& /* args */, |
| 436 | std::string* message) { |
| 437 | // Note that we use the HAL rather than mounting /metadata, since we want |
| 438 | // our results to match the bootloader. |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 439 | auto hal = device->boot1_1(); |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 440 | if (!hal) { |
| 441 | *message = "not supported"; |
| 442 | return false; |
| 443 | } |
| 444 | |
David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 445 | MergeStatus status = hal->getSnapshotMergeStatus(); |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 446 | switch (status) { |
| 447 | case MergeStatus::SNAPSHOTTED: |
| 448 | *message = "snapshotted"; |
| 449 | break; |
| 450 | case MergeStatus::MERGING: |
| 451 | *message = "merging"; |
| 452 | break; |
| 453 | default: |
| 454 | *message = "none"; |
| 455 | break; |
| 456 | } |
| 457 | return true; |
| 458 | } |
Bowgo Tsai | 33da5c9 | 2019-11-13 17:13:49 +0800 | [diff] [blame] | 459 | |
| 460 | bool GetCpuAbi(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 461 | std::string* message) { |
| 462 | *message = android::base::GetProperty("ro.product.cpu.abi", ""); |
| 463 | return true; |
| 464 | } |
Bowgo Tsai | 99f9a38 | 2020-01-21 18:31:23 +0800 | [diff] [blame] | 465 | |
| 466 | bool GetSystemFingerprint(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 467 | std::string* message) { |
| 468 | *message = android::base::GetProperty("ro.system.build.fingerprint", ""); |
| 469 | if (message->empty()) { |
| 470 | *message = android::base::GetProperty("ro.build.fingerprint", ""); |
| 471 | } |
| 472 | return true; |
| 473 | } |
| 474 | |
| 475 | bool GetVendorFingerprint(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 476 | std::string* message) { |
| 477 | *message = android::base::GetProperty("ro.vendor.build.fingerprint", ""); |
| 478 | return true; |
| 479 | } |
| 480 | |
| 481 | bool GetDynamicPartition(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 482 | std::string* message) { |
| 483 | *message = android::base::GetProperty("ro.boot.dynamic_partitions", ""); |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | bool GetFirstApiLevel(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 488 | std::string* message) { |
| 489 | *message = android::base::GetProperty("ro.product.first_api_level", ""); |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | bool GetSecurityPatchLevel(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 494 | std::string* message) { |
| 495 | *message = android::base::GetProperty("ro.build.version.security_patch", ""); |
| 496 | return true; |
| 497 | } |
| 498 | |
| 499 | bool GetTrebleEnabled(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 500 | std::string* message) { |
| 501 | *message = android::base::GetProperty("ro.treble.enabled", ""); |
| 502 | return true; |
| 503 | } |
Yifan Hong | b299cb7 | 2021-02-17 13:44:49 -0800 | [diff] [blame] | 504 | |
| 505 | bool GetMaxFetchSize(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 506 | std::string* message) { |
| 507 | if (!kEnableFetch) { |
| 508 | *message = "fetch not supported on user builds"; |
| 509 | return false; |
| 510 | } |
| 511 | *message = android::base::StringPrintf("0x%X", kMaxFetchSizeDefault); |
| 512 | return true; |
| 513 | } |
David Anderson | ebc8fe1 | 2022-06-17 19:17:43 -0700 | [diff] [blame] | 514 | |
| 515 | bool GetDmesg(FastbootDevice* device) { |
| 516 | if (GetDeviceLockStatus()) { |
| 517 | return device->WriteFail("Cannot use when device flashing is locked"); |
| 518 | } |
| 519 | |
| 520 | std::unique_ptr<FILE, decltype(&::fclose)> fp(popen("/system/bin/dmesg", "re"), ::fclose); |
| 521 | if (!fp) { |
| 522 | PLOG(ERROR) << "popen /system/bin/dmesg"; |
| 523 | return device->WriteFail("Unable to run dmesg: "s + strerror(errno)); |
| 524 | } |
| 525 | |
| 526 | ssize_t rv; |
| 527 | size_t n = 0; |
| 528 | char* str = nullptr; |
| 529 | while ((rv = ::getline(&str, &n, fp.get())) > 0) { |
| 530 | if (str[rv - 1] == '\n') { |
| 531 | rv--; |
| 532 | } |
| 533 | device->WriteInfo(std::string(str, rv)); |
| 534 | } |
| 535 | |
| 536 | int saved_errno = errno; |
| 537 | ::free(str); |
| 538 | |
| 539 | if (rv < 0 && saved_errno) { |
| 540 | LOG(ERROR) << "dmesg getline: " << strerror(saved_errno); |
| 541 | device->WriteFail("Unable to read dmesg: "s + strerror(saved_errno)); |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | return true; |
| 546 | } |