Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Ye Li | 185af30 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 3 | * Copyright 2018-2021 NXP |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Sean Anderson | 5d5c7bd | 2023-10-14 16:47:42 -0400 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_ARCH |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 7 | #include <common.h> |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 8 | #include <stdlib.h> |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 9 | #include <errno.h> |
Sean Anderson | 952ed67 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 10 | #include <imx_container.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Sean Anderson | 5ff7772 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 12 | #include <mapmem.h> |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 13 | #include <spl.h> |
Peng Fan | b590634 | 2021-08-07 16:00:38 +0800 | [diff] [blame] | 14 | #ifdef CONFIG_AHAB_BOOT |
Ye Li | 185af30 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 15 | #include <asm/mach-imx/ahab.h> |
Peng Fan | b590634 | 2021-08-07 16:00:38 +0800 | [diff] [blame] | 16 | #endif |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 17 | |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 18 | static struct boot_img_t *read_auth_image(struct spl_image_info *spl_image, |
| 19 | struct spl_load_info *info, |
| 20 | struct container_hdr *container, |
| 21 | int image_index, |
| 22 | u32 container_sector) |
| 23 | { |
| 24 | struct boot_img_t *images; |
| 25 | ulong sector; |
| 26 | u32 sectors; |
| 27 | |
| 28 | if (image_index > container->num_images) { |
| 29 | debug("Invalid image number\n"); |
| 30 | return NULL; |
| 31 | } |
| 32 | |
| 33 | images = (struct boot_img_t *)((u8 *)container + |
| 34 | sizeof(struct container_hdr)); |
| 35 | |
| 36 | if (images[image_index].offset % info->bl_len) { |
| 37 | printf("%s: image%d offset not aligned to %u\n", |
| 38 | __func__, image_index, info->bl_len); |
| 39 | return NULL; |
| 40 | } |
| 41 | |
| 42 | sectors = roundup(images[image_index].size, info->bl_len) / |
| 43 | info->bl_len; |
| 44 | sector = images[image_index].offset / info->bl_len + |
| 45 | container_sector; |
| 46 | |
| 47 | debug("%s: container: %p sector: %lu sectors: %u\n", __func__, |
| 48 | container, sector, sectors); |
| 49 | if (info->read(info, sector, sectors, |
Sean Anderson | 5ff7772 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 50 | map_sysmem(images[image_index].dst, |
| 51 | images[image_index].size)) != sectors) { |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 52 | printf("%s wrong\n", __func__); |
| 53 | return NULL; |
| 54 | } |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 55 | |
| 56 | #ifdef CONFIG_AHAB_BOOT |
Ye Li | 185af30 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 57 | if (ahab_verify_cntr_image(&images[image_index], image_index)) |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 58 | return NULL; |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 59 | #endif |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 60 | |
| 61 | return &images[image_index]; |
| 62 | } |
| 63 | |
| 64 | static int read_auth_container(struct spl_image_info *spl_image, |
| 65 | struct spl_load_info *info, ulong sector) |
| 66 | { |
| 67 | struct container_hdr *container = NULL; |
| 68 | u16 length; |
| 69 | u32 sectors; |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 70 | int i, size, ret = 0; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 71 | |
| 72 | size = roundup(CONTAINER_HDR_ALIGNMENT, info->bl_len); |
| 73 | sectors = size / info->bl_len; |
| 74 | |
| 75 | /* |
| 76 | * It will not override the ATF code, so safe to use it here, |
| 77 | * no need malloc |
| 78 | */ |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 79 | container = malloc(size); |
| 80 | if (!container) |
| 81 | return -ENOMEM; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 82 | |
| 83 | debug("%s: container: %p sector: %lu sectors: %u\n", __func__, |
| 84 | container, sector, sectors); |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 85 | if (info->read(info, sector, sectors, container) != sectors) { |
| 86 | ret = -EIO; |
| 87 | goto end; |
| 88 | } |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 89 | |
Sean Anderson | c512668 | 2023-10-14 16:47:43 -0400 | [diff] [blame] | 90 | if (!valid_container_hdr(container)) { |
Sean Anderson | 5d5c7bd | 2023-10-14 16:47:42 -0400 | [diff] [blame] | 91 | log_err("Wrong container header\n"); |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 92 | ret = -ENOENT; |
| 93 | goto end; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (!container->num_images) { |
Sean Anderson | 5d5c7bd | 2023-10-14 16:47:42 -0400 | [diff] [blame] | 97 | log_err("Wrong container, no image found\n"); |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 98 | ret = -ENOENT; |
| 99 | goto end; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | length = container->length_lsb + (container->length_msb << 8); |
| 103 | debug("Container length %u\n", length); |
| 104 | |
| 105 | if (length > CONTAINER_HDR_ALIGNMENT) { |
| 106 | size = roundup(length, info->bl_len); |
| 107 | sectors = size / info->bl_len; |
| 108 | |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 109 | free(container); |
| 110 | container = malloc(size); |
| 111 | if (!container) |
| 112 | return -ENOMEM; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 113 | |
| 114 | debug("%s: container: %p sector: %lu sectors: %u\n", |
| 115 | __func__, container, sector, sectors); |
| 116 | if (info->read(info, sector, sectors, container) != |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 117 | sectors) { |
| 118 | ret = -EIO; |
| 119 | goto end; |
| 120 | } |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 121 | } |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 122 | |
| 123 | #ifdef CONFIG_AHAB_BOOT |
Ye Li | 185af30 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 124 | ret = ahab_auth_cntr_hdr(container, length); |
| 125 | if (ret) |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 126 | goto end_auth; |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 127 | #endif |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 128 | |
| 129 | for (i = 0; i < container->num_images; i++) { |
| 130 | struct boot_img_t *image = read_auth_image(spl_image, info, |
| 131 | container, i, |
| 132 | sector); |
| 133 | |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 134 | if (!image) { |
| 135 | ret = -EINVAL; |
| 136 | goto end_auth; |
| 137 | } |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 138 | |
| 139 | if (i == 0) { |
| 140 | spl_image->load_addr = image->dst; |
| 141 | spl_image->entry_point = image->entry; |
| 142 | } |
| 143 | } |
| 144 | |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 145 | end_auth: |
| 146 | #ifdef CONFIG_AHAB_BOOT |
Ye Li | 185af30 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 147 | ahab_auth_release(); |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 148 | #endif |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 149 | |
| 150 | end: |
| 151 | free(container); |
| 152 | |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 153 | return ret; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | int spl_load_imx_container(struct spl_image_info *spl_image, |
| 157 | struct spl_load_info *info, ulong sector) |
| 158 | { |
| 159 | return read_auth_container(spl_image, info, sector); |
| 160 | } |