2 * Copyright (C) 2016 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
14 static ulong fdt_getprop_u32(const void *fdt, int node, const char *prop)
19 cell = fdt_getprop(fdt, node, prop, &len);
20 if (len != sizeof(*cell))
22 return fdt32_to_cpu(*cell);
25 static int spl_fit_select_fdt(const void *fdt, int images, int *fdt_offsetp)
27 const char *name, *fdt_name;
28 int conf, node, fdt_node;
32 conf = fdt_path_offset(fdt, FIT_CONFS_PATH);
34 debug("%s: Cannot find /configurations node: %d\n", __func__,
38 for (node = fdt_first_subnode(fdt, conf);
40 node = fdt_next_subnode(fdt, node)) {
41 name = fdt_getprop(fdt, node, "description", &len);
44 if (board_fit_config_name_match(name))
47 debug("Selecting config '%s'", name);
48 fdt_name = fdt_getprop(fdt, node, FIT_FDT_PROP, &len);
50 debug("%s: Cannot find fdt name property: %d\n",
55 debug(", fdt '%s'\n", fdt_name);
56 fdt_node = fdt_subnode_offset(fdt, images, fdt_name);
58 debug("%s: Cannot find fdt node '%s': %d\n",
59 __func__, fdt_name, fdt_node);
63 *fdt_offsetp = fdt_getprop_u32(fdt, fdt_node, "data-offset");
64 len = fdt_getprop_u32(fdt, fdt_node, "data-size");
65 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
66 printf("FIT: Selected '%s'\n", name);
72 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
73 printf("No matching DT out of these options:\n");
74 for (node = fdt_first_subnode(fdt, conf);
76 node = fdt_next_subnode(fdt, node)) {
77 name = fdt_getprop(fdt, node, "name", &len);
78 printf(" %s\n", name);
85 int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit)
92 int fdt_offset, fdt_len;
93 int data_offset, data_size;
99 * Figure out where the external images start. This is the base for the
100 * data-offset properties in each image.
102 size = fdt_totalsize(fit);
103 size = (size + 3) & ~3;
104 base_offset = (size + 3) & ~3;
107 * So far we only have one block of data from the FIT. Read the entire
108 * thing, including that first block, placing it so it finishes before
109 * where we will load the image.
111 * Note that we will load the image such that its first byte will be
112 * at the load address. Since that byte may be part-way through a
113 * block, we may load the image up to one block before the load
114 * address. So take account of that here by subtracting an addition
115 * block length from the FIT start position.
117 * In fact the FIT has its own load address, but we assume it cannot
118 * be before CONFIG_SYS_TEXT_BASE.
120 fit = (void *)(CONFIG_SYS_TEXT_BASE - size - info->bl_len);
121 sectors = (size + info->bl_len - 1) / info->bl_len;
122 count = info->read(info, sector, sectors, fit);
123 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
124 sector, sectors, fit, count);
128 /* find the firmware image to load */
129 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
131 debug("%s: Cannot find /images node: %d\n", __func__, images);
134 node = fdt_first_subnode(fit, images);
136 debug("%s: Cannot find first image node: %d\n", __func__, node);
140 /* Get its information and set up the spl_image structure */
141 data_offset = fdt_getprop_u32(fit, node, "data-offset");
142 data_size = fdt_getprop_u32(fit, node, "data-size");
143 load = fdt_getprop_u32(fit, node, "load");
144 debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
145 spl_image.load_addr = load;
146 spl_image.entry_point = load;
147 spl_image.os = IH_OS_U_BOOT;
150 * Work out where to place the image. We read it so that the first
151 * byte will be at 'load'. This may mean we need to load it starting
152 * before then, since we can only read whole blocks.
154 sectors = (data_size + info->bl_len - 1) / info->bl_len;
155 data_offset += base_offset;
156 load_ptr = (void *)load;
157 debug("U-Boot size %x, data %p\n", data_size, load_ptr);
158 dst = load_ptr - (data_offset % info->bl_len);
161 src_sector = sector + data_offset / info->bl_len;
162 debug("image: data_offset=%x, dst=%p, src_sector=%x, sectors=%x\n",
163 data_offset, dst, src_sector, sectors);
164 count = info->read(info, src_sector, sectors, dst);
165 if (count != sectors)
168 /* Figure out which device tree the board wants to use */
169 fdt_len = spl_fit_select_fdt(fit, images, &fdt_offset);
174 * Read the device tree and place it after the image. There may be
175 * some extra data before it since we can only read entire blocks.
177 dst = load_ptr + data_size;
178 fdt_offset += base_offset;
179 count = info->read(info, sector + fdt_offset / info->bl_len, sectors,
181 debug("fit read %x sectors to %x, dst %p, data_offset %x\n",
182 sectors, spl_image.load_addr, dst, fdt_offset);
183 if (count != sectors)
187 * Copy the device tree so that it starts immediately after the image.
188 * After this we will have the U-Boot image and its device tree ready
191 memcpy(dst, dst + fdt_offset % info->bl_len, fdt_len);