blob: c5b3abc4f29d0c0779f0fdc079b606945bed8db8 [file] [log] [blame]
Peng Fan1a7e6252019-08-22 07:42:33 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018-2019 NXP
4 */
5
6#include <common.h>
Nitin Gargf39c1f22023-06-15 18:09:22 +08007#include <stdlib.h>
Peng Fan1a7e6252019-08-22 07:42:33 +00008#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Peng Fan1a7e6252019-08-22 07:42:33 +000010#include <spl.h>
Ye Lieb101272021-08-07 16:00:37 +080011#include <asm/mach-imx/image.h>
Peng Fanb5906342021-08-07 16:00:38 +080012#ifdef CONFIG_AHAB_BOOT
Peng Fan2e0644a2023-04-28 12:08:09 +080013#include <firmware/imx/sci/sci.h>
Peng Fanb5906342021-08-07 16:00:38 +080014#endif
Peng Fanf1e0f9f2019-09-25 08:11:14 +000015
16#define SEC_SECURE_RAM_BASE 0x31800000UL
17#define SEC_SECURE_RAM_END_BASE (SEC_SECURE_RAM_BASE + 0xFFFFUL)
18#define SECO_LOCAL_SEC_SEC_SECURE_RAM_BASE 0x60000000UL
19
20#define SECO_PT 2U
21
22#ifdef CONFIG_AHAB_BOOT
23static int authenticate_image(struct boot_img_t *img, int image_index)
24{
25 sc_faddr_t start, end;
26 sc_rm_mr_t mr;
27 int err;
28 int ret = 0;
29
Peng Fanf818b742020-04-22 15:45:42 +080030 debug("img %d, dst 0x%x, src 0x%x, size 0x%x\n",
31 image_index, (uint32_t)img->dst, img->offset, img->size);
Peng Fanf1e0f9f2019-09-25 08:11:14 +000032
33 /* Find the memreg and set permission for seco pt */
34 err = sc_rm_find_memreg(-1, &mr,
35 img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1),
Peng Fand1cf84a2020-04-22 15:25:31 +080036 ALIGN(img->dst + img->size, CONFIG_SYS_CACHELINE_SIZE) - 1);
Peng Fanf1e0f9f2019-09-25 08:11:14 +000037
38 if (err) {
Peng Fan489c7402023-06-15 18:09:06 +080039 printf("can't find memreg for image %d load address 0x%llx, error %d\n",
Peng Fanf818b742020-04-22 15:45:42 +080040 image_index, img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1), err);
Peng Fanf1e0f9f2019-09-25 08:11:14 +000041 return -ENOMEM;
42 }
43
44 err = sc_rm_get_memreg_info(-1, mr, &start, &end);
45 if (!err)
Peng Fan489c7402023-06-15 18:09:06 +080046 debug("memreg %u 0x%llx -- 0x%llx\n", mr, start, end);
Peng Fanf1e0f9f2019-09-25 08:11:14 +000047
48 err = sc_rm_set_memreg_permissions(-1, mr,
49 SECO_PT, SC_RM_PERM_FULL);
50 if (err) {
51 printf("set permission failed for img %d, error %d\n",
52 image_index, err);
53 return -EPERM;
54 }
55
Ye Lid962d562019-10-14 23:08:46 -070056 err = sc_seco_authenticate(-1, SC_SECO_VERIFY_IMAGE,
Peng Fanf1e0f9f2019-09-25 08:11:14 +000057 1 << image_index);
58 if (err) {
59 printf("authenticate img %d failed, return %d\n",
60 image_index, err);
61 ret = -EIO;
62 }
63
64 err = sc_rm_set_memreg_permissions(-1, mr,
65 SECO_PT, SC_RM_PERM_NONE);
66 if (err) {
67 printf("remove permission failed for img %d, error %d\n",
68 image_index, err);
69 ret = -EPERM;
70 }
71
72 return ret;
73}
74#endif
Peng Fan1a7e6252019-08-22 07:42:33 +000075
76static struct boot_img_t *read_auth_image(struct spl_image_info *spl_image,
77 struct spl_load_info *info,
78 struct container_hdr *container,
79 int image_index,
80 u32 container_sector)
81{
82 struct boot_img_t *images;
83 ulong sector;
84 u32 sectors;
85
86 if (image_index > container->num_images) {
87 debug("Invalid image number\n");
88 return NULL;
89 }
90
91 images = (struct boot_img_t *)((u8 *)container +
92 sizeof(struct container_hdr));
93
94 if (images[image_index].offset % info->bl_len) {
95 printf("%s: image%d offset not aligned to %u\n",
96 __func__, image_index, info->bl_len);
97 return NULL;
98 }
99
100 sectors = roundup(images[image_index].size, info->bl_len) /
101 info->bl_len;
102 sector = images[image_index].offset / info->bl_len +
103 container_sector;
104
105 debug("%s: container: %p sector: %lu sectors: %u\n", __func__,
106 container, sector, sectors);
107 if (info->read(info, sector, sectors,
108 (void *)images[image_index].entry) != sectors) {
109 printf("%s wrong\n", __func__);
110 return NULL;
111 }
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000112
113#ifdef CONFIG_AHAB_BOOT
114 if (authenticate_image(&images[image_index], image_index)) {
115 printf("Failed to authenticate image %d\n", image_index);
116 return NULL;
117 }
118#endif
Peng Fan1a7e6252019-08-22 07:42:33 +0000119
120 return &images[image_index];
121}
122
123static int read_auth_container(struct spl_image_info *spl_image,
124 struct spl_load_info *info, ulong sector)
125{
126 struct container_hdr *container = NULL;
127 u16 length;
128 u32 sectors;
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000129 int i, size, ret = 0;
Peng Fan1a7e6252019-08-22 07:42:33 +0000130
131 size = roundup(CONTAINER_HDR_ALIGNMENT, info->bl_len);
132 sectors = size / info->bl_len;
133
134 /*
135 * It will not override the ATF code, so safe to use it here,
136 * no need malloc
137 */
Nitin Gargf39c1f22023-06-15 18:09:22 +0800138 container = malloc(size);
139 if (!container)
140 return -ENOMEM;
Peng Fan1a7e6252019-08-22 07:42:33 +0000141
142 debug("%s: container: %p sector: %lu sectors: %u\n", __func__,
143 container, sector, sectors);
Nitin Gargf39c1f22023-06-15 18:09:22 +0800144 if (info->read(info, sector, sectors, container) != sectors) {
145 ret = -EIO;
146 goto end;
147 }
Peng Fan1a7e6252019-08-22 07:42:33 +0000148
149 if (container->tag != 0x87 && container->version != 0x0) {
Nitin Gargf39c1f22023-06-15 18:09:22 +0800150 printf("Wrong container header");
151 ret = -ENOENT;
152 goto end;
Peng Fan1a7e6252019-08-22 07:42:33 +0000153 }
154
155 if (!container->num_images) {
Nitin Gargf39c1f22023-06-15 18:09:22 +0800156 printf("Wrong container, no image found");
157 ret = -ENOENT;
158 goto end;
Peng Fan1a7e6252019-08-22 07:42:33 +0000159 }
160
161 length = container->length_lsb + (container->length_msb << 8);
162 debug("Container length %u\n", length);
163
164 if (length > CONTAINER_HDR_ALIGNMENT) {
165 size = roundup(length, info->bl_len);
166 sectors = size / info->bl_len;
167
Nitin Gargf39c1f22023-06-15 18:09:22 +0800168 free(container);
169 container = malloc(size);
170 if (!container)
171 return -ENOMEM;
Peng Fan1a7e6252019-08-22 07:42:33 +0000172
173 debug("%s: container: %p sector: %lu sectors: %u\n",
174 __func__, container, sector, sectors);
175 if (info->read(info, sector, sectors, container) !=
Nitin Gargf39c1f22023-06-15 18:09:22 +0800176 sectors) {
177 ret = -EIO;
178 goto end;
179 }
Peng Fan1a7e6252019-08-22 07:42:33 +0000180 }
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000181
182#ifdef CONFIG_AHAB_BOOT
183 memcpy((void *)SEC_SECURE_RAM_BASE, (const void *)container,
184 ALIGN(length, CONFIG_SYS_CACHELINE_SIZE));
185
Ye Lid962d562019-10-14 23:08:46 -0700186 ret = sc_seco_authenticate(-1, SC_SECO_AUTH_CONTAINER,
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000187 SECO_LOCAL_SEC_SEC_SECURE_RAM_BASE);
188 if (ret) {
189 printf("authenticate container hdr failed, return %d\n", ret);
Nitin Gargf39c1f22023-06-15 18:09:22 +0800190 goto end_auth;
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000191 }
192#endif
Peng Fan1a7e6252019-08-22 07:42:33 +0000193
194 for (i = 0; i < container->num_images; i++) {
195 struct boot_img_t *image = read_auth_image(spl_image, info,
196 container, i,
197 sector);
198
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000199 if (!image) {
200 ret = -EINVAL;
201 goto end_auth;
202 }
Peng Fan1a7e6252019-08-22 07:42:33 +0000203
204 if (i == 0) {
205 spl_image->load_addr = image->dst;
206 spl_image->entry_point = image->entry;
207 }
208 }
209
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000210end_auth:
211#ifdef CONFIG_AHAB_BOOT
Ye Lid962d562019-10-14 23:08:46 -0700212 if (sc_seco_authenticate(-1, SC_SECO_REL_CONTAINER, 0))
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000213 printf("Error: release container failed!\n");
214#endif
Nitin Gargf39c1f22023-06-15 18:09:22 +0800215
216end:
217 free(container);
218
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000219 return ret;
Peng Fan1a7e6252019-08-22 07:42:33 +0000220}
221
222int spl_load_imx_container(struct spl_image_info *spl_image,
223 struct spl_load_info *info, ulong sector)
224{
225 return read_auth_container(spl_image, info, sector);
226}