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