blob: 994becccefded7fddf3f02c5fc5e91fcb9cbb490 [file] [log] [blame]
Peng Fanf1e0f9f2019-09-25 08:11:14 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
Gaurav Jain8ecc0db2023-06-15 18:09:24 +08003 * Copyright 2018-2019, 2022 NXP
Peng Fanf1e0f9f2019-09-25 08:11:14 +00004 */
5
6#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06007#include <command.h>
Peng Fanf1e0f9f2019-09-25 08:11:14 +00008#include <errno.h>
Sean Anderson952ed672023-10-14 16:47:44 -04009#include <imx_container.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Peng Fanf1e0f9f2019-09-25 08:11:14 +000012#include <asm/io.h>
Peng Fan2e0644a2023-04-28 12:08:09 +080013#include <firmware/imx/sci/sci.h>
Peng Fanf1e0f9f2019-09-25 08:11:14 +000014#include <asm/mach-imx/sys_proto.h>
15#include <asm/arch-imx/cpu.h>
16#include <asm/arch/sys_proto.h>
Peng Fanf1e0f9f2019-09-25 08:11:14 +000017#include <console.h>
Oliver Grautef46161a2020-11-06 09:09:29 +010018#include <cpu_func.h>
Gaurav Jain8ecc0db2023-06-15 18:09:24 +080019#include "u-boot/sha256.h"
Ye Li185af302023-06-15 18:09:23 +080020#include <asm/mach-imx/ahab.h>
Peng Fanf1e0f9f2019-09-25 08:11:14 +000021
22DECLARE_GLOBAL_DATA_PTR;
23
24#define SEC_SECURE_RAM_BASE (0x31800000UL)
25#define SEC_SECURE_RAM_END_BASE (SEC_SECURE_RAM_BASE + 0xFFFFUL)
26#define SECO_LOCAL_SEC_SEC_SECURE_RAM_BASE (0x60000000UL)
27
28#define SECO_PT 2U
Gaurav Jain8ecc0db2023-06-15 18:09:24 +080029#define AHAB_HASH_TYPE_MASK 0x00000700
30#define AHAB_HASH_TYPE_SHA256 0
Peng Fanf1e0f9f2019-09-25 08:11:14 +000031
Ye Li185af302023-06-15 18:09:23 +080032int ahab_auth_cntr_hdr(struct container_hdr *container, u16 length)
33{
34 int err;
35
36 memcpy((void *)SEC_SECURE_RAM_BASE, (const void *)container,
37 ALIGN(length, CONFIG_SYS_CACHELINE_SIZE));
38
39 err = sc_seco_authenticate(-1, SC_SECO_AUTH_CONTAINER,
40 SECO_LOCAL_SEC_SEC_SECURE_RAM_BASE);
41 if (err)
42 printf("Authenticate container hdr failed, return %d\n", err);
43
44 return err;
45}
46
47int ahab_auth_release(void)
48{
49 int err;
50
51 err = sc_seco_authenticate(-1, SC_SECO_REL_CONTAINER, 0);
52 if (err)
53 printf("Error: release container failed!\n");
54
55 return err;
56}
57
58int ahab_verify_cntr_image(struct boot_img_t *img, int image_index)
59{
60 sc_faddr_t start, end;
61 sc_rm_mr_t mr;
62 int err;
63 int ret = 0;
64
65 debug("img %d, dst 0x%llx, src 0x%x, size 0x%x\n",
66 image_index, img->dst, img->offset, img->size);
67
68 /* Find the memreg and set permission for seco pt */
69 err = sc_rm_find_memreg(-1, &mr,
70 img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1),
71 ALIGN(img->dst + img->size, CONFIG_SYS_CACHELINE_SIZE) - 1);
72
73 if (err) {
74 printf("Error: can't find memreg for image load address 0x%llx, error %d\n",
75 img->dst, err);
76 return -ENOMEM;
77 }
78
79 err = sc_rm_get_memreg_info(-1, mr, &start, &end);
80 if (!err)
81 debug("memreg %u 0x%llx -- 0x%llx\n", mr, start, end);
82
83 err = sc_rm_set_memreg_permissions(-1, mr,
84 SECO_PT, SC_RM_PERM_FULL);
85 if (err) {
86 printf("Set permission failed for img %d, error %d\n",
87 image_index, err);
88 return -EPERM;
89 }
90
91 err = sc_seco_authenticate(-1, SC_SECO_VERIFY_IMAGE,
92 1 << image_index);
93 if (err) {
94 printf("Authenticate img %d failed, return %d\n",
95 image_index, err);
96 ret = -EIO;
97 }
98
99 err = sc_rm_set_memreg_permissions(-1, mr,
100 SECO_PT, SC_RM_PERM_NONE);
101 if (err) {
102 printf("Remove permission failed for img %d, error %d\n",
103 image_index, err);
104 ret = -EPERM;
105 }
106
107 return ret;
108}
109
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000110static inline bool check_in_dram(ulong addr)
111{
112 int i;
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900113 struct bd_info *bd = gd->bd;
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000114
115 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
116 if (bd->bi_dram[i].size) {
117 if (addr >= bd->bi_dram[i].start &&
118 addr < (bd->bi_dram[i].start + bd->bi_dram[i].size))
119 return true;
120 }
121 }
122
123 return false;
124}
125
126int authenticate_os_container(ulong addr)
127{
128 struct container_hdr *phdr;
129 int i, ret = 0;
130 int err;
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000131 u16 length;
132 struct boot_img_t *img;
133 unsigned long s, e;
Gaurav Jain8ecc0db2023-06-15 18:09:24 +0800134#ifdef CONFIG_ARMV8_CE_SHA256
135 u8 hash_value[SHA256_SUM_LEN];
136#endif
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000137
138 if (addr % 4) {
139 puts("Error: Image's address is not 4 byte aligned\n");
140 return -EINVAL;
141 }
142
143 if (!check_in_dram(addr)) {
144 puts("Error: Image's address is invalid\n");
145 return -EINVAL;
146 }
147
148 phdr = (struct container_hdr *)addr;
Sean Andersonc5126682023-10-14 16:47:43 -0400149 if (!valid_container_hdr(phdr)) {
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000150 printf("Error: Wrong container header\n");
151 return -EFAULT;
152 }
153
154 if (!phdr->num_images) {
155 printf("Error: Wrong container, no image found\n");
156 return -EFAULT;
157 }
158
159 length = phdr->length_lsb + (phdr->length_msb << 8);
160
161 debug("container length %u\n", length);
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000162
Ye Li185af302023-06-15 18:09:23 +0800163 err = ahab_auth_cntr_hdr(phdr, length);
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000164 if (err) {
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000165 ret = -EIO;
166 goto exit;
167 }
168
169 /* Copy images to dest address */
170 for (i = 0; i < phdr->num_images; i++) {
171 img = (struct boot_img_t *)(addr +
172 sizeof(struct container_hdr) +
173 i * sizeof(struct boot_img_t));
174
Oliver Graute8ad653b2020-11-06 09:09:30 +0100175 debug("img %d, dst 0x%x, src 0x%lux, size 0x%x\n",
Seb Fagardc307ad02020-03-30 17:13:31 +0200176 i, (uint32_t) img->dst, img->offset + addr, img->size);
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000177
178 memcpy((void *)img->dst, (const void *)(img->offset + addr),
179 img->size);
180
181 s = img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1);
Peng Fan194dccc2020-04-22 15:36:34 +0800182 e = ALIGN(img->dst + img->size, CONFIG_SYS_CACHELINE_SIZE) - 1;
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000183
184 flush_dcache_range(s, e);
185
Gaurav Jain8ecc0db2023-06-15 18:09:24 +0800186#ifdef CONFIG_ARMV8_CE_SHA256
187 if (((img->hab_flags & AHAB_HASH_TYPE_MASK) >> 8) == AHAB_HASH_TYPE_SHA256) {
188 sha256_csum_wd((void *)img->dst, img->size, hash_value, CHUNKSZ_SHA256);
189 err = memcmp(&img->hash, &hash_value, SHA256_SUM_LEN);
190 if (err) {
191 printf("img %d hash comparison failed, error %d\n", i, err);
192 ret = -EIO;
193 goto exit;
194 }
195 } else {
196#endif
197 ret = ahab_verify_cntr_image(img, i);
198 if (ret)
199 goto exit;
200#ifdef CONFIG_ARMV8_CE_SHA256
201 }
202#endif
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000203 }
204
205exit:
Ye Li185af302023-06-15 18:09:23 +0800206 ahab_auth_release();
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000207
208 return ret;
209}
210
Simon Glassed38aef2020-05-10 11:40:03 -0600211static int do_authenticate(struct cmd_tbl *cmdtp, int flag, int argc,
212 char *const argv[])
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000213{
214 ulong addr;
215
216 if (argc < 2)
217 return CMD_RET_USAGE;
218
Simon Glass3ff49ec2021-07-24 09:03:29 -0600219 addr = hextoul(argv[1], NULL);
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000220
221 printf("Authenticate OS container at 0x%lx\n", addr);
222
223 if (authenticate_os_container(addr))
224 return CMD_RET_FAILURE;
225
226 return CMD_RET_SUCCESS;
227}
228
229static void display_life_cycle(u16 lc)
230{
231 printf("Lifecycle: 0x%04X, ", lc);
232 switch (lc) {
233 case 0x1:
234 printf("Pristine\n\n");
235 break;
236 case 0x2:
237 printf("Fab\n\n");
238 break;
239 case 0x8:
240 printf("Open\n\n");
241 break;
242 case 0x20:
243 printf("NXP closed\n\n");
244 break;
245 case 0x80:
246 printf("OEM closed\n\n");
247 break;
248 case 0x100:
249 printf("Partial field return\n\n");
250 break;
251 case 0x200:
252 printf("Full field return\n\n");
253 break;
254 case 0x400:
255 printf("No return\n\n");
256 break;
257 default:
258 printf("Unknown\n\n");
259 break;
260 }
261}
262
263#define AHAB_AUTH_CONTAINER_REQ 0x87
264#define AHAB_VERIFY_IMAGE_REQ 0x88
265
266#define AHAB_NO_AUTHENTICATION_IND 0xee
267#define AHAB_BAD_KEY_HASH_IND 0xfa
268#define AHAB_INVALID_KEY_IND 0xf9
269#define AHAB_BAD_SIGNATURE_IND 0xf0
270#define AHAB_BAD_HASH_IND 0xf1
271
272static void display_ahab_auth_event(u32 event)
273{
274 u8 cmd = (event >> 16) & 0xff;
275 u8 resp_ind = (event >> 8) & 0xff;
276
277 switch (cmd) {
278 case AHAB_AUTH_CONTAINER_REQ:
279 printf("\tCMD = AHAB_AUTH_CONTAINER_REQ (0x%02X)\n", cmd);
280 printf("\tIND = ");
281 break;
282 case AHAB_VERIFY_IMAGE_REQ:
283 printf("\tCMD = AHAB_VERIFY_IMAGE_REQ (0x%02X)\n", cmd);
284 printf("\tIND = ");
285 break;
286 default:
287 return;
288 }
289
290 switch (resp_ind) {
291 case AHAB_NO_AUTHENTICATION_IND:
292 printf("AHAB_NO_AUTHENTICATION_IND (0x%02X)\n\n", resp_ind);
293 break;
294 case AHAB_BAD_KEY_HASH_IND:
295 printf("AHAB_BAD_KEY_HASH_IND (0x%02X)\n\n", resp_ind);
296 break;
297 case AHAB_INVALID_KEY_IND:
298 printf("AHAB_INVALID_KEY_IND (0x%02X)\n\n", resp_ind);
299 break;
300 case AHAB_BAD_SIGNATURE_IND:
301 printf("AHAB_BAD_SIGNATURE_IND (0x%02X)\n\n", resp_ind);
302 break;
303 case AHAB_BAD_HASH_IND:
304 printf("AHAB_BAD_HASH_IND (0x%02X)\n\n", resp_ind);
305 break;
306 default:
307 printf("Unknown Indicator (0x%02X)\n\n", resp_ind);
308 break;
309 }
310}
311
Simon Glassed38aef2020-05-10 11:40:03 -0600312static int do_ahab_status(struct cmd_tbl *cmdtp, int flag, int argc,
313 char *const argv[])
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000314{
315 int err;
316 u8 idx = 0U;
317 u32 event;
318 u16 lc;
319
320 err = sc_seco_chip_info(-1, &lc, NULL, NULL, NULL);
Peng Fan25b7eb42023-06-15 18:08:58 +0800321 if (err) {
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000322 printf("Error in get lifecycle\n");
323 return -EIO;
324 }
325
326 display_life_cycle(lc);
327
328 err = sc_seco_get_event(-1, idx, &event);
Peng Fan25b7eb42023-06-15 18:08:58 +0800329 while (!err) {
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000330 printf("SECO Event[%u] = 0x%08X\n", idx, event);
331 display_ahab_auth_event(event);
332
333 idx++;
334 err = sc_seco_get_event(-1, idx, &event);
335 }
336
337 if (idx == 0)
338 printf("No SECO Events Found!\n\n");
339
340 return 0;
341}
342
343static int confirm_close(void)
344{
345 puts("Warning: Please ensure your sample is in NXP closed state, "
346 "OEM SRK hash has been fused, \n"
347 " and you are able to boot a signed image successfully "
348 "without any SECO events reported.\n"
349 " If not, your sample will be unrecoverable.\n"
350 "\nReally perform this operation? <y/N>\n");
351
352 if (confirm_yesno())
353 return 1;
354
355 puts("Ahab close aborted\n");
356 return 0;
357}
358
Simon Glassed38aef2020-05-10 11:40:03 -0600359static int do_ahab_close(struct cmd_tbl *cmdtp, int flag, int argc,
360 char *const argv[])
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000361{
Clément Pérone61e6bc2020-12-21 18:31:28 +0100362 int confirmed = argc >= 2 && !strcmp(argv[1], "-y");
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000363 int err;
364 u16 lc;
365
Clément Pérone61e6bc2020-12-21 18:31:28 +0100366 if (!confirmed && !confirm_close())
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000367 return -EACCES;
368
369 err = sc_seco_chip_info(-1, &lc, NULL, NULL, NULL);
Peng Fan25b7eb42023-06-15 18:08:58 +0800370 if (err) {
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000371 printf("Error in get lifecycle\n");
372 return -EIO;
373 }
374
375 if (lc != 0x20) {
376 puts("Current lifecycle is NOT NXP closed, can't move to OEM closed\n");
377 display_life_cycle(lc);
378 return -EPERM;
379 }
380
381 err = sc_seco_forward_lifecycle(-1, 16);
Peng Fan25b7eb42023-06-15 18:08:58 +0800382 if (err) {
Peng Fanf1e0f9f2019-09-25 08:11:14 +0000383 printf("Error in forward lifecycle to OEM closed\n");
384 return -EIO;
385 }
386
387 printf("Change to OEM closed successfully\n");
388
389 return 0;
390}
391
392U_BOOT_CMD(auth_cntr, CONFIG_SYS_MAXARGS, 1, do_authenticate,
393 "autenticate OS container via AHAB",
394 "addr\n"
395 "addr - OS container hex address\n"
396);
397
398U_BOOT_CMD(ahab_status, CONFIG_SYS_MAXARGS, 1, do_ahab_status,
399 "display AHAB lifecycle and events from seco",
400 ""
401);
402
403U_BOOT_CMD(ahab_close, CONFIG_SYS_MAXARGS, 1, do_ahab_close,
404 "Change AHAB lifecycle to OEM closed",
405 ""
406);