From c21dbdc19d18f53de68c55d7a68f4b9deb61cab6 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Fri, 16 Dec 2022 08:42:36 +0000 Subject: [PATCH 1/1] util/ifdtool: simplified error handling in main() Signed-off-by: Leah Rowe Change-Id: I1948b4e10cf812827b1ef5a775d817205bfcb11e --- util/ifdtool/ifdtool.c | 154 +++++++++++++++++------------------------ 1 file changed, 64 insertions(+), 90 deletions(-) diff --git a/util/ifdtool/ifdtool.c b/util/ifdtool/ifdtool.c index e159bef584..60a502a219 100644 --- a/util/ifdtool/ifdtool.c +++ b/util/ifdtool/ifdtool.c @@ -4,12 +4,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include "ifdtool.h" @@ -46,6 +48,7 @@ #define PLATFORM_HAS_10GBE_0_REGION (platform == PLATFORM_DNV) #define PLATFORM_HAS_10GBE_1_REGION (platform == PLATFORM_DNV) +char *prog; int ifd_version; int chipset; unsigned int max_regions = 0; @@ -111,6 +114,7 @@ const char *const ich_chipset_names[] = { void print_usage(const char *name); void print_version(void); +void errm(const char *restrict msg, ...); int get_platform(const char *strPlatform); int argv_component_density(int new_density); int get_region_type(const char *strRegionType); @@ -225,6 +229,8 @@ main(int argc, char *argv[]) {0, 0, 0, 0} }; + prog = argv[0]; + while ((opt = getopt_long(argc, argv, "S:V:df:D:C:M:xi:n:O:s:p:elruvth?", long_options, &option_index)) != EOF) { @@ -242,11 +248,8 @@ main(int argc, char *argv[]) case 'f': mode_layout = 1; layout_fname = strdup(optarg); - if (!layout_fname) { - fprintf(stderr, "No layout file specified\n"); - print_usage(argv[0]); - exit(EXIT_FAILURE); - } + if (!layout_fname) + errm("No layout file specified"); break; case 'x': mode_extract = 1; @@ -255,69 +258,44 @@ main(int argc, char *argv[]) // separate type and file name strRegionType = strdup(optarg); region_fname = strchr(strRegionType, ':'); - if (!region_fname) { - print_usage(argv[0]); - exit(EXIT_FAILURE); - } + if (!region_fname) + errm(NULL); region_fname[0] = '\0'; region_fname++; // Descriptor, BIOS, ME, GbE, Platform // valid type? - if ((region_type = get_region_type(strRegionType)) - == -1) { - fprintf(stderr, - "No such region type: '%s'\n\n", - strRegionType); - print_usage(argv[0]); - exit(EXIT_FAILURE); - } + if ((region_type=get_region_type(strRegionType) == -1)) + errm("No such region: '%s'", strRegionType); mode_inject = 1; break; case 'n': mode_newlayout = 1; layout_fname = strdup(optarg); - if (!layout_fname) { - fprintf(stderr, "No layout file specified\n"); - print_usage(argv[0]); - exit(EXIT_FAILURE); - } + if (!layout_fname) + errm("No layout file specified"); break; case 'O': new_filename = strdup(optarg); - if (!new_filename) { - fprintf(stderr, - "No output filename specified\n"); - print_usage(argv[0]); - exit(EXIT_FAILURE); - } + if (!new_filename) + errm("No output filename specified"); break; case 'D': if ((new_density = argv_component_density(strtoul(optarg, NULL, 0))) - == -1) { - fprintf(stderr, "error: Unknown density\n"); - print_usage(argv[0]); - exit(EXIT_FAILURE); - } + == -1) + errm("Unknown density"); mode_density = 1; break; case 'C': selected_chip = strtol(optarg, NULL, 0); - if (selected_chip > 2) { - fprintf(stderr, - "error: Invalid chip selection\n"); - print_usage(argv[0]); - exit(EXIT_FAILURE); - } + if (selected_chip > 2) + errm("Invalid chip selection"); break; case 'M': mode_altmedisable = 1; altmedisable = strtol(optarg, NULL, 0); - if (altmedisable > 1) { - fprintf(stderr, "error: Illegal value\n"); - print_usage(argv[0]); - exit(EXIT_FAILURE); - } + if (altmedisable > 1) + errm("Illegal value"); break; case 's': // Parse the requested SPI frequency @@ -342,10 +320,8 @@ main(int argc, char *argv[]) spifreq = SPI_FREQUENCY_50MHZ_30MHZ; break; default: - fprintf(stderr, "Invalid SPI Frequency: %d\n", - inputfreq); - print_usage(argv[0]); - exit(EXIT_FAILURE); + errm("Invalid SPI Frequency: %d", inputfreq); + break; } mode_spifreq = 1; break; @@ -354,29 +330,22 @@ main(int argc, char *argv[]) break; case 'l': mode_locked = 1; - if (mode_unlocked == 1) { - fprintf(stderr, "Locking/Unlocking FD and ME" - " are mutually exclusive\n"); - exit(EXIT_FAILURE); - } + if (mode_unlocked == 1) + errm("Locking/Unlocking FD and ME" + " are mutually exclusive"); break; case 'r': mode_read = 1; break; case 'u': mode_unlocked = 1; - if (mode_locked == 1) { - fprintf(stderr, "Locking/Unlocking FD and ME" - " are mutually exclusive\n"); - exit(EXIT_FAILURE); - } + if (mode_locked == 1) + errm("Locking/Unlocking FD and ME" + " are mutually exclusive"); break; case 'p': - if ((platform = get_platform(optarg)) == -1) { - fprintf(stderr, - "Unknown platform: %s\n", optarg); - exit(EXIT_FAILURE); - } + if ((platform = get_platform(optarg)) == -1) + errm("Unknown platform: %s", optarg); fprintf(stderr, "Platform is: %s\n", optarg); break; case 't': @@ -398,37 +367,24 @@ main(int argc, char *argv[]) if ((mode_dump + mode_layout + mode_extract + mode_inject + mode_setstrap + mode_newlayout + (mode_spifreq | mode_em100 | mode_unlocked | mode_locked) + - mode_altmedisable + mode_validate) > 1) { - fprintf(stderr, "You may not specify more than one mode.\n\n"); - print_usage(argv[0]); - exit(EXIT_FAILURE); - } + mode_altmedisable + mode_validate) > 1) + errm("You may not specify more than one mode."); if ((mode_dump + mode_layout + mode_extract + mode_inject + mode_setstrap + mode_newlayout + mode_spifreq + mode_em100 + mode_locked + mode_unlocked + mode_density + mode_altmedisable + - mode_validate) == 0) { - fprintf(stderr, "You need to specify a mode.\n\n"); - print_usage(argv[0]); - exit(EXIT_FAILURE); - } + mode_validate) == 0) + errm("You need to specify a mode."); - if (optind + 1 != argc) { - fprintf(stderr, "You need to specify a file.\n\n"); - print_usage(argv[0]); - exit(EXIT_FAILURE); - } + if (optind + 1 != argc) + errm("You need to specify a file."); filename = argv[optind]; bios_fd = open(filename, O_RDONLY | O_BINARY); - if (bios_fd == -1) { - perror("Could not open file"); - exit(EXIT_FAILURE); - } - if (fstat(bios_fd, &buf) == -1) { - perror("Could not stat file"); - exit(EXIT_FAILURE); - } + if (bios_fd == -1) + errm("Could not open file"); + if (fstat(bios_fd, &buf) == -1) + errm("Could not stat file"); size = buf.st_size; printf("File %s is %d bytes\n", filename, size); @@ -437,10 +393,8 @@ main(int argc, char *argv[]) if (image == NULL) err(1, NULL); - if (read(bios_fd, image, size) != size) { - perror("Could not read file"); - exit(EXIT_FAILURE); - } + if (read(bios_fd, image, size) != size) + errm("Could not read file"); close(bios_fd); @@ -514,6 +468,26 @@ main(int argc, char *argv[]) return 0; } +void +errm(const char *restrict msg, ...) +{ + va_list vars; + if (msg != NULL) { + fprintf(stderr, "%s: ", prog); + va_start(vars, msg); + vfprintf(stderr, msg, vars); + va_end(vars); + fprintf (stderr, "\n"); + } else { + print_usage(prog); + } + if (errno == 0) { + errno = ECANCELED; + } + fprintf(stderr, "errno: %d: %s\n", errno, strerror(errno)); + exit(errno); +} + int get_platform(const char *strPlatform) { -- 2.25.1