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 static int spl_fit_select_index(const void *fit, int images, int *offsetp, 64 const char *type, int index) 65 { 66 const char *name, *str; 67 int node, conf_node; 68 int len, i; 69 70 *offsetp = 0; 71 conf_node = spl_fit_find_config_node(fit); 72 if (conf_node < 0) { 73 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 74 printf("No matching DT out of these options:\n"); 75 for (node = fdt_first_subnode(fit, conf_node); 76 node >= 0; 77 node = fdt_next_subnode(fit, node)) { 78 name = fdt_getprop(fit, node, "description", &len); 79 printf(" %s\n", name); 80 } 81 #endif 82 return conf_node; 83 } 84 85 name = fdt_getprop(fit, conf_node, type, &len); 86 if (!name) { 87 debug("cannot find property '%s': %d\n", type, len); 88 return -EINVAL; 89 } 90 91 str = name; 92 for (i = 0; i < index; i++) { 93 str = strchr(str, '\0') + 1; 94 if (!str || (str - name >= len)) { 95 debug("no string for index %d\n", index); 96 return -E2BIG; 97 } 98 } 99 100 debug("%s: '%s'\n", type, str); 101 node = fdt_subnode_offset(fit, images, str); 102 if (node < 0) { 103 debug("cannot find image node '%s': %d\n", str, node); 104 return -EINVAL; 105 } 106 107 *offsetp = fdt_getprop_u32(fit, node, "data-offset"); 108 len = fdt_getprop_u32(fit, node, "data-size"); 109 110 return len; 111 } 112 113 static int get_aligned_image_offset(struct spl_load_info *info, int offset) 114 { 115 /* 116 * If it is a FS read, get the first address before offset which is 117 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the 118 * block number to which offset belongs. 119 */ 120 if (info->filename) 121 return offset & ~(ARCH_DMA_MINALIGN - 1); 122 123 return offset / info->bl_len; 124 } 125 126 static int get_aligned_image_overhead(struct spl_load_info *info, int offset) 127 { 128 /* 129 * If it is a FS read, get the difference between the offset and 130 * the first address before offset which is aligned to 131 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the 132 * block. 133 */ 134 if (info->filename) 135 return offset & (ARCH_DMA_MINALIGN - 1); 136 137 return offset % info->bl_len; 138 } 139 140 static int get_aligned_image_size(struct spl_load_info *info, int data_size, 141 int offset) 142 { 143 data_size = data_size + get_aligned_image_overhead(info, offset); 144 145 if (info->filename) 146 return data_size; 147 148 return (data_size + info->bl_len - 1) / info->bl_len; 149 } 150 151 int spl_load_simple_fit(struct spl_image_info *spl_image, 152 struct spl_load_info *info, ulong sector, void *fit) 153 { 154 int sectors; 155 ulong size, load; 156 unsigned long count; 157 int node, images; 158 void *load_ptr; 159 int fdt_offset, fdt_len; 160 int data_offset, data_size; 161 int base_offset, align_len = ARCH_DMA_MINALIGN - 1; 162 int src_sector; 163 void *dst, *src; 164 165 /* 166 * Figure out where the external images start. This is the base for the 167 * data-offset properties in each image. 168 */ 169 size = fdt_totalsize(fit); 170 size = (size + 3) & ~3; 171 base_offset = (size + 3) & ~3; 172 173 /* 174 * So far we only have one block of data from the FIT. Read the entire 175 * thing, including that first block, placing it so it finishes before 176 * where we will load the image. 177 * 178 * Note that we will load the image such that its first byte will be 179 * at the load address. Since that byte may be part-way through a 180 * block, we may load the image up to one block before the load 181 * address. So take account of that here by subtracting an addition 182 * block length from the FIT start position. 183 * 184 * In fact the FIT has its own load address, but we assume it cannot 185 * be before CONFIG_SYS_TEXT_BASE. 186 */ 187 fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len - 188 align_len) & ~align_len); 189 sectors = get_aligned_image_size(info, size, 0); 190 count = info->read(info, sector, sectors, fit); 191 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n", 192 sector, sectors, fit, count); 193 if (count == 0) 194 return -EIO; 195 196 /* find the firmware image to load */ 197 images = fdt_path_offset(fit, FIT_IMAGES_PATH); 198 if (images < 0) { 199 debug("%s: Cannot find /images node: %d\n", __func__, images); 200 return -1; 201 } 202 node = fdt_first_subnode(fit, images); 203 if (node < 0) { 204 debug("%s: Cannot find first image node: %d\n", __func__, node); 205 return -1; 206 } 207 208 /* Get its information and set up the spl_image structure */ 209 data_offset = fdt_getprop_u32(fit, node, "data-offset"); 210 data_size = fdt_getprop_u32(fit, node, "data-size"); 211 load = fdt_getprop_u32(fit, node, "load"); 212 debug("data_offset=%x, data_size=%x\n", data_offset, data_size); 213 spl_image->load_addr = load; 214 spl_image->entry_point = load; 215 spl_image->os = IH_OS_U_BOOT; 216 217 /* 218 * Work out where to place the image. We read it so that the first 219 * byte will be at 'load'. This may mean we need to load it starting 220 * before then, since we can only read whole blocks. 221 */ 222 data_offset += base_offset; 223 sectors = get_aligned_image_size(info, data_size, data_offset); 224 load_ptr = (void *)load; 225 debug("U-Boot size %x, data %p\n", data_size, load_ptr); 226 dst = load_ptr; 227 228 /* Read the image */ 229 src_sector = sector + get_aligned_image_offset(info, data_offset); 230 debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n", 231 dst, src_sector, sectors); 232 count = info->read(info, src_sector, sectors, dst); 233 if (count != sectors) 234 return -EIO; 235 debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset, 236 data_size); 237 src = dst + get_aligned_image_overhead(info, data_offset); 238 239 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS 240 board_fit_image_post_process((void **)&src, (size_t *)&data_size); 241 #endif 242 243 memcpy(dst, src, data_size); 244 245 /* Figure out which device tree the board wants to use */ 246 fdt_len = spl_fit_select_index(fit, images, &fdt_offset, 247 FIT_FDT_PROP, 0); 248 if (fdt_len < 0) 249 return fdt_len; 250 251 /* 252 * Read the device tree and place it after the image. There may be 253 * some extra data before it since we can only read entire blocks. 254 * And also align the destination address to ARCH_DMA_MINALIGN. 255 */ 256 dst = (void *)((load + data_size + align_len) & ~align_len); 257 fdt_offset += base_offset; 258 sectors = get_aligned_image_size(info, fdt_len, fdt_offset); 259 src_sector = sector + get_aligned_image_offset(info, fdt_offset); 260 count = info->read(info, src_sector, sectors, dst); 261 debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n", 262 dst, src_sector, sectors); 263 if (count != sectors) 264 return -EIO; 265 266 /* 267 * Copy the device tree so that it starts immediately after the image. 268 * After this we will have the U-Boot image and its device tree ready 269 * for us to start. 270 */ 271 debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset, 272 fdt_len); 273 src = dst + get_aligned_image_overhead(info, fdt_offset); 274 dst = load_ptr + data_size; 275 276 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS 277 board_fit_image_post_process((void **)&src, (size_t *)&fdt_len); 278 #endif 279 280 memcpy(dst, src, fdt_len); 281 282 return 0; 283 } 284