All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH] arm: mach-keystone: Use appended original image size in image processing
@ 2019-07-12 19:33 Andrew F. Davis
  2019-07-16  5:06 ` Lokesh Vutla
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew F. Davis @ 2019-07-12 19:33 UTC (permalink / raw)
  To: u-boot

When an image is signed/encrypted on K2 devices the image may get padded
to meet alignment requirements for the signature/encryption type. The
original size is appended as 4 bytes little-endian to the end of the
final image.

Normally the trailing extra bytes are ignored and so restoring the
exact original size is not important. In the case of initrd the
original size is important as the kernel uses it to look for
additional filesystem data and can do the wrong thing when the
size is not correct.

Read off the original size and report it back from the image post
processing stage.

Signed-off-by: Andrew F. Davis <afd@ti.com>
---
 arch/arm/mach-keystone/mon.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/arch/arm/mach-keystone/mon.c b/arch/arm/mach-keystone/mon.c
index 51af028590..b7ffe277c1 100644
--- a/arch/arm/mach-keystone/mon.c
+++ b/arch/arm/mach-keystone/mon.c
@@ -105,6 +105,7 @@ void board_fit_image_post_process(void **p_image, size_t *p_size)
 {
 	int result = 0;
 	void *image = *p_image;
+	uint8_t *size_image = image + (*p_size - 4);
 
 	if (strncmp(image + KS2_HS_SEC_TAG_OFFSET, "KEYS", 4)) {
 		printf("No signature found in image!\n");
@@ -120,9 +121,12 @@ void board_fit_image_post_process(void **p_image, size_t *p_size)
 	/*
 	 * Overwrite the image headers after authentication
 	 * and decryption. Update size to reflect removal
-	 * of header.
+	 * of header and restore original file size.
 	 */
-	*p_size -= KS2_HS_SEC_HEADER_LEN;
+	*p_size = *(size_image+0) <<  0 |
+	          *(size_image+1) <<  8 |
+	          *(size_image+2) << 16 |
+	          *(size_image+3) << 24;
 	memcpy(image, image + KS2_HS_SEC_HEADER_LEN, *p_size);
 
 	/*
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [U-Boot] [PATCH] arm: mach-keystone: Use appended original image size in image processing
  2019-07-12 19:33 [U-Boot] [PATCH] arm: mach-keystone: Use appended original image size in image processing Andrew F. Davis
@ 2019-07-16  5:06 ` Lokesh Vutla
  0 siblings, 0 replies; 2+ messages in thread
From: Lokesh Vutla @ 2019-07-16  5:06 UTC (permalink / raw)
  To: u-boot



On 13/07/19 1:03 AM, Andrew F. Davis wrote:
> When an image is signed/encrypted on K2 devices the image may get padded
> to meet alignment requirements for the signature/encryption type. The
> original size is appended as 4 bytes little-endian to the end of the
> final image.
> 
> Normally the trailing extra bytes are ignored and so restoring the
> exact original size is not important. In the case of initrd the
> original size is important as the kernel uses it to look for
> additional filesystem data and can do the wrong thing when the
> size is not correct.
> 
> Read off the original size and report it back from the image post
> processing stage.
> 
> Signed-off-by: Andrew F. Davis <afd@ti.com>
> ---
>  arch/arm/mach-keystone/mon.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm/mach-keystone/mon.c b/arch/arm/mach-keystone/mon.c
> index 51af028590..b7ffe277c1 100644
> --- a/arch/arm/mach-keystone/mon.c
> +++ b/arch/arm/mach-keystone/mon.c
> @@ -105,6 +105,7 @@ void board_fit_image_post_process(void **p_image, size_t *p_size)
>  {
>  	int result = 0;
>  	void *image = *p_image;
> +	uint8_t *size_image = image + (*p_size - 4);
>  
>  	if (strncmp(image + KS2_HS_SEC_TAG_OFFSET, "KEYS", 4)) {
>  		printf("No signature found in image!\n");
> @@ -120,9 +121,12 @@ void board_fit_image_post_process(void **p_image, size_t *p_size)
>  	/*
>  	 * Overwrite the image headers after authentication
>  	 * and decryption. Update size to reflect removal
> -	 * of header.
> +	 * of header and restore original file size.
>  	 */
> -	*p_size -= KS2_HS_SEC_HEADER_LEN;
> +	*p_size = *(size_image+0) <<  0 |
> +	          *(size_image+1) <<  8 |
> +	          *(size_image+2) << 16 |
> +	          *(size_image+3) << 24;
Instead use get_unaligned_le32(size)?

Thanks and regards,
Lokesh

>  	memcpy(image, image + KS2_HS_SEC_HEADER_LEN, *p_size);
>  
>  	/*
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-07-16  5:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-12 19:33 [U-Boot] [PATCH] arm: mach-keystone: Use appended original image size in image processing Andrew F. Davis
2019-07-16  5:06 ` Lokesh Vutla

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.