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

Tom Rini
2022-03-08T13:42:22Z

[-- Attachment #1: Type: text/plain, Size: 2322 bytes --]

On Fri, Jan 14, 2022 at 06:34:43PM +0100, Pali Rohár 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>
> Reviewed-by: Stefan Roese <sr@denx.de>
> Reviewed-by: Simon Glass <sjg@chromium.org>
> ---
> tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 41 insertions(+)
>
> diff --git a/tools/mkimage.c b/tools/mkimage.c
> index fbe883ce3620..d5ad0925225c 100644
> --- a/tools/mkimage.c
> +++ b/tools/mkimage.c
> @@ -336,6 +336,44 @@ static void process_args(int argc, char **argv)
> usage("Missing output filename");
> }
>
> +static void verify_image(const struct image_type_params *tparams)
> +{
> + struct stat sbuf;
> + void *ptr;
> + int ifd;
> +
> + 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;
> +
> + ptr = mmap(0, params.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));
> + exit(EXIT_FAILURE);
> + }
> +
> + if (tparams->verify_header((unsigned char *)ptr, params.file_size, &params) != 0) {
> + fprintf(stderr, "%s: Failed to verify header of %s\n",
> + params.cmdname, params.imagefile);
> + exit(EXIT_FAILURE);
> + }
> +
> + (void)munmap(ptr, params.file_size);
> + (void)close(ifd);
> +}
> +
> int main(int argc, char **argv)
> {
> int ifd = -1;
> @@ -698,6 +736,9 @@ int main(int argc, char **argv)
> exit (EXIT_FAILURE);
> }
>
> + if (tparams->verify_header)
> + verify_image(tparams);
> +
> exit (EXIT_SUCCESS);
> }

I've added Joseph Chen to the thread as with this patch applied,
evb-rk3568 fails to build now with:
./tools/mkimage: Failed to verify header of idbloader.img
which is another issue to resolve.

--
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]