blob: 65cfea327c0249aa9c27ddf8ecda21d60350df3f [file] [log] [blame]
Hridya Valsaraju31d2c262018-07-20 13:35:50 -07001/*
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 Anderson12211d12018-07-24 15:21:20 -070019#include <inttypes.h>
20
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070021#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 Anderson12211d12018-07-24 15:21:20 -070029#include "flashing.h"
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070030#include "utility.h"
31
32using ::android::hardware::boot::V1_0::BoolResult;
33using ::android::hardware::boot::V1_0::Slot;
34
35constexpr int kMaxDownloadSizeDefault = 0x20000000;
36constexpr char kFastbootProtocolVersion[] = "0.4";
37
David Anderson856b7ec2018-08-08 17:58:56 -070038bool GetVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
39 return device->WriteOkay(kFastbootProtocolVersion);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070040}
41
David Anderson856b7ec2018-08-08 17:58:56 -070042bool GetBootloaderVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
43 return device->WriteOkay(android::base::GetProperty("ro.bootloader", ""));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070044}
45
David Anderson856b7ec2018-08-08 17:58:56 -070046bool GetBasebandVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
47 return device->WriteOkay(android::base::GetProperty("ro.build.expect.baseband", ""));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070048}
49
David Anderson856b7ec2018-08-08 17:58:56 -070050bool GetProduct(FastbootDevice* device, const std::vector<std::string>& /* args */) {
51 return device->WriteOkay(android::base::GetProperty("ro.product.device", ""));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070052}
53
David Anderson856b7ec2018-08-08 17:58:56 -070054bool GetSerial(FastbootDevice* device, const std::vector<std::string>& /* args */) {
55 return device->WriteOkay(android::base::GetProperty("ro.serialno", ""));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070056}
57
David Anderson856b7ec2018-08-08 17:58:56 -070058bool GetSecure(FastbootDevice* device, const std::vector<std::string>& /* args */) {
59 return device->WriteOkay(android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070060}
61
David Anderson856b7ec2018-08-08 17:58:56 -070062bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070063 std::string suffix = device->GetCurrentSlot();
David Anderson856b7ec2018-08-08 17:58:56 -070064 std::string slot = suffix.size() == 2 ? suffix.substr(1) : suffix;
65 return device->WriteOkay(slot);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070066}
67
David Anderson856b7ec2018-08-08 17:58:56 -070068bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070069 auto boot_control_hal = device->boot_control_hal();
70 if (!boot_control_hal) {
71 return "0";
72 }
David Anderson856b7ec2018-08-08 17:58:56 -070073 return device->WriteOkay(std::to_string(boot_control_hal->getNumberSlots()));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070074}
75
David Anderson856b7ec2018-08-08 17:58:56 -070076bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070077 if (args.empty()) {
David Anderson856b7ec2018-08-08 17:58:56 -070078 return device->WriteFail("Missing argument");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070079 }
80 Slot slot;
81 if (!GetSlotNumber(args[0], &slot)) {
David Anderson856b7ec2018-08-08 17:58:56 -070082 return device->WriteFail("Invalid slot");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070083 }
84 auto boot_control_hal = device->boot_control_hal();
85 if (!boot_control_hal) {
David Anderson856b7ec2018-08-08 17:58:56 -070086 return device->WriteFail("Device has no slots");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070087 }
David Anderson856b7ec2018-08-08 17:58:56 -070088 if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) {
89 return device->WriteOkay("no");
90 }
91 return device->WriteOkay("yes");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070092}
93
David Anderson856b7ec2018-08-08 17:58:56 -070094bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070095 if (args.empty()) {
David Anderson856b7ec2018-08-08 17:58:56 -070096 return device->WriteFail("Missing argument");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070097 }
98 Slot slot;
99 if (!GetSlotNumber(args[0], &slot)) {
David Anderson856b7ec2018-08-08 17:58:56 -0700100 return device->WriteFail("Invalid slot");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700101 }
102 auto boot_control_hal = device->boot_control_hal();
103 if (!boot_control_hal) {
David Anderson856b7ec2018-08-08 17:58:56 -0700104 return device->WriteFail("Device has no slots");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700105 }
David Anderson856b7ec2018-08-08 17:58:56 -0700106 if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) {
107 return device->WriteOkay("yes");
108 }
109 return device->WriteOkay("no");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700110}
111
David Anderson856b7ec2018-08-08 17:58:56 -0700112bool GetMaxDownloadSize(FastbootDevice* device, const std::vector<std::string>& /* args */) {
113 return device->WriteOkay(std::to_string(kMaxDownloadSizeDefault));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700114}
115
David Anderson856b7ec2018-08-08 17:58:56 -0700116bool GetUnlocked(FastbootDevice* device, const std::vector<std::string>& /* args */) {
117 return device->WriteOkay("yes");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700118}
119
David Anderson856b7ec2018-08-08 17:58:56 -0700120bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700121 if (args.empty()) {
David Anderson856b7ec2018-08-08 17:58:56 -0700122 return device->WriteFail("Missing argument");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700123 }
124 std::string slot_suffix = device->GetCurrentSlot();
125 if (slot_suffix.empty()) {
David Anderson79ab0e32018-08-14 16:21:50 -0700126 return device->WriteOkay("no");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700127 }
David Anderson79ab0e32018-08-14 16:21:50 -0700128 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 Valsaraju31d2c262018-07-20 13:35:50 -0700134}
David Anderson12211d12018-07-24 15:21:20 -0700135
136bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args) {
137 if (args.size() < 1) {
138 return device->WriteFail("Missing argument");
139 }
David Anderson88ef0b12018-08-09 10:40:00 -0700140 // 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 Anderson12211d12018-07-24 15:21:20 -0700148 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 Anderson0d4277d2018-07-31 13:27:37 -0700155
156bool 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 Andersond9ba0612018-08-02 11:05:00 -0700172
173bool GetIsUserspace(FastbootDevice* device, const std::vector<std::string>& /* args */) {
174 return device->WriteOkay("yes");
175}