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> 110e00a84cSMasahiro Yamada #include <linux/libfdt.h> 12f1dcee59SSimon Glass #include <spl.h> 1390d1164aSKever Yang #include <malloc.h> 147a137075SJoseph Chen #include <optee_include/OpteeClientInterface.h> 15f1dcee59SSimon Glass 16b81c4739SYork Sun #ifndef CONFIG_SYS_BOOTM_LEN 17b81c4739SYork Sun #define CONFIG_SYS_BOOTM_LEN (64 << 20) 18b81c4739SYork Sun #endif 19b81c4739SYork Sun 20736806fbSAndre Przywara /** 211f1cf67bSPhilipp Tomsich * spl_fit_get_image_name(): By using the matching configuration subnode, 22736806fbSAndre Przywara * retrieve the name of an image, specified by a property name and an index 23736806fbSAndre Przywara * into that. 24736806fbSAndre Przywara * @fit: Pointer to the FDT blob. 25736806fbSAndre Przywara * @images: Offset of the /images subnode. 26736806fbSAndre Przywara * @type: Name of the property within the configuration subnode. 27736806fbSAndre Przywara * @index: Index into the list of strings in this property. 281f1cf67bSPhilipp Tomsich * @outname: Name of the image 29736806fbSAndre Przywara * 301f1cf67bSPhilipp Tomsich * Return: 0 on success, or a negative error number 31736806fbSAndre Przywara */ 321f1cf67bSPhilipp Tomsich static int spl_fit_get_image_name(const void *fit, int images, 331f1cf67bSPhilipp Tomsich const char *type, int index, 341f1cf67bSPhilipp Tomsich char **outname) 354b9340abSAndre Przywara { 364b9340abSAndre Przywara const char *name, *str; 371f1cf67bSPhilipp Tomsich __maybe_unused int node; 381f1cf67bSPhilipp Tomsich int conf_node; 394b9340abSAndre Przywara int len, i; 40f1dcee59SSimon Glass 413863f840SCooper Jr., Franklin conf_node = fit_find_config_node(fit); 424b9340abSAndre Przywara if (conf_node < 0) { 43f1dcee59SSimon Glass #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 44f1dcee59SSimon Glass printf("No matching DT out of these options:\n"); 454b9340abSAndre Przywara for (node = fdt_first_subnode(fit, conf_node); 46f1dcee59SSimon Glass node >= 0; 474b9340abSAndre Przywara node = fdt_next_subnode(fit, node)) { 484b9340abSAndre Przywara name = fdt_getprop(fit, node, "description", &len); 49f1dcee59SSimon Glass printf(" %s\n", name); 50f1dcee59SSimon Glass } 51f1dcee59SSimon Glass #endif 524b9340abSAndre Przywara return conf_node; 534b9340abSAndre Przywara } 54f1dcee59SSimon Glass 554b9340abSAndre Przywara name = fdt_getprop(fit, conf_node, type, &len); 564b9340abSAndre Przywara if (!name) { 574b9340abSAndre Przywara debug("cannot find property '%s': %d\n", type, len); 584b9340abSAndre Przywara return -EINVAL; 594b9340abSAndre Przywara } 604b9340abSAndre Przywara 614b9340abSAndre Przywara str = name; 624b9340abSAndre Przywara for (i = 0; i < index; i++) { 634b9340abSAndre Przywara str = strchr(str, '\0') + 1; 644b9340abSAndre Przywara if (!str || (str - name >= len)) { 654b9340abSAndre Przywara debug("no string for index %d\n", index); 664b9340abSAndre Przywara return -E2BIG; 674b9340abSAndre Przywara } 684b9340abSAndre Przywara } 694b9340abSAndre Przywara 701f1cf67bSPhilipp Tomsich *outname = (char *)str; 711f1cf67bSPhilipp Tomsich return 0; 721f1cf67bSPhilipp Tomsich } 731f1cf67bSPhilipp Tomsich 741f1cf67bSPhilipp Tomsich /** 751f1cf67bSPhilipp Tomsich * spl_fit_get_image_node(): By using the matching configuration subnode, 761f1cf67bSPhilipp Tomsich * retrieve the name of an image, specified by a property name and an index 771f1cf67bSPhilipp Tomsich * into that. 781f1cf67bSPhilipp Tomsich * @fit: Pointer to the FDT blob. 791f1cf67bSPhilipp Tomsich * @images: Offset of the /images subnode. 801f1cf67bSPhilipp Tomsich * @type: Name of the property within the configuration subnode. 811f1cf67bSPhilipp Tomsich * @index: Index into the list of strings in this property. 821f1cf67bSPhilipp Tomsich * 831f1cf67bSPhilipp Tomsich * Return: the node offset of the respective image node or a negative 841f1cf67bSPhilipp Tomsich * error number. 851f1cf67bSPhilipp Tomsich */ 861f1cf67bSPhilipp Tomsich static int spl_fit_get_image_node(const void *fit, int images, 871f1cf67bSPhilipp Tomsich const char *type, int index) 881f1cf67bSPhilipp Tomsich { 891f1cf67bSPhilipp Tomsich char *str; 901f1cf67bSPhilipp Tomsich int err; 911f1cf67bSPhilipp Tomsich int node; 921f1cf67bSPhilipp Tomsich 931f1cf67bSPhilipp Tomsich err = spl_fit_get_image_name(fit, images, type, index, &str); 941f1cf67bSPhilipp Tomsich if (err) 951f1cf67bSPhilipp Tomsich return err; 961f1cf67bSPhilipp Tomsich 974b9340abSAndre Przywara debug("%s: '%s'\n", type, str); 981f1cf67bSPhilipp Tomsich 994b9340abSAndre Przywara node = fdt_subnode_offset(fit, images, str); 1004b9340abSAndre Przywara if (node < 0) { 1014b9340abSAndre Przywara debug("cannot find image node '%s': %d\n", str, node); 1024b9340abSAndre Przywara return -EINVAL; 1034b9340abSAndre Przywara } 1044b9340abSAndre Przywara 105736806fbSAndre Przywara return node; 106f1dcee59SSimon Glass } 107f1dcee59SSimon Glass 108eafd5410SLokesh Vutla static int get_aligned_image_offset(struct spl_load_info *info, int offset) 109eafd5410SLokesh Vutla { 110eafd5410SLokesh Vutla /* 111eafd5410SLokesh Vutla * If it is a FS read, get the first address before offset which is 112eafd5410SLokesh Vutla * aligned to ARCH_DMA_MINALIGN. If it is raw read return the 113eafd5410SLokesh Vutla * block number to which offset belongs. 114eafd5410SLokesh Vutla */ 115eafd5410SLokesh Vutla if (info->filename) 116eafd5410SLokesh Vutla return offset & ~(ARCH_DMA_MINALIGN - 1); 117eafd5410SLokesh Vutla 118eafd5410SLokesh Vutla return offset / info->bl_len; 119eafd5410SLokesh Vutla } 120eafd5410SLokesh Vutla 121eafd5410SLokesh Vutla static int get_aligned_image_overhead(struct spl_load_info *info, int offset) 122eafd5410SLokesh Vutla { 123eafd5410SLokesh Vutla /* 124eafd5410SLokesh Vutla * If it is a FS read, get the difference between the offset and 125eafd5410SLokesh Vutla * the first address before offset which is aligned to 126eafd5410SLokesh Vutla * ARCH_DMA_MINALIGN. If it is raw read return the offset within the 127eafd5410SLokesh Vutla * block. 128eafd5410SLokesh Vutla */ 129eafd5410SLokesh Vutla if (info->filename) 130eafd5410SLokesh Vutla return offset & (ARCH_DMA_MINALIGN - 1); 131eafd5410SLokesh Vutla 132eafd5410SLokesh Vutla return offset % info->bl_len; 133eafd5410SLokesh Vutla } 134eafd5410SLokesh Vutla 135eafd5410SLokesh Vutla static int get_aligned_image_size(struct spl_load_info *info, int data_size, 136eafd5410SLokesh Vutla int offset) 137eafd5410SLokesh Vutla { 1383cc1f380SLokesh Vutla data_size = data_size + get_aligned_image_overhead(info, offset); 1393cc1f380SLokesh Vutla 140eafd5410SLokesh Vutla if (info->filename) 1413cc1f380SLokesh Vutla return data_size; 142eafd5410SLokesh Vutla 143eafd5410SLokesh Vutla return (data_size + info->bl_len - 1) / info->bl_len; 144eafd5410SLokesh Vutla } 145eafd5410SLokesh Vutla 1468baa3818SAndre Przywara /** 1478baa3818SAndre Przywara * spl_load_fit_image(): load the image described in a certain FIT node 1488baa3818SAndre Przywara * @info: points to information about the device to load data from 1498baa3818SAndre Przywara * @sector: the start sector of the FIT image on the device 1508baa3818SAndre Przywara * @fit: points to the flattened device tree blob describing the FIT 1518baa3818SAndre Przywara * image 1528baa3818SAndre Przywara * @base_offset: the beginning of the data area containing the actual 1538baa3818SAndre Przywara * image data, relative to the beginning of the FIT 1548baa3818SAndre Przywara * @node: offset of the DT node describing the image to load (relative 1558baa3818SAndre Przywara * to @fit) 1568baa3818SAndre Przywara * @image_info: will be filled with information about the loaded image 1578baa3818SAndre Przywara * If the FIT node does not contain a "load" (address) property, 1588baa3818SAndre Przywara * the image gets loaded to the address pointed to by the 1598baa3818SAndre Przywara * load_addr member in this struct. 1608baa3818SAndre Przywara * 1618baa3818SAndre Przywara * Return: 0 on success or a negative error number. 1628baa3818SAndre Przywara */ 1638baa3818SAndre Przywara static int spl_load_fit_image(struct spl_load_info *info, ulong sector, 1648baa3818SAndre Przywara void *fit, ulong base_offset, int node, 1658baa3818SAndre Przywara struct spl_image_info *image_info) 1668baa3818SAndre Przywara { 1670ea10b9fSYork Sun int offset; 1688baa3818SAndre Przywara size_t length; 1690ea10b9fSYork Sun int len; 170d18ad2e6SYork Sun ulong size; 1718baa3818SAndre Przywara ulong load_addr, load_ptr; 1728baa3818SAndre Przywara void *src; 1738baa3818SAndre Przywara ulong overhead; 1748baa3818SAndre Przywara int nr_sectors; 1758baa3818SAndre Przywara int align_len = ARCH_DMA_MINALIGN - 1; 176b81c4739SYork Sun uint8_t image_comp = -1, type = -1; 1770ea10b9fSYork Sun const void *data; 178b86dc419SPeng Fan bool external_data = false; 179b81c4739SYork Sun 180b81c4739SYork Sun if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) { 181b81c4739SYork Sun if (fit_image_get_comp(fit, node, &image_comp)) 182b81c4739SYork Sun puts("Cannot get image compression format.\n"); 183b81c4739SYork Sun else 184b81c4739SYork Sun debug("%s ", genimg_get_comp_name(image_comp)); 185b81c4739SYork Sun 186b81c4739SYork Sun if (fit_image_get_type(fit, node, &type)) 187b81c4739SYork Sun puts("Cannot get image type.\n"); 188b81c4739SYork Sun else 189b81c4739SYork Sun debug("%s ", genimg_get_type_name(type)); 190b81c4739SYork Sun } 1918baa3818SAndre Przywara 1920ea10b9fSYork Sun if (fit_image_get_load(fit, node, &load_addr)) 1938baa3818SAndre Przywara load_addr = image_info->load_addr; 1940ea10b9fSYork Sun 195b86dc419SPeng Fan if (!fit_image_get_data_position(fit, node, &offset)) { 196b86dc419SPeng Fan external_data = true; 197b86dc419SPeng Fan } else if (!fit_image_get_data_offset(fit, node, &offset)) { 1980ea10b9fSYork Sun offset += base_offset; 199b86dc419SPeng Fan external_data = true; 200b86dc419SPeng Fan } 201b86dc419SPeng Fan 202b86dc419SPeng Fan if (external_data) { 203b86dc419SPeng Fan /* External data */ 2040ea10b9fSYork Sun if (fit_image_get_data_size(fit, node, &len)) 2050ea10b9fSYork Sun return -ENOENT; 2060ea10b9fSYork Sun 2078baa3818SAndre Przywara load_ptr = (load_addr + align_len) & ~align_len; 20863363c40SAndy Yan #if defined(CONFIG_ARCH_ROCKCHIP) 209a5e7a2d4SKever Yang if ((load_ptr < CONFIG_SYS_SDRAM_BASE) || 210a5e7a2d4SKever Yang (load_ptr >= CONFIG_SYS_SDRAM_BASE + SDRAM_MAX_SIZE)) 21190d1164aSKever Yang load_ptr = (ulong)memalign(ARCH_DMA_MINALIGN, len); 21290d1164aSKever Yang #endif 2130ea10b9fSYork Sun length = len; 2148baa3818SAndre Przywara 2158baa3818SAndre Przywara overhead = get_aligned_image_overhead(info, offset); 2168baa3818SAndre Przywara nr_sectors = get_aligned_image_size(info, length, offset); 2178baa3818SAndre Przywara 2180ea10b9fSYork Sun if (info->read(info, 2190ea10b9fSYork Sun sector + get_aligned_image_offset(info, offset), 2208baa3818SAndre Przywara nr_sectors, (void *)load_ptr) != nr_sectors) 2218baa3818SAndre Przywara return -EIO; 2228baa3818SAndre Przywara 2230ea10b9fSYork Sun debug("External data: dst=%lx, offset=%x, size=%lx\n", 2240ea10b9fSYork Sun load_ptr, offset, (unsigned long)length); 2258baa3818SAndre Przywara src = (void *)load_ptr + overhead; 2260ea10b9fSYork Sun } else { 2270ea10b9fSYork Sun /* Embedded data */ 2280ea10b9fSYork Sun if (fit_image_get_data(fit, node, &data, &length)) { 2290ea10b9fSYork Sun puts("Cannot get image data/size\n"); 2300ea10b9fSYork Sun return -ENOENT; 2310ea10b9fSYork Sun } 2320ea10b9fSYork Sun debug("Embedded data: dst=%lx, size=%lx\n", load_addr, 2330ea10b9fSYork Sun (unsigned long)length); 2340ea10b9fSYork Sun src = (void *)data; 2350ea10b9fSYork Sun } 2360ea10b9fSYork Sun 237a515b6d2SJoseph Chen /* Check hashes and signature */ 238a515b6d2SJoseph Chen printf("## Checking %s ... ", 239f3219d95SBen Whitten fit_get_name(fit, node, NULL)); 240b92c7cf4SJoseph Chen #ifdef CONFIG_FIT_SPL_PRINT 241b92c7cf4SJoseph Chen printf("\n"); 242b92c7cf4SJoseph Chen fit_image_print(fit, node, ""); 243b92c7cf4SJoseph Chen #endif 244f3219d95SBen Whitten if (!fit_image_verify_with_data(fit, node, 245f3219d95SBen Whitten src, length)) 246f3219d95SBen Whitten return -EPERM; 247f3219d95SBen Whitten puts("OK\n"); 248f3219d95SBen Whitten 2498baa3818SAndre Przywara #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS 2508baa3818SAndre Przywara board_fit_image_post_process(&src, &length); 2518baa3818SAndre Przywara #endif 2528baa3818SAndre Przywara 253b81c4739SYork Sun if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && 254b81c4739SYork Sun IS_ENABLED(CONFIG_SPL_GZIP) && 255b81c4739SYork Sun image_comp == IH_COMP_GZIP && 256b81c4739SYork Sun type == IH_TYPE_KERNEL) { 257d18ad2e6SYork Sun size = length; 258b81c4739SYork Sun if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN, 259d18ad2e6SYork Sun src, &size)) { 260b81c4739SYork Sun puts("Uncompressing error\n"); 261b81c4739SYork Sun return -EIO; 262b81c4739SYork Sun } 263d18ad2e6SYork Sun length = size; 264b81c4739SYork Sun } else { 2658baa3818SAndre Przywara memcpy((void *)load_addr, src, length); 266b81c4739SYork Sun } 2678baa3818SAndre Przywara 2688baa3818SAndre Przywara if (image_info) { 2698baa3818SAndre Przywara image_info->load_addr = load_addr; 2708baa3818SAndre Przywara image_info->size = length; 2718baa3818SAndre Przywara image_info->entry_point = fdt_getprop_u32(fit, node, "entry"); 2728baa3818SAndre Przywara } 2738baa3818SAndre Przywara 2748baa3818SAndre Przywara return 0; 2758baa3818SAndre Przywara } 2768baa3818SAndre Przywara 2779719e14fSPhilipp Tomsich static int spl_fit_append_fdt(struct spl_image_info *spl_image, 2789719e14fSPhilipp Tomsich struct spl_load_info *info, ulong sector, 2799719e14fSPhilipp Tomsich void *fit, int images, ulong base_offset) 2809719e14fSPhilipp Tomsich { 2819719e14fSPhilipp Tomsich struct spl_image_info image_info; 2829719e14fSPhilipp Tomsich int node, ret; 2839719e14fSPhilipp Tomsich 2849719e14fSPhilipp Tomsich /* Figure out which device tree the board wants to use */ 2859719e14fSPhilipp Tomsich node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0); 2869719e14fSPhilipp Tomsich if (node < 0) { 2879719e14fSPhilipp Tomsich debug("%s: cannot find FDT node\n", __func__); 2889719e14fSPhilipp Tomsich return node; 2899719e14fSPhilipp Tomsich } 2909719e14fSPhilipp Tomsich 2919719e14fSPhilipp Tomsich /* 2929719e14fSPhilipp Tomsich * Read the device tree and place it after the image. 2939719e14fSPhilipp Tomsich * Align the destination address to ARCH_DMA_MINALIGN. 2949719e14fSPhilipp Tomsich */ 2959719e14fSPhilipp Tomsich image_info.load_addr = spl_image->load_addr + spl_image->size; 2969719e14fSPhilipp Tomsich ret = spl_load_fit_image(info, sector, fit, base_offset, node, 2979719e14fSPhilipp Tomsich &image_info); 2981f1cf67bSPhilipp Tomsich 2991f1cf67bSPhilipp Tomsich if (ret < 0) 3001f1cf67bSPhilipp Tomsich return ret; 3011f1cf67bSPhilipp Tomsich 3021f1cf67bSPhilipp Tomsich /* Make the load-address of the FDT available for the SPL framework */ 3031f1cf67bSPhilipp Tomsich spl_image->fdt_addr = (void *)image_info.load_addr; 3048118cce3SPhilipp Tomsich #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) 3051f1cf67bSPhilipp Tomsich /* Try to make space, so we can inject details on the loadables */ 3061f1cf67bSPhilipp Tomsich ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192); 3078118cce3SPhilipp Tomsich #endif 3081f1cf67bSPhilipp Tomsich 3091f1cf67bSPhilipp Tomsich return ret; 3101f1cf67bSPhilipp Tomsich } 3111f1cf67bSPhilipp Tomsich 3121f1cf67bSPhilipp Tomsich static int spl_fit_record_loadable(const void *fit, int images, int index, 3131f1cf67bSPhilipp Tomsich void *blob, struct spl_image_info *image) 3141f1cf67bSPhilipp Tomsich { 3158118cce3SPhilipp Tomsich int ret = 0; 3168118cce3SPhilipp Tomsich #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) 3171f1cf67bSPhilipp Tomsich char *name; 3188118cce3SPhilipp Tomsich int node; 3191f1cf67bSPhilipp Tomsich 3201f1cf67bSPhilipp Tomsich ret = spl_fit_get_image_name(fit, images, "loadables", 3211f1cf67bSPhilipp Tomsich index, &name); 3221f1cf67bSPhilipp Tomsich if (ret < 0) 3231f1cf67bSPhilipp Tomsich return ret; 3241f1cf67bSPhilipp Tomsich 3251f1cf67bSPhilipp Tomsich node = spl_fit_get_image_node(fit, images, "loadables", index); 3261f1cf67bSPhilipp Tomsich 3271f1cf67bSPhilipp Tomsich ret = fdt_record_loadable(blob, index, name, image->load_addr, 3281f1cf67bSPhilipp Tomsich image->size, image->entry_point, 3291f1cf67bSPhilipp Tomsich fdt_getprop(fit, node, "type", NULL), 3301f1cf67bSPhilipp Tomsich fdt_getprop(fit, node, "os", NULL)); 3318118cce3SPhilipp Tomsich #endif 3329719e14fSPhilipp Tomsich return ret; 3339719e14fSPhilipp Tomsich } 3349719e14fSPhilipp Tomsich 3358118cce3SPhilipp Tomsich static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os) 3368118cce3SPhilipp Tomsich { 3378118cce3SPhilipp Tomsich #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) 3388118cce3SPhilipp Tomsich return -ENOTSUPP; 3398118cce3SPhilipp Tomsich #else 3408118cce3SPhilipp Tomsich return fit_image_get_os(fit, noffset, os); 3418118cce3SPhilipp Tomsich #endif 3428118cce3SPhilipp Tomsich } 3438118cce3SPhilipp Tomsich 3449a65720bSJason Zhu __weak int spl_fit_standalone_release(uintptr_t entry_point) 345342d050eSJason Zhu { 346342d050eSJason Zhu return 0; 347342d050eSJason Zhu } 348342d050eSJason Zhu 34922c7c1a8SJoseph Chen static int spl_internal_load_simple_fit(struct spl_image_info *spl_image, 35022c7c1a8SJoseph Chen struct spl_load_info *info, 35122c7c1a8SJoseph Chen ulong sector, void *fit) 352f1dcee59SSimon Glass { 353f1dcee59SSimon Glass int sectors; 3548baa3818SAndre Przywara ulong size; 355f1dcee59SSimon Glass unsigned long count; 3568baa3818SAndre Przywara struct spl_image_info image_info; 357ebec805aSYork Sun int node = -1; 358ebec805aSYork Sun int images, ret; 359eafd5410SLokesh Vutla int base_offset, align_len = ARCH_DMA_MINALIGN - 1; 360411cf32dSAndre Przywara int index = 0; 361f1dcee59SSimon Glass 362f1dcee59SSimon Glass /* 363ebec805aSYork Sun * For FIT with external data, figure out where the external images 364ebec805aSYork Sun * start. This is the base for the data-offset properties in each 365ebec805aSYork Sun * image. 366f1dcee59SSimon Glass */ 367f1dcee59SSimon Glass size = fdt_totalsize(fit); 368acd43290SJoseph Chen size = FIT_ALIGN(size); 369acd43290SJoseph Chen base_offset = FIT_ALIGN(size); 370f1dcee59SSimon Glass 371f1dcee59SSimon Glass /* 372f1dcee59SSimon Glass * So far we only have one block of data from the FIT. Read the entire 373f1dcee59SSimon Glass * thing, including that first block, placing it so it finishes before 374f1dcee59SSimon Glass * where we will load the image. 375f1dcee59SSimon Glass * 376f1dcee59SSimon Glass * Note that we will load the image such that its first byte will be 377f1dcee59SSimon Glass * at the load address. Since that byte may be part-way through a 378f1dcee59SSimon Glass * block, we may load the image up to one block before the load 379f1dcee59SSimon Glass * address. So take account of that here by subtracting an addition 380f1dcee59SSimon Glass * block length from the FIT start position. 381f1dcee59SSimon Glass * 382f1dcee59SSimon Glass * In fact the FIT has its own load address, but we assume it cannot 383f1dcee59SSimon Glass * be before CONFIG_SYS_TEXT_BASE. 384ebec805aSYork Sun * 385ebec805aSYork Sun * For FIT with data embedded, data is loaded as part of FIT image. 386ebec805aSYork Sun * For FIT with external data, data is not loaded in this step. 387f1dcee59SSimon Glass */ 3888b528709SLokesh Vutla fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len - 3898b528709SLokesh Vutla align_len) & ~align_len); 390eafd5410SLokesh Vutla sectors = get_aligned_image_size(info, size, 0); 391f1dcee59SSimon Glass count = info->read(info, sector, sectors, fit); 392f1dcee59SSimon Glass debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n", 393f1dcee59SSimon Glass sector, sectors, fit, count); 394f1dcee59SSimon Glass if (count == 0) 395f1dcee59SSimon Glass return -EIO; 396f1dcee59SSimon Glass 397736806fbSAndre Przywara /* find the node holding the images information */ 398f1dcee59SSimon Glass images = fdt_path_offset(fit, FIT_IMAGES_PATH); 399f1dcee59SSimon Glass if (images < 0) { 400f1dcee59SSimon Glass debug("%s: Cannot find /images node: %d\n", __func__, images); 401f1dcee59SSimon Glass return -1; 402f1dcee59SSimon Glass } 403736806fbSAndre Przywara 4041f452cbfSJoseph Chen /* if board sigs verify required, check self */ 4051f452cbfSJoseph Chen if (fit_board_verify_required_sigs() && 4061f452cbfSJoseph Chen !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) { 4071f452cbfSJoseph Chen printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n"); 4081f452cbfSJoseph Chen hang(); 4091f452cbfSJoseph Chen } 4101f452cbfSJoseph Chen 411583377c4SJoseph Chen /* verify the configure node by keys, if required */ 412583377c4SJoseph Chen #ifdef CONFIG_SPL_FIT_SIGNATURE 413583377c4SJoseph Chen int conf_noffset; 414583377c4SJoseph Chen 415583377c4SJoseph Chen conf_noffset = fit_conf_get_node(fit, NULL); 416*a7560f55SJoseph Chen if (conf_noffset <= 0) { 417*a7560f55SJoseph Chen printf("No default config node\n"); 418*a7560f55SJoseph Chen return -EINVAL; 419*a7560f55SJoseph Chen } 420*a7560f55SJoseph Chen 421583377c4SJoseph Chen ret = fit_config_verify(fit, conf_noffset); 422583377c4SJoseph Chen if (ret) { 423583377c4SJoseph Chen printf("fit verify configure failed, ret=%d\n", ret); 424583377c4SJoseph Chen return ret; 425583377c4SJoseph Chen } 426583377c4SJoseph Chen printf("\n"); 427583377c4SJoseph Chen 4287a137075SJoseph Chen #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT 4297a137075SJoseph Chen uint32_t this_index, min_index; 4307a137075SJoseph Chen 4317a137075SJoseph Chen ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL, 4327a137075SJoseph Chen &this_index, &min_index); 4337a137075SJoseph Chen if (ret) { 4347a137075SJoseph Chen printf("fit failed to get rollback index, ret=%d\n", ret); 4357a137075SJoseph Chen return ret; 4367a137075SJoseph Chen } else if (this_index < min_index) { 4377a137075SJoseph Chen printf("fit reject rollback: %d < %d(min)\n", 4387a137075SJoseph Chen this_index, min_index); 4397a137075SJoseph Chen return -EINVAL; 4407a137075SJoseph Chen } 4417a137075SJoseph Chen 442e5ca21e8SJoseph Chen printf("rollback index: %d >= %d(min), OK\n", this_index, min_index); 4437a137075SJoseph Chen #endif 4447a137075SJoseph Chen #endif 445342d050eSJason Zhu 446342d050eSJason Zhu /* 447342d050eSJason Zhu * If required to start the other core before load "loadables" 448342d050eSJason Zhu * firmwares, use the config "standalone" to load the other core's 449342d050eSJason Zhu * firmware, then start it. 450342d050eSJason Zhu * Normally, different cores' firmware is attach to the config 451342d050eSJason Zhu * "loadables" and load them together. 452342d050eSJason Zhu */ 453342d050eSJason Zhu if (node < 0) 454342d050eSJason Zhu node = spl_fit_get_image_node(fit, images, FIT_STANDALONE_PROP, 455342d050eSJason Zhu 0); 456342d050eSJason Zhu if (node > 0) { 457342d050eSJason Zhu /* Load the image and set up the spl_image structure */ 458342d050eSJason Zhu ret = spl_load_fit_image(info, sector, fit, base_offset, node, 4599a65720bSJason Zhu &image_info); 460342d050eSJason Zhu if (!ret) { 4619a65720bSJason Zhu if (image_info.entry_point == FDT_ERROR) 4629a65720bSJason Zhu image_info.entry_point = image_info.load_addr; 4639a65720bSJason Zhu 4649a65720bSJason Zhu ret = spl_fit_standalone_release(image_info.entry_point); 465342d050eSJason Zhu if (ret) 466342d050eSJason Zhu printf("Start standalone fail, ret = %d\n", 467342d050eSJason Zhu ret); 468342d050eSJason Zhu } 469342d050eSJason Zhu 470342d050eSJason Zhu node = -1; 471342d050eSJason Zhu } 472342d050eSJason Zhu 4739719e14fSPhilipp Tomsich /* 4749719e14fSPhilipp Tomsich * Find the U-Boot image using the following search order: 4759719e14fSPhilipp Tomsich * - start at 'firmware' (e.g. an ARM Trusted Firmware) 4769719e14fSPhilipp Tomsich * - fall back 'kernel' (e.g. a Falcon-mode OS boot 4779719e14fSPhilipp Tomsich * - fall back to using the first 'loadables' entry 4789719e14fSPhilipp Tomsich */ 479ebec805aSYork Sun if (node < 0) 480c04fe8bfSMichal Simek node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP, 481c04fe8bfSMichal Simek 0); 4829719e14fSPhilipp Tomsich #ifdef CONFIG_SPL_OS_BOOT 4839719e14fSPhilipp Tomsich if (node < 0) 4849719e14fSPhilipp Tomsich node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0); 4859719e14fSPhilipp Tomsich #endif 486f1dcee59SSimon Glass if (node < 0) { 487736806fbSAndre Przywara debug("could not find firmware image, trying loadables...\n"); 488736806fbSAndre Przywara node = spl_fit_get_image_node(fit, images, "loadables", 0); 489411cf32dSAndre Przywara /* 490411cf32dSAndre Przywara * If we pick the U-Boot image from "loadables", start at 491411cf32dSAndre Przywara * the second image when later loading additional images. 492411cf32dSAndre Przywara */ 493411cf32dSAndre Przywara index = 1; 494736806fbSAndre Przywara } 495736806fbSAndre Przywara if (node < 0) { 496736806fbSAndre Przywara debug("%s: Cannot find u-boot image node: %d\n", 497736806fbSAndre Przywara __func__, node); 498f1dcee59SSimon Glass return -1; 499f1dcee59SSimon Glass } 500f1dcee59SSimon Glass 5018baa3818SAndre Przywara /* Load the image and set up the spl_image structure */ 5028baa3818SAndre Przywara ret = spl_load_fit_image(info, sector, fit, base_offset, node, 5038baa3818SAndre Przywara spl_image); 5048baa3818SAndre Przywara if (ret) 5058baa3818SAndre Przywara return ret; 5068baa3818SAndre Przywara 5079719e14fSPhilipp Tomsich /* 5089719e14fSPhilipp Tomsich * For backward compatibility, we treat the first node that is 5099719e14fSPhilipp Tomsich * as a U-Boot image, if no OS-type has been declared. 5109719e14fSPhilipp Tomsich */ 5118118cce3SPhilipp Tomsich if (!spl_fit_image_get_os(fit, node, &spl_image->os)) 512ebec805aSYork Sun debug("Image OS is %s\n", genimg_get_os_name(spl_image->os)); 5139719e14fSPhilipp Tomsich #if !defined(CONFIG_SPL_OS_BOOT) 5149719e14fSPhilipp Tomsich else 515f4d7d859SSimon Glass spl_image->os = IH_OS_U_BOOT; 516ebec805aSYork Sun #endif 517f1dcee59SSimon Glass 518f1dcee59SSimon Glass /* 5199719e14fSPhilipp Tomsich * Booting a next-stage U-Boot may require us to append the FDT. 5209719e14fSPhilipp Tomsich * We allow this to fail, as the U-Boot image might embed its FDT. 521f1dcee59SSimon Glass */ 5229719e14fSPhilipp Tomsich if (spl_image->os == IH_OS_U_BOOT) 5239719e14fSPhilipp Tomsich spl_fit_append_fdt(spl_image, info, sector, fit, 5249719e14fSPhilipp Tomsich images, base_offset); 525411cf32dSAndre Przywara 526411cf32dSAndre Przywara /* Now check if there are more images for us to load */ 527411cf32dSAndre Przywara for (; ; index++) { 5289719e14fSPhilipp Tomsich uint8_t os_type = IH_OS_INVALID; 5299719e14fSPhilipp Tomsich 530411cf32dSAndre Przywara node = spl_fit_get_image_node(fit, images, "loadables", index); 531411cf32dSAndre Przywara if (node < 0) 532411cf32dSAndre Przywara break; 533411cf32dSAndre Przywara 534411cf32dSAndre Przywara ret = spl_load_fit_image(info, sector, fit, base_offset, node, 535411cf32dSAndre Przywara &image_info); 536411cf32dSAndre Przywara if (ret < 0) 537411cf32dSAndre Przywara continue; 538411cf32dSAndre Przywara 5398118cce3SPhilipp Tomsich if (!spl_fit_image_get_os(fit, node, &os_type)) 5409719e14fSPhilipp Tomsich debug("Loadable is %s\n", genimg_get_os_name(os_type)); 5419719e14fSPhilipp Tomsich 5421f1cf67bSPhilipp Tomsich if (os_type == IH_OS_U_BOOT) { 5431f1cf67bSPhilipp Tomsich spl_fit_append_fdt(&image_info, info, sector, 5449719e14fSPhilipp Tomsich fit, images, base_offset); 5451f1cf67bSPhilipp Tomsich spl_image->fdt_addr = image_info.fdt_addr; 5461f1cf67bSPhilipp Tomsich } 5479719e14fSPhilipp Tomsich 548411cf32dSAndre Przywara /* 549411cf32dSAndre Przywara * If the "firmware" image did not provide an entry point, 550411cf32dSAndre Przywara * use the first valid entry point from the loadables. 551411cf32dSAndre Przywara */ 552411cf32dSAndre Przywara if (spl_image->entry_point == FDT_ERROR && 553411cf32dSAndre Przywara image_info.entry_point != FDT_ERROR) 554411cf32dSAndre Przywara spl_image->entry_point = image_info.entry_point; 5551f1cf67bSPhilipp Tomsich 5561f1cf67bSPhilipp Tomsich /* Record our loadables into the FDT */ 5571f1cf67bSPhilipp Tomsich if (spl_image->fdt_addr) 5581f1cf67bSPhilipp Tomsich spl_fit_record_loadable(fit, images, index, 5591f1cf67bSPhilipp Tomsich spl_image->fdt_addr, 5601f1cf67bSPhilipp Tomsich &image_info); 561411cf32dSAndre Przywara } 562411cf32dSAndre Przywara 563411cf32dSAndre Przywara /* 564411cf32dSAndre Przywara * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's 565411cf32dSAndre Przywara * Makefile will set it to 0 and it will end up as the entry point 566411cf32dSAndre Przywara * here. What it actually means is: use the load address. 567411cf32dSAndre Przywara */ 568411cf32dSAndre Przywara if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0) 569411cf32dSAndre Przywara spl_image->entry_point = spl_image->load_addr; 570411cf32dSAndre Przywara 571411cf32dSAndre Przywara return 0; 572f1dcee59SSimon Glass } 57322c7c1a8SJoseph Chen 57422c7c1a8SJoseph Chen int spl_load_simple_fit(struct spl_image_info *spl_image, 57522c7c1a8SJoseph Chen struct spl_load_info *info, ulong sector, void *fit) 57622c7c1a8SJoseph Chen { 57722c7c1a8SJoseph Chen ulong sector_offs = sector; 57822c7c1a8SJoseph Chen int i; 57922c7c1a8SJoseph Chen 58022c7c1a8SJoseph Chen for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) { 58122c7c1a8SJoseph Chen if (i > 0) { 58222c7c1a8SJoseph Chen sector_offs += 58322c7c1a8SJoseph Chen i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len); 58422c7c1a8SJoseph Chen printf("Trying fit image at 0x%lx sector\n", sector_offs); 58522c7c1a8SJoseph Chen if (info->read(info, sector_offs, 1, fit) != 1) { 58622c7c1a8SJoseph Chen printf("IO error\n"); 58722c7c1a8SJoseph Chen continue; 58822c7c1a8SJoseph Chen } 58922c7c1a8SJoseph Chen } 59022c7c1a8SJoseph Chen 59122c7c1a8SJoseph Chen if (image_get_magic(fit) != FDT_MAGIC) { 59222c7c1a8SJoseph Chen printf("Bad fit magic\n"); 59322c7c1a8SJoseph Chen continue; 59422c7c1a8SJoseph Chen } 59522c7c1a8SJoseph Chen 59622c7c1a8SJoseph Chen if (!spl_internal_load_simple_fit(spl_image, info, 59722c7c1a8SJoseph Chen sector_offs, fit)) 59822c7c1a8SJoseph Chen return 0; 59922c7c1a8SJoseph Chen } 60022c7c1a8SJoseph Chen 60122c7c1a8SJoseph Chen return -EINVAL; 60222c7c1a8SJoseph Chen } 60322c7c1a8SJoseph Chen 604