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 | |
| 19 | #include <android-base/file.h> |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android-base/properties.h> |
| 22 | #include <android-base/stringprintf.h> |
| 23 | #include <android-base/strings.h> |
| 24 | #include <ext4_utils/ext4_utils.h> |
| 25 | |
| 26 | #include "fastboot_device.h" |
| 27 | #include "utility.h" |
| 28 | |
| 29 | using ::android::hardware::boot::V1_0::BoolResult; |
| 30 | using ::android::hardware::boot::V1_0::Slot; |
| 31 | |
| 32 | constexpr int kMaxDownloadSizeDefault = 0x20000000; |
| 33 | constexpr char kFastbootProtocolVersion[] = "0.4"; |
| 34 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 35 | bool GetVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 36 | return device->WriteOkay(kFastbootProtocolVersion); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 37 | } |
| 38 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 39 | bool GetBootloaderVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 40 | return device->WriteOkay(android::base::GetProperty("ro.bootloader", "")); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 41 | } |
| 42 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 43 | bool GetBasebandVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 44 | return device->WriteOkay(android::base::GetProperty("ro.build.expect.baseband", "")); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 45 | } |
| 46 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 47 | bool GetProduct(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 48 | return device->WriteOkay(android::base::GetProperty("ro.product.device", "")); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 49 | } |
| 50 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 51 | bool GetSerial(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 52 | return device->WriteOkay(android::base::GetProperty("ro.serialno", "")); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 53 | } |
| 54 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 55 | bool GetSecure(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 56 | return device->WriteOkay(android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 57 | } |
| 58 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 59 | bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 60 | std::string suffix = device->GetCurrentSlot(); |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 61 | std::string slot = suffix.size() == 2 ? suffix.substr(1) : suffix; |
| 62 | return device->WriteOkay(slot); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 63 | } |
| 64 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 65 | bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 66 | auto boot_control_hal = device->boot_control_hal(); |
| 67 | if (!boot_control_hal) { |
| 68 | return "0"; |
| 69 | } |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 70 | return device->WriteOkay(std::to_string(boot_control_hal->getNumberSlots())); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 71 | } |
| 72 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 73 | bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 74 | if (args.empty()) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 75 | return device->WriteFail("Missing argument"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 76 | } |
| 77 | Slot slot; |
| 78 | if (!GetSlotNumber(args[0], &slot)) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 79 | return device->WriteFail("Invalid slot"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 80 | } |
| 81 | auto boot_control_hal = device->boot_control_hal(); |
| 82 | if (!boot_control_hal) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 83 | return device->WriteFail("Device has no slots"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 84 | } |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 85 | if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) { |
| 86 | return device->WriteOkay("no"); |
| 87 | } |
| 88 | return device->WriteOkay("yes"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 89 | } |
| 90 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 91 | bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 92 | if (args.empty()) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 93 | return device->WriteFail("Missing argument"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 94 | } |
| 95 | Slot slot; |
| 96 | if (!GetSlotNumber(args[0], &slot)) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 97 | return device->WriteFail("Invalid slot"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 98 | } |
| 99 | auto boot_control_hal = device->boot_control_hal(); |
| 100 | if (!boot_control_hal) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 101 | return device->WriteFail("Device has no slots"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 102 | } |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 103 | if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) { |
| 104 | return device->WriteOkay("yes"); |
| 105 | } |
| 106 | return device->WriteOkay("no"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 107 | } |
| 108 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 109 | bool GetMaxDownloadSize(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 110 | return device->WriteOkay(std::to_string(kMaxDownloadSizeDefault)); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 111 | } |
| 112 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 113 | bool GetUnlocked(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 114 | return device->WriteOkay("yes"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 115 | } |
| 116 | |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 117 | bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args) { |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 118 | if (args.empty()) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 119 | return device->WriteFail("Missing argument"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 120 | } |
| 121 | std::string slot_suffix = device->GetCurrentSlot(); |
| 122 | if (slot_suffix.empty()) { |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 123 | return device->WriteFail("Invalid slot"); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 124 | } |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame^] | 125 | std::string result = (args[0] == "userdata" ? "no" : "yes"); |
| 126 | return device->WriteOkay(result); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 127 | } |