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