/** * spl_load_fit_image(): load the image described in a certain FIT node * @info: points to information about the device to load data from * @sector: the start sector of the FIT image on the device * @fit: points to the flattened device tree blob describing the FIT * image * @base_offset: the beginning of the data area containing the actual * image data, relative to the beginning of the FIT * @node: offset of the DT node describing the image to load (relative * to @fit) * @image_info: will be filled with information about the loaded image * If the FIT node does not contain a "load" (address) property, * the image gets loaded to the address pointed to by the * load_addr member in this struct. * * Return: 0 on success or a negative error number. */ static int spl_load_fit_image(struct spl_load_info *info, ulong sector, void *fit, ulong base_offset, int node, struct spl_image_info *image_info) { int offset; size_t length; int len; ulong size; ulong load_addr, load_ptr; void *src; ulong overhead; int nr_sectors; int align_len = ARCH_DMA_MINALIGN - 1; uint8_t image_comp = -1, type = -1; const void *data; if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) { if (fit_image_get_comp(fit, node, &image_comp)) puts("Cannot get image compression format.\n"); else debug("%s ", genimg_get_comp_name(image_comp)); if (fit_image_get_type(fit, node, &type)) puts("Cannot get image type.\n"); else debug("%s ", genimg_get_type_name(type)); } if (fit_image_get_load(fit, node, &load_addr)) load_addr = image_info->load_addr; if (!fit_image_get_data_offset(fit, node, &offset)) { /* External data */ offset += base_offset; if (fit_image_get_data_size(fit, node, &len)) return -ENOENT; load_ptr = (load_addr + align_len) & ~align_len; length = len; overhead = get_aligned_image_overhead(info, offset); nr_sectors = get_aligned_image_size(info, length, offset); if (info->read(info, sector + get_aligned_image_offset(info, offset), nr_sectors, (void *)load_ptr) != nr_sectors) return -EIO; debug("External data: dst=%lx, offset=%x, size=%lx\n", load_ptr, offset, (unsigned long)length); src = (void *)load_ptr + overhead; } else { /* Embedded data */ if (fit_image_get_data(fit, node, &data, &length)) { puts("Cannot get image data/size\n"); return -ENOENT; } debug("Embedded data: dst=%lx, size=%lx\n", load_addr, (unsigned long)length); src = (void *)data; } #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS board_fit_image_post_process(&src, &length); #endif if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP && type == IH_TYPE_KERNEL) { size = length; if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN, src, &size)) { puts("Uncompressing error\n"); return -EIO; } length = size; } else { memcpy((void *)load_addr, src, length); } if (image_info) { image_info->load_addr = load_addr; image_info->size = length; image_info->entry_point = fdt_getprop_u32(fit, node, "entry"); } return 0; }
int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit) { int sectors; ulong size, load; unsigned long count; int node, images; void *load_ptr; int fdt_offset, fdt_len; int data_offset, data_size; int base_offset, align_len = ARCH_DMA_MINALIGN - 1; int src_sector; void *dst; /* * Figure out where the external images start. This is the base for the * data-offset properties in each image. */ size = fdt_totalsize(fit); size = (size + 3) & ~3; base_offset = (size + 3) & ~3; /* * So far we only have one block of data from the FIT. Read the entire * thing, including that first block, placing it so it finishes before * where we will load the image. * * Note that we will load the image such that its first byte will be * at the load address. Since that byte may be part-way through a * block, we may load the image up to one block before the load * address. So take account of that here by subtracting an addition * block length from the FIT start position. * * In fact the FIT has its own load address, but we assume it cannot * be before CONFIG_SYS_TEXT_BASE. */ fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len - align_len) & ~align_len); sectors = get_aligned_image_size(info, size, 0); count = info->read(info, sector, sectors, fit); debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n", sector, sectors, fit, count); if (count == 0) return -EIO; /* find the firmware image to load */ images = fdt_path_offset(fit, FIT_IMAGES_PATH); if (images < 0) { debug("%s: Cannot find /images node: %d\n", __func__, images); return -1; } node = fdt_first_subnode(fit, images); if (node < 0) { debug("%s: Cannot find first image node: %d\n", __func__, node); return -1; } /* Get its information and set up the spl_image structure */ data_offset = fdt_getprop_u32(fit, node, "data-offset"); data_size = fdt_getprop_u32(fit, node, "data-size"); load = fdt_getprop_u32(fit, node, "load"); debug("data_offset=%x, data_size=%x\n", data_offset, data_size); spl_image.load_addr = load; spl_image.entry_point = load; spl_image.os = IH_OS_U_BOOT; /* * Work out where to place the image. We read it so that the first * byte will be at 'load'. This may mean we need to load it starting * before then, since we can only read whole blocks. */ data_offset += base_offset; sectors = get_aligned_image_size(info, data_size, data_offset); load_ptr = (void *)load; debug("U-Boot size %x, data %p\n", data_size, load_ptr); dst = load_ptr; /* Read the image */ src_sector = sector + get_aligned_image_offset(info, data_offset); debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n", dst, src_sector, sectors); count = info->read(info, src_sector, sectors, dst); if (count != sectors) return -EIO; debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset, data_size); memcpy(dst, dst + get_aligned_image_overhead(info, data_offset), data_size); /* Figure out which device tree the board wants to use */ fdt_len = spl_fit_select_fdt(fit, images, &fdt_offset); if (fdt_len < 0) return fdt_len; /* * Read the device tree and place it after the image. There may be * some extra data before it since we can only read entire blocks. * And also align the destination address to ARCH_DMA_MINALIGN. */ dst = (void *)((load + data_size + align_len) & ~align_len); fdt_offset += base_offset; sectors = get_aligned_image_size(info, fdt_len, fdt_offset); src_sector = sector + get_aligned_image_offset(info, fdt_offset); count = info->read(info, src_sector, sectors, dst); debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n", dst, src_sector, sectors); if (count != sectors) return -EIO; /* * Copy the device tree so that it starts immediately after the image. * After this we will have the U-Boot image and its device tree ready * for us to start. */ debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset, fdt_len); memcpy(load_ptr + data_size, dst + get_aligned_image_overhead(info, fdt_offset), fdt_len); return 0; }