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