Re: [PATCH] mkimage: Call verify_header after writing image to disk

Simon Glass
2022-01-13T19:30:21Z

Hi Pali,

On Thu, 13 Jan 2022 at 07:42, Pali Rohár <pali@kernel.org> wrote:
>
> If image backend provides verify_header callback then call it after writing
> image to disk. This ensures that written image is correct.
>
> Signed-off-by: Pali Rohár <pali@kernel.org>
> ---
> tools/mkimage.c | 34 ++++++++++++++++++++++++++++++++++
> 1 file changed, 34 insertions(+)
>
> diff --git a/tools/mkimage.c b/tools/mkimage.c
> index fbe883ce3620..2d282630787c 100644
> --- a/tools/mkimage.c
> +++ b/tools/mkimage.c
> @@ -698,6 +698,40 @@ int main(int argc, char **argv)
> exit (EXIT_FAILURE);
> }
>
> + if (tparams->verify_header) {
> + ifd = open(params.imagefile, O_RDONLY | O_BINARY);
> + if (ifd < 0) {
> + fprintf(stderr, "%s: Can't open %s: %s\n",
> + params.cmdname, params.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));
> + exit(EXIT_FAILURE);
> + }
> + params.file_size = sbuf.st_size;
> +
> + map_len = sbuf.st_size;
> + ptr = mmap(0, sbuf.st_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));
> + exit(EXIT_FAILURE);
> + }
> +
> + if (tparams->verify_header((unsigned char *)ptr, sbuf.st_size, &params) != 0) {
> + fprintf(stderr, "%s: Failed to verify header of %s\n",
> + params.cmdname, params.imagefile);
> + exit(EXIT_FAILURE);
> + }
> +
> + (void)munmap((void *)ptr, map_len);
> + close(ifd);
> + }
> +
> exit (EXIT_SUCCESS);
> }

main() is so long already. Could this move into a function?

Regards,
Simon