blob: 33f7f74bd3c0fe54730dede2ce46ed33fd65ce52 [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
19#include <android-base/file.h>
20#include <android-base/logging.h>
21#include <android-base/properties.h>
22#include <android-base/stringprintf.h>
23#include <android-base/strings.h>
24#include <ext4_utils/ext4_utils.h>
25
26#include "fastboot_device.h"
27#include "utility.h"
28
29using ::android::hardware::boot::V1_0::BoolResult;
30using ::android::hardware::boot::V1_0::Slot;
31
32constexpr int kMaxDownloadSizeDefault = 0x20000000;
33constexpr char kFastbootProtocolVersion[] = "0.4";
34
David Anderson856b7ec2018-08-08 17:58:56 -070035bool GetVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
36 return device->WriteOkay(kFastbootProtocolVersion);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070037}
38
David Anderson856b7ec2018-08-08 17:58:56 -070039bool GetBootloaderVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
40 return device->WriteOkay(android::base::GetProperty("ro.bootloader", ""));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070041}
42
David Anderson856b7ec2018-08-08 17:58:56 -070043bool GetBasebandVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
44 return device->WriteOkay(android::base::GetProperty("ro.build.expect.baseband", ""));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070045}
46
David Anderson856b7ec2018-08-08 17:58:56 -070047bool GetProduct(FastbootDevice* device, const std::vector<std::string>& /* args */) {
48 return device->WriteOkay(android::base::GetProperty("ro.product.device", ""));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070049}
50
David Anderson856b7ec2018-08-08 17:58:56 -070051bool GetSerial(FastbootDevice* device, const std::vector<std::string>& /* args */) {
52 return device->WriteOkay(android::base::GetProperty("ro.serialno", ""));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070053}
54
David Anderson856b7ec2018-08-08 17:58:56 -070055bool GetSecure(FastbootDevice* device, const std::vector<std::string>& /* args */) {
56 return device->WriteOkay(android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070057}
58
David Anderson856b7ec2018-08-08 17:58:56 -070059bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070060 std::string suffix = device->GetCurrentSlot();
David Anderson856b7ec2018-08-08 17:58:56 -070061 std::string slot = suffix.size() == 2 ? suffix.substr(1) : suffix;
62 return device->WriteOkay(slot);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070063}
64
David Anderson856b7ec2018-08-08 17:58:56 -070065bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070066 auto boot_control_hal = device->boot_control_hal();
67 if (!boot_control_hal) {
68 return "0";
69 }
David Anderson856b7ec2018-08-08 17:58:56 -070070 return device->WriteOkay(std::to_string(boot_control_hal->getNumberSlots()));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070071}
72
David Anderson856b7ec2018-08-08 17:58:56 -070073bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070074 if (args.empty()) {
David Anderson856b7ec2018-08-08 17:58:56 -070075 return device->WriteFail("Missing argument");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070076 }
77 Slot slot;
78 if (!GetSlotNumber(args[0], &slot)) {
David Anderson856b7ec2018-08-08 17:58:56 -070079 return device->WriteFail("Invalid slot");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070080 }
81 auto boot_control_hal = device->boot_control_hal();
82 if (!boot_control_hal) {
David Anderson856b7ec2018-08-08 17:58:56 -070083 return device->WriteFail("Device has no slots");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070084 }
David Anderson856b7ec2018-08-08 17:58:56 -070085 if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) {
86 return device->WriteOkay("no");
87 }
88 return device->WriteOkay("yes");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070089}
90
David Anderson856b7ec2018-08-08 17:58:56 -070091bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070092 if (args.empty()) {
David Anderson856b7ec2018-08-08 17:58:56 -070093 return device->WriteFail("Missing argument");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070094 }
95 Slot slot;
96 if (!GetSlotNumber(args[0], &slot)) {
David Anderson856b7ec2018-08-08 17:58:56 -070097 return device->WriteFail("Invalid slot");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070098 }
99 auto boot_control_hal = device->boot_control_hal();
100 if (!boot_control_hal) {
David Anderson856b7ec2018-08-08 17:58:56 -0700101 return device->WriteFail("Device has no slots");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700102 }
David Anderson856b7ec2018-08-08 17:58:56 -0700103 if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) {
104 return device->WriteOkay("yes");
105 }
106 return device->WriteOkay("no");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700107}
108
David Anderson856b7ec2018-08-08 17:58:56 -0700109bool GetMaxDownloadSize(FastbootDevice* device, const std::vector<std::string>& /* args */) {
110 return device->WriteOkay(std::to_string(kMaxDownloadSizeDefault));
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700111}
112
David Anderson856b7ec2018-08-08 17:58:56 -0700113bool GetUnlocked(FastbootDevice* device, const std::vector<std::string>& /* args */) {
114 return device->WriteOkay("yes");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700115}
116
David Anderson856b7ec2018-08-08 17:58:56 -0700117bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700118 if (args.empty()) {
David Anderson856b7ec2018-08-08 17:58:56 -0700119 return device->WriteFail("Missing argument");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700120 }
121 std::string slot_suffix = device->GetCurrentSlot();
122 if (slot_suffix.empty()) {
David Anderson856b7ec2018-08-08 17:58:56 -0700123 return device->WriteFail("Invalid slot");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700124 }
David Anderson856b7ec2018-08-08 17:58:56 -0700125 std::string result = (args[0] == "userdata" ? "no" : "yes");
126 return device->WriteOkay(result);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700127}