blob: 77cd4bc25a28ee886a18360a6d5eec1c2830450d [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;
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -070034using ::android::hardware::fastboot::V1_0::FileSystemType;
35using ::android::hardware::fastboot::V1_0::Result;
36using ::android::hardware::fastboot::V1_0::Status;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070037
38constexpr int kMaxDownloadSizeDefault = 0x20000000;
39constexpr char kFastbootProtocolVersion[] = "0.4";
40
David Anderson1fb3fd72018-08-31 14:40:22 -070041bool GetVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
42 std::string* message) {
43 *message = kFastbootProtocolVersion;
44 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070045}
46
David Anderson1fb3fd72018-08-31 14:40:22 -070047bool GetBootloaderVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
48 std::string* message) {
49 *message = android::base::GetProperty("ro.bootloader", "");
50 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070051}
52
David Anderson1fb3fd72018-08-31 14:40:22 -070053bool 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 Valsaraju31d2c262018-07-20 13:35:50 -070057}
58
David Anderson1fb3fd72018-08-31 14:40:22 -070059bool 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 Valsaraju31d2c262018-07-20 13:35:50 -070063}
64
David Anderson1fb3fd72018-08-31 14:40:22 -070065bool GetSerial(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
66 std::string* message) {
67 *message = android::base::GetProperty("ro.serialno", "");
68 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070069}
70
David Anderson1fb3fd72018-08-31 14:40:22 -070071bool 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 Valsaraju31d2c262018-07-20 13:35:50 -070075}
76
Hridya Valsaraju4af80902018-09-26 13:08:16 -070077bool GetVariant(FastbootDevice* device, const std::vector<std::string>& /* args */,
78 std::string* message) {
79 auto fastboot_hal = device->fastboot_hal();
80 if (!fastboot_hal) {
81 *message = "Fastboot HAL not found";
82 return false;
83 }
84
85 Result ret;
86 auto ret_val = fastboot_hal->getVariant([&](std::string device_variant, Result result) {
87 *message = device_variant;
88 ret = result;
89 });
90 if (!ret_val.isOk() || ret.status != Status::SUCCESS) {
91 *message = "Unable to get device variant";
92 return false;
93 }
94
95 return true;
96}
97
David Anderson1fb3fd72018-08-31 14:40:22 -070098bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */,
99 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700100 std::string suffix = device->GetCurrentSlot();
David Anderson1fb3fd72018-08-31 14:40:22 -0700101 *message = suffix.size() == 2 ? suffix.substr(1) : suffix;
102 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700103}
104
David Anderson1fb3fd72018-08-31 14:40:22 -0700105bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */,
106 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700107 auto boot_control_hal = device->boot_control_hal();
108 if (!boot_control_hal) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700109 *message = "0";
110 } else {
111 *message = std::to_string(boot_control_hal->getNumberSlots());
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700112 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700113 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700114}
115
David Anderson1fb3fd72018-08-31 14:40:22 -0700116bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args,
117 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700118 if (args.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700119 *message = "Missing argument";
120 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700121 }
122 Slot slot;
123 if (!GetSlotNumber(args[0], &slot)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700124 *message = "Invalid slot";
125 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700126 }
127 auto boot_control_hal = device->boot_control_hal();
128 if (!boot_control_hal) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700129 *message = "Device has no slots";
130 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700131 }
David Anderson856b7ec2018-08-08 17:58:56 -0700132 if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700133 *message = "no";
134 } else {
135 *message = "yes";
David Anderson856b7ec2018-08-08 17:58:56 -0700136 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700137 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700138}
139
David Anderson1fb3fd72018-08-31 14:40:22 -0700140bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args,
141 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700142 if (args.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700143 *message = "Missing argument";
144 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700145 }
146 Slot slot;
147 if (!GetSlotNumber(args[0], &slot)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700148 *message = "Invalid slot";
149 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700150 }
151 auto boot_control_hal = device->boot_control_hal();
152 if (!boot_control_hal) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700153 *message = "Device has no slots";
154 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700155 }
David Anderson856b7ec2018-08-08 17:58:56 -0700156 if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700157 *message = "yes";
158 } else {
159 *message = "no";
David Anderson856b7ec2018-08-08 17:58:56 -0700160 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700161 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700162}
163
David Anderson1fb3fd72018-08-31 14:40:22 -0700164bool GetMaxDownloadSize(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
165 std::string* message) {
David Anderson28b81cd2018-09-04 16:51:29 -0700166 *message = android::base::StringPrintf("0x%X", kMaxDownloadSizeDefault);
David Anderson1fb3fd72018-08-31 14:40:22 -0700167 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700168}
169
David Anderson1fb3fd72018-08-31 14:40:22 -0700170bool GetUnlocked(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
171 std::string* message) {
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700172 *message = GetDeviceLockStatus() ? "no" : "yes";
David Anderson1fb3fd72018-08-31 14:40:22 -0700173 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700174}
175
David Anderson1fb3fd72018-08-31 14:40:22 -0700176bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args,
177 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700178 if (args.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700179 *message = "Missing argument";
180 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700181 }
182 std::string slot_suffix = device->GetCurrentSlot();
183 if (slot_suffix.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700184 *message = "no";
185 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700186 }
David Anderson79ab0e32018-08-14 16:21:50 -0700187 std::string partition_name = args[0] + slot_suffix;
188 if (FindPhysicalPartition(partition_name) ||
189 LogicalPartitionExists(partition_name, slot_suffix)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700190 *message = "yes";
191 } else {
192 *message = "no";
David Anderson79ab0e32018-08-14 16:21:50 -0700193 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700194 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700195}
David Anderson12211d12018-07-24 15:21:20 -0700196
David Anderson1fb3fd72018-08-31 14:40:22 -0700197bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args,
198 std::string* message) {
David Anderson12211d12018-07-24 15:21:20 -0700199 if (args.size() < 1) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700200 *message = "Missing argument";
201 return false;
David Anderson12211d12018-07-24 15:21:20 -0700202 }
David Anderson88ef0b12018-08-09 10:40:00 -0700203 // Zero-length partitions cannot be created through device-mapper, so we
204 // special case them here.
205 bool is_zero_length;
206 if (LogicalPartitionExists(args[0], device->GetCurrentSlot(), &is_zero_length) &&
207 is_zero_length) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700208 *message = "0";
209 return true;
David Anderson88ef0b12018-08-09 10:40:00 -0700210 }
211 // Otherwise, open the partition as normal.
David Anderson12211d12018-07-24 15:21:20 -0700212 PartitionHandle handle;
213 if (!OpenPartition(device, args[0], &handle)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700214 *message = "Could not open partition";
215 return false;
David Anderson12211d12018-07-24 15:21:20 -0700216 }
217 uint64_t size = get_block_device_size(handle.fd());
David Anderson47589672018-09-04 16:22:41 -0700218 *message = android::base::StringPrintf("0x%" PRIX64, size);
David Anderson1fb3fd72018-08-31 14:40:22 -0700219 return true;
David Anderson12211d12018-07-24 15:21:20 -0700220}
David Anderson0d4277d2018-07-31 13:27:37 -0700221
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -0700222bool GetPartitionType(FastbootDevice* device, const std::vector<std::string>& args,
223 std::string* message) {
224 if (args.size() < 1) {
225 *message = "Missing argument";
226 return false;
227 }
228 std::string partition_name = args[0];
229 auto fastboot_hal = device->fastboot_hal();
230 if (!fastboot_hal) {
231 *message = "Fastboot HAL not found";
232 return false;
233 }
234
235 FileSystemType type;
236 Result ret;
237 auto ret_val =
238 fastboot_hal->getPartitionType(args[0], [&](FileSystemType fs_type, Result result) {
239 type = fs_type;
240 ret = result;
241 });
242 if (!ret_val.isOk() || (ret.status != Status::SUCCESS)) {
243 *message = "Unable to retrieve partition type";
244 } else {
245 switch (type) {
246 case FileSystemType::RAW:
247 *message = "raw";
248 return true;
249 case FileSystemType::EXT4:
250 *message = "ext4";
251 return true;
252 case FileSystemType::F2FS:
253 *message = "f2fs";
254 return true;
255 default:
256 *message = "Unknown file system type";
257 }
258 }
259
260 return false;
261}
262
David Anderson1fb3fd72018-08-31 14:40:22 -0700263bool GetPartitionIsLogical(FastbootDevice* device, const std::vector<std::string>& args,
264 std::string* message) {
David Anderson0d4277d2018-07-31 13:27:37 -0700265 if (args.size() < 1) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700266 *message = "Missing argument";
267 return false;
David Anderson0d4277d2018-07-31 13:27:37 -0700268 }
269 // Note: if a partition name is in both the GPT and the super partition, we
270 // return "true", to be consistent with prefering to flash logical partitions
271 // over physical ones.
272 std::string partition_name = args[0];
273 if (LogicalPartitionExists(partition_name, device->GetCurrentSlot())) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700274 *message = "yes";
275 return true;
David Anderson0d4277d2018-07-31 13:27:37 -0700276 }
277 if (FindPhysicalPartition(partition_name)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700278 *message = "no";
279 return true;
David Anderson0d4277d2018-07-31 13:27:37 -0700280 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700281 *message = "Partition not found";
282 return false;
David Anderson0d4277d2018-07-31 13:27:37 -0700283}
David Andersond9ba0612018-08-02 11:05:00 -0700284
David Anderson1fb3fd72018-08-31 14:40:22 -0700285bool GetIsUserspace(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
286 std::string* message) {
287 *message = "yes";
288 return true;
David Andersond9ba0612018-08-02 11:05:00 -0700289}
David Anderson0f626632018-08-31 16:44:25 -0700290
291std::vector<std::vector<std::string>> GetAllPartitionArgsWithSlot(FastbootDevice* device) {
292 std::vector<std::vector<std::string>> args;
293 auto partitions = ListPartitions(device);
294 for (const auto& partition : partitions) {
295 args.emplace_back(std::initializer_list<std::string>{partition});
296 }
297 return args;
298}
299
300std::vector<std::vector<std::string>> GetAllPartitionArgsNoSlot(FastbootDevice* device) {
301 auto partitions = ListPartitions(device);
302
303 std::string slot_suffix = device->GetCurrentSlot();
304 if (!slot_suffix.empty()) {
305 auto names = std::move(partitions);
306 for (const auto& name : names) {
307 std::string slotless_name = name;
308 if (android::base::EndsWith(name, "_a") || android::base::EndsWith(name, "_b")) {
309 slotless_name = name.substr(0, name.rfind("_"));
310 }
311 if (std::find(partitions.begin(), partitions.end(), slotless_name) ==
312 partitions.end()) {
313 partitions.emplace_back(slotless_name);
314 }
315 }
316 }
317
318 std::vector<std::vector<std::string>> args;
319 for (const auto& partition : partitions) {
320 args.emplace_back(std::initializer_list<std::string>{partition});
321 }
322 return args;
323}
David Andersonc091c172018-09-04 18:11:03 -0700324
325bool GetHardwareRevision(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
326 std::string* message) {
327 *message = android::base::GetProperty("ro.revision", "");
328 return true;
329}