2018-07-20 20:35:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "variables.h"
|
|
|
|
|
2018-07-24 22:21:20 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2018-07-20 20:35:50 +00:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
|
|
|
#include <ext4_utils/ext4_utils.h>
|
|
|
|
|
|
|
|
#include "fastboot_device.h"
|
2018-07-24 22:21:20 +00:00
|
|
|
#include "flashing.h"
|
2018-07-20 20:35:50 +00:00
|
|
|
#include "utility.h"
|
|
|
|
|
|
|
|
using ::android::hardware::boot::V1_0::BoolResult;
|
|
|
|
using ::android::hardware::boot::V1_0::Slot;
|
2018-09-05 23:57:24 +00:00
|
|
|
using ::android::hardware::fastboot::V1_0::FileSystemType;
|
|
|
|
using ::android::hardware::fastboot::V1_0::Result;
|
|
|
|
using ::android::hardware::fastboot::V1_0::Status;
|
2018-07-20 20:35:50 +00:00
|
|
|
|
|
|
|
constexpr int kMaxDownloadSizeDefault = 0x20000000;
|
|
|
|
constexpr char kFastbootProtocolVersion[] = "0.4";
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
|
|
|
*message = kFastbootProtocolVersion;
|
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetBootloaderVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
|
|
|
*message = android::base::GetProperty("ro.bootloader", "");
|
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetBasebandVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
|
|
|
*message = android::base::GetProperty("ro.build.expect.baseband", "");
|
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetProduct(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
|
|
|
*message = android::base::GetProperty("ro.product.device", "");
|
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetSerial(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
|
|
|
*message = android::base::GetProperty("ro.serialno", "");
|
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetSecure(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
|
|
|
*message = android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no";
|
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
2018-07-20 20:35:50 +00:00
|
|
|
std::string suffix = device->GetCurrentSlot();
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = suffix.size() == 2 ? suffix.substr(1) : suffix;
|
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
2018-07-20 20:35:50 +00:00
|
|
|
auto boot_control_hal = device->boot_control_hal();
|
|
|
|
if (!boot_control_hal) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "0";
|
|
|
|
} else {
|
|
|
|
*message = std::to_string(boot_control_hal->getNumberSlots());
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
2018-08-31 21:40:22 +00:00
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args,
|
|
|
|
std::string* message) {
|
2018-07-20 20:35:50 +00:00
|
|
|
if (args.empty()) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "Missing argument";
|
|
|
|
return false;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
Slot slot;
|
|
|
|
if (!GetSlotNumber(args[0], &slot)) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "Invalid slot";
|
|
|
|
return false;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
auto boot_control_hal = device->boot_control_hal();
|
|
|
|
if (!boot_control_hal) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "Device has no slots";
|
|
|
|
return false;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
2018-08-09 00:58:56 +00:00
|
|
|
if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "no";
|
|
|
|
} else {
|
|
|
|
*message = "yes";
|
2018-08-09 00:58:56 +00:00
|
|
|
}
|
2018-08-31 21:40:22 +00:00
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args,
|
|
|
|
std::string* message) {
|
2018-07-20 20:35:50 +00:00
|
|
|
if (args.empty()) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "Missing argument";
|
|
|
|
return false;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
Slot slot;
|
|
|
|
if (!GetSlotNumber(args[0], &slot)) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "Invalid slot";
|
|
|
|
return false;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
auto boot_control_hal = device->boot_control_hal();
|
|
|
|
if (!boot_control_hal) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "Device has no slots";
|
|
|
|
return false;
|
2018-08-09 00:58:56 +00:00
|
|
|
}
|
|
|
|
if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "yes";
|
|
|
|
} else {
|
|
|
|
*message = "no";
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
2018-08-31 21:40:22 +00:00
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetMaxDownloadSize(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
2018-09-04 23:51:29 +00:00
|
|
|
*message = android::base::StringPrintf("0x%X", kMaxDownloadSizeDefault);
|
2018-08-31 21:40:22 +00:00
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetUnlocked(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
|
|
|
*message = "yes";
|
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args,
|
|
|
|
std::string* message) {
|
2018-07-20 20:35:50 +00:00
|
|
|
if (args.empty()) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "Missing argument";
|
|
|
|
return false;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
|
|
|
std::string slot_suffix = device->GetCurrentSlot();
|
|
|
|
if (slot_suffix.empty()) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "no";
|
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
2018-08-14 23:21:50 +00:00
|
|
|
std::string partition_name = args[0] + slot_suffix;
|
|
|
|
if (FindPhysicalPartition(partition_name) ||
|
|
|
|
LogicalPartitionExists(partition_name, slot_suffix)) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "yes";
|
|
|
|
} else {
|
|
|
|
*message = "no";
|
2018-08-14 23:21:50 +00:00
|
|
|
}
|
2018-08-31 21:40:22 +00:00
|
|
|
return true;
|
2018-07-20 20:35:50 +00:00
|
|
|
}
|
2018-07-24 22:21:20 +00:00
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args,
|
|
|
|
std::string* message) {
|
2018-07-24 22:21:20 +00:00
|
|
|
if (args.size() < 1) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "Missing argument";
|
|
|
|
return false;
|
2018-07-24 22:21:20 +00:00
|
|
|
}
|
2018-08-09 17:40:00 +00:00
|
|
|
// Zero-length partitions cannot be created through device-mapper, so we
|
|
|
|
// special case them here.
|
|
|
|
bool is_zero_length;
|
|
|
|
if (LogicalPartitionExists(args[0], device->GetCurrentSlot(), &is_zero_length) &&
|
|
|
|
is_zero_length) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "0";
|
|
|
|
return true;
|
2018-08-09 17:40:00 +00:00
|
|
|
}
|
|
|
|
// Otherwise, open the partition as normal.
|
2018-07-24 22:21:20 +00:00
|
|
|
PartitionHandle handle;
|
|
|
|
if (!OpenPartition(device, args[0], &handle)) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "Could not open partition";
|
|
|
|
return false;
|
2018-07-24 22:21:20 +00:00
|
|
|
}
|
|
|
|
uint64_t size = get_block_device_size(handle.fd());
|
2018-09-04 23:22:41 +00:00
|
|
|
*message = android::base::StringPrintf("0x%" PRIX64, size);
|
2018-08-31 21:40:22 +00:00
|
|
|
return true;
|
2018-07-24 22:21:20 +00:00
|
|
|
}
|
2018-07-31 20:27:37 +00:00
|
|
|
|
2018-09-05 23:57:24 +00:00
|
|
|
bool GetPartitionType(FastbootDevice* device, const std::vector<std::string>& args,
|
|
|
|
std::string* message) {
|
|
|
|
if (args.size() < 1) {
|
|
|
|
*message = "Missing argument";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string partition_name = args[0];
|
|
|
|
auto fastboot_hal = device->fastboot_hal();
|
|
|
|
if (!fastboot_hal) {
|
|
|
|
*message = "Fastboot HAL not found";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemType type;
|
|
|
|
Result ret;
|
|
|
|
auto ret_val =
|
|
|
|
fastboot_hal->getPartitionType(args[0], [&](FileSystemType fs_type, Result result) {
|
|
|
|
type = fs_type;
|
|
|
|
ret = result;
|
|
|
|
});
|
|
|
|
if (!ret_val.isOk() || (ret.status != Status::SUCCESS)) {
|
|
|
|
*message = "Unable to retrieve partition type";
|
|
|
|
} else {
|
|
|
|
switch (type) {
|
|
|
|
case FileSystemType::RAW:
|
|
|
|
*message = "raw";
|
|
|
|
return true;
|
|
|
|
case FileSystemType::EXT4:
|
|
|
|
*message = "ext4";
|
|
|
|
return true;
|
|
|
|
case FileSystemType::F2FS:
|
|
|
|
*message = "f2fs";
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
*message = "Unknown file system type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetPartitionIsLogical(FastbootDevice* device, const std::vector<std::string>& args,
|
|
|
|
std::string* message) {
|
2018-07-31 20:27:37 +00:00
|
|
|
if (args.size() < 1) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "Missing argument";
|
|
|
|
return false;
|
2018-07-31 20:27:37 +00:00
|
|
|
}
|
|
|
|
// Note: if a partition name is in both the GPT and the super partition, we
|
|
|
|
// return "true", to be consistent with prefering to flash logical partitions
|
|
|
|
// over physical ones.
|
|
|
|
std::string partition_name = args[0];
|
|
|
|
if (LogicalPartitionExists(partition_name, device->GetCurrentSlot())) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "yes";
|
|
|
|
return true;
|
2018-07-31 20:27:37 +00:00
|
|
|
}
|
|
|
|
if (FindPhysicalPartition(partition_name)) {
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "no";
|
|
|
|
return true;
|
2018-07-31 20:27:37 +00:00
|
|
|
}
|
2018-08-31 21:40:22 +00:00
|
|
|
*message = "Partition not found";
|
|
|
|
return false;
|
2018-07-31 20:27:37 +00:00
|
|
|
}
|
2018-08-02 18:05:00 +00:00
|
|
|
|
2018-08-31 21:40:22 +00:00
|
|
|
bool GetIsUserspace(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
|
|
|
*message = "yes";
|
|
|
|
return true;
|
2018-08-02 18:05:00 +00:00
|
|
|
}
|
2018-08-31 23:44:25 +00:00
|
|
|
|
|
|
|
std::vector<std::vector<std::string>> GetAllPartitionArgsWithSlot(FastbootDevice* device) {
|
|
|
|
std::vector<std::vector<std::string>> args;
|
|
|
|
auto partitions = ListPartitions(device);
|
|
|
|
for (const auto& partition : partitions) {
|
|
|
|
args.emplace_back(std::initializer_list<std::string>{partition});
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::vector<std::string>> GetAllPartitionArgsNoSlot(FastbootDevice* device) {
|
|
|
|
auto partitions = ListPartitions(device);
|
|
|
|
|
|
|
|
std::string slot_suffix = device->GetCurrentSlot();
|
|
|
|
if (!slot_suffix.empty()) {
|
|
|
|
auto names = std::move(partitions);
|
|
|
|
for (const auto& name : names) {
|
|
|
|
std::string slotless_name = name;
|
|
|
|
if (android::base::EndsWith(name, "_a") || android::base::EndsWith(name, "_b")) {
|
|
|
|
slotless_name = name.substr(0, name.rfind("_"));
|
|
|
|
}
|
|
|
|
if (std::find(partitions.begin(), partitions.end(), slotless_name) ==
|
|
|
|
partitions.end()) {
|
|
|
|
partitions.emplace_back(slotless_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::vector<std::string>> args;
|
|
|
|
for (const auto& partition : partitions) {
|
|
|
|
args.emplace_back(std::initializer_list<std::string>{partition});
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
2018-09-05 01:11:03 +00:00
|
|
|
|
|
|
|
bool GetHardwareRevision(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
|
|
|
|
std::string* message) {
|
|
|
|
*message = android::base::GetProperty("ro.revision", "");
|
|
|
|
return true;
|
|
|
|
}
|