blob: 01415d72b83ccfc856f2f128e755d944f1d6969d [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>
Hridya Valsaraju47658ca2018-09-28 11:41:22 -070027#include <healthhalutils/HealthHalUtils.h>
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070028
29#include "fastboot_device.h"
David Anderson12211d12018-07-24 15:21:20 -070030#include "flashing.h"
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070031#include "utility.h"
32
33using ::android::hardware::boot::V1_0::BoolResult;
34using ::android::hardware::boot::V1_0::Slot;
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -070035using ::android::hardware::fastboot::V1_0::FileSystemType;
36using ::android::hardware::fastboot::V1_0::Result;
37using ::android::hardware::fastboot::V1_0::Status;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070038
39constexpr int kMaxDownloadSizeDefault = 0x20000000;
40constexpr char kFastbootProtocolVersion[] = "0.4";
41
David Anderson1fb3fd72018-08-31 14:40:22 -070042bool GetVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
43 std::string* message) {
44 *message = kFastbootProtocolVersion;
45 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070046}
47
David Anderson1fb3fd72018-08-31 14:40:22 -070048bool GetBootloaderVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
49 std::string* message) {
50 *message = android::base::GetProperty("ro.bootloader", "");
51 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070052}
53
David Anderson1fb3fd72018-08-31 14:40:22 -070054bool GetBasebandVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
55 std::string* message) {
56 *message = android::base::GetProperty("ro.build.expect.baseband", "");
57 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070058}
59
David Anderson1fb3fd72018-08-31 14:40:22 -070060bool GetProduct(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
61 std::string* message) {
62 *message = android::base::GetProperty("ro.product.device", "");
63 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070064}
65
David Anderson1fb3fd72018-08-31 14:40:22 -070066bool GetSerial(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
67 std::string* message) {
68 *message = android::base::GetProperty("ro.serialno", "");
69 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070070}
71
David Anderson1fb3fd72018-08-31 14:40:22 -070072bool GetSecure(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
73 std::string* message) {
74 *message = android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no";
75 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070076}
77
Hridya Valsaraju4af80902018-09-26 13:08:16 -070078bool GetVariant(FastbootDevice* device, const std::vector<std::string>& /* args */,
79 std::string* message) {
80 auto fastboot_hal = device->fastboot_hal();
81 if (!fastboot_hal) {
82 *message = "Fastboot HAL not found";
83 return false;
84 }
85
86 Result ret;
87 auto ret_val = fastboot_hal->getVariant([&](std::string device_variant, Result result) {
88 *message = device_variant;
89 ret = result;
90 });
91 if (!ret_val.isOk() || ret.status != Status::SUCCESS) {
92 *message = "Unable to get device variant";
93 return false;
94 }
95
96 return true;
97}
98
Hridya Valsaraju7c9bbe92018-09-27 10:41:01 -070099bool GetOffModeChargeState(FastbootDevice* device, const std::vector<std::string>& /* args */,
100 std::string* message) {
101 auto fastboot_hal = device->fastboot_hal();
102 if (!fastboot_hal) {
103 *message = "Fastboot HAL not found";
104 return false;
105 }
106
107 Result ret;
108 auto ret_val =
109 fastboot_hal->getOffModeChargeState([&](bool off_mode_charging_state, Result result) {
110 *message = off_mode_charging_state ? "1" : "0";
111 ret = result;
112 });
113 if (!ret_val.isOk() || (ret.status != Status::SUCCESS)) {
114 *message = "Unable to get off mode charge state";
115 return false;
116 }
117
118 return true;
119}
120
Hridya Valsaraju47658ca2018-09-28 11:41:22 -0700121bool GetBatteryVoltage(FastbootDevice* device, const std::vector<std::string>& /* args */,
122 std::string* message) {
123 using android::hardware::health::V2_0::HealthInfo;
124 using android::hardware::health::V2_0::Result;
125
126 auto health_hal = device->health_hal();
127 if (!health_hal) {
128 *message = "Health HAL not found";
129 return false;
130 }
131
132 Result ret;
133 auto ret_val = health_hal->getHealthInfo([&](Result result, HealthInfo info) {
134 *message = std::to_string(info.legacy.batteryVoltage);
135 ret = result;
136 });
137 if (!ret_val.isOk() || (ret != Result::SUCCESS)) {
138 *message = "Unable to get battery voltage";
139 return false;
140 }
141
142 return true;
143}
144
David Anderson1fb3fd72018-08-31 14:40:22 -0700145bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */,
146 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700147 std::string suffix = device->GetCurrentSlot();
David Anderson1fb3fd72018-08-31 14:40:22 -0700148 *message = suffix.size() == 2 ? suffix.substr(1) : suffix;
149 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700150}
151
David Anderson1fb3fd72018-08-31 14:40:22 -0700152bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */,
153 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700154 auto boot_control_hal = device->boot_control_hal();
155 if (!boot_control_hal) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700156 *message = "0";
157 } else {
158 *message = std::to_string(boot_control_hal->getNumberSlots());
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700159 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700160 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700161}
162
David Anderson1fb3fd72018-08-31 14:40:22 -0700163bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args,
164 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700165 if (args.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700166 *message = "Missing argument";
167 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700168 }
169 Slot slot;
170 if (!GetSlotNumber(args[0], &slot)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700171 *message = "Invalid slot";
172 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700173 }
174 auto boot_control_hal = device->boot_control_hal();
175 if (!boot_control_hal) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700176 *message = "Device has no slots";
177 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700178 }
David Anderson856b7ec2018-08-08 17:58:56 -0700179 if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700180 *message = "no";
181 } else {
182 *message = "yes";
David Anderson856b7ec2018-08-08 17:58:56 -0700183 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700184 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700185}
186
David Anderson1fb3fd72018-08-31 14:40:22 -0700187bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args,
188 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700189 if (args.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700190 *message = "Missing argument";
191 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700192 }
193 Slot slot;
194 if (!GetSlotNumber(args[0], &slot)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700195 *message = "Invalid slot";
196 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700197 }
198 auto boot_control_hal = device->boot_control_hal();
199 if (!boot_control_hal) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700200 *message = "Device has no slots";
201 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700202 }
David Anderson856b7ec2018-08-08 17:58:56 -0700203 if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700204 *message = "yes";
205 } else {
206 *message = "no";
David Anderson856b7ec2018-08-08 17:58:56 -0700207 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700208 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700209}
210
David Anderson1fb3fd72018-08-31 14:40:22 -0700211bool GetMaxDownloadSize(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
212 std::string* message) {
David Anderson28b81cd2018-09-04 16:51:29 -0700213 *message = android::base::StringPrintf("0x%X", kMaxDownloadSizeDefault);
David Anderson1fb3fd72018-08-31 14:40:22 -0700214 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700215}
216
David Anderson1fb3fd72018-08-31 14:40:22 -0700217bool GetUnlocked(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
218 std::string* message) {
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700219 *message = GetDeviceLockStatus() ? "no" : "yes";
David Anderson1fb3fd72018-08-31 14:40:22 -0700220 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700221}
222
David Anderson1fb3fd72018-08-31 14:40:22 -0700223bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args,
224 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700225 if (args.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700226 *message = "Missing argument";
227 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700228 }
229 std::string slot_suffix = device->GetCurrentSlot();
230 if (slot_suffix.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700231 *message = "no";
232 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700233 }
David Anderson79ab0e32018-08-14 16:21:50 -0700234 std::string partition_name = args[0] + slot_suffix;
235 if (FindPhysicalPartition(partition_name) ||
236 LogicalPartitionExists(partition_name, slot_suffix)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700237 *message = "yes";
238 } else {
239 *message = "no";
David Anderson79ab0e32018-08-14 16:21:50 -0700240 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700241 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700242}
David Anderson12211d12018-07-24 15:21:20 -0700243
David Anderson1fb3fd72018-08-31 14:40:22 -0700244bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args,
245 std::string* message) {
David Anderson12211d12018-07-24 15:21:20 -0700246 if (args.size() < 1) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700247 *message = "Missing argument";
248 return false;
David Anderson12211d12018-07-24 15:21:20 -0700249 }
David Anderson88ef0b12018-08-09 10:40:00 -0700250 // Zero-length partitions cannot be created through device-mapper, so we
251 // special case them here.
252 bool is_zero_length;
253 if (LogicalPartitionExists(args[0], device->GetCurrentSlot(), &is_zero_length) &&
254 is_zero_length) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700255 *message = "0";
256 return true;
David Anderson88ef0b12018-08-09 10:40:00 -0700257 }
258 // Otherwise, open the partition as normal.
David Anderson12211d12018-07-24 15:21:20 -0700259 PartitionHandle handle;
260 if (!OpenPartition(device, args[0], &handle)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700261 *message = "Could not open partition";
262 return false;
David Anderson12211d12018-07-24 15:21:20 -0700263 }
264 uint64_t size = get_block_device_size(handle.fd());
David Anderson47589672018-09-04 16:22:41 -0700265 *message = android::base::StringPrintf("0x%" PRIX64, size);
David Anderson1fb3fd72018-08-31 14:40:22 -0700266 return true;
David Anderson12211d12018-07-24 15:21:20 -0700267}
David Anderson0d4277d2018-07-31 13:27:37 -0700268
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -0700269bool GetPartitionType(FastbootDevice* device, const std::vector<std::string>& args,
270 std::string* message) {
271 if (args.size() < 1) {
272 *message = "Missing argument";
273 return false;
274 }
275 std::string partition_name = args[0];
276 auto fastboot_hal = device->fastboot_hal();
277 if (!fastboot_hal) {
278 *message = "Fastboot HAL not found";
279 return false;
280 }
281
282 FileSystemType type;
283 Result ret;
284 auto ret_val =
285 fastboot_hal->getPartitionType(args[0], [&](FileSystemType fs_type, Result result) {
286 type = fs_type;
287 ret = result;
288 });
289 if (!ret_val.isOk() || (ret.status != Status::SUCCESS)) {
290 *message = "Unable to retrieve partition type";
291 } else {
292 switch (type) {
293 case FileSystemType::RAW:
294 *message = "raw";
295 return true;
296 case FileSystemType::EXT4:
297 *message = "ext4";
298 return true;
299 case FileSystemType::F2FS:
300 *message = "f2fs";
301 return true;
302 default:
303 *message = "Unknown file system type";
304 }
305 }
306
307 return false;
308}
309
David Anderson1fb3fd72018-08-31 14:40:22 -0700310bool GetPartitionIsLogical(FastbootDevice* device, const std::vector<std::string>& args,
311 std::string* message) {
David Anderson0d4277d2018-07-31 13:27:37 -0700312 if (args.size() < 1) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700313 *message = "Missing argument";
314 return false;
David Anderson0d4277d2018-07-31 13:27:37 -0700315 }
316 // Note: if a partition name is in both the GPT and the super partition, we
317 // return "true", to be consistent with prefering to flash logical partitions
318 // over physical ones.
319 std::string partition_name = args[0];
320 if (LogicalPartitionExists(partition_name, device->GetCurrentSlot())) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700321 *message = "yes";
322 return true;
David Anderson0d4277d2018-07-31 13:27:37 -0700323 }
324 if (FindPhysicalPartition(partition_name)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700325 *message = "no";
326 return true;
David Anderson0d4277d2018-07-31 13:27:37 -0700327 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700328 *message = "Partition not found";
329 return false;
David Anderson0d4277d2018-07-31 13:27:37 -0700330}
David Andersond9ba0612018-08-02 11:05:00 -0700331
David Anderson1fb3fd72018-08-31 14:40:22 -0700332bool GetIsUserspace(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
333 std::string* message) {
334 *message = "yes";
335 return true;
David Andersond9ba0612018-08-02 11:05:00 -0700336}
David Anderson0f626632018-08-31 16:44:25 -0700337
338std::vector<std::vector<std::string>> GetAllPartitionArgsWithSlot(FastbootDevice* device) {
339 std::vector<std::vector<std::string>> args;
340 auto partitions = ListPartitions(device);
341 for (const auto& partition : partitions) {
342 args.emplace_back(std::initializer_list<std::string>{partition});
343 }
344 return args;
345}
346
347std::vector<std::vector<std::string>> GetAllPartitionArgsNoSlot(FastbootDevice* device) {
348 auto partitions = ListPartitions(device);
349
350 std::string slot_suffix = device->GetCurrentSlot();
351 if (!slot_suffix.empty()) {
352 auto names = std::move(partitions);
353 for (const auto& name : names) {
354 std::string slotless_name = name;
355 if (android::base::EndsWith(name, "_a") || android::base::EndsWith(name, "_b")) {
356 slotless_name = name.substr(0, name.rfind("_"));
357 }
358 if (std::find(partitions.begin(), partitions.end(), slotless_name) ==
359 partitions.end()) {
360 partitions.emplace_back(slotless_name);
361 }
362 }
363 }
364
365 std::vector<std::vector<std::string>> args;
366 for (const auto& partition : partitions) {
367 args.emplace_back(std::initializer_list<std::string>{partition});
368 }
369 return args;
370}
David Andersonc091c172018-09-04 18:11:03 -0700371
372bool GetHardwareRevision(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
373 std::string* message) {
374 *message = android::base::GetProperty("ro.revision", "");
375 return true;
376}