vboot/futility: update fatal errors to use FATAL

Previously, a mix of:
 - DIE
 - Fatal (customly defined in cmd_vbutil_kernel.c)
 - VbExError
... were all used to print an error message and exit.

In the case of futility, standardize on using the FATAL macro
defined in futility.h.

BUG=b:124141368
TEST=Check that FATAL works correctly:
     $ build/futility/futility vbutil_key --in a --out a --algorithm 18
     FATAL: do_vbutil_key: Unknown option
TEST=make clean && make runtests
BRANCH=none

Change-Id: I97ca1153dc36e7208c69185883518c52d5d75293
Signed-off-by: Joel Kitching <kitching@google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/1679799
Commit-Queue: Joel Kitching <kitching@chromium.org>
Tested-by: Joel Kitching <kitching@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
Joel Kitching 2019-06-28 14:53:57 +08:00 committed by Commit Bot
parent a529598bd4
commit 772908aef9
13 changed files with 79 additions and 94 deletions

View File

@ -355,7 +355,7 @@ static int do_create(int argc, char *argv[])
case 0: /* handled option */
break;
default:
DIE;
FATAL("Unrecognized getopt output: %d\n", i);
}
}

View File

@ -125,7 +125,7 @@ static int do_load_fmap(int argc, char *argv[])
errorcnt++;
break;
default:
DIE;
FATAL("Unrecognized getopt output: %d\n", i);
}
}

View File

@ -141,7 +141,7 @@ static int do_pcr(int argc, char *argv[])
errorcnt++;
break;
default:
DIE;
FATAL("Unrecognized getopt output: %d\n", i);
}
}

View File

@ -559,7 +559,7 @@ static int do_show(int argc, char *argv[])
case 0: /* handled option */
break;
default:
DIE;
FATAL("Unrecognized getopt output: %d\n", i);
}
}

View File

@ -143,7 +143,7 @@ int ft_sign_raw_kernel(const char *name, uint8_t *buf, uint32_t len,
/* We should be creating a completely new output file.
* If not, something's wrong. */
if (!sign_option.create_new_outfile)
DIE;
FATAL("create_new_outfile should be selected\n");
if (sign_option.vblockonly)
rv = WriteSomeParts(sign_option.outfile,
@ -862,8 +862,7 @@ static int do_sign(int argc, char *argv[])
case 0: /* handled option */
break;
default:
VB2_DEBUG("i=%d\n", i);
DIE;
FATAL("Unrecognized getopt output: %d\n", i);
}
}

View File

@ -97,34 +97,34 @@ static int do_vblock(const char *outfile, const char *keyblock_file,
int retval = 1;
if (!outfile) {
VbExError("Must specify output filename\n");
FATAL("Must specify output filename\n");
goto vblock_cleanup;
}
if (!keyblock_file || !signprivate || !kernelkey_file) {
VbExError("Must specify all keys\n");
FATAL("Must specify all keys\n");
goto vblock_cleanup;
}
if (!fv_file) {
VbExError("Must specify firmware volume\n");
FATAL("Must specify firmware volume\n");
goto vblock_cleanup;
}
/* Read the key block and keys */
keyblock = vb2_read_keyblock(keyblock_file);
if (!keyblock) {
VbExError("Error reading key block.\n");
FATAL("Error reading key block.\n");
goto vblock_cleanup;
}
signing_key = vb2_read_private_key(signprivate);
if (!signing_key) {
VbExError("Error reading signing key.\n");
FATAL("Error reading signing key.\n");
goto vblock_cleanup;
}
kernel_subkey = vb2_read_packed_key(kernelkey_file);
if (!kernel_subkey) {
VbExError("Error reading kernel subkey.\n");
FATAL("Error reading kernel subkey.\n");
goto vblock_cleanup;
}
@ -133,12 +133,12 @@ static int do_vblock(const char *outfile, const char *keyblock_file,
if (VB2_SUCCESS != vb2_read_file(fv_file, &fv_data, &fv_size))
goto vblock_cleanup;
if (!fv_size) {
VbExError("Empty firmware volume file\n");
FATAL("Empty firmware volume file\n");
goto vblock_cleanup;
}
body_sig = vb2_calculate_signature(fv_data, fv_size, signing_key);
if (!body_sig) {
VbExError("Error calculating body signature\n");
FATAL("Error calculating body signature\n");
goto vblock_cleanup;
}
@ -146,21 +146,21 @@ static int do_vblock(const char *outfile, const char *keyblock_file,
preamble = vb2_create_fw_preamble(version, kernel_subkey, body_sig,
signing_key, preamble_flags);
if (!preamble) {
VbExError("Error creating preamble.\n");
FATAL("Error creating preamble.\n");
goto vblock_cleanup;
}
/* Write the output file */
FILE *f = fopen(outfile, "wb");
if (!f) {
VbExError("Can't open output file %s\n", outfile);
FATAL("Can't open output file %s\n", outfile);
goto vblock_cleanup;
}
int i = ((1 != fwrite(keyblock, keyblock->keyblock_size, 1, f)) ||
(1 != fwrite(preamble, preamble->preamble_size, 1, f)));
fclose(f);
if (i) {
VbExError("Can't write output file %s\n", outfile);
FATAL("Can't write output file %s\n", outfile);
unlink(outfile);
goto vblock_cleanup;
}
@ -200,7 +200,7 @@ static int do_verify(const char *infile, const char *signpubkey,
int retval = 1;
if (!infile || !signpubkey || !fv_file) {
VbExError("Must specify filename, signpubkey, and fv\n");
FATAL("Must specify filename, signpubkey, and fv\n");
goto verify_cleanup;
}
@ -219,14 +219,14 @@ static int do_verify(const char *infile, const char *signpubkey,
/* Read blob */
uint32_t blob_size;
if (VB2_SUCCESS != vb2_read_file(infile, &blob, &blob_size)) {
VbExError("Error reading input file\n");
FATAL("Error reading input file\n");
goto verify_cleanup;
}
/* Read firmware volume */
uint32_t fv_size;
if (VB2_SUCCESS != vb2_read_file(fv_file, &fv_data, &fv_size)) {
VbExError("Error reading firmware volume\n");
FATAL("Error reading firmware volume\n");
goto verify_cleanup;
}
@ -234,7 +234,7 @@ static int do_verify(const char *infile, const char *signpubkey,
struct vb2_keyblock *keyblock = (struct vb2_keyblock *)blob;
if (VB2_SUCCESS !=
vb2_verify_keyblock(keyblock, blob_size, &sign_key, &wb)) {
VbExError("Error verifying key block.\n");
FATAL("Error verifying key block.\n");
goto verify_cleanup;
}
@ -263,7 +263,7 @@ static int do_verify(const char *infile, const char *signpubkey,
struct vb2_fw_preamble *pre2 = (struct vb2_fw_preamble *)(blob + now);
if (VB2_SUCCESS !=
vb2_verify_fw_preamble(pre2, blob_size - now, &data_key, &wb)) {
VbExError("Error2 verifying preamble.\n");
FATAL("Error2 verifying preamble.\n");
goto verify_cleanup;
}
now += pre2->preamble_size;
@ -298,14 +298,14 @@ static int do_verify(const char *infile, const char *signpubkey,
&data_key, &wb)) {
printf("Body verification succeeded.\n");
} else {
VbExError("Error verifying firmware body.\n");
FATAL("Error verifying firmware body.\n");
goto verify_cleanup;
}
if (kernelkey_file &&
VB2_SUCCESS != vb2_write_packed_key(kernelkey_file,
kernel_subkey)) {
VbExError("Unable to write kernel subkey\n");
FATAL("Unable to write kernel subkey\n");
goto verify_cleanup;
}

View File

@ -30,16 +30,6 @@
#include "vb2_common.h"
#include "vb2_struct.h"
static void Fatal(const char *format, ...)
{
va_list ap;
va_start(ap, format);
fprintf(stderr, "ERROR: ");
vfprintf(stderr, format, ap);
va_end(ap);
exit(1);
}
/* Global opts */
static int opt_verbose;
static int opt_vblockonly;
@ -182,7 +172,7 @@ static uint8_t *ReadOldKPartFromFileOrDie(const char *filename,
uint32_t file_size = 0;
if (0 != stat(filename, &statbuf))
Fatal("Unable to stat %s: %s\n", filename, strerror(errno));
FATAL("Unable to stat %s: %s\n", filename, strerror(errno));
if (S_ISBLK(statbuf.st_mode)) {
#ifndef HAVE_MACOS
@ -197,17 +187,17 @@ static uint8_t *ReadOldKPartFromFileOrDie(const char *filename,
}
VB2_DEBUG("%s size is 0x%x\n", filename, file_size);
if (file_size < opt_pad)
Fatal("%s is too small to be a valid kernel blob\n", filename);
FATAL("%s is too small to be a valid kernel blob\n", filename);
VB2_DEBUG("Reading %s\n", filename);
fp = fopen(filename, "rb");
if (!fp)
Fatal("Unable to open file %s: %s\n", filename,
FATAL("Unable to open file %s: %s\n", filename,
strerror(errno));
buf = malloc(file_size);
if (1 != fread(buf, file_size, 1, fp))
Fatal("Unable to read entirety of %s: %s\n", filename,
FATAL("Unable to read entirety of %s: %s\n", filename,
error_fread(fp));
if (size_ptr)
@ -399,50 +389,50 @@ static int do_vbutil_kernel(int argc, char *argv[])
case OPT_MODE_PACK:
if (!keyblock_file)
Fatal("Missing required keyblock file.\n");
FATAL("Missing required keyblock file.\n");
t_keyblock = (struct vb2_keyblock *)ReadFile(keyblock_file, 0);
if (!t_keyblock)
Fatal("Error reading key block.\n");
FATAL("Error reading key block.\n");
if (!signprivkey_file)
Fatal("Missing required signprivate file.\n");
FATAL("Missing required signprivate file.\n");
signpriv_key = vb2_read_private_key(signprivkey_file);
if (!signpriv_key)
Fatal("Error reading signing key.\n");
FATAL("Error reading signing key.\n");
if (!config_file)
Fatal("Missing required config file.\n");
FATAL("Missing required config file.\n");
VB2_DEBUG("Reading %s\n", config_file);
t_config_data =
ReadConfigFile(config_file, &t_config_size);
if (!t_config_data)
Fatal("Error reading config file.\n");
FATAL("Error reading config file.\n");
if (!bootloader_file)
Fatal("Missing required bootloader file.\n");
FATAL("Missing required bootloader file.\n");
VB2_DEBUG("Reading %s\n", bootloader_file);
if (VB2_SUCCESS != vb2_read_file(bootloader_file,
&t_bootloader_data,
&t_bootloader_size))
Fatal("Error reading bootloader file.\n");
FATAL("Error reading bootloader file.\n");
VB2_DEBUG(" bootloader file size=0x%x\n", t_bootloader_size);
if (!vmlinuz_file)
Fatal("Missing required vmlinuz file.\n");
FATAL("Missing required vmlinuz file.\n");
VB2_DEBUG("Reading %s\n", vmlinuz_file);
if (VB2_SUCCESS !=
vb2_read_file(vmlinuz_file, &vmlinuz_buf, &vmlinuz_size))
Fatal("Error reading vmlinuz file.\n");
FATAL("Error reading vmlinuz file.\n");
VB2_DEBUG(" vmlinuz file size=0x%x\n", vmlinuz_size);
if (!vmlinuz_size)
Fatal("Empty vmlinuz file\n");
FATAL("Empty vmlinuz file\n");
kblob_data = CreateKernelBlob(
vmlinuz_buf, vmlinuz_size,
@ -451,7 +441,7 @@ static int do_vbutil_kernel(int argc, char *argv[])
t_bootloader_data, t_bootloader_size,
&kblob_size);
if (!kblob_data)
Fatal("Unable to create kernel blob\n");
FATAL("Unable to create kernel blob\n");
VB2_DEBUG("kblob_size = 0x%x\n", kblob_size);
@ -460,7 +450,7 @@ static int do_vbutil_kernel(int argc, char *argv[])
t_keyblock, signpriv_key, flags,
&vblock_size);
if (!vblock_data)
Fatal("Unable to sign kernel blob\n");
FATAL("Unable to sign kernel blob\n");
VB2_DEBUG("vblock_size = 0x%x\n", vblock_size);
@ -485,14 +475,14 @@ static int do_vbutil_kernel(int argc, char *argv[])
/* Required */
if (!signprivkey_file)
Fatal("Missing required signprivate file.\n");
FATAL("Missing required signprivate file.\n");
signpriv_key = vb2_read_private_key(signprivkey_file);
if (!signpriv_key)
Fatal("Error reading signing key.\n");
FATAL("Error reading signing key.\n");
if (!oldfile)
Fatal("Missing previously packed blob.\n");
FATAL("Missing previously packed blob.\n");
/* Load the kernel partition */
kpart_data = ReadOldKPartFromFileOrDie(oldfile, &kpart_size);
@ -500,14 +490,14 @@ static int do_vbutil_kernel(int argc, char *argv[])
/* Make sure we have a kernel partition */
if (FILE_TYPE_KERN_PREAMBLE !=
futil_file_type_buf(kpart_data, kpart_size))
Fatal("%s is not a kernel blob\n", oldfile);
FATAL("%s is not a kernel blob\n", oldfile);
kblob_data = unpack_kernel_partition(kpart_data, kpart_size,
opt_pad, &keyblock,
&preamble, &kblob_size);
if (!kblob_data)
Fatal("Unable to unpack kernel partition\n");
FATAL("Unable to unpack kernel partition\n");
kernel_body_load_address = preamble->body_load_address;
@ -517,11 +507,11 @@ static int do_vbutil_kernel(int argc, char *argv[])
t_config_data =
ReadConfigFile(config_file, &t_config_size);
if (!t_config_data)
Fatal("Error reading config file.\n");
FATAL("Error reading config file.\n");
if (0 != UpdateKernelBlobConfig(
kblob_data, kblob_size,
t_config_data, t_config_size))
Fatal("Unable to update config\n");
FATAL("Unable to update config\n");
}
if (!version_str)
@ -534,7 +524,7 @@ static int do_vbutil_kernel(int argc, char *argv[])
t_keyblock = (struct vb2_keyblock *)
ReadFile(keyblock_file, 0);
if (!t_keyblock)
Fatal("Error reading key block.\n");
FATAL("Error reading key block.\n");
}
/* Reuse previous body size */
@ -543,7 +533,7 @@ static int do_vbutil_kernel(int argc, char *argv[])
t_keyblock ? t_keyblock : keyblock,
signpriv_key, flags, &vblock_size);
if (!vblock_data)
Fatal("Unable to sign kernel blob\n");
FATAL("Unable to sign kernel blob\n");
if (opt_vblockonly)
rv = WriteSomeParts(filename,
@ -562,7 +552,7 @@ static int do_vbutil_kernel(int argc, char *argv[])
if (signpubkey_file) {
signpub_key = vb2_read_packed_key(signpubkey_file);
if (!signpub_key)
Fatal("Error reading public key.\n");
FATAL("Error reading public key.\n");
}
/* Do it */
@ -574,7 +564,7 @@ static int do_vbutil_kernel(int argc, char *argv[])
opt_pad, 0, 0,
&kblob_size);
if (!kblob_data)
Fatal("Unable to unpack kernel partition\n");
FATAL("Unable to unpack kernel partition\n");
rv = VerifyKernelBlob(kblob_data, kblob_size,
signpub_key, keyblock_file, min_version);
@ -598,11 +588,11 @@ static int do_vbutil_kernel(int argc, char *argv[])
&preamble, &kblob_size);
if (!kblob_data)
Fatal("Unable to unpack kernel partition\n");
FATAL("Unable to unpack kernel partition\n");
f = fopen(vmlinuz_out_file, "wb");
if (!f) {
Fatal("Can't open output file %s\n", vmlinuz_out_file);
FATAL("Can't open output file %s\n", vmlinuz_out_file);
return 1;
}
@ -621,7 +611,7 @@ static int do_vbutil_kernel(int argc, char *argv[])
vmlinuz_header_size)) {
fclose(f);
unlink(vmlinuz_out_file);
Fatal("Vmlinuz header not signed!\n");
FATAL("Vmlinuz header not signed!\n");
return 1;
}
// calculate the vmlinuz_header offset from
@ -645,7 +635,7 @@ static int do_vbutil_kernel(int argc, char *argv[])
if (errcount) {
fclose(f);
unlink(vmlinuz_out_file);
Fatal("Can't write output file %s\n", vmlinuz_out_file);
FATAL("Can't write output file %s\n", vmlinuz_out_file);
return 1;
}

View File

@ -103,7 +103,7 @@ static int do_pack(const char *infile, const char *outfile, uint32_t algorithm,
return 0;
}
VbExError("Unable to parse either .keyb or .pem from %s\n", infile);
FATAL("Unable to parse either .keyb or .pem from %s\n", infile);
return 1;
}
@ -154,8 +154,7 @@ static int do_unpack(const char *infile, const char *outfile)
return 0;
}
VbExError("Unable to parse either .vbpubk or vbprivk from %s\n",
infile);
FATAL("Unable to parse either .vbpubk or vbprivk from %s\n", infile);
return 1;
}
@ -175,7 +174,7 @@ static int do_vbutil_key(int argc, char *argv[])
switch (i) {
case '?':
/* Unhandled option */
VbExError("Unknown option\n");
FATAL("Unknown option\n");
parse_error = 1;
break;
case OPT_HELP:
@ -189,7 +188,7 @@ static int do_vbutil_key(int argc, char *argv[])
case OPT_KEY_VERSION:
version = strtoul(optarg, &e, 0);
if (!*optarg || (e && *e)) {
VbExError("Invalid --version\n");
FATAL("Invalid --version\n");
parse_error = 1;
}
break;
@ -197,7 +196,7 @@ static int do_vbutil_key(int argc, char *argv[])
case OPT_ALGORITHM:
algorithm = strtoul(optarg, &e, 0);
if (!*optarg || (e && *e)) {
VbExError("Invalid --algorithm\n");
FATAL("Invalid --algorithm\n");
parse_error = 1;
}
break;

View File

@ -14,6 +14,7 @@
#include <sys/types.h>
#include <unistd.h>
#include "futility.h"
#include "host_common.h"
#include "kernel_blob.h"
#include "vb2_struct.h"
@ -67,24 +68,24 @@ static char *FindKernelConfigFromStream(void *ctx, ReadFullyFn read_fn,
/* Skip the key block */
if (read_fn(ctx, &keyblock, sizeof(keyblock)) != sizeof(keyblock)) {
VbExError("not enough data to fill keyblock header\n");
FATAL("not enough data to fill keyblock header\n");
return NULL;
}
ssize_t to_skip = keyblock.keyblock_size - sizeof(keyblock);
if (to_skip < 0 || SkipWithRead(ctx, read_fn, to_skip)) {
VbExError("keyblock_size advances past the end of the blob\n");
FATAL("keyblock_size advances past the end of the blob\n");
return NULL;
}
now += keyblock.keyblock_size;
/* Open up the preamble */
if (read_fn(ctx, &preamble, sizeof(preamble)) != sizeof(preamble)) {
VbExError("not enough data to fill preamble\n");
FATAL("not enough data to fill preamble\n");
return NULL;
}
to_skip = preamble.preamble_size - sizeof(preamble);
if (to_skip < 0 || SkipWithRead(ctx, read_fn, to_skip)) {
VbExError("preamble_size advances past the end of the blob\n");
FATAL("preamble_size advances past the end of the blob\n");
return NULL;
}
now += preamble.preamble_size;
@ -102,17 +103,16 @@ static char *FindKernelConfigFromStream(void *ctx, ReadFullyFn read_fn,
CROS_CONFIG_SIZE) + now;
to_skip = offset - now;
if (to_skip < 0 || SkipWithRead(ctx, read_fn, to_skip)) {
VbExError("params are outside of the memory blob: %x\n",
offset);
FATAL("params are outside of the memory blob: %x\n", offset);
return NULL;
}
char *ret = malloc(CROS_CONFIG_SIZE);
if (!ret) {
VbExError("No memory\n");
FATAL("No memory\n");
return NULL;
}
if (read_fn(ctx, ret, CROS_CONFIG_SIZE) != CROS_CONFIG_SIZE) {
VbExError("Cannot read kernel config\n");
FATAL("Cannot read kernel config\n");
free(ret);
ret = NULL;
}
@ -125,7 +125,7 @@ char *FindKernelConfig(const char *infile, uint64_t kernel_body_load_address)
int fd = open(infile, O_RDONLY | O_CLOEXEC | O_LARGEFILE);
if (fd < 0) {
VbExError("Cannot open %s\n", infile);
FATAL("Cannot open %s\n", infile);
return NULL;
}

View File

@ -284,7 +284,7 @@ static int fmap_sign_fw_preamble(const char *name, uint8_t *buf, uint32_t len,
fw_body_area = &state->area[BIOS_FMAP_FW_MAIN_B];
break;
default:
DIE;
FATAL("Can only handle VBLOCK_A or VBLOCK_B\n");
}
if (fw_size > fw_body_area->len) {

View File

@ -347,8 +347,7 @@ int main(int argc, char *argv[], char *envp[])
case 0: /* handled option */
break;
default:
VB2_DEBUG("i=%d\n", i);
DIE;
FATAL("Unrecognized getopt output: %d\n", i);
}
}
vboot_version = vb_ver;

View File

@ -82,16 +82,14 @@ extern const struct futil_cmd_t *const futil_cmds[];
#define BUILD_ASSERT(cond) _BA0_(cond, __LINE__)
#endif
/* Fatal internal stupidness */
#ifndef DIE
#define DIE do { \
fprintf(stderr, MYNAME ": internal error at %s:%d\n", \
__FILE__, __LINE__); \
exit(1); \
/* Fatal error (print error message and exit). */
#define FATAL(format, ...) do { \
fprintf(stderr, "FATAL: %s: " format, __func__, \
##__VA_ARGS__ ); \
exit(1); \
} while (0)
#endif
/* Print error messages (similar to VbExError but won't exit). */
/* Print error messages (won't exit). */
#define ERROR(format, ...) fprintf(stderr, "ERROR: %s: " format, __func__, \
##__VA_ARGS__ )
#define WARN(format, ...) fprintf(stderr, "WARNING: %s: " format, __func__, \

View File

@ -214,7 +214,7 @@ int ft_show_pem(const char *name, uint8_t *buf, uint32_t len, void *data)
/* We're called only after ft_recognize_pem, so this should work. */
rsa_key = rsa_from_buffer(buf, len);
if (!rsa_key)
DIE;
FATAL("No RSA key found in buffer\n");
/* Use to presence of the private exponent to decide if it's public */
RSA_get0_key(rsa_key, &rsa_key_n, NULL, &rsa_key_d);