spl: Weed out CONFIG_SYS_TEXT_BASE usage
authorMarek Vasut <marex@denx.de>
Tue, 14 Aug 2018 09:27:02 +0000 (11:27 +0200)
committerTom Rini <trini@konsulko.com>
Wed, 26 Sep 2018 01:48:43 +0000 (21:48 -0400)
The SPL loaders assume that the CONFIG_SYS_TEXT_BASE memory location
is available and can be corrupted by loading ie. uImage or fitImage
headers there. Sometimes it could be beneficial to load the headers
elsewhere, ie. if CONFIG_SYS_TEXT_BASE is not yet writable while we
still want to parse the image headers in some local onchip memory to
ie. extract firmware from that image.

Add the possibility to override the location where the headers get
loaded by introducing new function, spl_get_load_buffer() which takes
two arguments -- offset from the CONFIG_SYS_TEXT_BASE and size of the
data that are to be loaded there -- and returns a valid buffer address
or hangs the system. The default behavior is the same as before, add
the offset to CONFIG_SYS_TEXT_BASE and return that address. User can
override the weak spl_get_load_buffer() function though.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
common/spl/spl.c
common/spl/spl_ext.c
common/spl/spl_fat.c
common/spl/spl_fit.c
common/spl/spl_mmc.c
common/spl/spl_nand.c
common/spl/spl_onenand.c
common/spl/spl_ram.c
common/spl/spl_spi.c
common/spl/spl_ubi.c
include/spl.h

index 19508c71685d82bd425c6912c32c79ce4085ad44..038f2b0e83c328f28cf9e814950773c5c41cfc18 100644 (file)
@@ -127,6 +127,11 @@ __weak void spl_board_prepare_for_boot(void)
        /* Nothing to do! */
 }
 
+__weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
+{
+       return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
+}
+
 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
 {
        ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
index fd30a61f9adbed0728e488b068187fcf9f7d70f1..fe052236053c5959b8f632ce43c1ee4795938569 100644 (file)
@@ -16,8 +16,7 @@ int spl_load_image_ext(struct spl_image_info *spl_image,
        loff_t filelen, actlen;
        disk_partition_t part_info = {};
 
-       header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
-                                               sizeof(struct image_header));
+       header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
 
        if (part_get_info(block_dev, partition, &part_info)) {
                printf("spl: no partition table found\n");
index 0403016bb433c7bdb2299bdcc215204c6cb6bcec..163e540622b18823bb082911b421f8c2fb56a2c4 100644 (file)
@@ -63,8 +63,7 @@ int spl_load_image_fat(struct spl_image_info *spl_image,
        if (err)
                goto end;
 
-       header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
-                                               sizeof(struct image_header));
+       header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
 
        err = file_fat_read(filename, header, sizeof(struct image_header));
        if (err <= 0)
index 9eabb1c1058b3df670bfaf9925b14e15a19623c3..f08e5018c3f31b9da3afbf3c3a7f140269e105a4 100644 (file)
@@ -357,7 +357,7 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,
        struct spl_image_info image_info;
        int node = -1;
        int images, ret;
-       int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
+       int base_offset, hsize, align_len = ARCH_DMA_MINALIGN - 1;
        int index = 0;
 
        /*
@@ -386,8 +386,8 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,
         * For FIT with data embedded, data is loaded as part of FIT image.
         * For FIT with external data, data is not loaded in this step.
         */
-       fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
-                       align_len) & ~align_len);
+       hsize = (size + info->bl_len + align_len) & ~align_len;
+       fit = spl_get_load_buffer(-hsize, hsize);
        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",
index 0b2f0595702e1c3f567ae2db920413ef1ee4effd..75c41598e6bc22d223e1cda65c7272b6a7246fb4 100644 (file)
@@ -55,13 +55,13 @@ int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
 {
        unsigned long count;
        struct image_header *header;
+       struct blk_desc *bd = mmc_get_blk_desc(mmc);
        int ret = 0;
 
-       header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
-                                        sizeof(struct image_header));
+       header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
 
        /* read image header to find the image size & load address */
-       count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header);
+       count = blk_dread(bd, sector, 1, header);
        debug("hdr read sector %lx, count=%lu\n", sector, count);
        if (count == 0) {
                ret = -EIO;
index 2722fd3860e55a149ffa1c1aee123db0244a4389..6eb190f1ea540da99fc2a6db9c3bedf92aadf4c8 100644 (file)
@@ -83,8 +83,8 @@ static int spl_nand_load_image(struct spl_image_info *spl_image,
 #endif
        nand_init();
 
-       /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
-       header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
+       header = spl_get_load_buffer(0, sizeof(*header));
+
 #ifdef CONFIG_SPL_OS_BOOT
        if (!spl_start_uboot()) {
                /*
index d32333935a79b6fa75229ca2ec59cde3852d2748..ee30f328e6027e92a22f0b387b7aa326c467dbae 100644 (file)
@@ -21,8 +21,7 @@ static int spl_onenand_load_image(struct spl_image_info *spl_image,
 
        debug("spl: onenand\n");
 
-       /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
-       header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
+       header = spl_get_load_buffer(0, CONFIG_SYS_ONENAND_PAGE_SIZE);
        /* Load u-boot */
        onenand_spl_load_image(CONFIG_SYS_ONENAND_U_BOOT_OFFS,
                CONFIG_SYS_ONENAND_PAGE_SIZE, (void *)header);
index e594beaeaa3a5534b1188c4fdc3b14152b9d3724..619b39a537485f79fd3527ee243a2b312c5b3ab7 100644 (file)
@@ -63,8 +63,9 @@ static int spl_ram_load_image(struct spl_image_info *spl_image,
                         * No binman support or no information. For now, fix it
                         * to the address pointed to by U-Boot.
                         */
-                       u_boot_pos = CONFIG_SYS_TEXT_BASE -
-                                       sizeof(struct image_header);
+                       header = spl_get_load_buffer(-sizeof(*header),
+                                                    sizeof(*header));
+
                }
                header = (struct image_header *)map_sysmem(u_boot_pos, 0);
 
index ba60a3a3c5053d07b026d3aeb96914faa6dbf111..e10cf0124fb74bcc888b5e016a16d67a14bb0a07 100644 (file)
@@ -88,8 +88,7 @@ static int spl_spi_load_image(struct spl_image_info *spl_image,
                return -ENODEV;
        }
 
-       /* use CONFIG_SYS_TEXT_BASE as temporary storage area */
-       header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
+       header = spl_get_load_buffer(-sizeof(*header), 0x40);
 
 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
        payload_offs = fdtdec_get_config_int(gd->fdt_blob,
index a7939e90305f1250b5806cdd268e54fb097e77ea..67e5fadd7c0343f03b356529c5209da722975184 100644 (file)
@@ -61,8 +61,7 @@ int spl_ubi_load_image(struct spl_image_info *spl_image,
                puts("Loading Linux failed, falling back to U-Boot.\n");
        }
 #endif
-       header = (struct image_header *)
-               (CONFIG_SYS_TEXT_BASE - sizeof(struct image_header));
+       header = spl_get_load_buffer(-sizeof(*header), sizeof(header));
        volumes[0].vol_id = CONFIG_SPL_UBI_LOAD_MONITOR_ID;
        volumes[0].load_addr = (void *)header;
 
index 7fad62c043ee082422107872102c5483853b0ffa..b42683c9e71a99bcb41fe53a5198f98d59989011 100644 (file)
@@ -303,4 +303,13 @@ void board_return_to_bootrom(void);
  *                        the boot-payload
  */
 void spl_perform_fixups(struct spl_image_info *spl_image);
+
+/*
+ * spl_get_load_buffer() - get buffer for loading partial image data
+ *
+ * Returns memory area which can be populated by partial image data,
+ * ie. uImage or fitImage header.
+ */
+struct image_header *spl_get_load_buffer(ssize_t offset, size_t size);
+
 #endif