From 3145aa5aaa3ef95442edad1ce6759802ef4a9c2a Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Fri, 16 Dec 2022 09:14:16 +0000 Subject: [PATCH 1/1] util/ifdtool: consolidated program exits Signed-off-by: Leah Rowe Change-Id: I9a58b697323b6077d32fc1cbcea99a671936256f --- util/ifdtool/ifdtool.c | 148 +++++++++++++++-------------------------- 1 file changed, 55 insertions(+), 93 deletions(-) diff --git a/util/ifdtool/ifdtool.c b/util/ifdtool/ifdtool.c index 619f65cb0d..4f2d188d8a 100644 --- a/util/ifdtool/ifdtool.c +++ b/util/ifdtool/ifdtool.c @@ -353,14 +353,12 @@ main(int argc, char *argv[]) break; case 'v': print_version(); - exit(EXIT_SUCCESS); - break; + goto ifdtool_exit; case 'h': case '?': default: print_usage(argv[0]); - exit(EXIT_SUCCESS); - break; + goto ifdtool_exit; } } @@ -463,7 +461,7 @@ main(int argc, char *argv[]) free(new_filename); free(image); - +ifdtool_exit: return 0; } @@ -694,7 +692,7 @@ ich_chipset ifd1_guess_chipset(char *image, int size) const fdbar_t *fdb = find_fd(image, size); if (!fdb) - exit(EXIT_FAILURE); + errm(NULL); iccriba = (fdb->flmap2 >> 16) & 0xff; msl = (fdb->flmap2 >> 8) & 0xff; @@ -947,10 +945,8 @@ dump_frba_layout(const frba_t *frba, const char *layout_fname) int layout_fd = open(layout_fname, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - if (layout_fd == -1) { - perror("Could not open file"); - exit(EXIT_FAILURE); - } + if (layout_fd == -1) + errm("Could not open file"); for (i = 0; i < max_regions; i++) { region_t region = get_region(frba, i); @@ -959,10 +955,8 @@ dump_frba_layout(const frba_t *frba, const char *layout_fname) continue; dump_region_layout(buf, bufsize, i, frba); - if (write(layout_fd, buf, strlen(buf)) < 0) { - perror("Could not write to file"); - exit(EXIT_FAILURE); - } + if (write(layout_fd, buf, strlen(buf)) < 0) + errm("Could not write to file"); } close(layout_fd); printf("Wrote layout to %s\n", layout_fname); @@ -1462,7 +1456,7 @@ cmd_dump_fd(char *image, int size) char *flumap = NULL; const fdbar_t *fdb = find_fd(image, size); if (!fdb) - exit(EXIT_FAILURE); + errm(NULL); printf("%s", is_platform_with_pch() ? "PCH" : "ICH"); printf(" Revision: %s\n", ich_chipset_names[chipset]); @@ -1529,7 +1523,7 @@ cmd_dump_layout(char *image, int size, const char *layout_fname) { const frba_t *frba = find_frba(image, size); if (!frba) - exit(EXIT_FAILURE); + errm(NULL); dump_frba_layout(frba, layout_fname); } @@ -1542,7 +1536,7 @@ cmd_extract_regions(char *image, int size) const frba_t *frba = find_frba(image, size); if (!frba) - exit(EXIT_FAILURE); + errm(NULL); for (i = 0; i < max_regions; i++) { region_t region = get_region(frba, i); @@ -1551,13 +1545,11 @@ cmd_extract_regions(char *image, int size) region_fd = open(region_filename(i), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - if (region_fd < 0) { - perror("Error while trying to open file"); - exit(EXIT_FAILURE); - } + if (region_fd < 0) + errm("Error while trying to open file"); if (write(region_fd, image + region.base, region.size) != region.size) - perror("Error while writing"); + errm("Error while writing"); close(region_fd); } @@ -1574,7 +1566,7 @@ cmd_validate_layout(char *image, int size) const struct fmap_area *area = NULL; if (fmap_loc < 0 || !frba) - exit(EXIT_FAILURE); + errm(NULL); fmap = (struct fmap *)(image + fmap_loc); @@ -1607,7 +1599,7 @@ cmd_validate_layout(char *image, int size) } if (errors > 0) - exit(EXIT_FAILURE); + errm(NULL); } void @@ -1620,12 +1612,10 @@ write_image(const char *filename, char *image, int size) new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - if (new_fd < 0) { - perror("Error while trying to open file"); - exit(EXIT_FAILURE); - } + if (new_fd < 0) + errm("Error while trying to open file"); if (write(new_fd, image, size) != size) - perror("Error while writing"); + errm("Error while writing"); close(new_fd); } @@ -1635,7 +1625,7 @@ cmd_set_spi_frequency(const char *filename, char *image, int size, { fcba_t *fcba = find_fcba(image, size); if (!fcba) - exit(EXIT_FAILURE); + errm(NULL); /* clear bits 21-29 */ fcba->flcomp &= ~0x3fe00000; @@ -1655,7 +1645,7 @@ cmd_set_em100_mode(const char *filename, char *image, int size) int freq; fcba_t *fcba = find_fcba(image, size); if (!fcba) - exit(EXIT_FAILURE); + errm(NULL); switch (ifd_version) { case IFD_VERSION_1: @@ -1680,7 +1670,7 @@ cmd_set_chipdensity(const char *filename, char *image, int size, fcba_t *fcba = find_fcba(image, size); uint8_t mask, chip2_offset; if (!fcba) - exit(EXIT_FAILURE); + errm(NULL); printf("Setting chip density to "); decode_component_density(density); @@ -1691,11 +1681,9 @@ cmd_set_chipdensity(const char *filename, char *image, int size, /* fail if selected density is not supported by this version */ if ( (density == COMPONENT_DENSITY_32MB) || (density == COMPONENT_DENSITY_64MB) || - (density == COMPONENT_DENSITY_UNUSED) ) { - printf("error: Selected density not" - " supported in IFD version 1.\n"); - exit(EXIT_FAILURE); - } + (density == COMPONENT_DENSITY_UNUSED) ) + errm("error: Selected density not" + " supported in IFD version 1."); mask = 0x7; chip2_offset = 3; break; @@ -1704,8 +1692,7 @@ cmd_set_chipdensity(const char *filename, char *image, int size, chip2_offset = 4; break; default: - printf("error: Unknown IFD version\n"); - exit(EXIT_FAILURE); + errm("error: Unknown IFD version"); break; } @@ -1750,7 +1737,7 @@ cmd_lock_descriptor(const char *filename, char *image, int size) fmba_t *fmba = find_fmba(image, size); const frba_t *frba = find_frba(image, size); if (!fmba) - exit(EXIT_FAILURE); + errm(NULL); if (ifd_version >= IFD_VERSION_2) { wr_shift = FLMSTR_WR_SHIFT_V2; @@ -1877,7 +1864,7 @@ cmd_enable_cpu_read_me(const char *filename, char *image, int size) fmba_t *fmba = find_fmba(image, size); if (!fmba) - exit(EXIT_FAILURE); + errm(NULL); if (ifd_version >= IFD_VERSION_2) rd_shift = FLMSTR_RD_SHIFT_V2; @@ -1895,7 +1882,7 @@ cmd_unlock_descriptor(const char *filename, char *image, int size) { fmba_t *fmba = find_fmba(image, size); if (!fmba) - exit(EXIT_FAILURE); + errm(NULL); if (ifd_version >= IFD_VERSION_2) { /* Access bits for each region are read: 19:8 write: 31:20 */ @@ -1918,18 +1905,13 @@ set_pchstrap(fpsba_t *fpsba, const fdbar_t *fdb, const int strap, const unsigned int value) { int SS; - if (!fpsba || !fdb) { - fprintf(stderr, "Internal error\n"); - exit(EXIT_FAILURE); - } + if (!fpsba || !fdb) + errm("Internal error"); /* SoC Strap, aka PSL, aka ISL */ SS = (fdb->flmap1 >> 24) & 0xff; - if (strap >= SS) { - fprintf(stderr, - "Strap index %d out of range (max: %d)\n", strap, SS); - exit(EXIT_FAILURE); - } + if (strap >= SS) + errm("Strap index %d out of range (max: %d)", strap, SS); fpsba->pchstrp[strap] = value; } @@ -1983,37 +1965,28 @@ cmd_inject_region(const char *filename, char *image, int size, int region_fd, region_size, offset; frba_t *frba = find_frba(image, size); if (!frba) - exit(EXIT_FAILURE); + errm(NULL); region_t region = get_region(frba, region_type); - if (region.size <= 0xfff) { - fprintf(stderr, - "Region %s is disabled in target. Not injecting.\n", - region_name(region_type)); - exit(EXIT_FAILURE); - } + if (region.size <= 0xfff) + errm("Region %s is disabled in target. Not injecting.", + region_name(region_type)); region_fd = open(region_fname, O_RDONLY | O_BINARY); - if (region_fd == -1) { - perror("Could not open file"); - exit(EXIT_FAILURE); - } - if (fstat(region_fd, &buf) == -1) { - perror("Could not stat file"); - exit(EXIT_FAILURE); - } + if (region_fd == -1) + errm("Could not open file"); + if (fstat(region_fd, &buf) == -1) + errm("Could not stat file"); region_size = buf.st_size; printf("File %s is %d bytes\n", region_fname, region_size); if ( (region_size > region.size) || ((region_type != 1) && - (region_size > region.size))) { - fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x)" - " bytes. Not injecting.\n", + (region_size > region.size))) + errm("Region %s is %d(0x%x) bytes. File is %d(0x%x)" + " bytes. Not injecting.", region_name(region_type), region.size, region.size, region_size, region_size); - exit(EXIT_FAILURE); - } offset = 0; if ((region_type == 1) && (region_size < region.size)) { @@ -2025,17 +1998,13 @@ cmd_inject_region(const char *filename, char *image, int size, memset(image + region.base, 0xff, offset); } - if (size < region.base + offset + region_size) { - fprintf(stderr, "Output file is too small. (%d < %d)\n", + if (size < region.base + offset + region_size) + errm("Output file is too small. (%d < %d)\n", size, region.base + offset + region_size); - exit(EXIT_FAILURE); - } if (read(region_fd, image + region.base + offset, region_size) - != region_size) { - perror("Could not read file"); - exit(EXIT_FAILURE); - } + != region_size) + errm("Could not read file"); close(region_fd); @@ -2092,7 +2061,7 @@ cmd_new_layout(const char *filename, char *image, int size, /* load current descriptor map and regions */ frba_t *frba = find_frba(image, size); if (!frba) - exit(EXIT_FAILURE); + errm(NULL); for (i = 0; i < max_regions; i++) { current_regions[i] = get_region(frba, i); @@ -2102,10 +2071,8 @@ cmd_new_layout(const char *filename, char *image, int size, /* read new layout */ romlayout = fopen(layout_fname, "r"); - if (!romlayout) { - perror("Could not read layout file.\n"); - exit(EXIT_FAILURE); - } + if (!romlayout) + errm("Could not read layout file."); while (!feof(romlayout)) { if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, @@ -2118,10 +2085,8 @@ cmd_new_layout(const char *filename, char *image, int size, tstr1 = strtok(tempstr, ":"); tstr2 = strtok(NULL, ":"); - if (!tstr1 || !tstr2) { - fprintf(stderr, "Could not parse layout file.\n"); - exit(EXIT_FAILURE); - } + if (!tstr1 || !tstr2) + errm("Could not parse layout file."); new_regions[region_number].base = strtol(tstr1, (char **)NULL, 16); new_regions[region_number].limit = strtol(tstr2, @@ -2149,10 +2114,7 @@ cmd_new_layout(const char *filename, char *image, int size, for (j = i + 1; j < max_regions; j++) { if (regions_collide(&new_regions[i], &new_regions[j])) - { - fprintf(stderr, "Regions would overlap.\n"); - exit(EXIT_FAILURE); - } + errm("Regions would overlap."); } /* detect if the image size should grow */ @@ -2220,7 +2182,7 @@ cmd_new_layout(const char *filename, char *image, int size, /* update new descriptor regions */ if (!(frba = find_frba(new_image, new_extent))) - exit(EXIT_FAILURE); + errm(NULL); printf("Modify Flash Descriptor regions\n"); for (i = 1; i < max_regions; i++) -- 2.25.1