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