bootctrl: run clang-format

Bug: 156694052
Signed-off-by: Fernando Lugo <flugo@google.com>
Change-Id: I003fb99e96bcee2d745365992b3c8d4a3816f818
This commit is contained in:
Fernando Lugo 2021-03-08 15:36:55 -08:00
parent fa50bf6c7c
commit 2b75765719
5 changed files with 53 additions and 54 deletions

View file

@ -17,16 +17,15 @@
#define LOG_TAG "bootcontrolhal" #define LOG_TAG "bootcontrolhal"
#include "BootControl.h" #include "BootControl.h"
#include "GptUtils.h"
#include <android-base/file.h> #include <android-base/file.h>
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#include <bootloader_message/bootloader_message.h> #include <bootloader_message/bootloader_message.h>
#include <cutils/properties.h> #include <cutils/properties.h>
//#include <hardware/boot_control.h>
#include <libboot_control/libboot_control.h> #include <libboot_control/libboot_control.h>
#include <log/log.h> #include <log/log.h>
#include "GptUtils.h"
namespace android { namespace android {
namespace hardware { namespace hardware {
@ -37,12 +36,14 @@ namespace implementation {
using android::bootable::GetMiscVirtualAbMergeStatus; using android::bootable::GetMiscVirtualAbMergeStatus;
using android::bootable::InitMiscVirtualAbMessageIfNeeded; using android::bootable::InitMiscVirtualAbMessageIfNeeded;
using android::bootable::SetMiscVirtualAbMergeStatus; using android::bootable::SetMiscVirtualAbMergeStatus;
using android::hardware::boot::V1_1::MergeStatus;
using android::hardware::boot::V1_0::CommandResult;
using android::hardware::boot::V1_0::BoolResult; using android::hardware::boot::V1_0::BoolResult;
using android::hardware::boot::V1_0::CommandResult;
using android::hardware::boot::V1_1::MergeStatus;
namespace { namespace {
// clang-format off
#define BOOT_A_PATH "/dev/block/by-name/boot_a" #define BOOT_A_PATH "/dev/block/by-name/boot_a"
#define BOOT_B_PATH "/dev/block/by-name/boot_b" #define BOOT_B_PATH "/dev/block/by-name/boot_b"
@ -59,6 +60,8 @@ namespace {
#define AB_ATTR_MAX_PRIORITY 3UL #define AB_ATTR_MAX_PRIORITY 3UL
#define AB_ATTR_MAX_RETRY_COUNT 3UL #define AB_ATTR_MAX_RETRY_COUNT 3UL
// clang-format on
static std::string getDevPath(uint32_t slot) { static std::string getDevPath(uint32_t slot) {
char real_path[PATH_MAX]; char real_path[PATH_MAX];
@ -122,17 +125,17 @@ static int setSlotFlag(uint32_t slot, uint64_t flag) {
return 0; return 0;
} }
} } // namespace
// Methods from ::android::hardware::boot::V1_0::IBootControl follow. // Methods from ::android::hardware::boot::V1_0::IBootControl follow.
Return<uint32_t> BootControl::getNumberSlots() { Return<uint32_t> BootControl::getNumberSlots() {
uint32_t slots = 0; uint32_t slots = 0;
if (access(BOOT_A_PATH, F_OK) == 0) if (access(BOOT_A_PATH, F_OK) == 0)
slots++; slots++;
if (access(BOOT_B_PATH, F_OK) == 0) if (access(BOOT_B_PATH, F_OK) == 0)
slots++; slots++;
return slots; return slots;
} }
@ -189,14 +192,14 @@ Return<void> BootControl::setActiveBootSlot(uint32_t slot, setActiveBootSlot_cb
return Void(); return Void();
} }
std::string boot_lun_path = std::string("/sys/devices/platform/") + std::string boot_lun_path =
boot_dev + "/pixel/boot_lun_enabled"; std::string("/sys/devices/platform/") + boot_dev + "/pixel/boot_lun_enabled";
int fd = open(boot_lun_path.c_str(), O_RDWR); int fd = open(boot_lun_path.c_str(), O_RDWR);
if (fd < 0) { if (fd < 0) {
// Try old path for kernels < 5.4 // Try old path for kernels < 5.4
// TODO: remove once kernel 4.19 support is deprecated // TODO: remove once kernel 4.19 support is deprecated
std::string boot_lun_path = std::string("/sys/devices/platform/") + std::string boot_lun_path =
boot_dev + "/attributes/boot_lun_enabled"; std::string("/sys/devices/platform/") + boot_dev + "/attributes/boot_lun_enabled";
fd = open(boot_lun_path.c_str(), O_RDWR); fd = open(boot_lun_path.c_str(), O_RDWR);
if (fd < 0) { if (fd < 0) {
_hidl_cb({false, "failed to open ufs attr boot_lun_enabled"}); _hidl_cb({false, "failed to open ufs attr boot_lun_enabled"});
@ -257,7 +260,8 @@ Return<::android::hardware::boot::V1_0::BoolResult> BootControl::isSlotBootable(
return isSlotFlagSet(slot, AB_ATTR_UNBOOTABLE) ? BoolResult::FALSE : BoolResult::TRUE; return isSlotFlagSet(slot, AB_ATTR_UNBOOTABLE) ? BoolResult::FALSE : BoolResult::TRUE;
} }
Return<::android::hardware::boot::V1_0::BoolResult> BootControl::isSlotMarkedSuccessful(uint32_t slot) { Return<::android::hardware::boot::V1_0::BoolResult> BootControl::isSlotMarkedSuccessful(
uint32_t slot) {
if (getNumberSlots() == 0) { if (getNumberSlots() == 0) {
// just return true so that we don't we another call trying to mark it as successful // just return true so that we don't we another call trying to mark it as successful
// when there is no slots // when there is no slots
@ -278,7 +282,8 @@ bool BootControl::Init() {
return InitMiscVirtualAbMessageIfNeeded(); return InitMiscVirtualAbMessageIfNeeded();
} }
Return<bool> BootControl::setSnapshotMergeStatus(::android::hardware::boot::V1_1::MergeStatus status) { Return<bool> BootControl::setSnapshotMergeStatus(
::android::hardware::boot::V1_1::MergeStatus status) {
return SetMiscVirtualAbMergeStatus(getCurrentSlot(), status); return SetMiscVirtualAbMergeStatus(getCurrentSlot(), status);
} }
@ -300,8 +305,7 @@ Return<uint32_t> BootControl::getActiveBootSlot() {
// Methods from ::android::hidl::base::V1_0::IBase follow. // Methods from ::android::hidl::base::V1_0::IBase follow.
IBootControl *HIDL_FETCH_IBootControl(const char * /* name */) {
IBootControl* HIDL_FETCH_IBootControl(const char* /* name */) {
auto module = new BootControl(); auto module = new BootControl();
module->Init(); module->Init();

View file

@ -26,16 +26,15 @@ namespace boot {
namespace V1_2 { namespace V1_2 {
namespace implementation { namespace implementation {
using ::android::sp;
using ::android::hardware::hidl_array; using ::android::hardware::hidl_array;
using ::android::hardware::hidl_memory; using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string; using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec; using ::android::hardware::hidl_vec;
using ::android::hardware::Return; using ::android::hardware::Return;
using ::android::hardware::Void; using ::android::hardware::Void;
using ::android::sp;
struct BootControl : public IBootControl { struct BootControl : public IBootControl {
bool Init(); bool Init();
// Methods from ::android::hardware::boot::V1_0::IBootControl follow. // Methods from ::android::hardware::boot::V1_0::IBootControl follow.
@ -45,22 +44,23 @@ struct BootControl : public IBootControl {
Return<void> setActiveBootSlot(uint32_t slot, setActiveBootSlot_cb _hidl_cb) override; Return<void> setActiveBootSlot(uint32_t slot, setActiveBootSlot_cb _hidl_cb) override;
Return<void> setSlotAsUnbootable(uint32_t slot, setSlotAsUnbootable_cb _hidl_cb) override; Return<void> setSlotAsUnbootable(uint32_t slot, setSlotAsUnbootable_cb _hidl_cb) override;
Return<::android::hardware::boot::V1_0::BoolResult> isSlotBootable(uint32_t slot) override; Return<::android::hardware::boot::V1_0::BoolResult> isSlotBootable(uint32_t slot) override;
Return<::android::hardware::boot::V1_0::BoolResult> isSlotMarkedSuccessful(uint32_t slot) override; Return<::android::hardware::boot::V1_0::BoolResult> isSlotMarkedSuccessful(
uint32_t slot) override;
Return<void> getSuffix(uint32_t slot, getSuffix_cb _hidl_cb) override; Return<void> getSuffix(uint32_t slot, getSuffix_cb _hidl_cb) override;
// Methods from ::android::hardware::boot::V1_1::IBootControl follow. // Methods from ::android::hardware::boot::V1_1::IBootControl follow.
Return<bool> setSnapshotMergeStatus(::android::hardware::boot::V1_1::MergeStatus status) override; Return<bool> setSnapshotMergeStatus(
::android::hardware::boot::V1_1::MergeStatus status) override;
Return<::android::hardware::boot::V1_1::MergeStatus> getSnapshotMergeStatus() override; Return<::android::hardware::boot::V1_1::MergeStatus> getSnapshotMergeStatus() override;
// Methods from ::android::hardware::boot::V1_2::IBootControl follow. // Methods from ::android::hardware::boot::V1_2::IBootControl follow.
Return<uint32_t> getActiveBootSlot() override; Return<uint32_t> getActiveBootSlot() override;
// Methods from ::android::hidl::base::V1_0::IBase follow. // Methods from ::android::hidl::base::V1_0::IBase follow.
}; };
// FIXME: most likely delete, this is only for passthrough implementations // FIXME: most likely delete, this is only for passthrough implementations
extern "C" IBootControl* HIDL_FETCH_IBootControl(const char* name); extern "C" IBootControl *HIDL_FETCH_IBootControl(const char *name);
} // namespace implementation } // namespace implementation
} // namespace V1_2 } // namespace V1_2

View file

@ -18,10 +18,10 @@
#include "GptUtils.h" #include "GptUtils.h"
#include <errno.h>
#include <log/log.h>
#include <android-base/file.h> #include <android-base/file.h>
#include <errno.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <log/log.h>
#include <zlib.h> #include <zlib.h>
namespace android { namespace android {
@ -32,8 +32,7 @@ namespace implementation {
namespace { namespace {
static int ValidateGptHeader(gpt_header *gpt) static int ValidateGptHeader(gpt_header *gpt) {
{
if (gpt->signature != GPT_SIGNATURE) { if (gpt->signature != GPT_SIGNATURE) {
ALOGE("invalid gpt signature 0x%lx\n", gpt->signature); ALOGE("invalid gpt signature 0x%lx\n", gpt->signature);
return -1; return -1;
@ -52,12 +51,11 @@ static int ValidateGptHeader(gpt_header *gpt)
return 0; return 0;
} }
} } // namespace
GptUtils::GptUtils(const std::string dev_path) : dev_path(dev_path), fd(0) {} GptUtils::GptUtils(const std::string dev_path) : dev_path(dev_path), fd(0) {}
int GptUtils::Load(void) int GptUtils::Load(void) {
{
fd = open(dev_path.c_str(), O_RDWR); fd = open(dev_path.c_str(), O_RDWR);
if (fd < 0) { if (fd < 0) {
ALOGE("failed to open block dev %s, %d\n", dev_path.c_str(), errno); ALOGE("failed to open block dev %s, %d\n", dev_path.c_str(), errno);
@ -102,20 +100,19 @@ int GptUtils::Load(void)
} }
if (ValidateGptHeader(&gpt_backup)) { if (ValidateGptHeader(&gpt_backup)) {
ALOGW("error validating gpt backup\n"); // just warn about it, not fail ALOGW("error validating gpt backup\n"); // just warn about it, not fail
} }
// Create map <partition name, gpt_entry pointer> // Create map <partition name, gpt_entry pointer>
auto get_name = [](const uint16_t *efi_name) { auto get_name = [](const uint16_t *efi_name) {
char name[37] = {}; char name[37] = {};
for (int i = 0; efi_name[i] && i < sizeof name - 1; ++i) for (int i = 0; efi_name[i] && i < sizeof name - 1; ++i) name[i] = efi_name[i];
name[i] = efi_name[i];
return std::string(name); return std::string(name);
}; };
for (auto const &e: entry_array) { for (auto const &e : entry_array) {
if (e.name[0] == 0) if (e.name[0] == 0)
break; // stop at the first partition with no name break; // stop at the first partition with no name
std::string s = get_name(e.name); std::string s = get_name(e.name);
entries[s] = const_cast<gpt_entry *>(&e); entries[s] = const_cast<gpt_entry *>(&e);
} }
@ -123,19 +120,17 @@ int GptUtils::Load(void)
return 0; return 0;
} }
gpt_entry *GptUtils::GetPartitionEntry(std::string name) gpt_entry *GptUtils::GetPartitionEntry(std::string name) {
{ return entries.find(name) != entries.end() ? entries[name] : nullptr;
return entries.find(name) != entries.end() ? entries[name] : nullptr;
} }
int GptUtils::Sync(void) int GptUtils::Sync(void) {
{
if (!fd) if (!fd)
return -1; return -1;
// calculate crc and check if we need to update gpt // calculate crc and check if we need to update gpt
gpt_primary.entries_crc32 = crc32(0, reinterpret_cast<uint8_t *>(entry_array.data()), gpt_primary.entries_crc32 = crc32(0, reinterpret_cast<uint8_t *>(entry_array.data()),
entry_array.size() * sizeof(gpt_entry)); entry_array.size() * sizeof(gpt_entry));
// save old crc // save old crc
uint32_t crc = gpt_primary.crc32; uint32_t crc = gpt_primary.crc32;
@ -143,7 +138,7 @@ int GptUtils::Sync(void)
gpt_primary.crc32 = crc32(0, reinterpret_cast<uint8_t *>(&gpt_primary), sizeof gpt_primary); gpt_primary.crc32 = crc32(0, reinterpret_cast<uint8_t *>(&gpt_primary), sizeof gpt_primary);
if (crc == gpt_primary.crc32) if (crc == gpt_primary.crc32)
return 0; // nothing to do (no changes) return 0; // nothing to do (no changes)
ALOGI("updating GPT\n"); ALOGI("updating GPT\n");
@ -161,7 +156,7 @@ int GptUtils::Sync(void)
return -1; return -1;
} }
//update GPT backup entries and backup // update GPT backup entries and backup
lseek64(fd, block_size * gpt_backup.start_lba, SEEK_SET); lseek64(fd, block_size * gpt_backup.start_lba, SEEK_SET);
ret = write(fd, entry_array.data(), entry_array.size() * sizeof(gpt_entry)); ret = write(fd, entry_array.data(), entry_array.size() * sizeof(gpt_entry));
if (ret < 0) { if (ret < 0) {
@ -184,8 +179,7 @@ int GptUtils::Sync(void)
return 0; return 0;
} }
GptUtils::~GptUtils() GptUtils::~GptUtils() {
{
if (fd) { if (fd) {
Sync(); Sync();
close(fd); close(fd);

View file

@ -16,9 +16,9 @@
#pragma once #pragma once
#include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include <map>
namespace android { namespace android {
namespace hardware { namespace hardware {
@ -26,7 +26,7 @@ namespace boot {
namespace V1_2 { namespace V1_2 {
namespace implementation { namespace implementation {
#define GPT_SIGNATURE 0x5452415020494645UL #define GPT_SIGNATURE 0x5452415020494645UL
typedef struct { typedef struct {
uint8_t type_guid[16]; uint8_t type_guid[16];
@ -55,21 +55,21 @@ typedef struct {
} __attribute__((packed)) gpt_header; } __attribute__((packed)) gpt_header;
class GptUtils { class GptUtils {
public: public:
GptUtils(const std::string dev_path); GptUtils(const std::string dev_path);
int Load(void); int Load(void);
gpt_entry *GetPartitionEntry(std::string name); gpt_entry *GetPartitionEntry(std::string name);
int Sync(void); int Sync(void);
~GptUtils(); ~GptUtils();
private: private:
std::string dev_path; std::string dev_path;
int fd; int fd;
uint32_t block_size; uint32_t block_size;
gpt_header gpt_primary; gpt_header gpt_primary;
gpt_header gpt_backup; gpt_header gpt_backup;
std::vector<gpt_entry> entry_array; std::vector<gpt_entry> entry_array;
std::map<std::string, gpt_entry *>entries; std::map<std::string, gpt_entry *> entries;
}; };
} // namespace implementation } // namespace implementation

View file

@ -16,10 +16,11 @@
#define LOG_TAG "android.hardware.boot@1.2-service" #define LOG_TAG "android.hardware.boot@1.2-service"
#include <log/log.h> #include <android/hardware/boot/1.2/IBootControl.h>
#include <hidl/HidlTransportSupport.h> #include <hidl/HidlTransportSupport.h>
#include <hidl/Status.h> #include <hidl/Status.h>
#include <android/hardware/boot/1.2/IBootControl.h> #include <log/log.h>
#include "BootControl.h" #include "BootControl.h"
using ::android::status_t; using ::android::status_t;
@ -27,9 +28,9 @@ using ::android::status_t;
using ::android::hardware::boot::V1_2::IBootControl; using ::android::hardware::boot::V1_2::IBootControl;
using ::android::hardware::boot::V1_2::implementation::BootControl; using ::android::hardware::boot::V1_2::implementation::BootControl;
//using ::android::hardware::boot::implementation::BootControl; // using ::android::hardware::boot::implementation::BootControl;
int main (int /* argc */, char * /* argv */ []) { int main(int /* argc */, char * /* argv */[]) {
// This function must be called before you join to ensure the proper // This function must be called before you join to ensure the proper
// number of threads are created. The threadpool will never exceed // number of threads are created. The threadpool will never exceed
// size one because of this call. // size one because of this call.
@ -38,12 +39,12 @@ int main (int /* argc */, char * /* argv */ []) {
::android::sp bootctrl = new BootControl(); ::android::sp bootctrl = new BootControl();
const status_t status = bootctrl->registerAsService(); const status_t status = bootctrl->registerAsService();
if (status != ::android::OK) { if (status != ::android::OK) {
return 1; // or handle error return 1; // or handle error
} }
// Adds this thread to the threadpool, resulting in one total // Adds this thread to the threadpool, resulting in one total
// thread in the threadpool. We could also do other things, but // thread in the threadpool. We could also do other things, but
// would have to specify 'false' to willJoin in configureRpcThreadpool. // would have to specify 'false' to willJoin in configureRpcThreadpool.
::android::hardware::joinRpcThreadpool(); ::android::hardware::joinRpcThreadpool();
return 1; // joinRpcThreadpool should never return return 1; // joinRpcThreadpool should never return
} }