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