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; |
| 34 | |
| 35 | constexpr int kMaxDownloadSizeDefault = 0x20000000; |
| 36 | constexpr char kFastbootProtocolVersion[] = "0.4"; |
| 37 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 38 | bool GetVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 39 | return device->WriteOkay(kFastbootProtocolVersion); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 40 | } |
| 41 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 42 | bool GetBootloaderVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 43 | return device->WriteOkay(android::base::GetProperty("ro.bootloader", "")); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 44 | } |
| 45 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 46 | bool GetBasebandVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 47 | return device->WriteOkay(android::base::GetProperty("ro.build.expect.baseband", "")); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 48 | } |
| 49 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 50 | bool GetProduct(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 51 | return device->WriteOkay(android::base::GetProperty("ro.product.device", "")); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 52 | } |
| 53 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 54 | bool GetSerial(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 55 | return device->WriteOkay(android::base::GetProperty("ro.serialno", "")); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 56 | } |
| 57 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 58 | bool GetSecure(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 59 | return device->WriteOkay(android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 60 | } |
| 61 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 62 | bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 63 | std::string suffix = device->GetCurrentSlot(); |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 64 | std::string slot = suffix.size() == 2 ? suffix.substr(1) : suffix; |
| 65 | return device->WriteOkay(slot); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 66 | } |
| 67 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 68 | bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 69 | auto boot_control_hal = device->boot_control_hal(); |
| 70 | if (!boot_control_hal) { |
| 71 | return "0"; |
| 72 | } |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 73 | return device->WriteOkay(std::to_string(boot_control_hal->getNumberSlots())); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 74 | } |
| 75 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 76 | bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 77 | if (args.empty()) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 78 | return device->WriteFail("Missing argument"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 79 | } |
| 80 | Slot slot; |
| 81 | if (!GetSlotNumber(args[0], &slot)) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 82 | return device->WriteFail("Invalid slot"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 83 | } |
| 84 | auto boot_control_hal = device->boot_control_hal(); |
| 85 | if (!boot_control_hal) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 86 | return device->WriteFail("Device has no slots"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 87 | } |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 88 | if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) { |
| 89 | return device->WriteOkay("no"); |
| 90 | } |
| 91 | return device->WriteOkay("yes"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 92 | } |
| 93 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 94 | bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 95 | if (args.empty()) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 96 | return device->WriteFail("Missing argument"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 97 | } |
| 98 | Slot slot; |
| 99 | if (!GetSlotNumber(args[0], &slot)) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 100 | return device->WriteFail("Invalid slot"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 101 | } |
| 102 | auto boot_control_hal = device->boot_control_hal(); |
| 103 | if (!boot_control_hal) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 104 | return device->WriteFail("Device has no slots"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 105 | } |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 106 | if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) { |
| 107 | return device->WriteOkay("yes"); |
| 108 | } |
| 109 | return device->WriteOkay("no"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 110 | } |
| 111 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 112 | bool GetMaxDownloadSize(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 113 | return device->WriteOkay(std::to_string(kMaxDownloadSizeDefault)); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 114 | } |
| 115 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 116 | bool GetUnlocked(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 117 | return device->WriteOkay("yes"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 118 | } |
| 119 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 120 | bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 121 | if (args.empty()) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 122 | return device->WriteFail("Missing argument"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 123 | } |
| 124 | std::string slot_suffix = device->GetCurrentSlot(); |
| 125 | if (slot_suffix.empty()) { |
David Anderson | 79ab0e3 | 2018-08-14 16:21:50 -0700 | [diff] [blame^] | 126 | return device->WriteOkay("no"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 127 | } |
David Anderson | 79ab0e3 | 2018-08-14 16:21:50 -0700 | [diff] [blame^] | 128 | std::string partition_name = args[0] + slot_suffix; |
| 129 | if (FindPhysicalPartition(partition_name) || |
| 130 | LogicalPartitionExists(partition_name, slot_suffix)) { |
| 131 | return device->WriteOkay("yes"); |
| 132 | } |
| 133 | return device->WriteOkay("no"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 134 | } |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 135 | |
| 136 | bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args) { |
| 137 | if (args.size() < 1) { |
| 138 | return device->WriteFail("Missing argument"); |
| 139 | } |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 140 | // Zero-length partitions cannot be created through device-mapper, so we |
| 141 | // special case them here. |
| 142 | bool is_zero_length; |
| 143 | if (LogicalPartitionExists(args[0], device->GetCurrentSlot(), &is_zero_length) && |
| 144 | is_zero_length) { |
| 145 | return device->WriteOkay("0"); |
| 146 | } |
| 147 | // Otherwise, open the partition as normal. |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 148 | PartitionHandle handle; |
| 149 | if (!OpenPartition(device, args[0], &handle)) { |
| 150 | return device->WriteFail("Could not open partition"); |
| 151 | } |
| 152 | uint64_t size = get_block_device_size(handle.fd()); |
| 153 | return device->WriteOkay(android::base::StringPrintf("%" PRIX64, size)); |
| 154 | } |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 155 | |
| 156 | bool GetPartitionIsLogical(FastbootDevice* device, const std::vector<std::string>& args) { |
| 157 | if (args.size() < 1) { |
| 158 | return device->WriteFail("Missing argument"); |
| 159 | } |
| 160 | // Note: if a partition name is in both the GPT and the super partition, we |
| 161 | // return "true", to be consistent with prefering to flash logical partitions |
| 162 | // over physical ones. |
| 163 | std::string partition_name = args[0]; |
| 164 | if (LogicalPartitionExists(partition_name, device->GetCurrentSlot())) { |
| 165 | return device->WriteOkay("yes"); |
| 166 | } |
| 167 | if (FindPhysicalPartition(partition_name)) { |
| 168 | return device->WriteOkay("no"); |
| 169 | } |
| 170 | return device->WriteFail("Partition not found"); |
| 171 | } |
David Anderson | d9ba061 | 2018-08-02 11:05:00 -0700 | [diff] [blame] | 172 | |
| 173 | bool GetIsUserspace(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 174 | return device->WriteOkay("yes"); |
| 175 | } |