2019-08-22 07:42:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Copyright 2018-2019 NXP
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <errno.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2019-08-22 07:42:33 +00:00
|
|
|
#include <spl.h>
|
|
|
|
#include <asm/arch/image.h>
|
2019-09-25 08:11:14 +00:00
|
|
|
#include <asm/arch/sci/sci.h>
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
#ifdef CONFIG_AHAB_BOOT
|
|
|
|
static int authenticate_image(struct boot_img_t *img, int image_index)
|
|
|
|
{
|
|
|
|
sc_faddr_t start, end;
|
|
|
|
sc_rm_mr_t mr;
|
|
|
|
int err;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-04-22 07:45:42 +00:00
|
|
|
debug("img %d, dst 0x%x, src 0x%x, size 0x%x\n",
|
|
|
|
image_index, (uint32_t)img->dst, img->offset, img->size);
|
2019-09-25 08:11:14 +00:00
|
|
|
|
|
|
|
/* Find the memreg and set permission for seco pt */
|
|
|
|
err = sc_rm_find_memreg(-1, &mr,
|
|
|
|
img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1),
|
2020-04-22 07:25:31 +00:00
|
|
|
ALIGN(img->dst + img->size, CONFIG_SYS_CACHELINE_SIZE) - 1);
|
2019-09-25 08:11:14 +00:00
|
|
|
|
|
|
|
if (err) {
|
2020-04-22 07:45:42 +00:00
|
|
|
printf("can't find memreg for image %d load address 0x%x, error %d\n",
|
|
|
|
image_index, img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1), err);
|
2019-09-25 08:11:14 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sc_rm_get_memreg_info(-1, mr, &start, &end);
|
|
|
|
if (!err)
|
2020-04-22 07:45:42 +00:00
|
|
|
debug("memreg %u 0x%x -- 0x%x\n", mr, start, end);
|
2019-09-25 08:11:14 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-15 06:08:46 +00:00
|
|
|
err = sc_seco_authenticate(-1, SC_SECO_VERIFY_IMAGE,
|
2019-09-25 08:11:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
2019-08-22 07:42:33 +00:00
|
|
|
|
|
|
|
static struct boot_img_t *read_auth_image(struct spl_image_info *spl_image,
|
|
|
|
struct spl_load_info *info,
|
|
|
|
struct container_hdr *container,
|
|
|
|
int image_index,
|
|
|
|
u32 container_sector)
|
|
|
|
{
|
|
|
|
struct boot_img_t *images;
|
|
|
|
ulong sector;
|
|
|
|
u32 sectors;
|
|
|
|
|
|
|
|
if (image_index > container->num_images) {
|
|
|
|
debug("Invalid image number\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
images = (struct boot_img_t *)((u8 *)container +
|
|
|
|
sizeof(struct container_hdr));
|
|
|
|
|
|
|
|
if (images[image_index].offset % info->bl_len) {
|
|
|
|
printf("%s: image%d offset not aligned to %u\n",
|
|
|
|
__func__, image_index, info->bl_len);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sectors = roundup(images[image_index].size, info->bl_len) /
|
|
|
|
info->bl_len;
|
|
|
|
sector = images[image_index].offset / info->bl_len +
|
|
|
|
container_sector;
|
|
|
|
|
|
|
|
debug("%s: container: %p sector: %lu sectors: %u\n", __func__,
|
|
|
|
container, sector, sectors);
|
|
|
|
if (info->read(info, sector, sectors,
|
|
|
|
(void *)images[image_index].entry) != sectors) {
|
|
|
|
printf("%s wrong\n", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:11:14 +00:00
|
|
|
#ifdef CONFIG_AHAB_BOOT
|
|
|
|
if (authenticate_image(&images[image_index], image_index)) {
|
|
|
|
printf("Failed to authenticate image %d\n", image_index);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-22 07:42:33 +00:00
|
|
|
return &images[image_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_auth_container(struct spl_image_info *spl_image,
|
|
|
|
struct spl_load_info *info, ulong sector)
|
|
|
|
{
|
|
|
|
struct container_hdr *container = NULL;
|
|
|
|
u16 length;
|
|
|
|
u32 sectors;
|
2019-09-25 08:11:14 +00:00
|
|
|
int i, size, ret = 0;
|
2019-08-22 07:42:33 +00:00
|
|
|
|
|
|
|
size = roundup(CONTAINER_HDR_ALIGNMENT, info->bl_len);
|
|
|
|
sectors = size / info->bl_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It will not override the ATF code, so safe to use it here,
|
|
|
|
* no need malloc
|
|
|
|
*/
|
|
|
|
container = (struct container_hdr *)spl_get_load_buffer(-size, size);
|
|
|
|
|
|
|
|
debug("%s: container: %p sector: %lu sectors: %u\n", __func__,
|
|
|
|
container, sector, sectors);
|
|
|
|
if (info->read(info, sector, sectors, container) != sectors)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
if (container->tag != 0x87 && container->version != 0x0) {
|
|
|
|
printf("Wrong container header");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!container->num_images) {
|
|
|
|
printf("Wrong container, no image found");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
length = container->length_lsb + (container->length_msb << 8);
|
|
|
|
debug("Container length %u\n", length);
|
|
|
|
|
|
|
|
if (length > CONTAINER_HDR_ALIGNMENT) {
|
|
|
|
size = roundup(length, info->bl_len);
|
|
|
|
sectors = size / info->bl_len;
|
|
|
|
|
|
|
|
container = (struct container_hdr *)spl_get_load_buffer(-size, size);
|
|
|
|
|
|
|
|
debug("%s: container: %p sector: %lu sectors: %u\n",
|
|
|
|
__func__, container, sector, sectors);
|
|
|
|
if (info->read(info, sector, sectors, container) !=
|
|
|
|
sectors)
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:11:14 +00:00
|
|
|
#ifdef CONFIG_AHAB_BOOT
|
|
|
|
memcpy((void *)SEC_SECURE_RAM_BASE, (const void *)container,
|
|
|
|
ALIGN(length, CONFIG_SYS_CACHELINE_SIZE));
|
|
|
|
|
2019-10-15 06:08:46 +00:00
|
|
|
ret = sc_seco_authenticate(-1, SC_SECO_AUTH_CONTAINER,
|
2019-09-25 08:11:14 +00:00
|
|
|
SECO_LOCAL_SEC_SEC_SECURE_RAM_BASE);
|
|
|
|
if (ret) {
|
|
|
|
printf("authenticate container hdr failed, return %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-22 07:42:33 +00:00
|
|
|
for (i = 0; i < container->num_images; i++) {
|
|
|
|
struct boot_img_t *image = read_auth_image(spl_image, info,
|
|
|
|
container, i,
|
|
|
|
sector);
|
|
|
|
|
2019-09-25 08:11:14 +00:00
|
|
|
if (!image) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto end_auth;
|
|
|
|
}
|
2019-08-22 07:42:33 +00:00
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
spl_image->load_addr = image->dst;
|
|
|
|
spl_image->entry_point = image->entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:11:14 +00:00
|
|
|
end_auth:
|
|
|
|
#ifdef CONFIG_AHAB_BOOT
|
2019-10-15 06:08:46 +00:00
|
|
|
if (sc_seco_authenticate(-1, SC_SECO_REL_CONTAINER, 0))
|
2019-09-25 08:11:14 +00:00
|
|
|
printf("Error: release container failed!\n");
|
|
|
|
#endif
|
|
|
|
return ret;
|
2019-08-22 07:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int spl_load_imx_container(struct spl_image_info *spl_image,
|
|
|
|
struct spl_load_info *info, ulong sector)
|
|
|
|
{
|
|
|
|
return read_auth_container(spl_image, info, sector);
|
|
|
|
}
|