2019-08-22 07:42:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
2023-06-15 10:09:23 +00:00
|
|
|
* Copyright 2018-2021 NXP
|
2019-08-22 07:42:33 +00:00
|
|
|
*/
|
|
|
|
|
2023-10-14 20:47:42 +00:00
|
|
|
#define LOG_CATEGORY LOGC_ARCH
|
2019-08-22 07:42:33 +00:00
|
|
|
#include <common.h>
|
2023-06-15 10:09:22 +00:00
|
|
|
#include <stdlib.h>
|
2019-08-22 07:42:33 +00:00
|
|
|
#include <errno.h>
|
arm: imx: Check header before calling spl_load_imx_container
Make sure we have an IMX header before calling spl_load_imx_container,
since if we don't it will fail with -ENOENT. This allows us to fall back to
legacy/raw images if they are also enabled.
This is a functional change, one which likely should have been in place
from the start, but a functional change nonetheless. Previously, all
non-IMX8 images (except FITs without FIT_FULL) would be optimized out if
the only image load method enabled supported IMX8 images. With this change,
support for other image types now has an effect.
There are seven boards with SPL_LOAD_IMX_CONTAINER enabled: three with
SPL_BOOTROM_SUPPORT:
imx93_11x11_evk_ld imx93_11x11_evk imx8ulp_evk
and four with SPL_MMC:
deneb imx8qxp_mek giedi imx8qm_mek
All of these boards also have SPL_RAW_IMAGE_SUPPORT and
SPL_LEGACY_IMAGE_FORMAT enabled as well. However, none have FIT support
enabled. Of the six load methods affected by this patch, only SPL_MMC and
SPL_BOOTROM_SUPPORT are enabled with SPL_LOAD_IMX_CONTAINER.
spl_romapi_load_image_seekable does not support legacy or raw images, so
there is no growth. However, mmc_load_image_raw_sector does support loading
legacy/raw images. Since these images could not have been booted before, I
have disabled support for legacy/raw images on these four boards. This
reduces bloat from around 800 bytes to around 200.
There are no in-tree boards with SPL_LOAD_IMX_CONTAINER and AHAB_BOOT both
enabled, so we do not need to worry about potentially falling back to
legacy images in a secure boot scenario.
Future work could include merging imx_container.h with imx8image.h, since
they appear to define mostly the same structures.
Signed-off-by: Sean Anderson <seanga2@gmail.com>
2023-10-14 20:47:44 +00:00
|
|
|
#include <imx_container.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2023-10-14 20:47:55 +00:00
|
|
|
#include <mapmem.h>
|
2019-08-22 07:42:33 +00:00
|
|
|
#include <spl.h>
|
2021-08-07 08:00:38 +00:00
|
|
|
#ifdef CONFIG_AHAB_BOOT
|
2023-06-15 10:09:23 +00:00
|
|
|
#include <asm/mach-imx/ahab.h>
|
2019-09-25 08:11:14 +00:00
|
|
|
#endif
|
2019-08-22 07:42:33 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2023-11-08 16:48:39 +00:00
|
|
|
if (!IS_ALIGNED(images[image_index].offset, info->bl_len)) {
|
2019-08-22 07:42:33 +00:00
|
|
|
printf("%s: image%d offset not aligned to %u\n",
|
|
|
|
__func__, image_index, info->bl_len);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-11-08 16:48:39 +00:00
|
|
|
sectors = ALIGN(images[image_index].size, info->bl_len) /
|
2019-08-22 07:42:33 +00:00
|
|
|
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,
|
2023-10-14 20:47:55 +00:00
|
|
|
map_sysmem(images[image_index].dst,
|
|
|
|
images[image_index].size)) != sectors) {
|
2019-08-22 07:42:33 +00:00
|
|
|
printf("%s wrong\n", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:11:14 +00:00
|
|
|
#ifdef CONFIG_AHAB_BOOT
|
2023-06-15 10:09:23 +00:00
|
|
|
if (ahab_verify_cntr_image(&images[image_index], image_index))
|
2019-09-25 08:11:14 +00:00
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
|
2019-08-22 07:42:33 +00:00
|
|
|
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;
|
2019-09-25 08:11:14 +00:00
|
|
|
int i, size, ret = 0;
|
2019-08-22 07:42:33 +00:00
|
|
|
|
2023-11-08 16:48:39 +00:00
|
|
|
size = ALIGN(CONTAINER_HDR_ALIGNMENT, info->bl_len);
|
2019-08-22 07:42:33 +00:00
|
|
|
sectors = size / info->bl_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It will not override the ATF code, so safe to use it here,
|
|
|
|
* no need malloc
|
|
|
|
*/
|
2023-06-15 10:09:22 +00:00
|
|
|
container = malloc(size);
|
|
|
|
if (!container)
|
|
|
|
return -ENOMEM;
|
2019-08-22 07:42:33 +00:00
|
|
|
|
|
|
|
debug("%s: container: %p sector: %lu sectors: %u\n", __func__,
|
|
|
|
container, sector, sectors);
|
2023-06-15 10:09:22 +00:00
|
|
|
if (info->read(info, sector, sectors, container) != sectors) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto end;
|
|
|
|
}
|
2019-08-22 07:42:33 +00:00
|
|
|
|
2023-10-14 20:47:43 +00:00
|
|
|
if (!valid_container_hdr(container)) {
|
2023-10-14 20:47:42 +00:00
|
|
|
log_err("Wrong container header\n");
|
2023-06-15 10:09:22 +00:00
|
|
|
ret = -ENOENT;
|
|
|
|
goto end;
|
2019-08-22 07:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!container->num_images) {
|
2023-10-14 20:47:42 +00:00
|
|
|
log_err("Wrong container, no image found\n");
|
2023-06-15 10:09:22 +00:00
|
|
|
ret = -ENOENT;
|
|
|
|
goto end;
|
2019-08-22 07:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
length = container->length_lsb + (container->length_msb << 8);
|
|
|
|
debug("Container length %u\n", length);
|
|
|
|
|
|
|
|
if (length > CONTAINER_HDR_ALIGNMENT) {
|
2023-11-08 16:48:39 +00:00
|
|
|
size = ALIGN(length, info->bl_len);
|
2019-08-22 07:42:33 +00:00
|
|
|
sectors = size / info->bl_len;
|
|
|
|
|
2023-06-15 10:09:22 +00:00
|
|
|
free(container);
|
|
|
|
container = malloc(size);
|
|
|
|
if (!container)
|
|
|
|
return -ENOMEM;
|
2019-08-22 07:42:33 +00:00
|
|
|
|
|
|
|
debug("%s: container: %p sector: %lu sectors: %u\n",
|
|
|
|
__func__, container, sector, sectors);
|
|
|
|
if (info->read(info, sector, sectors, container) !=
|
2023-06-15 10:09:22 +00:00
|
|
|
sectors) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto end;
|
|
|
|
}
|
2019-08-22 07:42:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 08:11:14 +00:00
|
|
|
#ifdef CONFIG_AHAB_BOOT
|
2023-06-15 10:09:23 +00:00
|
|
|
ret = ahab_auth_cntr_hdr(container, length);
|
|
|
|
if (ret)
|
2023-06-15 10:09:22 +00:00
|
|
|
goto end_auth;
|
2019-09-25 08:11:14 +00:00
|
|
|
#endif
|
|
|
|
|
2019-08-22 07:42:33 +00:00
|
|
|
for (i = 0; i < container->num_images; i++) {
|
|
|
|
struct boot_img_t *image = read_auth_image(spl_image, info,
|
|
|
|
container, i,
|
|
|
|
sector);
|
|
|
|
|
2019-09-25 08:11:14 +00:00
|
|
|
if (!image) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto end_auth;
|
|
|
|
}
|
2019-08-22 07:42:33 +00:00
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
spl_image->load_addr = image->dst;
|
|
|
|
spl_image->entry_point = image->entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:11:14 +00:00
|
|
|
end_auth:
|
|
|
|
#ifdef CONFIG_AHAB_BOOT
|
2023-06-15 10:09:23 +00:00
|
|
|
ahab_auth_release();
|
2019-09-25 08:11:14 +00:00
|
|
|
#endif
|
2023-06-15 10:09:22 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
free(container);
|
|
|
|
|
2019-09-25 08:11:14 +00:00
|
|
|
return ret;
|
2019-08-22 07:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|