1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018-2019 NXP
10 #include <asm/mach-imx/image.h>
11 #ifdef CONFIG_AHAB_BOOT
12 #include <firmware/imx/sci/sci.h>
15 #define SEC_SECURE_RAM_BASE 0x31800000UL
16 #define SEC_SECURE_RAM_END_BASE (SEC_SECURE_RAM_BASE + 0xFFFFUL)
17 #define SECO_LOCAL_SEC_SEC_SECURE_RAM_BASE 0x60000000UL
21 #ifdef CONFIG_AHAB_BOOT
22 static int authenticate_image(struct boot_img_t *img, int image_index)
24 sc_faddr_t start, end;
29 debug("img %d, dst 0x%x, src 0x%x, size 0x%x\n",
30 image_index, (uint32_t)img->dst, img->offset, img->size);
32 /* Find the memreg and set permission for seco pt */
33 err = sc_rm_find_memreg(-1, &mr,
34 img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1),
35 ALIGN(img->dst + img->size, CONFIG_SYS_CACHELINE_SIZE) - 1);
38 printf("can't find memreg for image %d load address 0x%llx, error %d\n",
39 image_index, img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1), err);
43 err = sc_rm_get_memreg_info(-1, mr, &start, &end);
45 debug("memreg %u 0x%llx -- 0x%llx\n", mr, start, end);
47 err = sc_rm_set_memreg_permissions(-1, mr,
48 SECO_PT, SC_RM_PERM_FULL);
50 printf("set permission failed for img %d, error %d\n",
55 err = sc_seco_authenticate(-1, SC_SECO_VERIFY_IMAGE,
58 printf("authenticate img %d failed, return %d\n",
63 err = sc_rm_set_memreg_permissions(-1, mr,
64 SECO_PT, SC_RM_PERM_NONE);
66 printf("remove permission failed for img %d, error %d\n",
75 static struct boot_img_t *read_auth_image(struct spl_image_info *spl_image,
76 struct spl_load_info *info,
77 struct container_hdr *container,
81 struct boot_img_t *images;
85 if (image_index > container->num_images) {
86 debug("Invalid image number\n");
90 images = (struct boot_img_t *)((u8 *)container +
91 sizeof(struct container_hdr));
93 if (images[image_index].offset % info->bl_len) {
94 printf("%s: image%d offset not aligned to %u\n",
95 __func__, image_index, info->bl_len);
99 sectors = roundup(images[image_index].size, info->bl_len) /
101 sector = images[image_index].offset / info->bl_len +
104 debug("%s: container: %p sector: %lu sectors: %u\n", __func__,
105 container, sector, sectors);
106 if (info->read(info, sector, sectors,
107 (void *)images[image_index].entry) != sectors) {
108 printf("%s wrong\n", __func__);
112 #ifdef CONFIG_AHAB_BOOT
113 if (authenticate_image(&images[image_index], image_index)) {
114 printf("Failed to authenticate image %d\n", image_index);
119 return &images[image_index];
122 static int read_auth_container(struct spl_image_info *spl_image,
123 struct spl_load_info *info, ulong sector)
125 struct container_hdr *container = NULL;
128 int i, size, ret = 0;
130 size = roundup(CONTAINER_HDR_ALIGNMENT, info->bl_len);
131 sectors = size / info->bl_len;
134 * It will not override the ATF code, so safe to use it here,
137 container = (struct container_hdr *)spl_get_load_buffer(-size, size);
139 debug("%s: container: %p sector: %lu sectors: %u\n", __func__,
140 container, sector, sectors);
141 if (info->read(info, sector, sectors, container) != sectors)
144 if (container->tag != 0x87 && container->version != 0x0) {
145 printf("Wrong container header\n");
149 if (!container->num_images) {
150 printf("Wrong container, no image found\n");
154 length = container->length_lsb + (container->length_msb << 8);
155 debug("Container length %u\n", length);
157 if (length > CONTAINER_HDR_ALIGNMENT) {
158 size = roundup(length, info->bl_len);
159 sectors = size / info->bl_len;
161 container = (struct container_hdr *)spl_get_load_buffer(-size, size);
163 debug("%s: container: %p sector: %lu sectors: %u\n",
164 __func__, container, sector, sectors);
165 if (info->read(info, sector, sectors, container) !=
170 #ifdef CONFIG_AHAB_BOOT
171 memcpy((void *)SEC_SECURE_RAM_BASE, (const void *)container,
172 ALIGN(length, CONFIG_SYS_CACHELINE_SIZE));
174 ret = sc_seco_authenticate(-1, SC_SECO_AUTH_CONTAINER,
175 SECO_LOCAL_SEC_SEC_SECURE_RAM_BASE);
177 printf("authenticate container hdr failed, return %d\n", ret);
182 for (i = 0; i < container->num_images; i++) {
183 struct boot_img_t *image = read_auth_image(spl_image, info,
193 spl_image->load_addr = image->dst;
194 spl_image->entry_point = image->entry;
199 #ifdef CONFIG_AHAB_BOOT
200 if (sc_seco_authenticate(-1, SC_SECO_REL_CONTAINER, 0))
201 printf("Error: release container failed!\n");
206 int spl_load_imx_container(struct spl_image_info *spl_image,
207 struct spl_load_info *info, ulong sector)
209 return read_auth_container(spl_image, info, sector);