blob: 8f66fea76017c837d777bf946ca718ec7e18a35a [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 Anderson856b7ec2018-08-08 17:58:56 -0700126 return device->WriteFail("Invalid slot");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700127 }
David Anderson856b7ec2018-08-08 17:58:56 -0700128 std::string result = (args[0] == "userdata" ? "no" : "yes");
129 return device->WriteOkay(result);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700130}
David Anderson12211d12018-07-24 15:21:20 -0700131
132bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args) {
133 if (args.size() < 1) {
134 return device->WriteFail("Missing argument");
135 }
136 PartitionHandle handle;
137 if (!OpenPartition(device, args[0], &handle)) {
138 return device->WriteFail("Could not open partition");
139 }
140 uint64_t size = get_block_device_size(handle.fd());
141 return device->WriteOkay(android::base::StringPrintf("%" PRIX64, size));
142}