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