Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Ye Li | 00ce415 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 3 | * Copyright 2018-2021 NXP |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Sean Anderson | d9416cc | 2023-10-14 16:47:42 -0400 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_ARCH |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 7 | #include <common.h> |
Nitin Garg | 6e81ca2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 8 | #include <stdlib.h> |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 9 | #include <errno.h> |
Sean Anderson | ab12179 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 10 | #include <imx_container.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Sean Anderson | b02c4e9 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 12 | #include <mapmem.h> |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 13 | #include <spl.h> |
Peng Fan | 20ed81e | 2021-08-07 16:00:38 +0800 | [diff] [blame] | 14 | #ifdef CONFIG_AHAB_BOOT |
Ye Li | 00ce415 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 15 | #include <asm/mach-imx/ahab.h> |
Peng Fan | 7e2db74 | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 16 | #endif |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 17 | |
| 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 | |
Sean Anderson | 33c8d01 | 2023-11-08 11:48:39 -0500 | [diff] [blame^] | 36 | if (!IS_ALIGNED(images[image_index].offset, info->bl_len)) { |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 37 | printf("%s: image%d offset not aligned to %u\n", |
| 38 | __func__, image_index, info->bl_len); |
| 39 | return NULL; |
| 40 | } |
| 41 | |
Sean Anderson | 33c8d01 | 2023-11-08 11:48:39 -0500 | [diff] [blame^] | 42 | sectors = ALIGN(images[image_index].size, info->bl_len) / |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 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 | b02c4e9 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 50 | map_sysmem(images[image_index].dst, |
| 51 | images[image_index].size)) != sectors) { |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 52 | printf("%s wrong\n", __func__); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
Peng Fan | 7e2db74 | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 56 | #ifdef CONFIG_AHAB_BOOT |
Ye Li | 00ce415 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 57 | if (ahab_verify_cntr_image(&images[image_index], image_index)) |
Peng Fan | 7e2db74 | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 58 | return NULL; |
Peng Fan | 7e2db74 | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 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 | 7e2db74 | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 70 | int i, size, ret = 0; |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 71 | |
Sean Anderson | 33c8d01 | 2023-11-08 11:48:39 -0500 | [diff] [blame^] | 72 | size = ALIGN(CONTAINER_HDR_ALIGNMENT, info->bl_len); |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 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 | 6e81ca2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 79 | container = malloc(size); |
| 80 | if (!container) |
| 81 | return -ENOMEM; |
Peng Fan | 7b86cd4 | 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 | 6e81ca2 | 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 | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 89 | |
Sean Anderson | d401e0b | 2023-10-14 16:47:43 -0400 | [diff] [blame] | 90 | if (!valid_container_hdr(container)) { |
Sean Anderson | d9416cc | 2023-10-14 16:47:42 -0400 | [diff] [blame] | 91 | log_err("Wrong container header\n"); |
Nitin Garg | 6e81ca2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 92 | ret = -ENOENT; |
| 93 | goto end; |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (!container->num_images) { |
Sean Anderson | d9416cc | 2023-10-14 16:47:42 -0400 | [diff] [blame] | 97 | log_err("Wrong container, no image found\n"); |
Nitin Garg | 6e81ca2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 98 | ret = -ENOENT; |
| 99 | goto end; |
Peng Fan | 7b86cd4 | 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) { |
Sean Anderson | 33c8d01 | 2023-11-08 11:48:39 -0500 | [diff] [blame^] | 106 | size = ALIGN(length, info->bl_len); |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 107 | sectors = size / info->bl_len; |
| 108 | |
Nitin Garg | 6e81ca2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 109 | free(container); |
| 110 | container = malloc(size); |
| 111 | if (!container) |
| 112 | return -ENOMEM; |
Peng Fan | 7b86cd4 | 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 | 6e81ca2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 117 | sectors) { |
| 118 | ret = -EIO; |
| 119 | goto end; |
| 120 | } |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Peng Fan | 7e2db74 | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 123 | #ifdef CONFIG_AHAB_BOOT |
Ye Li | 00ce415 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 124 | ret = ahab_auth_cntr_hdr(container, length); |
| 125 | if (ret) |
Nitin Garg | 6e81ca2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 126 | goto end_auth; |
Peng Fan | 7e2db74 | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
Peng Fan | 7b86cd4 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 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 | 7e2db74 | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 134 | if (!image) { |
| 135 | ret = -EINVAL; |
| 136 | goto end_auth; |
| 137 | } |
Peng Fan | 7b86cd4 | 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 | 7e2db74 | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 145 | end_auth: |
| 146 | #ifdef CONFIG_AHAB_BOOT |
Ye Li | 00ce415 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 147 | ahab_auth_release(); |
Peng Fan | 7e2db74 | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 148 | #endif |
Nitin Garg | 6e81ca2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 149 | |
| 150 | end: |
| 151 | free(container); |
| 152 | |
Peng Fan | 7e2db74 | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 153 | return ret; |
Peng Fan | 7b86cd4 | 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 | } |