summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward O'Callaghan <quasisec@google.com>2023-03-07 22:16:57 +1100
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-03-25 12:52:28 +0000
commite1ad854c54e691234e2d8e6a60cbce83fed78d6e (patch)
treeacef3b297d134209294b394d7cf4b8e6f647f923
parentd5b59cc7ec723ae10161cfffd1c3a70a8caabe08 (diff)
downloadvboot-e1ad854c54e691234e2d8e6a60cbce83fed78d6e.tar.gz
futility/file_type*: Minor cleanups for maintainability
Use C99 features to scope indexer to loop constructs and local itermediates within more constrained lexical scopes. Be canonical in predicates of branch conditions. Use const correctness. Apply clang-format where reasonable BUG=b:268397597 TEST=`emerge-nissa vboot_reference`. TEST=`cros_run_unit_tests --host --packages vboot_reference`. TEST=`cros_run_unit_tests --board nissa --packages vboot_reference`. Change-Id: I8ce232f791a95a668ab5017cff628c199c36e917 Signed-off-by: Edward O'Callaghan <quasisec@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/4349558 Tested-by: Edward O'Callaghan <quasisec@chromium.org> Auto-Submit: Edward O'Callaghan <quasisec@chromium.org> Commit-Queue: Edward O'Callaghan <quasisec@chromium.org> Reviewed-by: Hung-Te Lin <hungte@chromium.org>
-rw-r--r--futility/file_type.c26
-rw-r--r--futility/file_type.h12
-rw-r--r--futility/file_type_bios.c55
-rw-r--r--futility/file_type_bios.h16
-rw-r--r--futility/file_type_rwsig.c32
-rw-r--r--futility/file_type_usbpd1.c75
6 files changed, 87 insertions, 129 deletions
diff --git a/futility/file_type.c b/futility/file_type.c
index aad3c9d1..527dcbe4 100644
--- a/futility/file_type.c
+++ b/futility/file_type.c
@@ -30,7 +30,7 @@ struct futil_file_type_s {
/* Populate a list of file types and operator functions. */
static const struct futil_file_type_s futil_file_types[] = {
- {"unknown", "not something we know about", 0, 0, 0},
+ {"unknown", "not something we know about", 0, 0, 0},
#define R_(x) x
#define S_(x) x
#define NONE 0
@@ -42,12 +42,12 @@ static const struct futil_file_type_s futil_file_types[] = {
#undef R_
};
-const char * const futil_file_type_name(enum futil_file_type type)
+const char *const futil_file_type_name(enum futil_file_type type)
{
return futil_file_types[type].name;
}
-const char * const futil_file_type_desc(enum futil_file_type type)
+const char *const futil_file_type_desc(enum futil_file_type type)
{
return futil_file_types[type].desc;
}
@@ -55,12 +55,12 @@ const char * const futil_file_type_desc(enum futil_file_type type)
/* Name to enum. Returns true on success. */
int futil_str_to_file_type(const char *str, enum futil_file_type *type)
{
- int i;
- for (i = 0; i < NUM_FILE_TYPES; i++)
+ for (enum futil_file_type i = 0; i < NUM_FILE_TYPES; i++) {
if (!strcasecmp(str, futil_file_types[i].name)) {
*type = i;
return 1;
}
+ }
*type = FILE_TYPE_UNKNOWN;
return 0;
@@ -69,9 +69,8 @@ int futil_str_to_file_type(const char *str, enum futil_file_type *type)
/* Print the list of type names and exit with the given value. */
void print_file_types_and_exit(int retval)
{
- int i;
printf("\nValid file types are:\n\n");
- for (i = 0; i < NUM_FILE_TYPES; i++)
+ for (enum futil_file_type i = 0; i < NUM_FILE_TYPES; i++)
printf(" %-20s%s\n", futil_file_types[i].name,
futil_file_types[i].desc);
printf("\n");
@@ -82,12 +81,9 @@ void print_file_types_and_exit(int retval)
/* Try to figure out what we're looking at */
enum futil_file_type futil_file_type_buf(uint8_t *buf, uint32_t len)
{
- enum futil_file_type type;
- int i;
-
- for (i = 0; i < NUM_FILE_TYPES; i++) {
+ for (enum futil_file_type i = 0; i < NUM_FILE_TYPES; i++) {
if (futil_file_types[i].recognize) {
- type = futil_file_types[i].recognize(buf, len);
+ enum futil_file_type type = futil_file_types[i].recognize(buf, len);
if (type != FILE_TYPE_UNKNOWN)
return type;
}
@@ -103,15 +99,14 @@ enum futil_file_err futil_file_type(const char *filename,
uint8_t *buf = NULL;
uint32_t buf_len = 0;
struct stat sb;
- enum futil_file_err err = FILE_ERR_NONE;
*type = FILE_TYPE_UNKNOWN;
- err = futil_open_file(filename, &ifd, FILE_RO);
+ enum futil_file_err err = futil_open_file(filename, &ifd, FILE_RO);
if (err != FILE_ERR_NONE)
goto done;
- if (0 != fstat(ifd, &sb)) {
+ if (fstat(ifd, &sb)) {
fprintf(stderr, "Can't stat input file: %s\n", strerror(errno));
err = FILE_ERR_STAT;
goto done;
@@ -131,6 +126,7 @@ enum futil_file_err futil_file_type(const char *filename,
} else if (S_ISSOCK(sb.st_mode)) {
err = FILE_ERR_SOCK;
}
+
done:
futil_unmap_and_close_file(ifd, FILE_RO, buf, buf_len);
return err;
diff --git a/futility/file_type.h b/futility/file_type.h
index bff6904d..5efd2405 100644
--- a/futility/file_type.h
+++ b/futility/file_type.h
@@ -16,10 +16,10 @@ enum futil_file_type {
};
/* Short name for file types */
-const char * const futil_file_type_name(enum futil_file_type type);
+const char *const futil_file_type_name(enum futil_file_type type);
/* Description of file type */
-const char * const futil_file_type_desc(enum futil_file_type type);
+const char *const futil_file_type_desc(enum futil_file_type type);
/* Print the list of type names and exit with the given value. */
void print_file_types_and_exit(int retval);
@@ -59,10 +59,8 @@ int futil_file_type_sign(enum futil_file_type type, const char *filename);
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wredundant-decls"
-#define R_(FOO) \
- enum futil_file_type FOO(uint8_t *buf, uint32_t len);
-#define S_(FOO) \
- int FOO(const char *name, void *data);
+#define R_(FOO) enum futil_file_type FOO(uint8_t *buf, uint32_t len);
+#define S_(FOO) int FOO(const char *name, void *data);
#define NONE
#define FILE_TYPE(A, B, C, D, E, F) D E F
#include "file_type.inc"
@@ -79,4 +77,4 @@ int show_fw_preamble_buf(const char *name, uint8_t *buf, uint32_t len,
int show_vb21_pubkey_buf(const char *name, uint8_t *buf, uint32_t len,
void *data);
-#endif /* VBOOT_REFERENCE_FILE_TYPE_H_ */
+#endif /* VBOOT_REFERENCE_FILE_TYPE_H_ */
diff --git a/futility/file_type_bios.c b/futility/file_type_bios.c
index 884dfc8e..295fdcd9 100644
--- a/futility/file_type_bios.c
+++ b/futility/file_type_bios.c
@@ -19,10 +19,9 @@
#include "host_common.h"
#include "vb1_helper.h"
-
static void fmap_limit_area(FmapAreaHeader *ah, uint32_t len)
{
- uint32_t sum = ah->area_offset + ah->area_size;
+ const uint32_t sum = ah->area_offset + ah->area_size;
if (sum < ah->area_size || sum > len) {
VB2_DEBUG("%.*s %#x + %#x > %#x\n", FMAP_NAMELEN,
ah->area_name, ah->area_offset, ah->area_size, len);
@@ -95,8 +94,7 @@ static int show_gbb_buf(const char *name, uint8_t *buf, uint32_t len,
}
pubkey = (struct vb2_packed_key *)(buf + gbb->recovery_key_offset);
- if (vb2_packed_key_looks_ok(pubkey, gbb->recovery_key_size)
- == VB2_SUCCESS) {
+ if (vb2_packed_key_looks_ok(pubkey, gbb->recovery_key_size) == VB2_SUCCESS) {
if (state) {
state->recovery_key.offset =
state->area[BIOS_FMAP_GBB].offset +
@@ -121,12 +119,11 @@ static int show_gbb_buf(const char *name, uint8_t *buf, uint32_t len,
int ft_show_gbb(const char *name, void *data)
{
- int retval = 0;
int fd = -1;
uint8_t *buf;
uint32_t len;
- retval = futil_open_and_map_file(name, &fd, FILE_RO, &buf, &len);
+ int retval = futil_open_and_map_file(name, &fd, FILE_RO, &buf, &len);
if (retval)
return 1;
@@ -177,32 +174,27 @@ _Static_assert(ARRAY_SIZE(fmap_show_fn) == NUM_BIOS_COMPONENTS,
int ft_show_bios(const char *name, void *data)
{
- FmapHeader *fmap;
- FmapAreaHeader *ah = 0;
- char ah_name[FMAP_NAMELEN + 1];
- enum bios_component c;
- int retval = 0;
- struct bios_state_s state;
+ struct bios_state_s state = {0}; /* loop inc state on each pass. */
int fd = -1;
uint8_t *buf;
uint32_t len;
- retval = futil_open_and_map_file(name, &fd, FILE_RO, &buf, &len);
+ int retval = futil_open_and_map_file(name, &fd, FILE_RO, &buf, &len);
if (retval)
return 1;
- memset(&state, 0, sizeof(state));
-
printf("BIOS: %s\n", name);
/* We've already checked, so we know this will work. */
- fmap = fmap_find(buf, len);
- for (c = 0; c < NUM_BIOS_COMPONENTS; c++) {
+ FmapHeader *fmap = fmap_find(buf, len);
+ for (enum bios_component c = 0; c < NUM_BIOS_COMPONENTS; c++) {
+ FmapAreaHeader *ah = NULL;
/* We know one of these will work, too */
if (fmap_find_by_name(buf, len, fmap, fmap_name[c], &ah)) {
/* But the file might be truncated */
fmap_limit_area(ah, len);
/* The name is not necessarily null-terminated */
+ char ah_name[FMAP_NAMELEN + 1];
snprintf(ah_name, sizeof(ah_name), "%s", ah->area_name);
/* Update the state we're passing around */
@@ -235,10 +227,9 @@ static int write_new_preamble(struct bios_area_s *vblock,
struct vb2_private_key *signkey,
struct vb2_keyblock *keyblock)
{
- struct vb2_signature *body_sig = NULL;
- struct vb2_fw_preamble *preamble = NULL;
int retval = 1;
+ struct vb2_signature *body_sig;
if (fw_body->metadata_hash.algo != VB2_HASH_INVALID)
body_sig =
vb2_create_signature_from_hash(&fw_body->metadata_hash);
@@ -248,10 +239,10 @@ static int write_new_preamble(struct bios_area_s *vblock,
if (!body_sig) {
ERROR("Error calculating or creating body signature\n");
- goto end;
+ return 1;
}
- preamble = vb2_create_fw_preamble(vblock->version,
+ struct vb2_fw_preamble *preamble = vb2_create_fw_preamble(vblock->version,
(struct vb2_packed_key *)sign_option.kernel_subkey,
body_sig,
signkey,
@@ -283,8 +274,7 @@ end:
static int write_loem(const char *ab, struct bios_area_s *vblock)
{
char filename[PATH_MAX];
- int n;
- n = snprintf(filename, sizeof(filename), "%s/vblock_%s.%s",
+ int n = snprintf(filename, sizeof(filename), "%s/vblock_%s.%s",
sign_option.loemdir ? sign_option.loemdir : ".",
ab, sign_option.loemid);
if (n >= sizeof(filename)) {
@@ -356,20 +346,19 @@ static int prepare_slot(uint8_t *buf, uint32_t len, enum bios_component fw_c,
enum bios_component vblock_c,
struct bios_state_s *state)
{
- FmapHeader *fmap;
- FmapAreaHeader *ah;
const char *fw_main_name = fmap_name[fw_c];
const char *vblock_name = fmap_name[vblock_c];
static uint8_t workbuf[VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE]
__attribute__((aligned(VB2_WORKBUF_ALIGN)));
static struct vb2_workbuf wb;
- fmap = fmap_find(buf, len);
+ FmapHeader *fmap = fmap_find(buf, len);
vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
VB2_DEBUG("Preparing areas: %s and %s\n", fw_main_name, vblock_name);
/* FW_MAIN */
+ FmapAreaHeader *ah;
if (!fmap_find_by_name(buf, len, fmap, fw_main_name, &ah)) {
ERROR("%s area not found in FMAP\n", fw_main_name);
return 1;
@@ -533,15 +522,11 @@ static void check_slot_after_prepare(enum bios_component fw_c,
int ft_sign_bios(const char *name, void *data)
{
- int retval = 0;
- struct bios_state_s state;
+ struct bios_state_s state = {0};
int fd = -1;
uint8_t *buf = NULL;
uint32_t len = 0;
- bool uses_cbfs_integration =
- image_uses_cbfs_integration(name);
-
- memset(&state, 0, sizeof(state));
+ bool uses_cbfs_integration = image_uses_cbfs_integration(name);
image_check_and_prepare_cbfs(name, BIOS_FMAP_FW_MAIN_A,
uses_cbfs_integration, &state);
@@ -552,7 +537,7 @@ int ft_sign_bios(const char *name, void *data)
&buf, &len))
return 1;
- retval = prepare_slot(buf, len, BIOS_FMAP_FW_MAIN_A, BIOS_FMAP_VBLOCK_A,
+ int retval = prepare_slot(buf, len, BIOS_FMAP_FW_MAIN_A, BIOS_FMAP_VBLOCK_A,
&state);
if (retval)
goto done;
@@ -575,9 +560,7 @@ done:
enum futil_file_type ft_recognize_bios_image(uint8_t *buf, uint32_t len)
{
- FmapHeader *fmap;
-
- fmap = fmap_find(buf, len);
+ FmapHeader *fmap = fmap_find(buf, len);
if (!fmap)
return FILE_TYPE_UNKNOWN;
diff --git a/futility/file_type_bios.h b/futility/file_type_bios.h
index 1f957b1c..8c88fb1d 100644
--- a/futility/file_type_bios.h
+++ b/futility/file_type_bios.h
@@ -24,19 +24,19 @@ enum bios_component {
NUM_BIOS_COMPONENTS
};
-static const char * const fmap_name[] = {
- "GBB", /* BIOS_FMAP_GBB */
- "FW_MAIN_A", /* BIOS_FMAP_FW_MAIN_A */
- "FW_MAIN_B", /* BIOS_FMAP_FW_MAIN_B */
- "VBLOCK_A", /* BIOS_FMAP_VBLOCK_A */
- "VBLOCK_B", /* BIOS_FMAP_VBLOCK_B */
+static const char *const fmap_name[] = {
+ "GBB", /* BIOS_FMAP_GBB */
+ "FW_MAIN_A", /* BIOS_FMAP_FW_MAIN_A */
+ "FW_MAIN_B", /* BIOS_FMAP_FW_MAIN_B */
+ "VBLOCK_A", /* BIOS_FMAP_VBLOCK_A */
+ "VBLOCK_B", /* BIOS_FMAP_VBLOCK_B */
};
_Static_assert(ARRAY_SIZE(fmap_name) == NUM_BIOS_COMPONENTS,
"Size of fmap_name[] should match NUM_BIOS_COMPONENTS");
/* Location information for each component */
struct bios_area_s {
- uint32_t offset; /* to avoid pointer math */
+ uint32_t offset; /* to avoid pointer math */
uint8_t *buf;
uint32_t len;
uint32_t is_valid;
@@ -60,4 +60,4 @@ struct bios_state_s {
struct bios_area_s rootkey;
};
-#endif /* VBOOT_REFERENCE_FILE_TYPE_BIOS_H_ */
+#endif /* VBOOT_REFERENCE_FILE_TYPE_BIOS_H_ */
diff --git a/futility/file_type_rwsig.c b/futility/file_type_rwsig.c
index 56d92c5f..b5913a35 100644
--- a/futility/file_type_rwsig.c
+++ b/futility/file_type_rwsig.c
@@ -52,12 +52,10 @@ static void show_sig(const char *name, const struct vb21_signature *sig)
int ft_show_rwsig(const char *name, void *nuthin)
{
- const struct vb21_signature *sig = 0;
const struct vb21_packed_key *pkey = show_option.pkey;
struct vb2_public_key key;
uint8_t workbuf[VB2_VERIFY_DATA_WORKBUF_BYTES]
- __attribute__((aligned(VB2_WORKBUF_ALIGN)));
- struct vb2_workbuf wb;
+ __attribute__((aligned(VB2_WORKBUF_ALIGN)));
uint32_t data_size, sig_size = SIGNATURE_RSVD_SIZE;
uint32_t total_data_size = 0;
uint8_t *data;
@@ -66,18 +64,16 @@ int ft_show_rwsig(const char *name, void *nuthin)
int fd = -1;
uint8_t *buf;
uint32_t len;
- int rv;
+ int rv = 1;
if (futil_open_and_map_file(name, &fd, FILE_RO, &buf, &len))
return 1;
- rv = 1;
-
VB2_DEBUG("name %s len 0x%08x (%d)\n", name, len, len);
/* Am I just looking at a signature file? */
VB2_DEBUG("Looking for signature at 0x0\n");
- sig = (const struct vb21_signature *)buf;
+ const struct vb21_signature *sig = (const struct vb21_signature *)buf;
if (VB2_SUCCESS == vb21_verify_signature(sig, len)) {
show_sig(name, sig);
if (!show_option.fv) {
@@ -115,7 +111,7 @@ int ft_show_rwsig(const char *name, void *nuthin)
sig_size = fmaparea->area_size;
VB2_DEBUG("Looking for signature at %#tx (%#x)\n",
- (uint8_t*)sig - buf, sig_size);
+ (uint8_t *)sig - buf, sig_size);
if (VB2_SUCCESS != vb21_verify_signature(sig, sig_size))
goto done;
@@ -127,7 +123,7 @@ int ft_show_rwsig(const char *name, void *nuthin)
* TODO(crosbug.com/p/62231): EC_RW region should not include
* the signature.
*/
- total_data_size = fmaparea->area_size-sig_size;
+ total_data_size = fmaparea->area_size - sig_size;
if (!data) {
VB2_DEBUG("No EC_RW in FMAP.\n");
@@ -164,9 +160,7 @@ int ft_show_rwsig(const char *name, void *nuthin)
}
/* We already did this once, so it should work again */
- if (vb21_unpack_key(&key,
- (const uint8_t *)pkey,
- pkey->c.total_size)) {
+ if (vb21_unpack_key(&key, (const uint8_t *)pkey, pkey->c.total_size)) {
VB2_DEBUG("Can't unpack pubkey\n");
goto done;
}
@@ -180,6 +174,7 @@ int ft_show_rwsig(const char *name, void *nuthin)
{
uint8_t sigbuf[sig->c.total_size];
memcpy(sigbuf, sig, sizeof(sigbuf));
+ struct vb2_workbuf wb;
vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
@@ -252,7 +247,7 @@ int ft_sign_rwsig(const char *name, void *nuthin)
sig_size = fmaparea->area_size;
VB2_DEBUG("Looking for signature at %#tx (%#x)\n",
- (uint8_t*)old_sig - buf, sig_size);
+ (uint8_t *)old_sig - buf, sig_size);
data = fmap_find_by_name(buf, len, fmap, "EC_RW",
&fmaparea);
@@ -353,14 +348,14 @@ int ft_sign_rwsig(const char *name, void *nuthin)
/* Create the public key */
if (vb2_public_key_alloc(&pubkey,
- sign_option.prikey->sig_alg)) {
+ sign_option.prikey->sig_alg)) {
fprintf(stderr, "Unable to allocate the public key\n");
goto done;
}
/* Extract the keyb blob */
if (vb_keyb_from_rsa(sign_option.prikey->rsa_private_key,
- &keyb_data, &keyb_size)) {
+ &keyb_data, &keyb_size)) {
fprintf(stderr, "Couldn't extract the public key\n");
goto done;
}
@@ -385,7 +380,7 @@ int ft_sign_rwsig(const char *name, void *nuthin)
vb2_public_key_set_desc(pubkey, sign_option.prikey->desc);
memcpy((struct vb2_id *)pubkey->id, &sign_option.prikey->id,
- sizeof(*(pubkey->id)));
+ sizeof(*(pubkey->id)));
if (vb21_public_key_pack(&packedkey, pubkey)) {
goto done;
@@ -400,7 +395,7 @@ int ft_sign_rwsig(const char *name, void *nuthin)
/* Overwrite the old signature */
if (packedkey->c.total_size > fmaparea->area_size) {
fprintf(stderr, "New sig is too large (%d > %d)\n",
- packedkey->c.total_size, sig_size);
+ packedkey->c.total_size, sig_size);
goto done;
}
@@ -423,14 +418,13 @@ done:
enum futil_file_type ft_recognize_rwsig(uint8_t *buf, uint32_t len)
{
- FmapHeader *fmap;
const struct vb21_signature *sig = NULL;
uint32_t sig_size;
if (!vb21_verify_signature((const struct vb21_signature *)buf, len))
return FILE_TYPE_RWSIG;
- fmap = fmap_find(buf, len);
+ FmapHeader *fmap = fmap_find(buf, len);
if (fmap) {
/* This looks like a full image. */
FmapAreaHeader *fmaparea;
diff --git a/futility/file_type_usbpd1.c b/futility/file_type_usbpd1.c
index dca5d6ae..056b994e 100644
--- a/futility/file_type_usbpd1.c
+++ b/futility/file_type_usbpd1.c
@@ -30,15 +30,15 @@
#include "util_misc.h"
/* Return 1 if okay, 0 if not */
-static int parse_size_opts(uint32_t len,
+static int parse_size_opts(const uint32_t len,
uint32_t *ro_size_ptr, uint32_t *rw_size_ptr,
uint32_t *ro_offset_ptr, uint32_t * rw_offset_ptr)
{
- uint32_t ro_size, rw_size, ro_offset, rw_offset;
-
/* Assume the image has both RO and RW, evenly split. */
- ro_offset = 0;
- ro_size = rw_size = rw_offset = len / 2;
+ uint32_t ro_offset = 0;
+ uint32_t ro_size = len / 2;
+ uint32_t rw_size = len / 2;
+ uint32_t rw_offset = len / 2;
/* Unless told otherwise... */
if (sign_option.ro_size != 0xffffffff)
@@ -99,7 +99,7 @@ int ft_sign_usbpd1(const char *name, void *data)
int fd = -1;
if (futil_open_and_map_file(name, &fd, FILE_MODE_SIGN(sign_option),
- &buf, &len))
+ &buf, &len))
return 1;
VB2_DEBUG("name %s len %#.8x (%d)\n", name, len, len);
@@ -157,11 +157,9 @@ int ft_sign_usbpd1(const char *name, void *data)
}
/* Okay, looking good. Update the signature. */
- memcpy(buf + sig_offset,
- (uint8_t *)sig_ptr + sig_ptr->sig_offset,
+ memcpy(buf + sig_offset, (uint8_t *)sig_ptr + sig_ptr->sig_offset,
sig_ptr->sig_size);
-
/* If there's no RO section, we're done. */
if (!ro_size) {
retval = 0;
@@ -227,17 +225,11 @@ int ft_sign_usbpd1(const char *name, void *data)
VB2_DEBUG("pub_pad 0x%08x\n", pub_pad);
/* Copy n[nwords] */
- memcpy(buf + dst_ofs_n,
- keyb_data + src_ofs_n,
- nbytes);
+ memcpy(buf + dst_ofs_n, keyb_data + src_ofs_n, nbytes);
/* Copy rr[nwords] */
- memcpy(buf + dst_ofs_rr,
- keyb_data + src_ofs_rr,
- nbytes);
+ memcpy(buf + dst_ofs_rr, keyb_data + src_ofs_rr, nbytes);
/* Copy n0inv */
- memcpy(buf + dst_ofs_n0inv,
- keyb_data + src_ofs_n0inv,
- 4);
+ memcpy(buf + dst_ofs_n0inv, keyb_data + src_ofs_n0inv, 4);
/* Pad with 0xff */
memset(buf + dst_ofs_n0inv + 4, 0xff, pub_pad);
@@ -253,7 +245,6 @@ done:
return retval;
}
-
/*
* Algorithms that we want to try, in order. We've only ever shipped with
* RSA2048 / SHA256, but the others should work in tests.
@@ -326,7 +317,7 @@ static vb2_error_t vb21_sig_from_usbpd1(struct vb21_signature **sig,
.sig_size = vb2_rsa_sig_size(sig_alg),
.sig_offset = sizeof(s),
};
- uint32_t total_size = sizeof(s) + o_sig_size;
+ const uint32_t total_size = sizeof(s) + o_sig_size;
uint8_t *buf = calloc(1, total_size);
if (!buf)
return VB2_ERROR_UNKNOWN;
@@ -369,7 +360,6 @@ static void show_usbpd1_stuff(const char *name,
free(pkey);
}
-
/* Returns VB2_SUCCESS or random error code */
static vb2_error_t try_our_own(enum vb2_signature_algorithm sig_alg,
enum vb2_hash_algorithm hash_alg,
@@ -410,19 +400,18 @@ static vb2_error_t check_self_consistency(const uint8_t *buf, const char *name,
enum vb2_hash_algorithm hash_alg)
{
/* Where are the important bits? */
- uint32_t sig_size = vb2_rsa_sig_size(sig_alg);
- uint32_t sig_offset = rw_offset + rw_size - sig_size;
- uint32_t pubkey_size = usbpd1_packed_key_size(sig_alg);
- uint32_t pubkey_offset = ro_offset + ro_size - pubkey_size;
- vb2_error_t rv;
+ const uint32_t sig_size = vb2_rsa_sig_size(sig_alg);
+ const uint32_t sig_offset = rw_offset + rw_size - sig_size;
+ const uint32_t pubkey_size = usbpd1_packed_key_size(sig_alg);
+ const uint32_t pubkey_offset = ro_offset + ro_size - pubkey_size;
/* Skip stuff that obviously doesn't work */
if (sig_size > rw_size || pubkey_size > ro_size)
return VB2_ERROR_UNKNOWN;
- rv = try_our_own(sig_alg, hash_alg, /* algs */
- buf + pubkey_offset, pubkey_size, /* pubkey blob */
- buf + sig_offset, sig_size, /* sig blob */
+ vb2_error_t rv = try_our_own(sig_alg, hash_alg, /* algs */
+ buf + pubkey_offset, pubkey_size, /* pubkey blob */
+ buf + sig_offset, sig_size, /* sig blob */
buf + rw_offset, rw_size - sig_size); /* RW image */
if (rv == VB2_SUCCESS && name)
@@ -432,11 +421,8 @@ static vb2_error_t check_self_consistency(const uint8_t *buf, const char *name,
return rv;
}
-
int ft_show_usbpd1(const char *name, void *data)
{
- uint32_t ro_size, rw_size, ro_offset, rw_offset;
- int s, h;
int fd = -1;
uint8_t *buf;
uint32_t len;
@@ -448,6 +434,7 @@ int ft_show_usbpd1(const char *name, void *data)
VB2_DEBUG("name %s len 0x%08x (%d)\n", name, len, len);
/* Get image locations */
+ uint32_t ro_size, rw_size, ro_offset, rw_offset;
if (!parse_size_opts(len, &ro_size, &rw_size, &ro_offset, &rw_offset))
goto done;
@@ -459,8 +446,8 @@ int ft_show_usbpd1(const char *name, void *data)
}
/* TODO: Only loop through the numbers we haven't been given */
- for (s = 0; s < ARRAY_SIZE(sigs); s++) {
- for (h = 0; h < ARRAY_SIZE(hashes); h++) {
+ for (enum vb2_signature_algorithm s = 0; s < ARRAY_SIZE(sigs); s++) {
+ for (enum vb2_hash_algorithm h = 0; h < ARRAY_SIZE(hashes); h++) {
if (!check_self_consistency(buf, name, ro_size, rw_size,
ro_offset, rw_offset,
sigs[s], hashes[h])) {
@@ -478,9 +465,6 @@ done:
enum futil_file_type ft_recognize_usbpd1(uint8_t *buf, uint32_t len)
{
- uint32_t ro_size, rw_size, ro_offset, rw_offset;
- int s, h;
-
/*
* Since we don't use any headers to identify or locate the pubkey and
* signature, in order to identify blob as the right type we have to
@@ -488,16 +472,19 @@ enum futil_file_type ft_recognize_usbpd1(uint8_t *buf, uint32_t len)
* split. Then we just try to use what we think might be the pubkey to
* validate what we think might be the signature.
*/
- ro_offset = 0;
- ro_size = rw_size = rw_offset = len / 2;
-
- for (s = 0; s < ARRAY_SIZE(sigs); s++)
- for (h = 0; h < ARRAY_SIZE(hashes); h++)
- if (!check_self_consistency(buf, 0,
- ro_size, rw_size,
+ const uint32_t ro_offset = 0;
+ const uint32_t ro_size = len / 2;
+ const uint32_t rw_size = len / 2;
+ const uint32_t rw_offset = len / 2;
+
+ for (enum vb2_signature_algorithm s = 0; s < ARRAY_SIZE(sigs); s++) {
+ for (enum vb2_hash_algorithm h = 0; h < ARRAY_SIZE(hashes); h++) {
+ if (!check_self_consistency(buf, 0, ro_size, rw_size,
ro_offset, rw_offset,
sigs[s], hashes[h]))
return FILE_TYPE_USBPD1;
+ }
+ }
return FILE_TYPE_UNKNOWN;
}