Simon Glass | f1dcee5 | 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 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <errno.h> |
| 10 | #include <image.h> |
| 11 | #include <libfdt.h> |
| 12 | #include <spl.h> |
| 13 | |
Andre Przywara | 5c8c8fa | 2017-04-26 01:32:35 +0100 | [diff] [blame] | 14 | #define FDT_ERROR ((ulong)(-1)) |
| 15 | |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 16 | static ulong fdt_getprop_u32(const void *fdt, int node, const char *prop) |
| 17 | { |
| 18 | const u32 *cell; |
| 19 | int len; |
| 20 | |
| 21 | cell = fdt_getprop(fdt, node, prop, &len); |
Andre Przywara | 5c8c8fa | 2017-04-26 01:32:35 +0100 | [diff] [blame] | 22 | if (!cell || len != sizeof(*cell)) |
| 23 | return FDT_ERROR; |
| 24 | |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 25 | return fdt32_to_cpu(*cell); |
| 26 | } |
| 27 | |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 28 | /* |
| 29 | * Iterate over all /configurations subnodes and call a platform specific |
| 30 | * function to find the matching configuration. |
| 31 | * Returns the node offset or a negative error number. |
| 32 | */ |
| 33 | static int spl_fit_find_config_node(const void *fdt) |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 34 | { |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 35 | const char *name; |
| 36 | int conf, node, len; |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 37 | |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 38 | conf = fdt_path_offset(fdt, FIT_CONFS_PATH); |
| 39 | if (conf < 0) { |
| 40 | debug("%s: Cannot find /configurations node: %d\n", __func__, |
| 41 | conf); |
| 42 | return -EINVAL; |
| 43 | } |
| 44 | for (node = fdt_first_subnode(fdt, conf); |
| 45 | node >= 0; |
| 46 | node = fdt_next_subnode(fdt, node)) { |
| 47 | name = fdt_getprop(fdt, node, "description", &len); |
Michal Simek | 5adfa26 | 2016-05-04 15:08:00 +0200 | [diff] [blame] | 48 | if (!name) { |
| 49 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 50 | printf("%s: Missing FDT description in DTB\n", |
| 51 | __func__); |
| 52 | #endif |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 53 | return -EINVAL; |
Michal Simek | 5adfa26 | 2016-05-04 15:08:00 +0200 | [diff] [blame] | 54 | } |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 55 | if (board_fit_config_name_match(name)) |
| 56 | continue; |
| 57 | |
| 58 | debug("Selecting config '%s'", name); |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 59 | |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 60 | return node; |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 63 | return -ENOENT; |
| 64 | } |
| 65 | |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 66 | /** |
| 67 | * spl_fit_get_image_node(): By using the matching configuration subnode, |
| 68 | * retrieve the name of an image, specified by a property name and an index |
| 69 | * into that. |
| 70 | * @fit: Pointer to the FDT blob. |
| 71 | * @images: Offset of the /images subnode. |
| 72 | * @type: Name of the property within the configuration subnode. |
| 73 | * @index: Index into the list of strings in this property. |
| 74 | * |
| 75 | * Return: the node offset of the respective image node or a negative |
| 76 | * error number. |
| 77 | */ |
| 78 | static int spl_fit_get_image_node(const void *fit, int images, |
| 79 | const char *type, int index) |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 80 | { |
| 81 | const char *name, *str; |
| 82 | int node, conf_node; |
| 83 | int len, i; |
| 84 | |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 85 | conf_node = spl_fit_find_config_node(fit); |
| 86 | if (conf_node < 0) { |
| 87 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 88 | printf("No matching DT out of these options:\n"); |
| 89 | for (node = fdt_first_subnode(fit, conf_node); |
| 90 | node >= 0; |
| 91 | node = fdt_next_subnode(fit, node)) { |
| 92 | name = fdt_getprop(fit, node, "description", &len); |
| 93 | printf(" %s\n", name); |
| 94 | } |
| 95 | #endif |
| 96 | return conf_node; |
| 97 | } |
| 98 | |
| 99 | name = fdt_getprop(fit, conf_node, type, &len); |
| 100 | if (!name) { |
| 101 | debug("cannot find property '%s': %d\n", type, len); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | |
| 105 | str = name; |
| 106 | for (i = 0; i < index; i++) { |
| 107 | str = strchr(str, '\0') + 1; |
| 108 | if (!str || (str - name >= len)) { |
| 109 | debug("no string for index %d\n", index); |
| 110 | return -E2BIG; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | debug("%s: '%s'\n", type, str); |
| 115 | node = fdt_subnode_offset(fit, images, str); |
| 116 | if (node < 0) { |
| 117 | debug("cannot find image node '%s': %d\n", str, node); |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 121 | return node; |
Andre Przywara | 4b9340a | 2017-04-26 01:32:33 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Lokesh Vutla | eafd541 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 124 | static int get_aligned_image_offset(struct spl_load_info *info, int offset) |
| 125 | { |
| 126 | /* |
| 127 | * If it is a FS read, get the first address before offset which is |
| 128 | * aligned to ARCH_DMA_MINALIGN. If it is raw read return the |
| 129 | * block number to which offset belongs. |
| 130 | */ |
| 131 | if (info->filename) |
| 132 | return offset & ~(ARCH_DMA_MINALIGN - 1); |
| 133 | |
| 134 | return offset / info->bl_len; |
| 135 | } |
| 136 | |
| 137 | static int get_aligned_image_overhead(struct spl_load_info *info, int offset) |
| 138 | { |
| 139 | /* |
| 140 | * If it is a FS read, get the difference between the offset and |
| 141 | * the first address before offset which is aligned to |
| 142 | * ARCH_DMA_MINALIGN. If it is raw read return the offset within the |
| 143 | * block. |
| 144 | */ |
| 145 | if (info->filename) |
| 146 | return offset & (ARCH_DMA_MINALIGN - 1); |
| 147 | |
| 148 | return offset % info->bl_len; |
| 149 | } |
| 150 | |
| 151 | static int get_aligned_image_size(struct spl_load_info *info, int data_size, |
| 152 | int offset) |
| 153 | { |
Lokesh Vutla | 3cc1f38 | 2016-07-19 14:56:14 +0530 | [diff] [blame] | 154 | data_size = data_size + get_aligned_image_overhead(info, offset); |
| 155 | |
Lokesh Vutla | eafd541 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 156 | if (info->filename) |
Lokesh Vutla | 3cc1f38 | 2016-07-19 14:56:14 +0530 | [diff] [blame] | 157 | return data_size; |
Lokesh Vutla | eafd541 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 158 | |
| 159 | return (data_size + info->bl_len - 1) / info->bl_len; |
| 160 | } |
| 161 | |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame^] | 162 | /** |
| 163 | * spl_load_fit_image(): load the image described in a certain FIT node |
| 164 | * @info: points to information about the device to load data from |
| 165 | * @sector: the start sector of the FIT image on the device |
| 166 | * @fit: points to the flattened device tree blob describing the FIT |
| 167 | * image |
| 168 | * @base_offset: the beginning of the data area containing the actual |
| 169 | * image data, relative to the beginning of the FIT |
| 170 | * @node: offset of the DT node describing the image to load (relative |
| 171 | * to @fit) |
| 172 | * @image_info: will be filled with information about the loaded image |
| 173 | * If the FIT node does not contain a "load" (address) property, |
| 174 | * the image gets loaded to the address pointed to by the |
| 175 | * load_addr member in this struct. |
| 176 | * |
| 177 | * Return: 0 on success or a negative error number. |
| 178 | */ |
| 179 | static int spl_load_fit_image(struct spl_load_info *info, ulong sector, |
| 180 | void *fit, ulong base_offset, int node, |
| 181 | struct spl_image_info *image_info) |
| 182 | { |
| 183 | ulong offset; |
| 184 | size_t length; |
| 185 | ulong load_addr, load_ptr; |
| 186 | void *src; |
| 187 | ulong overhead; |
| 188 | int nr_sectors; |
| 189 | int align_len = ARCH_DMA_MINALIGN - 1; |
| 190 | |
| 191 | offset = fdt_getprop_u32(fit, node, "data-offset"); |
| 192 | if (offset == FDT_ERROR) |
| 193 | return -ENOENT; |
| 194 | offset += base_offset; |
| 195 | length = fdt_getprop_u32(fit, node, "data-size"); |
| 196 | if (length == FDT_ERROR) |
| 197 | return -ENOENT; |
| 198 | load_addr = fdt_getprop_u32(fit, node, "load"); |
| 199 | if (load_addr == FDT_ERROR && image_info) |
| 200 | load_addr = image_info->load_addr; |
| 201 | load_ptr = (load_addr + align_len) & ~align_len; |
| 202 | |
| 203 | overhead = get_aligned_image_overhead(info, offset); |
| 204 | nr_sectors = get_aligned_image_size(info, length, offset); |
| 205 | |
| 206 | if (info->read(info, sector + get_aligned_image_offset(info, offset), |
| 207 | nr_sectors, (void*)load_ptr) != nr_sectors) |
| 208 | return -EIO; |
| 209 | debug("image: dst=%lx, offset=%lx, size=%lx\n", load_ptr, offset, |
| 210 | (unsigned long)length); |
| 211 | |
| 212 | src = (void *)load_ptr + overhead; |
| 213 | #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS |
| 214 | board_fit_image_post_process(&src, &length); |
| 215 | #endif |
| 216 | |
| 217 | memcpy((void*)load_addr, src, length); |
| 218 | |
| 219 | if (image_info) { |
| 220 | image_info->load_addr = load_addr; |
| 221 | image_info->size = length; |
| 222 | image_info->entry_point = fdt_getprop_u32(fit, node, "entry"); |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Simon Glass | f4d7d85 | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 228 | int spl_load_simple_fit(struct spl_image_info *spl_image, |
| 229 | struct spl_load_info *info, ulong sector, void *fit) |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 230 | { |
| 231 | int sectors; |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame^] | 232 | ulong size; |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 233 | unsigned long count; |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame^] | 234 | struct spl_image_info image_info; |
| 235 | int node, images, ret; |
Lokesh Vutla | eafd541 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 236 | int base_offset, align_len = ARCH_DMA_MINALIGN - 1; |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * Figure out where the external images start. This is the base for the |
| 240 | * data-offset properties in each image. |
| 241 | */ |
| 242 | size = fdt_totalsize(fit); |
| 243 | size = (size + 3) & ~3; |
| 244 | base_offset = (size + 3) & ~3; |
| 245 | |
| 246 | /* |
| 247 | * So far we only have one block of data from the FIT. Read the entire |
| 248 | * thing, including that first block, placing it so it finishes before |
| 249 | * where we will load the image. |
| 250 | * |
| 251 | * Note that we will load the image such that its first byte will be |
| 252 | * at the load address. Since that byte may be part-way through a |
| 253 | * block, we may load the image up to one block before the load |
| 254 | * address. So take account of that here by subtracting an addition |
| 255 | * block length from the FIT start position. |
| 256 | * |
| 257 | * In fact the FIT has its own load address, but we assume it cannot |
| 258 | * be before CONFIG_SYS_TEXT_BASE. |
| 259 | */ |
Lokesh Vutla | 8b52870 | 2016-06-01 10:28:31 +0530 | [diff] [blame] | 260 | fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len - |
| 261 | align_len) & ~align_len); |
Lokesh Vutla | eafd541 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 262 | sectors = get_aligned_image_size(info, size, 0); |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 263 | count = info->read(info, sector, sectors, fit); |
| 264 | debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n", |
| 265 | sector, sectors, fit, count); |
| 266 | if (count == 0) |
| 267 | return -EIO; |
| 268 | |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 269 | /* find the node holding the images information */ |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 270 | images = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 271 | if (images < 0) { |
| 272 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 273 | return -1; |
| 274 | } |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 275 | |
| 276 | /* find the U-Boot image */ |
| 277 | node = spl_fit_get_image_node(fit, images, "firmware", 0); |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 278 | if (node < 0) { |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 279 | debug("could not find firmware image, trying loadables...\n"); |
| 280 | node = spl_fit_get_image_node(fit, images, "loadables", 0); |
| 281 | } |
| 282 | if (node < 0) { |
| 283 | debug("%s: Cannot find u-boot image node: %d\n", |
| 284 | __func__, node); |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 287 | |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame^] | 288 | /* Load the image and set up the spl_image structure */ |
| 289 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, |
| 290 | spl_image); |
| 291 | if (ret) |
| 292 | return ret; |
| 293 | |
Simon Glass | f4d7d85 | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 294 | spl_image->os = IH_OS_U_BOOT; |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 295 | |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 296 | /* Figure out which device tree the board wants to use */ |
Andre Przywara | 736806f | 2017-04-26 01:32:34 +0100 | [diff] [blame] | 297 | node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0); |
| 298 | if (node < 0) { |
| 299 | debug("%s: cannot find FDT node\n", __func__); |
| 300 | return node; |
| 301 | } |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 302 | |
| 303 | /* |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame^] | 304 | * Read the device tree and place it after the image. |
| 305 | * Align the destination address to ARCH_DMA_MINALIGN. |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 306 | */ |
Andre Przywara | 8baa381 | 2017-04-26 01:32:36 +0100 | [diff] [blame^] | 307 | image_info.load_addr = spl_image->load_addr + spl_image->size; |
| 308 | return spl_load_fit_image(info, sector, fit, base_offset, node, |
| 309 | &image_info); |
Simon Glass | f1dcee5 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 310 | } |