Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | f1dcee5 | 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 | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <errno.h> |
Michal Simek | 3313ae6 | 2018-07-18 14:33:15 +0200 | [diff] [blame] | 9 | #include <fpga.h> |
Simon Glass | 0c670fc | 2019-08-01 09:46:36 -0600 | [diff] [blame] | 10 | #include <gzip.h> |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 11 | #include <image.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 12 | #include <linux/libfdt.h> |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 13 | #include <spl.h> |
| 14 | |
York Sun | 7264f29 | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 15 | #ifndef CONFIG_SYS_BOOTM_LEN |
| 16 | #define CONFIG_SYS_BOOTM_LEN (64 << 20) |
| 17 | #endif |
| 18 | |
Ye Li | e246bfc | 2018-11-17 09:10:25 +0000 | [diff] [blame] | 19 | __weak void board_spl_fit_post_load(ulong load_addr, size_t length) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | __weak ulong board_spl_fit_size_align(ulong size) |
| 24 | { |
| 25 | return size; |
| 26 | } |
| 27 | |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 28 | /** |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 29 | * spl_fit_get_image_name(): By using the matching configuration subnode, |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 30 | * retrieve the name of an image, specified by a property name and an index |
| 31 | * into that. |
| 32 | * @fit: Pointer to the FDT blob. |
| 33 | * @images: Offset of the /images subnode. |
| 34 | * @type: Name of the property within the configuration subnode. |
| 35 | * @index: Index into the list of strings in this property. |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 36 | * @outname: Name of the image |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 37 | * |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 38 | * Return: 0 on success, or a negative error number |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 39 | */ |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 40 | static int spl_fit_get_image_name(const void *fit, int images, |
| 41 | const char *type, int index, |
| 42 | char **outname) |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 43 | { |
| 44 | const char *name, *str; |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 45 | __maybe_unused int node; |
| 46 | int conf_node; |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 47 | int len, i; |
| 48 | |
Cooper Jr., Franklin | 3863f84 | 2017-06-16 17:25:05 -0500 | [diff] [blame] | 49 | conf_node = fit_find_config_node(fit); |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 50 | if (conf_node < 0) { |
| 51 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 52 | printf("No matching DT out of these options:\n"); |
| 53 | for (node = fdt_first_subnode(fit, conf_node); |
| 54 | node >= 0; |
| 55 | node = fdt_next_subnode(fit, node)) { |
| 56 | name = fdt_getprop(fit, node, "description", &len); |
| 57 | printf(" %s\n", name); |
| 58 | } |
| 59 | #endif |
| 60 | return conf_node; |
| 61 | } |
| 62 | |
| 63 | name = fdt_getprop(fit, conf_node, type, &len); |
| 64 | if (!name) { |
| 65 | debug("cannot find property '%s': %d\n", type, len); |
| 66 | return -EINVAL; |
| 67 | } |
| 68 | |
| 69 | str = name; |
| 70 | for (i = 0; i < index; i++) { |
| 71 | str = strchr(str, '\0') + 1; |
| 72 | if (!str || (str - name >= len)) { |
| 73 | debug("no string for index %d\n", index); |
| 74 | return -E2BIG; |
| 75 | } |
| 76 | } |
| 77 | |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 78 | *outname = (char *)str; |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * spl_fit_get_image_node(): By using the matching configuration subnode, |
| 84 | * retrieve the name of an image, specified by a property name and an index |
| 85 | * into that. |
| 86 | * @fit: Pointer to the FDT blob. |
| 87 | * @images: Offset of the /images subnode. |
| 88 | * @type: Name of the property within the configuration subnode. |
| 89 | * @index: Index into the list of strings in this property. |
| 90 | * |
| 91 | * Return: the node offset of the respective image node or a negative |
| 92 | * error number. |
| 93 | */ |
| 94 | static int spl_fit_get_image_node(const void *fit, int images, |
| 95 | const char *type, int index) |
| 96 | { |
| 97 | char *str; |
| 98 | int err; |
| 99 | int node; |
| 100 | |
| 101 | err = spl_fit_get_image_name(fit, images, type, index, &str); |
| 102 | if (err) |
| 103 | return err; |
| 104 | |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 105 | debug("%s: '%s'\n", type, str); |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 106 | |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 107 | node = fdt_subnode_offset(fit, images, str); |
| 108 | if (node < 0) { |
| 109 | debug("cannot find image node '%s': %d\n", str, node); |
| 110 | return -EINVAL; |
| 111 | } |
| 112 | |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 113 | return node; |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Lokesh Vutla | eafd541 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 116 | static int get_aligned_image_offset(struct spl_load_info *info, int offset) |
| 117 | { |
| 118 | /* |
| 119 | * If it is a FS read, get the first address before offset which is |
| 120 | * aligned to ARCH_DMA_MINALIGN. If it is raw read return the |
| 121 | * block number to which offset belongs. |
| 122 | */ |
| 123 | if (info->filename) |
| 124 | return offset & ~(ARCH_DMA_MINALIGN - 1); |
| 125 | |
| 126 | return offset / info->bl_len; |
| 127 | } |
| 128 | |
| 129 | static int get_aligned_image_overhead(struct spl_load_info *info, int offset) |
| 130 | { |
| 131 | /* |
| 132 | * If it is a FS read, get the difference between the offset and |
| 133 | * the first address before offset which is aligned to |
| 134 | * ARCH_DMA_MINALIGN. If it is raw read return the offset within the |
| 135 | * block. |
| 136 | */ |
| 137 | if (info->filename) |
| 138 | return offset & (ARCH_DMA_MINALIGN - 1); |
| 139 | |
| 140 | return offset % info->bl_len; |
| 141 | } |
| 142 | |
| 143 | static int get_aligned_image_size(struct spl_load_info *info, int data_size, |
| 144 | int offset) |
| 145 | { |
Lokesh Vutla | 3cc1f38 | 2016-07-19 14:56:14 +0530 | [diff] [blame] | 146 | data_size = data_size + get_aligned_image_overhead(info, offset); |
| 147 | |
Lokesh Vutla | eafd541 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 148 | if (info->filename) |
Lokesh Vutla | 3cc1f38 | 2016-07-19 14:56:14 +0530 | [diff] [blame] | 149 | return data_size; |
Lokesh Vutla | eafd541 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 150 | |
| 151 | return (data_size + info->bl_len - 1) / info->bl_len; |
| 152 | } |
| 153 | |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 154 | /** |
| 155 | * spl_load_fit_image(): load the image described in a certain FIT node |
| 156 | * @info: points to information about the device to load data from |
| 157 | * @sector: the start sector of the FIT image on the device |
| 158 | * @fit: points to the flattened device tree blob describing the FIT |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 159 | * image |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 160 | * @base_offset: the beginning of the data area containing the actual |
| 161 | * image data, relative to the beginning of the FIT |
| 162 | * @node: offset of the DT node describing the image to load (relative |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 163 | * to @fit) |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 164 | * @image_info: will be filled with information about the loaded image |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 165 | * If the FIT node does not contain a "load" (address) property, |
| 166 | * the image gets loaded to the address pointed to by the |
| 167 | * load_addr member in this struct. |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 168 | * |
| 169 | * Return: 0 on success or a negative error number. |
| 170 | */ |
| 171 | static int spl_load_fit_image(struct spl_load_info *info, ulong sector, |
| 172 | void *fit, ulong base_offset, int node, |
| 173 | struct spl_image_info *image_info) |
| 174 | { |
Michal Simek | 3313ae6 | 2018-07-18 14:33:15 +0200 | [diff] [blame] | 175 | int offset; |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 176 | size_t length; |
York Sun | 5fd13d9 | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 177 | int len; |
York Sun | 933f67a | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 178 | ulong size; |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 179 | ulong load_addr, load_ptr; |
| 180 | void *src; |
| 181 | ulong overhead; |
| 182 | int nr_sectors; |
| 183 | int align_len = ARCH_DMA_MINALIGN - 1; |
York Sun | 7264f29 | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 184 | uint8_t image_comp = -1, type = -1; |
York Sun | 5fd13d9 | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 185 | const void *data; |
Peng Fan | a1be94b | 2017-12-05 13:20:59 +0800 | [diff] [blame] | 186 | bool external_data = false; |
York Sun | 7264f29 | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 187 | |
Marek Vasut | 56419ea | 2018-06-01 23:19:29 +0200 | [diff] [blame] | 188 | if (IS_ENABLED(CONFIG_SPL_FPGA_SUPPORT) || |
| 189 | (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) { |
| 190 | if (fit_image_get_type(fit, node, &type)) |
| 191 | puts("Cannot get image type.\n"); |
| 192 | else |
| 193 | debug("%s ", genimg_get_type_name(type)); |
| 194 | } |
| 195 | |
York Sun | 7264f29 | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 196 | if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) { |
| 197 | if (fit_image_get_comp(fit, node, &image_comp)) |
| 198 | puts("Cannot get image compression format.\n"); |
| 199 | else |
| 200 | debug("%s ", genimg_get_comp_name(image_comp)); |
York Sun | 7264f29 | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 201 | } |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 202 | |
York Sun | 5fd13d9 | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 203 | if (fit_image_get_load(fit, node, &load_addr)) |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 204 | load_addr = image_info->load_addr; |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 205 | |
Peng Fan | a1be94b | 2017-12-05 13:20:59 +0800 | [diff] [blame] | 206 | if (!fit_image_get_data_position(fit, node, &offset)) { |
| 207 | external_data = true; |
| 208 | } else if (!fit_image_get_data_offset(fit, node, &offset)) { |
York Sun | 5fd13d9 | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 209 | offset += base_offset; |
Peng Fan | a1be94b | 2017-12-05 13:20:59 +0800 | [diff] [blame] | 210 | external_data = true; |
| 211 | } |
| 212 | |
| 213 | if (external_data) { |
| 214 | /* External data */ |
York Sun | 5fd13d9 | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 215 | if (fit_image_get_data_size(fit, node, &len)) |
| 216 | return -ENOENT; |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 217 | |
York Sun | 5fd13d9 | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 218 | load_ptr = (load_addr + align_len) & ~align_len; |
| 219 | length = len; |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 220 | |
York Sun | 5fd13d9 | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 221 | overhead = get_aligned_image_overhead(info, offset); |
| 222 | nr_sectors = get_aligned_image_size(info, length, offset); |
| 223 | |
Michal Simek | 3313ae6 | 2018-07-18 14:33:15 +0200 | [diff] [blame] | 224 | if (info->read(info, |
| 225 | sector + get_aligned_image_offset(info, offset), |
York Sun | 5fd13d9 | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 226 | nr_sectors, (void *)load_ptr) != nr_sectors) |
| 227 | return -EIO; |
| 228 | |
| 229 | debug("External data: dst=%lx, offset=%x, size=%lx\n", |
| 230 | load_ptr, offset, (unsigned long)length); |
| 231 | src = (void *)load_ptr + overhead; |
| 232 | } else { |
| 233 | /* Embedded data */ |
| 234 | if (fit_image_get_data(fit, node, &data, &length)) { |
| 235 | puts("Cannot get image data/size\n"); |
| 236 | return -ENOENT; |
| 237 | } |
| 238 | debug("Embedded data: dst=%lx, size=%lx\n", load_addr, |
| 239 | (unsigned long)length); |
| 240 | src = (void *)data; |
| 241 | } |
| 242 | |
Ben Whitten | d154ca6 | 2018-06-07 11:37:27 +0100 | [diff] [blame] | 243 | #ifdef CONFIG_SPL_FIT_SIGNATURE |
| 244 | printf("## Checking hash(es) for Image %s ... ", |
| 245 | fit_get_name(fit, node, NULL)); |
| 246 | if (!fit_image_verify_with_data(fit, node, |
| 247 | src, length)) |
| 248 | return -EPERM; |
| 249 | puts("OK\n"); |
| 250 | #endif |
| 251 | |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 252 | #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS |
| 253 | board_fit_image_post_process(&src, &length); |
| 254 | #endif |
| 255 | |
Michal Simek | 975e789 | 2018-07-24 15:05:00 +0200 | [diff] [blame] | 256 | if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) { |
York Sun | 933f67a | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 257 | size = length; |
York Sun | 7264f29 | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 258 | if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN, |
York Sun | 933f67a | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 259 | src, &size)) { |
York Sun | 7264f29 | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 260 | puts("Uncompressing error\n"); |
| 261 | return -EIO; |
| 262 | } |
York Sun | 933f67a | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 263 | length = size; |
York Sun | 7264f29 | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 264 | } else { |
| 265 | memcpy((void *)load_addr, src, length); |
| 266 | } |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 267 | |
| 268 | if (image_info) { |
| 269 | image_info->load_addr = load_addr; |
| 270 | image_info->size = length; |
| 271 | image_info->entry_point = fdt_getprop_u32(fit, node, "entry"); |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 277 | static int spl_fit_append_fdt(struct spl_image_info *spl_image, |
| 278 | struct spl_load_info *info, ulong sector, |
| 279 | void *fit, int images, ulong base_offset) |
| 280 | { |
| 281 | struct spl_image_info image_info; |
| 282 | int node, ret; |
| 283 | |
| 284 | /* Figure out which device tree the board wants to use */ |
| 285 | node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0); |
| 286 | if (node < 0) { |
| 287 | debug("%s: cannot find FDT node\n", __func__); |
| 288 | return node; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Read the device tree and place it after the image. |
| 293 | * Align the destination address to ARCH_DMA_MINALIGN. |
| 294 | */ |
| 295 | image_info.load_addr = spl_image->load_addr + spl_image->size; |
| 296 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, |
| 297 | &image_info); |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 298 | |
| 299 | if (ret < 0) |
| 300 | return ret; |
| 301 | |
| 302 | /* Make the load-address of the FDT available for the SPL framework */ |
| 303 | spl_image->fdt_addr = (void *)image_info.load_addr; |
Philipp Tomsich | 337bbb6 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 304 | #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 305 | /* Try to make space, so we can inject details on the loadables */ |
| 306 | ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192); |
Philipp Tomsich | 337bbb6 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 307 | #endif |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 308 | |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | static int spl_fit_record_loadable(const void *fit, int images, int index, |
| 313 | void *blob, struct spl_image_info *image) |
| 314 | { |
Philipp Tomsich | 337bbb6 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 315 | int ret = 0; |
| 316 | #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 317 | char *name; |
Philipp Tomsich | 337bbb6 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 318 | int node; |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 319 | |
| 320 | ret = spl_fit_get_image_name(fit, images, "loadables", |
| 321 | index, &name); |
| 322 | if (ret < 0) |
| 323 | return ret; |
| 324 | |
| 325 | node = spl_fit_get_image_node(fit, images, "loadables", index); |
| 326 | |
| 327 | ret = fdt_record_loadable(blob, index, name, image->load_addr, |
| 328 | image->size, image->entry_point, |
| 329 | fdt_getprop(fit, node, "type", NULL), |
| 330 | fdt_getprop(fit, node, "os", NULL)); |
Philipp Tomsich | 337bbb6 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 331 | #endif |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 332 | return ret; |
| 333 | } |
| 334 | |
Philipp Tomsich | 337bbb6 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 335 | static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os) |
| 336 | { |
Jean-Jacques Hiblot | 7296a33 | 2019-04-26 15:21:25 +0200 | [diff] [blame] | 337 | #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) && !defined(CONFIG_SPL_OS_BOOT) |
Philipp Tomsich | 337bbb6 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 338 | return -ENOTSUPP; |
| 339 | #else |
| 340 | return fit_image_get_os(fit, noffset, os); |
| 341 | #endif |
| 342 | } |
| 343 | |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 344 | /* |
| 345 | * Weak default function to allow customizing SPL fit loading for load-only |
| 346 | * use cases by allowing to skip the parsing/processing of the FIT contents |
| 347 | * (so that this can be done separately in a more customized fashion) |
| 348 | */ |
| 349 | __weak bool spl_load_simple_fit_skip_processing(void) |
| 350 | { |
| 351 | return false; |
| 352 | } |
| 353 | |
Simon Glass | f4d7d85 | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 354 | int spl_load_simple_fit(struct spl_image_info *spl_image, |
| 355 | struct spl_load_info *info, ulong sector, void *fit) |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 356 | { |
| 357 | int sectors; |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 358 | ulong size; |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 359 | unsigned long count; |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 360 | struct spl_image_info image_info; |
York Sun | c8bc3c0 | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 361 | int node = -1; |
| 362 | int images, ret; |
Marek Vasut | 04ce542 | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 363 | int base_offset, hsize, align_len = ARCH_DMA_MINALIGN - 1; |
Andre Przywara | 411cf32 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 364 | int index = 0; |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 365 | |
| 366 | /* |
York Sun | c8bc3c0 | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 367 | * For FIT with external data, figure out where the external images |
| 368 | * start. This is the base for the data-offset properties in each |
| 369 | * image. |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 370 | */ |
| 371 | size = fdt_totalsize(fit); |
| 372 | size = (size + 3) & ~3; |
Ye Li | e246bfc | 2018-11-17 09:10:25 +0000 | [diff] [blame] | 373 | size = board_spl_fit_size_align(size); |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 374 | base_offset = (size + 3) & ~3; |
| 375 | |
| 376 | /* |
| 377 | * So far we only have one block of data from the FIT. Read the entire |
| 378 | * thing, including that first block, placing it so it finishes before |
| 379 | * where we will load the image. |
| 380 | * |
| 381 | * Note that we will load the image such that its first byte will be |
| 382 | * at the load address. Since that byte may be part-way through a |
| 383 | * block, we may load the image up to one block before the load |
| 384 | * address. So take account of that here by subtracting an addition |
| 385 | * block length from the FIT start position. |
| 386 | * |
| 387 | * In fact the FIT has its own load address, but we assume it cannot |
| 388 | * be before CONFIG_SYS_TEXT_BASE. |
York Sun | c8bc3c0 | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 389 | * |
| 390 | * For FIT with data embedded, data is loaded as part of FIT image. |
| 391 | * For FIT with external data, data is not loaded in this step. |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 392 | */ |
Marek Vasut | 04ce542 | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 393 | hsize = (size + info->bl_len + align_len) & ~align_len; |
| 394 | fit = spl_get_load_buffer(-hsize, hsize); |
Lokesh Vutla | eafd541 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 395 | sectors = get_aligned_image_size(info, size, 0); |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 396 | count = info->read(info, sector, sectors, fit); |
Ye Li | e246bfc | 2018-11-17 09:10:25 +0000 | [diff] [blame] | 397 | debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n", |
| 398 | sector, sectors, fit, count, size); |
| 399 | |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 400 | if (count == 0) |
| 401 | return -EIO; |
| 402 | |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 403 | /* skip further processing if requested to enable load-only use cases */ |
| 404 | if (spl_load_simple_fit_skip_processing()) |
| 405 | return 0; |
| 406 | |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 407 | /* find the node holding the images information */ |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 408 | images = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 409 | if (images < 0) { |
| 410 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 411 | return -1; |
| 412 | } |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 413 | |
Marek Vasut | 26a6422 | 2018-05-12 22:25:28 +0200 | [diff] [blame] | 414 | #ifdef CONFIG_SPL_FPGA_SUPPORT |
| 415 | node = spl_fit_get_image_node(fit, images, "fpga", 0); |
| 416 | if (node >= 0) { |
| 417 | /* Load the image and set up the spl_image structure */ |
| 418 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, |
| 419 | spl_image); |
| 420 | if (ret) { |
| 421 | printf("%s: Cannot load the FPGA: %i\n", __func__, ret); |
| 422 | return ret; |
| 423 | } |
Michal Simek | 3313ae6 | 2018-07-18 14:33:15 +0200 | [diff] [blame] | 424 | |
| 425 | debug("FPGA bitstream at: %x, size: %x\n", |
| 426 | (u32)spl_image->load_addr, spl_image->size); |
| 427 | |
| 428 | ret = fpga_load(0, (const void *)spl_image->load_addr, |
| 429 | spl_image->size, BIT_FULL); |
| 430 | if (ret) { |
| 431 | printf("%s: Cannot load the image to the FPGA\n", |
| 432 | __func__); |
| 433 | return ret; |
| 434 | } |
| 435 | |
Luis Araneda | 3907eef | 2018-07-19 03:10:16 -0400 | [diff] [blame] | 436 | puts("FPGA image loaded from FIT\n"); |
Marek Vasut | 26a6422 | 2018-05-12 22:25:28 +0200 | [diff] [blame] | 437 | node = -1; |
| 438 | } |
| 439 | #endif |
| 440 | |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 441 | /* |
| 442 | * Find the U-Boot image using the following search order: |
| 443 | * - start at 'firmware' (e.g. an ARM Trusted Firmware) |
| 444 | * - fall back 'kernel' (e.g. a Falcon-mode OS boot |
| 445 | * - fall back to using the first 'loadables' entry |
| 446 | */ |
York Sun | c8bc3c0 | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 447 | if (node < 0) |
Michal Simek | 1f8e4bf | 2018-03-26 16:31:26 +0200 | [diff] [blame] | 448 | node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP, |
| 449 | 0); |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 450 | #ifdef CONFIG_SPL_OS_BOOT |
| 451 | if (node < 0) |
| 452 | node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0); |
| 453 | #endif |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 454 | if (node < 0) { |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 455 | debug("could not find firmware image, trying loadables...\n"); |
| 456 | node = spl_fit_get_image_node(fit, images, "loadables", 0); |
Andre Przywara | 411cf32 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 457 | /* |
| 458 | * If we pick the U-Boot image from "loadables", start at |
| 459 | * the second image when later loading additional images. |
| 460 | */ |
| 461 | index = 1; |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 462 | } |
| 463 | if (node < 0) { |
| 464 | debug("%s: Cannot find u-boot image node: %d\n", |
| 465 | __func__, node); |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 466 | return -1; |
| 467 | } |
| 468 | |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 469 | /* Load the image and set up the spl_image structure */ |
| 470 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, |
| 471 | spl_image); |
| 472 | if (ret) |
| 473 | return ret; |
| 474 | |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 475 | /* |
| 476 | * For backward compatibility, we treat the first node that is |
| 477 | * as a U-Boot image, if no OS-type has been declared. |
| 478 | */ |
Philipp Tomsich | 337bbb6 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 479 | if (!spl_fit_image_get_os(fit, node, &spl_image->os)) |
York Sun | c8bc3c0 | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 480 | debug("Image OS is %s\n", genimg_get_os_name(spl_image->os)); |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 481 | #if !defined(CONFIG_SPL_OS_BOOT) |
| 482 | else |
| 483 | spl_image->os = IH_OS_U_BOOT; |
York Sun | c8bc3c0 | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 484 | #endif |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 485 | |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 486 | /* |
| 487 | * Booting a next-stage U-Boot may require us to append the FDT. |
| 488 | * We allow this to fail, as the U-Boot image might embed its FDT. |
| 489 | */ |
| 490 | if (spl_image->os == IH_OS_U_BOOT) |
| 491 | spl_fit_append_fdt(spl_image, info, sector, fit, |
| 492 | images, base_offset); |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 493 | |
Andre Przywara | 411cf32 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 494 | /* Now check if there are more images for us to load */ |
| 495 | for (; ; index++) { |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 496 | uint8_t os_type = IH_OS_INVALID; |
| 497 | |
Andre Przywara | 411cf32 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 498 | node = spl_fit_get_image_node(fit, images, "loadables", index); |
| 499 | if (node < 0) |
| 500 | break; |
| 501 | |
| 502 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, |
| 503 | &image_info); |
| 504 | if (ret < 0) |
| 505 | continue; |
| 506 | |
Philipp Tomsich | 337bbb6 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 507 | if (!spl_fit_image_get_os(fit, node, &os_type)) |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 508 | debug("Loadable is %s\n", genimg_get_os_name(os_type)); |
Abel Vesa | cf8dcc5 | 2019-03-12 08:34:31 +0000 | [diff] [blame] | 509 | #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) |
| 510 | else |
| 511 | os_type = IH_OS_U_BOOT; |
| 512 | #endif |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 513 | |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 514 | if (os_type == IH_OS_U_BOOT) { |
| 515 | spl_fit_append_fdt(&image_info, info, sector, |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 516 | fit, images, base_offset); |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 517 | spl_image->fdt_addr = image_info.fdt_addr; |
| 518 | } |
Philipp Tomsich | d879616 | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 519 | |
Andre Przywara | 411cf32 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 520 | /* |
| 521 | * If the "firmware" image did not provide an entry point, |
| 522 | * use the first valid entry point from the loadables. |
| 523 | */ |
| 524 | if (spl_image->entry_point == FDT_ERROR && |
| 525 | image_info.entry_point != FDT_ERROR) |
| 526 | spl_image->entry_point = image_info.entry_point; |
Philipp Tomsich | a616c78 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 527 | |
| 528 | /* Record our loadables into the FDT */ |
| 529 | if (spl_image->fdt_addr) |
| 530 | spl_fit_record_loadable(fit, images, index, |
| 531 | spl_image->fdt_addr, |
| 532 | &image_info); |
Andre Przywara | 411cf32 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /* |
| 536 | * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's |
| 537 | * Makefile will set it to 0 and it will end up as the entry point |
| 538 | * here. What it actually means is: use the load address. |
| 539 | */ |
| 540 | if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0) |
| 541 | spl_image->entry_point = spl_image->load_addr; |
| 542 | |
Ye Li | e246bfc | 2018-11-17 09:10:25 +0000 | [diff] [blame] | 543 | spl_image->flags |= SPL_FIT_FOUND; |
| 544 | |
| 545 | #ifdef CONFIG_SECURE_BOOT |
| 546 | board_spl_fit_post_load((ulong)fit, size); |
| 547 | #endif |
| 548 | |
Andre Przywara | 411cf32 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 549 | return 0; |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 550 | } |