blob: b4ea9241d68595c7e852c7eb0fbb49c9e251ee44 [file] [log] [blame]
Peng Fan1a7e6252019-08-22 07:42:33 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
Ye Li185af302023-06-15 18:09:23 +08003 * Copyright 2018-2021 NXP
Peng Fan1a7e6252019-08-22 07:42:33 +00004 */
5
Sean Anderson5d5c7bd2023-10-14 16:47:42 -04006#define LOG_CATEGORY LOGC_ARCH
Peng Fan1a7e6252019-08-22 07:42:33 +00007#include <common.h>
Nitin Gargf39c1f22023-06-15 18:09:22 +08008#include <stdlib.h>
Peng Fan1a7e6252019-08-22 07:42:33 +00009#include <errno.h>
Sean Anderson952ed672023-10-14 16:47:44 -040010#include <imx_container.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Sean Anderson5ff77722023-10-14 16:47:55 -040012#include <mapmem.h>
Peng Fan1a7e6252019-08-22 07:42:33 +000013#include <spl.h>
Peng Fanb5906342021-08-07 16:00:38 +080014#ifdef CONFIG_AHAB_BOOT
Ye Li185af302023-06-15 18:09:23 +080015#include <asm/mach-imx/ahab.h>
Peng Fanb5906342021-08-07 16:00:38 +080016#endif
Peng Fanf1e0f9f2019-09-25 08:11:14 +000017
Peng Fan1a7e6252019-08-22 07:42:33 +000018static 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 Anderson7d8d6132023-11-08 11:48:40 -050022 ulong container_offset)
Peng Fan1a7e6252019-08-22 07:42:33 +000023{
24 struct boot_img_t *images;
Sean Anderson7d8d6132023-11-08 11:48:40 -050025 ulong offset, overhead, size;
Peng Fan1a7e6252019-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 Anderson35f15fe2023-11-08 11:48:43 -050035 if (!IS_ALIGNED(images[image_index].offset, spl_get_bl_len(info))) {
Peng Fan1a7e6252019-08-22 07:42:33 +000036 printf("%s: image%d offset not aligned to %u\n",
Sean Anderson35f15fe2023-11-08 11:48:43 -050037 __func__, image_index, spl_get_bl_len(info));
Peng Fan1a7e6252019-08-22 07:42:33 +000038 return NULL;
39 }
40
Sean Anderson35f15fe2023-11-08 11:48:43 -050041 size = ALIGN(images[image_index].size, spl_get_bl_len(info));
Sean Anderson7d8d6132023-11-08 11:48:40 -050042 offset = images[image_index].offset + container_offset;
Peng Fan1a7e6252019-08-22 07:42:33 +000043
Sean Anderson7d8d6132023-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 Andersonb27c5f82023-11-08 11:48:41 -050048 images[image_index].size)) <
49 images[image_index].size) {
Peng Fan1a7e6252019-08-22 07:42:33 +000050 printf("%s wrong\n", __func__);
51 return NULL;
52 }
Peng Fanf1e0f9f2019-09-25 08:11:14 +000053
54#ifdef CONFIG_AHAB_BOOT
Ye Li185af302023-06-15 18:09:23 +080055 if (ahab_verify_cntr_image(&images[image_index], image_index))
Peng Fanf1e0f9f2019-09-25 08:11:14 +000056 return NULL;
Peng Fanf1e0f9f2019-09-25 08:11:14 +000057#endif
Peng Fan1a7e6252019-08-22 07:42:33 +000058
59 return &images[image_index];
60}
61
62static int read_auth_container(struct spl_image_info *spl_image,
Sean Anderson7d8d6132023-11-08 11:48:40 -050063 struct spl_load_info *info, ulong offset)
Peng Fan1a7e6252019-08-22 07:42:33 +000064{
65 struct container_hdr *container = NULL;
66 u16 length;
Peng Fanf1e0f9f2019-09-25 08:11:14 +000067 int i, size, ret = 0;
Peng Fan1a7e6252019-08-22 07:42:33 +000068
Sean Anderson35f15fe2023-11-08 11:48:43 -050069 size = ALIGN(CONTAINER_HDR_ALIGNMENT, spl_get_bl_len(info));
Peng Fan1a7e6252019-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 Gargf39c1f22023-06-15 18:09:22 +080075 container = malloc(size);
76 if (!container)
77 return -ENOMEM;
Peng Fan1a7e6252019-08-22 07:42:33 +000078
Sean Anderson7d8d6132023-11-08 11:48:40 -050079 debug("%s: container: %p offset: %lu size: %u\n", __func__,
80 container, offset, size);
Sean Andersonb27c5f82023-11-08 11:48:41 -050081 if (info->read(info, offset, size, container) <
82 CONTAINER_HDR_ALIGNMENT) {
Nitin Gargf39c1f22023-06-15 18:09:22 +080083 ret = -EIO;
84 goto end;
85 }
Peng Fan1a7e6252019-08-22 07:42:33 +000086
Sean Andersonc5126682023-10-14 16:47:43 -040087 if (!valid_container_hdr(container)) {
Sean Anderson5d5c7bd2023-10-14 16:47:42 -040088 log_err("Wrong container header\n");
Nitin Gargf39c1f22023-06-15 18:09:22 +080089 ret = -ENOENT;
90 goto end;
Peng Fan1a7e6252019-08-22 07:42:33 +000091 }
92
93 if (!container->num_images) {
Sean Anderson5d5c7bd2023-10-14 16:47:42 -040094 log_err("Wrong container, no image found\n");
Nitin Gargf39c1f22023-06-15 18:09:22 +080095 ret = -ENOENT;
96 goto end;
Peng Fan1a7e6252019-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 Anderson35f15fe2023-11-08 11:48:43 -0500103 size = ALIGN(length, spl_get_bl_len(info));
Peng Fan1a7e6252019-08-22 07:42:33 +0000104
Nitin Gargf39c1f22023-06-15 18:09:22 +0800105 free(container);
106 container = malloc(size);
107 if (!container)
108 return -ENOMEM;
Peng Fan1a7e6252019-08-22 07:42:33 +0000109
Sean Anderson7d8d6132023-11-08 11:48:40 -0500110 debug("%s: container: %p offset: %lu size: %u\n",
111 __func__, container, offset, size);
Sean Andersonb27c5f82023-11-08 11:48:41 -0500112 if (info->read(info, offset, size, container) < length) {
Nitin Gargf39c1f22023-06-15 18:09:22 +0800113 ret = -EIO;
114 goto end;
115 }
Peng Fan1a7e6252019-08-22 07:42:33 +0000116 }
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000117
118#ifdef CONFIG_AHAB_BOOT
Ye Li185af302023-06-15 18:09:23 +0800119 ret = ahab_auth_cntr_hdr(container, length);
120 if (ret)
Nitin Gargf39c1f22023-06-15 18:09:22 +0800121 goto end_auth;
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000122#endif
Peng Fan1a7e6252019-08-22 07:42:33 +0000123
124 for (i = 0; i < container->num_images; i++) {
125 struct boot_img_t *image = read_auth_image(spl_image, info,
126 container, i,
Sean Anderson7d8d6132023-11-08 11:48:40 -0500127 offset);
Peng Fan1a7e6252019-08-22 07:42:33 +0000128
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000129 if (!image) {
130 ret = -EINVAL;
131 goto end_auth;
132 }
Peng Fan1a7e6252019-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 Fanf1e0f9f2019-09-25 08:11:14 +0000140end_auth:
141#ifdef CONFIG_AHAB_BOOT
Ye Li185af302023-06-15 18:09:23 +0800142 ahab_auth_release();
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000143#endif
Nitin Gargf39c1f22023-06-15 18:09:22 +0800144
145end:
146 free(container);
147
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000148 return ret;
Peng Fan1a7e6252019-08-22 07:42:33 +0000149}
150
151int spl_load_imx_container(struct spl_image_info *spl_image,
Sean Anderson7d8d6132023-11-08 11:48:40 -0500152 struct spl_load_info *info, ulong offset)
Peng Fan1a7e6252019-08-22 07:42:33 +0000153{
Sean Anderson7d8d6132023-11-08 11:48:40 -0500154 return read_auth_container(spl_image, info, offset);
Peng Fan1a7e6252019-08-22 07:42:33 +0000155}