mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-11-27 11:44:02 +00:00
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:
committed by
ChromeOS Commit Bot
parent
45ecc3d447
commit
35c69cc159
@@ -18,45 +18,53 @@
|
|||||||
#include "futility.h"
|
#include "futility.h"
|
||||||
#include "gbb_header.h"
|
#include "gbb_header.h"
|
||||||
|
|
||||||
/* Human-readable strings */
|
/* Description and functions to handle each file type */
|
||||||
static const struct {
|
struct futil_file_type_s {
|
||||||
const char * const name;
|
/* Short name for this type */
|
||||||
const char * const desc;
|
const char *name;
|
||||||
} type_strings[] = {
|
/* Human-readable description */
|
||||||
#define FILE_TYPE(A, B, C) {B, C},
|
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"
|
#include "file_type.inc"
|
||||||
#undef FILE_TYPE
|
#undef FILE_TYPE
|
||||||
|
#undef NONE
|
||||||
|
#undef S_
|
||||||
|
#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)
|
||||||
{
|
{
|
||||||
if ((int) type < 0 || type >= NUM_FILE_TYPES)
|
return futil_file_types[type].name;
|
||||||
type = FILE_TYPE_UNKNOWN;
|
|
||||||
|
|
||||||
return type_strings[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)
|
||||||
{
|
{
|
||||||
if ((int) type < 0 || type >= NUM_FILE_TYPES)
|
return futil_file_types[type].desc;
|
||||||
type = FILE_TYPE_UNKNOWN;
|
|
||||||
|
|
||||||
return type_strings[type].desc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Name to enum. Returns true on success. */
|
/* 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;
|
int i;
|
||||||
for (i = 0; i < NUM_FILE_TYPES; i++)
|
for (i = 0; i < NUM_FILE_TYPES; i++)
|
||||||
if (!strcasecmp(str, type_strings[i].name)) {
|
if (!strcasecmp(str, futil_file_types[i].name)) {
|
||||||
if (tptr)
|
*type = i;
|
||||||
*tptr = i;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tptr)
|
*type = FILE_TYPE_UNKNOWN;
|
||||||
*tptr = FILE_TYPE_UNKNOWN;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,50 +74,26 @@ void print_file_types_and_exit(int retval)
|
|||||||
int i;
|
int i;
|
||||||
printf("\nValid file types are:\n\n");
|
printf("\nValid file types are:\n\n");
|
||||||
for (i = 0; i < NUM_FILE_TYPES; i++)
|
for (i = 0; i < NUM_FILE_TYPES; i++)
|
||||||
printf(" %-20s%s\n", type_strings[i].name,
|
printf(" %-20s%s\n", futil_file_types[i].name,
|
||||||
type_strings[i].desc);
|
futil_file_types[i].desc);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
exit(retval);
|
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 */
|
/* 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 futil_file_type_buf(uint8_t *buf, uint32_t len)
|
||||||
{
|
{
|
||||||
enum futil_file_type type;
|
enum futil_file_type type;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_SIZE(recognizers); i++) {
|
for (i = 0; i < NUM_FILE_TYPES; i++) {
|
||||||
type = recognizers[i](buf, len);
|
if (futil_file_types[i].recognize) {
|
||||||
|
type = futil_file_types[i].recognize(buf, len);
|
||||||
if (type != FILE_TYPE_UNKNOWN)
|
if (type != FILE_TYPE_UNKNOWN)
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return FILE_TYPE_UNKNOWN;
|
return FILE_TYPE_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,8 @@
|
|||||||
|
|
||||||
/* What type of things do I know how to handle? */
|
/* What type of things do I know how to handle? */
|
||||||
enum futil_file_type {
|
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"
|
#include "file_type.inc"
|
||||||
#undef FILE_TYPE
|
#undef FILE_TYPE
|
||||||
NUM_FILE_TYPES
|
NUM_FILE_TYPES
|
||||||
@@ -16,16 +17,14 @@ enum futil_file_type {
|
|||||||
|
|
||||||
/* Short name for file types */
|
/* 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 */
|
/* 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);
|
||||||
|
|
||||||
/* 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. */
|
/* Print the list of type names and exit with the given value. */
|
||||||
void print_file_types_and_exit(int retval);
|
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);
|
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_err futil_file_type(const char *filename,
|
||||||
enum futil_file_type *type);
|
enum futil_file_type *type);
|
||||||
|
|
||||||
/* Routines to identify particular file types. */
|
/* Declare the file_type functions. */
|
||||||
enum futil_file_type recognize_bios_image(uint8_t *buf, uint32_t len);
|
#define R_(FOO) \
|
||||||
enum futil_file_type recognize_gbb(uint8_t *buf, uint32_t len);
|
enum futil_file_type FOO(uint8_t *buf, uint32_t len);
|
||||||
enum futil_file_type recognize_vblock1(uint8_t *buf, uint32_t len);
|
#define S_(FOO) \
|
||||||
enum futil_file_type recognize_gpt(uint8_t *buf, uint32_t len);
|
int FOO(const char *name, uint8_t *buf, uint32_t len, void *data);
|
||||||
enum futil_file_type recognize_vb1_key(uint8_t *buf, uint32_t len);
|
#define NONE
|
||||||
enum futil_file_type recognize_vb2_key(uint8_t *buf, uint32_t len);
|
#define FILE_TYPE(A, B, C, D, E, F) D E F
|
||||||
enum futil_file_type recognize_pem(uint8_t *buf, uint32_t len);
|
#include "file_type.inc"
|
||||||
|
#undef FILE_TYPE
|
||||||
|
#undef NONE
|
||||||
|
#undef S_
|
||||||
|
#undef R_
|
||||||
|
|
||||||
#endif /* VBOOT_REFERENCE_FUTILITY_FILE_TYPE_H_ */
|
#endif /* VBOOT_REFERENCE_FUTILITY_FILE_TYPE_H_ */
|
||||||
|
|||||||
@@ -3,21 +3,71 @@
|
|||||||
* Copyright 2015 The Chromium OS Authors. All rights reserved.
|
* Copyright 2015 The Chromium OS Authors. All rights reserved.
|
||||||
* Use of this source code is governed by a BSD-style license that can be
|
* Use of this source code is governed by a BSD-style license that can be
|
||||||
* found in the LICENSE file.
|
* 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")
|
* enum --type desc
|
||||||
FILE_TYPE(PUBKEY, "pubkey", "VbPublicKey (.vbpubk)")
|
* recognizer function
|
||||||
FILE_TYPE(KEYBLOCK, "keyblock", "VbKeyBlock")
|
* show function
|
||||||
FILE_TYPE(FW_PREAMBLE, "fw_pre", "VbFirmwarePreamble (VBLOCK_A/B)")
|
* sign function
|
||||||
FILE_TYPE(GBB, "gbb", "GBB")
|
*/
|
||||||
FILE_TYPE(BIOS_IMAGE, "bios", "Chrome OS BIOS image")
|
FILE_TYPE(PUBKEY, "pubkey", "VbPublicKey (.vbpubk)",
|
||||||
FILE_TYPE(OLD_BIOS_IMAGE, "oldbios", "Cr-48 Chrome OS BIOS image")
|
R_(ft_recognize_vb1_key),
|
||||||
FILE_TYPE(KERN_PREAMBLE, "kernel", "kernel preamble/partition")
|
NONE,
|
||||||
FILE_TYPE(RAW_FIRMWARE, "fwblob", "raw firmware blob (FW_MAIN_A/B)")
|
NONE)
|
||||||
FILE_TYPE(RAW_KERNEL, "vmlinuz", "raw linux kernel")
|
FILE_TYPE(KEYBLOCK, "keyblock", "VbKeyBlock",
|
||||||
FILE_TYPE(CHROMIUMOS_DISK, "disk_img", "chromiumos disk image")
|
R_(ft_recognize_vblock1),
|
||||||
FILE_TYPE(PRIVKEY, "prikey", "VbPrivateKey (.vbprivk)")
|
NONE,
|
||||||
FILE_TYPE(VB2_PUBKEY, "pubkey21", "vb21 public key (.vbpubk2)")
|
NONE)
|
||||||
FILE_TYPE(VB2_PRIVKEY, "prikey21", "vb21 private key (.vbprik2)")
|
FILE_TYPE(FW_PREAMBLE, "fw_pre", "VbFirmwarePreamble (VBLOCK_A/B)",
|
||||||
FILE_TYPE(PEM, "pem", "RSA private key (.pem)")
|
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)
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ static inline uint32_t max(uint32_t a, uint32_t b)
|
|||||||
return a > b ? a : 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;
|
GoogleBinaryBlockHeader *gbb = (GoogleBinaryBlockHeader *)buf;
|
||||||
|
|
||||||
@@ -301,7 +301,7 @@ enum futil_file_err futil_unmap_file(int fd, int writeable,
|
|||||||
|
|
||||||
|
|
||||||
#define DISK_SECTOR_SIZE 512
|
#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;
|
GptHeader *h;
|
||||||
|
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ static int has_all_areas(uint8_t *buf, uint32_t len, FmapHeader *fmap,
|
|||||||
return 1;
|
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);
|
FmapHeader *fmap = fmap_find(buf, len);
|
||||||
if (fmap) {
|
if (fmap) {
|
||||||
|
|||||||
@@ -715,7 +715,7 @@ uint8_t *CreateKernelBlob(uint8_t *vmlinuz_buf, uint64_t vmlinuz_size,
|
|||||||
return g_kernel_blob_data;
|
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;
|
VbKeyBlockHeader *key_block = (VbKeyBlockHeader *)buf;
|
||||||
VbFirmwarePreambleHeader *fw_preamble;
|
VbFirmwarePreambleHeader *fw_preamble;
|
||||||
@@ -745,7 +745,7 @@ enum futil_file_type recognize_vblock1(uint8_t *buf, uint32_t len)
|
|||||||
return FILE_TYPE_UNKNOWN;
|
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;
|
VbPublicKey *pubkey = (VbPublicKey *)buf;
|
||||||
VbPrivateKey key;
|
VbPrivateKey key;
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
#include "futility.h"
|
#include "futility.h"
|
||||||
#include "traversal.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_public_key pubkey;
|
||||||
struct vb2_private_key *privkey = 0;
|
struct vb2_private_key *privkey = 0;
|
||||||
@@ -168,7 +168,7 @@ static RSA *rsa_from_buffer(uint8_t *buf, uint32_t len)
|
|||||||
return rsa_key;
|
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);
|
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);
|
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);
|
rsa_key = rsa_from_buffer(state->my_area->buf, state->my_area->len);
|
||||||
if (!rsa_key)
|
if (!rsa_key)
|
||||||
DIE;
|
DIE;
|
||||||
|
|||||||
Reference in New Issue
Block a user