blob: b4ea9241d68595c7e852c7eb0fbb49c9e251ee44 [file] [log] [blame]
Peng Fan7b86cd42019-08-22 07:42:33 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
Ye Li00ce4152023-06-15 18:09:23 +08003 * Copyright 2018-2021 NXP
Peng Fan7b86cd42019-08-22 07:42:33 +00004 */
5
Sean Andersond9416cc2023-10-14 16:47:42 -04006#define LOG_CATEGORY LOGC_ARCH
Peng Fan7b86cd42019-08-22 07:42:33 +00007#include <common.h>
Nitin Garg6e81ca22023-06-15 18:09:22 +08008#include <stdlib.h>
Peng Fan7b86cd42019-08-22 07:42:33 +00009#include <errno.h>
Sean Andersonab121792023-10-14 16:47:44 -040010#include <imx_container.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Sean Andersonb02c4e92023-10-14 16:47:55 -040012#include <mapmem.h>
Peng Fan7b86cd42019-08-22 07:42:33 +000013#include <spl.h>
Peng Fan20ed81e2021-08-07 16:00:38 +080014#ifdef CONFIG_AHAB_BOOT
Ye Li00ce4152023-06-15 18:09:23 +080015#include <asm/mach-imx/ahab.h>
Peng Fan7e2db742019-09-25 08:11:14 +000016#endif
Peng Fan7b86cd42019-08-22 07:42:33 +000017
18static struct boot_img_t *read_auth_image(struct spl_image_info *spl_image,
19 struct spl_load_info *info,
20 struct container_hdr *container,
21 int image_index,
Sean Anderson73c40fc2023-11-08 11:48:40 -050022 ulong container_offset)
Peng Fan7b86cd42019-08-22 07:42:33 +000023{
24 struct boot_img_t *images;
Sean Anderson73c40fc2023-11-08 11:48:40 -050025 ulong offset, overhead, size;
Peng Fan7b86cd42019-08-22 07:42:33 +000026
27 if (image_index > container->num_images) {
28 debug("Invalid image number\n");
29 return NULL;
30 }
31
32 images = (struct boot_img_t *)((u8 *)container +
33 sizeof(struct container_hdr));
34
Sean Anderson5271e352023-11-08 11:48:43 -050035 if (!IS_ALIGNED(images[image_index].offset, spl_get_bl_len(info))) {
Peng Fan7b86cd42019-08-22 07:42:33 +000036 printf("%s: image%d offset not aligned to %u\n",
Sean Anderson5271e352023-11-08 11:48:43 -050037 __func__, image_index, spl_get_bl_len(info));
Peng Fan7b86cd42019-08-22 07:42:33 +000038 return NULL;
39 }
40
Sean Anderson5271e352023-11-08 11:48:43 -050041 size = ALIGN(images[image_index].size, spl_get_bl_len(info));
Sean Anderson73c40fc2023-11-08 11:48:40 -050042 offset = images[image_index].offset + container_offset;
Peng Fan7b86cd42019-08-22 07:42:33 +000043
Sean Anderson73c40fc2023-11-08 11:48:40 -050044 debug("%s: container: %p offset: %lu size: %lu\n", __func__,
45 container, offset, size);
46 if (info->read(info, offset, size,
47 map_sysmem(images[image_index].dst - overhead,
Sean Andersonb63664b2023-11-08 11:48:41 -050048 images[image_index].size)) <
49 images[image_index].size) {
Peng Fan7b86cd42019-08-22 07:42:33 +000050 printf("%s wrong\n", __func__);
51 return NULL;
52 }
53
Peng Fan7e2db742019-09-25 08:11:14 +000054#ifdef CONFIG_AHAB_BOOT
Ye Li00ce4152023-06-15 18:09:23 +080055 if (ahab_verify_cntr_image(&images[image_index], image_index))
Peng Fan7e2db742019-09-25 08:11:14 +000056 return NULL;
Peng Fan7e2db742019-09-25 08:11:14 +000057#endif
58
Peng Fan7b86cd42019-08-22 07:42:33 +000059 return &images[image_index];
60}
61
62static int read_auth_container(struct spl_image_info *spl_image,
Sean Anderson73c40fc2023-11-08 11:48:40 -050063 struct spl_load_info *info, ulong offset)
Peng Fan7b86cd42019-08-22 07:42:33 +000064{
65 struct container_hdr *container = NULL;
66 u16 length;
Peng Fan7e2db742019-09-25 08:11:14 +000067 int i, size, ret = 0;
Peng Fan7b86cd42019-08-22 07:42:33 +000068
Sean Anderson5271e352023-11-08 11:48:43 -050069 size = ALIGN(CONTAINER_HDR_ALIGNMENT, spl_get_bl_len(info));
Peng Fan7b86cd42019-08-22 07:42:33 +000070
71 /*
72 * It will not override the ATF code, so safe to use it here,
73 * no need malloc
74 */
Nitin Garg6e81ca22023-06-15 18:09:22 +080075 container = malloc(size);
76 if (!container)
77 return -ENOMEM;
Peng Fan7b86cd42019-08-22 07:42:33 +000078
Sean Anderson73c40fc2023-11-08 11:48:40 -050079 debug("%s: container: %p offset: %lu size: %u\n", __func__,
80 container, offset, size);
Sean Andersonb63664b2023-11-08 11:48:41 -050081 if (info->read(info, offset, size, container) <
82 CONTAINER_HDR_ALIGNMENT) {
Nitin Garg6e81ca22023-06-15 18:09:22 +080083 ret = -EIO;
84 goto end;
85 }
Peng Fan7b86cd42019-08-22 07:42:33 +000086
Sean Andersond401e0b2023-10-14 16:47:43 -040087 if (!valid_container_hdr(container)) {
Sean Andersond9416cc2023-10-14 16:47:42 -040088 log_err("Wrong container header\n");
Nitin Garg6e81ca22023-06-15 18:09:22 +080089 ret = -ENOENT;
90 goto end;
Peng Fan7b86cd42019-08-22 07:42:33 +000091 }
92
93 if (!container->num_images) {
Sean Andersond9416cc2023-10-14 16:47:42 -040094 log_err("Wrong container, no image found\n");
Nitin Garg6e81ca22023-06-15 18:09:22 +080095 ret = -ENOENT;
96 goto end;
Peng Fan7b86cd42019-08-22 07:42:33 +000097 }
98
99 length = container->length_lsb + (container->length_msb << 8);
100 debug("Container length %u\n", length);
101
102 if (length > CONTAINER_HDR_ALIGNMENT) {
Sean Anderson5271e352023-11-08 11:48:43 -0500103 size = ALIGN(length, spl_get_bl_len(info));
Peng Fan7b86cd42019-08-22 07:42:33 +0000104
Nitin Garg6e81ca22023-06-15 18:09:22 +0800105 free(container);
106 container = malloc(size);
107 if (!container)
108 return -ENOMEM;
Peng Fan7b86cd42019-08-22 07:42:33 +0000109
Sean Anderson73c40fc2023-11-08 11:48:40 -0500110 debug("%s: container: %p offset: %lu size: %u\n",
111 __func__, container, offset, size);
Sean Andersonb63664b2023-11-08 11:48:41 -0500112 if (info->read(info, offset, size, container) < length) {
Nitin Garg6e81ca22023-06-15 18:09:22 +0800113 ret = -EIO;
114 goto end;
115 }
Peng Fan7b86cd42019-08-22 07:42:33 +0000116 }
117
Peng Fan7e2db742019-09-25 08:11:14 +0000118#ifdef CONFIG_AHAB_BOOT
Ye Li00ce4152023-06-15 18:09:23 +0800119 ret = ahab_auth_cntr_hdr(container, length);
120 if (ret)
Nitin Garg6e81ca22023-06-15 18:09:22 +0800121 goto end_auth;
Peng Fan7e2db742019-09-25 08:11:14 +0000122#endif
123
Peng Fan7b86cd42019-08-22 07:42:33 +0000124 for (i = 0; i < container->num_images; i++) {
125 struct boot_img_t *image = read_auth_image(spl_image, info,
126 container, i,
Sean Anderson73c40fc2023-11-08 11:48:40 -0500127 offset);
Peng Fan7b86cd42019-08-22 07:42:33 +0000128
Peng Fan7e2db742019-09-25 08:11:14 +0000129 if (!image) {
130 ret = -EINVAL;
131 goto end_auth;
132 }
Peng Fan7b86cd42019-08-22 07:42:33 +0000133
134 if (i == 0) {
135 spl_image->load_addr = image->dst;
136 spl_image->entry_point = image->entry;
137 }
138 }
139
Peng Fan7e2db742019-09-25 08:11:14 +0000140end_auth:
141#ifdef CONFIG_AHAB_BOOT
Ye Li00ce4152023-06-15 18:09:23 +0800142 ahab_auth_release();
Peng Fan7e2db742019-09-25 08:11:14 +0000143#endif
Nitin Garg6e81ca22023-06-15 18:09:22 +0800144
145end:
146 free(container);
147
Peng Fan7e2db742019-09-25 08:11:14 +0000148 return ret;
Peng Fan7b86cd42019-08-22 07:42:33 +0000149}
150
151int spl_load_imx_container(struct spl_image_info *spl_image,
Sean Anderson73c40fc2023-11-08 11:48:40 -0500152 struct spl_load_info *info, ulong offset)
Peng Fan7b86cd42019-08-22 07:42:33 +0000153{
Sean Anderson73c40fc2023-11-08 11:48:40 -0500154 return read_auth_container(spl_image, info, offset);
Peng Fan7b86cd42019-08-22 07:42:33 +0000155}