2019-09-25 08:11:14 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
2023-06-15 10:09:24 +00:00
|
|
|
* Copyright 2018-2019, 2022 NXP
|
2019-09-25 08:11:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-05-10 17:40:03 +00:00
|
|
|
#include <command.h>
|
2019-09-25 08:11:14 +00:00
|
|
|
#include <errno.h>
|
arm: imx: Check header before calling spl_load_imx_container
Make sure we have an IMX header before calling spl_load_imx_container,
since if we don't it will fail with -ENOENT. This allows us to fall back to
legacy/raw images if they are also enabled.
This is a functional change, one which likely should have been in place
from the start, but a functional change nonetheless. Previously, all
non-IMX8 images (except FITs without FIT_FULL) would be optimized out if
the only image load method enabled supported IMX8 images. With this change,
support for other image types now has an effect.
There are seven boards with SPL_LOAD_IMX_CONTAINER enabled: three with
SPL_BOOTROM_SUPPORT:
imx93_11x11_evk_ld imx93_11x11_evk imx8ulp_evk
and four with SPL_MMC:
deneb imx8qxp_mek giedi imx8qm_mek
All of these boards also have SPL_RAW_IMAGE_SUPPORT and
SPL_LEGACY_IMAGE_FORMAT enabled as well. However, none have FIT support
enabled. Of the six load methods affected by this patch, only SPL_MMC and
SPL_BOOTROM_SUPPORT are enabled with SPL_LOAD_IMX_CONTAINER.
spl_romapi_load_image_seekable does not support legacy or raw images, so
there is no growth. However, mmc_load_image_raw_sector does support loading
legacy/raw images. Since these images could not have been booted before, I
have disabled support for legacy/raw images on these four boards. This
reduces bloat from around 800 bytes to around 200.
There are no in-tree boards with SPL_LOAD_IMX_CONTAINER and AHAB_BOOT both
enabled, so we do not need to worry about potentially falling back to
legacy images in a secure boot scenario.
Future work could include merging imx_container.h with imx8image.h, since
they appear to define mostly the same structures.
Signed-off-by: Sean Anderson <seanga2@gmail.com>
2023-10-14 20:47:44 +00:00
|
|
|
#include <imx_container.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2019-09-25 08:11:14 +00:00
|
|
|
#include <asm/io.h>
|
2023-04-28 04:08:09 +00:00
|
|
|
#include <firmware/imx/sci/sci.h>
|
2019-09-25 08:11:14 +00:00
|
|
|
#include <asm/mach-imx/sys_proto.h>
|
|
|
|
#include <asm/arch-imx/cpu.h>
|
|
|
|
#include <asm/arch/sys_proto.h>
|
|
|
|
#include <console.h>
|
2020-11-06 08:09:29 +00:00
|
|
|
#include <cpu_func.h>
|
2023-06-15 10:09:24 +00:00
|
|
|
#include "u-boot/sha256.h"
|
2023-06-15 10:09:23 +00:00
|
|
|
#include <asm/mach-imx/ahab.h>
|
2019-09-25 08:11:14 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
#define SEC_SECURE_RAM_BASE (0x31800000UL)
|
|
|
|
#define SEC_SECURE_RAM_END_BASE (SEC_SECURE_RAM_BASE + 0xFFFFUL)
|
|
|
|
#define SECO_LOCAL_SEC_SEC_SECURE_RAM_BASE (0x60000000UL)
|
|
|
|
|
|
|
|
#define SECO_PT 2U
|
2023-06-15 10:09:24 +00:00
|
|
|
#define AHAB_HASH_TYPE_MASK 0x00000700
|
|
|
|
#define AHAB_HASH_TYPE_SHA256 0
|
2019-09-25 08:11:14 +00:00
|
|
|
|
2023-06-15 10:09:23 +00:00
|
|
|
int ahab_auth_cntr_hdr(struct container_hdr *container, u16 length)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
memcpy((void *)SEC_SECURE_RAM_BASE, (const void *)container,
|
|
|
|
ALIGN(length, CONFIG_SYS_CACHELINE_SIZE));
|
|
|
|
|
|
|
|
err = sc_seco_authenticate(-1, SC_SECO_AUTH_CONTAINER,
|
|
|
|
SECO_LOCAL_SEC_SEC_SECURE_RAM_BASE);
|
|
|
|
if (err)
|
|
|
|
printf("Authenticate container hdr failed, return %d\n", err);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ahab_auth_release(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = sc_seco_authenticate(-1, SC_SECO_REL_CONTAINER, 0);
|
|
|
|
if (err)
|
|
|
|
printf("Error: release container failed!\n");
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ahab_verify_cntr_image(struct boot_img_t *img, int image_index)
|
|
|
|
{
|
|
|
|
sc_faddr_t start, end;
|
|
|
|
sc_rm_mr_t mr;
|
|
|
|
int err;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
debug("img %d, dst 0x%llx, src 0x%x, size 0x%x\n",
|
|
|
|
image_index, img->dst, img->offset, img->size);
|
|
|
|
|
|
|
|
/* Find the memreg and set permission for seco pt */
|
|
|
|
err = sc_rm_find_memreg(-1, &mr,
|
|
|
|
img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1),
|
|
|
|
ALIGN(img->dst + img->size, CONFIG_SYS_CACHELINE_SIZE) - 1);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
printf("Error: can't find memreg for image load address 0x%llx, error %d\n",
|
|
|
|
img->dst, err);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sc_rm_get_memreg_info(-1, mr, &start, &end);
|
|
|
|
if (!err)
|
|
|
|
debug("memreg %u 0x%llx -- 0x%llx\n", mr, start, end);
|
|
|
|
|
|
|
|
err = sc_rm_set_memreg_permissions(-1, mr,
|
|
|
|
SECO_PT, SC_RM_PERM_FULL);
|
|
|
|
if (err) {
|
|
|
|
printf("Set permission failed for img %d, error %d\n",
|
|
|
|
image_index, err);
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sc_seco_authenticate(-1, SC_SECO_VERIFY_IMAGE,
|
|
|
|
1 << image_index);
|
|
|
|
if (err) {
|
|
|
|
printf("Authenticate img %d failed, return %d\n",
|
|
|
|
image_index, err);
|
|
|
|
ret = -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sc_rm_set_memreg_permissions(-1, mr,
|
|
|
|
SECO_PT, SC_RM_PERM_NONE);
|
|
|
|
if (err) {
|
|
|
|
printf("Remove permission failed for img %d, error %d\n",
|
|
|
|
image_index, err);
|
|
|
|
ret = -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:11:14 +00:00
|
|
|
static inline bool check_in_dram(ulong addr)
|
|
|
|
{
|
|
|
|
int i;
|
2020-06-26 06:13:33 +00:00
|
|
|
struct bd_info *bd = gd->bd;
|
2019-09-25 08:11:14 +00:00
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
|
|
|
|
if (bd->bi_dram[i].size) {
|
|
|
|
if (addr >= bd->bi_dram[i].start &&
|
|
|
|
addr < (bd->bi_dram[i].start + bd->bi_dram[i].size))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int authenticate_os_container(ulong addr)
|
|
|
|
{
|
|
|
|
struct container_hdr *phdr;
|
|
|
|
int i, ret = 0;
|
|
|
|
int err;
|
|
|
|
u16 length;
|
|
|
|
struct boot_img_t *img;
|
|
|
|
unsigned long s, e;
|
2023-06-15 10:09:24 +00:00
|
|
|
#ifdef CONFIG_ARMV8_CE_SHA256
|
|
|
|
u8 hash_value[SHA256_SUM_LEN];
|
|
|
|
#endif
|
2019-09-25 08:11:14 +00:00
|
|
|
|
|
|
|
if (addr % 4) {
|
|
|
|
puts("Error: Image's address is not 4 byte aligned\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!check_in_dram(addr)) {
|
|
|
|
puts("Error: Image's address is invalid\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
phdr = (struct container_hdr *)addr;
|
2023-10-14 20:47:43 +00:00
|
|
|
if (!valid_container_hdr(phdr)) {
|
2019-09-25 08:11:14 +00:00
|
|
|
printf("Error: Wrong container header\n");
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!phdr->num_images) {
|
|
|
|
printf("Error: Wrong container, no image found\n");
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
length = phdr->length_lsb + (phdr->length_msb << 8);
|
|
|
|
|
|
|
|
debug("container length %u\n", length);
|
|
|
|
|
2023-06-15 10:09:23 +00:00
|
|
|
err = ahab_auth_cntr_hdr(phdr, length);
|
2019-09-25 08:11:14 +00:00
|
|
|
if (err) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy images to dest address */
|
|
|
|
for (i = 0; i < phdr->num_images; i++) {
|
|
|
|
img = (struct boot_img_t *)(addr +
|
|
|
|
sizeof(struct container_hdr) +
|
|
|
|
i * sizeof(struct boot_img_t));
|
|
|
|
|
2020-11-06 08:09:30 +00:00
|
|
|
debug("img %d, dst 0x%x, src 0x%lux, size 0x%x\n",
|
2020-03-30 15:13:31 +00:00
|
|
|
i, (uint32_t) img->dst, img->offset + addr, img->size);
|
2019-09-25 08:11:14 +00:00
|
|
|
|
|
|
|
memcpy((void *)img->dst, (const void *)(img->offset + addr),
|
|
|
|
img->size);
|
|
|
|
|
|
|
|
s = img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1);
|
2020-04-22 07:36:34 +00:00
|
|
|
e = ALIGN(img->dst + img->size, CONFIG_SYS_CACHELINE_SIZE) - 1;
|
2019-09-25 08:11:14 +00:00
|
|
|
|
|
|
|
flush_dcache_range(s, e);
|
|
|
|
|
2023-06-15 10:09:24 +00:00
|
|
|
#ifdef CONFIG_ARMV8_CE_SHA256
|
|
|
|
if (((img->hab_flags & AHAB_HASH_TYPE_MASK) >> 8) == AHAB_HASH_TYPE_SHA256) {
|
|
|
|
sha256_csum_wd((void *)img->dst, img->size, hash_value, CHUNKSZ_SHA256);
|
|
|
|
err = memcmp(&img->hash, &hash_value, SHA256_SUM_LEN);
|
|
|
|
if (err) {
|
|
|
|
printf("img %d hash comparison failed, error %d\n", i, err);
|
|
|
|
ret = -EIO;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
ret = ahab_verify_cntr_image(img, i);
|
|
|
|
if (ret)
|
|
|
|
goto exit;
|
|
|
|
#ifdef CONFIG_ARMV8_CE_SHA256
|
|
|
|
}
|
|
|
|
#endif
|
2019-09-25 08:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-06-15 10:09:23 +00:00
|
|
|
ahab_auth_release();
|
2019-09-25 08:11:14 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:40:03 +00:00
|
|
|
static int do_authenticate(struct cmd_tbl *cmdtp, int flag, int argc,
|
|
|
|
char *const argv[])
|
2019-09-25 08:11:14 +00:00
|
|
|
{
|
|
|
|
ulong addr;
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
2021-07-24 15:03:29 +00:00
|
|
|
addr = hextoul(argv[1], NULL);
|
2019-09-25 08:11:14 +00:00
|
|
|
|
|
|
|
printf("Authenticate OS container at 0x%lx\n", addr);
|
|
|
|
|
|
|
|
if (authenticate_os_container(addr))
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
|
|
|
|
return CMD_RET_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void display_life_cycle(u16 lc)
|
|
|
|
{
|
|
|
|
printf("Lifecycle: 0x%04X, ", lc);
|
|
|
|
switch (lc) {
|
|
|
|
case 0x1:
|
|
|
|
printf("Pristine\n\n");
|
|
|
|
break;
|
|
|
|
case 0x2:
|
|
|
|
printf("Fab\n\n");
|
|
|
|
break;
|
|
|
|
case 0x8:
|
|
|
|
printf("Open\n\n");
|
|
|
|
break;
|
|
|
|
case 0x20:
|
|
|
|
printf("NXP closed\n\n");
|
|
|
|
break;
|
|
|
|
case 0x80:
|
|
|
|
printf("OEM closed\n\n");
|
|
|
|
break;
|
|
|
|
case 0x100:
|
|
|
|
printf("Partial field return\n\n");
|
|
|
|
break;
|
|
|
|
case 0x200:
|
|
|
|
printf("Full field return\n\n");
|
|
|
|
break;
|
|
|
|
case 0x400:
|
|
|
|
printf("No return\n\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("Unknown\n\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define AHAB_AUTH_CONTAINER_REQ 0x87
|
|
|
|
#define AHAB_VERIFY_IMAGE_REQ 0x88
|
|
|
|
|
|
|
|
#define AHAB_NO_AUTHENTICATION_IND 0xee
|
|
|
|
#define AHAB_BAD_KEY_HASH_IND 0xfa
|
|
|
|
#define AHAB_INVALID_KEY_IND 0xf9
|
|
|
|
#define AHAB_BAD_SIGNATURE_IND 0xf0
|
|
|
|
#define AHAB_BAD_HASH_IND 0xf1
|
|
|
|
|
|
|
|
static void display_ahab_auth_event(u32 event)
|
|
|
|
{
|
|
|
|
u8 cmd = (event >> 16) & 0xff;
|
|
|
|
u8 resp_ind = (event >> 8) & 0xff;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case AHAB_AUTH_CONTAINER_REQ:
|
|
|
|
printf("\tCMD = AHAB_AUTH_CONTAINER_REQ (0x%02X)\n", cmd);
|
|
|
|
printf("\tIND = ");
|
|
|
|
break;
|
|
|
|
case AHAB_VERIFY_IMAGE_REQ:
|
|
|
|
printf("\tCMD = AHAB_VERIFY_IMAGE_REQ (0x%02X)\n", cmd);
|
|
|
|
printf("\tIND = ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (resp_ind) {
|
|
|
|
case AHAB_NO_AUTHENTICATION_IND:
|
|
|
|
printf("AHAB_NO_AUTHENTICATION_IND (0x%02X)\n\n", resp_ind);
|
|
|
|
break;
|
|
|
|
case AHAB_BAD_KEY_HASH_IND:
|
|
|
|
printf("AHAB_BAD_KEY_HASH_IND (0x%02X)\n\n", resp_ind);
|
|
|
|
break;
|
|
|
|
case AHAB_INVALID_KEY_IND:
|
|
|
|
printf("AHAB_INVALID_KEY_IND (0x%02X)\n\n", resp_ind);
|
|
|
|
break;
|
|
|
|
case AHAB_BAD_SIGNATURE_IND:
|
|
|
|
printf("AHAB_BAD_SIGNATURE_IND (0x%02X)\n\n", resp_ind);
|
|
|
|
break;
|
|
|
|
case AHAB_BAD_HASH_IND:
|
|
|
|
printf("AHAB_BAD_HASH_IND (0x%02X)\n\n", resp_ind);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("Unknown Indicator (0x%02X)\n\n", resp_ind);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:40:03 +00:00
|
|
|
static int do_ahab_status(struct cmd_tbl *cmdtp, int flag, int argc,
|
|
|
|
char *const argv[])
|
2019-09-25 08:11:14 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
u8 idx = 0U;
|
|
|
|
u32 event;
|
|
|
|
u16 lc;
|
|
|
|
|
|
|
|
err = sc_seco_chip_info(-1, &lc, NULL, NULL, NULL);
|
2023-06-15 10:08:58 +00:00
|
|
|
if (err) {
|
2019-09-25 08:11:14 +00:00
|
|
|
printf("Error in get lifecycle\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
display_life_cycle(lc);
|
|
|
|
|
|
|
|
err = sc_seco_get_event(-1, idx, &event);
|
2023-06-15 10:08:58 +00:00
|
|
|
while (!err) {
|
2019-09-25 08:11:14 +00:00
|
|
|
printf("SECO Event[%u] = 0x%08X\n", idx, event);
|
|
|
|
display_ahab_auth_event(event);
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
err = sc_seco_get_event(-1, idx, &event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx == 0)
|
|
|
|
printf("No SECO Events Found!\n\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-21 19:33:41 +00:00
|
|
|
int ahab_close(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
u16 lc;
|
|
|
|
|
|
|
|
err = sc_seco_chip_info(-1, &lc, NULL, NULL, NULL);
|
|
|
|
if (err != SC_ERR_NONE) {
|
|
|
|
printf("Error in get lifecycle\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lc != 0x20) {
|
|
|
|
puts("Current lifecycle is NOT NXP closed, can't move to OEM closed\n");
|
|
|
|
display_life_cycle(lc);
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sc_seco_forward_lifecycle(-1, 16);
|
|
|
|
if (err != SC_ERR_NONE) {
|
|
|
|
printf("Error in forward lifecycle to OEM closed\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:11:14 +00:00
|
|
|
static int confirm_close(void)
|
|
|
|
{
|
|
|
|
puts("Warning: Please ensure your sample is in NXP closed state, "
|
|
|
|
"OEM SRK hash has been fused, \n"
|
|
|
|
" and you are able to boot a signed image successfully "
|
|
|
|
"without any SECO events reported.\n"
|
|
|
|
" If not, your sample will be unrecoverable.\n"
|
|
|
|
"\nReally perform this operation? <y/N>\n");
|
|
|
|
|
|
|
|
if (confirm_yesno())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
puts("Ahab close aborted\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:40:03 +00:00
|
|
|
static int do_ahab_close(struct cmd_tbl *cmdtp, int flag, int argc,
|
|
|
|
char *const argv[])
|
2019-09-25 08:11:14 +00:00
|
|
|
{
|
2020-12-21 17:31:28 +00:00
|
|
|
int confirmed = argc >= 2 && !strcmp(argv[1], "-y");
|
2019-09-25 08:11:14 +00:00
|
|
|
int err;
|
|
|
|
|
2020-12-21 17:31:28 +00:00
|
|
|
if (!confirmed && !confirm_close())
|
2019-09-25 08:11:14 +00:00
|
|
|
return -EACCES;
|
|
|
|
|
2023-08-21 19:33:41 +00:00
|
|
|
err = ahab_close();
|
2023-06-15 10:08:58 +00:00
|
|
|
if (err) {
|
2023-08-21 19:33:41 +00:00
|
|
|
printf("Change to OEM closed failed\n");
|
|
|
|
return err;
|
2019-09-25 08:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Change to OEM closed successfully\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_CMD(auth_cntr, CONFIG_SYS_MAXARGS, 1, do_authenticate,
|
|
|
|
"autenticate OS container via AHAB",
|
|
|
|
"addr\n"
|
|
|
|
"addr - OS container hex address\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
U_BOOT_CMD(ahab_status, CONFIG_SYS_MAXARGS, 1, do_ahab_status,
|
|
|
|
"display AHAB lifecycle and events from seco",
|
|
|
|
""
|
|
|
|
);
|
|
|
|
|
|
|
|
U_BOOT_CMD(ahab_close, CONFIG_SYS_MAXARGS, 1, do_ahab_close,
|
|
|
|
"Change AHAB lifecycle to OEM closed",
|
|
|
|
""
|
|
|
|
);
|