blob: c13d9f0e00e3957a688534f3d869f3ac9f05badc [file] [log] [blame]
Ye Li1c610742022-07-26 16:40:55 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2022 NXP
4 */
5
Ye Li1c610742022-07-26 16:40:55 +08006#include <command.h>
7#include <errno.h>
Sean Anderson952ed672023-10-14 16:47:44 -04008#include <imx_container.h>
Ye Li1c610742022-07-26 16:40:55 +08009#include <asm/io.h>
Peng Fand5c31832023-06-15 18:09:05 +080010#include <asm/mach-imx/ele_api.h>
Ye Li1c610742022-07-26 16:40:55 +080011#include <asm/mach-imx/sys_proto.h>
12#include <asm/arch-imx/cpu.h>
13#include <asm/arch/sys_proto.h>
Ye Li1c610742022-07-26 16:40:55 +080014#include <console.h>
15#include <cpu_func.h>
Ye Li1c610742022-07-26 16:40:55 +080016#include <asm/global_data.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
Ye Li1c610742022-07-26 16:40:55 +080020#define IMG_CONTAINER_END_BASE (IMG_CONTAINER_BASE + 0xFFFFUL)
21
Ye Lic9e34362023-01-30 18:39:51 +080022#define AHAB_MAX_EVENTS 8
23
24static char *ele_ipc_str[] = {
25 "IPC = MU RTD (0x1)\n",
26 "IPC = MU APD (0x2)\n",
27 "IPC = INVALID\n",
28 NULL
29};
30
31static char *ele_status_str[] = {
32 "STA = ELE_SUCCESS_IND (0xD6)\n",
33 "STA = ELE_FAILURE_IND (0x29)\n",
34 "STA = INVALID\n",
35 NULL
36};
37
38static char *ele_cmd_str[] = {
39 "CMD = ELE_PING_REQ (0x01)\n",
40 "CMD = ELE_FW_AUTH_REQ (0x02)\n",
41 "CMD = ELE_RESTART_RST_TIMER_REQ (0x04)\n",
42 "CMD = ELE_DUMP_DEBUG_BUFFER_REQ (0x21)\n",
43 "CMD = ELE_OEM_CNTN_AUTH_REQ (0x87)\n",
44 "CMD = ELE_VERIFY_IMAGE_REQ (0x88)\n",
45 "CMD = ELE_RELEASE_CONTAINER_REQ (0x89)\n",
46 "CMD = ELE_WRITE_SECURE_FUSE_REQ (0x91)\n",
47 "CMD = ELE_FWD_LIFECYCLE_UP_REQ (0x95)\n",
48 "CMD = ELE_READ_FUSE_REQ (0x97)\n",
49 "CMD = ELE_GET_FW_VERSION_REQ (0x9D)\n",
50 "CMD = ELE_RET_LIFECYCLE_UP_REQ (0xA0)\n",
51 "CMD = ELE_GET_EVENTS_REQ (0xA2)\n",
52 "CMD = ELE_ENABLE_PATCH_REQ (0xC3)\n",
53 "CMD = ELE_RELEASE_RDC_REQ (0xC4)\n",
54 "CMD = ELE_GET_FW_STATUS_REQ (0xC5)\n",
55 "CMD = ELE_ENABLE_OTFAD_REQ (0xC6)\n",
56 "CMD = ELE_RESET_REQ (0xC7)\n",
57 "CMD = ELE_UPDATE_OTP_CLKDIV_REQ (0xD0)\n",
58 "CMD = ELE_POWER_DOWN_REQ (0xD1)\n",
59 "CMD = ELE_ENABLE_APC_REQ (0xD2)\n",
60 "CMD = ELE_ENABLE_RTC_REQ (0xD3)\n",
61 "CMD = ELE_DEEP_POWER_DOWN_REQ (0xD4)\n",
62 "CMD = ELE_STOP_RST_TIMER_REQ (0xD5)\n",
63 "CMD = ELE_WRITE_FUSE_REQ (0xD6)\n",
64 "CMD = ELE_RELEASE_CAAM_REQ (0xD7)\n",
65 "CMD = ELE_RESET_A35_CTX_REQ (0xD8)\n",
66 "CMD = ELE_MOVE_TO_UNSECURED_REQ (0xD9)\n",
67 "CMD = ELE_GET_INFO_REQ (0xDA)\n",
68 "CMD = ELE_ATTEST_REQ (0xDB)\n",
69 "CMD = ELE_RELEASE_PATCH_REQ (0xDC)\n",
70 "CMD = ELE_OTP_SEQ_SWITH_REQ (0xDD)\n",
71 "CMD = INVALID\n",
72 NULL
73};
74
75static char *ele_ind_str[] = {
76 "IND = ELE_ROM_PING_FAILURE_IND (0x0A)\n",
77 "IND = ELE_FW_PING_FAILURE_IND (0x1A)\n",
78 "IND = ELE_BAD_SIGNATURE_FAILURE_IND (0xF0)\n",
79 "IND = ELE_BAD_HASH_FAILURE_IND (0xF1)\n",
80 "IND = ELE_INVALID_LIFECYCLE_IND (0xF2)\n",
81 "IND = ELE_PERMISSION_DENIED_FAILURE_IND (0xF3)\n",
82 "IND = ELE_INVALID_MESSAGE_FAILURE_IND (0xF4)\n",
83 "IND = ELE_BAD_VALUE_FAILURE_IND (0xF5)\n",
84 "IND = ELE_BAD_FUSE_ID_FAILURE_IND (0xF6)\n",
85 "IND = ELE_BAD_CONTAINER_FAILURE_IND (0xF7)\n",
86 "IND = ELE_BAD_VERSION_FAILURE_IND (0xF8)\n",
87 "IND = ELE_INVALID_KEY_FAILURE_IND (0xF9)\n",
88 "IND = ELE_BAD_KEY_HASH_FAILURE_IND (0xFA)\n",
89 "IND = ELE_NO_VALID_CONTAINER_FAILURE_IND (0xFB)\n",
90 "IND = ELE_BAD_CERTIFICATE_FAILURE_IND (0xFC)\n",
91 "IND = ELE_BAD_UID_FAILURE_IND (0xFD)\n",
92 "IND = ELE_BAD_MONOTONIC_COUNTER_FAILURE_IND (0xFE)\n",
93 "IND = ELE_MUST_SIGNED_FAILURE_IND (0xE0)\n",
94 "IND = ELE_NO_AUTHENTICATION_FAILURE_IND (0xEE)\n",
95 "IND = ELE_BAD_SRK_SET_FAILURE_IND (0xEF)\n",
96 "IND = ELE_UNALIGNED_PAYLOAD_FAILURE_IND (0xA6)\n",
97 "IND = ELE_WRONG_SIZE_FAILURE_IND (0xA7)\n",
98 "IND = ELE_ENCRYPTION_FAILURE_IND (0xA8)\n",
99 "IND = ELE_DECRYPTION_FAILURE_IND (0xA9)\n",
100 "IND = ELE_OTP_PROGFAIL_FAILURE_IND (0xAA)\n",
101 "IND = ELE_OTP_LOCKED_FAILURE_IND (0xAB)\n",
102 "IND = ELE_OTP_INVALID_IDX_FAILURE_IND (0xAD)\n",
103 "IND = ELE_TIME_OUT_FAILURE_IND (0xB0)\n",
104 "IND = ELE_BAD_PAYLOAD_FAILURE_IND (0xB1)\n",
105 "IND = ELE_WRONG_ADDRESS_FAILURE_IND (0xB4)\n",
106 "IND = ELE_DMA_FAILURE_IND (0xB5)\n",
107 "IND = ELE_DISABLED_FEATURE_FAILURE_IND (0xB6)\n",
108 "IND = ELE_MUST_ATTEST_FAILURE_IND (0xB7)\n",
109 "IND = ELE_RNG_NOT_STARTED_FAILURE_IND (0xB8)\n",
110 "IND = ELE_CRC_ERROR_IND (0xB9)\n",
111 "IND = ELE_AUTH_SKIPPED_OR_FAILED_FAILURE_IND (0xBB)\n",
112 "IND = ELE_INCONSISTENT_PAR_FAILURE_IND (0xBC)\n",
113 "IND = ELE_RNG_INST_FAILURE_FAILURE_IND (0xBD)\n",
114 "IND = ELE_LOCKED_REG_FAILURE_IND (0xBE)\n",
115 "IND = ELE_BAD_ID_FAILURE_IND (0xBF)\n",
116 "IND = ELE_INVALID_OPERATION_FAILURE_IND (0xC0)\n",
117 "IND = ELE_NON_SECURE_STATE_FAILURE_IND (0xC1)\n",
118 "IND = ELE_MSG_TRUNCATED_IND (0xC2)\n",
119 "IND = ELE_BAD_IMAGE_NUM_FAILURE_IND (0xC3)\n",
120 "IND = ELE_BAD_IMAGE_ADDR_FAILURE_IND (0xC4)\n",
121 "IND = ELE_BAD_IMAGE_PARAM_FAILURE_IND (0xC5)\n",
122 "IND = ELE_BAD_IMAGE_TYPE_FAILURE_IND (0xC6)\n",
123 "IND = ELE_CORRUPTED_SRK_FAILURE_IND (0xD0)\n",
124 "IND = ELE_OUT_OF_MEMORY_IND (0xD1)\n",
125 "IND = ELE_CSTM_FAILURE_IND (0xCF)\n",
126 "IND = ELE_OLD_VERSION_FAILURE_IND (0xCE)\n",
127 "IND = ELE_WRONG_BOOT_MODE_FAILURE_IND (0xCD)\n",
128 "IND = ELE_APC_ALREADY_ENABLED_FAILURE_IND (0xCB)\n",
129 "IND = ELE_RTC_ALREADY_ENABLED_FAILURE_IND (0xCC)\n",
130 "IND = ELE_ABORT_IND (0xFF)\n",
131 "IND = INVALID\n",
132 NULL
133};
134
135static u8 ele_cmd[] = {
136 ELE_PING_REQ,
137 ELE_FW_AUTH_REQ,
138 ELE_RESTART_RST_TIMER_REQ,
139 ELE_DUMP_DEBUG_BUFFER_REQ,
140 ELE_OEM_CNTN_AUTH_REQ,
141 ELE_VERIFY_IMAGE_REQ,
142 ELE_RELEASE_CONTAINER_REQ,
143 ELE_WRITE_SECURE_FUSE_REQ,
144 ELE_FWD_LIFECYCLE_UP_REQ,
145 ELE_READ_FUSE_REQ,
146 ELE_GET_FW_VERSION_REQ,
147 ELE_RET_LIFECYCLE_UP_REQ,
148 ELE_GET_EVENTS_REQ,
149 ELE_ENABLE_PATCH_REQ,
150 ELE_RELEASE_RDC_REQ,
151 ELE_GET_FW_STATUS_REQ,
152 ELE_ENABLE_OTFAD_REQ,
153 ELE_RESET_REQ,
154 ELE_UPDATE_OTP_CLKDIV_REQ,
155 ELE_POWER_DOWN_REQ,
156 ELE_ENABLE_APC_REQ,
157 ELE_ENABLE_RTC_REQ,
158 ELE_DEEP_POWER_DOWN_REQ,
159 ELE_STOP_RST_TIMER_REQ,
160 ELE_WRITE_FUSE_REQ,
161 ELE_RELEASE_CAAM_REQ,
162 ELE_RESET_A35_CTX_REQ,
163 ELE_MOVE_TO_UNSECURED_REQ,
164 ELE_GET_INFO_REQ,
165 ELE_ATTEST_REQ,
166 ELE_RELEASE_PATCH_REQ,
167 ELE_OTP_SEQ_SWITH_REQ
168};
169
170static u8 ele_ind[] = {
171 ELE_ROM_PING_FAILURE_IND,
172 ELE_FW_PING_FAILURE_IND,
173 ELE_BAD_SIGNATURE_FAILURE_IND,
174 ELE_BAD_HASH_FAILURE_IND,
175 ELE_INVALID_LIFECYCLE_IND,
176 ELE_PERMISSION_DENIED_FAILURE_IND,
177 ELE_INVALID_MESSAGE_FAILURE_IND,
178 ELE_BAD_VALUE_FAILURE_IND,
179 ELE_BAD_FUSE_ID_FAILURE_IND,
180 ELE_BAD_CONTAINER_FAILURE_IND,
181 ELE_BAD_VERSION_FAILURE_IND,
182 ELE_INVALID_KEY_FAILURE_IND,
183 ELE_BAD_KEY_HASH_FAILURE_IND,
184 ELE_NO_VALID_CONTAINER_FAILURE_IND,
185 ELE_BAD_CERTIFICATE_FAILURE_IND,
186 ELE_BAD_UID_FAILURE_IND,
187 ELE_BAD_MONOTONIC_COUNTER_FAILURE_IND,
188 ELE_MUST_SIGNED_FAILURE_IND,
189 ELE_NO_AUTHENTICATION_FAILURE_IND,
190 ELE_BAD_SRK_SET_FAILURE_IND,
191 ELE_UNALIGNED_PAYLOAD_FAILURE_IND,
192 ELE_WRONG_SIZE_FAILURE_IND,
193 ELE_ENCRYPTION_FAILURE_IND,
194 ELE_DECRYPTION_FAILURE_IND,
195 ELE_OTP_PROGFAIL_FAILURE_IND,
196 ELE_OTP_LOCKED_FAILURE_IND,
197 ELE_OTP_INVALID_IDX_FAILURE_IND,
198 ELE_TIME_OUT_FAILURE_IND,
199 ELE_BAD_PAYLOAD_FAILURE_IND,
200 ELE_WRONG_ADDRESS_FAILURE_IND,
201 ELE_DMA_FAILURE_IND,
202 ELE_DISABLED_FEATURE_FAILURE_IND,
203 ELE_MUST_ATTEST_FAILURE_IND,
204 ELE_RNG_NOT_STARTED_FAILURE_IND,
205 ELE_CRC_ERROR_IND,
206 ELE_AUTH_SKIPPED_OR_FAILED_FAILURE_IND,
207 ELE_INCONSISTENT_PAR_FAILURE_IND,
208 ELE_RNG_INST_FAILURE_FAILURE_IND,
209 ELE_LOCKED_REG_FAILURE_IND,
210 ELE_BAD_ID_FAILURE_IND,
211 ELE_INVALID_OPERATION_FAILURE_IND,
212 ELE_NON_SECURE_STATE_FAILURE_IND,
213 ELE_MSG_TRUNCATED_IND,
214 ELE_BAD_IMAGE_NUM_FAILURE_IND,
215 ELE_BAD_IMAGE_ADDR_FAILURE_IND,
216 ELE_BAD_IMAGE_PARAM_FAILURE_IND,
217 ELE_BAD_IMAGE_TYPE_FAILURE_IND,
218 ELE_CORRUPTED_SRK_FAILURE_IND,
219 ELE_OUT_OF_MEMORY_IND,
220 ELE_CSTM_FAILURE_IND,
221 ELE_OLD_VERSION_FAILURE_IND,
222 ELE_WRONG_BOOT_MODE_FAILURE_IND,
223 ELE_APC_ALREADY_ENABLED_FAILURE_IND,
224 ELE_RTC_ALREADY_ENABLED_FAILURE_IND,
225 ELE_ABORT_IND
226};
227
228static u8 ele_ipc[] = {
229 ELE_IPC_MU_RTD,
230 ELE_IPC_MU_APD
231};
232
233static u8 ele_status[] = {
234 ELE_SUCCESS_IND,
235 ELE_FAILURE_IND
236};
237
238static inline u32 get_idx(u8 *list, u8 tgt, u32 size)
239{
240 u32 i;
241
242 for (i = 0; i < size; i++) {
243 if (list[i] == tgt)
244 return i;
245 }
246
247 return i; /* last str is invalid */
248}
Ye Li1c610742022-07-26 16:40:55 +0800249
250static void display_ahab_auth_ind(u32 event)
251{
252 u8 resp_ind = (event >> 8) & 0xff;
253
Ye Lic9e34362023-01-30 18:39:51 +0800254 printf("%s\n", ele_ind_str[get_idx(ele_ind, resp_ind, ARRAY_SIZE(ele_ind))]);
Ye Li1c610742022-07-26 16:40:55 +0800255}
256
257int ahab_auth_cntr_hdr(struct container_hdr *container, u16 length)
258{
259 int err;
260 u32 resp;
261
262 memcpy((void *)IMG_CONTAINER_BASE, (const void *)container,
263 ALIGN(length, CONFIG_SYS_CACHELINE_SIZE));
264
265 flush_dcache_range(IMG_CONTAINER_BASE,
266 IMG_CONTAINER_BASE + ALIGN(length, CONFIG_SYS_CACHELINE_SIZE) - 1);
267
Peng Fand5c31832023-06-15 18:09:05 +0800268 err = ele_auth_oem_ctnr(IMG_CONTAINER_BASE, &resp);
Ye Li1c610742022-07-26 16:40:55 +0800269 if (err) {
270 printf("Authenticate container hdr failed, return %d, resp 0x%x\n",
271 err, resp);
272 display_ahab_auth_ind(resp);
273 }
274
275 return err;
276}
277
278int ahab_auth_release(void)
279{
280 int err;
281 u32 resp;
282
Peng Fand5c31832023-06-15 18:09:05 +0800283 err = ele_release_container(&resp);
Ye Li1c610742022-07-26 16:40:55 +0800284 if (err) {
285 printf("Error: release container failed, resp 0x%x!\n", resp);
286 display_ahab_auth_ind(resp);
287 }
288
289 return err;
290}
291
292int ahab_verify_cntr_image(struct boot_img_t *img, int image_index)
293{
294 int err;
295 u32 resp;
296
Peng Fand5c31832023-06-15 18:09:05 +0800297 err = ele_verify_image(image_index, &resp);
Ye Li1c610742022-07-26 16:40:55 +0800298 if (err) {
299 printf("Authenticate img %d failed, return %d, resp 0x%x\n",
300 image_index, err, resp);
301 display_ahab_auth_ind(resp);
302
303 return -EIO;
304 }
305
306 return 0;
307}
308
309static inline bool check_in_dram(ulong addr)
310{
311 int i;
312 struct bd_info *bd = gd->bd;
313
314 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
315 if (bd->bi_dram[i].size) {
316 if (addr >= bd->bi_dram[i].start &&
317 addr < (bd->bi_dram[i].start + bd->bi_dram[i].size))
318 return true;
319 }
320 }
321
322 return false;
323}
324
325int authenticate_os_container(ulong addr)
326{
327 struct container_hdr *phdr;
328 int i, ret = 0;
329 int err;
330 u16 length;
331 struct boot_img_t *img;
332 unsigned long s, e;
333
334 if (addr % 4) {
335 puts("Error: Image's address is not 4 byte aligned\n");
336 return -EINVAL;
337 }
338
339 if (!check_in_dram(addr)) {
340 puts("Error: Image's address is invalid\n");
341 return -EINVAL;
342 }
343
344 phdr = (struct container_hdr *)addr;
Sean Andersonc5126682023-10-14 16:47:43 -0400345 if (!valid_container_hdr(phdr)) {
Ye Li1c610742022-07-26 16:40:55 +0800346 printf("Error: Wrong container header\n");
347 return -EFAULT;
348 }
349
350 if (!phdr->num_images) {
351 printf("Error: Wrong container, no image found\n");
352 return -EFAULT;
353 }
354
355 length = phdr->length_lsb + (phdr->length_msb << 8);
356
357 debug("container length %u\n", length);
358
359 err = ahab_auth_cntr_hdr(phdr, length);
360 if (err) {
361 ret = -EIO;
362 goto exit;
363 }
364
365 debug("Verify images\n");
366
367 /* Copy images to dest address */
368 for (i = 0; i < phdr->num_images; i++) {
369 img = (struct boot_img_t *)(addr +
370 sizeof(struct container_hdr) +
371 i * sizeof(struct boot_img_t));
372
373 debug("img %d, dst 0x%x, src 0x%lx, size 0x%x\n",
374 i, (uint32_t)img->dst, img->offset + addr, img->size);
375
376 memcpy((void *)img->dst, (const void *)(img->offset + addr),
377 img->size);
378
379 s = img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1);
380 e = ALIGN(img->dst + img->size, CONFIG_SYS_CACHELINE_SIZE) - 1;
381
382 flush_dcache_range(s, e);
383
384 ret = ahab_verify_cntr_image(img, i);
385 if (ret)
386 goto exit;
387 }
388
389exit:
390 debug("ahab_auth_release, 0x%x\n", ret);
391 ahab_auth_release();
392
393 return ret;
394}
395
396static int do_authenticate(struct cmd_tbl *cmdtp, int flag, int argc,
397 char *const argv[])
398{
399 ulong addr;
400
401 if (argc < 2)
402 return CMD_RET_USAGE;
403
Peng Fan786b90a2023-06-15 18:09:07 +0800404 addr = hextoul(argv[1], NULL);
Ye Li1c610742022-07-26 16:40:55 +0800405
406 printf("Authenticate OS container at 0x%lx\n", addr);
407
408 if (authenticate_os_container(addr))
409 return CMD_RET_FAILURE;
410
411 return CMD_RET_SUCCESS;
412}
413
414static void display_life_cycle(u32 lc)
415{
416 printf("Lifecycle: 0x%08X, ", lc);
417 switch (lc) {
418 case 0x1:
419 printf("BLANK\n\n");
420 break;
421 case 0x2:
422 printf("FAB\n\n");
423 break;
424 case 0x4:
425 printf("NXP Provisioned\n\n");
426 break;
427 case 0x8:
428 printf("OEM Open\n\n");
429 break;
Ye Li1c610742022-07-26 16:40:55 +0800430 case 0x20:
431 printf("OEM closed\n\n");
432 break;
433 case 0x40:
434 printf("Field Return OEM\n\n");
435 break;
436 case 0x80:
437 printf("Field Return NXP\n\n");
438 break;
439 case 0x100:
440 printf("OEM Locked\n\n");
441 break;
442 case 0x200:
443 printf("BRICKED\n\n");
444 break;
445 default:
446 printf("Unknown\n\n");
447 break;
448 }
449}
450
451static int confirm_close(void)
452{
453 puts("Warning: Please ensure your sample is in NXP closed state, "
454 "OEM SRK hash has been fused, \n"
455 " and you are able to boot a signed image successfully "
456 "without any SECO events reported.\n"
457 " If not, your sample will be unrecoverable.\n"
458 "\nReally perform this operation? <y/N>\n");
459
460 if (confirm_yesno())
461 return 1;
462
463 puts("Ahab close aborted\n");
464 return 0;
465}
466
467static int do_ahab_close(struct cmd_tbl *cmdtp, int flag, int argc,
468 char *const argv[])
469{
470 int err;
471 u32 resp;
Ye Li6579e522023-01-30 18:39:54 +0800472 u32 lc;
Ye Li1c610742022-07-26 16:40:55 +0800473
474 if (!confirm_close())
475 return -EACCES;
476
Ye Li6579e522023-01-30 18:39:54 +0800477 lc = readl(FSB_BASE_ADDR + 0x41c);
478 lc &= 0x3ff;
479
480 if (lc != 0x8) {
481 puts("Current lifecycle is NOT OEM open, can't move to OEM closed\n");
482 display_life_cycle(lc);
483 return -EPERM;
484 }
485
Peng Fand5c31832023-06-15 18:09:05 +0800486 err = ele_forward_lifecycle(8, &resp);
Ye Li1c610742022-07-26 16:40:55 +0800487 if (err != 0) {
488 printf("Error in forward lifecycle to OEM closed\n");
489 return -EIO;
490 }
491
492 printf("Change to OEM closed successfully\n");
493
494 return 0;
495}
496
497int ahab_dump(void)
498{
499 u32 buffer[32];
500 int ret, i = 0;
501
502 do {
Peng Fand5c31832023-06-15 18:09:05 +0800503 ret = ele_dump_buffer(buffer, 32);
Ye Li1c610742022-07-26 16:40:55 +0800504 if (ret < 0) {
505 printf("Error in dump AHAB log\n");
506 return -EIO;
507 }
508
509 if (ret == 1)
510 break;
511 for (i = 0; i < ret; i++)
512 printf("0x%x\n", buffer[i]);
513 } while (ret >= 21);
514
515 return 0;
516}
517
518static int do_ahab_dump(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
519{
520 return ahab_dump();
521}
522
Ye Lic9e34362023-01-30 18:39:51 +0800523static void display_event(u32 event)
524{
525 printf("\n\t0x%08x\n", event);
526 printf("\t%s", ele_ipc_str[get_idx(ele_ipc,
527 (event >> 24) & 0xFF, ARRAY_SIZE(ele_ipc))]);
528 printf("\t%s", ele_cmd_str[get_idx(ele_cmd,
529 (event >> 16) & 0xFF, ARRAY_SIZE(ele_cmd))]);
530 printf("\t%s", ele_ind_str[get_idx(ele_ind,
531 (event >> 8) & 0xFF, ARRAY_SIZE(ele_ind))]);
532 printf("\t%s", ele_status_str[get_idx(ele_status,
533 event & 0xFF, ARRAY_SIZE(ele_status))]);
534}
535
Ye Li1c610742022-07-26 16:40:55 +0800536static int do_ahab_status(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
537{
Ye Lic9e34362023-01-30 18:39:51 +0800538 u32 lc, i;
539 u32 events[AHAB_MAX_EVENTS];
540 u32 cnt = AHAB_MAX_EVENTS;
541 int ret;
Ye Li1c610742022-07-26 16:40:55 +0800542
543 lc = readl(FSB_BASE_ADDR + 0x41c);
544 lc &= 0x3ff;
545
546 display_life_cycle(lc);
Ye Lic9e34362023-01-30 18:39:51 +0800547
Peng Fand5c31832023-06-15 18:09:05 +0800548 ret = ele_get_events(events, &cnt, NULL);
Ye Lic9e34362023-01-30 18:39:51 +0800549 if (ret) {
550 printf("Get ELE EVENTS error %d\n", ret);
551 return CMD_RET_FAILURE;
552 }
553
554 if (!cnt) {
555 puts("\n\tNo Events Found!\n");
556 return 0;
557 }
558
559 for (i = 0; i < cnt; i++)
560 display_event(events[i]);
561
Ye Li1c610742022-07-26 16:40:55 +0800562 return 0;
563}
564
Peng Fanc88036d2023-06-15 18:09:08 +0800565static int do_sec_fuse_prog(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
566{
567 ulong addr;
568 u32 header, response;
569
570 if (argc < 2)
571 return CMD_RET_USAGE;
572
573 addr = hextoul(argv[1], NULL);
574 header = *(u32 *)addr;
575
576 if ((header & 0xff0000ff) != 0x89000000) {
577 printf("Wrong Signed message block format, header 0x%x\n", header);
578 return CMD_RET_FAILURE;
579 }
580
581 header = (header & 0xffff00) >> 8;
582
583 printf("Signed Message block at 0x%lx, size 0x%x\n", addr, header);
584 flush_dcache_range(addr, addr + header - 1);
585
586 if (ele_write_secure_fuse(addr, &response)) {
587 printf("Program secure fuse failed, response 0x%x\n", response);
588 return CMD_RET_FAILURE;
589 }
590
591 printf("Program secure fuse completed, response 0x%x\n", response);
592
593 return CMD_RET_SUCCESS;
594}
595
596static int do_ahab_return_lifecycle(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
597{
598 ulong addr;
599 u32 header, response;
600
601 if (argc < 2)
602 return CMD_RET_USAGE;
603
604 addr = hextoul(argv[1], NULL);
605 header = *(u32 *)addr;
606
607 if ((header & 0xff0000ff) != 0x89000000) {
608 printf("Wrong Signed message block format, header 0x%x\n", header);
609 return CMD_RET_FAILURE;
610 }
611
612 header = (header & 0xffff00) >> 8;
613
614 printf("Signed Message block at 0x%lx, size 0x%x\n", addr, header);
615 flush_dcache_range(addr, addr + header - 1);
616
617 if (ele_return_lifecycle_update(addr, &response)) {
618 printf("Return lifecycle failed, response 0x%x\n", response);
619 return CMD_RET_FAILURE;
620 }
621
622 printf("Return lifecycle completed, response 0x%x\n", response);
623
624 return CMD_RET_SUCCESS;
625}
626
Mathieu Othacehe8df8e592024-03-21 08:19:53 +0100627static int do_ahab_commit(struct cmd_tbl *cmdtp, int flag, int argc,
628 char *const argv[])
629{
630 u32 index;
631 u32 resp;
632 u32 info_type;
633
634 if (argc < 2)
635 return CMD_RET_USAGE;
636
637 index = simple_strtoul(argv[1], NULL, 16);
638 printf("Commit index is 0x%x\n", index);
639
640 if (ele_commit(index, &resp, &info_type)) {
641 printf("Error in AHAB commit\n");
642 return -EIO;
643 }
644
645 printf("Ahab commit succeeded. Information type is 0x%x\n", info_type);
646
647 return 0;
648}
649
Ye Li1c610742022-07-26 16:40:55 +0800650U_BOOT_CMD(auth_cntr, CONFIG_SYS_MAXARGS, 1, do_authenticate,
651 "autenticate OS container via AHAB",
652 "addr\n"
653 "addr - OS container hex address\n"
654);
655
656U_BOOT_CMD(ahab_close, CONFIG_SYS_MAXARGS, 1, do_ahab_close,
657 "Change AHAB lifecycle to OEM closed",
658 ""
659);
660
661U_BOOT_CMD(ahab_dump, CONFIG_SYS_MAXARGS, 1, do_ahab_dump,
662 "Dump AHAB log for debug",
663 ""
664);
665
666U_BOOT_CMD(ahab_status, CONFIG_SYS_MAXARGS, 1, do_ahab_status,
667 "display AHAB lifecycle only",
668 ""
669);
Peng Fanc88036d2023-06-15 18:09:08 +0800670
671U_BOOT_CMD(ahab_sec_fuse_prog, CONFIG_SYS_MAXARGS, 1, do_sec_fuse_prog,
672 "Program secure fuse via signed message block",
673 "addr\n"
674 "addr - Signed message block for secure fuse\n"
675);
676
677U_BOOT_CMD(ahab_return_lifecycle, CONFIG_SYS_MAXARGS, 1, do_ahab_return_lifecycle,
678 "Return lifecycle to OEM field return via signed message block",
679 "addr\n"
680 "addr - Return lifecycle message block signed by OEM SRK\n"
681);
Mathieu Othacehe8df8e592024-03-21 08:19:53 +0100682
683U_BOOT_CMD(ahab_commit, CONFIG_SYS_MAXARGS, 1, do_ahab_commit,
684 "commit into the fuses any new SRK revocation and FW version information\n"
685 "that have been found into the NXP (ELE FW) and OEM containers",
686 ""
687);