Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <errno.h> |
Michal Simek | f707b5a | 2018-07-18 14:33:15 +0200 | [diff] [blame] | 9 | #include <fpga.h> |
Simon Glass | 1a974af | 2019-08-01 09:46:36 -0600 | [diff] [blame] | 10 | #include <gzip.h> |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 11 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Jean-Jacques Hiblot | 0da5815 | 2019-10-22 16:39:13 +0200 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 14 | #include <spl.h> |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 15 | #include <sysinfo.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 16 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Jean-Jacques Hiblot | 0da5815 | 2019-10-22 16:39:13 +0200 | [diff] [blame] | 18 | #include <linux/libfdt.h> |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 19 | |
Lukas Auer | 80afee7 | 2019-08-21 21:14:42 +0200 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Jean-Jacques Hiblot | 0da5815 | 2019-10-22 16:39:13 +0200 | [diff] [blame] | 22 | #ifndef CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ |
| 23 | #define CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ (64 * 1024) |
| 24 | #endif |
| 25 | |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 26 | #ifndef CONFIG_SYS_BOOTM_LEN |
| 27 | #define CONFIG_SYS_BOOTM_LEN (64 << 20) |
| 28 | #endif |
| 29 | |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 30 | struct spl_fit_info { |
| 31 | const void *fit; /* Pointer to a valid FIT blob */ |
| 32 | size_t ext_data_offset; /* Offset to FIT external data (end of FIT) */ |
| 33 | int images_node; /* FDT offset to "/images" node */ |
Alexandru Gagniuc | fa11a44 | 2021-01-20 10:46:53 -0600 | [diff] [blame] | 34 | int conf_node; /* FDT offset to selected configuration node */ |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 35 | }; |
| 36 | |
Alexandru Gagniuc | 44af2a5 | 2021-01-20 10:46:49 -0600 | [diff] [blame] | 37 | __weak void board_spl_fit_post_load(const void *fit) |
Ye Li | 9d5b0f4 | 2018-11-17 09:10:25 +0000 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
| 41 | __weak ulong board_spl_fit_size_align(ulong size) |
| 42 | { |
| 43 | return size; |
| 44 | } |
| 45 | |
Jean-Jacques Hiblot | 9037b7f | 2019-10-22 16:39:22 +0200 | [diff] [blame] | 46 | static int find_node_from_desc(const void *fit, int node, const char *str) |
| 47 | { |
| 48 | int child; |
| 49 | |
| 50 | if (node < 0) |
| 51 | return -EINVAL; |
| 52 | |
| 53 | /* iterate the FIT nodes and find a matching description */ |
| 54 | for (child = fdt_first_subnode(fit, node); child >= 0; |
| 55 | child = fdt_next_subnode(fit, child)) { |
| 56 | int len; |
| 57 | const char *desc = fdt_getprop(fit, child, "description", &len); |
| 58 | |
| 59 | if (!desc) |
| 60 | continue; |
| 61 | |
| 62 | if (!strcmp(desc, str)) |
| 63 | return child; |
| 64 | } |
| 65 | |
| 66 | return -ENOENT; |
| 67 | } |
| 68 | |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 69 | /** |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 70 | * spl_fit_get_image_name(): By using the matching configuration subnode, |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 71 | * retrieve the name of an image, specified by a property name and an index |
| 72 | * into that. |
| 73 | * @fit: Pointer to the FDT blob. |
| 74 | * @images: Offset of the /images subnode. |
| 75 | * @type: Name of the property within the configuration subnode. |
| 76 | * @index: Index into the list of strings in this property. |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 77 | * @outname: Name of the image |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 78 | * |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 79 | * Return: 0 on success, or a negative error number |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 80 | */ |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 81 | static int spl_fit_get_image_name(const struct spl_fit_info *ctx, |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 82 | const char *type, int index, |
Jean-Jacques Hiblot | 64e82dc | 2019-10-22 16:39:17 +0200 | [diff] [blame] | 83 | const char **outname) |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 84 | { |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 85 | struct udevice *sysinfo; |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 86 | const char *name, *str; |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 87 | __maybe_unused int node; |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 88 | int len, i; |
Jean-Jacques Hiblot | 9037b7f | 2019-10-22 16:39:22 +0200 | [diff] [blame] | 89 | bool found = true; |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 90 | |
Alexandru Gagniuc | fa11a44 | 2021-01-20 10:46:53 -0600 | [diff] [blame] | 91 | name = fdt_getprop(ctx->fit, ctx->conf_node, type, &len); |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 92 | if (!name) { |
| 93 | debug("cannot find property '%s': %d\n", type, len); |
| 94 | return -EINVAL; |
| 95 | } |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 96 | |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 97 | str = name; |
| 98 | for (i = 0; i < index; i++) { |
| 99 | str = strchr(str, '\0') + 1; |
| 100 | if (!str || (str - name >= len)) { |
Jean-Jacques Hiblot | 9037b7f | 2019-10-22 16:39:22 +0200 | [diff] [blame] | 101 | found = false; |
| 102 | break; |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 103 | } |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 106 | if (!found && CONFIG_IS_ENABLED(SYSINFO) && !sysinfo_get(&sysinfo)) { |
Jean-Jacques Hiblot | 9037b7f | 2019-10-22 16:39:22 +0200 | [diff] [blame] | 107 | int rc; |
| 108 | /* |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 109 | * no string in the property for this index. Check if the |
| 110 | * sysinfo-level code can supply one. |
Jean-Jacques Hiblot | 9037b7f | 2019-10-22 16:39:22 +0200 | [diff] [blame] | 111 | */ |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 112 | rc = sysinfo_get_fit_loadable(sysinfo, index - i - 1, type, |
| 113 | &str); |
Jean-Jacques Hiblot | 9037b7f | 2019-10-22 16:39:22 +0200 | [diff] [blame] | 114 | if (rc && rc != -ENOENT) |
| 115 | return rc; |
| 116 | |
| 117 | if (!rc) { |
| 118 | /* |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 119 | * The sysinfo provided a name for a loadable. |
Jean-Jacques Hiblot | 9037b7f | 2019-10-22 16:39:22 +0200 | [diff] [blame] | 120 | * Try to match it against the description properties |
| 121 | * first. If no matching node is found, use it as a |
| 122 | * node name. |
| 123 | */ |
| 124 | int node; |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 125 | int images = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH); |
Jean-Jacques Hiblot | 9037b7f | 2019-10-22 16:39:22 +0200 | [diff] [blame] | 126 | |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 127 | node = find_node_from_desc(ctx->fit, images, str); |
Jean-Jacques Hiblot | 9037b7f | 2019-10-22 16:39:22 +0200 | [diff] [blame] | 128 | if (node > 0) |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 129 | str = fdt_get_name(ctx->fit, node, NULL); |
Jean-Jacques Hiblot | 9037b7f | 2019-10-22 16:39:22 +0200 | [diff] [blame] | 130 | |
| 131 | found = true; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (!found) { |
| 136 | debug("no string for index %d\n", index); |
| 137 | return -E2BIG; |
| 138 | } |
| 139 | |
| 140 | *outname = str; |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * spl_fit_get_image_node(): By using the matching configuration subnode, |
| 146 | * retrieve the name of an image, specified by a property name and an index |
| 147 | * into that. |
| 148 | * @fit: Pointer to the FDT blob. |
| 149 | * @images: Offset of the /images subnode. |
| 150 | * @type: Name of the property within the configuration subnode. |
| 151 | * @index: Index into the list of strings in this property. |
| 152 | * |
| 153 | * Return: the node offset of the respective image node or a negative |
| 154 | * error number. |
| 155 | */ |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 156 | static int spl_fit_get_image_node(const struct spl_fit_info *ctx, |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 157 | const char *type, int index) |
| 158 | { |
Jean-Jacques Hiblot | 64e82dc | 2019-10-22 16:39:17 +0200 | [diff] [blame] | 159 | const char *str; |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 160 | int err; |
| 161 | int node; |
| 162 | |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 163 | err = spl_fit_get_image_name(ctx, type, index, &str); |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 164 | if (err) |
| 165 | return err; |
| 166 | |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 167 | debug("%s: '%s'\n", type, str); |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 168 | |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 169 | node = fdt_subnode_offset(ctx->fit, ctx->images_node, str); |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 170 | if (node < 0) { |
Jean-Jacques Hiblot | 9443a73 | 2019-10-22 16:39:15 +0200 | [diff] [blame] | 171 | pr_err("cannot find image node '%s': %d\n", str, node); |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 172 | return -EINVAL; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 173 | } |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 174 | |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 175 | return node; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Lokesh Vutla | f62cef1 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 178 | static int get_aligned_image_offset(struct spl_load_info *info, int offset) |
| 179 | { |
| 180 | /* |
| 181 | * If it is a FS read, get the first address before offset which is |
| 182 | * aligned to ARCH_DMA_MINALIGN. If it is raw read return the |
| 183 | * block number to which offset belongs. |
| 184 | */ |
| 185 | if (info->filename) |
| 186 | return offset & ~(ARCH_DMA_MINALIGN - 1); |
| 187 | |
| 188 | return offset / info->bl_len; |
| 189 | } |
| 190 | |
| 191 | static int get_aligned_image_overhead(struct spl_load_info *info, int offset) |
| 192 | { |
| 193 | /* |
| 194 | * If it is a FS read, get the difference between the offset and |
| 195 | * the first address before offset which is aligned to |
| 196 | * ARCH_DMA_MINALIGN. If it is raw read return the offset within the |
| 197 | * block. |
| 198 | */ |
| 199 | if (info->filename) |
| 200 | return offset & (ARCH_DMA_MINALIGN - 1); |
| 201 | |
| 202 | return offset % info->bl_len; |
| 203 | } |
| 204 | |
| 205 | static int get_aligned_image_size(struct spl_load_info *info, int data_size, |
| 206 | int offset) |
| 207 | { |
Lokesh Vutla | f0531a6 | 2016-07-19 14:56:14 +0530 | [diff] [blame] | 208 | data_size = data_size + get_aligned_image_overhead(info, offset); |
| 209 | |
Lokesh Vutla | f62cef1 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 210 | if (info->filename) |
Lokesh Vutla | f0531a6 | 2016-07-19 14:56:14 +0530 | [diff] [blame] | 211 | return data_size; |
Lokesh Vutla | f62cef1 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 212 | |
| 213 | return (data_size + info->bl_len - 1) / info->bl_len; |
| 214 | } |
| 215 | |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 216 | /** |
| 217 | * spl_load_fit_image(): load the image described in a certain FIT node |
| 218 | * @info: points to information about the device to load data from |
| 219 | * @sector: the start sector of the FIT image on the device |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 220 | * @ctx: points to the FIT context structure |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 221 | * @node: offset of the DT node describing the image to load (relative |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 222 | * to @fit) |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 223 | * @image_info: will be filled with information about the loaded image |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 224 | * If the FIT node does not contain a "load" (address) property, |
| 225 | * the image gets loaded to the address pointed to by the |
| 226 | * load_addr member in this struct. |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 227 | * |
| 228 | * Return: 0 on success or a negative error number. |
| 229 | */ |
| 230 | static int spl_load_fit_image(struct spl_load_info *info, ulong sector, |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 231 | const struct spl_fit_info *ctx, int node, |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 232 | struct spl_image_info *image_info) |
| 233 | { |
Michal Simek | f707b5a | 2018-07-18 14:33:15 +0200 | [diff] [blame] | 234 | int offset; |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 235 | size_t length; |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 236 | int len; |
York Sun | 25d65f1 | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 237 | ulong size; |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 238 | ulong load_addr, load_ptr; |
| 239 | void *src; |
| 240 | ulong overhead; |
| 241 | int nr_sectors; |
| 242 | int align_len = ARCH_DMA_MINALIGN - 1; |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 243 | uint8_t image_comp = -1, type = -1; |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 244 | const void *data; |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 245 | const void *fit = ctx->fit; |
Peng Fan | 8876c7e | 2017-12-05 13:20:59 +0800 | [diff] [blame] | 246 | bool external_data = false; |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 247 | |
Michal Simek | 1aab114 | 2020-09-09 14:41:56 +0200 | [diff] [blame] | 248 | if (IS_ENABLED(CONFIG_SPL_FPGA) || |
Marek Vasut | 72bc5d5 | 2018-06-01 23:19:29 +0200 | [diff] [blame] | 249 | (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) { |
| 250 | if (fit_image_get_type(fit, node, &type)) |
| 251 | puts("Cannot get image type.\n"); |
| 252 | else |
| 253 | debug("%s ", genimg_get_type_name(type)); |
| 254 | } |
| 255 | |
Klaus H. Sorensen | 4a9a042 | 2019-12-11 11:03:33 +0000 | [diff] [blame] | 256 | if (IS_ENABLED(CONFIG_SPL_GZIP)) { |
| 257 | fit_image_get_comp(fit, node, &image_comp); |
| 258 | debug("%s ", genimg_get_comp_name(image_comp)); |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 259 | } |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 260 | |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 261 | if (fit_image_get_load(fit, node, &load_addr)) |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 262 | load_addr = image_info->load_addr; |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 263 | |
Peng Fan | 8876c7e | 2017-12-05 13:20:59 +0800 | [diff] [blame] | 264 | if (!fit_image_get_data_position(fit, node, &offset)) { |
| 265 | external_data = true; |
| 266 | } else if (!fit_image_get_data_offset(fit, node, &offset)) { |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 267 | offset += ctx->ext_data_offset; |
Peng Fan | 8876c7e | 2017-12-05 13:20:59 +0800 | [diff] [blame] | 268 | external_data = true; |
| 269 | } |
| 270 | |
| 271 | if (external_data) { |
| 272 | /* External data */ |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 273 | if (fit_image_get_data_size(fit, node, &len)) |
| 274 | return -ENOENT; |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 275 | |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 276 | load_ptr = (load_addr + align_len) & ~align_len; |
| 277 | length = len; |
| 278 | |
| 279 | overhead = get_aligned_image_overhead(info, offset); |
| 280 | nr_sectors = get_aligned_image_size(info, length, offset); |
| 281 | |
Michal Simek | f707b5a | 2018-07-18 14:33:15 +0200 | [diff] [blame] | 282 | if (info->read(info, |
| 283 | sector + get_aligned_image_offset(info, offset), |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 284 | nr_sectors, (void *)load_ptr) != nr_sectors) |
| 285 | return -EIO; |
| 286 | |
| 287 | debug("External data: dst=%lx, offset=%x, size=%lx\n", |
| 288 | load_ptr, offset, (unsigned long)length); |
| 289 | src = (void *)load_ptr + overhead; |
| 290 | } else { |
| 291 | /* Embedded data */ |
| 292 | if (fit_image_get_data(fit, node, &data, &length)) { |
| 293 | puts("Cannot get image data/size\n"); |
| 294 | return -ENOENT; |
| 295 | } |
| 296 | debug("Embedded data: dst=%lx, size=%lx\n", load_addr, |
| 297 | (unsigned long)length); |
| 298 | src = (void *)data; |
| 299 | } |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 300 | |
Alexandru Gagniuc | a4fed79 | 2021-01-20 10:46:55 -0600 | [diff] [blame] | 301 | if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) { |
| 302 | printf("## Checking hash(es) for Image %s ... ", |
| 303 | fit_get_name(fit, node, NULL)); |
| 304 | if (!fit_image_verify_with_data(fit, node, src, length)) |
| 305 | return -EPERM; |
| 306 | puts("OK\n"); |
| 307 | } |
Ben Whitten | 54a9119 | 2018-06-07 11:37:27 +0100 | [diff] [blame] | 308 | |
Alexandru Gagniuc | a4fed79 | 2021-01-20 10:46:55 -0600 | [diff] [blame] | 309 | if (CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS)) |
| 310 | board_fit_image_post_process(&src, &length); |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 311 | |
Michal Simek | f354c3f | 2018-07-24 15:05:00 +0200 | [diff] [blame] | 312 | if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) { |
York Sun | 25d65f1 | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 313 | size = length; |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 314 | if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN, |
York Sun | 25d65f1 | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 315 | src, &size)) { |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 316 | puts("Uncompressing error\n"); |
| 317 | return -EIO; |
| 318 | } |
York Sun | 25d65f1 | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 319 | length = size; |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 320 | } else { |
| 321 | memcpy((void *)load_addr, src, length); |
| 322 | } |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 323 | |
| 324 | if (image_info) { |
Michal Simek | 9e64a55 | 2020-09-03 11:24:28 +0200 | [diff] [blame] | 325 | ulong entry_point; |
| 326 | |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 327 | image_info->load_addr = load_addr; |
| 328 | image_info->size = length; |
Michal Simek | 9e64a55 | 2020-09-03 11:24:28 +0200 | [diff] [blame] | 329 | |
| 330 | if (!fit_image_get_entry(fit, node, &entry_point)) |
| 331 | image_info->entry_point = entry_point; |
| 332 | else |
| 333 | image_info->entry_point = FDT_ERROR; |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
Alexandru Gagniuc | 769ed25 | 2021-01-20 10:46:56 -0600 | [diff] [blame] | 339 | static bool os_takes_devicetree(uint8_t os) |
| 340 | { |
| 341 | switch (os) { |
| 342 | case IH_OS_U_BOOT: |
| 343 | return true; |
| 344 | case IH_OS_LINUX: |
| 345 | return IS_ENABLED(CONFIG_SPL_OS_BOOT); |
| 346 | default: |
| 347 | return false; |
| 348 | } |
| 349 | } |
| 350 | |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 351 | static int spl_fit_append_fdt(struct spl_image_info *spl_image, |
| 352 | struct spl_load_info *info, ulong sector, |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 353 | const struct spl_fit_info *ctx) |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 354 | { |
| 355 | struct spl_image_info image_info; |
Michal Simek | bf703df | 2019-10-22 16:39:11 +0200 | [diff] [blame] | 356 | int node, ret = 0, index = 0; |
Lukas Auer | 80afee7 | 2019-08-21 21:14:42 +0200 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * Use the address following the image as target address for the |
Marek Vasut | c27cbe8 | 2020-10-19 23:40:26 +0200 | [diff] [blame] | 360 | * device tree. |
Lukas Auer | 80afee7 | 2019-08-21 21:14:42 +0200 | [diff] [blame] | 361 | */ |
Marek Vasut | c27cbe8 | 2020-10-19 23:40:26 +0200 | [diff] [blame] | 362 | image_info.load_addr = spl_image->load_addr + spl_image->size; |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 363 | |
| 364 | /* Figure out which device tree the board wants to use */ |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 365 | node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index++); |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 366 | if (node < 0) { |
| 367 | debug("%s: cannot find FDT node\n", __func__); |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 368 | |
Lukas Auer | 80afee7 | 2019-08-21 21:14:42 +0200 | [diff] [blame] | 369 | /* |
| 370 | * U-Boot did not find a device tree inside the FIT image. Use |
| 371 | * the U-Boot device tree instead. |
| 372 | */ |
| 373 | if (gd->fdt_blob) |
| 374 | memcpy((void *)image_info.load_addr, gd->fdt_blob, |
| 375 | fdt_totalsize(gd->fdt_blob)); |
| 376 | else |
| 377 | return node; |
| 378 | } else { |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 379 | ret = spl_load_fit_image(info, sector, ctx, node, |
Lukas Auer | 80afee7 | 2019-08-21 21:14:42 +0200 | [diff] [blame] | 380 | &image_info); |
| 381 | if (ret < 0) |
| 382 | return ret; |
| 383 | } |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 384 | |
| 385 | /* Make the load-address of the FDT available for the SPL framework */ |
| 386 | spl_image->fdt_addr = (void *)image_info.load_addr; |
Alexandru Gagniuc | a4fed79 | 2021-01-20 10:46:55 -0600 | [diff] [blame] | 387 | if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY)) |
| 388 | return 0; |
| 389 | |
Michal Simek | bf703df | 2019-10-22 16:39:11 +0200 | [diff] [blame] | 390 | if (CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)) { |
Jean-Jacques Hiblot | 0da5815 | 2019-10-22 16:39:13 +0200 | [diff] [blame] | 391 | void *tmpbuffer = NULL; |
| 392 | |
Michal Simek | bf703df | 2019-10-22 16:39:11 +0200 | [diff] [blame] | 393 | for (; ; index++) { |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 394 | node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index); |
Jean-Jacques Hiblot | c6ab6a1 | 2019-10-22 16:39:14 +0200 | [diff] [blame] | 395 | if (node == -E2BIG) { |
Michal Simek | bf703df | 2019-10-22 16:39:11 +0200 | [diff] [blame] | 396 | debug("%s: No additional FDT node\n", __func__); |
Jean-Jacques Hiblot | 0da5815 | 2019-10-22 16:39:13 +0200 | [diff] [blame] | 397 | break; |
Jean-Jacques Hiblot | c6ab6a1 | 2019-10-22 16:39:14 +0200 | [diff] [blame] | 398 | } else if (node < 0) { |
| 399 | debug("%s: unable to find FDT node %d\n", |
| 400 | __func__, index); |
| 401 | continue; |
Michal Simek | bf703df | 2019-10-22 16:39:11 +0200 | [diff] [blame] | 402 | } |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 403 | |
Jean-Jacques Hiblot | 0da5815 | 2019-10-22 16:39:13 +0200 | [diff] [blame] | 404 | if (!tmpbuffer) { |
| 405 | /* |
| 406 | * allocate memory to store the DT overlay |
| 407 | * before it is applied. It may not be used |
| 408 | * depending on how the overlay is stored, so |
| 409 | * don't fail yet if the allocation failed. |
| 410 | */ |
| 411 | tmpbuffer = malloc(CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ); |
| 412 | if (!tmpbuffer) |
| 413 | debug("%s: unable to allocate space for overlays\n", |
| 414 | __func__); |
| 415 | } |
| 416 | image_info.load_addr = (ulong)tmpbuffer; |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 417 | ret = spl_load_fit_image(info, sector, ctx, |
Michal Simek | bf703df | 2019-10-22 16:39:11 +0200 | [diff] [blame] | 418 | node, &image_info); |
| 419 | if (ret < 0) |
Jean-Jacques Hiblot | 0da5815 | 2019-10-22 16:39:13 +0200 | [diff] [blame] | 420 | break; |
Michal Simek | bf703df | 2019-10-22 16:39:11 +0200 | [diff] [blame] | 421 | |
Jean-Jacques Hiblot | 74addb6 | 2019-10-22 16:39:12 +0200 | [diff] [blame] | 422 | /* Make room in FDT for changes from the overlay */ |
| 423 | ret = fdt_increase_size(spl_image->fdt_addr, |
| 424 | image_info.size); |
| 425 | if (ret < 0) |
Jean-Jacques Hiblot | 0da5815 | 2019-10-22 16:39:13 +0200 | [diff] [blame] | 426 | break; |
Jean-Jacques Hiblot | 74addb6 | 2019-10-22 16:39:12 +0200 | [diff] [blame] | 427 | |
Michal Simek | bf703df | 2019-10-22 16:39:11 +0200 | [diff] [blame] | 428 | ret = fdt_overlay_apply_verbose(spl_image->fdt_addr, |
| 429 | (void *)image_info.load_addr); |
Jean-Jacques Hiblot | 9443a73 | 2019-10-22 16:39:15 +0200 | [diff] [blame] | 430 | if (ret) { |
| 431 | pr_err("failed to apply DT overlay %s\n", |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 432 | fit_get_name(ctx->fit, node, NULL)); |
Jean-Jacques Hiblot | 0da5815 | 2019-10-22 16:39:13 +0200 | [diff] [blame] | 433 | break; |
Jean-Jacques Hiblot | 9443a73 | 2019-10-22 16:39:15 +0200 | [diff] [blame] | 434 | } |
Michal Simek | bf703df | 2019-10-22 16:39:11 +0200 | [diff] [blame] | 435 | |
| 436 | debug("%s: DT overlay %s applied\n", __func__, |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 437 | fit_get_name(ctx->fit, node, NULL)); |
Michal Simek | bf703df | 2019-10-22 16:39:11 +0200 | [diff] [blame] | 438 | } |
Heinrich Schuchardt | 939a961 | 2020-04-20 12:44:01 +0200 | [diff] [blame] | 439 | free(tmpbuffer); |
Jean-Jacques Hiblot | 0da5815 | 2019-10-22 16:39:13 +0200 | [diff] [blame] | 440 | if (ret) |
| 441 | return ret; |
Michal Simek | bf703df | 2019-10-22 16:39:11 +0200 | [diff] [blame] | 442 | } |
Jean-Jacques Hiblot | 74addb6 | 2019-10-22 16:39:12 +0200 | [diff] [blame] | 443 | /* Try to make space, so we can inject details on the loadables */ |
| 444 | ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192); |
| 445 | if (ret < 0) |
| 446 | return ret; |
Jean-Jacques Hiblot | 74addb6 | 2019-10-22 16:39:12 +0200 | [diff] [blame] | 447 | |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 448 | return ret; |
| 449 | } |
| 450 | |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 451 | static int spl_fit_record_loadable(const struct spl_fit_info *ctx, int index, |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 452 | void *blob, struct spl_image_info *image) |
| 453 | { |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 454 | int ret = 0; |
Jean-Jacques Hiblot | 64e82dc | 2019-10-22 16:39:17 +0200 | [diff] [blame] | 455 | const char *name; |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 456 | int node; |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 457 | |
Alexandru Gagniuc | a4fed79 | 2021-01-20 10:46:55 -0600 | [diff] [blame] | 458 | if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY)) |
| 459 | return 0; |
| 460 | |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 461 | ret = spl_fit_get_image_name(ctx, "loadables", index, &name); |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 462 | if (ret < 0) |
| 463 | return ret; |
| 464 | |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 465 | node = spl_fit_get_image_node(ctx, "loadables", index); |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 466 | |
| 467 | ret = fdt_record_loadable(blob, index, name, image->load_addr, |
| 468 | image->size, image->entry_point, |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 469 | fdt_getprop(ctx->fit, node, "type", NULL), |
| 470 | fdt_getprop(ctx->fit, node, "os", NULL)); |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 471 | return ret; |
| 472 | } |
| 473 | |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 474 | static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os) |
| 475 | { |
Alexandru Gagniuc | a4fed79 | 2021-01-20 10:46:55 -0600 | [diff] [blame] | 476 | if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) || CONFIG_IS_ENABLED(OS_BOOT)) |
| 477 | return fit_image_get_os(fit, noffset, os); |
Samuel Holland | e646f51 | 2020-10-21 21:12:13 -0500 | [diff] [blame] | 478 | |
Alexandru Gagniuc | a4fed79 | 2021-01-20 10:46:55 -0600 | [diff] [blame] | 479 | const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL); |
Samuel Holland | e646f51 | 2020-10-21 21:12:13 -0500 | [diff] [blame] | 480 | if (!name) |
| 481 | return -ENOENT; |
| 482 | |
| 483 | /* |
| 484 | * We don't care what the type of the image actually is, |
| 485 | * only whether or not it is U-Boot. This saves some |
| 486 | * space by omitting the large table of OS types. |
| 487 | */ |
| 488 | if (!strcmp(name, "u-boot")) |
| 489 | *os = IH_OS_U_BOOT; |
| 490 | else |
| 491 | *os = IH_OS_INVALID; |
| 492 | |
| 493 | return 0; |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 496 | /* |
Alexandru Gagniuc | 0256017 | 2020-10-21 18:32:58 -0500 | [diff] [blame] | 497 | * The purpose of the FIT load buffer is to provide a memory location that is |
| 498 | * independent of the load address of any FIT component. |
| 499 | */ |
| 500 | static void *spl_get_fit_load_buffer(size_t size) |
| 501 | { |
| 502 | void *buf; |
| 503 | |
| 504 | buf = malloc(size); |
| 505 | if (!buf) { |
| 506 | pr_err("Could not get FIT buffer of %lu bytes\n", (ulong)size); |
| 507 | pr_err("\tcheck CONFIG_SYS_SPL_MALLOC_SIZE\n"); |
| 508 | buf = spl_get_load_buffer(0, size); |
| 509 | } |
| 510 | return buf; |
| 511 | } |
| 512 | |
| 513 | /* |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 514 | * Weak default function to allow customizing SPL fit loading for load-only |
| 515 | * use cases by allowing to skip the parsing/processing of the FIT contents |
| 516 | * (so that this can be done separately in a more customized fashion) |
| 517 | */ |
| 518 | __weak bool spl_load_simple_fit_skip_processing(void) |
| 519 | { |
| 520 | return false; |
| 521 | } |
| 522 | |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 523 | static int spl_simple_fit_read(struct spl_fit_info *ctx, |
| 524 | struct spl_load_info *info, ulong sector, |
| 525 | const void *fit_header) |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 526 | { |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 527 | unsigned long count, size; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 528 | int sectors; |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 529 | void *buf; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 530 | |
| 531 | /* |
York Sun | a058b8a | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 532 | * For FIT with external data, figure out where the external images |
| 533 | * start. This is the base for the data-offset properties in each |
| 534 | * image. |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 535 | */ |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 536 | size = ALIGN(fdt_totalsize(fit_header), 4); |
Ye Li | 9d5b0f4 | 2018-11-17 09:10:25 +0000 | [diff] [blame] | 537 | size = board_spl_fit_size_align(size); |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 538 | ctx->ext_data_offset = ALIGN(size, 4); |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 539 | |
| 540 | /* |
| 541 | * So far we only have one block of data from the FIT. Read the entire |
Alexandru Gagniuc | 0256017 | 2020-10-21 18:32:58 -0500 | [diff] [blame] | 542 | * thing, including that first block. |
York Sun | a058b8a | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 543 | * |
| 544 | * For FIT with data embedded, data is loaded as part of FIT image. |
| 545 | * For FIT with external data, data is not loaded in this step. |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 546 | */ |
Lokesh Vutla | f62cef1 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 547 | sectors = get_aligned_image_size(info, size, 0); |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 548 | buf = spl_get_fit_load_buffer(sectors * info->bl_len); |
Ye Li | 9d5b0f4 | 2018-11-17 09:10:25 +0000 | [diff] [blame] | 549 | |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 550 | count = info->read(info, sector, sectors, buf); |
| 551 | ctx->fit = buf; |
| 552 | debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n", |
| 553 | sector, sectors, buf, count, size); |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 554 | |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 555 | return (count == 0) ? -EIO : 0; |
| 556 | } |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 557 | |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 558 | static int spl_simple_fit_parse(struct spl_fit_info *ctx) |
| 559 | { |
Alexandru Gagniuc | fa11a44 | 2021-01-20 10:46:53 -0600 | [diff] [blame] | 560 | /* Find the correct subnode under "/configurations" */ |
| 561 | ctx->conf_node = fit_find_config_node(ctx->fit); |
| 562 | if (ctx->conf_node < 0) |
| 563 | return -EINVAL; |
Philippe Reynes | 6c5c03c | 2020-10-29 18:50:29 +0100 | [diff] [blame] | 564 | |
Alexandru Gagniuc | fa11a44 | 2021-01-20 10:46:53 -0600 | [diff] [blame] | 565 | if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) { |
Philippe Reynes | 6c5c03c | 2020-10-29 18:50:29 +0100 | [diff] [blame] | 566 | printf("## Checking hash(es) for config %s ... ", |
Alexandru Gagniuc | fa11a44 | 2021-01-20 10:46:53 -0600 | [diff] [blame] | 567 | fit_get_name(ctx->fit, ctx->conf_node, NULL)); |
| 568 | if (fit_config_verify(ctx->fit, ctx->conf_node)) |
Philippe Reynes | 6c5c03c | 2020-10-29 18:50:29 +0100 | [diff] [blame] | 569 | return -EPERM; |
| 570 | puts("OK\n"); |
| 571 | } |
| 572 | |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 573 | /* find the node holding the images information */ |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 574 | ctx->images_node = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH); |
| 575 | if (ctx->images_node < 0) { |
| 576 | debug("%s: Cannot find /images node: %d\n", __func__, |
| 577 | ctx->images_node); |
| 578 | return -EINVAL; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 579 | } |
Marek Vasut | 33dd00e | 2018-05-12 22:25:28 +0200 | [diff] [blame] | 580 | |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | int spl_load_simple_fit(struct spl_image_info *spl_image, |
| 585 | struct spl_load_info *info, ulong sector, void *fit) |
| 586 | { |
| 587 | struct spl_image_info image_info; |
| 588 | struct spl_fit_info ctx; |
| 589 | int node = -1; |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 590 | int ret; |
Alexandru Gagniuc | 23243c9 | 2021-01-20 10:46:50 -0600 | [diff] [blame] | 591 | int index = 0; |
| 592 | int firmware_node; |
| 593 | |
| 594 | ret = spl_simple_fit_read(&ctx, info, sector, fit); |
| 595 | if (ret < 0) |
| 596 | return ret; |
| 597 | |
| 598 | /* skip further processing if requested to enable load-only use cases */ |
| 599 | if (spl_load_simple_fit_skip_processing()) |
| 600 | return 0; |
| 601 | |
| 602 | ret = spl_simple_fit_parse(&ctx); |
| 603 | if (ret < 0) |
| 604 | return ret; |
| 605 | |
Michal Simek | 1aab114 | 2020-09-09 14:41:56 +0200 | [diff] [blame] | 606 | #ifdef CONFIG_SPL_FPGA |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 607 | node = spl_fit_get_image_node(&ctx, "fpga", 0); |
Marek Vasut | 33dd00e | 2018-05-12 22:25:28 +0200 | [diff] [blame] | 608 | if (node >= 0) { |
| 609 | /* Load the image and set up the spl_image structure */ |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 610 | ret = spl_load_fit_image(info, sector, &ctx, node, spl_image); |
Marek Vasut | 33dd00e | 2018-05-12 22:25:28 +0200 | [diff] [blame] | 611 | if (ret) { |
| 612 | printf("%s: Cannot load the FPGA: %i\n", __func__, ret); |
| 613 | return ret; |
| 614 | } |
Michal Simek | f707b5a | 2018-07-18 14:33:15 +0200 | [diff] [blame] | 615 | |
| 616 | debug("FPGA bitstream at: %x, size: %x\n", |
| 617 | (u32)spl_image->load_addr, spl_image->size); |
| 618 | |
| 619 | ret = fpga_load(0, (const void *)spl_image->load_addr, |
| 620 | spl_image->size, BIT_FULL); |
| 621 | if (ret) { |
| 622 | printf("%s: Cannot load the image to the FPGA\n", |
| 623 | __func__); |
| 624 | return ret; |
| 625 | } |
| 626 | |
Luis Araneda | eece623 | 2018-07-19 03:10:16 -0400 | [diff] [blame] | 627 | puts("FPGA image loaded from FIT\n"); |
Marek Vasut | 33dd00e | 2018-05-12 22:25:28 +0200 | [diff] [blame] | 628 | node = -1; |
| 629 | } |
| 630 | #endif |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 631 | |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 632 | /* |
| 633 | * Find the U-Boot image using the following search order: |
| 634 | * - start at 'firmware' (e.g. an ARM Trusted Firmware) |
| 635 | * - fall back 'kernel' (e.g. a Falcon-mode OS boot |
| 636 | * - fall back to using the first 'loadables' entry |
| 637 | */ |
| 638 | if (node < 0) |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 639 | node = spl_fit_get_image_node(&ctx, FIT_FIRMWARE_PROP, 0); |
Alexandru Gagniuc | a4fed79 | 2021-01-20 10:46:55 -0600 | [diff] [blame] | 640 | |
| 641 | if (node < 0 && IS_ENABLED(CONFIG_SPL_OS_BOOT)) |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 642 | node = spl_fit_get_image_node(&ctx, FIT_KERNEL_PROP, 0); |
Alexandru Gagniuc | a4fed79 | 2021-01-20 10:46:55 -0600 | [diff] [blame] | 643 | |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 644 | if (node < 0) { |
| 645 | debug("could not find firmware image, trying loadables...\n"); |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 646 | node = spl_fit_get_image_node(&ctx, "loadables", 0); |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 647 | /* |
| 648 | * If we pick the U-Boot image from "loadables", start at |
| 649 | * the second image when later loading additional images. |
| 650 | */ |
| 651 | index = 1; |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 652 | } |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 653 | if (node < 0) { |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 654 | debug("%s: Cannot find u-boot image node: %d\n", |
| 655 | __func__, node); |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 656 | return -1; |
| 657 | } |
| 658 | |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 659 | /* Load the image and set up the spl_image structure */ |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 660 | ret = spl_load_fit_image(info, sector, &ctx, node, spl_image); |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 661 | if (ret) |
| 662 | return ret; |
Daniel Allred | cf839bd | 2016-06-27 09:19:21 -0500 | [diff] [blame] | 663 | |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 664 | /* |
| 665 | * For backward compatibility, we treat the first node that is |
| 666 | * as a U-Boot image, if no OS-type has been declared. |
| 667 | */ |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 668 | if (!spl_fit_image_get_os(ctx.fit, node, &spl_image->os)) |
York Sun | a058b8a | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 669 | debug("Image OS is %s\n", genimg_get_os_name(spl_image->os)); |
Alexandru Gagniuc | a4fed79 | 2021-01-20 10:46:55 -0600 | [diff] [blame] | 670 | else if (!IS_ENABLED(CONFIG_SPL_OS_BOOT)) |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 671 | spl_image->os = IH_OS_U_BOOT; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 672 | |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 673 | /* |
| 674 | * Booting a next-stage U-Boot may require us to append the FDT. |
| 675 | * We allow this to fail, as the U-Boot image might embed its FDT. |
| 676 | */ |
Alexandru Gagniuc | 769ed25 | 2021-01-20 10:46:56 -0600 | [diff] [blame] | 677 | if (os_takes_devicetree(spl_image->os)) { |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 678 | ret = spl_fit_append_fdt(spl_image, info, sector, &ctx); |
Alexandru Gagniuc | 769ed25 | 2021-01-20 10:46:56 -0600 | [diff] [blame] | 679 | if (ret < 0 && spl_image->os != IH_OS_U_BOOT) |
Dario Binacchi | faf8d14 | 2020-05-27 13:56:19 +0200 | [diff] [blame] | 680 | return ret; |
| 681 | } |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 682 | |
Jean-Jacques Hiblot | 776ce60 | 2019-10-22 16:39:10 +0200 | [diff] [blame] | 683 | firmware_node = node; |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 684 | /* Now check if there are more images for us to load */ |
| 685 | for (; ; index++) { |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 686 | uint8_t os_type = IH_OS_INVALID; |
| 687 | |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 688 | node = spl_fit_get_image_node(&ctx, "loadables", index); |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 689 | if (node < 0) |
| 690 | break; |
| 691 | |
Jean-Jacques Hiblot | 776ce60 | 2019-10-22 16:39:10 +0200 | [diff] [blame] | 692 | /* |
| 693 | * if the firmware is also a loadable, skip it because |
| 694 | * it already has been loaded. This is typically the case with |
| 695 | * u-boot.img generated by mkimage. |
| 696 | */ |
| 697 | if (firmware_node == node) |
| 698 | continue; |
| 699 | |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 700 | ret = spl_load_fit_image(info, sector, &ctx, node, &image_info); |
Philippe Reynes | ba87da4 | 2020-11-24 16:15:05 +0100 | [diff] [blame] | 701 | if (ret < 0) { |
| 702 | printf("%s: can't load image loadables index %d (ret = %d)\n", |
| 703 | __func__, index, ret); |
| 704 | return ret; |
| 705 | } |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 706 | |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 707 | if (!spl_fit_image_get_os(ctx.fit, node, &os_type)) |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 708 | debug("Loadable is %s\n", genimg_get_os_name(os_type)); |
| 709 | |
Alexandru Gagniuc | 769ed25 | 2021-01-20 10:46:56 -0600 | [diff] [blame] | 710 | if (os_takes_devicetree(os_type)) { |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 711 | spl_fit_append_fdt(&image_info, info, sector, &ctx); |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 712 | spl_image->fdt_addr = image_info.fdt_addr; |
| 713 | } |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 714 | |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 715 | /* |
| 716 | * If the "firmware" image did not provide an entry point, |
| 717 | * use the first valid entry point from the loadables. |
| 718 | */ |
| 719 | if (spl_image->entry_point == FDT_ERROR && |
| 720 | image_info.entry_point != FDT_ERROR) |
| 721 | spl_image->entry_point = image_info.entry_point; |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 722 | |
| 723 | /* Record our loadables into the FDT */ |
| 724 | if (spl_image->fdt_addr) |
Alexandru Gagniuc | 93d1557 | 2021-01-20 10:46:51 -0600 | [diff] [blame] | 725 | spl_fit_record_loadable(&ctx, index, |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 726 | spl_image->fdt_addr, |
| 727 | &image_info); |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /* |
| 731 | * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's |
| 732 | * Makefile will set it to 0 and it will end up as the entry point |
| 733 | * here. What it actually means is: use the load address. |
| 734 | */ |
| 735 | if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0) |
| 736 | spl_image->entry_point = spl_image->load_addr; |
| 737 | |
Ye Li | 9d5b0f4 | 2018-11-17 09:10:25 +0000 | [diff] [blame] | 738 | spl_image->flags |= SPL_FIT_FOUND; |
| 739 | |
Alexandru Gagniuc | a4fed79 | 2021-01-20 10:46:55 -0600 | [diff] [blame] | 740 | if (IS_ENABLED(CONFIG_IMX_HAB)) |
| 741 | board_spl_fit_post_load(ctx.fit); |
Ye Li | 9d5b0f4 | 2018-11-17 09:10:25 +0000 | [diff] [blame] | 742 | |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 743 | return 0; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 744 | } |