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