Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Fix-up coding style
Browse files Browse the repository at this point in the history
The .clang-format is a symlink to ../init/.clang-format, which is merged
recently. As init is the major user of fs_mgr, it's better to keep the
style consistent.

Only recent newly-added files written by me are formatted. For other
files, let's format them gradually to keep 'git blame' intact.

Bug: None
Test: Device can boot with AVB

Change-Id: I5c72f23b38534d5bcef3e4f9f0f477fa40496433
  • Loading branch information
bowgotsai committed Feb 18, 2017
1 parent 141e984 commit 4caf4c0
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 256 deletions.
1 change: 1 addition & 0 deletions fs_mgr/.clang-format
237 changes: 91 additions & 146 deletions fs_mgr/fs_mgr_avb.cpp

Large diffs are not rendered by default.

71 changes: 25 additions & 46 deletions fs_mgr/fs_mgr_avb_ops.cpp
Expand Up @@ -39,15 +39,11 @@
#include "fs_mgr_avb_ops.h"
#include "fs_mgr_priv.h"

static struct fstab *fs_mgr_fstab = nullptr;

static AvbIOResult read_from_partition(AvbOps *ops ATTRIBUTE_UNUSED,
const char *partition,
int64_t offset,
size_t num_bytes,
void *buffer,
size_t *out_num_read)
{
static struct fstab* fs_mgr_fstab = nullptr;

static AvbIOResult read_from_partition(AvbOps* ops ATTRIBUTE_UNUSED, const char* partition,
int64_t offset, size_t num_bytes, void* buffer,
size_t* out_num_read) {
// The input |partition| name is with ab_suffix, e.g. system_a.
// Slot suffix (e.g. _a) will be appended to the device file path
// for partitions having 'slotselect' optin in fstab file, but it
Expand All @@ -62,8 +58,7 @@ static AvbIOResult read_from_partition(AvbOps *ops ATTRIBUTE_UNUSED,
// - /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
// - /dev/block/platform/soc.0/7824900.sdhci/by-name/system_a

struct fstab_rec *fstab_entry =
fs_mgr_get_entry_for_mount_point(fs_mgr_fstab, "/misc");
struct fstab_rec* fstab_entry = fs_mgr_get_entry_for_mount_point(fs_mgr_fstab, "/misc");

if (fstab_entry == nullptr) {
LERROR << "/misc mount point not found in fstab";
Expand All @@ -86,8 +81,7 @@ static AvbIOResult read_from_partition(AvbOps *ops ATTRIBUTE_UNUSED,
return AVB_IO_RESULT_ERROR_IO;
}

android::base::unique_fd fd(
TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));

if (fd < 0) {
PERROR << "Failed to open " << path.c_str();
Expand All @@ -112,12 +106,11 @@ static AvbIOResult read_from_partition(AvbOps *ops ATTRIBUTE_UNUSED,

// On Linux, we never get partial reads from block devices (except
// for EOF).
ssize_t num_read =
TEMP_FAILURE_RETRY(pread64(fd, buffer, num_bytes, offset));
ssize_t num_read = TEMP_FAILURE_RETRY(pread64(fd, buffer, num_bytes, offset));

if (num_read < 0 || (size_t)num_read != num_bytes) {
PERROR << "Failed to read " << num_bytes << " bytes from "
<< path.c_str() << " offset " << offset;
PERROR << "Failed to read " << num_bytes << " bytes from " << path.c_str() << " offset "
<< offset;
return AVB_IO_RESULT_ERROR_IO;
}

Expand All @@ -128,25 +121,19 @@ static AvbIOResult read_from_partition(AvbOps *ops ATTRIBUTE_UNUSED,
return AVB_IO_RESULT_OK;
}

static AvbIOResult dummy_read_rollback_index(AvbOps *ops ATTRIBUTE_UNUSED,
size_t rollback_index_location
ATTRIBUTE_UNUSED,
uint64_t *out_rollback_index)
{
static AvbIOResult dummy_read_rollback_index(AvbOps* ops ATTRIBUTE_UNUSED,
size_t rollback_index_location ATTRIBUTE_UNUSED,
uint64_t* out_rollback_index) {
// rollback_index has been checked in bootloader phase.
// In user-space, returns the smallest value 0 to pass the check.
*out_rollback_index = 0;
return AVB_IO_RESULT_OK;
}

static AvbIOResult dummy_validate_vbmeta_public_key(
AvbOps *ops ATTRIBUTE_UNUSED,
const uint8_t *public_key_data ATTRIBUTE_UNUSED,
size_t public_key_length ATTRIBUTE_UNUSED,
const uint8_t *public_key_metadata ATTRIBUTE_UNUSED,
size_t public_key_metadata_length ATTRIBUTE_UNUSED,
bool *out_is_trusted)
{
AvbOps* ops ATTRIBUTE_UNUSED, const uint8_t* public_key_data ATTRIBUTE_UNUSED,
size_t public_key_length ATTRIBUTE_UNUSED, const uint8_t* public_key_metadata ATTRIBUTE_UNUSED,
size_t public_key_metadata_length ATTRIBUTE_UNUSED, bool* out_is_trusted) {
// vbmeta public key has been checked in bootloader phase.
// In user-space, returns true to pass the check.
//
Expand All @@ -158,22 +145,18 @@ static AvbIOResult dummy_validate_vbmeta_public_key(
return AVB_IO_RESULT_OK;
}

static AvbIOResult dummy_read_is_device_unlocked(AvbOps *ops ATTRIBUTE_UNUSED,
bool *out_is_unlocked)
{
static AvbIOResult dummy_read_is_device_unlocked(AvbOps* ops ATTRIBUTE_UNUSED,
bool* out_is_unlocked) {
// The function is for bootloader to update the value into
// androidboot.vbmeta.device_state in kernel cmdline.
// In user-space, returns true as we don't need to update it anymore.
*out_is_unlocked = true;
return AVB_IO_RESULT_OK;
}

static AvbIOResult dummy_get_unique_guid_for_partition(
AvbOps *ops ATTRIBUTE_UNUSED,
const char *partition ATTRIBUTE_UNUSED,
char *guid_buf,
size_t guid_buf_size)
{
static AvbIOResult dummy_get_unique_guid_for_partition(AvbOps* ops ATTRIBUTE_UNUSED,
const char* partition ATTRIBUTE_UNUSED,
char* guid_buf, size_t guid_buf_size) {
// The function is for bootloader to set the correct UUID
// for a given partition in kernel cmdline.
// In user-space, returns a faking one as we don't need to update
Expand All @@ -182,14 +165,13 @@ static AvbIOResult dummy_get_unique_guid_for_partition(
return AVB_IO_RESULT_OK;
}

AvbOps *fs_mgr_dummy_avb_ops_new(struct fstab *fstab)
{
AvbOps *ops;
AvbOps* fs_mgr_dummy_avb_ops_new(struct fstab* fstab) {
AvbOps* ops;

// Assigns the fstab to the static variable for later use.
fs_mgr_fstab = fstab;

ops = (AvbOps *)calloc(1, sizeof(AvbOps));
ops = (AvbOps*)calloc(1, sizeof(AvbOps));
if (ops == nullptr) {
LERROR << "Error allocating memory for AvbOps";
return nullptr;
Expand All @@ -207,7 +189,4 @@ AvbOps *fs_mgr_dummy_avb_ops_new(struct fstab *fstab)
return ops;
}

void fs_mgr_dummy_avb_ops_free(AvbOps *ops)
{
free(ops);
}
void fs_mgr_dummy_avb_ops_free(AvbOps* ops) { free(ops); }
4 changes: 2 additions & 2 deletions fs_mgr/fs_mgr_avb_ops.h
Expand Up @@ -49,10 +49,10 @@ __BEGIN_DECLS
*
* Frees with fs_mgr_dummy_avb_ops_free().
*/
AvbOps *fs_mgr_dummy_avb_ops_new(struct fstab *fstab);
AvbOps* fs_mgr_dummy_avb_ops_new(struct fstab* fstab);

/* Frees an AvbOps instance previously allocated with fs_mgr_avb_ops_new(). */
void fs_mgr_dummy_avb_ops_free(AvbOps *ops);
void fs_mgr_dummy_avb_ops_free(AvbOps* ops);

__END_DECLS

Expand Down
27 changes: 6 additions & 21 deletions fs_mgr/fs_mgr_dm_ioctl.cpp
Expand Up @@ -23,10 +23,7 @@
#include "fs_mgr_priv.h"
#include "fs_mgr_priv_dm_ioctl.h"

void fs_mgr_verity_ioctl_init(struct dm_ioctl *io,
const std::string &name,
unsigned flags)
{
void fs_mgr_verity_ioctl_init(struct dm_ioctl* io, const std::string& name, unsigned flags) {
memset(io, 0, DM_BUF_SIZE);
io->data_size = DM_BUF_SIZE;
io->data_start = sizeof(struct dm_ioctl);
Expand All @@ -39,10 +36,7 @@ void fs_mgr_verity_ioctl_init(struct dm_ioctl *io,
}
}

bool fs_mgr_create_verity_device(struct dm_ioctl *io,
const std::string &name,
int fd)
{
bool fs_mgr_create_verity_device(struct dm_ioctl* io, const std::string& name, int fd) {
fs_mgr_verity_ioctl_init(io, name, 1);
if (ioctl(fd, DM_DEV_CREATE, io)) {
PERROR << "Error creating device mapping";
Expand All @@ -51,10 +45,7 @@ bool fs_mgr_create_verity_device(struct dm_ioctl *io,
return true;
}

bool fs_mgr_destroy_verity_device(struct dm_ioctl *io,
const std::string &name,
int fd)
{
bool fs_mgr_destroy_verity_device(struct dm_ioctl* io, const std::string& name, int fd) {
fs_mgr_verity_ioctl_init(io, name, 0);
if (ioctl(fd, DM_DEV_REMOVE, io)) {
PERROR << "Error removing device mapping";
Expand All @@ -63,11 +54,8 @@ bool fs_mgr_destroy_verity_device(struct dm_ioctl *io,
return true;
}

bool fs_mgr_get_verity_device_name(struct dm_ioctl *io,
const std::string &name,
int fd,
std::string *out_dev_name)
{
bool fs_mgr_get_verity_device_name(struct dm_ioctl* io, const std::string& name, int fd,
std::string* out_dev_name) {
FS_MGR_CHECK(out_dev_name != nullptr);

fs_mgr_verity_ioctl_init(io, name, 0);
Expand All @@ -82,10 +70,7 @@ bool fs_mgr_get_verity_device_name(struct dm_ioctl *io,
return true;
}

bool fs_mgr_resume_verity_table(struct dm_ioctl *io,
const std::string &name,
int fd)
{
bool fs_mgr_resume_verity_table(struct dm_ioctl* io, const std::string& name, int fd) {
fs_mgr_verity_ioctl_init(io, name, 0);
if (ioctl(fd, DM_DEV_SUSPEND, io)) {
PERROR << "Error activating verity device";
Expand Down
4 changes: 2 additions & 2 deletions fs_mgr/fs_mgr_priv_avb.h
Expand Up @@ -45,11 +45,11 @@ bool fs_mgr_is_avb_used();
* developers to make the filesystem writable to allow replacing
* binaries on the device.
*/
int fs_mgr_load_vbmeta_images(struct fstab *fstab);
int fs_mgr_load_vbmeta_images(struct fstab* fstab);

void fs_mgr_unload_vbmeta_images();

int fs_mgr_setup_avb(struct fstab_rec *fstab_entry);
int fs_mgr_setup_avb(struct fstab_rec* fstab_entry);

__END_DECLS

Expand Down
24 changes: 7 additions & 17 deletions fs_mgr/fs_mgr_priv_dm_ioctl.h
Expand Up @@ -17,28 +17,18 @@
#ifndef __CORE_FS_MGR_PRIV_DM_IOCTL_H
#define __CORE_FS_MGR_PRIV_DM_IOCTL_H

#include <string>
#include <linux/dm-ioctl.h>
#include <string>

void fs_mgr_verity_ioctl_init(struct dm_ioctl *io,
const std::string &name,
unsigned flags);
void fs_mgr_verity_ioctl_init(struct dm_ioctl* io, const std::string& name, unsigned flags);

bool fs_mgr_create_verity_device(struct dm_ioctl *io,
const std::string &name,
int fd);
bool fs_mgr_create_verity_device(struct dm_ioctl* io, const std::string& name, int fd);

bool fs_mgr_destroy_verity_device(struct dm_ioctl *io,
const std::string &name,
int fd);
bool fs_mgr_destroy_verity_device(struct dm_ioctl* io, const std::string& name, int fd);

bool fs_mgr_get_verity_device_name(struct dm_ioctl *io,
const std::string &name,
int fd,
std::string *out_dev_name);
bool fs_mgr_get_verity_device_name(struct dm_ioctl* io, const std::string& name, int fd,
std::string* out_dev_name);

bool fs_mgr_resume_verity_table(struct dm_ioctl *io,
const std::string &name,
int fd);
bool fs_mgr_resume_verity_table(struct dm_ioctl* io, const std::string& name, int fd);

#endif /* __CORE_FS_MGR_PRIV_DM_IOCTL_H */
30 changes: 8 additions & 22 deletions fs_mgr/fs_mgr_priv_sha.h
Expand Up @@ -19,53 +19,39 @@

#include <openssl/sha.h>

class SHA256Hasher
{
class SHA256Hasher {
private:
SHA256_CTX sha256_ctx;
uint8_t hash[SHA256_DIGEST_LENGTH];

public:
enum { DIGEST_SIZE = SHA256_DIGEST_LENGTH };

SHA256Hasher()
{
SHA256_Init(&sha256_ctx);
}
SHA256Hasher() { SHA256_Init(&sha256_ctx); }

void update(const void *data, size_t data_size)
{
SHA256_Update(&sha256_ctx, data, data_size);
}
void update(const void* data, size_t data_size) { SHA256_Update(&sha256_ctx, data, data_size); }

const uint8_t *finalize()
{
const uint8_t* finalize() {
SHA256_Final(hash, &sha256_ctx);
return hash;
}
};

class SHA512Hasher
{
class SHA512Hasher {
private:
SHA512_CTX sha512_ctx;
uint8_t hash[SHA512_DIGEST_LENGTH];

public:
enum { DIGEST_SIZE = SHA512_DIGEST_LENGTH };

SHA512Hasher()
{
SHA512_Init(&sha512_ctx);
}
SHA512Hasher() { SHA512_Init(&sha512_ctx); }

void update(const uint8_t *data, size_t data_size)
{
void update(const uint8_t* data, size_t data_size) {
SHA512_Update(&sha512_ctx, data, data_size);
}

const uint8_t *finalize()
{
const uint8_t* finalize() {
SHA512_Final(hash, &sha512_ctx);
return hash;
}
Expand Down

0 comments on commit 4caf4c0

Please sign in to comment.