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, |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame^] | 22 | ulong container_offset) |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 23 | { |
| 24 | struct boot_img_t *images; |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame^] | 25 | ulong offset, overhead, size; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 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 | |
Sean Anderson | f97fc6d | 2023-11-08 11:48:39 -0500 | [diff] [blame] | 35 | if (!IS_ALIGNED(images[image_index].offset, info->bl_len)) { |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 36 | printf("%s: image%d offset not aligned to %u\n", |
| 37 | __func__, image_index, info->bl_len); |
| 38 | return NULL; |
| 39 | } |
| 40 | |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame^] | 41 | size = ALIGN(images[image_index].size, info->bl_len); |
| 42 | offset = images[image_index].offset + container_offset; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 43 | |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame^] | 44 | debug("%s: container: %p offset: %lu size: %lu\n", __func__, |
| 45 | container, offset, size); |
| 46 | if (info->read(info, offset, size, |
| 47 | map_sysmem(images[image_index].dst - overhead, |
| 48 | images[image_index].size)) != size) { |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 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, |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame^] | 62 | struct spl_load_info *info, ulong offset) |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 63 | { |
| 64 | struct container_hdr *container = NULL; |
| 65 | u16 length; |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 66 | int i, size, ret = 0; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 67 | |
Sean Anderson | f97fc6d | 2023-11-08 11:48:39 -0500 | [diff] [blame] | 68 | size = ALIGN(CONTAINER_HDR_ALIGNMENT, info->bl_len); |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * It will not override the ATF code, so safe to use it here, |
| 72 | * no need malloc |
| 73 | */ |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 74 | container = malloc(size); |
| 75 | if (!container) |
| 76 | return -ENOMEM; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 77 | |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame^] | 78 | debug("%s: container: %p offset: %lu size: %u\n", __func__, |
| 79 | container, offset, size); |
| 80 | if (info->read(info, offset, size, container) != size) { |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 81 | ret = -EIO; |
| 82 | goto end; |
| 83 | } |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 84 | |
Sean Anderson | c512668 | 2023-10-14 16:47:43 -0400 | [diff] [blame] | 85 | if (!valid_container_hdr(container)) { |
Sean Anderson | 5d5c7bd | 2023-10-14 16:47:42 -0400 | [diff] [blame] | 86 | log_err("Wrong container header\n"); |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 87 | ret = -ENOENT; |
| 88 | goto end; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (!container->num_images) { |
Sean Anderson | 5d5c7bd | 2023-10-14 16:47:42 -0400 | [diff] [blame] | 92 | log_err("Wrong container, no image found\n"); |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 93 | ret = -ENOENT; |
| 94 | goto end; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | length = container->length_lsb + (container->length_msb << 8); |
| 98 | debug("Container length %u\n", length); |
| 99 | |
| 100 | if (length > CONTAINER_HDR_ALIGNMENT) { |
Sean Anderson | f97fc6d | 2023-11-08 11:48:39 -0500 | [diff] [blame] | 101 | size = ALIGN(length, info->bl_len); |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 102 | |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 103 | free(container); |
| 104 | container = malloc(size); |
| 105 | if (!container) |
| 106 | return -ENOMEM; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 107 | |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame^] | 108 | debug("%s: container: %p offset: %lu size: %u\n", |
| 109 | __func__, container, offset, size); |
| 110 | if (info->read(info, offset, size, container) != size) { |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 111 | ret = -EIO; |
| 112 | goto end; |
| 113 | } |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 114 | } |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 115 | |
| 116 | #ifdef CONFIG_AHAB_BOOT |
Ye Li | 185af30 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 117 | ret = ahab_auth_cntr_hdr(container, length); |
| 118 | if (ret) |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 119 | goto end_auth; |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 120 | #endif |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 121 | |
| 122 | for (i = 0; i < container->num_images; i++) { |
| 123 | struct boot_img_t *image = read_auth_image(spl_image, info, |
| 124 | container, i, |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame^] | 125 | offset); |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 126 | |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 127 | if (!image) { |
| 128 | ret = -EINVAL; |
| 129 | goto end_auth; |
| 130 | } |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 131 | |
| 132 | if (i == 0) { |
| 133 | spl_image->load_addr = image->dst; |
| 134 | spl_image->entry_point = image->entry; |
| 135 | } |
| 136 | } |
| 137 | |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 138 | end_auth: |
| 139 | #ifdef CONFIG_AHAB_BOOT |
Ye Li | 185af30 | 2023-06-15 18:09:23 +0800 | [diff] [blame] | 140 | ahab_auth_release(); |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 141 | #endif |
Nitin Garg | f39c1f2 | 2023-06-15 18:09:22 +0800 | [diff] [blame] | 142 | |
| 143 | end: |
| 144 | free(container); |
| 145 | |
Peng Fan | f1e0f9f | 2019-09-25 08:11:14 +0000 | [diff] [blame] | 146 | return ret; |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int spl_load_imx_container(struct spl_image_info *spl_image, |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame^] | 150 | struct spl_load_info *info, ulong offset) |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 151 | { |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame^] | 152 | return read_auth_container(spl_image, info, offset); |
Peng Fan | 1a7e625 | 2019-08-22 07:42:33 +0000 | [diff] [blame] | 153 | } |