futility: put the recognizer functions in file_type.inc

This is preparation for a refactoring of how files are traversed.
file_type.inc will specify functions to recognize, show, or sign
each type of file.

This change puts the recognizer functions in file_type.inc, but
just stubs out the show and sign commands.

BUG=chromium:231574
BRANCH=none
TEST=make runtests

Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Change-Id: I1596a21319a8fb1182537abdf9be0196bef4b84b
Reviewed-on: https://chromium-review.googlesource.com/262893
Reviewed-by: Randall Spangler <rspangler@chromium.org>
This commit is contained in:
Bill Richardson
2015-03-26 15:31:54 -07:00
committed by ChromeOS Commit Bot
parent 45ecc3d447
commit 35c69cc159
7 changed files with 126 additions and 89 deletions

View File

@@ -18,45 +18,53 @@
#include "futility.h"
#include "gbb_header.h"
/* Human-readable strings */
static const struct {
const char * const name;
const char * const desc;
} type_strings[] = {
#define FILE_TYPE(A, B, C) {B, C},
/* Description and functions to handle each file type */
struct futil_file_type_s {
/* Short name for this type */
const char *name;
/* Human-readable description */
const char *desc;
/* Functions to identify, display, and sign this type of file. */
enum futil_file_type (*recognize)(uint8_t *buf, uint32_t len);
int (*show)(const char *name, uint8_t *buf, uint32_t len, void *data);
int (*sign)(const char *name, uint8_t *buf, uint32_t len, void *data);
};
/* Populate a list of file types and operator functions. */
static const struct futil_file_type_s const futil_file_types[] = {
{"unknown", "not something we know about", 0, 0, 0},
#define R_(x) x
#define S_(x) x
#define NONE 0
#define FILE_TYPE(A, B, C, D, E, F) {B, C, D, E, F},
#include "file_type.inc"
#undef FILE_TYPE
#undef NONE
#undef S_
#undef R_
};
const char * const futil_file_type_name(enum futil_file_type type)
{
if ((int) type < 0 || type >= NUM_FILE_TYPES)
type = FILE_TYPE_UNKNOWN;
return type_strings[type].name;
return futil_file_types[type].name;
}
const char * const futil_file_type_desc(enum futil_file_type type)
{
if ((int) type < 0 || type >= NUM_FILE_TYPES)
type = FILE_TYPE_UNKNOWN;
return type_strings[type].desc;
return futil_file_types[type].desc;
}
/* Name to enum. Returns true on success. */
int futil_file_str_to_type(const char *str, enum futil_file_type *tptr)
int futil_str_to_file_type(const char *str, enum futil_file_type *type)
{
int i;
for (i = 0; i < NUM_FILE_TYPES; i++)
if (!strcasecmp(str, type_strings[i].name)) {
if (tptr)
*tptr = i;
if (!strcasecmp(str, futil_file_types[i].name)) {
*type = i;
return 1;
}
if (tptr)
*tptr = FILE_TYPE_UNKNOWN;
*type = FILE_TYPE_UNKNOWN;
return 0;
}
@@ -66,49 +74,25 @@ 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++)
printf(" %-20s%s\n", type_strings[i].name,
type_strings[i].desc);
printf(" %-20s%s\n", futil_file_types[i].name,
futil_file_types[i].desc);
printf("\n");
exit(retval);
}
/* Try these in order so we recognize the larger objects first */
enum futil_file_type (*recognizers[])(uint8_t *buf, uint32_t len) = {
&recognize_gpt,
&recognize_bios_image,
&recognize_gbb,
&recognize_vblock1,
&recognize_vb1_key,
&recognize_vb2_key,
&recognize_pem,
};
int futil_str_to_file_type(const char *str, enum futil_file_type *type)
{
int i;
for (i = 0; i < NUM_FILE_TYPES; i++)
if (!strcasecmp(str, type_strings[i].name))
break;
if (i < NUM_FILE_TYPES) {
*type = i;
return 1;
}
return 0;
}
/* 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 < ARRAY_SIZE(recognizers); i++) {
type = recognizers[i](buf, len);
if (type != FILE_TYPE_UNKNOWN)
return type;
for (i = 0; i < NUM_FILE_TYPES; i++) {
if (futil_file_types[i].recognize) {
type = futil_file_types[i].recognize(buf, len);
if (type != FILE_TYPE_UNKNOWN)
return type;
}
}
return FILE_TYPE_UNKNOWN;

View File

@@ -8,7 +8,8 @@
/* What type of things do I know how to handle? */
enum futil_file_type {
#define FILE_TYPE(A, B, C) FILE_TYPE_ ## A,
FILE_TYPE_UNKNOWN,
#define FILE_TYPE(A, B, C, D, E, F) FILE_TYPE_ ## A,
#include "file_type.inc"
#undef FILE_TYPE
NUM_FILE_TYPES
@@ -16,16 +17,14 @@ enum futil_file_type {
/* Short name for file types */
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);
/* Name to enum. Returns true on success. */
int futil_file_str_to_type(const char *str, enum futil_file_type *tptr);
/* Print the list of type names and exit with the given value. */
void print_file_types_and_exit(int retval);
/* Lookup an type by name. Return true on success */
/* Lookup a type by name. Return true on success */
int futil_str_to_file_type(const char *str, enum futil_file_type *type);
/*
@@ -40,13 +39,17 @@ enum futil_file_type futil_file_type_buf(uint8_t *buf, uint32_t len);
enum futil_file_err futil_file_type(const char *filename,
enum futil_file_type *type);
/* Routines to identify particular file types. */
enum futil_file_type recognize_bios_image(uint8_t *buf, uint32_t len);
enum futil_file_type recognize_gbb(uint8_t *buf, uint32_t len);
enum futil_file_type recognize_vblock1(uint8_t *buf, uint32_t len);
enum futil_file_type recognize_gpt(uint8_t *buf, uint32_t len);
enum futil_file_type recognize_vb1_key(uint8_t *buf, uint32_t len);
enum futil_file_type recognize_vb2_key(uint8_t *buf, uint32_t len);
enum futil_file_type recognize_pem(uint8_t *buf, uint32_t len);
/* Declare the file_type functions. */
#define R_(FOO) \
enum futil_file_type FOO(uint8_t *buf, uint32_t len);
#define S_(FOO) \
int FOO(const char *name, uint8_t *buf, uint32_t len, void *data);
#define NONE
#define FILE_TYPE(A, B, C, D, E, F) D E F
#include "file_type.inc"
#undef FILE_TYPE
#undef NONE
#undef S_
#undef R_
#endif /* VBOOT_REFERENCE_FUTILITY_FILE_TYPE_H_ */

View File

@@ -3,21 +3,71 @@
* Copyright 2015 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* This declares the file types that we can handle. Note that the order may be
* important for types with recognizer functions, since we generally want to to
* look for big things first.
*/
/* enum --type desc */
FILE_TYPE(UNKNOWN, "unknown", "not something we know about")
FILE_TYPE(PUBKEY, "pubkey", "VbPublicKey (.vbpubk)")
FILE_TYPE(KEYBLOCK, "keyblock", "VbKeyBlock")
FILE_TYPE(FW_PREAMBLE, "fw_pre", "VbFirmwarePreamble (VBLOCK_A/B)")
FILE_TYPE(GBB, "gbb", "GBB")
FILE_TYPE(BIOS_IMAGE, "bios", "Chrome OS BIOS image")
FILE_TYPE(OLD_BIOS_IMAGE, "oldbios", "Cr-48 Chrome OS BIOS image")
FILE_TYPE(KERN_PREAMBLE, "kernel", "kernel preamble/partition")
FILE_TYPE(RAW_FIRMWARE, "fwblob", "raw firmware blob (FW_MAIN_A/B)")
FILE_TYPE(RAW_KERNEL, "vmlinuz", "raw linux kernel")
FILE_TYPE(CHROMIUMOS_DISK, "disk_img", "chromiumos disk image")
FILE_TYPE(PRIVKEY, "prikey", "VbPrivateKey (.vbprivk)")
FILE_TYPE(VB2_PUBKEY, "pubkey21", "vb21 public key (.vbpubk2)")
FILE_TYPE(VB2_PRIVKEY, "prikey21", "vb21 private key (.vbprik2)")
FILE_TYPE(PEM, "pem", "RSA private key (.pem)")
/*
* enum --type desc
* recognizer function
* show function
* sign function
*/
FILE_TYPE(PUBKEY, "pubkey", "VbPublicKey (.vbpubk)",
R_(ft_recognize_vb1_key),
NONE,
NONE)
FILE_TYPE(KEYBLOCK, "keyblock", "VbKeyBlock",
R_(ft_recognize_vblock1),
NONE,
NONE)
FILE_TYPE(FW_PREAMBLE, "fw_pre", "VbFirmwarePreamble (VBLOCK_A/B)",
R_(ft_recognize_vblock1),
NONE,
NONE)
FILE_TYPE(GBB, "gbb", "GBB",
R_(ft_recognize_gbb),
NONE,
NONE)
FILE_TYPE(BIOS_IMAGE, "bios", "Chrome OS BIOS image",
R_(ft_recognize_bios_image),
NONE,
NONE)
FILE_TYPE(OLD_BIOS_IMAGE, "oldbios", "Cr-48 Chrome OS BIOS image",
R_(ft_recognize_bios_image),
NONE,
NONE)
FILE_TYPE(KERN_PREAMBLE, "kernel", "kernel preamble/partition",
R_(ft_recognize_vblock1),
NONE,
NONE)
FILE_TYPE(RAW_FIRMWARE, "fwblob", "raw firmware blob (FW_MAIN_A/B)",
NONE,
NONE,
NONE)
FILE_TYPE(RAW_KERNEL, "vmlinuz", "raw linux kernel",
NONE,
NONE,
NONE)
FILE_TYPE(CHROMIUMOS_DISK, "disk_img", "chromiumos disk image",
NONE,
NONE,
NONE)
FILE_TYPE(PRIVKEY, "prikey", "VbPrivateKey (.vbprivk)",
R_(ft_recognize_vb1_key),
NONE,
NONE)
FILE_TYPE(VB2_PUBKEY, "pubkey21", "vb21 public key (.vbpubk2)",
R_(ft_recognize_vb2_key),
NONE,
NONE)
FILE_TYPE(VB2_PRIVKEY, "prikey21", "vb21 private key (.vbprik2)",
R_(ft_recognize_vb2_key),
NONE,
NONE)
FILE_TYPE(PEM, "pem", "RSA private key (.pem)",
R_(ft_recognize_pem),
NONE,
NONE)

View File

@@ -56,7 +56,7 @@ static inline uint32_t max(uint32_t a, uint32_t b)
return a > b ? a : b;
}
enum futil_file_type recognize_gbb(uint8_t *buf, uint32_t len)
enum futil_file_type ft_recognize_gbb(uint8_t *buf, uint32_t len)
{
GoogleBinaryBlockHeader *gbb = (GoogleBinaryBlockHeader *)buf;
@@ -301,7 +301,7 @@ enum futil_file_err futil_unmap_file(int fd, int writeable,
#define DISK_SECTOR_SIZE 512
enum futil_file_type recognize_gpt(uint8_t *buf, uint32_t len)
enum futil_file_type ft_recognize_gpt(uint8_t *buf, uint32_t len)
{
GptHeader *h;

View File

@@ -133,7 +133,7 @@ static int has_all_areas(uint8_t *buf, uint32_t len, FmapHeader *fmap,
return 1;
}
enum futil_file_type recognize_bios_image(uint8_t *buf, uint32_t len)
enum futil_file_type ft_recognize_bios_image(uint8_t *buf, uint32_t len)
{
FmapHeader *fmap = fmap_find(buf, len);
if (fmap) {

View File

@@ -715,7 +715,7 @@ uint8_t *CreateKernelBlob(uint8_t *vmlinuz_buf, uint64_t vmlinuz_size,
return g_kernel_blob_data;
}
enum futil_file_type recognize_vblock1(uint8_t *buf, uint32_t len)
enum futil_file_type ft_recognize_vblock1(uint8_t *buf, uint32_t len)
{
VbKeyBlockHeader *key_block = (VbKeyBlockHeader *)buf;
VbFirmwarePreambleHeader *fw_preamble;
@@ -745,7 +745,7 @@ enum futil_file_type recognize_vblock1(uint8_t *buf, uint32_t len)
return FILE_TYPE_UNKNOWN;
}
enum futil_file_type recognize_vb1_key(uint8_t *buf, uint32_t len)
enum futil_file_type ft_recognize_vb1_key(uint8_t *buf, uint32_t len)
{
VbPublicKey *pubkey = (VbPublicKey *)buf;
VbPrivateKey key;

View File

@@ -23,7 +23,7 @@
#include "futility.h"
#include "traversal.h"
enum futil_file_type recognize_vb2_key(uint8_t *buf, uint32_t len)
enum futil_file_type ft_recognize_vb2_key(uint8_t *buf, uint32_t len)
{
struct vb2_public_key pubkey;
struct vb2_private_key *privkey = 0;
@@ -168,7 +168,7 @@ static RSA *rsa_from_buffer(uint8_t *buf, uint32_t len)
return rsa_key;
}
enum futil_file_type recognize_pem(uint8_t *buf, uint32_t len)
enum futil_file_type ft_recognize_pem(uint8_t *buf, uint32_t len)
{
RSA *rsa_key = rsa_from_buffer(buf, len);
@@ -189,7 +189,7 @@ int futil_cb_show_pem(struct futil_traverse_state_s *state)
printf("Private Key file: %s\n", state->in_filename);
/* We're called only after recognize_pem, so this should work. */
/* We're called only after ft_recognize_pem, so this should work. */
rsa_key = rsa_from_buffer(state->my_area->buf, state->my_area->len);
if (!rsa_key)
DIE;