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);
43 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
44 printf("%s: Missing FDT description in DTB\n",
49 if (board_fit_config_name_match(name))
52 debug("Selecting config '%s'", name);
53 fdt_name = fdt_getprop(fdt, node, FIT_FDT_PROP, &len);
55 debug("%s: Cannot find fdt name property: %d\n",
60 debug(", fdt '%s'\n", fdt_name);
61 fdt_node = fdt_subnode_offset(fdt, images, fdt_name);
63 debug("%s: Cannot find fdt node '%s': %d\n",
64 __func__, fdt_name, fdt_node);
68 *fdt_offsetp = fdt_getprop_u32(fdt, fdt_node, "data-offset");
69 len = fdt_getprop_u32(fdt, fdt_node, "data-size");
70 debug("FIT: Selected '%s'\n", name);
75 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
76 printf("No matching DT out of these options:\n");
77 for (node = fdt_first_subnode(fdt, conf);
79 node = fdt_next_subnode(fdt, node)) {
80 name = fdt_getprop(fdt, node, "description", &len);
81 printf(" %s\n", name);
88 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
91 * If it is a FS read, get the first address before offset which is
92 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
93 * block number to which offset belongs.
96 return offset & ~(ARCH_DMA_MINALIGN - 1);
98 return offset / info->bl_len;
101 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
104 * If it is a FS read, get the difference between the offset and
105 * the first address before offset which is aligned to
106 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
110 return offset & (ARCH_DMA_MINALIGN - 1);
112 return offset % info->bl_len;
115 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
118 data_size = data_size + get_aligned_image_overhead(info, offset);
123 return (data_size + info->bl_len - 1) / info->bl_len;
126 int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit)
133 int fdt_offset, fdt_len;
134 int data_offset, data_size;
135 int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
140 * Figure out where the external images start. This is the base for the
141 * data-offset properties in each image.
143 size = fdt_totalsize(fit);
144 size = (size + 3) & ~3;
145 base_offset = (size + 3) & ~3;
148 * So far we only have one block of data from the FIT. Read the entire
149 * thing, including that first block, placing it so it finishes before
150 * where we will load the image.
152 * Note that we will load the image such that its first byte will be
153 * at the load address. Since that byte may be part-way through a
154 * block, we may load the image up to one block before the load
155 * address. So take account of that here by subtracting an addition
156 * block length from the FIT start position.
158 * In fact the FIT has its own load address, but we assume it cannot
159 * be before CONFIG_SYS_TEXT_BASE.
161 fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
162 align_len) & ~align_len);
163 sectors = get_aligned_image_size(info, size, 0);
164 count = info->read(info, sector, sectors, fit);
165 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
166 sector, sectors, fit, count);
170 /* find the firmware image to load */
171 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
173 debug("%s: Cannot find /images node: %d\n", __func__, images);
176 node = fdt_first_subnode(fit, images);
178 debug("%s: Cannot find first image node: %d\n", __func__, node);
182 /* Get its information and set up the spl_image structure */
183 data_offset = fdt_getprop_u32(fit, node, "data-offset");
184 data_size = fdt_getprop_u32(fit, node, "data-size");
185 load = fdt_getprop_u32(fit, node, "load");
186 debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
187 spl_image.load_addr = load;
188 spl_image.entry_point = load;
189 spl_image.os = IH_OS_U_BOOT;
192 * Work out where to place the image. We read it so that the first
193 * byte will be at 'load'. This may mean we need to load it starting
194 * before then, since we can only read whole blocks.
196 data_offset += base_offset;
197 sectors = get_aligned_image_size(info, data_size, data_offset);
198 load_ptr = (void *)load;
199 debug("U-Boot size %x, data %p\n", data_size, load_ptr);
203 src_sector = sector + get_aligned_image_offset(info, data_offset);
204 debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n",
205 dst, src_sector, sectors);
206 count = info->read(info, src_sector, sectors, dst);
207 if (count != sectors)
209 debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset,
211 src = dst + get_aligned_image_overhead(info, data_offset);
213 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
214 board_fit_image_post_process((void **)&src, (size_t *)&data_size);
217 memcpy(dst, src, data_size);
219 /* Figure out which device tree the board wants to use */
220 fdt_len = spl_fit_select_fdt(fit, images, &fdt_offset);
225 * Read the device tree and place it after the image. There may be
226 * some extra data before it since we can only read entire blocks.
227 * And also align the destination address to ARCH_DMA_MINALIGN.
229 dst = (void *)((load + data_size + align_len) & ~align_len);
230 fdt_offset += base_offset;
231 sectors = get_aligned_image_size(info, fdt_len, fdt_offset);
232 src_sector = sector + get_aligned_image_offset(info, fdt_offset);
233 count = info->read(info, src_sector, sectors, dst);
234 debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n",
235 dst, src_sector, sectors);
236 if (count != sectors)
240 * Copy the device tree so that it starts immediately after the image.
241 * After this we will have the U-Boot image and its device tree ready
244 debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset,
246 src = dst + get_aligned_image_overhead(info, fdt_offset);
247 dst = load_ptr + data_size;
249 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
250 board_fit_image_post_process((void **)&src, (size_t *)&fdt_len);
253 memcpy(dst, src, fdt_len);