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