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> |
| 27 | |
| 28 | #include "fastboot_device.h" |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 29 | #include "flashing.h" |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 30 | #include "utility.h" |
| 31 | |
| 32 | using ::android::hardware::boot::V1_0::BoolResult; |
| 33 | using ::android::hardware::boot::V1_0::Slot; |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 34 | using ::android::hardware::fastboot::V1_0::FileSystemType; |
| 35 | using ::android::hardware::fastboot::V1_0::Result; |
| 36 | using ::android::hardware::fastboot::V1_0::Status; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 37 | |
| 38 | constexpr int kMaxDownloadSizeDefault = 0x20000000; |
| 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 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 77 | bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */, |
| 78 | std::string* message) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 79 | std::string suffix = device->GetCurrentSlot(); |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 80 | *message = suffix.size() == 2 ? suffix.substr(1) : suffix; |
| 81 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 82 | } |
| 83 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 84 | bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */, |
| 85 | std::string* message) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 86 | auto boot_control_hal = device->boot_control_hal(); |
| 87 | if (!boot_control_hal) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 88 | *message = "0"; |
| 89 | } else { |
| 90 | *message = std::to_string(boot_control_hal->getNumberSlots()); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 91 | } |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 92 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 93 | } |
| 94 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 95 | bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args, |
| 96 | std::string* message) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 97 | if (args.empty()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 98 | *message = "Missing argument"; |
| 99 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 100 | } |
| 101 | Slot slot; |
| 102 | if (!GetSlotNumber(args[0], &slot)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 103 | *message = "Invalid slot"; |
| 104 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 105 | } |
| 106 | auto boot_control_hal = device->boot_control_hal(); |
| 107 | if (!boot_control_hal) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 108 | *message = "Device has no slots"; |
| 109 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 110 | } |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 111 | if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 112 | *message = "no"; |
| 113 | } else { |
| 114 | *message = "yes"; |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 115 | } |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 116 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 117 | } |
| 118 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 119 | bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args, |
| 120 | std::string* message) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 121 | if (args.empty()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 122 | *message = "Missing argument"; |
| 123 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 124 | } |
| 125 | Slot slot; |
| 126 | if (!GetSlotNumber(args[0], &slot)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 127 | *message = "Invalid slot"; |
| 128 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 129 | } |
| 130 | auto boot_control_hal = device->boot_control_hal(); |
| 131 | if (!boot_control_hal) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 132 | *message = "Device has no slots"; |
| 133 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 134 | } |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 135 | if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 136 | *message = "yes"; |
| 137 | } else { |
| 138 | *message = "no"; |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 139 | } |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 140 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 141 | } |
| 142 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 143 | bool GetMaxDownloadSize(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 144 | std::string* message) { |
David Anderson | 28b81cd | 2018-09-04 16:51:29 -0700 | [diff] [blame] | 145 | *message = android::base::StringPrintf("0x%X", kMaxDownloadSizeDefault); |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 146 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 147 | } |
| 148 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 149 | bool GetUnlocked(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 150 | std::string* message) { |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame^] | 151 | *message = GetDeviceLockStatus() ? "no" : "yes"; |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 152 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 153 | } |
| 154 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 155 | bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args, |
| 156 | std::string* message) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 157 | if (args.empty()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 158 | *message = "Missing argument"; |
| 159 | return false; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 160 | } |
| 161 | std::string slot_suffix = device->GetCurrentSlot(); |
| 162 | if (slot_suffix.empty()) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 163 | *message = "no"; |
| 164 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 165 | } |
David Anderson | 79ab0e3 | 2018-08-14 16:21:50 -0700 | [diff] [blame] | 166 | std::string partition_name = args[0] + slot_suffix; |
| 167 | if (FindPhysicalPartition(partition_name) || |
| 168 | LogicalPartitionExists(partition_name, slot_suffix)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 169 | *message = "yes"; |
| 170 | } else { |
| 171 | *message = "no"; |
David Anderson | 79ab0e3 | 2018-08-14 16:21:50 -0700 | [diff] [blame] | 172 | } |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 173 | return true; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 174 | } |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 175 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 176 | bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args, |
| 177 | std::string* message) { |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 178 | if (args.size() < 1) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 179 | *message = "Missing argument"; |
| 180 | return false; |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 181 | } |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 182 | // Zero-length partitions cannot be created through device-mapper, so we |
| 183 | // special case them here. |
| 184 | bool is_zero_length; |
| 185 | if (LogicalPartitionExists(args[0], device->GetCurrentSlot(), &is_zero_length) && |
| 186 | is_zero_length) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 187 | *message = "0"; |
| 188 | return true; |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 189 | } |
| 190 | // Otherwise, open the partition as normal. |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 191 | PartitionHandle handle; |
| 192 | if (!OpenPartition(device, args[0], &handle)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 193 | *message = "Could not open partition"; |
| 194 | return false; |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 195 | } |
| 196 | uint64_t size = get_block_device_size(handle.fd()); |
David Anderson | 4758967 | 2018-09-04 16:22:41 -0700 | [diff] [blame] | 197 | *message = android::base::StringPrintf("0x%" PRIX64, size); |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 198 | return true; |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 199 | } |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 200 | |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 201 | bool GetPartitionType(FastbootDevice* device, const std::vector<std::string>& args, |
| 202 | std::string* message) { |
| 203 | if (args.size() < 1) { |
| 204 | *message = "Missing argument"; |
| 205 | return false; |
| 206 | } |
| 207 | std::string partition_name = args[0]; |
| 208 | auto fastboot_hal = device->fastboot_hal(); |
| 209 | if (!fastboot_hal) { |
| 210 | *message = "Fastboot HAL not found"; |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | FileSystemType type; |
| 215 | Result ret; |
| 216 | auto ret_val = |
| 217 | fastboot_hal->getPartitionType(args[0], [&](FileSystemType fs_type, Result result) { |
| 218 | type = fs_type; |
| 219 | ret = result; |
| 220 | }); |
| 221 | if (!ret_val.isOk() || (ret.status != Status::SUCCESS)) { |
| 222 | *message = "Unable to retrieve partition type"; |
| 223 | } else { |
| 224 | switch (type) { |
| 225 | case FileSystemType::RAW: |
| 226 | *message = "raw"; |
| 227 | return true; |
| 228 | case FileSystemType::EXT4: |
| 229 | *message = "ext4"; |
| 230 | return true; |
| 231 | case FileSystemType::F2FS: |
| 232 | *message = "f2fs"; |
| 233 | return true; |
| 234 | default: |
| 235 | *message = "Unknown file system type"; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return false; |
| 240 | } |
| 241 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 242 | bool GetPartitionIsLogical(FastbootDevice* device, const std::vector<std::string>& args, |
| 243 | std::string* message) { |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 244 | if (args.size() < 1) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 245 | *message = "Missing argument"; |
| 246 | return false; |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 247 | } |
| 248 | // Note: if a partition name is in both the GPT and the super partition, we |
| 249 | // return "true", to be consistent with prefering to flash logical partitions |
| 250 | // over physical ones. |
| 251 | std::string partition_name = args[0]; |
| 252 | if (LogicalPartitionExists(partition_name, device->GetCurrentSlot())) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 253 | *message = "yes"; |
| 254 | return true; |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 255 | } |
| 256 | if (FindPhysicalPartition(partition_name)) { |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 257 | *message = "no"; |
| 258 | return true; |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 259 | } |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 260 | *message = "Partition not found"; |
| 261 | return false; |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 262 | } |
David Anderson | d9ba061 | 2018-08-02 11:05:00 -0700 | [diff] [blame] | 263 | |
David Anderson | 1fb3fd7 | 2018-08-31 14:40:22 -0700 | [diff] [blame] | 264 | bool GetIsUserspace(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 265 | std::string* message) { |
| 266 | *message = "yes"; |
| 267 | return true; |
David Anderson | d9ba061 | 2018-08-02 11:05:00 -0700 | [diff] [blame] | 268 | } |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 269 | |
| 270 | std::vector<std::vector<std::string>> GetAllPartitionArgsWithSlot(FastbootDevice* device) { |
| 271 | std::vector<std::vector<std::string>> args; |
| 272 | auto partitions = ListPartitions(device); |
| 273 | for (const auto& partition : partitions) { |
| 274 | args.emplace_back(std::initializer_list<std::string>{partition}); |
| 275 | } |
| 276 | return args; |
| 277 | } |
| 278 | |
| 279 | std::vector<std::vector<std::string>> GetAllPartitionArgsNoSlot(FastbootDevice* device) { |
| 280 | auto partitions = ListPartitions(device); |
| 281 | |
| 282 | std::string slot_suffix = device->GetCurrentSlot(); |
| 283 | if (!slot_suffix.empty()) { |
| 284 | auto names = std::move(partitions); |
| 285 | for (const auto& name : names) { |
| 286 | std::string slotless_name = name; |
| 287 | if (android::base::EndsWith(name, "_a") || android::base::EndsWith(name, "_b")) { |
| 288 | slotless_name = name.substr(0, name.rfind("_")); |
| 289 | } |
| 290 | if (std::find(partitions.begin(), partitions.end(), slotless_name) == |
| 291 | partitions.end()) { |
| 292 | partitions.emplace_back(slotless_name); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | std::vector<std::vector<std::string>> args; |
| 298 | for (const auto& partition : partitions) { |
| 299 | args.emplace_back(std::initializer_list<std::string>{partition}); |
| 300 | } |
| 301 | return args; |
| 302 | } |
David Anderson | c091c17 | 2018-09-04 18:11:03 -0700 | [diff] [blame] | 303 | |
| 304 | bool GetHardwareRevision(FastbootDevice* /* device */, const std::vector<std::string>& /* args */, |
| 305 | std::string* message) { |
| 306 | *message = android::base::GetProperty("ro.revision", ""); |
| 307 | return true; |
| 308 | } |