[PATCH 40/41] imx: parse_container: use malloc for container processing

Peng Fan (OSS) peng.fan at oss.nxp.com
Mon Jan 23 10:16:59 CET 2023


From: Peng Fan <peng.fan at nxp.com>

The container may have image which conflicts with
spl_get_load_buffer address, then there are processing failures.
So use malloc instead of spl_get_load_buffer.

Signed-off-by: Peng Fan <peng.fan at nxp.com>
---
 arch/arm/mach-imx/parse-container.c | 37 +++++++++++++++++++----------
 1 file changed, 24 insertions(+), 13 deletions(-)

diff --git a/arch/arm/mach-imx/parse-container.c b/arch/arm/mach-imx/parse-container.c
index 5f87b6c202c..7d5c677814d 100644
--- a/arch/arm/mach-imx/parse-container.c
+++ b/arch/arm/mach-imx/parse-container.c
@@ -4,6 +4,7 @@
  */
 
 #include <common.h>
+#include <stdlib.h>
 #include <errno.h>
 #include <log.h>
 #include <spl.h>
@@ -68,25 +69,27 @@ static int read_auth_container(struct spl_image_info *spl_image,
 	size = roundup(CONTAINER_HDR_ALIGNMENT, info->bl_len);
 	sectors = size / info->bl_len;
 
-	/*
-	 * It will not override the ATF code, so safe to use it here,
-	 * no need malloc
-	 */
-	container = (struct container_hdr *)spl_get_load_buffer(-size, size);
+	container = malloc(size);
+	if (!container)
+		return -ENOMEM;
 
 	debug("%s: container: %p sector: %lu sectors: %u\n", __func__,
 	      container, sector, sectors);
-	if (info->read(info, sector, sectors, container) != sectors)
-		return -EIO;
+	if (info->read(info, sector, sectors, container) != sectors) {
+		ret = -EIO;
+		goto end;
+	}
 
 	if (container->tag != 0x87 && container->version != 0x0) {
 		printf("Wrong container header\n");
-		return -ENOENT;
+		ret = -ENOENT;
+		goto end;
 	}
 
 	if (!container->num_images) {
 		printf("Wrong container, no image found\n");
-		return -ENOENT;
+		ret = -ENOENT;
+		goto end;
 	}
 
 	length = container->length_lsb + (container->length_msb << 8);
@@ -96,13 +99,17 @@ static int read_auth_container(struct spl_image_info *spl_image,
 		size = roundup(length, info->bl_len);
 		sectors = size / info->bl_len;
 
-		container = (struct container_hdr *)spl_get_load_buffer(-size, size);
+		free(container);
+		container = malloc(size);
+		if (!container)
+			return -ENOMEM;
 
 		debug("%s: container: %p sector: %lu sectors: %u\n",
 		      __func__, container, sector, sectors);
-		if (info->read(info, sector, sectors, container) !=
-		    sectors)
-			return -EIO;
+		if (info->read(info, sector, sectors, container) != sectors) {
+			ret = -EIO;
+			goto end;
+		}
 	}
 
 #ifdef CONFIG_AHAB_BOOT
@@ -131,6 +138,10 @@ end_auth:
 #ifdef CONFIG_AHAB_BOOT
 	ahab_auth_release();
 #endif
+
+end:
+	free(container);
+
 	return ret;
 }
 
-- 
2.36.0



More information about the U-Boot mailing list