Skip to contentSkip to conversation

mkimage: Call verify_header after writing image to disk

PATCH Superseded Pali Rohár 7 participants 21 messages Jan 13, 2022 – Apr 6, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mkimage: Call verify_header after writing image to disk

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
tools/mkimage.c +34 -0
index fbe883ce3620..2d282630787c 100644
@@ -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);
 }
 
Signature · 1 line
2.20.1
#1 lore raw

Hi Pali,

Quoted 43 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
tools/mkimage.c +26 -0
index fbe883ce3620..2d282630787c 100644
@@ -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",

tools/mkimage.c int main(int argc, char **argv) · line 736

Quoted 10 lines
tools/mkimage.c +8 -0
+                               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

#2 lore raw

PATCH v2 1/1 tools: mkimage: Call verify_header after writing image to disk

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
tools/mkimage.c +41 -0
index fbe883ce3620..d5ad0925225c 100644
@@ -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);
 }
 
Signature · 1 line
2.20.1
Reviewed-by on v2 1/1
#3 lore raw

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

Quoted 4 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

Thanks,
Stefan

Quoted 55 lines
---
  tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
  1 file changed, 41 insertions(+)
tools/mkimage.c +38 -0
index fbe883ce3620..d5ad0925225c 100644
@@ -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;

tools/mkimage.c int main(int argc, char **argv) · line 744

Quoted 9 lines
tools/mkimage.c +3 -0
@@ -698,6 +736,9 @@ int main(int argc, char **argv)
  		exit (EXIT_FAILURE);
  	}
 
+	if (tparams->verify_header)
+		verify_image(tparams);
+
  	exit (EXIT_SUCCESS);
  }
 

Viele Grüße,
Stefan Roese

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Reviewed-by on v2 1/1
#4 lore raw

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

In reply to Pali Rohár
Quoted 9 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

Missing change log, BTW.

#5 lore raw

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

In reply to Pali Rohár
Quoted 9 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Signature · 1 line
Tom
#6 lore raw

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

Quoted 12 lines · On Friday 21 January 2022 16:21:33 Tom Rini wrote:
Quoted 9 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
Quoted 2 lines

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Maybe they were already broken and this patch just detected it? Or verify_header callback for particular image type is reject valid image?

Do you have some pointers to failed build logs?

#7 lore raw

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

Quoted 13 lines · Pali Rohár
Quoted 12 lines · On Friday 21 January 2022 16:21:33 Tom Rini wrote:
Quoted 9 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
Quoted 8 lines
Quoted 2 lines

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Maybe they were already broken and this patch just detected it? Or verify_header callback for particular image type is reject valid image?

Do you have some pointers to failed build logs?

Try building for ls1021atwr_sdcard_qspi with your patch applied, the only new thing that's shown in the logs is the error message.

Signature · 1 line
Tom
#8 lore raw

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

Quoted 17 lines · On Friday 21 January 2022 21:15:43 Tom Rini wrote:
Quoted 16 lines · Pali Rohár
Quoted 14 lines · On Friday 21 January 2022 16:21:33 Tom Rini wrote:
Quoted 9 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Quoted 8 lines
Quoted 5 lines

Maybe they were already broken and this patch just detected it? Or verify_header callback for particular image type is reject valid image?

Do you have some pointers to failed build logs?

Try building for ls1021atwr_sdcard_qspi with your patch applied, the only new thing that's shown in the logs is the error message.

So... I have tried following without this patch:

$ make ls1021atwr_sdcard_qspi_defconfig
$ make CROSS_COMPILE=arm-linux-gnueabi- -j8

It generated file spl/u-boot-spl.pbl without error. Now I called -l on this generated file for type pblimage and I got following output:

$ ./tools/dumpimage -T pblimage -l spl/u-boot-spl.pbl
GP Header: Size aa55aa55 LoadAddr 1ee0100
$ ./tools/mkimage -T pblimage -l spl/u-boot-spl.pbl
GP Header: Size aa55aa55 LoadAddr 1ee0100

"GP Header:" line is from the TI OMAP image backend type gpimage or type omapimage (implemented in file gpimage-common.c).

So it means that files generated by ls1021atwr_sdcard_qspi are already broken and my patch just detected it. Or it is also possible that validation code in pblimage.c file is incorrect and broken.

What to do with it now?

#9 lore raw

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

Quoted 41 lines · On Saturday 22 January 2022 17:31:18 Pali Rohár wrote:
Quoted 17 lines · On Friday 21 January 2022 21:15:43 Tom Rini wrote:
Quoted 16 lines · Pali Rohár
Quoted 14 lines · On Friday 21 January 2022 16:21:33 Tom Rini wrote:
Quoted 9 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Quoted 8 lines
Quoted 5 lines

Maybe they were already broken and this patch just detected it? Or verify_header callback for particular image type is reject valid image?

Do you have some pointers to failed build logs?

Try building for ls1021atwr_sdcard_qspi with your patch applied, the only new thing that's shown in the logs is the error message.

So... I have tried following without this patch:

$ make ls1021atwr_sdcard_qspi_defconfig
$ make CROSS_COMPILE=arm-linux-gnueabi- -j8

It generated file spl/u-boot-spl.pbl without error. Now I called -l on this generated file for type pblimage and I got following output:

$ ./tools/dumpimage -T pblimage -l spl/u-boot-spl.pbl
GP Header: Size aa55aa55 LoadAddr 1ee0100
$ ./tools/mkimage -T pblimage -l spl/u-boot-spl.pbl
GP Header: Size aa55aa55 LoadAddr 1ee0100
Quoted 6 lines

"GP Header:" line is from the TI OMAP image backend type gpimage or type omapimage (implemented in file gpimage-common.c).

So it means that files generated by ls1021atwr_sdcard_qspi are already broken and my patch just detected it. Or it is also possible that validation code in pblimage.c file is incorrect and broken.

Just to note validation is failing on this tools/pblimage.c check:

if (pbl_hdr->rcwheader != reverse_byte(RCW_HEADER))

Quoted 1 line

What to do with it now?

#10 lore raw

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

In reply to Pali Rohár
Quoted 41 lines · Pali Rohár
Quoted 17 lines · On Friday 21 January 2022 21:15:43 Tom Rini wrote:
Quoted 16 lines · Pali Rohár
Quoted 14 lines · On Friday 21 January 2022 16:21:33 Tom Rini wrote:
Quoted 9 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Quoted 8 lines
Quoted 5 lines

Maybe they were already broken and this patch just detected it? Or verify_header callback for particular image type is reject valid image?

Do you have some pointers to failed build logs?

Try building for ls1021atwr_sdcard_qspi with your patch applied, the only new thing that's shown in the logs is the error message.

So... I have tried following without this patch:

$ make ls1021atwr_sdcard_qspi_defconfig
$ make CROSS_COMPILE=arm-linux-gnueabi- -j8

It generated file spl/u-boot-spl.pbl without error. Now I called -l on this generated file for type pblimage and I got following output:

$ ./tools/dumpimage -T pblimage -l spl/u-boot-spl.pbl
GP Header: Size aa55aa55 LoadAddr 1ee0100
$ ./tools/mkimage -T pblimage -l spl/u-boot-spl.pbl
GP Header: Size aa55aa55 LoadAddr 1ee0100
Quoted 8 lines

"GP Header:" line is from the TI OMAP image backend type gpimage or type omapimage (implemented in file gpimage-common.c).

So it means that files generated by ls1021atwr_sdcard_qspi are already broken and my patch just detected it. Or it is also possible that validation code in pblimage.c file is incorrect and broken.

What to do with it now?

Thanks for digging. This is a problem for a number of the ls1021, ls1043 and ls1046 platforms, so lets add some maintainers there.

Signature · 1 line
Tom
#11 lore raw

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

Quoted 65 lines

-----Original Message-----
From: Tom Rini <[email protected]>
Sent: Saturday, January 22, 2022 10:05 PM
To: Pali Rohár <[email protected]>; Alison Wang <[email protected]>; Priyanka Jain <[email protected]>; Mingkai Hu <[email protected]>; Rajesh Bhagat <[email protected]>
Cc: Simon Glass <[email protected]>; Alexandru Gagniuc <[email protected]>; Yann Dirson <[email protected]>; Stefan Roese <[email protected]>; Marek Behún <[email protected]>; u- [email protected]
Subject: Re: [PATCH v2] tools: mkimage: Call verify_header after writing image to disk

Quoted 49 lines · Pali Rohár
Quoted 18 lines · On Friday 21 January 2022 21:15:43 Tom Rini wrote:
Quoted 17 lines · Pali Rohár
Quoted 15 lines · On Friday 21 January 2022 16:21:33 Tom Rini wrote:
Quoted 10 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41
+++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Quoted 8 lines
Quoted 5 lines

Maybe they were already broken and this patch just detected it? Or verify_header callback for particular image type is reject valid image?

Do you have some pointers to failed build logs?

Try building for ls1021atwr_sdcard_qspi with your patch applied, the only new thing that's shown in the logs is the error message.

So... I have tried following without this patch:

$ make ls1021atwr_sdcard_qspi_defconfig $ make
CROSS_COMPILE=arm-linux-gnueabi- -j8

It generated file spl/u-boot-spl.pbl without error. Now I called -l on this generated file for type pblimage and I got following output:

$ ./tools/dumpimage -T pblimage -l spl/u-boot-spl.pbl GP Header: Size
aa55aa55 LoadAddr 1ee0100
$ ./tools/mkimage -T pblimage -l spl/u-boot-spl.pbl GP Header: Size
aa55aa55 LoadAddr 1ee0100

"GP Header:" line is from the TI OMAP image backend type gpimage or type omapimage (implemented in file gpimage-common.c).

So it means that files generated by ls1021atwr_sdcard_qspi are already broken and my patch just detected it. Or it is also possible that validation code in pblimage.c file is incorrect and broken.

Quoted 7 lines
Quoted 1 line

What to do with it now?

Thanks for digging. This is a problem for a number of the ls1021, ls1043 and ls1046 platforms, so lets add some maintainers there.

--
Tom

I will ask NXP-platform owners to check on this.

Thanks
Priyanka

#12 lore raw

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

Quoted 68 lines · On Wednesday 02 February 2022 09:06:30 Priyanka Jain wrote:
Quoted 68 lines

-----Original Message-----
From: Tom Rini <[email protected]>
Sent: Saturday, January 22, 2022 10:05 PM
To: Pali Rohár <[email protected]>; Alison Wang <[email protected]>; Priyanka Jain <[email protected]>; Mingkai Hu <[email protected]>; Rajesh Bhagat <[email protected]>
Cc: Simon Glass <[email protected]>; Alexandru Gagniuc <[email protected]>; Yann Dirson <[email protected]>; Stefan Roese <[email protected]>; Marek Behún <[email protected]>; u- [email protected]
Subject: Re: [PATCH v2] tools: mkimage: Call verify_header after writing image to disk

Quoted 42 lines · Pali Rohár
Quoted 18 lines · On Friday 21 January 2022 21:15:43 Tom Rini wrote:
Quoted 17 lines · Pali Rohár
Quoted 15 lines · On Friday 21 January 2022 16:21:33 Tom Rini wrote:
Quoted 10 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41
+++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Quoted 8 lines
Quoted 5 lines

Maybe they were already broken and this patch just detected it? Or verify_header callback for particular image type is reject valid image?

Do you have some pointers to failed build logs?

Try building for ls1021atwr_sdcard_qspi with your patch applied, the only new thing that's shown in the logs is the error message.

So... I have tried following without this patch:

$ make ls1021atwr_sdcard_qspi_defconfig $ make
CROSS_COMPILE=arm-linux-gnueabi- -j8

It generated file spl/u-boot-spl.pbl without error. Now I called -l on this generated file for type pblimage and I got following output:

$ ./tools/dumpimage -T pblimage -l spl/u-boot-spl.pbl GP Header: Size
aa55aa55 LoadAddr 1ee0100
$ ./tools/mkimage -T pblimage -l spl/u-boot-spl.pbl GP Header: Size
aa55aa55 LoadAddr 1ee0100
Quoted 8 lines

"GP Header:" line is from the TI OMAP image backend type gpimage or type omapimage (implemented in file gpimage-common.c).

So it means that files generated by ls1021atwr_sdcard_qspi are already broken and my patch just detected it. Or it is also possible that validation code in pblimage.c file is incorrect and broken.

What to do with it now?

Thanks for digging. This is a problem for a number of the ls1021, ls1043 and ls1046 platforms, so lets add some maintainers there.

Quoted 8 lines
Quoted 2 lines

--
Tom

I will ask NXP-platform owners to check on this.

Thanks
Priyanka

Hello! Any news on this?

#13 lore raw

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

Quoted 90 lines

-----Original Message-----
From: Pali Rohár <[email protected]>
Sent: Wednesday, February 16, 2022 1:22 AM
To: Priyanka Jain <[email protected]>
Cc: Tom Rini <[email protected]>; Alison Wang <[email protected]>; Mingkai Hu <[email protected]>; Rajesh Bhagat <[email protected]>; Jiafei Pan <[email protected]>; Simon Glass <[email protected]>; Alexandru Gagniuc <[email protected]>; Yann Dirson <[email protected]>; Stefan Roese <[email protected]>; Marek Behún <[email protected]>; u- [email protected]
Subject: Re: [PATCH v2] tools: mkimage: Call verify_header after writing image to disk

Quoted 13 lines · On Wednesday 02 February 2022 09:06:30 Priyanka Jain wrote:
Quoted 13 lines

-----Original Message-----
From: Tom Rini <[email protected]>
Sent: Saturday, January 22, 2022 10:05 PM
To: Pali Rohár <[email protected]>; Alison Wang <[email protected]>; Priyanka Jain <[email protected]>; Mingkai Hu <[email protected]>; Rajesh Bhagat <[email protected]>
Cc: Simon Glass <[email protected]>; Alexandru Gagniuc <[email protected]>; Yann Dirson <[email protected]>; Stefan Roese <[email protected]>; Marek Behún <[email protected]>; u- [email protected]
Subject: Re: [PATCH v2] tools: mkimage: Call verify_header after writing image to disk

Quoted 8 lines
Quoted 8 lines · Pali Rohár
Quoted 7 lines · On Friday 21 January 2022 21:15:43 Tom Rini wrote:
Quoted 6 lines · Pali Rohár
Quoted 5 lines · On Friday 21 January 2022 16:21:33 Tom Rini wrote:
Quoted 4 lines · Pali Rohár
Quoted 3 lines

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is

correct.

Quoted 52 lines
Quoted 50 lines
Quoted 36 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines
---
 tools/mkimage.c | 41
+++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Quoted 8 lines
Quoted 5 lines

Maybe they were already broken and this patch just detected it? Or verify_header callback for particular image type is reject valid image?

Do you have some pointers to failed build logs?

Try building for ls1021atwr_sdcard_qspi with your patch applied, the only new thing that's shown in the logs is the error message.

So... I have tried following without this patch:

$ make ls1021atwr_sdcard_qspi_defconfig $ make
CROSS_COMPILE=arm-linux-gnueabi- -j8

It generated file spl/u-boot-spl.pbl without error. Now I called -l on this generated file for type pblimage and I got following output:

$ ./tools/dumpimage -T pblimage -l spl/u-boot-spl.pbl GP Header:
Size
aa55aa55 LoadAddr 1ee0100
$ ./tools/mkimage -T pblimage -l spl/u-boot-spl.pbl GP Header: Size
aa55aa55 LoadAddr 1ee0100
Quoted 8 lines

"GP Header:" line is from the TI OMAP image backend type gpimage or type omapimage (implemented in file gpimage-common.c).

So it means that files generated by ls1021atwr_sdcard_qspi are already broken and my patch just detected it. Or it is also possible that validation code in pblimage.c file is incorrect and broken.

What to do with it now?

Thanks for digging. This is a problem for a number of the ls1021, ls1043 and ls1046 platforms, so lets add some maintainers there.

--
Tom

Quoted 6 lines
Quoted 4 lines

I will ask NXP-platform owners to check on this.

Thanks
Priyanka

Hello! Any news on this?

Not yet, Actually team was busy in some other critical work. I will try my best to get response as soon as possible.

Thanks
Priyanka

#14 lore raw

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

In reply to Pali Rohár
Quoted 91 lines

-----Original Message-----
From: Pali Rohár <[email protected]>
Sent: 2022年2月16日 3:52
To: Priyanka Jain <[email protected]>
Cc: Tom Rini <[email protected]>; Alison Wang <[email protected]>; Mingkai Hu <[email protected]>; Rajesh Bhagat <[email protected]>; Jiafei Pan <[email protected]>; Simon Glass <[email protected]>; Alexandru Gagniuc <[email protected]>; Yann Dirson <[email protected]>; Stefan Roese <[email protected]>; Marek Behún <[email protected]>; [email protected]
Subject: Re: [PATCH v2] tools: mkimage: Call verify_header after writing image to disk

Quoted 8 lines · On Wednesday 02 February 2022 09:06:30 Priyanka Jain wrote:
Quoted 8 lines

-----Original Message-----
From: Tom Rini <[email protected]>
Sent: Saturday, January 22, 2022 10:05 PM
To: Pali Rohár <[email protected]>; Alison Wang <[email protected]>; Priyanka Jain <[email protected]>; Mingkai Hu <[email protected]>; Rajesh Bhagat <[email protected]>
Cc: Simon Glass <[email protected]>; Alexandru Gagniuc <[email protected]>; Yann Dirson <[email protected]>;

Stefan

Quoted 5 lines
Quoted 5 lines

Roese <[email protected]>; Marek Behún <[email protected]>; u- [email protected]
Subject: Re: [PATCH v2] tools: mkimage: Call verify_header after writing image to disk

Quoted 8 lines
Quoted 8 lines · Pali Rohár
Quoted 7 lines · On Friday 21 January 2022 21:15:43 Tom Rini wrote:
Quoted 6 lines · Pali Rohár
Quoted 5 lines · On Friday 21 January 2022 16:21:33 Tom Rini wrote:
Quoted 4 lines · Pali Rohár
Quoted 3 lines

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is

correct.

Quoted 52 lines
Quoted 50 lines
Quoted 36 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines
---
 tools/mkimage.c | 41
+++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Quoted 8 lines
Quoted 5 lines

Maybe they were already broken and this patch just detected it? Or verify_header callback for particular image type is reject valid image?

Do you have some pointers to failed build logs?

Try building for ls1021atwr_sdcard_qspi with your patch applied, the only new thing that's shown in the logs is the error message.

So... I have tried following without this patch:

$ make ls1021atwr_sdcard_qspi_defconfig $ make
CROSS_COMPILE=arm-linux-gnueabi- -j8

It generated file spl/u-boot-spl.pbl without error. Now I called -l on this generated file for type pblimage and I got following output:

$ ./tools/dumpimage -T pblimage -l spl/u-boot-spl.pbl GP Header:
Size
aa55aa55 LoadAddr 1ee0100
$ ./tools/mkimage -T pblimage -l spl/u-boot-spl.pbl GP Header: Size
aa55aa55 LoadAddr 1ee0100
Quoted 8 lines

"GP Header:" line is from the TI OMAP image backend type gpimage or type omapimage (implemented in file gpimage-common.c).

So it means that files generated by ls1021atwr_sdcard_qspi are already broken and my patch just detected it. Or it is also possible that validation code in pblimage.c file is incorrect and broken.

What to do with it now?

Thanks for digging. This is a problem for a number of the ls1021, ls1043 and ls1046 platforms, so lets add some maintainers there.

--
Tom

Quoted 6 lines
Quoted 4 lines

I will ask NXP-platform owners to check on this.

Thanks
Priyanka

Hello! Any news on this?

Indeed it is a historical problem and exposed by this patch.
The problem is the RCW headers are different on PPC and ARM platforms, while ls1021a/ls1043a/ls1046a leveraged the PPC tool to generate PBL image, but didn’t handle this differentially, this result in the image header verification failed. I'll submit a patch to fix it.

Thanks,
Zhiqiang

#15 lore raw

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

In reply to Tom Rini
Quoted 12 lines · On Friday 21 January 2022 16:21:33 Tom Rini wrote:
Quoted 9 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
Quoted 5 lines

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Signature · 1 line
Tom

Hello Tom! Is something still blocking this change?

#16 lore raw

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

Quoted 13 lines · Pali Rohár
Quoted 12 lines · On Friday 21 January 2022 16:21:33 Tom Rini wrote:
Quoted 9 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
Quoted 4 lines
Quoted 2 lines

This breaks a number of platforms such as ls1021atwr_sdcard_qspi and it's not clear to me why exactly.

Hello Tom! Is something still blocking this change?

Nope, thanks for the reminder.

Signature · 1 line
Tom
#17 lore raw

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

In reply to Pali Rohár
Quoted 60 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
tools/mkimage.c +38 -0
index fbe883ce3620..d5ad0925225c 100644
@@ -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;

tools/mkimage.c int main(int argc, char **argv) · line 743

Quoted 8 lines
tools/mkimage.c +3 -0
@@ -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.

Signature · 1 line
Tom
#18 lore raw

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

Hi,
The rk3568 uses V2 IDB structrure and the rkcommon_verify_header() has not cover V2. My teammate will fix it these days and kever.yang will submit his patch to mainline.

陈健洪 (Joseph Chen)
E-mail:[email protected]
瑞芯微电子股份有限公司
Rockchip Electronics Co.Ltd
福建省福州市铜盘路软件大道89号软件园A区21号楼 (350003)
No. 21 Building, A District, No.89,software Boulevard Fuzhou,Fujian,PRC TEL:0591-83991906/07-8573

From: Tom Rini
Date: 2022-03-08 21:42
To: PaliRohár; Joseph Chen
CC: Simon Glass; Alexandru Gagniuc; Yann Dirson; Stefan Roese; MarekBehún; u-boot
Subject: Re: [PATCH v2] tools: mkimage: Call verify_header after writing image to disk

Quoted 60 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
tools/mkimage.c +38 -0
index fbe883ce3620..d5ad0925225c 100644
@@ -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;

tools/mkimage.c int main(int argc, char **argv) · line 743

Quoted 8 lines
tools/mkimage.c +3 -0
@@ -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.

Signature · 1 line
Tom
#19 lore raw

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

In reply to Tom Rini

Hi,
The rk3568 uses V2 IDB structrure and the rkcommon_verify_header() has not cover V2. My teammate will fix it these days and kever.yang will submit his patch to mainline.

陈健洪 (Joseph Chen)
E-mail:[email protected]
瑞芯微电子股份有限公司
Rockchip Electronics Co.Ltd
福建省福州市铜盘路软件大道89号软件园A区21号楼 (350003)
No. 21 Building, A District, No.89,software Boulevard Fuzhou,Fujian,PRC TEL:0591-83991906/07-8573

From: Tom Rini
Date: 2022-03-08 21:42
To: PaliRohár; Joseph Chen
CC: Simon Glass; Alexandru Gagniuc; Yann Dirson; Stefan Roese; MarekBehún; u-boot
Subject: Re: [PATCH v2] tools: mkimage: Call verify_header after writing image to disk

Quoted 60 lines · Pali Rohár

If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct.

---
 tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
tools/mkimage.c +38 -0
index fbe883ce3620..d5ad0925225c 100644
@@ -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;

tools/mkimage.c int main(int argc, char **argv) · line 743

Quoted 8 lines
tools/mkimage.c +3 -0
@@ -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.

Signature · 1 line
Tom