Move i.MX8 container image loading support to common/spl
authorSean Anderson <seanga2@gmail.com>
Sat, 14 Oct 2023 20:47:45 +0000 (16:47 -0400)
committerTom Rini <trini@konsulko.com>
Wed, 18 Oct 2023 00:50:52 +0000 (20:50 -0400)
To facilitate testing loading i.MX8 container images, move the
parse-container code to common/spl.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
MAINTAINERS
arch/arm/mach-imx/Kconfig
arch/arm/mach-imx/Makefile
arch/arm/mach-imx/parse-container.c [deleted file]
common/spl/Kconfig
common/spl/Makefile
common/spl/spl_imx_container.c [new file with mode: 0644]

index 67f0fe8c8cf82b32aa3b49c3c9a0f9807d3540b8..cde778bc4d3dd3918356b579ec58bc41c3f4cee6 100644 (file)
@@ -300,6 +300,7 @@ F:  arch/arm/include/asm/arch-vf610/
 F:     arch/arm/include/asm/mach-imx/
 F:     board/freescale/*mx*/
 F:     board/freescale/common/
+F:     common/spl/spl_imx_container.c
 F:     drivers/serial/serial_mxc.c
 F:     include/imx_container.h
 
index 266bb20df9d93dba7899fbc03acf7c91c7dc49f0..08ab7069187eceef479488e0903e51610f9f775b 100644 (file)
@@ -194,19 +194,6 @@ config IMX_DCD_ADDR
          This information is shared with the user via mkimage -l just so the
          image can be signed.
 
-config SPL_LOAD_IMX_CONTAINER
-       bool "Enable SPL loading U-Boot as a i.MX Container image"
-       depends on SPL
-       help
-         This is to let SPL could load i.MX Container image
-
-config IMX_CONTAINER_CFG
-       string "i.MX Container config file"
-       depends on SPL
-       help
-         This is to specific the cfg file for generating container
-         image which will be loaded by SPL.
-
 config IOMUX_LPSR
        bool
 
index 6904cf38802ac3e950e5fb41f36a7fee6f824b61..a3b44c93e3d63036ef723f738bcc448af78455ef 100644 (file)
@@ -79,7 +79,7 @@ obj-$(CONFIG_CMD_NANDBCB) += cmd_nandbcb.o
 endif
 
 ifeq ($(CONFIG_SPL_BUILD),y)
-obj-$(CONFIG_SPL_LOAD_IMX_CONTAINER) += image-container.o parse-container.o
+obj-$(CONFIG_SPL_LOAD_IMX_CONTAINER) += image-container.o
 endif
 
 ifeq ($(SOC),$(filter $(SOC),imx8ulp imx9))
diff --git a/arch/arm/mach-imx/parse-container.c b/arch/arm/mach-imx/parse-container.c
deleted file mode 100644 (file)
index c29cb15..0000000
+++ /dev/null
@@ -1,158 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0+
-/*
- * Copyright 2018-2021 NXP
- */
-
-#define LOG_CATEGORY LOGC_ARCH
-#include <common.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <imx_container.h>
-#include <log.h>
-#include <spl.h>
-#ifdef CONFIG_AHAB_BOOT
-#include <asm/mach-imx/ahab.h>
-#endif
-
-static struct boot_img_t *read_auth_image(struct spl_image_info *spl_image,
-                                         struct spl_load_info *info,
-                                         struct container_hdr *container,
-                                         int image_index,
-                                         u32 container_sector)
-{
-       struct boot_img_t *images;
-       ulong sector;
-       u32 sectors;
-
-       if (image_index > container->num_images) {
-               debug("Invalid image number\n");
-               return NULL;
-       }
-
-       images = (struct boot_img_t *)((u8 *)container +
-                                      sizeof(struct container_hdr));
-
-       if (images[image_index].offset % info->bl_len) {
-               printf("%s: image%d offset not aligned to %u\n",
-                      __func__, image_index, info->bl_len);
-               return NULL;
-       }
-
-       sectors = roundup(images[image_index].size, info->bl_len) /
-               info->bl_len;
-       sector = images[image_index].offset / info->bl_len +
-               container_sector;
-
-       debug("%s: container: %p sector: %lu sectors: %u\n", __func__,
-             container, sector, sectors);
-       if (info->read(info, sector, sectors,
-                      (void *)images[image_index].dst) != sectors) {
-               printf("%s wrong\n", __func__);
-               return NULL;
-       }
-
-#ifdef CONFIG_AHAB_BOOT
-       if (ahab_verify_cntr_image(&images[image_index], image_index))
-               return NULL;
-#endif
-
-       return &images[image_index];
-}
-
-static int read_auth_container(struct spl_image_info *spl_image,
-                              struct spl_load_info *info, ulong sector)
-{
-       struct container_hdr *container = NULL;
-       u16 length;
-       u32 sectors;
-       int i, size, ret = 0;
-
-       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 = 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) {
-               ret = -EIO;
-               goto end;
-       }
-
-       if (!valid_container_hdr(container)) {
-               log_err("Wrong container header\n");
-               ret = -ENOENT;
-               goto end;
-       }
-
-       if (!container->num_images) {
-               log_err("Wrong container, no image found\n");
-               ret = -ENOENT;
-               goto end;
-       }
-
-       length = container->length_lsb + (container->length_msb << 8);
-       debug("Container length %u\n", length);
-
-       if (length > CONTAINER_HDR_ALIGNMENT) {
-               size = roundup(length, info->bl_len);
-               sectors = size / info->bl_len;
-
-               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) {
-                       ret = -EIO;
-                       goto end;
-               }
-       }
-
-#ifdef CONFIG_AHAB_BOOT
-       ret = ahab_auth_cntr_hdr(container, length);
-       if (ret)
-               goto end_auth;
-#endif
-
-       for (i = 0; i < container->num_images; i++) {
-               struct boot_img_t *image = read_auth_image(spl_image, info,
-                                                          container, i,
-                                                          sector);
-
-               if (!image) {
-                       ret = -EINVAL;
-                       goto end_auth;
-               }
-
-               if (i == 0) {
-                       spl_image->load_addr = image->dst;
-                       spl_image->entry_point = image->entry;
-               }
-       }
-
-end_auth:
-#ifdef CONFIG_AHAB_BOOT
-       ahab_auth_release();
-#endif
-
-end:
-       free(container);
-
-       return ret;
-}
-
-int spl_load_imx_container(struct spl_image_info *spl_image,
-                          struct spl_load_info *info, ulong sector)
-{
-       return read_auth_container(spl_image, info, sector);
-}
index 46323597942717499afcf55c6c95723347daeac4..ad574a600e31c9e90e3401a7a073bec86dfcb48c 100644 (file)
@@ -330,6 +330,20 @@ config SPL_LEGACY_IMAGE_CRC_CHECK
          If disabled, Legacy images are booted if the image magic and size
          are correct, without further integrity checks.
 
+config SPL_LOAD_IMX_CONTAINER
+       bool "Enable SPL loading and booting of i.MX8 Containers"
+       depends on SPL
+       help
+         Support booting U-Boot from an i.MX8 container image. If you are not
+         using i.MX8, say 'n'.
+
+config IMX_CONTAINER_CFG
+       string "i.MX8 Container config file"
+       depends on SPL && SPL_LOAD_IMX_CONTAINER
+       help
+         Specify the cfg file for generating the container image which will be
+         loaded by SPL.
+
 config SPL_SYS_MALLOC_SIMPLE
        bool "Only use malloc_simple functions in the SPL"
        help
index bad2bbf6cf1b2bdc41f640ce11ec7bd47b24c6f4..4f8eb2ec0cabb8bc452112eb89007af9e31df86b 100644 (file)
@@ -28,6 +28,7 @@ obj-$(CONFIG_$(SPL_TPL_)OPENSBI) += spl_opensbi.o
 obj-$(CONFIG_$(SPL_TPL_)USB_STORAGE) += spl_usb.o
 obj-$(CONFIG_$(SPL_TPL_)FS_FAT) += spl_fat.o
 obj-$(CONFIG_$(SPL_TPL_)FS_EXT4) += spl_ext.o
+obj-$(CONFIG_$(SPL_TPL_)LOAD_IMX_CONTAINER) += spl_imx_container.o
 obj-$(CONFIG_$(SPL_TPL_)SATA) += spl_sata.o
 obj-$(CONFIG_$(SPL_TPL_)NVME) += spl_nvme.o
 obj-$(CONFIG_$(SPL_TPL_)SEMIHOSTING) += spl_semihosting.o
diff --git a/common/spl/spl_imx_container.c b/common/spl/spl_imx_container.c
new file mode 100644 (file)
index 0000000..c29cb15
--- /dev/null
@@ -0,0 +1,158 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2018-2021 NXP
+ */
+
+#define LOG_CATEGORY LOGC_ARCH
+#include <common.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <imx_container.h>
+#include <log.h>
+#include <spl.h>
+#ifdef CONFIG_AHAB_BOOT
+#include <asm/mach-imx/ahab.h>
+#endif
+
+static struct boot_img_t *read_auth_image(struct spl_image_info *spl_image,
+                                         struct spl_load_info *info,
+                                         struct container_hdr *container,
+                                         int image_index,
+                                         u32 container_sector)
+{
+       struct boot_img_t *images;
+       ulong sector;
+       u32 sectors;
+
+       if (image_index > container->num_images) {
+               debug("Invalid image number\n");
+               return NULL;
+       }
+
+       images = (struct boot_img_t *)((u8 *)container +
+                                      sizeof(struct container_hdr));
+
+       if (images[image_index].offset % info->bl_len) {
+               printf("%s: image%d offset not aligned to %u\n",
+                      __func__, image_index, info->bl_len);
+               return NULL;
+       }
+
+       sectors = roundup(images[image_index].size, info->bl_len) /
+               info->bl_len;
+       sector = images[image_index].offset / info->bl_len +
+               container_sector;
+
+       debug("%s: container: %p sector: %lu sectors: %u\n", __func__,
+             container, sector, sectors);
+       if (info->read(info, sector, sectors,
+                      (void *)images[image_index].dst) != sectors) {
+               printf("%s wrong\n", __func__);
+               return NULL;
+       }
+
+#ifdef CONFIG_AHAB_BOOT
+       if (ahab_verify_cntr_image(&images[image_index], image_index))
+               return NULL;
+#endif
+
+       return &images[image_index];
+}
+
+static int read_auth_container(struct spl_image_info *spl_image,
+                              struct spl_load_info *info, ulong sector)
+{
+       struct container_hdr *container = NULL;
+       u16 length;
+       u32 sectors;
+       int i, size, ret = 0;
+
+       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 = 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) {
+               ret = -EIO;
+               goto end;
+       }
+
+       if (!valid_container_hdr(container)) {
+               log_err("Wrong container header\n");
+               ret = -ENOENT;
+               goto end;
+       }
+
+       if (!container->num_images) {
+               log_err("Wrong container, no image found\n");
+               ret = -ENOENT;
+               goto end;
+       }
+
+       length = container->length_lsb + (container->length_msb << 8);
+       debug("Container length %u\n", length);
+
+       if (length > CONTAINER_HDR_ALIGNMENT) {
+               size = roundup(length, info->bl_len);
+               sectors = size / info->bl_len;
+
+               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) {
+                       ret = -EIO;
+                       goto end;
+               }
+       }
+
+#ifdef CONFIG_AHAB_BOOT
+       ret = ahab_auth_cntr_hdr(container, length);
+       if (ret)
+               goto end_auth;
+#endif
+
+       for (i = 0; i < container->num_images; i++) {
+               struct boot_img_t *image = read_auth_image(spl_image, info,
+                                                          container, i,
+                                                          sector);
+
+               if (!image) {
+                       ret = -EINVAL;
+                       goto end_auth;
+               }
+
+               if (i == 0) {
+                       spl_image->load_addr = image->dst;
+                       spl_image->entry_point = image->entry;
+               }
+       }
+
+end_auth:
+#ifdef CONFIG_AHAB_BOOT
+       ahab_auth_release();
+#endif
+
+end:
+       free(container);
+
+       return ret;
+}
+
+int spl_load_imx_container(struct spl_image_info *spl_image,
+                          struct spl_load_info *info, ulong sector)
+{
+       return read_auth_container(spl_image, info, sector);
+}