SPL: Add support for specifying offset between header and image

Some image types (e.g. kwbimage v1) store the offset to SPL binary and
offset to U-Boot proper binary in their headers. To avoid reading SPL
binary when loading U-Boot proper, add support for specifying offset in
struct spl_image_info, which defines the offset from the beginning of
the header and the beginning of the executable data.

Initial support is added only for SPI, MMC and SATA code. We can extend
it later if needed.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>
Reviewed-by: Stefan Roese <sr@denx.de>
diff --git a/common/spl/spl_mmc.c b/common/spl/spl_mmc.c
index 4dff9bf..212a2b0 100644
--- a/common/spl/spl_mmc.c
+++ b/common/spl/spl_mmc.c
@@ -20,26 +20,40 @@
 static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc,
 			   ulong sector, struct image_header *header)
 {
+	u32 image_offset_sectors;
 	u32 image_size_sectors;
 	unsigned long count;
+	u32 image_offset;
 	int ret;
 
 	ret = spl_parse_image_header(spl_image, header);
 	if (ret)
 		return ret;
 
+	/* convert offset to sectors - round down */
+	image_offset_sectors = spl_image->offset / mmc->read_bl_len;
+	/* calculate remaining offset */
+	image_offset = spl_image->offset % mmc->read_bl_len;
+
 	/* convert size to sectors - round up */
 	image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
 			     mmc->read_bl_len;
 
 	/* Read the header too to avoid extra memcpy */
-	count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
+	count = blk_dread(mmc_get_blk_desc(mmc),
+			  sector + image_offset_sectors,
+			  image_size_sectors,
 			  (void *)(ulong)spl_image->load_addr);
 	debug("read %x sectors to %lx\n", image_size_sectors,
 	      spl_image->load_addr);
 	if (count != image_size_sectors)
 		return -EIO;
 
+	if (image_offset)
+		memmove((void *)(ulong)spl_image->load_addr,
+			(void *)(ulong)spl_image->load_addr + image_offset,
+			spl_image->size);
+
 	return 0;
 }