Merge "fastbootd: Allow returning errors from getvar handlers."

am: 4264b7f5d2

Change-Id: I1e14040ca3aa2dd671e910f0bedd8f138e9ec2f0
This commit is contained in:
David Anderson 2018-08-09 09:40:44 -07:00 committed by android-build-merger
commit e60694d66a
5 changed files with 69 additions and 54 deletions

View file

@ -37,8 +37,7 @@ using ::android::hardware::boot::V1_0::CommandResult;
using ::android::hardware::boot::V1_0::Slot; using ::android::hardware::boot::V1_0::Slot;
bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) { bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) {
using VariableHandler = using VariableHandler = std::function<bool(FastbootDevice*, const std::vector<std::string>&)>;
std::function<std::string(FastbootDevice*, const std::vector<std::string>&)>;
const std::unordered_map<std::string, VariableHandler> kVariableMap = { const std::unordered_map<std::string, VariableHandler> kVariableMap = {
{FB_VAR_VERSION, GetVersion}, {FB_VAR_VERSION, GetVersion},
{FB_VAR_VERSION_BOOTLOADER, GetBootloaderVersion}, {FB_VAR_VERSION_BOOTLOADER, GetBootloaderVersion},
@ -61,8 +60,7 @@ bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args)
} }
std::vector<std::string> getvar_args(args.begin() + 2, args.end()); std::vector<std::string> getvar_args(args.begin() + 2, args.end());
auto result = found_variable->second(device, getvar_args); return found_variable->second(device, getvar_args);
return device->WriteStatus(FastbootResult::OKAY, result);
} }
bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) { bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {

View file

@ -122,3 +122,11 @@ void FastbootDevice::ExecuteCommands() {
} }
} }
} }
bool FastbootDevice::WriteOkay(const std::string& message) {
return WriteStatus(FastbootResult::OKAY, message);
}
bool FastbootDevice::WriteFail(const std::string& message) {
return WriteStatus(FastbootResult::FAIL, message);
}

View file

@ -39,6 +39,10 @@ class FastbootDevice {
bool HandleData(bool read, std::vector<char>* data); bool HandleData(bool read, std::vector<char>* data);
std::string GetCurrentSlot(); std::string GetCurrentSlot();
// Shortcuts for writing OKAY and FAIL status results.
bool WriteOkay(const std::string& message);
bool WriteFail(const std::string& message);
std::vector<char>& get_download_data() { return download_data_; } std::vector<char>& get_download_data() { return download_data_; }
void set_upload_data(const std::vector<char>& data) { upload_data_ = data; } void set_upload_data(const std::vector<char>& data) { upload_data_ = data; }
void set_upload_data(std::vector<char>&& data) { upload_data_ = std::move(data); } void set_upload_data(std::vector<char>&& data) { upload_data_ = std::move(data); }

View file

@ -32,91 +32,96 @@ using ::android::hardware::boot::V1_0::Slot;
constexpr int kMaxDownloadSizeDefault = 0x20000000; constexpr int kMaxDownloadSizeDefault = 0x20000000;
constexpr char kFastbootProtocolVersion[] = "0.4"; constexpr char kFastbootProtocolVersion[] = "0.4";
std::string GetVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */) { bool GetVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
return kFastbootProtocolVersion; return device->WriteOkay(kFastbootProtocolVersion);
} }
std::string GetBootloaderVersion(FastbootDevice* /* device */, bool GetBootloaderVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
const std::vector<std::string>& /* args */) { return device->WriteOkay(android::base::GetProperty("ro.bootloader", ""));
return android::base::GetProperty("ro.bootloader", "");
} }
std::string GetBasebandVersion(FastbootDevice* /* device */, bool GetBasebandVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
const std::vector<std::string>& /* args */) { return device->WriteOkay(android::base::GetProperty("ro.build.expect.baseband", ""));
return android::base::GetProperty("ro.build.expect.baseband", "");
} }
std::string GetProduct(FastbootDevice* /* device */, const std::vector<std::string>& /* args */) { bool GetProduct(FastbootDevice* device, const std::vector<std::string>& /* args */) {
return android::base::GetProperty("ro.product.device", ""); return device->WriteOkay(android::base::GetProperty("ro.product.device", ""));
} }
std::string GetSerial(FastbootDevice* /* device */, const std::vector<std::string>& /* args */) { bool GetSerial(FastbootDevice* device, const std::vector<std::string>& /* args */) {
return android::base::GetProperty("ro.serialno", ""); return device->WriteOkay(android::base::GetProperty("ro.serialno", ""));
} }
std::string GetSecure(FastbootDevice* /* device */, const std::vector<std::string>& /* args */) { bool GetSecure(FastbootDevice* device, const std::vector<std::string>& /* args */) {
return android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no"; return device->WriteOkay(android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no");
} }
std::string GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */) { bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */) {
std::string suffix = device->GetCurrentSlot(); std::string suffix = device->GetCurrentSlot();
return suffix.size() == 2 ? suffix.substr(1) : suffix; std::string slot = suffix.size() == 2 ? suffix.substr(1) : suffix;
return device->WriteOkay(slot);
} }
std::string GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */) { bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */) {
auto boot_control_hal = device->boot_control_hal(); auto boot_control_hal = device->boot_control_hal();
if (!boot_control_hal) { if (!boot_control_hal) {
return "0"; return "0";
} }
return std::to_string(boot_control_hal->getNumberSlots()); return device->WriteOkay(std::to_string(boot_control_hal->getNumberSlots()));
} }
std::string GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args) { bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args) {
if (args.empty()) { if (args.empty()) {
return "no"; return device->WriteFail("Missing argument");
} }
Slot slot; Slot slot;
if (!GetSlotNumber(args[0], &slot)) { if (!GetSlotNumber(args[0], &slot)) {
return "no"; return device->WriteFail("Invalid slot");
} }
auto boot_control_hal = device->boot_control_hal(); auto boot_control_hal = device->boot_control_hal();
if (!boot_control_hal) { if (!boot_control_hal) {
return "no"; return device->WriteFail("Device has no slots");
} }
return boot_control_hal->isSlotMarkedSuccessful(slot) == BoolResult::TRUE ? "yes" : "no"; if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) {
return device->WriteOkay("no");
}
return device->WriteOkay("yes");
} }
std::string GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args) { bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args) {
if (args.empty()) { if (args.empty()) {
return "no"; return device->WriteFail("Missing argument");
} }
Slot slot; Slot slot;
if (!GetSlotNumber(args[0], &slot)) { if (!GetSlotNumber(args[0], &slot)) {
return "no"; return device->WriteFail("Invalid slot");
} }
auto boot_control_hal = device->boot_control_hal(); auto boot_control_hal = device->boot_control_hal();
if (!boot_control_hal) { if (!boot_control_hal) {
return "no"; return device->WriteFail("Device has no slots");
} }
return boot_control_hal->isSlotBootable(slot) == BoolResult::TRUE ? "no" : "yes"; if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) {
return device->WriteOkay("yes");
}
return device->WriteOkay("no");
} }
std::string GetMaxDownloadSize(FastbootDevice* /* device */, bool GetMaxDownloadSize(FastbootDevice* device, const std::vector<std::string>& /* args */) {
const std::vector<std::string>& /* args */) { return device->WriteOkay(std::to_string(kMaxDownloadSizeDefault));
return std::to_string(kMaxDownloadSizeDefault);
} }
std::string GetUnlocked(FastbootDevice* /* device */, const std::vector<std::string>& /* args */) { bool GetUnlocked(FastbootDevice* device, const std::vector<std::string>& /* args */) {
return "yes"; return device->WriteOkay("yes");
} }
std::string GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args) { bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args) {
if (args.empty()) { if (args.empty()) {
return "no"; return device->WriteFail("Missing argument");
} }
std::string slot_suffix = device->GetCurrentSlot(); std::string slot_suffix = device->GetCurrentSlot();
if (slot_suffix.empty()) { if (slot_suffix.empty()) {
return "no"; return device->WriteFail("Invalid slot");
} }
return args[0] == "userdata" ? "no" : "yes"; std::string result = (args[0] == "userdata" ? "no" : "yes");
return device->WriteOkay(result);
} }

View file

@ -21,16 +21,16 @@
class FastbootDevice; class FastbootDevice;
std::string GetVersion(FastbootDevice* device, const std::vector<std::string>& args); bool GetVersion(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetBootloaderVersion(FastbootDevice* device, const std::vector<std::string>& args); bool GetBootloaderVersion(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetBasebandVersion(FastbootDevice* device, const std::vector<std::string>& args); bool GetBasebandVersion(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetProduct(FastbootDevice* device, const std::vector<std::string>& args); bool GetProduct(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetSerial(FastbootDevice* device, const std::vector<std::string>& args); bool GetSerial(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetSecure(FastbootDevice* device, const std::vector<std::string>& args); bool GetSecure(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& args); bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetSlotCount(FastbootDevice* device, const std::vector<std::string>& args); bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args); bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args); bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetMaxDownloadSize(FastbootDevice* device, const std::vector<std::string>& args); bool GetMaxDownloadSize(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetUnlocked(FastbootDevice* device, const std::vector<std::string>& args); bool GetUnlocked(FastbootDevice* device, const std::vector<std::string>& args);
std::string GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args); bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args);