blob: 601af3446336ff65d0c86d691213669dcec1ea46 [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>
David Anderson90fe0a42018-11-05 18:01:32 -080027#include <fs_mgr.h>
Hridya Valsaraju47658ca2018-09-28 11:41:22 -070028#include <healthhalutils/HealthHalUtils.h>
David Anderson90fe0a42018-11-05 18:01:32 -080029#include <liblp/liblp.h>
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070030
31#include "fastboot_device.h"
David Anderson12211d12018-07-24 15:21:20 -070032#include "flashing.h"
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070033#include "utility.h"
34
35using ::android::hardware::boot::V1_0::BoolResult;
36using ::android::hardware::boot::V1_0::Slot;
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -070037using ::android::hardware::fastboot::V1_0::FileSystemType;
38using ::android::hardware::fastboot::V1_0::Result;
39using ::android::hardware::fastboot::V1_0::Status;
David Anderson90fe0a42018-11-05 18:01:32 -080040using namespace android::fs_mgr;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070041
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070042constexpr char kFastbootProtocolVersion[] = "0.4";
43
David Anderson1fb3fd72018-08-31 14:40:22 -070044bool GetVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
45 std::string* message) {
46 *message = kFastbootProtocolVersion;
47 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070048}
49
David Anderson1fb3fd72018-08-31 14:40:22 -070050bool GetBootloaderVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
51 std::string* message) {
52 *message = android::base::GetProperty("ro.bootloader", "");
53 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070054}
55
David Anderson1fb3fd72018-08-31 14:40:22 -070056bool GetBasebandVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
57 std::string* message) {
58 *message = android::base::GetProperty("ro.build.expect.baseband", "");
59 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070060}
61
David Anderson1fb3fd72018-08-31 14:40:22 -070062bool GetProduct(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
63 std::string* message) {
64 *message = android::base::GetProperty("ro.product.device", "");
65 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070066}
67
David Anderson1fb3fd72018-08-31 14:40:22 -070068bool GetSerial(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
69 std::string* message) {
70 *message = android::base::GetProperty("ro.serialno", "");
71 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070072}
73
David Anderson1fb3fd72018-08-31 14:40:22 -070074bool GetSecure(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
75 std::string* message) {
76 *message = android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no";
77 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070078}
79
Hridya Valsaraju4af80902018-09-26 13:08:16 -070080bool GetVariant(FastbootDevice* device, const std::vector<std::string>& /* args */,
81 std::string* message) {
82 auto fastboot_hal = device->fastboot_hal();
83 if (!fastboot_hal) {
84 *message = "Fastboot HAL not found";
85 return false;
86 }
87
88 Result ret;
89 auto ret_val = fastboot_hal->getVariant([&](std::string device_variant, Result result) {
90 *message = device_variant;
91 ret = result;
92 });
93 if (!ret_val.isOk() || ret.status != Status::SUCCESS) {
94 *message = "Unable to get device variant";
95 return false;
96 }
97
98 return true;
99}
100
Hridya Valsarajua534a5a2018-10-03 15:53:22 -0700101bool GetBatteryVoltageHelper(FastbootDevice* device, int32_t* battery_voltage) {
102 using android::hardware::health::V2_0::HealthInfo;
103 using android::hardware::health::V2_0::Result;
104
105 auto health_hal = device->health_hal();
106 if (!health_hal) {
107 return false;
108 }
109
110 Result ret;
111 auto ret_val = health_hal->getHealthInfo([&](Result result, HealthInfo info) {
112 *battery_voltage = info.legacy.batteryVoltage;
113 ret = result;
114 });
115 if (!ret_val.isOk() || (ret != Result::SUCCESS)) {
116 return false;
117 }
118
119 return true;
120}
121
122bool GetBatterySoCOk(FastbootDevice* device, const std::vector<std::string>& /* args */,
123 std::string* message) {
124 int32_t battery_voltage = 0;
125 if (!GetBatteryVoltageHelper(device, &battery_voltage)) {
126 *message = "Unable to read battery voltage";
127 return false;
128 }
129
130 auto fastboot_hal = device->fastboot_hal();
131 if (!fastboot_hal) {
132 *message = "Fastboot HAL not found";
133 return false;
134 }
135
136 Result ret;
137 auto ret_val = fastboot_hal->getBatteryVoltageFlashingThreshold(
138 [&](int32_t voltage_threshold, Result result) {
139 *message = battery_voltage >= voltage_threshold ? "yes" : "no";
140 ret = result;
141 });
142
143 if (!ret_val.isOk() || ret.status != Status::SUCCESS) {
144 *message = "Unable to get battery voltage flashing threshold";
145 return false;
146 }
147
148 return true;
149}
150
Hridya Valsaraju7c9bbe92018-09-27 10:41:01 -0700151bool GetOffModeChargeState(FastbootDevice* device, const std::vector<std::string>& /* args */,
152 std::string* message) {
153 auto fastboot_hal = device->fastboot_hal();
154 if (!fastboot_hal) {
155 *message = "Fastboot HAL not found";
156 return false;
157 }
158
159 Result ret;
160 auto ret_val =
161 fastboot_hal->getOffModeChargeState([&](bool off_mode_charging_state, Result result) {
162 *message = off_mode_charging_state ? "1" : "0";
163 ret = result;
164 });
165 if (!ret_val.isOk() || (ret.status != Status::SUCCESS)) {
166 *message = "Unable to get off mode charge state";
167 return false;
168 }
169
170 return true;
171}
172
Hridya Valsaraju47658ca2018-09-28 11:41:22 -0700173bool GetBatteryVoltage(FastbootDevice* device, const std::vector<std::string>& /* args */,
174 std::string* message) {
Hridya Valsarajua534a5a2018-10-03 15:53:22 -0700175 int32_t battery_voltage = 0;
176 if (GetBatteryVoltageHelper(device, &battery_voltage)) {
177 *message = std::to_string(battery_voltage);
178 return true;
Hridya Valsaraju47658ca2018-09-28 11:41:22 -0700179 }
Hridya Valsarajua534a5a2018-10-03 15:53:22 -0700180 *message = "Unable to get battery voltage";
181 return false;
Hridya Valsaraju47658ca2018-09-28 11:41:22 -0700182}
183
David Anderson1fb3fd72018-08-31 14:40:22 -0700184bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */,
185 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700186 std::string suffix = device->GetCurrentSlot();
David Anderson1fb3fd72018-08-31 14:40:22 -0700187 *message = suffix.size() == 2 ? suffix.substr(1) : suffix;
188 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700189}
190
David Anderson1fb3fd72018-08-31 14:40:22 -0700191bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */,
192 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700193 auto boot_control_hal = device->boot_control_hal();
194 if (!boot_control_hal) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700195 *message = "0";
196 } else {
197 *message = std::to_string(boot_control_hal->getNumberSlots());
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700198 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700199 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700200}
201
David Anderson1fb3fd72018-08-31 14:40:22 -0700202bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args,
203 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700204 if (args.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700205 *message = "Missing argument";
206 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700207 }
208 Slot slot;
209 if (!GetSlotNumber(args[0], &slot)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700210 *message = "Invalid slot";
211 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700212 }
213 auto boot_control_hal = device->boot_control_hal();
214 if (!boot_control_hal) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700215 *message = "Device has no slots";
216 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700217 }
David Anderson856b7ec2018-08-08 17:58:56 -0700218 if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700219 *message = "no";
220 } else {
221 *message = "yes";
David Anderson856b7ec2018-08-08 17:58:56 -0700222 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700223 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700224}
225
David Anderson1fb3fd72018-08-31 14:40:22 -0700226bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args,
227 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700228 if (args.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700229 *message = "Missing argument";
230 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700231 }
232 Slot slot;
233 if (!GetSlotNumber(args[0], &slot)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700234 *message = "Invalid slot";
235 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700236 }
237 auto boot_control_hal = device->boot_control_hal();
238 if (!boot_control_hal) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700239 *message = "Device has no slots";
240 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700241 }
David Anderson856b7ec2018-08-08 17:58:56 -0700242 if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700243 *message = "yes";
244 } else {
245 *message = "no";
David Anderson856b7ec2018-08-08 17:58:56 -0700246 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700247 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700248}
249
David Anderson1fb3fd72018-08-31 14:40:22 -0700250bool GetMaxDownloadSize(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
251 std::string* message) {
David Anderson28b81cd2018-09-04 16:51:29 -0700252 *message = android::base::StringPrintf("0x%X", kMaxDownloadSizeDefault);
David Anderson1fb3fd72018-08-31 14:40:22 -0700253 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700254}
255
David Anderson1fb3fd72018-08-31 14:40:22 -0700256bool GetUnlocked(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
257 std::string* message) {
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700258 *message = GetDeviceLockStatus() ? "no" : "yes";
David Anderson1fb3fd72018-08-31 14:40:22 -0700259 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700260}
261
David Anderson1fb3fd72018-08-31 14:40:22 -0700262bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args,
263 std::string* message) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700264 if (args.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700265 *message = "Missing argument";
266 return false;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700267 }
268 std::string slot_suffix = device->GetCurrentSlot();
269 if (slot_suffix.empty()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700270 *message = "no";
271 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700272 }
David Anderson79ab0e32018-08-14 16:21:50 -0700273 std::string partition_name = args[0] + slot_suffix;
274 if (FindPhysicalPartition(partition_name) ||
275 LogicalPartitionExists(partition_name, slot_suffix)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700276 *message = "yes";
277 } else {
278 *message = "no";
David Anderson79ab0e32018-08-14 16:21:50 -0700279 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700280 return true;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700281}
David Anderson12211d12018-07-24 15:21:20 -0700282
David Anderson1fb3fd72018-08-31 14:40:22 -0700283bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args,
284 std::string* message) {
David Anderson12211d12018-07-24 15:21:20 -0700285 if (args.size() < 1) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700286 *message = "Missing argument";
287 return false;
David Anderson12211d12018-07-24 15:21:20 -0700288 }
David Anderson88ef0b12018-08-09 10:40:00 -0700289 // Zero-length partitions cannot be created through device-mapper, so we
290 // special case them here.
291 bool is_zero_length;
292 if (LogicalPartitionExists(args[0], device->GetCurrentSlot(), &is_zero_length) &&
293 is_zero_length) {
Hridya Valsaraju2a377da2018-10-09 10:03:51 -0700294 *message = "0x0";
David Anderson1fb3fd72018-08-31 14:40:22 -0700295 return true;
David Anderson88ef0b12018-08-09 10:40:00 -0700296 }
297 // Otherwise, open the partition as normal.
David Anderson12211d12018-07-24 15:21:20 -0700298 PartitionHandle handle;
299 if (!OpenPartition(device, args[0], &handle)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700300 *message = "Could not open partition";
301 return false;
David Anderson12211d12018-07-24 15:21:20 -0700302 }
303 uint64_t size = get_block_device_size(handle.fd());
David Anderson47589672018-09-04 16:22:41 -0700304 *message = android::base::StringPrintf("0x%" PRIX64, size);
David Anderson1fb3fd72018-08-31 14:40:22 -0700305 return true;
David Anderson12211d12018-07-24 15:21:20 -0700306}
David Anderson0d4277d2018-07-31 13:27:37 -0700307
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -0700308bool GetPartitionType(FastbootDevice* device, const std::vector<std::string>& args,
309 std::string* message) {
310 if (args.size() < 1) {
311 *message = "Missing argument";
312 return false;
313 }
Hridya Valsaraju4165e002018-10-09 10:40:35 -0700314
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -0700315 std::string partition_name = args[0];
Hridya Valsaraju4165e002018-10-09 10:40:35 -0700316 if (!FindPhysicalPartition(partition_name) &&
317 !LogicalPartitionExists(partition_name, device->GetCurrentSlot())) {
318 *message = "Invalid partition";
319 return false;
320 }
321
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -0700322 auto fastboot_hal = device->fastboot_hal();
323 if (!fastboot_hal) {
324 *message = "Fastboot HAL not found";
325 return false;
326 }
327
328 FileSystemType type;
329 Result ret;
330 auto ret_val =
331 fastboot_hal->getPartitionType(args[0], [&](FileSystemType fs_type, Result result) {
332 type = fs_type;
333 ret = result;
334 });
335 if (!ret_val.isOk() || (ret.status != Status::SUCCESS)) {
336 *message = "Unable to retrieve partition type";
337 } else {
338 switch (type) {
339 case FileSystemType::RAW:
340 *message = "raw";
341 return true;
342 case FileSystemType::EXT4:
343 *message = "ext4";
344 return true;
345 case FileSystemType::F2FS:
346 *message = "f2fs";
347 return true;
348 default:
349 *message = "Unknown file system type";
350 }
351 }
352
353 return false;
354}
355
David Anderson1fb3fd72018-08-31 14:40:22 -0700356bool GetPartitionIsLogical(FastbootDevice* device, const std::vector<std::string>& args,
357 std::string* message) {
David Anderson0d4277d2018-07-31 13:27:37 -0700358 if (args.size() < 1) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700359 *message = "Missing argument";
360 return false;
David Anderson0d4277d2018-07-31 13:27:37 -0700361 }
362 // Note: if a partition name is in both the GPT and the super partition, we
363 // return "true", to be consistent with prefering to flash logical partitions
364 // over physical ones.
365 std::string partition_name = args[0];
366 if (LogicalPartitionExists(partition_name, device->GetCurrentSlot())) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700367 *message = "yes";
368 return true;
David Anderson0d4277d2018-07-31 13:27:37 -0700369 }
370 if (FindPhysicalPartition(partition_name)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700371 *message = "no";
372 return true;
David Anderson0d4277d2018-07-31 13:27:37 -0700373 }
David Anderson1fb3fd72018-08-31 14:40:22 -0700374 *message = "Partition not found";
375 return false;
David Anderson0d4277d2018-07-31 13:27:37 -0700376}
David Andersond9ba0612018-08-02 11:05:00 -0700377
David Anderson1fb3fd72018-08-31 14:40:22 -0700378bool GetIsUserspace(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
379 std::string* message) {
380 *message = "yes";
381 return true;
David Andersond9ba0612018-08-02 11:05:00 -0700382}
David Anderson0f626632018-08-31 16:44:25 -0700383
384std::vector<std::vector<std::string>> GetAllPartitionArgsWithSlot(FastbootDevice* device) {
385 std::vector<std::vector<std::string>> args;
386 auto partitions = ListPartitions(device);
387 for (const auto& partition : partitions) {
388 args.emplace_back(std::initializer_list<std::string>{partition});
389 }
390 return args;
391}
392
393std::vector<std::vector<std::string>> GetAllPartitionArgsNoSlot(FastbootDevice* device) {
394 auto partitions = ListPartitions(device);
395
396 std::string slot_suffix = device->GetCurrentSlot();
397 if (!slot_suffix.empty()) {
398 auto names = std::move(partitions);
399 for (const auto& name : names) {
400 std::string slotless_name = name;
401 if (android::base::EndsWith(name, "_a") || android::base::EndsWith(name, "_b")) {
402 slotless_name = name.substr(0, name.rfind("_"));
403 }
404 if (std::find(partitions.begin(), partitions.end(), slotless_name) ==
405 partitions.end()) {
406 partitions.emplace_back(slotless_name);
407 }
408 }
409 }
410
411 std::vector<std::vector<std::string>> args;
412 for (const auto& partition : partitions) {
413 args.emplace_back(std::initializer_list<std::string>{partition});
414 }
415 return args;
416}
David Andersonc091c172018-09-04 18:11:03 -0700417
418bool GetHardwareRevision(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
419 std::string* message) {
420 *message = android::base::GetProperty("ro.revision", "");
421 return true;
422}
David Anderson90fe0a42018-11-05 18:01:32 -0800423
424bool GetSuperPartitionName(FastbootDevice* device, const std::vector<std::string>& /* args */,
425 std::string* message) {
426 uint32_t slot_number = SlotNumberForSlotSuffix(device->GetCurrentSlot());
427 *message = fs_mgr_get_super_partition_name(slot_number);
428 return true;
429}