Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <errno.h> |
| 10 | #include <image.h> |
| 11 | #include <libfdt.h> |
| 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 | |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 18 | /** |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 19 | * spl_fit_get_image_name(): By using the matching configuration subnode, |
Andre Przywara | 525e9c9 | 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 | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 26 | * @outname: Name of the image |
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 | * Return: 0 on success, or a negative error number |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 29 | */ |
Philipp Tomsich | e61eff9 | 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 | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 33 | { |
| 34 | const char *name, *str; |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 35 | __maybe_unused int node; |
| 36 | int conf_node; |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 37 | int len, i; |
| 38 | |
Cooper Jr., Franklin | e679240 | 2017-06-16 17:25:05 -0500 | [diff] [blame] | 39 | conf_node = fit_find_config_node(fit); |
Andre Przywara | 1e329b6 | 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); |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 48 | } |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 49 | #endif |
| 50 | return conf_node; |
| 51 | } |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 52 | |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 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 | } |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 58 | |
Andre Przywara | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 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 | } |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Philipp Tomsich | e61eff9 | 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 | 1e329b6 | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 95 | debug("%s: '%s'\n", type, str); |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 96 | |
Andre Przywara | 1e329b6 | 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; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 101 | } |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 102 | |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 103 | return node; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Lokesh Vutla | f62cef1 | 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 | f0531a6 | 2016-07-19 14:56:14 +0530 | [diff] [blame] | 136 | data_size = data_size + get_aligned_image_overhead(info, offset); |
| 137 | |
Lokesh Vutla | f62cef1 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 138 | if (info->filename) |
Lokesh Vutla | f0531a6 | 2016-07-19 14:56:14 +0530 | [diff] [blame] | 139 | return data_size; |
Lokesh Vutla | f62cef1 | 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 | 29053e9 | 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 | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 149 | * image |
Andre Przywara | 29053e9 | 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 | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 153 | * to @fit) |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 154 | * @image_info: will be filled with information about the loaded image |
Philipp Tomsich | e61eff9 | 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 | 29053e9 | 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 | { |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 165 | int offset; |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 166 | size_t length; |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 167 | int len; |
York Sun | 25d65f1 | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 168 | ulong size; |
Andre Przywara | 29053e9 | 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 | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 174 | uint8_t image_comp = -1, type = -1; |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 175 | const void *data; |
Peng Fan | 8876c7e | 2017-12-05 13:20:59 +0800 | [diff] [blame] | 176 | bool external_data = false; |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 177 | |
| 178 | if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) { |
| 179 | if (fit_image_get_comp(fit, node, &image_comp)) |
| 180 | puts("Cannot get image compression format.\n"); |
| 181 | else |
| 182 | debug("%s ", genimg_get_comp_name(image_comp)); |
| 183 | |
| 184 | if (fit_image_get_type(fit, node, &type)) |
| 185 | puts("Cannot get image type.\n"); |
| 186 | else |
| 187 | debug("%s ", genimg_get_type_name(type)); |
| 188 | } |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 189 | |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 190 | if (fit_image_get_load(fit, node, &load_addr)) |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 191 | load_addr = image_info->load_addr; |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 192 | |
Peng Fan | 8876c7e | 2017-12-05 13:20:59 +0800 | [diff] [blame] | 193 | if (!fit_image_get_data_position(fit, node, &offset)) { |
| 194 | external_data = true; |
| 195 | } else if (!fit_image_get_data_offset(fit, node, &offset)) { |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 196 | offset += base_offset; |
Peng Fan | 8876c7e | 2017-12-05 13:20:59 +0800 | [diff] [blame] | 197 | external_data = true; |
| 198 | } |
| 199 | |
| 200 | if (external_data) { |
| 201 | /* External data */ |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 202 | if (fit_image_get_data_size(fit, node, &len)) |
| 203 | return -ENOENT; |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 204 | |
York Sun | adf99fa | 2017-08-15 11:14:44 -0700 | [diff] [blame] | 205 | load_ptr = (load_addr + align_len) & ~align_len; |
| 206 | length = len; |
| 207 | |
| 208 | overhead = get_aligned_image_overhead(info, offset); |
| 209 | nr_sectors = get_aligned_image_size(info, length, offset); |
| 210 | |
| 211 | if (info->read(info, |
| 212 | sector + get_aligned_image_offset(info, offset), |
| 213 | nr_sectors, (void *)load_ptr) != nr_sectors) |
| 214 | return -EIO; |
| 215 | |
| 216 | debug("External data: dst=%lx, offset=%x, size=%lx\n", |
| 217 | load_ptr, offset, (unsigned long)length); |
| 218 | src = (void *)load_ptr + overhead; |
| 219 | } else { |
| 220 | /* Embedded data */ |
| 221 | if (fit_image_get_data(fit, node, &data, &length)) { |
| 222 | puts("Cannot get image data/size\n"); |
| 223 | return -ENOENT; |
| 224 | } |
| 225 | debug("Embedded data: dst=%lx, size=%lx\n", load_addr, |
| 226 | (unsigned long)length); |
| 227 | src = (void *)data; |
| 228 | } |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 229 | |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 230 | #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS |
| 231 | board_fit_image_post_process(&src, &length); |
| 232 | #endif |
| 233 | |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 234 | if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && |
| 235 | IS_ENABLED(CONFIG_SPL_GZIP) && |
| 236 | image_comp == IH_COMP_GZIP && |
| 237 | type == IH_TYPE_KERNEL) { |
York Sun | 25d65f1 | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 238 | size = length; |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 239 | if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN, |
York Sun | 25d65f1 | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 240 | src, &size)) { |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 241 | puts("Uncompressing error\n"); |
| 242 | return -EIO; |
| 243 | } |
York Sun | 25d65f1 | 2017-09-15 08:21:13 -0700 | [diff] [blame] | 244 | length = size; |
York Sun | a6945fe | 2017-08-15 11:14:43 -0700 | [diff] [blame] | 245 | } else { |
| 246 | memcpy((void *)load_addr, src, length); |
| 247 | } |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 248 | |
| 249 | if (image_info) { |
| 250 | image_info->load_addr = load_addr; |
| 251 | image_info->size = length; |
| 252 | image_info->entry_point = fdt_getprop_u32(fit, node, "entry"); |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 258 | static int spl_fit_append_fdt(struct spl_image_info *spl_image, |
| 259 | struct spl_load_info *info, ulong sector, |
| 260 | void *fit, int images, ulong base_offset) |
| 261 | { |
| 262 | struct spl_image_info image_info; |
| 263 | int node, ret; |
| 264 | |
| 265 | /* Figure out which device tree the board wants to use */ |
| 266 | node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0); |
| 267 | if (node < 0) { |
| 268 | debug("%s: cannot find FDT node\n", __func__); |
| 269 | return node; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Read the device tree and place it after the image. |
| 274 | * Align the destination address to ARCH_DMA_MINALIGN. |
| 275 | */ |
| 276 | image_info.load_addr = spl_image->load_addr + spl_image->size; |
| 277 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, |
| 278 | &image_info); |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 279 | |
| 280 | if (ret < 0) |
| 281 | return ret; |
| 282 | |
| 283 | /* Make the load-address of the FDT available for the SPL framework */ |
| 284 | spl_image->fdt_addr = (void *)image_info.load_addr; |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 285 | #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 286 | /* Try to make space, so we can inject details on the loadables */ |
| 287 | ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192); |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 288 | #endif |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 289 | |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 290 | return ret; |
| 291 | } |
| 292 | |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 293 | static int spl_fit_record_loadable(const void *fit, int images, int index, |
| 294 | void *blob, struct spl_image_info *image) |
| 295 | { |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 296 | int ret = 0; |
| 297 | #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 298 | char *name; |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 299 | int node; |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 300 | |
| 301 | ret = spl_fit_get_image_name(fit, images, "loadables", |
| 302 | index, &name); |
| 303 | if (ret < 0) |
| 304 | return ret; |
| 305 | |
| 306 | node = spl_fit_get_image_node(fit, images, "loadables", index); |
| 307 | |
| 308 | ret = fdt_record_loadable(blob, index, name, image->load_addr, |
| 309 | image->size, image->entry_point, |
| 310 | fdt_getprop(fit, node, "type", NULL), |
| 311 | fdt_getprop(fit, node, "os", NULL)); |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 312 | #endif |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 313 | return ret; |
| 314 | } |
| 315 | |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 316 | static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os) |
| 317 | { |
| 318 | #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) |
| 319 | return -ENOTSUPP; |
| 320 | #else |
| 321 | return fit_image_get_os(fit, noffset, os); |
| 322 | #endif |
| 323 | } |
| 324 | |
Simon Glass | 43a734f | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 325 | int spl_load_simple_fit(struct spl_image_info *spl_image, |
| 326 | struct spl_load_info *info, ulong sector, void *fit) |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 327 | { |
| 328 | int sectors; |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 329 | ulong size; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 330 | unsigned long count; |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 331 | struct spl_image_info image_info; |
York Sun | a058b8a | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 332 | int node = -1; |
| 333 | int images, ret; |
Lokesh Vutla | f62cef1 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 334 | int base_offset, align_len = ARCH_DMA_MINALIGN - 1; |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 335 | int index = 0; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 336 | |
| 337 | /* |
York Sun | a058b8a | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 338 | * For FIT with external data, figure out where the external images |
| 339 | * start. This is the base for the data-offset properties in each |
| 340 | * image. |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 341 | */ |
| 342 | size = fdt_totalsize(fit); |
| 343 | size = (size + 3) & ~3; |
| 344 | base_offset = (size + 3) & ~3; |
| 345 | |
| 346 | /* |
| 347 | * So far we only have one block of data from the FIT. Read the entire |
| 348 | * thing, including that first block, placing it so it finishes before |
| 349 | * where we will load the image. |
| 350 | * |
| 351 | * Note that we will load the image such that its first byte will be |
| 352 | * at the load address. Since that byte may be part-way through a |
| 353 | * block, we may load the image up to one block before the load |
| 354 | * address. So take account of that here by subtracting an addition |
| 355 | * block length from the FIT start position. |
| 356 | * |
| 357 | * In fact the FIT has its own load address, but we assume it cannot |
| 358 | * be before CONFIG_SYS_TEXT_BASE. |
York Sun | a058b8a | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 359 | * |
| 360 | * For FIT with data embedded, data is loaded as part of FIT image. |
| 361 | * For FIT with external data, data is not loaded in this step. |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 362 | */ |
Lokesh Vutla | c840585 | 2016-06-01 10:28:31 +0530 | [diff] [blame] | 363 | fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len - |
| 364 | align_len) & ~align_len); |
Lokesh Vutla | f62cef1 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 365 | sectors = get_aligned_image_size(info, size, 0); |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 366 | count = info->read(info, sector, sectors, fit); |
| 367 | debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n", |
| 368 | sector, sectors, fit, count); |
| 369 | if (count == 0) |
| 370 | return -EIO; |
| 371 | |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 372 | /* find the node holding the images information */ |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 373 | images = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 374 | if (images < 0) { |
| 375 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 376 | return -1; |
| 377 | } |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 378 | |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 379 | /* |
| 380 | * Find the U-Boot image using the following search order: |
| 381 | * - start at 'firmware' (e.g. an ARM Trusted Firmware) |
| 382 | * - fall back 'kernel' (e.g. a Falcon-mode OS boot |
| 383 | * - fall back to using the first 'loadables' entry |
| 384 | */ |
| 385 | if (node < 0) |
| 386 | node = spl_fit_get_image_node(fit, images, "firmware", 0); |
York Sun | a058b8a | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 387 | #ifdef CONFIG_SPL_OS_BOOT |
York Sun | a058b8a | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 388 | if (node < 0) |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 389 | node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0); |
York Sun | a058b8a | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 390 | #endif |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 391 | if (node < 0) { |
| 392 | debug("could not find firmware image, trying loadables...\n"); |
| 393 | node = spl_fit_get_image_node(fit, images, "loadables", 0); |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 394 | /* |
| 395 | * If we pick the U-Boot image from "loadables", start at |
| 396 | * the second image when later loading additional images. |
| 397 | */ |
| 398 | index = 1; |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 399 | } |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 400 | if (node < 0) { |
Andre Przywara | 525e9c9 | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 401 | debug("%s: Cannot find u-boot image node: %d\n", |
| 402 | __func__, node); |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 403 | return -1; |
| 404 | } |
| 405 | |
Andre Przywara | 29053e9 | 2017-04-26 01:32:36 +0100 | [diff] [blame] | 406 | /* Load the image and set up the spl_image structure */ |
| 407 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, |
| 408 | spl_image); |
| 409 | if (ret) |
| 410 | return ret; |
Daniel Allred | cf839bd | 2016-06-27 09:19:21 -0500 | [diff] [blame] | 411 | |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 412 | /* |
| 413 | * For backward compatibility, we treat the first node that is |
| 414 | * as a U-Boot image, if no OS-type has been declared. |
| 415 | */ |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 416 | if (!spl_fit_image_get_os(fit, node, &spl_image->os)) |
York Sun | a058b8a | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 417 | 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] | 418 | #if !defined(CONFIG_SPL_OS_BOOT) |
| 419 | else |
| 420 | spl_image->os = IH_OS_U_BOOT; |
York Sun | a058b8a | 2017-08-15 11:14:45 -0700 | [diff] [blame] | 421 | #endif |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 422 | |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 423 | /* |
| 424 | * Booting a next-stage U-Boot may require us to append the FDT. |
| 425 | * We allow this to fail, as the U-Boot image might embed its FDT. |
| 426 | */ |
| 427 | if (spl_image->os == IH_OS_U_BOOT) |
| 428 | spl_fit_append_fdt(spl_image, info, sector, fit, |
| 429 | images, base_offset); |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 430 | |
| 431 | /* Now check if there are more images for us to load */ |
| 432 | for (; ; index++) { |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 433 | uint8_t os_type = IH_OS_INVALID; |
| 434 | |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 435 | node = spl_fit_get_image_node(fit, images, "loadables", index); |
| 436 | if (node < 0) |
| 437 | break; |
| 438 | |
| 439 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, |
| 440 | &image_info); |
| 441 | if (ret < 0) |
| 442 | continue; |
| 443 | |
Philipp Tomsich | 4faa011 | 2017-11-24 13:26:03 +0100 | [diff] [blame] | 444 | if (!spl_fit_image_get_os(fit, node, &os_type)) |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 445 | debug("Loadable is %s\n", genimg_get_os_name(os_type)); |
| 446 | |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 447 | if (os_type == IH_OS_U_BOOT) { |
| 448 | spl_fit_append_fdt(&image_info, info, sector, |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 449 | fit, images, base_offset); |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 450 | spl_image->fdt_addr = image_info.fdt_addr; |
| 451 | } |
Philipp Tomsich | 1345ffa | 2017-09-13 21:29:32 +0200 | [diff] [blame] | 452 | |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 453 | /* |
| 454 | * If the "firmware" image did not provide an entry point, |
| 455 | * use the first valid entry point from the loadables. |
| 456 | */ |
| 457 | if (spl_image->entry_point == FDT_ERROR && |
| 458 | image_info.entry_point != FDT_ERROR) |
| 459 | spl_image->entry_point = image_info.entry_point; |
Philipp Tomsich | e61eff9 | 2017-09-13 21:29:34 +0200 | [diff] [blame] | 460 | |
| 461 | /* Record our loadables into the FDT */ |
| 462 | if (spl_image->fdt_addr) |
| 463 | spl_fit_record_loadable(fit, images, index, |
| 464 | spl_image->fdt_addr, |
| 465 | &image_info); |
Andre Przywara | e7c5856 | 2017-04-26 01:32:37 +0100 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* |
| 469 | * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's |
| 470 | * Makefile will set it to 0 and it will end up as the entry point |
| 471 | * here. What it actually means is: use the load address. |
| 472 | */ |
| 473 | if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0) |
| 474 | spl_image->entry_point = spl_image->load_addr; |
| 475 | |
| 476 | return 0; |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 477 | } |