[PATCH 00/21] mkimage: Start to tidy up mkimage and friends

The current mkimage code is a bit messy: - the main() function is very long - two similarly named structs are used throughout: the first (struct image_tool_params) is not actually just parameters, the second (struct image_type_params) is confusingly similar - quite a bit of FIT processing happens right at the start of main(), which can be hard to follow - the program calls exit() from many different places This series renames the main structures, avoiding using the common 'params' word. It breaks up part of main() into separate functions and starts the process of exiting in one place. It also reverts a patch which causes an invalid 'loadables' property to be added with '-f auto'. More remains to be done, but this is a start. Simon Glass (21): mkimage: Move copy_file() higher in the file mkimage: Move code from main() to a new function mkimage: Return the exist code from run_mkimage() mkimage: Update usage() to return a value mkimage: Rename struct image_type_params to imgtool_funcs mkimage: Return struct image_tool_params to struct imgtool mkimage: Use a consistent parameter for struct imgtool * mkimage: Pass struct imgtool out from main() mkimage: Update add_content() to take an imgtool parameter mkimage: Update process_args() to take an imgtool parameter mkimage: Update verify_image() to take an imgtool parameter mkimage: Update copy_file() etc. to take an imgtool parameter mkimage: Update run_mkimage() to take an imgtool parameter mkimage: Drop the global for struct imgtool mkimage: Update process_args() to return a value always mkimage: Update verify_image() to return a value always mkimage: Update copy_file() et al to return a value always mkimage: Split out initial checking and processing mkimage: Split out file-opening into its own function mkimage: Move the list-or-process code into a function Revert "tools: fit_image: Add the loadable property to configs" tools/aisimage.c | 57 ++- tools/atmelimage.c | 27 +- tools/default_image.c | 54 +-- tools/dumpimage.c | 8 +- tools/fit_common.c | 5 +- tools/fit_common.h | 7 +- tools/fit_image.c | 239 ++++++----- tools/gpheader.h | 2 +- tools/gpimage-common.c | 8 +- tools/gpimage.c | 8 +- tools/imagetool.c | 56 +-- tools/imagetool.h | 67 ++- tools/imx8image.c | 12 +- tools/imx8mimage.c | 8 +- tools/imximage.c | 41 +- tools/kwbimage.c | 164 +++---- tools/lpc32xximage.c | 6 +- tools/mkimage.c | 869 +++++++++++++++++++++----------------- tools/mtk_image.c | 26 +- tools/mxsimage.c | 37 +- tools/omapimage.c | 11 +- tools/pblimage.c | 40 +- tools/renesas_spkgimage.c | 35 +- tools/rkcommon.c | 109 +++-- tools/rkcommon.h | 20 +- tools/rkimage.c | 8 +- tools/rkspi.c | 17 +- tools/sfspl.c | 25 +- tools/socfpgaimage.c | 66 ++- tools/stm32image.c | 10 +- tools/sunxi_egon.c | 37 +- tools/sunxi_toc0.c | 29 +- tools/ublimage.c | 24 +- tools/vybridimage.c | 6 +- tools/zynqimage.c | 24 +- tools/zynqmpbif.c | 14 +- tools/zynqmpimage.c | 42 +- tools/zynqmpimage.h | 2 +- 38 files changed, 1138 insertions(+), 1082 deletions(-) -- 2.43.0 base-commit: 816236b75e1b9248d282fb81e0c77426add8385a branch: loadd

It is confusing to have the main program in the moddle of the mkimage.c file. Move copy_file() higher, so that main() is at the bottom. Drop the now-unnecessary forward declaration. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 233 ++++++++++++++++++++++++------------------------ 1 file changed, 115 insertions(+), 118 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 2954626a283..385581b5078 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -18,8 +18,6 @@ #include <sys/ioctl.h> #endif -static void copy_file(int, const char *, int); - /* parameters initialized by core will be used by the image type code */ static struct image_tool_params params = { .os = IH_OS_LINUX, @@ -445,6 +443,121 @@ static void verify_image(const struct image_type_params *tparams) (void)close(ifd); } +static void copy_file(int ifd, const char *datafile, int pad) +{ + int dfd; + struct stat sbuf; + unsigned char *ptr; + int tail; + int zero = 0; + uint8_t zeros[4096]; + int offset = 0; + int size, ret; + struct image_type_params *tparams = imagetool_get_type(params.type); + + memset(zeros, 0, sizeof(zeros)); + + if (params.vflag) + fprintf(stderr, "Adding Image %s\n", datafile); + + dfd = open(datafile, O_RDONLY | O_BINARY); + if (dfd < 0) { + fprintf(stderr, "%s: Can't open %s: %s\n", + params.cmdname, datafile, strerror(errno)); + exit (EXIT_FAILURE); + } + + if (fstat(dfd, &sbuf) < 0) { + fprintf(stderr, "%s: Can't stat %s: %s\n", + params.cmdname, datafile, strerror(errno)); + exit (EXIT_FAILURE); + } + + if (sbuf.st_size == 0) { + fprintf(stderr, "%s: Input file %s is empty, bailing out\n", + params.cmdname, datafile); + exit (EXIT_FAILURE); + } + + ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); + if (ptr == MAP_FAILED) { + fprintf(stderr, "%s: Can't read %s: %s\n", + params.cmdname, datafile, strerror(errno)); + exit (EXIT_FAILURE); + } + + if (params.xflag && + ((params.type > IH_TYPE_INVALID && params.type < IH_TYPE_FLATDT) || + params.type == IH_TYPE_KERNEL_NOLOAD || + params.type == IH_TYPE_FIRMWARE_IVT)) { + unsigned char *p = NULL; + /* + * XIP: do not append the struct legacy_img_hdr at the + * beginning of the file, but consume the space + * reserved for it. + */ + + if ((unsigned int)sbuf.st_size < tparams->header_size) { + fprintf(stderr, + "%s: Bad size: \"%s\" is too small for XIP\n", + params.cmdname, datafile); + exit (EXIT_FAILURE); + } + + for (p = ptr; p < ptr + tparams->header_size; p++) { + if (*p != 0xff) { + fprintf(stderr, + "%s: Bad file: \"%s\" has invalid buffer for XIP\n", + params.cmdname, datafile); + exit (EXIT_FAILURE); + } + } + + offset = tparams->header_size; + } + + size = sbuf.st_size - offset; + + ret = write(ifd, ptr + offset, size); + if (ret != size) { + if (ret < 0) + fprintf(stderr, "%s: Write error on %s: %s\n", + params.cmdname, params.imagefile, strerror(errno)); + else if (ret < size) + fprintf(stderr, "%s: Write only %d/%d bytes, " + "probably no space left on the device\n", + params.cmdname, ret, size); + exit (EXIT_FAILURE); + } + + tail = size % 4; + if (pad == 1 && tail != 0) { + if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) { + fprintf(stderr, "%s: Write error on %s: %s\n", + params.cmdname, params.imagefile, + strerror(errno)); + exit (EXIT_FAILURE); + } + } else if (pad > 1) { + while (pad > 0) { + int todo = sizeof(zeros); + + if (todo > pad) + todo = pad; + if (write(ifd, (char *)&zeros, todo) != todo) { + fprintf(stderr, "%s: Write error on %s: %s\n", + params.cmdname, params.imagefile, + strerror(errno)); + exit(EXIT_FAILURE); + } + pad -= todo; + } + } + + (void)munmap((void *)ptr, sbuf.st_size); + (void)close(dfd); +} + void copy_datafile(int ifd, char *file) { if (!file) @@ -822,119 +935,3 @@ int main(int argc, char **argv) exit (EXIT_SUCCESS); } - -static void -copy_file (int ifd, const char *datafile, int pad) -{ - int dfd; - struct stat sbuf; - unsigned char *ptr; - int tail; - int zero = 0; - uint8_t zeros[4096]; - int offset = 0; - int size, ret; - struct image_type_params *tparams = imagetool_get_type(params.type); - - memset(zeros, 0, sizeof(zeros)); - - if (params.vflag) { - fprintf (stderr, "Adding Image %s\n", datafile); - } - - if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { - fprintf (stderr, "%s: Can't open %s: %s\n", - params.cmdname, datafile, strerror(errno)); - exit (EXIT_FAILURE); - } - - if (fstat(dfd, &sbuf) < 0) { - fprintf (stderr, "%s: Can't stat %s: %s\n", - params.cmdname, datafile, strerror(errno)); - exit (EXIT_FAILURE); - } - - if (sbuf.st_size == 0) { - fprintf (stderr, "%s: Input file %s is empty, bailing out\n", - params.cmdname, datafile); - exit (EXIT_FAILURE); - } - - ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); - if (ptr == MAP_FAILED) { - fprintf (stderr, "%s: Can't read %s: %s\n", - params.cmdname, datafile, strerror(errno)); - exit (EXIT_FAILURE); - } - - if (params.xflag && - (((params.type > IH_TYPE_INVALID) && (params.type < IH_TYPE_FLATDT)) || - (params.type == IH_TYPE_KERNEL_NOLOAD) || (params.type == IH_TYPE_FIRMWARE_IVT))) { - unsigned char *p = NULL; - /* - * XIP: do not append the struct legacy_img_hdr at the - * beginning of the file, but consume the space - * reserved for it. - */ - - if ((unsigned)sbuf.st_size < tparams->header_size) { - fprintf (stderr, - "%s: Bad size: \"%s\" is too small for XIP\n", - params.cmdname, datafile); - exit (EXIT_FAILURE); - } - - for (p = ptr; p < ptr + tparams->header_size; p++) { - if ( *p != 0xff ) { - fprintf (stderr, - "%s: Bad file: \"%s\" has invalid buffer for XIP\n", - params.cmdname, datafile); - exit (EXIT_FAILURE); - } - } - - offset = tparams->header_size; - } - - size = sbuf.st_size - offset; - - ret = write(ifd, ptr + offset, size); - if (ret != size) { - if (ret < 0) - fprintf (stderr, "%s: Write error on %s: %s\n", - params.cmdname, params.imagefile, strerror(errno)); - else if (ret < size) - fprintf (stderr, "%s: Write only %d/%d bytes, "\ - "probably no space left on the device\n", - params.cmdname, ret, size); - exit (EXIT_FAILURE); - } - - tail = size % 4; - if ((pad == 1) && (tail != 0)) { - - if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { - fprintf (stderr, "%s: Write error on %s: %s\n", - params.cmdname, params.imagefile, - strerror(errno)); - exit (EXIT_FAILURE); - } - } else if (pad > 1) { - while (pad > 0) { - int todo = sizeof(zeros); - - if (todo > pad) - todo = pad; - if (write(ifd, (char *)&zeros, todo) != todo) { - fprintf(stderr, "%s: Write error on %s: %s\n", - params.cmdname, params.imagefile, - strerror(errno)); - exit(EXIT_FAILURE); - } - pad -= todo; - } - } - - (void) munmap((void *)ptr, sbuf.st_size); - (void) close (dfd); -} -- 2.43.0

Create a new run_mkimage() function which will hold the top-level logic for mkimage. For the few cases where there is a 'return', use that as the exit code of the problem, preserving existing behaviour. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 385581b5078..0f432a20cfb 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -577,22 +577,23 @@ void copy_datafile(int ifd, char *file) } } -int main(int argc, char **argv) +/** + * run_mkimage() - Run the mkimage tool + * + * The program arguments are in params + * + * Return: 0 on success, or non-zero exit code + */ +static int run_mkimage(void) { - int ifd = -1; - struct stat sbuf; - char *ptr; - int retval = 0; struct image_type_params *tparams = NULL; + struct stat sbuf; int pad_len = 0; - int dfd; + int retval = 0; size_t map_len; - - params.cmdname = *argv; - params.addr = 0; - params.ep = 0; - - process_args(argc, argv); + int ifd = -1; + char *ptr; + int dfd; /* set tparams as per input type_id */ tparams = imagetool_get_type(params.type); @@ -935,3 +936,18 @@ int main(int argc, char **argv) exit (EXIT_SUCCESS); } + +int main(int argc, char **argv) +{ + int ret; + + params.cmdname = *argv; + params.addr = 0; + params.ep = 0; + + process_args(argc, argv); + + ret = run_mkimage(); + + return ret; +} -- 2.43.0

Rather than exiting in the middle of the function, return the exit code to main() so it can return it, in turn. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 0f432a20cfb..a8073485180 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -600,7 +600,7 @@ static int run_mkimage(void) if (tparams == NULL && !params.lflag) { fprintf (stderr, "%s: unsupported type %s\n", params.cmdname, genimg_get_type_name(params.type)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } /* @@ -622,7 +622,7 @@ static int run_mkimage(void) if (!tparams) { fprintf(stderr, "%s: Missing FIT support\n", params.cmdname); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } if (tparams->fflag_handle) /* @@ -648,7 +648,7 @@ static int run_mkimage(void) fprintf (stderr, "%s: Can't open %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } if (params.lflag || params.fflag) { @@ -660,7 +660,7 @@ static int run_mkimage(void) fprintf (stderr, "%s: Can't stat %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } if ((sbuf.st_mode & S_IFMT) == S_IFBLK) { @@ -672,13 +672,13 @@ static int run_mkimage(void) fprintf (stderr, "%s: failed to get size of block device \"%s\"\n", params.cmdname, params.imagefile); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } #else fprintf (stderr, "%s: \"%s\" is block device, don't know how to get its size\n", params.cmdname, params.imagefile); - exit (EXIT_FAILURE); + return EXIT_FAILURE; #endif } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) { fprintf (stderr, @@ -686,7 +686,7 @@ static int run_mkimage(void) params.cmdname, params.imagefile, (unsigned long long) sbuf.st_size, tparams->header_size); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } else { size = sbuf.st_size; } @@ -696,7 +696,7 @@ static int run_mkimage(void) fprintf (stderr, "%s: Can't read %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } /* @@ -712,28 +712,28 @@ static int run_mkimage(void) summary_show(¶ms.summary, params.imagefile, params.keydest); - exit (retval); + return retval; } if (!params.skipcpy && params.type != IH_TYPE_MULTI && params.type != IH_TYPE_SCRIPT) { if (!params.datafile) { fprintf(stderr, "%s: Option -d with image data file was not specified\n", params.cmdname); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } dfd = open(params.datafile, O_RDONLY | O_BINARY); if (dfd < 0) { fprintf(stderr, "%s: Can't open %s: %s\n", params.cmdname, params.datafile, strerror(errno)); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } if (fstat(dfd, &sbuf) < 0) { fprintf(stderr, "%s: Can't stat %s: %s\n", params.cmdname, params.datafile, strerror(errno)); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } params.file_size = sbuf.st_size + tparams->header_size; @@ -755,7 +755,7 @@ static int run_mkimage(void) != tparams->header_size) { fprintf (stderr, "%s: Write error on %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } if (!params.skipcpy) { @@ -775,7 +775,7 @@ static int run_mkimage(void) if (stat (file, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", params.cmdname, file, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } size = cpu_to_uimage (sbuf.st_size); } else { @@ -786,7 +786,7 @@ static int run_mkimage(void) fprintf (stderr, "%s: Write error on %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } if (!file) { @@ -855,7 +855,7 @@ static int run_mkimage(void) params.cmdname, params.imagefile, strerror(errno)); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } } if (write(ifd, &ivt_header, sizeof(flash_header_v2_t)) @@ -864,7 +864,7 @@ static int run_mkimage(void) params.cmdname, params.imagefile, strerror(errno)); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } } } @@ -883,7 +883,7 @@ static int run_mkimage(void) if (fstat(ifd, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } params.file_size = sbuf.st_size; @@ -892,7 +892,7 @@ static int run_mkimage(void) if (ptr == MAP_FAILED) { fprintf (stderr, "%s: Can't map %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } /* Setup the image header as per input image type*/ @@ -901,7 +901,7 @@ static int run_mkimage(void) else { fprintf (stderr, "%s: Can't set header for %s\n", params.cmdname, tparams->name); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } /* Print the image information by processing image header */ @@ -928,13 +928,13 @@ static int run_mkimage(void) if (close(ifd)) { fprintf (stderr, "%s: Write error on %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } if (tparams->verify_header) verify_image(tparams); - exit (EXIT_SUCCESS); + return 0; } int main(int argc, char **argv) -- 2.43.0

Rather than exiting in this function, return an exit code. The caller is then responsible for returning it, so update all the callers to return the value. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index a8073485180..8a132362a5b 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -78,7 +78,13 @@ static int show_valid_options(enum ih_category category) return 0; } -static void usage(const char *msg) +/** + * usage() - Print a usage message + * + * Return: + * EXIT_FAILURE (always) + */ +static int usage(const char *msg) { fprintf(stderr, "Error: %s\n", msg); fprintf(stderr, "Usage: %s [-T type] -l image\n" @@ -136,7 +142,7 @@ static void usage(const char *msg) fprintf(stderr, "Use '-T list' to see a list of available image types\n"); fprintf(stderr, "Long options are available; read the man page for details\n"); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } static int add_content(int type, const char *fname) @@ -197,7 +203,7 @@ static const struct option longopts[] = { { /* sentinel */ }, }; -static void process_args(int argc, char **argv) +static int process_args(int argc, char **argv) { char *ptr; int type = IH_TYPE_INVALID; @@ -219,7 +225,7 @@ static void process_args(int argc, char **argv) params.arch = genimg_get_arch_id(optarg); if (params.arch < 0) { show_valid_options(IH_ARCH); - usage("Invalid architecture"); + return usage("Invalid architecture"); } params.Aflag = 1; break; @@ -247,7 +253,7 @@ static void process_args(int argc, char **argv) params.comp = genimg_get_comp_id(optarg); if (params.comp < 0) { show_valid_options(IH_COMP); - usage("Invalid compression type"); + return usage("Invalid compression type"); } break; case 'd': @@ -315,7 +321,7 @@ static void process_args(int argc, char **argv) params.os = genimg_get_os_id(optarg); if (params.os < 0) { show_valid_options(IH_OS); - usage("Invalid operating system"); + return usage("Invalid operating system"); } break; case 'p': @@ -353,7 +359,7 @@ static void process_args(int argc, char **argv) type = genimg_get_type_id(optarg); if (type < 0) { show_valid_options(IH_TYPE); - usage("Invalid image type"); + return usage("Invalid image type"); } break; case 'v': @@ -366,7 +372,7 @@ static void process_args(int argc, char **argv) params.xflag++; break; default: - usage("Invalid option"); + return usage("Invalid option"); } } @@ -376,11 +382,12 @@ static void process_args(int argc, char **argv) if (params.auto_fit == AF_SIGNED_CONF) { if (!params.keyname || !params.algo_name) - usage("Missing key/algo for auto-FIT with signed configs (use -g -o)"); + return usage( + "Missing key/algo for auto-FIT with signed configs (use -g -o)"); } else if (params.auto_fit == AF_HASHED_IMG && params.keyname) { params.auto_fit = AF_SIGNED_IMG; if (!params.algo_name) - usage("Missing algorithm for auto-FIT with signed images (use -g)"); + return usage("Missing algorithm for auto-FIT with signed images (use -g)"); } /* @@ -394,15 +401,17 @@ static void process_args(int argc, char **argv) if (!params.auto_fit) params.datafile = datafile; else if (!params.datafile) - usage("Missing data file for auto-FIT (use -d)"); + return usage("Missing data file for auto-FIT (use -d)"); } else if (params.lflag || type != IH_TYPE_INVALID) { if (type == IH_TYPE_SCRIPT && !params.datafile) - usage("Missing data file for script (use -d)"); + return usage("Missing data file for script (use -d)"); params.type = type; } if (!params.imagefile) - usage("Missing output filename"); + return usage("Missing output filename"); + + return 0; } static void verify_image(const struct image_type_params *tparams) @@ -609,7 +618,7 @@ static int run_mkimage(void) */ if (tparams && tparams->check_params) if (tparams->check_params (¶ms)) - usage("Bad parameters for image type"); + return usage("Bad parameters for image type"); if (!params.eflag) { params.ep = params.addr; @@ -634,7 +643,7 @@ static int run_mkimage(void) retval = tparams->fflag_handle(¶ms); if (retval != EXIT_SUCCESS) - usage("Bad parameters for FIT image type"); + return usage("Bad parameters for FIT image type"); } if (params.lflag || params.fflag) { @@ -945,9 +954,9 @@ int main(int argc, char **argv) params.addr = 0; params.ep = 0; - process_args(argc, argv); - - ret = run_mkimage(); + ret = process_args(argc, argv); + if (!ret) + ret = run_mkimage(); return ret; } -- 2.43.0

The current name is quite confusing. Mostly this struct holds function pointers, not parameters. Rename it to imgtool_funcs, so that is has the same prefix as imgtool (introduced in the next patch) and it is clear that it provides functions. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/aisimage.c | 4 ++-- tools/atmelimage.c | 2 +- tools/dumpimage.c | 6 +++--- tools/imagetool.c | 20 ++++++++++---------- tools/imagetool.h | 28 +++++++++++++--------------- tools/imximage.c | 2 +- tools/kwbimage.c | 2 +- tools/mkimage.c | 6 +++--- tools/mtk_image.c | 2 +- tools/mxsimage.c | 4 ++-- tools/renesas_spkgimage.c | 2 +- tools/rkcommon.c | 2 +- tools/rkcommon.h | 2 +- tools/rkspi.c | 2 +- tools/sfspl.c | 2 +- tools/socfpgaimage.c | 6 +++--- tools/sunxi_egon.c | 2 +- tools/sunxi_toc0.c | 2 +- tools/zynqmpimage.c | 2 +- 19 files changed, 48 insertions(+), 50 deletions(-) diff --git a/tools/aisimage.c b/tools/aisimage.c index c5b33b559b0..697b3c0bad7 100644 --- a/tools/aisimage.c +++ b/tools/aisimage.c @@ -157,7 +157,7 @@ static void aisimage_print_header(const void *hdr, struct image_tool_params *par } static uint32_t *ais_insert_cmd_header(uint32_t cmd, uint32_t nargs, - uint32_t *parms, struct image_type_params *tparams, + uint32_t *parms, struct imgtool_funcs *tparams, uint32_t *ptr) { int i; @@ -252,7 +252,7 @@ static uint32_t *ais_copy_image(struct image_tool_params *params, } static int aisimage_generate(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { FILE *fd = NULL; char *line = NULL; diff --git a/tools/atmelimage.c b/tools/atmelimage.c index 6a2d9d8feab..e1909f39c45 100644 --- a/tools/atmelimage.c +++ b/tools/atmelimage.c @@ -238,7 +238,7 @@ static int atmel_check_params(struct image_tool_params *params) } static int atmel_vrec_header(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { uint32_t tmp; size_t pos; diff --git a/tools/dumpimage.c b/tools/dumpimage.c index 4791dd0dfe1..b15aee934c9 100644 --- a/tools/dumpimage.c +++ b/tools/dumpimage.c @@ -25,8 +25,8 @@ static struct image_tool_params params; * returns negative if input image format does not match with any of * supported image types */ -static int dumpimage_extract_subimage(struct image_type_params *tparams, - void *ptr, struct stat *sbuf) +static int dumpimage_extract_subimage(struct imgtool_funcs *tparams, + void *ptr, struct stat *sbuf) { int retval = -1; @@ -68,7 +68,7 @@ int main(int argc, char **argv) struct stat sbuf; char *ptr; int retval = EXIT_SUCCESS; - struct image_type_params *tparams = NULL; + struct imgtool_funcs *tparams = NULL; params.cmdname = *argv; diff --git a/tools/imagetool.c b/tools/imagetool.c index b293211cf88..b36b8d735a4 100644 --- a/tools/imagetool.c +++ b/tools/imagetool.c @@ -9,13 +9,13 @@ #include <image.h> -struct image_type_params *imagetool_get_type(int type) +struct imgtool_funcs *imagetool_get_type(int type) { - struct image_type_params **curr; + struct imgtool_funcs **curr; INIT_SECTION(image_type); - struct image_type_params **start = __start_image_type; - struct image_type_params **end = __stop_image_type; + struct imgtool_funcs **start = __start_image_type; + struct imgtool_funcs **end = __stop_image_type; for (curr = start; curr != end; curr++) { if ((*curr)->check_image_type) { @@ -29,21 +29,21 @@ struct image_type_params *imagetool_get_type(int type) static int imagetool_verify_print_header_by_type( void *ptr, struct stat *sbuf, - struct image_type_params *tparams, + struct imgtool_funcs *tparams, struct image_tool_params *params); int imagetool_verify_print_header( void *ptr, struct stat *sbuf, - struct image_type_params *tparams, + struct imgtool_funcs *tparams, struct image_tool_params *params) { int retval = -1; - struct image_type_params **curr; + struct imgtool_funcs **curr; INIT_SECTION(image_type); - struct image_type_params **start = __start_image_type; - struct image_type_params **end = __stop_image_type; + struct imgtool_funcs **start = __start_image_type; + struct imgtool_funcs **end = __stop_image_type; if (tparams) return imagetool_verify_print_header_by_type(ptr, sbuf, tparams, params); @@ -88,7 +88,7 @@ int imagetool_verify_print_header( static int imagetool_verify_print_header_by_type( void *ptr, struct stat *sbuf, - struct image_type_params *tparams, + struct imgtool_funcs *tparams, struct image_tool_params *params) { int retval = -1; diff --git a/tools/imagetool.h b/tools/imagetool.h index 57be608210a..3cbb053e42b 100644 --- a/tools/imagetool.h +++ b/tools/imagetool.h @@ -101,10 +101,8 @@ struct image_tool_params { struct image_summary summary; /* results of signing process */ }; -/* - * image type specific variables and callback functions - */ -struct image_type_params { +/** struct imgtool_funcs - image-type-specific variables and callbacks */ +struct imgtool_funcs { /* name is an identification tag string for added support */ char *name; /* @@ -163,13 +161,13 @@ struct image_type_params { /* * This callback function will be executed for variable size record * It is expected to build this header in memory and return its length - * and a pointer to it by using image_type_params.header_size and - * image_type_params.hdr. The return value shall indicate if an + * and a pointer to it by using imgtool_funcs.header_size and + * imgtool_funcs.hdr. The return value shall indicate if an * additional padding should be used when copying the data image * by returning the padding length. */ - int (*vrec_header) (struct image_tool_params *, - struct image_type_params *); + int (*vrec_header)(struct image_tool_params *, + struct imgtool_funcs *funcs); }; /** @@ -179,11 +177,11 @@ struct image_type_params { * checks the input type for each supported image type * * if successful, - * returns respective image_type_params pointer if success + * returns respective imgtool_funcs pointer if success * if input type_id is not supported by any of image_type_support * returns NULL */ -struct image_type_params *imagetool_get_type(int type); +struct imgtool_funcs *imagetool_get_type(int type); /* * imagetool_verify_print_header() - verifies the image header @@ -203,7 +201,7 @@ struct image_type_params *imagetool_get_type(int type); int imagetool_verify_print_header( void *ptr, struct stat *sbuf, - struct image_type_params *tparams, + struct imgtool_funcs *tparams, struct image_tool_params *params); /** @@ -286,14 +284,14 @@ int rockchip_copy_image(int fd, struct image_tool_params *mparams); } while (0) #define SECTION(name) __attribute__((section("__DATA, " #name))) -struct image_type_params **__start_image_type, **__stop_image_type; +struct imgtool_funcs **__start_image_type, **__stop_image_type; #else #define INIT_SECTION(name) /* no-op for ELF */ #define SECTION(name) __attribute__((section(#name))) /* We construct a table of pointers in an ELF section (pointers generally * go unpadded by gcc). ld creates boundary syms for us. */ -extern struct image_type_params *__start_image_type[], *__stop_image_type[]; +extern struct imgtool_funcs *__start_image_type[], *__stop_image_type[]; #endif /* __MACH__ */ #if !defined(__used) @@ -318,7 +316,7 @@ extern struct image_type_params *__start_image_type[], *__stop_image_type[]; _fflag_handle, \ _vrec_header \ ) \ - static struct image_type_params __cat(image_type_, _id) = \ + static struct imgtool_funcs __cat(image_type_, _id) = \ { \ .name = _name, \ .header_size = _header_size, \ @@ -332,7 +330,7 @@ extern struct image_type_params *__start_image_type[], *__stop_image_type[]; .fflag_handle = _fflag_handle, \ .vrec_header = _vrec_header \ }; \ - static struct image_type_params *SECTION(image_type) __used \ + static struct imgtool_funcs *SECTION(image_type) __used \ __cat(image_type_ptr_, _id) = &__cat(image_type_, _id) #endif /* _IMAGETOOL_H_ */ diff --git a/tools/imximage.c b/tools/imximage.c index 55231caf8f3..7a5dde617d9 100644 --- a/tools/imximage.c +++ b/tools/imximage.c @@ -967,7 +967,7 @@ static void generate_fspi_header(int ifd) #endif static int imximage_generate(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { struct imx_header *imxhdr; size_t alloc_len; diff --git a/tools/kwbimage.c b/tools/kwbimage.c index 3dcf5ba66b9..d6c7206868e 100644 --- a/tools/kwbimage.c +++ b/tools/kwbimage.c @@ -2160,7 +2160,7 @@ static int kwbimage_verify_header(unsigned char *ptr, int image_size, } static int kwbimage_generate(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { FILE *fcfg; struct stat s; diff --git a/tools/mkimage.c b/tools/mkimage.c index 8a132362a5b..6e50254452d 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -414,7 +414,7 @@ static int process_args(int argc, char **argv) return 0; } -static void verify_image(const struct image_type_params *tparams) +static void verify_image(const struct imgtool_funcs *tparams) { struct stat sbuf; void *ptr; @@ -462,7 +462,7 @@ static void copy_file(int ifd, const char *datafile, int pad) uint8_t zeros[4096]; int offset = 0; int size, ret; - struct image_type_params *tparams = imagetool_get_type(params.type); + struct imgtool_funcs *tparams = imagetool_get_type(params.type); memset(zeros, 0, sizeof(zeros)); @@ -595,7 +595,7 @@ void copy_datafile(int ifd, char *file) */ static int run_mkimage(void) { - struct image_type_params *tparams = NULL; + struct imgtool_funcs *tparams = NULL; struct stat sbuf; int pad_len = 0; int retval = 0; diff --git a/tools/mtk_image.c b/tools/mtk_image.c index 1b1aed59924..8a22b4ef789 100644 --- a/tools/mtk_image.c +++ b/tools/mtk_image.c @@ -244,7 +244,7 @@ static int mtk_image_check_params(struct image_tool_params *params) } static int mtk_image_vrec_header(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { if (use_lk_hdr) { tparams->header_size = sizeof(union lk_hdr); diff --git a/tools/mxsimage.c b/tools/mxsimage.c index 42df0698ca2..45c6e3624c9 100644 --- a/tools/mxsimage.c +++ b/tools/mxsimage.c @@ -2245,7 +2245,7 @@ static void mxsimage_print_header(const void *hdr, struct image_tool_params *par } static int sb_build_image(struct sb_image_ctx *ictx, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { struct sb_boot_image_header *sb_header = &ictx->payload; struct sb_section_ctx *sctx; @@ -2315,7 +2315,7 @@ static int sb_build_image(struct sb_image_ctx *ictx, } static int mxsimage_generate(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { int ret; struct sb_image_ctx ctx; diff --git a/tools/renesas_spkgimage.c b/tools/renesas_spkgimage.c index ce3b2b28ae7..ed80aec3c6b 100644 --- a/tools/renesas_spkgimage.c +++ b/tools/renesas_spkgimage.c @@ -217,7 +217,7 @@ static inline uint32_t roundup(uint32_t x, uint32_t y) } static int spkgimage_vrec_header(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { struct stat s; struct spkg_file *out_buf; diff --git a/tools/rkcommon.c b/tools/rkcommon.c index a0caa029cc0..f938b524329 100644 --- a/tools/rkcommon.c +++ b/tools/rkcommon.c @@ -544,7 +544,7 @@ void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size) } int rkcommon_vrec_header(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { /* * The SPL image looks as follows: diff --git a/tools/rkcommon.h b/tools/rkcommon.h index 5d2770a80f1..792a046c7af 100644 --- a/tools/rkcommon.h +++ b/tools/rkcommon.h @@ -102,6 +102,6 @@ void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size); * Return: 0 (always) */ int rkcommon_vrec_header(struct image_tool_params *params, - struct image_type_params *tparams); + struct imgtool_funcs *tparams); #endif diff --git a/tools/rkspi.c b/tools/rkspi.c index f2530f7bde3..284d9dbc02c 100644 --- a/tools/rkspi.c +++ b/tools/rkspi.c @@ -58,7 +58,7 @@ static int rkspi_check_image_type(uint8_t type) * (i.e. only the first 2K of each 4K sector is used). */ static int rkspi_vrec_header(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { rkcommon_vrec_header(params, tparams); diff --git a/tools/sfspl.c b/tools/sfspl.c index 516e96e8dd9..e0c15243c9d 100644 --- a/tools/sfspl.c +++ b/tools/sfspl.c @@ -153,7 +153,7 @@ static void sfspl_set_header(void *buf, struct stat *sbuf, int infd, } static int sfspl_vrec_header(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { tparams->hdr = calloc(sizeof(struct spl_hdr), 1); diff --git a/tools/socfpgaimage.c b/tools/socfpgaimage.c index 953dfeed4d5..64c5f498355 100644 --- a/tools/socfpgaimage.c +++ b/tools/socfpgaimage.c @@ -398,7 +398,7 @@ static int sfp_fake_header_size(unsigned int size, uint8_t ver) } static int sfp_vrec_header(struct image_tool_params *params, - struct image_type_params *tparams, uint8_t ver) + struct imgtool_funcs *tparams, uint8_t ver) { struct stat sbuf; @@ -413,13 +413,13 @@ static int sfp_vrec_header(struct image_tool_params *params, } static int socfpgaimage_vrec_header_v0(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { return sfp_vrec_header(params, tparams, 0); } static int socfpgaimage_vrec_header_v1(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { return sfp_vrec_header(params, tparams, 1); } diff --git a/tools/sunxi_egon.c b/tools/sunxi_egon.c index a514427809a..d0a55226a9e 100644 --- a/tools/sunxi_egon.c +++ b/tools/sunxi_egon.c @@ -172,7 +172,7 @@ static int egon_check_image_type(uint8_t type) } static int egon_vrec_header(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { int pad_size = ALIGN(params->bl_len ?: PAD_SIZE, PAD_SIZE_MIN); diff --git a/tools/sunxi_toc0.c b/tools/sunxi_toc0.c index 76693647a09..e52cbe325a3 100644 --- a/tools/sunxi_toc0.c +++ b/tools/sunxi_toc0.c @@ -889,7 +889,7 @@ static int toc0_check_image_type(uint8_t type) } static int toc0_vrec_header(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { tparams->hdr = calloc(tparams->header_size, 1); diff --git a/tools/zynqmpimage.c b/tools/zynqmpimage.c index 4db9877127e..a075ac22bdd 100644 --- a/tools/zynqmpimage.c +++ b/tools/zynqmpimage.c @@ -499,7 +499,7 @@ static int zynqmpimage_extract_contents(void *ptr, struct image_tool_params *par } static int zynqmpimage_vrec_header(struct image_tool_params *params, - struct image_type_params *tparams) + struct imgtool_funcs *tparams) { struct stat path_stat; char *filename = params->imagename; -- 2.43.0

Update usage(), process_args() and run_mkimage() to pass the image-tool info around, so we can eventually drop the global. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 03492c795aa..0d661e77e4e 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -84,14 +84,14 @@ static int show_valid_options(enum ih_category category) * Return: * EXIT_FAILURE (always) */ -static int usage(const char *msg) +static int usage(const struct imgtool *itl, const char *msg) { fprintf(stderr, "Error: %s\n", msg); fprintf(stderr, "Usage: %s [-T type] -l image\n" " -l ==> list image header information\n" " -T ==> parse image file as 'type'\n" " -q ==> quiet\n", - params.cmdname); + itl->cmdname); fprintf(stderr, " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n" " -A ==> set architecture to 'arch'\n" @@ -106,11 +106,11 @@ static int usage(const char *msg) " -x ==> set XIP (execute in place)\n" " -s ==> create an image with no data\n" " -v ==> verbose\n", - params.cmdname); + itl->cmdname); fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-f auto|-f auto-conf|-F] [-b <dtb> [-b <dtb>]] [-E] [-B size] [-i <ramdisk.cpio.gz>] fit-image\n" " <dtb> file is used with -f auto, it may occur multiple times.\n", - params.cmdname); + itl->cmdname); fprintf(stderr, " -D => set all options for device tree compiler\n" " -f => input filename for FIT source\n" @@ -138,7 +138,7 @@ static int usage(const char *msg) #endif fprintf(stderr, " %s -V ==> print version information and exit\n", - params.cmdname); + itl->cmdname); fprintf(stderr, "Use '-T list' to see a list of available image types\n"); fprintf(stderr, "Long options are available; read the man page for details\n"); @@ -203,7 +203,7 @@ static const struct option longopts[] = { { /* sentinel */ }, }; -static int process_args(int argc, char **argv) +static int process_args(struct imgtool *itl, int argc, char **argv) { char *ptr; int type = IH_TYPE_INVALID; @@ -225,7 +225,7 @@ static int process_args(int argc, char **argv) params.arch = genimg_get_arch_id(optarg); if (params.arch < 0) { show_valid_options(IH_ARCH); - return usage("Invalid architecture"); + return usage(itl, "Invalid architecture"); } params.Aflag = 1; break; @@ -253,7 +253,7 @@ static int process_args(int argc, char **argv) params.comp = genimg_get_comp_id(optarg); if (params.comp < 0) { show_valid_options(IH_COMP); - return usage("Invalid compression type"); + return usage(itl, "Invalid compression type"); } break; case 'd': @@ -321,7 +321,7 @@ static int process_args(int argc, char **argv) params.os = genimg_get_os_id(optarg); if (params.os < 0) { show_valid_options(IH_OS); - return usage("Invalid operating system"); + return usage(itl, "Invalid operating system"); } break; case 'p': @@ -359,7 +359,7 @@ static int process_args(int argc, char **argv) type = genimg_get_type_id(optarg); if (type < 0) { show_valid_options(IH_TYPE); - return usage("Invalid image type"); + return usage(itl, "Invalid image type"); } break; case 'v': @@ -372,7 +372,7 @@ static int process_args(int argc, char **argv) params.xflag++; break; default: - return usage("Invalid option"); + return usage(itl, "Invalid option"); } } @@ -382,12 +382,13 @@ static int process_args(int argc, char **argv) if (params.auto_fit == AF_SIGNED_CONF) { if (!params.keyname || !params.algo_name) - return usage( + return usage(itl, "Missing key/algo for auto-FIT with signed configs (use -g -o)"); } else if (params.auto_fit == AF_HASHED_IMG && params.keyname) { params.auto_fit = AF_SIGNED_IMG; if (!params.algo_name) - return usage("Missing algorithm for auto-FIT with signed images (use -g)"); + return usage(itl, + "Missing algorithm for auto-FIT with signed images (use -g)"); } /* @@ -401,15 +402,17 @@ static int process_args(int argc, char **argv) if (!params.auto_fit) params.datafile = datafile; else if (!params.datafile) - return usage("Missing data file for auto-FIT (use -d)"); + return usage(itl, + "Missing data file for auto-FIT (use -d)"); } else if (params.lflag || type != IH_TYPE_INVALID) { if (type == IH_TYPE_SCRIPT && !params.datafile) - return usage("Missing data file for script (use -d)"); + return usage(itl, + "Missing data file for script (use -d)"); params.type = type; } if (!params.imagefile) - return usage("Missing output filename"); + return usage(itl, "Missing output filename"); return 0; } @@ -593,7 +596,7 @@ void copy_datafile(int ifd, char *file) * * Return: 0 on success, or non-zero exit code */ -static int run_mkimage(void) +static int run_mkimage(struct imgtool *itl) { struct imgtool_funcs *tparams = NULL; struct stat sbuf; @@ -618,7 +621,7 @@ static int run_mkimage(void) */ if (tparams && tparams->check_params) if (tparams->check_params (¶ms)) - return usage("Bad parameters for image type"); + return usage(itl, "Bad parameters for image type"); if (!params.eflag) { params.ep = params.addr; @@ -643,7 +646,7 @@ static int run_mkimage(void) retval = tparams->fflag_handle(¶ms); if (retval != EXIT_SUCCESS) - return usage("Bad parameters for FIT image type"); + return usage(itl, "Bad parameters for FIT image type"); } if (params.lflag || params.fflag) { @@ -954,9 +957,9 @@ int main(int argc, char **argv) params.addr = 0; params.ep = 0; - ret = process_args(argc, argv); + ret = process_args(¶ms, argc, argv); if (!ret) - ret = run_mkimage(); + ret = run_mkimage(¶ms); return ret; } -- 2.43.0

Pass this pointer in rather than using the global. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 0d661e77e4e..c90db7dad60 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -145,7 +145,7 @@ static int usage(const struct imgtool *itl, const char *msg) return EXIT_FAILURE; } -static int add_content(int type, const char *fname) +static int add_content(struct imgtool *itl, int type, const char *fname) { struct content_info *cont; @@ -154,11 +154,11 @@ static int add_content(int type, const char *fname) return -1; cont->type = type; cont->fname = fname; - if (params.content_tail) - params.content_tail->next = cont; + if (itl->content_tail) + itl->content_tail->next = cont; else - params.content_head = cont; - params.content_tail = cont; + itl->content_head = cont; + itl->content_tail = cont; return 0; } @@ -230,7 +230,7 @@ static int process_args(struct imgtool *itl, int argc, char **argv) params.Aflag = 1; break; case 'b': - if (add_content(IH_TYPE_FLATDT, optarg)) { + if (add_content(itl, IH_TYPE_FLATDT, optarg)) { fprintf(stderr, "%s: Out of memory adding content '%s'", params.cmdname, optarg); -- 2.43.0

Pass this pointer in rather than using the global. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 118 ++++++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index c90db7dad60..6074449e399 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -214,142 +214,142 @@ static int process_args(struct imgtool *itl, int argc, char **argv) longopts, NULL)) != -1) { switch (opt) { case 'a': - params.addr = strtoull(optarg, &ptr, 16); + itl->addr = strtoull(optarg, &ptr, 16); if (*ptr) { fprintf(stderr, "%s: invalid load address %s\n", - params.cmdname, optarg); + itl->cmdname, optarg); exit(EXIT_FAILURE); } break; case 'A': - params.arch = genimg_get_arch_id(optarg); - if (params.arch < 0) { + itl->arch = genimg_get_arch_id(optarg); + if (itl->arch < 0) { show_valid_options(IH_ARCH); return usage(itl, "Invalid architecture"); } - params.Aflag = 1; + itl->Aflag = 1; break; case 'b': if (add_content(itl, IH_TYPE_FLATDT, optarg)) { fprintf(stderr, "%s: Out of memory adding content '%s'", - params.cmdname, optarg); + itl->cmdname, optarg); exit(EXIT_FAILURE); } break; case 'B': - params.bl_len = strtoull(optarg, &ptr, 16); + itl->bl_len = strtoull(optarg, &ptr, 16); if (*ptr) { fprintf(stderr, "%s: invalid block length %s\n", - params.cmdname, optarg); + itl->cmdname, optarg); exit(EXIT_FAILURE); } break; case 'c': - params.comment = optarg; + itl->comment = optarg; break; case 'C': - params.comp = genimg_get_comp_id(optarg); - if (params.comp < 0) { + itl->comp = genimg_get_comp_id(optarg); + if (itl->comp < 0) { show_valid_options(IH_COMP); return usage(itl, "Invalid compression type"); } break; case 'd': - params.datafile = optarg; - params.dflag = 1; + itl->datafile = optarg; + itl->dflag = 1; break; case 'D': - params.dtc = optarg; + itl->dtc = optarg; break; case 'e': - params.ep = strtoull(optarg, &ptr, 16); + itl->ep = strtoull(optarg, &ptr, 16); if (*ptr) { fprintf(stderr, "%s: invalid entry point %s\n", - params.cmdname, optarg); + itl->cmdname, optarg); exit(EXIT_FAILURE); } - params.eflag = 1; + itl->eflag = 1; break; case 'E': - params.external_data = true; + itl->external_data = true; break; case 'f': datafile = optarg; if (!strcmp(datafile, "auto")) - params.auto_fit = AF_HASHED_IMG; + itl->auto_fit = AF_HASHED_IMG; else if (!strcmp(datafile, "auto-conf")) - params.auto_fit = AF_SIGNED_CONF; + itl->auto_fit = AF_SIGNED_CONF; /* fallthrough */ case 'F': /* * The flattened image tree (FIT) format * requires a flattened device tree image type */ - params.type = IH_TYPE_FLATDT; - params.fflag = 1; + itl->type = IH_TYPE_FLATDT; + itl->fflag = 1; break; case 'g': - params.keyname = optarg; + itl->keyname = optarg; break; case 'G': - params.keyfile = optarg; + itl->keyfile = optarg; break; case 'i': - params.fit_ramdisk = optarg; + itl->fit_ramdisk = optarg; break; case 'k': - params.keydir = optarg; + itl->keydir = optarg; break; case 'K': - params.keydest = optarg; + itl->keydest = optarg; break; case 'l': - params.lflag = 1; + itl->lflag = 1; break; case 'n': - params.imagename = optarg; + itl->imagename = optarg; break; case 'N': - params.engine_id = optarg; + itl->engine_id = optarg; break; case 'o': - params.algo_name = optarg; + itl->algo_name = optarg; break; case 'O': - params.os = genimg_get_os_id(optarg); - if (params.os < 0) { + itl->os = genimg_get_os_id(optarg); + if (itl->os < 0) { show_valid_options(IH_OS); return usage(itl, "Invalid operating system"); } break; case 'p': - params.external_offset = strtoull(optarg, &ptr, 16); + itl->external_offset = strtoull(optarg, &ptr, 16); if (*ptr) { fprintf(stderr, "%s: invalid offset size %s\n", - params.cmdname, optarg); + itl->cmdname, optarg); exit(EXIT_FAILURE); } break; case 'q': - params.quiet = 1; + itl->quiet = 1; break; case 'r': - params.require_keys = 1; + itl->require_keys = 1; break; case 'R': /* * This entry is for the second configuration * file, if only one is not enough. */ - params.imagename2 = optarg; + itl->imagename2 = optarg; break; case 's': - params.skipcpy = 1; + itl->skipcpy = 1; break; case 't': - params.reset_timestamp = 1; + itl->reset_timestamp = 1; break; case 'T': if (strcmp(optarg, "list") == 0) { @@ -363,13 +363,13 @@ static int process_args(struct imgtool *itl, int argc, char **argv) } break; case 'v': - params.vflag++; + itl->vflag++; break; case 'V': printf("mkimage version %s\n", PLAIN_VERSION); exit(EXIT_SUCCESS); case 'x': - params.xflag++; + itl->xflag++; break; default: return usage(itl, "Invalid option"); @@ -378,17 +378,17 @@ static int process_args(struct imgtool *itl, int argc, char **argv) /* The last parameter is expected to be the imagefile */ if (optind < argc) - params.imagefile = argv[optind]; + itl->imagefile = argv[optind]; - if (params.auto_fit == AF_SIGNED_CONF) { - if (!params.keyname || !params.algo_name) + if (itl->auto_fit == AF_SIGNED_CONF) { + if (!itl->keyname || !itl->algo_name) return usage(itl, - "Missing key/algo for auto-FIT with signed configs (use -g -o)"); - } else if (params.auto_fit == AF_HASHED_IMG && params.keyname) { - params.auto_fit = AF_SIGNED_IMG; - if (!params.algo_name) + "Missing key/algo for auto-FIT with signed configs (use -g -o)"); + } else if (itl->auto_fit == AF_HASHED_IMG && itl->keyname) { + itl->auto_fit = AF_SIGNED_IMG; + if (!itl->algo_name) return usage(itl, - "Missing algorithm for auto-FIT with signed images (use -g)"); + "Missing algorithm for auto-FIT with signed images (use -g)"); } /* @@ -396,22 +396,22 @@ static int process_args(struct imgtool *itl, int argc, char **argv) * in the FIT, which is separate from the file's image type (which * will always be IH_TYPE_FLATDT in this case). */ - if (params.type == IH_TYPE_FLATDT) { - params.fit_image_type = type ? type : IH_TYPE_KERNEL; + if (itl->type == IH_TYPE_FLATDT) { + itl->fit_image_type = type ? type : IH_TYPE_KERNEL; /* For auto-FIT, datafile has to be provided with -d */ - if (!params.auto_fit) - params.datafile = datafile; - else if (!params.datafile) + if (!itl->auto_fit) + itl->datafile = datafile; + else if (!itl->datafile) return usage(itl, "Missing data file for auto-FIT (use -d)"); - } else if (params.lflag || type != IH_TYPE_INVALID) { - if (type == IH_TYPE_SCRIPT && !params.datafile) + } else if (itl->lflag || type != IH_TYPE_INVALID) { + if (type == IH_TYPE_SCRIPT && !itl->datafile) return usage(itl, "Missing data file for script (use -d)"); - params.type = type; + itl->type = type; } - if (!params.imagefile) + if (!itl->imagefile) return usage(itl, "Missing output filename"); return 0; -- 2.43.0

Pass this pointer in rather than using the global. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 6074449e399..e83172124ca 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -417,41 +417,41 @@ static int process_args(struct imgtool *itl, int argc, char **argv) return 0; } -static void verify_image(const struct imgtool_funcs *tparams) +static void verify_image(struct imgtool *itl, const struct imgtool_funcs *tparams) { struct stat sbuf; void *ptr; int ifd; - ifd = open(params.imagefile, O_RDONLY | O_BINARY); + ifd = open(itl->imagefile, O_RDONLY | O_BINARY); if (ifd < 0) { fprintf(stderr, "%s: Can't open %s: %s\n", - params.cmdname, params.imagefile, + itl->cmdname, itl->imagefile, strerror(errno)); exit(EXIT_FAILURE); } if (fstat(ifd, &sbuf) < 0) { fprintf(stderr, "%s: Can't stat %s: %s\n", - params.cmdname, params.imagefile, strerror(errno)); + itl->cmdname, itl->imagefile, strerror(errno)); exit(EXIT_FAILURE); } - params.file_size = sbuf.st_size; + itl->file_size = sbuf.st_size; - ptr = mmap(0, params.file_size, PROT_READ, MAP_SHARED, ifd, 0); + ptr = mmap(0, itl->file_size, PROT_READ, MAP_SHARED, ifd, 0); if (ptr == MAP_FAILED) { fprintf(stderr, "%s: Can't map %s: %s\n", - params.cmdname, params.imagefile, strerror(errno)); + itl->cmdname, itl->imagefile, strerror(errno)); exit(EXIT_FAILURE); } - if (tparams->verify_header((unsigned char *)ptr, params.file_size, ¶ms) != 0) { + if (tparams->verify_header((unsigned char *)ptr, itl->file_size, itl) != 0) { fprintf(stderr, "%s: Failed to verify header of %s\n", - params.cmdname, params.imagefile); + itl->cmdname, itl->imagefile); exit(EXIT_FAILURE); } - (void)munmap(ptr, params.file_size); + (void)munmap(ptr, itl->file_size); (void)close(ifd); } @@ -944,7 +944,7 @@ static int run_mkimage(struct imgtool *itl) } if (tparams->verify_header) - verify_image(tparams); + verify_image(itl, tparams); return 0; } -- 2.43.0

Pass this pointer in rather than using the global. Update copy_data_file() too since it calls copy_file() Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 52 ++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index e83172124ca..f27f6a23276 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -455,7 +455,7 @@ static void verify_image(struct imgtool *itl, const struct imgtool_funcs *tparam (void)close(ifd); } -static void copy_file(int ifd, const char *datafile, int pad) +static void copy_file(struct imgtool *itl, int ifd, const char *datafile, int pad) { int dfd; struct stat sbuf; @@ -465,43 +465,43 @@ static void copy_file(int ifd, const char *datafile, int pad) uint8_t zeros[4096]; int offset = 0; int size, ret; - struct imgtool_funcs *tparams = imagetool_get_type(params.type); + struct imgtool_funcs *tparams = imagetool_get_type(itl->type); memset(zeros, 0, sizeof(zeros)); - if (params.vflag) + if (itl->vflag) fprintf(stderr, "Adding Image %s\n", datafile); dfd = open(datafile, O_RDONLY | O_BINARY); if (dfd < 0) { fprintf(stderr, "%s: Can't open %s: %s\n", - params.cmdname, datafile, strerror(errno)); + itl->cmdname, datafile, strerror(errno)); exit (EXIT_FAILURE); } if (fstat(dfd, &sbuf) < 0) { fprintf(stderr, "%s: Can't stat %s: %s\n", - params.cmdname, datafile, strerror(errno)); + itl->cmdname, datafile, strerror(errno)); exit (EXIT_FAILURE); } if (sbuf.st_size == 0) { fprintf(stderr, "%s: Input file %s is empty, bailing out\n", - params.cmdname, datafile); + itl->cmdname, datafile); exit (EXIT_FAILURE); } ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); if (ptr == MAP_FAILED) { fprintf(stderr, "%s: Can't read %s: %s\n", - params.cmdname, datafile, strerror(errno)); + itl->cmdname, datafile, strerror(errno)); exit (EXIT_FAILURE); } - if (params.xflag && - ((params.type > IH_TYPE_INVALID && params.type < IH_TYPE_FLATDT) || - params.type == IH_TYPE_KERNEL_NOLOAD || - params.type == IH_TYPE_FIRMWARE_IVT)) { + if (itl->xflag && + ((itl->type > IH_TYPE_INVALID && itl->type < IH_TYPE_FLATDT) || + itl->type == IH_TYPE_KERNEL_NOLOAD || + itl->type == IH_TYPE_FIRMWARE_IVT)) { unsigned char *p = NULL; /* * XIP: do not append the struct legacy_img_hdr at the @@ -512,7 +512,7 @@ static void copy_file(int ifd, const char *datafile, int pad) if ((unsigned int)sbuf.st_size < tparams->header_size) { fprintf(stderr, "%s: Bad size: \"%s\" is too small for XIP\n", - params.cmdname, datafile); + itl->cmdname, datafile); exit (EXIT_FAILURE); } @@ -520,7 +520,7 @@ static void copy_file(int ifd, const char *datafile, int pad) if (*p != 0xff) { fprintf(stderr, "%s: Bad file: \"%s\" has invalid buffer for XIP\n", - params.cmdname, datafile); + itl->cmdname, datafile); exit (EXIT_FAILURE); } } @@ -534,19 +534,19 @@ static void copy_file(int ifd, const char *datafile, int pad) if (ret != size) { if (ret < 0) fprintf(stderr, "%s: Write error on %s: %s\n", - params.cmdname, params.imagefile, strerror(errno)); + itl->cmdname, itl->imagefile, strerror(errno)); else if (ret < size) fprintf(stderr, "%s: Write only %d/%d bytes, " "probably no space left on the device\n", - params.cmdname, ret, size); + itl->cmdname, ret, size); exit (EXIT_FAILURE); } tail = size % 4; - if (pad == 1 && tail != 0) { + if (pad == 1 && tail) { if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) { fprintf(stderr, "%s: Write error on %s: %s\n", - params.cmdname, params.imagefile, + itl->cmdname, itl->imagefile, strerror(errno)); exit (EXIT_FAILURE); } @@ -558,7 +558,7 @@ static void copy_file(int ifd, const char *datafile, int pad) todo = pad; if (write(ifd, (char *)&zeros, todo) != todo) { fprintf(stderr, "%s: Write error on %s: %s\n", - params.cmdname, params.imagefile, + itl->cmdname, itl->imagefile, strerror(errno)); exit(EXIT_FAILURE); } @@ -570,7 +570,7 @@ static void copy_file(int ifd, const char *datafile, int pad) (void)close(dfd); } -void copy_datafile(int ifd, char *file) +void copy_datafile(struct imgtool *itl, int ifd, char *file) { if (!file) return; @@ -579,11 +579,11 @@ void copy_datafile(int ifd, char *file) if (sep) { *sep = '\0'; - copy_file(ifd, file, 1); + copy_file(itl, ifd, file, 1); *sep++ = ':'; file = sep; } else { - copy_file(ifd, file, 0); + copy_file(itl, ifd, file, 0); break; } } @@ -650,10 +650,10 @@ static int run_mkimage(struct imgtool *itl) } if (params.lflag || params.fflag) { - ifd = open (params.imagefile, O_RDONLY|O_BINARY); + ifd = open(params.imagefile, O_RDONLY | O_BINARY); } else { - ifd = open (params.imagefile, - O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); + ifd = open(params.imagefile, + O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); } if (ifd < 0) { @@ -812,7 +812,7 @@ static int run_mkimage(struct imgtool *itl) file = NULL; } } - copy_datafile(ifd, params.datafile); + copy_datafile(itl, ifd, params.datafile); } else if (params.type == IH_TYPE_PBLIMAGE) { /* PBL has special Image format, implements its' own */ pbl_load_uboot(ifd, ¶ms); @@ -846,7 +846,7 @@ static int run_mkimage(struct imgtool *itl) if (ret) return ret; } else { - copy_file(ifd, params.datafile, pad_len); + copy_file(itl, ifd, params.datafile, pad_len); } if (params.type == IH_TYPE_FIRMWARE_IVT) { /* Add alignment and IVT */ -- 2.43.0

Pass this pointer in rather than using the global. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 139 ++++++++++++++++++++++++------------------------ 1 file changed, 69 insertions(+), 70 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index f27f6a23276..6e4ff8463bc 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -608,10 +608,10 @@ static int run_mkimage(struct imgtool *itl) int dfd; /* set tparams as per input type_id */ - tparams = imagetool_get_type(params.type); - if (tparams == NULL && !params.lflag) { + tparams = imagetool_get_type(itl->type); + if (!tparams && !itl->lflag) { fprintf (stderr, "%s: unsupported type %s\n", - params.cmdname, genimg_get_type_name(params.type)); + itl->cmdname, genimg_get_type_name(itl->type)); return EXIT_FAILURE; } @@ -620,20 +620,20 @@ static int run_mkimage(struct imgtool *itl) * as per image type to be generated/listed */ if (tparams && tparams->check_params) - if (tparams->check_params (¶ms)) + if (tparams->check_params(itl)) return usage(itl, "Bad parameters for image type"); - if (!params.eflag) { - params.ep = params.addr; + if (!itl->eflag) { + itl->ep = itl->addr; /* If XIP, entry point must be after the U-Boot header */ - if (params.xflag && tparams) - params.ep += tparams->header_size; + if (itl->xflag && tparams) + itl->ep += tparams->header_size; } - if (params.fflag){ + if (itl->fflag) { if (!tparams) { fprintf(stderr, "%s: Missing FIT support\n", - params.cmdname); + itl->cmdname); return EXIT_FAILURE; } if (tparams->fflag_handle) @@ -643,34 +643,34 @@ static int run_mkimage(struct imgtool *itl) * * For ex. fit_handle_file for Fit file support */ - retval = tparams->fflag_handle(¶ms); + retval = tparams->fflag_handle(itl); if (retval != EXIT_SUCCESS) return usage(itl, "Bad parameters for FIT image type"); } - if (params.lflag || params.fflag) { - ifd = open(params.imagefile, O_RDONLY | O_BINARY); + if (itl->lflag || itl->fflag) { + ifd = open(itl->imagefile, O_RDONLY | O_BINARY); } else { - ifd = open(params.imagefile, + ifd = open(itl->imagefile, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); } if (ifd < 0) { fprintf (stderr, "%s: Can't open %s: %s\n", - params.cmdname, params.imagefile, + itl->cmdname, itl->imagefile, strerror(errno)); return EXIT_FAILURE; } - if (params.lflag || params.fflag) { + if (itl->lflag || itl->fflag) { uint64_t size; /* * list header information of existing image */ if (fstat(ifd, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", - params.cmdname, params.imagefile, + itl->cmdname, itl->imagefile, strerror(errno)); return EXIT_FAILURE; } @@ -683,19 +683,19 @@ static int run_mkimage(struct imgtool *itl) if (ioctl(ifd, BLKGETSIZE64, &size) < 0) { fprintf (stderr, "%s: failed to get size of block device \"%s\"\n", - params.cmdname, params.imagefile); + itl->cmdname, itl->imagefile); return EXIT_FAILURE; } #else fprintf (stderr, "%s: \"%s\" is block device, don't know how to get its size\n", - params.cmdname, params.imagefile); + itl->cmdname, itl->imagefile); return EXIT_FAILURE; #endif } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) { fprintf (stderr, "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n", - params.cmdname, params.imagefile, + itl->cmdname, itl->imagefile, (unsigned long long) sbuf.st_size, tparams->header_size); return EXIT_FAILURE; @@ -706,49 +706,48 @@ static int run_mkimage(struct imgtool *itl) ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0); if (ptr == MAP_FAILED) { fprintf (stderr, "%s: Can't read %s: %s\n", - params.cmdname, params.imagefile, + itl->cmdname, itl->imagefile, strerror(errno)); return EXIT_FAILURE; } /* * Verifies the header format based on the expected header for image - * type in tparams. If tparams is NULL simply check all image types + * type in titl-> If tparams is NULL simply check all image types * to find one that matches our header. */ - retval = imagetool_verify_print_header(ptr, &sbuf, tparams, ¶ms); + retval = imagetool_verify_print_header(ptr, &sbuf, tparams, itl); (void) munmap((void *)ptr, sbuf.st_size); (void) close (ifd); if (!retval) - summary_show(¶ms.summary, params.imagefile, - params.keydest); + summary_show(&itl->summary, itl->imagefile, itl->keydest); return retval; } - if (!params.skipcpy && params.type != IH_TYPE_MULTI && params.type != IH_TYPE_SCRIPT) { - if (!params.datafile) { + if (!itl->skipcpy && itl->type != IH_TYPE_MULTI && itl->type != IH_TYPE_SCRIPT) { + if (!itl->datafile) { fprintf(stderr, "%s: Option -d with image data file was not specified\n", - params.cmdname); + itl->cmdname); return EXIT_FAILURE; } - dfd = open(params.datafile, O_RDONLY | O_BINARY); + dfd = open(itl->datafile, O_RDONLY | O_BINARY); if (dfd < 0) { fprintf(stderr, "%s: Can't open %s: %s\n", - params.cmdname, params.datafile, + itl->cmdname, itl->datafile, strerror(errno)); return EXIT_FAILURE; } if (fstat(dfd, &sbuf) < 0) { fprintf(stderr, "%s: Can't stat %s: %s\n", - params.cmdname, params.datafile, + itl->cmdname, itl->datafile, strerror(errno)); return EXIT_FAILURE; } - params.file_size = sbuf.st_size + tparams->header_size; + itl->file_size = sbuf.st_size + tparams->header_size; close(dfd); } @@ -759,21 +758,21 @@ static int run_mkimage(struct imgtool *itl) * allocate memory for the header itself. */ if (tparams->vrec_header) - pad_len = tparams->vrec_header(¶ms, tparams); + pad_len = tparams->vrec_header(itl, tparams); else memset(tparams->hdr, 0, tparams->header_size); if (write(ifd, tparams->hdr, tparams->header_size) != tparams->header_size) { fprintf (stderr, "%s: Write error on %s: %s\n", - params.cmdname, params.imagefile, strerror(errno)); + itl->cmdname, itl->imagefile, strerror(errno)); return EXIT_FAILURE; } - if (!params.skipcpy) { - if (params.type == IH_TYPE_MULTI || - params.type == IH_TYPE_SCRIPT) { - char *file = params.datafile; + if (!itl->skipcpy) { + if (itl->type == IH_TYPE_MULTI || + itl->type == IH_TYPE_SCRIPT) { + char *file = itl->datafile; uint32_t size; for (;;) { @@ -786,7 +785,7 @@ static int run_mkimage(struct imgtool *itl) if (stat (file, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", - params.cmdname, file, strerror(errno)); + itl->cmdname, file, strerror(errno)); return EXIT_FAILURE; } size = cpu_to_uimage (sbuf.st_size); @@ -796,7 +795,7 @@ static int run_mkimage(struct imgtool *itl) if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { fprintf (stderr, "%s: Write error on %s: %s\n", - params.cmdname, params.imagefile, + itl->cmdname, itl->imagefile, strerror(errno)); return EXIT_FAILURE; } @@ -812,60 +811,60 @@ static int run_mkimage(struct imgtool *itl) file = NULL; } } - copy_datafile(itl, ifd, params.datafile); - } else if (params.type == IH_TYPE_PBLIMAGE) { + copy_datafile(itl, ifd, itl->datafile); + } else if (itl->type == IH_TYPE_PBLIMAGE) { /* PBL has special Image format, implements its' own */ - pbl_load_uboot(ifd, ¶ms); - } else if (params.type == IH_TYPE_ZYNQMPBIF) { + pbl_load_uboot(ifd, itl); + } else if (itl->type == IH_TYPE_ZYNQMPBIF) { /* Image file is meta, walk through actual targets */ int ret; - ret = zynqmpbif_copy_image(ifd, ¶ms); + ret = zynqmpbif_copy_image(ifd, itl); if (ret) return ret; - } else if (params.type == IH_TYPE_IMX8IMAGE) { + } else if (itl->type == IH_TYPE_IMX8IMAGE) { /* i.MX8/8X has special Image format */ int ret; - ret = imx8image_copy_image(ifd, ¶ms); + ret = imx8image_copy_image(ifd, itl); if (ret) return ret; - } else if (params.type == IH_TYPE_IMX8MIMAGE) { + } else if (itl->type == IH_TYPE_IMX8MIMAGE) { /* i.MX8M has special Image format */ int ret; - ret = imx8mimage_copy_image(ifd, ¶ms); + ret = imx8mimage_copy_image(ifd, itl); if (ret) return ret; - } else if ((params.type == IH_TYPE_RKSD) || - (params.type == IH_TYPE_RKSPI)) { + } else if ((itl->type == IH_TYPE_RKSD) || + (itl->type == IH_TYPE_RKSPI)) { /* Rockchip has special Image format */ int ret; - ret = rockchip_copy_image(ifd, ¶ms); + ret = rockchip_copy_image(ifd, itl); if (ret) return ret; } else { - copy_file(itl, ifd, params.datafile, pad_len); + copy_file(itl, ifd, itl->datafile, pad_len); } - if (params.type == IH_TYPE_FIRMWARE_IVT) { + if (itl->type == IH_TYPE_FIRMWARE_IVT) { /* Add alignment and IVT */ - uint32_t aligned_filesize = ALIGN(params.file_size, + uint32_t aligned_filesize = ALIGN(itl->file_size, 0x1000); flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 }, - params.addr, 0, 0, 0, params.addr + itl->addr, 0, 0, 0, itl->addr + aligned_filesize - tparams->header_size, - params.addr + aligned_filesize + itl->addr + aligned_filesize - tparams->header_size + 0x20, 0 }; - int i = params.file_size; + int i = itl->file_size; for (; i < aligned_filesize; i++) { if (write(ifd, (char *) &i, 1) != 1) { fprintf(stderr, "%s: Write error on %s: %s\n", - params.cmdname, - params.imagefile, + itl->cmdname, + itl->imagefile, strerror(errno)); return EXIT_FAILURE; } @@ -873,8 +872,8 @@ static int run_mkimage(struct imgtool *itl) if (write(ifd, &ivt_header, sizeof(flash_header_v2_t)) != sizeof(flash_header_v2_t)) { fprintf(stderr, "%s: Write error on %s: %s\n", - params.cmdname, - params.imagefile, + itl->cmdname, + itl->imagefile, strerror(errno)); return EXIT_FAILURE; } @@ -894,34 +893,34 @@ static int run_mkimage(struct imgtool *itl) if (fstat(ifd, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", - params.cmdname, params.imagefile, strerror(errno)); + itl->cmdname, itl->imagefile, strerror(errno)); return EXIT_FAILURE; } - params.file_size = sbuf.st_size; + itl->file_size = sbuf.st_size; map_len = sbuf.st_size; ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0); if (ptr == MAP_FAILED) { fprintf (stderr, "%s: Can't map %s: %s\n", - params.cmdname, params.imagefile, strerror(errno)); + itl->cmdname, itl->imagefile, strerror(errno)); return EXIT_FAILURE; } /* Setup the image header as per input image type*/ if (tparams->set_header) - tparams->set_header (ptr, &sbuf, ifd, ¶ms); + tparams->set_header(ptr, &sbuf, ifd, itl); else { fprintf (stderr, "%s: Can't set header for %s\n", - params.cmdname, tparams->name); + itl->cmdname, tparams->name); return EXIT_FAILURE; } /* Print the image information by processing image header */ if (tparams->print_header) - tparams->print_header (ptr, ¶ms); + tparams->print_header(ptr, itl); else { fprintf (stderr, "%s: Can't print header for %s\n", - params.cmdname, tparams->name); + itl->cmdname, tparams->name); } (void)munmap((void *)ptr, map_len); @@ -939,7 +938,7 @@ static int run_mkimage(struct imgtool *itl) if (close(ifd)) { fprintf (stderr, "%s: Write error on %s: %s\n", - params.cmdname, params.imagefile, strerror(errno)); + itl->cmdname, itl->imagefile, strerror(errno)); return EXIT_FAILURE; } -- 2.43.0

Use a local variable for this state information. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 6e4ff8463bc..426b1965e14 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -18,17 +18,6 @@ #include <sys/ioctl.h> #endif -/* parameters initialized by core will be used by the image type code */ -static struct imgtool params = { - .os = IH_OS_LINUX, - .arch = IH_ARCH_PPC, - .type = IH_TYPE_KERNEL, - .comp = IH_COMP_GZIP, - .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS, - .imagename = "", - .imagename2 = "", -}; - static enum ih_category cur_category; static int h_compare_category_name(const void *vtype1, const void *vtype2) @@ -950,15 +939,22 @@ static int run_mkimage(struct imgtool *itl) int main(int argc, char **argv) { + /* parameters initialized by core will be used by the image type code */ + struct imgtool itl = { + .cmdname = *argv, + .os = IH_OS_LINUX, + .arch = IH_ARCH_PPC, + .type = IH_TYPE_KERNEL, + .comp = IH_COMP_GZIP, + .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS, + .imagename = "", + .imagename2 = "", + }; int ret; - params.cmdname = *argv; - params.addr = 0; - params.ep = 0; - - ret = process_args(¶ms, argc, argv); + ret = process_args(&itl, argc, argv); if (!ret) - ret = run_mkimage(¶ms); + ret = run_mkimage(&itl); return ret; } -- 2.43.0

Rather than exiting in this function, return an exit code. The caller already handles this. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 426b1965e14..23dd281e9ec 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -207,7 +207,7 @@ static int process_args(struct imgtool *itl, int argc, char **argv) if (*ptr) { fprintf(stderr, "%s: invalid load address %s\n", itl->cmdname, optarg); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } break; case 'A': @@ -223,7 +223,7 @@ static int process_args(struct imgtool *itl, int argc, char **argv) fprintf(stderr, "%s: Out of memory adding content '%s'", itl->cmdname, optarg); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } break; case 'B': @@ -231,7 +231,7 @@ static int process_args(struct imgtool *itl, int argc, char **argv) if (*ptr) { fprintf(stderr, "%s: invalid block length %s\n", itl->cmdname, optarg); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } break; @@ -257,7 +257,7 @@ static int process_args(struct imgtool *itl, int argc, char **argv) if (*ptr) { fprintf(stderr, "%s: invalid entry point %s\n", itl->cmdname, optarg); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } itl->eflag = 1; break; @@ -318,7 +318,7 @@ static int process_args(struct imgtool *itl, int argc, char **argv) if (*ptr) { fprintf(stderr, "%s: invalid offset size %s\n", itl->cmdname, optarg); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } break; case 'q': -- 2.43.0

Rather than exiting in this function, return an exit code. The caller is then responsible for returning it, so update it. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 23dd281e9ec..31a4813bd79 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -406,7 +406,7 @@ static int process_args(struct imgtool *itl, int argc, char **argv) return 0; } -static void verify_image(struct imgtool *itl, const struct imgtool_funcs *tparams) +static int verify_image(struct imgtool *itl, const struct imgtool_funcs *tparams) { struct stat sbuf; void *ptr; @@ -417,13 +417,13 @@ static void verify_image(struct imgtool *itl, const struct imgtool_funcs *tparam fprintf(stderr, "%s: Can't open %s: %s\n", itl->cmdname, itl->imagefile, strerror(errno)); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } if (fstat(ifd, &sbuf) < 0) { fprintf(stderr, "%s: Can't stat %s: %s\n", itl->cmdname, itl->imagefile, strerror(errno)); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } itl->file_size = sbuf.st_size; @@ -431,17 +431,19 @@ static void verify_image(struct imgtool *itl, const struct imgtool_funcs *tparam if (ptr == MAP_FAILED) { fprintf(stderr, "%s: Can't map %s: %s\n", itl->cmdname, itl->imagefile, strerror(errno)); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } if (tparams->verify_header((unsigned char *)ptr, itl->file_size, itl) != 0) { fprintf(stderr, "%s: Failed to verify header of %s\n", itl->cmdname, itl->imagefile); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } (void)munmap(ptr, itl->file_size); (void)close(ifd); + + return 0; } static void copy_file(struct imgtool *itl, int ifd, const char *datafile, int pad) @@ -931,8 +933,8 @@ static int run_mkimage(struct imgtool *itl) return EXIT_FAILURE; } - if (tparams->verify_header) - verify_image(itl, tparams); + if (tparams->verify_header && verify_image(itl, tparams)) + return EXIT_FAILURE; return 0; } -- 2.43.0

Rather than exiting in copy_file() and copy_datafile(), return an exit code. The caller is then responsible for returning it, so update it. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 31a4813bd79..0e750ef87e9 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -446,7 +446,7 @@ static int verify_image(struct imgtool *itl, const struct imgtool_funcs *tparams return 0; } -static void copy_file(struct imgtool *itl, int ifd, const char *datafile, int pad) +static int copy_file(struct imgtool *itl, int ifd, const char *datafile, int pad) { int dfd; struct stat sbuf; @@ -467,26 +467,26 @@ static void copy_file(struct imgtool *itl, int ifd, const char *datafile, int pa if (dfd < 0) { fprintf(stderr, "%s: Can't open %s: %s\n", itl->cmdname, datafile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } if (fstat(dfd, &sbuf) < 0) { fprintf(stderr, "%s: Can't stat %s: %s\n", itl->cmdname, datafile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } if (sbuf.st_size == 0) { fprintf(stderr, "%s: Input file %s is empty, bailing out\n", itl->cmdname, datafile); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); if (ptr == MAP_FAILED) { fprintf(stderr, "%s: Can't read %s: %s\n", itl->cmdname, datafile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } if (itl->xflag && @@ -504,7 +504,7 @@ static void copy_file(struct imgtool *itl, int ifd, const char *datafile, int pa fprintf(stderr, "%s: Bad size: \"%s\" is too small for XIP\n", itl->cmdname, datafile); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } for (p = ptr; p < ptr + tparams->header_size; p++) { @@ -512,7 +512,7 @@ static void copy_file(struct imgtool *itl, int ifd, const char *datafile, int pa fprintf(stderr, "%s: Bad file: \"%s\" has invalid buffer for XIP\n", itl->cmdname, datafile); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } } @@ -530,7 +530,7 @@ static void copy_file(struct imgtool *itl, int ifd, const char *datafile, int pa fprintf(stderr, "%s: Write only %d/%d bytes, " "probably no space left on the device\n", itl->cmdname, ret, size); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } tail = size % 4; @@ -539,7 +539,7 @@ static void copy_file(struct imgtool *itl, int ifd, const char *datafile, int pa fprintf(stderr, "%s: Write error on %s: %s\n", itl->cmdname, itl->imagefile, strerror(errno)); - exit (EXIT_FAILURE); + return EXIT_FAILURE; } } else if (pad > 1) { while (pad > 0) { @@ -551,7 +551,7 @@ static void copy_file(struct imgtool *itl, int ifd, const char *datafile, int pa fprintf(stderr, "%s: Write error on %s: %s\n", itl->cmdname, itl->imagefile, strerror(errno)); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } pad -= todo; } @@ -559,25 +559,31 @@ static void copy_file(struct imgtool *itl, int ifd, const char *datafile, int pa (void)munmap((void *)ptr, sbuf.st_size); (void)close(dfd); + + return 0; } -void copy_datafile(struct imgtool *itl, int ifd, char *file) +static int copy_datafile(struct imgtool *itl, int ifd, char *file) { if (!file) - return; + return 0; for (;;) { char *sep = strchr(file, ':'); if (sep) { *sep = '\0'; - copy_file(itl, ifd, file, 1); + if (copy_file(itl, ifd, file, 1)) + return EXIT_FAILURE; *sep++ = ':'; file = sep; } else { - copy_file(itl, ifd, file, 0); + if (copy_file(itl, ifd, file, 0)) + return EXIT_FAILURE; break; } } + + return 0; } /** @@ -802,7 +808,8 @@ static int run_mkimage(struct imgtool *itl) file = NULL; } } - copy_datafile(itl, ifd, itl->datafile); + if (copy_datafile(itl, ifd, itl->datafile)) + return EXIT_FAILURE; } else if (itl->type == IH_TYPE_PBLIMAGE) { /* PBL has special Image format, implements its' own */ pbl_load_uboot(ifd, itl); @@ -836,7 +843,8 @@ static int run_mkimage(struct imgtool *itl) if (ret) return ret; } else { - copy_file(itl, ifd, itl->datafile, pad_len); + if (copy_file(itl, ifd, itl->datafile, pad_len)) + return EXIT_FAILURE; } if (itl->type == IH_TYPE_FIRMWARE_IVT) { /* Add alignment and IVT */ -- 2.43.0

The run_mkimage() function is long and complicated. Make a start by moving some initial checks out into their own check_params() function. Also move the FIT-processing into a new process_fit() function. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 93 +++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 37 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 0e750ef87e9..5edc2123b40 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -587,65 +587,84 @@ static int copy_datafile(struct imgtool *itl, int ifd, char *file) } /** - * run_mkimage() - Run the mkimage tool - * - * The program arguments are in params + * check_params() - Do some initial checks on the parameters * - * Return: 0 on success, or non-zero exit code + * @itl: Image-tool info + * @tfuncs: image-tool functions + * Return: 0 if OK, non-zero on error */ -static int run_mkimage(struct imgtool *itl) +static int check_params(struct imgtool *itl, struct imgtool_funcs *tfuncs) { - struct imgtool_funcs *tparams = NULL; - struct stat sbuf; - int pad_len = 0; - int retval = 0; - size_t map_len; - int ifd = -1; - char *ptr; - int dfd; - /* set tparams as per input type_id */ - tparams = imagetool_get_type(itl->type); - if (!tparams && !itl->lflag) { + tfuncs = imagetool_get_type(itl->type); + if (!tfuncs && !itl->lflag) { fprintf (stderr, "%s: unsupported type %s\n", itl->cmdname, genimg_get_type_name(itl->type)); - return EXIT_FAILURE; + return -EINVAL; } /* * check the passed arguments parameters meets the requirements * as per image type to be generated/listed */ - if (tparams && tparams->check_params) - if (tparams->check_params(itl)) + if (tfuncs && tfuncs->check_params) + if (tfuncs->check_params(itl)) return usage(itl, "Bad parameters for image type"); if (!itl->eflag) { itl->ep = itl->addr; /* If XIP, entry point must be after the U-Boot header */ - if (itl->xflag && tparams) - itl->ep += tparams->header_size; + if (itl->xflag && tfuncs) + itl->ep += tfuncs->header_size; } - if (itl->fflag) { - if (!tparams) { - fprintf(stderr, "%s: Missing FIT support\n", - itl->cmdname); - return EXIT_FAILURE; - } - if (tparams->fflag_handle) - /* - * in some cases, some additional processing needs - * to be done if fflag is defined - * - * For ex. fit_handle_file for Fit file support - */ - retval = tparams->fflag_handle(itl); + return 0; +} - if (retval != EXIT_SUCCESS) - return usage(itl, "Bad parameters for FIT image type"); +static int process_fit(struct imgtool *itl, struct imgtool_funcs *tfuncs) +{ + if (!tfuncs) { + fprintf(stderr, "%s: Missing FIT support\n", + itl->cmdname); + return -ENOTSUP; } + /* + * in some cases, some additional processing needs to be done if fflag + * is defined + * + * E.g. fit_handle_file for Fit file support + */ + if (tfuncs->fflag_handle && tfuncs->fflag_handle(itl)) + return usage(itl, "Bad parameters for FIT image type"); + + return 0; +} + +/** + * run_mkimage() - Run the mkimage tool + * + * The program arguments are in params + * + * Return: 0 on success, or non-zero exit code + */ +static int run_mkimage(struct imgtool *itl) +{ + struct imgtool_funcs *tparams = NULL; + struct stat sbuf; + int pad_len = 0; + int retval = 0; + size_t map_len; + int ifd = -1; + char *ptr; + int dfd; + + tparams = imagetool_get_type(itl->type); + if (check_params(itl, tparams)) + return EXIT_FAILURE; + if (itl->fflag && process_fit(itl, tparams)) + return EXIT_FAILURE; + if (itl->lflag || itl->fflag) { ifd = open(itl->imagefile, O_RDONLY | O_BINARY); } else { -- 2.43.0

Add a new open_image() function to handle the initial opening of the output file. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 5edc2123b40..d1dcc5a6930 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -641,6 +641,33 @@ static int process_fit(struct imgtool *itl, struct imgtool_funcs *tfuncs) return 0; } +/** + * open_image() - Open the image file to create/update + * + * @itl: Image-tool info + * + * Return: file handle if OK, or -ve on error + */ +static int open_image(struct imgtool *itl) +{ + int ifd; + + if (itl->lflag || itl->fflag) { + ifd = open(itl->imagefile, O_RDONLY | O_BINARY); + } else { + ifd = open(itl->imagefile, + O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); + } + + if (ifd < 0) { + fprintf(stderr, "%s: Can't open %s: %s\n", + itl->cmdname, itl->imagefile, strerror(errno)); + return -ENOENT; + } + + return ifd; +} + /** * run_mkimage() - Run the mkimage tool * @@ -665,19 +692,9 @@ static int run_mkimage(struct imgtool *itl) if (itl->fflag && process_fit(itl, tparams)) return EXIT_FAILURE; - if (itl->lflag || itl->fflag) { - ifd = open(itl->imagefile, O_RDONLY | O_BINARY); - } else { - ifd = open(itl->imagefile, - O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); - } - - if (ifd < 0) { - fprintf (stderr, "%s: Can't open %s: %s\n", - itl->cmdname, itl->imagefile, - strerror(errno)); + ifd = open_image(itl); + if (ifd < 0) return EXIT_FAILURE; - } if (itl->lflag || itl->fflag) { uint64_t size; -- 2.43.0

For listing an image, or processing an existing FIT, an initial code path is used, then the program exits. Move this into its own function so it is clear that this is an early-return path. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 139 ++++++++++++++++++++++++++---------------------- 1 file changed, 76 insertions(+), 63 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index d1dcc5a6930..684e895938b 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -668,6 +668,80 @@ static int open_image(struct imgtool *itl) return ifd; } +/** + * list_or_process() - List the image, or do processing on it + * + * @itl: Image-tool info + * @tfuncs: image-tool functions + * @ifd: File handle of file to process + * + * Return: file handle if OK, or -ve on error + */ +static int list_or_process(struct imgtool *itl, struct imgtool_funcs *tfuncs, + int ifd) +{ + struct stat sbuf; + uint64_t size; + void *ptr; + int ret; + + if (fstat(ifd, &sbuf) < 0) { + fprintf(stderr, "%s: Can't stat %s: %s\n", itl->cmdname, + itl->imagefile, strerror(errno)); + return EXIT_FAILURE; + } + + if ((sbuf.st_mode & S_IFMT) == S_IFBLK) { +#ifdef __linux__ +#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64) +#define BLKGETSIZE64 _IOR(0x12, 114, size_t) /* return device size in bytes (u64 *arg) */ +#endif + if (ioctl(ifd, BLKGETSIZE64, &size) < 0) { + fprintf(stderr, + "%s: failed to get size of block device \"%s\"\n", + itl->cmdname, itl->imagefile); + return EXIT_FAILURE; + } +#else + fprintf(stderr, + "%s: \"%s\" is block device, don't know how to get its size\n", + itl->cmdname, itl->imagefile); + return EXIT_FAILURE; +#endif + } else if (tfuncs && sbuf.st_size < (off_t)tfuncs->header_size) { + fprintf(stderr, + "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n", + itl->cmdname, itl->imagefile, + (unsigned long long)sbuf.st_size, + tfuncs->header_size); + return EXIT_FAILURE; + size = sbuf.st_size; + } else { + size = sbuf.st_size; + } + + ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0); + if (ptr == MAP_FAILED) { + fprintf(stderr, "%s: Can't read %s: %s\n", itl->cmdname, + itl->imagefile, strerror(errno)); + return EXIT_FAILURE; + } + + /* + * Verify the header format based on the expected header for image + * type in titl-> If tfuncs is NULL simply check all image types + * to find one that matches our header. + */ + ret = imagetool_verify_print_header(ptr, &sbuf, tfuncs, itl); + + (void)munmap((void *)ptr, sbuf.st_size); + (void)close(ifd); + if (!ret) + summary_show(&itl->summary, itl->imagefile, itl->keydest); + + return ret; +} + /** * run_mkimage() - Run the mkimage tool * @@ -680,7 +754,6 @@ static int run_mkimage(struct imgtool *itl) struct imgtool_funcs *tparams = NULL; struct stat sbuf; int pad_len = 0; - int retval = 0; size_t map_len; int ifd = -1; char *ptr; @@ -696,68 +769,8 @@ static int run_mkimage(struct imgtool *itl) if (ifd < 0) return EXIT_FAILURE; - if (itl->lflag || itl->fflag) { - uint64_t size; - /* - * list header information of existing image - */ - if (fstat(ifd, &sbuf) < 0) { - fprintf (stderr, "%s: Can't stat %s: %s\n", - itl->cmdname, itl->imagefile, - strerror(errno)); - return EXIT_FAILURE; - } - - if ((sbuf.st_mode & S_IFMT) == S_IFBLK) { -#ifdef __linux__ -#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64) -#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */ -#endif - if (ioctl(ifd, BLKGETSIZE64, &size) < 0) { - fprintf (stderr, - "%s: failed to get size of block device \"%s\"\n", - itl->cmdname, itl->imagefile); - return EXIT_FAILURE; - } -#else - fprintf (stderr, - "%s: \"%s\" is block device, don't know how to get its size\n", - itl->cmdname, itl->imagefile); - return EXIT_FAILURE; -#endif - } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) { - fprintf (stderr, - "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n", - itl->cmdname, itl->imagefile, - (unsigned long long) sbuf.st_size, - tparams->header_size); - return EXIT_FAILURE; - } else { - size = sbuf.st_size; - } - - ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0); - if (ptr == MAP_FAILED) { - fprintf (stderr, "%s: Can't read %s: %s\n", - itl->cmdname, itl->imagefile, - strerror(errno)); - return EXIT_FAILURE; - } - - /* - * Verifies the header format based on the expected header for image - * type in titl-> If tparams is NULL simply check all image types - * to find one that matches our header. - */ - retval = imagetool_verify_print_header(ptr, &sbuf, tparams, itl); - - (void) munmap((void *)ptr, sbuf.st_size); - (void) close (ifd); - if (!retval) - summary_show(&itl->summary, itl->imagefile, itl->keydest); - - return retval; - } + if (itl->lflag || itl->fflag) + return list_or_process(itl, tparams, ifd); if (!itl->skipcpy && itl->type != IH_TYPE_MULTI && itl->type != IH_TYPE_SCRIPT) { if (!itl->datafile) { -- 2.43.0

This is not correct when building a kernel FIT, since it adds a second loadable in addition to the kernel. There may in fact be a bug in SPL FIT, in which case that should be fixed, rather than adding an invalid loadable to the FIT. This reverts commit cabde449b97799d4377f62b90374f261327a3a90. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/fit_image.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/fit_image.c b/tools/fit_image.c index aa71544f4cb..e2840cf48b4 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -381,7 +381,6 @@ static void fit_write_configs(struct imgtool *itl, char *fdt) typename = genimg_get_type_short_name(itl->fit_image_type); snprintf(str, sizeof(str), "%s-1", typename); fdt_property_string(fdt, typename, str); - fdt_property_string(fdt, FIT_LOADABLE_PROP, str); if (itl->fit_ramdisk) fdt_property_string(fdt, FIT_RAMDISK_PROP, -- 2.43.0
participants (1)
-
Simon Glass