1f1dcee59SSimon Glass /* 2f1dcee59SSimon Glass * Copyright (C) 2016 Google, Inc 3f1dcee59SSimon Glass * Written by Simon Glass <sjg@chromium.org> 4f1dcee59SSimon Glass * 5f1dcee59SSimon Glass * SPDX-License-Identifier: GPL-2.0+ 6f1dcee59SSimon Glass */ 7f1dcee59SSimon Glass 8f1dcee59SSimon Glass #include <common.h> 9f1dcee59SSimon Glass #include <errno.h> 10f1dcee59SSimon Glass #include <image.h> 11f1dcee59SSimon Glass #include <libfdt.h> 12f1dcee59SSimon Glass #include <spl.h> 13f1dcee59SSimon Glass 14b81c4739SYork Sun #ifndef CONFIG_SYS_BOOTM_LEN 15b81c4739SYork Sun #define CONFIG_SYS_BOOTM_LEN (64 << 20) 16b81c4739SYork Sun #endif 17b81c4739SYork Sun 18736806fbSAndre Przywara /** 19736806fbSAndre Przywara * spl_fit_get_image_node(): By using the matching configuration subnode, 20736806fbSAndre Przywara * retrieve the name of an image, specified by a property name and an index 21736806fbSAndre Przywara * into that. 22736806fbSAndre Przywara * @fit: Pointer to the FDT blob. 23736806fbSAndre Przywara * @images: Offset of the /images subnode. 24736806fbSAndre Przywara * @type: Name of the property within the configuration subnode. 25736806fbSAndre Przywara * @index: Index into the list of strings in this property. 26736806fbSAndre Przywara * 27736806fbSAndre Przywara * Return: the node offset of the respective image node or a negative 28736806fbSAndre Przywara * error number. 29736806fbSAndre Przywara */ 30736806fbSAndre Przywara static int spl_fit_get_image_node(const void *fit, int images, 314b9340abSAndre Przywara const char *type, int index) 324b9340abSAndre Przywara { 334b9340abSAndre Przywara const char *name, *str; 344b9340abSAndre Przywara int node, conf_node; 354b9340abSAndre Przywara int len, i; 36f1dcee59SSimon Glass 373863f840SCooper Jr., Franklin conf_node = fit_find_config_node(fit); 384b9340abSAndre Przywara if (conf_node < 0) { 39f1dcee59SSimon Glass #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 40f1dcee59SSimon Glass printf("No matching DT out of these options:\n"); 414b9340abSAndre Przywara for (node = fdt_first_subnode(fit, conf_node); 42f1dcee59SSimon Glass node >= 0; 434b9340abSAndre Przywara node = fdt_next_subnode(fit, node)) { 444b9340abSAndre Przywara name = fdt_getprop(fit, node, "description", &len); 45f1dcee59SSimon Glass printf(" %s\n", name); 46f1dcee59SSimon Glass } 47f1dcee59SSimon Glass #endif 484b9340abSAndre Przywara return conf_node; 494b9340abSAndre Przywara } 50f1dcee59SSimon Glass 514b9340abSAndre Przywara name = fdt_getprop(fit, conf_node, type, &len); 524b9340abSAndre Przywara if (!name) { 534b9340abSAndre Przywara debug("cannot find property '%s': %d\n", type, len); 544b9340abSAndre Przywara return -EINVAL; 554b9340abSAndre Przywara } 564b9340abSAndre Przywara 574b9340abSAndre Przywara str = name; 584b9340abSAndre Przywara for (i = 0; i < index; i++) { 594b9340abSAndre Przywara str = strchr(str, '\0') + 1; 604b9340abSAndre Przywara if (!str || (str - name >= len)) { 614b9340abSAndre Przywara debug("no string for index %d\n", index); 624b9340abSAndre Przywara return -E2BIG; 634b9340abSAndre Przywara } 644b9340abSAndre Przywara } 654b9340abSAndre Przywara 664b9340abSAndre Przywara debug("%s: '%s'\n", type, str); 674b9340abSAndre Przywara node = fdt_subnode_offset(fit, images, str); 684b9340abSAndre Przywara if (node < 0) { 694b9340abSAndre Przywara debug("cannot find image node '%s': %d\n", str, node); 704b9340abSAndre Przywara return -EINVAL; 714b9340abSAndre Przywara } 724b9340abSAndre Przywara 73736806fbSAndre Przywara return node; 74f1dcee59SSimon Glass } 75f1dcee59SSimon Glass 76eafd5410SLokesh Vutla static int get_aligned_image_offset(struct spl_load_info *info, int offset) 77eafd5410SLokesh Vutla { 78eafd5410SLokesh Vutla /* 79eafd5410SLokesh Vutla * If it is a FS read, get the first address before offset which is 80eafd5410SLokesh Vutla * aligned to ARCH_DMA_MINALIGN. If it is raw read return the 81eafd5410SLokesh Vutla * block number to which offset belongs. 82eafd5410SLokesh Vutla */ 83eafd5410SLokesh Vutla if (info->filename) 84eafd5410SLokesh Vutla return offset & ~(ARCH_DMA_MINALIGN - 1); 85eafd5410SLokesh Vutla 86eafd5410SLokesh Vutla return offset / info->bl_len; 87eafd5410SLokesh Vutla } 88eafd5410SLokesh Vutla 89eafd5410SLokesh Vutla static int get_aligned_image_overhead(struct spl_load_info *info, int offset) 90eafd5410SLokesh Vutla { 91eafd5410SLokesh Vutla /* 92eafd5410SLokesh Vutla * If it is a FS read, get the difference between the offset and 93eafd5410SLokesh Vutla * the first address before offset which is aligned to 94eafd5410SLokesh Vutla * ARCH_DMA_MINALIGN. If it is raw read return the offset within the 95eafd5410SLokesh Vutla * block. 96eafd5410SLokesh Vutla */ 97eafd5410SLokesh Vutla if (info->filename) 98eafd5410SLokesh Vutla return offset & (ARCH_DMA_MINALIGN - 1); 99eafd5410SLokesh Vutla 100eafd5410SLokesh Vutla return offset % info->bl_len; 101eafd5410SLokesh Vutla } 102eafd5410SLokesh Vutla 103eafd5410SLokesh Vutla static int get_aligned_image_size(struct spl_load_info *info, int data_size, 104eafd5410SLokesh Vutla int offset) 105eafd5410SLokesh Vutla { 1063cc1f380SLokesh Vutla data_size = data_size + get_aligned_image_overhead(info, offset); 1073cc1f380SLokesh Vutla 108eafd5410SLokesh Vutla if (info->filename) 1093cc1f380SLokesh Vutla return data_size; 110eafd5410SLokesh Vutla 111eafd5410SLokesh Vutla return (data_size + info->bl_len - 1) / info->bl_len; 112eafd5410SLokesh Vutla } 113eafd5410SLokesh Vutla 1148baa3818SAndre Przywara /** 1158baa3818SAndre Przywara * spl_load_fit_image(): load the image described in a certain FIT node 1168baa3818SAndre Przywara * @info: points to information about the device to load data from 1178baa3818SAndre Przywara * @sector: the start sector of the FIT image on the device 1188baa3818SAndre Przywara * @fit: points to the flattened device tree blob describing the FIT 1198baa3818SAndre Przywara * image 1208baa3818SAndre Przywara * @base_offset: the beginning of the data area containing the actual 1218baa3818SAndre Przywara * image data, relative to the beginning of the FIT 1228baa3818SAndre Przywara * @node: offset of the DT node describing the image to load (relative 1238baa3818SAndre Przywara * to @fit) 1248baa3818SAndre Przywara * @image_info: will be filled with information about the loaded image 1258baa3818SAndre Przywara * If the FIT node does not contain a "load" (address) property, 1268baa3818SAndre Przywara * the image gets loaded to the address pointed to by the 1278baa3818SAndre Przywara * load_addr member in this struct. 1288baa3818SAndre Przywara * 1298baa3818SAndre Przywara * Return: 0 on success or a negative error number. 1308baa3818SAndre Przywara */ 1318baa3818SAndre Przywara static int spl_load_fit_image(struct spl_load_info *info, ulong sector, 1328baa3818SAndre Przywara void *fit, ulong base_offset, int node, 1338baa3818SAndre Przywara struct spl_image_info *image_info) 1348baa3818SAndre Przywara { 135*0ea10b9fSYork Sun int offset; 1368baa3818SAndre Przywara size_t length; 137*0ea10b9fSYork Sun int len; 1388baa3818SAndre Przywara ulong load_addr, load_ptr; 1398baa3818SAndre Przywara void *src; 1408baa3818SAndre Przywara ulong overhead; 1418baa3818SAndre Przywara int nr_sectors; 1428baa3818SAndre Przywara int align_len = ARCH_DMA_MINALIGN - 1; 143b81c4739SYork Sun uint8_t image_comp = -1, type = -1; 144*0ea10b9fSYork Sun const void *data; 145b81c4739SYork Sun 146b81c4739SYork Sun if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) { 147b81c4739SYork Sun if (fit_image_get_comp(fit, node, &image_comp)) 148b81c4739SYork Sun puts("Cannot get image compression format.\n"); 149b81c4739SYork Sun else 150b81c4739SYork Sun debug("%s ", genimg_get_comp_name(image_comp)); 151b81c4739SYork Sun 152b81c4739SYork Sun if (fit_image_get_type(fit, node, &type)) 153b81c4739SYork Sun puts("Cannot get image type.\n"); 154b81c4739SYork Sun else 155b81c4739SYork Sun debug("%s ", genimg_get_type_name(type)); 156b81c4739SYork Sun } 1578baa3818SAndre Przywara 158*0ea10b9fSYork Sun if (fit_image_get_load(fit, node, &load_addr)) 1598baa3818SAndre Przywara load_addr = image_info->load_addr; 160*0ea10b9fSYork Sun 161*0ea10b9fSYork Sun if (!fit_image_get_data_offset(fit, node, &offset)) { 162*0ea10b9fSYork Sun /* External data */ 163*0ea10b9fSYork Sun offset += base_offset; 164*0ea10b9fSYork Sun if (fit_image_get_data_size(fit, node, &len)) 165*0ea10b9fSYork Sun return -ENOENT; 166*0ea10b9fSYork Sun 1678baa3818SAndre Przywara load_ptr = (load_addr + align_len) & ~align_len; 168*0ea10b9fSYork Sun length = len; 1698baa3818SAndre Przywara 1708baa3818SAndre Przywara overhead = get_aligned_image_overhead(info, offset); 1718baa3818SAndre Przywara nr_sectors = get_aligned_image_size(info, length, offset); 1728baa3818SAndre Przywara 173*0ea10b9fSYork Sun if (info->read(info, 174*0ea10b9fSYork Sun sector + get_aligned_image_offset(info, offset), 1758baa3818SAndre Przywara nr_sectors, (void *)load_ptr) != nr_sectors) 1768baa3818SAndre Przywara return -EIO; 1778baa3818SAndre Przywara 178*0ea10b9fSYork Sun debug("External data: dst=%lx, offset=%x, size=%lx\n", 179*0ea10b9fSYork Sun load_ptr, offset, (unsigned long)length); 1808baa3818SAndre Przywara src = (void *)load_ptr + overhead; 181*0ea10b9fSYork Sun } else { 182*0ea10b9fSYork Sun /* Embedded data */ 183*0ea10b9fSYork Sun if (fit_image_get_data(fit, node, &data, &length)) { 184*0ea10b9fSYork Sun puts("Cannot get image data/size\n"); 185*0ea10b9fSYork Sun return -ENOENT; 186*0ea10b9fSYork Sun } 187*0ea10b9fSYork Sun debug("Embedded data: dst=%lx, size=%lx\n", load_addr, 188*0ea10b9fSYork Sun (unsigned long)length); 189*0ea10b9fSYork Sun src = (void *)data; 190*0ea10b9fSYork Sun } 191*0ea10b9fSYork Sun 1928baa3818SAndre Przywara #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS 1938baa3818SAndre Przywara board_fit_image_post_process(&src, &length); 1948baa3818SAndre Przywara #endif 1958baa3818SAndre Przywara 196b81c4739SYork Sun if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && 197b81c4739SYork Sun IS_ENABLED(CONFIG_SPL_GZIP) && 198b81c4739SYork Sun image_comp == IH_COMP_GZIP && 199b81c4739SYork Sun type == IH_TYPE_KERNEL) { 200b81c4739SYork Sun if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN, 201b81c4739SYork Sun src, &length)) { 202b81c4739SYork Sun puts("Uncompressing error\n"); 203b81c4739SYork Sun return -EIO; 204b81c4739SYork Sun } 205b81c4739SYork Sun } else { 2068baa3818SAndre Przywara memcpy((void *)load_addr, src, length); 207b81c4739SYork Sun } 2088baa3818SAndre Przywara 2098baa3818SAndre Przywara if (image_info) { 2108baa3818SAndre Przywara image_info->load_addr = load_addr; 2118baa3818SAndre Przywara image_info->size = length; 2128baa3818SAndre Przywara image_info->entry_point = fdt_getprop_u32(fit, node, "entry"); 2138baa3818SAndre Przywara } 2148baa3818SAndre Przywara 2158baa3818SAndre Przywara return 0; 2168baa3818SAndre Przywara } 2178baa3818SAndre Przywara 218f4d7d859SSimon Glass int spl_load_simple_fit(struct spl_image_info *spl_image, 219f4d7d859SSimon Glass struct spl_load_info *info, ulong sector, void *fit) 220f1dcee59SSimon Glass { 221f1dcee59SSimon Glass int sectors; 2228baa3818SAndre Przywara ulong size; 223f1dcee59SSimon Glass unsigned long count; 2248baa3818SAndre Przywara struct spl_image_info image_info; 2258baa3818SAndre Przywara int node, images, ret; 226eafd5410SLokesh Vutla int base_offset, align_len = ARCH_DMA_MINALIGN - 1; 227411cf32dSAndre Przywara int index = 0; 228f1dcee59SSimon Glass 229f1dcee59SSimon Glass /* 230f1dcee59SSimon Glass * Figure out where the external images start. This is the base for the 231f1dcee59SSimon Glass * data-offset properties in each image. 232f1dcee59SSimon Glass */ 233f1dcee59SSimon Glass size = fdt_totalsize(fit); 234f1dcee59SSimon Glass size = (size + 3) & ~3; 235f1dcee59SSimon Glass base_offset = (size + 3) & ~3; 236f1dcee59SSimon Glass 237f1dcee59SSimon Glass /* 238f1dcee59SSimon Glass * So far we only have one block of data from the FIT. Read the entire 239f1dcee59SSimon Glass * thing, including that first block, placing it so it finishes before 240f1dcee59SSimon Glass * where we will load the image. 241f1dcee59SSimon Glass * 242f1dcee59SSimon Glass * Note that we will load the image such that its first byte will be 243f1dcee59SSimon Glass * at the load address. Since that byte may be part-way through a 244f1dcee59SSimon Glass * block, we may load the image up to one block before the load 245f1dcee59SSimon Glass * address. So take account of that here by subtracting an addition 246f1dcee59SSimon Glass * block length from the FIT start position. 247f1dcee59SSimon Glass * 248f1dcee59SSimon Glass * In fact the FIT has its own load address, but we assume it cannot 249f1dcee59SSimon Glass * be before CONFIG_SYS_TEXT_BASE. 250f1dcee59SSimon Glass */ 2518b528709SLokesh Vutla fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len - 2528b528709SLokesh Vutla align_len) & ~align_len); 253eafd5410SLokesh Vutla sectors = get_aligned_image_size(info, size, 0); 254f1dcee59SSimon Glass count = info->read(info, sector, sectors, fit); 255f1dcee59SSimon Glass debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n", 256f1dcee59SSimon Glass sector, sectors, fit, count); 257f1dcee59SSimon Glass if (count == 0) 258f1dcee59SSimon Glass return -EIO; 259f1dcee59SSimon Glass 260736806fbSAndre Przywara /* find the node holding the images information */ 261f1dcee59SSimon Glass images = fdt_path_offset(fit, FIT_IMAGES_PATH); 262f1dcee59SSimon Glass if (images < 0) { 263f1dcee59SSimon Glass debug("%s: Cannot find /images node: %d\n", __func__, images); 264f1dcee59SSimon Glass return -1; 265f1dcee59SSimon Glass } 266736806fbSAndre Przywara 267736806fbSAndre Przywara /* find the U-Boot image */ 268736806fbSAndre Przywara node = spl_fit_get_image_node(fit, images, "firmware", 0); 269f1dcee59SSimon Glass if (node < 0) { 270736806fbSAndre Przywara debug("could not find firmware image, trying loadables...\n"); 271736806fbSAndre Przywara node = spl_fit_get_image_node(fit, images, "loadables", 0); 272411cf32dSAndre Przywara /* 273411cf32dSAndre Przywara * If we pick the U-Boot image from "loadables", start at 274411cf32dSAndre Przywara * the second image when later loading additional images. 275411cf32dSAndre Przywara */ 276411cf32dSAndre Przywara index = 1; 277736806fbSAndre Przywara } 278736806fbSAndre Przywara if (node < 0) { 279736806fbSAndre Przywara debug("%s: Cannot find u-boot image node: %d\n", 280736806fbSAndre Przywara __func__, node); 281f1dcee59SSimon Glass return -1; 282f1dcee59SSimon Glass } 283f1dcee59SSimon Glass 2848baa3818SAndre Przywara /* Load the image and set up the spl_image structure */ 2858baa3818SAndre Przywara ret = spl_load_fit_image(info, sector, fit, base_offset, node, 2868baa3818SAndre Przywara spl_image); 2878baa3818SAndre Przywara if (ret) 2888baa3818SAndre Przywara return ret; 2898baa3818SAndre Przywara 290f4d7d859SSimon Glass spl_image->os = IH_OS_U_BOOT; 291f1dcee59SSimon Glass 292f1dcee59SSimon Glass /* Figure out which device tree the board wants to use */ 293736806fbSAndre Przywara node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0); 294736806fbSAndre Przywara if (node < 0) { 295736806fbSAndre Przywara debug("%s: cannot find FDT node\n", __func__); 296736806fbSAndre Przywara return node; 297736806fbSAndre Przywara } 298f1dcee59SSimon Glass 299f1dcee59SSimon Glass /* 3008baa3818SAndre Przywara * Read the device tree and place it after the image. 3018baa3818SAndre Przywara * Align the destination address to ARCH_DMA_MINALIGN. 302f1dcee59SSimon Glass */ 3038baa3818SAndre Przywara image_info.load_addr = spl_image->load_addr + spl_image->size; 304411cf32dSAndre Przywara ret = spl_load_fit_image(info, sector, fit, base_offset, node, 3058baa3818SAndre Przywara &image_info); 306411cf32dSAndre Przywara if (ret < 0) 307411cf32dSAndre Przywara return ret; 308411cf32dSAndre Przywara 309411cf32dSAndre Przywara /* Now check if there are more images for us to load */ 310411cf32dSAndre Przywara for (; ; index++) { 311411cf32dSAndre Przywara node = spl_fit_get_image_node(fit, images, "loadables", index); 312411cf32dSAndre Przywara if (node < 0) 313411cf32dSAndre Przywara break; 314411cf32dSAndre Przywara 315411cf32dSAndre Przywara ret = spl_load_fit_image(info, sector, fit, base_offset, node, 316411cf32dSAndre Przywara &image_info); 317411cf32dSAndre Przywara if (ret < 0) 318411cf32dSAndre Przywara continue; 319411cf32dSAndre Przywara 320411cf32dSAndre Przywara /* 321411cf32dSAndre Przywara * If the "firmware" image did not provide an entry point, 322411cf32dSAndre Przywara * use the first valid entry point from the loadables. 323411cf32dSAndre Przywara */ 324411cf32dSAndre Przywara if (spl_image->entry_point == FDT_ERROR && 325411cf32dSAndre Przywara image_info.entry_point != FDT_ERROR) 326411cf32dSAndre Przywara spl_image->entry_point = image_info.entry_point; 327411cf32dSAndre Przywara } 328411cf32dSAndre Przywara 329411cf32dSAndre Przywara /* 330411cf32dSAndre Przywara * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's 331411cf32dSAndre Przywara * Makefile will set it to 0 and it will end up as the entry point 332411cf32dSAndre Przywara * here. What it actually means is: use the load address. 333411cf32dSAndre Przywara */ 334411cf32dSAndre Przywara if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0) 335411cf32dSAndre Przywara spl_image->entry_point = spl_image->load_addr; 336411cf32dSAndre Przywara 337411cf32dSAndre Przywara return 0; 338f1dcee59SSimon Glass } 339