Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2018-2019 NXP |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame^] | 7 | #include <stdlib.h> |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 8 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 10 | #include <spl.h> |
Ye Li | eb10127 | 2021-08-07 16:00:37 +0800 | [diff] [blame] | 11 | #include <asm/mach-imx/image.h> |
Peng Fan | b590634 | 2021-08-07 16:00:38 +0800 | [diff] [blame] | 12 | #ifdef CONFIG_AHAB_BOOT |
Peng Fan | 2e0644a | 2023-04-28 12:08:09 +0800 | [diff] [blame] | 13 | #include <firmware/imx/sci/sci.h> |
Peng Fan | b590634 | 2021-08-07 16:00:38 +0800 | [diff] [blame] | 14 | #endif |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 15 | |
| 16 | #define SEC_SECURE_RAM_BASE 0x31800000UL |
| 17 | #define SEC_SECURE_RAM_END_BASE (SEC_SECURE_RAM_BASE + 0xFFFFUL) |
| 18 | #define SECO_LOCAL_SEC_SEC_SECURE_RAM_BASE 0x60000000UL |
| 19 | |
| 20 | #define SECO_PT 2U |
| 21 | |
| 22 | #ifdef CONFIG_AHAB_BOOT |
| 23 | static int authenticate_image(struct boot_img_t *img, int image_index) |
| 24 | { |
| 25 | sc_faddr_t start, end; |
| 26 | sc_rm_mr_t mr; |
| 27 | int err; |
| 28 | int ret = 0; |
| 29 | |
Peng Fan | f818b74 | 2020-04-22 15:45:42 +0800 | [diff] [blame] | 30 | debug("img %d, dst 0x%x, src 0x%x, size 0x%x\n", |
| 31 | image_index, (uint32_t)img->dst, img->offset, img->size); |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 32 | |
| 33 | /* Find the memreg and set permission for seco pt */ |
| 34 | err = sc_rm_find_memreg(-1, &mr, |
| 35 | img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1), |
Peng Fan | d1cf84a | 2020-04-22 15:25:31 +0800 | [diff] [blame] | 36 | ALIGN(img->dst + img->size, CONFIG_SYS_CACHELINE_SIZE) - 1); |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 37 | |
| 38 | if (err) { |
Peng Fan | 489c740 | 2023-06-15 18:09:06 +0800 | [diff] [blame] | 39 | printf("can't find memreg for image %d load address 0x%llx, error %d\n", |
Peng Fan | f818b74 | 2020-04-22 15:45:42 +0800 | [diff] [blame] | 40 | image_index, img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1), err); |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 41 | return -ENOMEM; |
| 42 | } |
| 43 | |
| 44 | err = sc_rm_get_memreg_info(-1, mr, &start, &end); |
| 45 | if (!err) |
Peng Fan | 489c740 | 2023-06-15 18:09:06 +0800 | [diff] [blame] | 46 | debug("memreg %u 0x%llx -- 0x%llx\n", mr, start, end); |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 47 | |
| 48 | err = sc_rm_set_memreg_permissions(-1, mr, |
| 49 | SECO_PT, SC_RM_PERM_FULL); |
| 50 | if (err) { |
| 51 | printf("set permission failed for img %d, error %d\n", |
| 52 | image_index, err); |
| 53 | return -EPERM; |
| 54 | } |
| 55 | |
Ye Li | d962d56 | 2019-10-14 23:08:46 -0700 | [diff] [blame] | 56 | err = sc_seco_authenticate(-1, SC_SECO_VERIFY_IMAGE, |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 57 | 1 << image_index); |
| 58 | if (err) { |
| 59 | printf("authenticate img %d failed, return %d\n", |
| 60 | image_index, err); |
| 61 | ret = -EIO; |
| 62 | } |
| 63 | |
| 64 | err = sc_rm_set_memreg_permissions(-1, mr, |
| 65 | SECO_PT, SC_RM_PERM_NONE); |
| 66 | if (err) { |
| 67 | printf("remove permission failed for img %d, error %d\n", |
| 68 | image_index, err); |
| 69 | ret = -EPERM; |
| 70 | } |
| 71 | |
| 72 | return ret; |
| 73 | } |
| 74 | #endif |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 75 | |
| 76 | static struct boot_img_t *read_auth_image(struct spl_image_info *spl_image, |
| 77 | struct spl_load_info *info, |
| 78 | struct container_hdr *container, |
| 79 | int image_index, |
| 80 | u32 container_sector) |
| 81 | { |
| 82 | struct boot_img_t *images; |
| 83 | ulong sector; |
| 84 | u32 sectors; |
| 85 | |
| 86 | if (image_index > container->num_images) { |
| 87 | debug("Invalid image number\n"); |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | images = (struct boot_img_t *)((u8 *)container + |
| 92 | sizeof(struct container_hdr)); |
| 93 | |
| 94 | if (images[image_index].offset % info->bl_len) { |
| 95 | printf("%s: image%d offset not aligned to %u\n", |
| 96 | __func__, image_index, info->bl_len); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | sectors = roundup(images[image_index].size, info->bl_len) / |
| 101 | info->bl_len; |
| 102 | sector = images[image_index].offset / info->bl_len + |
| 103 | container_sector; |
| 104 | |
| 105 | debug("%s: container: %p sector: %lu sectors: %u\n", __func__, |
| 106 | container, sector, sectors); |
| 107 | if (info->read(info, sector, sectors, |
| 108 | (void *)images[image_index].entry) != sectors) { |
| 109 | printf("%s wrong\n", __func__); |
| 110 | return NULL; |
| 111 | } |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 112 | |
| 113 | #ifdef CONFIG_AHAB_BOOT |
| 114 | if (authenticate_image(&images[image_index], image_index)) { |
| 115 | printf("Failed to authenticate image %d\n", image_index); |
| 116 | return NULL; |
| 117 | } |
| 118 | #endif |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 119 | |
| 120 | return &images[image_index]; |
| 121 | } |
| 122 | |
| 123 | static int read_auth_container(struct spl_image_info *spl_image, |
| 124 | struct spl_load_info *info, ulong sector) |
| 125 | { |
| 126 | struct container_hdr *container = NULL; |
| 127 | u16 length; |
| 128 | u32 sectors; |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 129 | int i, size, ret = 0; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 130 | |
| 131 | size = roundup(CONTAINER_HDR_ALIGNMENT, info->bl_len); |
| 132 | sectors = size / info->bl_len; |
| 133 | |
| 134 | /* |
| 135 | * It will not override the ATF code, so safe to use it here, |
| 136 | * no need malloc |
| 137 | */ |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame^] | 138 | container = malloc(size); |
| 139 | if (!container) |
| 140 | return -ENOMEM; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 141 | |
| 142 | debug("%s: container: %p sector: %lu sectors: %u\n", __func__, |
| 143 | container, sector, sectors); |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame^] | 144 | if (info->read(info, sector, sectors, container) != sectors) { |
| 145 | ret = -EIO; |
| 146 | goto end; |
| 147 | } |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 148 | |
| 149 | if (container->tag != 0x87 && container->version != 0x0) { |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame^] | 150 | printf("Wrong container header"); |
| 151 | ret = -ENOENT; |
| 152 | goto end; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | if (!container->num_images) { |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame^] | 156 | printf("Wrong container, no image found"); |
| 157 | ret = -ENOENT; |
| 158 | goto end; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | length = container->length_lsb + (container->length_msb << 8); |
| 162 | debug("Container length %u\n", length); |
| 163 | |
| 164 | if (length > CONTAINER_HDR_ALIGNMENT) { |
| 165 | size = roundup(length, info->bl_len); |
| 166 | sectors = size / info->bl_len; |
| 167 | |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame^] | 168 | free(container); |
| 169 | container = malloc(size); |
| 170 | if (!container) |
| 171 | return -ENOMEM; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 172 | |
| 173 | debug("%s: container: %p sector: %lu sectors: %u\n", |
| 174 | __func__, container, sector, sectors); |
| 175 | if (info->read(info, sector, sectors, container) != |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame^] | 176 | sectors) { |
| 177 | ret = -EIO; |
| 178 | goto end; |
| 179 | } |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 180 | } |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 181 | |
| 182 | #ifdef CONFIG_AHAB_BOOT |
| 183 | memcpy((void *)SEC_SECURE_RAM_BASE, (const void *)container, |
| 184 | ALIGN(length, CONFIG_SYS_CACHELINE_SIZE)); |
| 185 | |
Ye Li | d962d56 | 2019-10-14 23:08:46 -0700 | [diff] [blame] | 186 | ret = sc_seco_authenticate(-1, SC_SECO_AUTH_CONTAINER, |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 187 | SECO_LOCAL_SEC_SEC_SECURE_RAM_BASE); |
| 188 | if (ret) { |
| 189 | printf("authenticate container hdr failed, return %d\n", ret); |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame^] | 190 | goto end_auth; |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 191 | } |
| 192 | #endif |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 193 | |
| 194 | for (i = 0; i < container->num_images; i++) { |
| 195 | struct boot_img_t *image = read_auth_image(spl_image, info, |
| 196 | container, i, |
| 197 | sector); |
| 198 | |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 199 | if (!image) { |
| 200 | ret = -EINVAL; |
| 201 | goto end_auth; |
| 202 | } |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 203 | |
| 204 | if (i == 0) { |
| 205 | spl_image->load_addr = image->dst; |
| 206 | spl_image->entry_point = image->entry; |
| 207 | } |
| 208 | } |
| 209 | |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 210 | end_auth: |
| 211 | #ifdef CONFIG_AHAB_BOOT |
Ye Li | d962d56 | 2019-10-14 23:08:46 -0700 | [diff] [blame] | 212 | if (sc_seco_authenticate(-1, SC_SECO_REL_CONTAINER, 0)) |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 213 | printf("Error: release container failed!\n"); |
| 214 | #endif |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame^] | 215 | |
| 216 | end: |
| 217 | free(container); |
| 218 | |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 219 | return ret; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | int spl_load_imx_container(struct spl_image_info *spl_image, |
| 223 | struct spl_load_info *info, ulong sector) |
| 224 | { |
| 225 | return read_auth_container(spl_image, info, sector); |
| 226 | } |