diff options
Diffstat (limited to 'common/spl')
-rw-r--r-- | common/spl/spl_fat.c | 36 | ||||
-rw-r--r-- | common/spl/spl_fit.c | 76 | ||||
-rw-r--r-- | common/spl/spl_mmc.c | 1 | ||||
-rw-r--r-- | common/spl/spl_nand.c | 37 | ||||
-rw-r--r-- | common/spl/spl_ymodem.c | 98 |
5 files changed, 209 insertions, 39 deletions
diff --git a/common/spl/spl_fat.c b/common/spl/spl_fat.c index 5b0d969..db67618 100644 --- a/common/spl/spl_fat.c +++ b/common/spl/spl_fat.c @@ -15,6 +15,7 @@ #include <fat.h> #include <errno.h> #include <image.h> +#include <libfdt.h> static int fat_registered; @@ -39,6 +40,20 @@ static int spl_register_fat_device(struct blk_desc *block_dev, int partition) return err; } +static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, + ulong size, void *buf) +{ + loff_t actread; + int ret; + char *filename = (char *)load->filename; + + ret = fat_read_file(filename, buf, file_offset, size, &actread); + if (ret) + return ret; + + return actread; +} + int spl_load_image_fat(struct blk_desc *block_dev, int partition, const char *filename) @@ -57,11 +72,24 @@ int spl_load_image_fat(struct blk_desc *block_dev, if (err <= 0) goto end; - err = spl_parse_image_header(header); - if (err) - goto end; + if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && + image_get_magic(header) == FDT_MAGIC) { + struct spl_load_info load; + + debug("Found FIT\n"); + load.read = spl_fit_read; + load.bl_len = 1; + load.filename = (void *)filename; + load.priv = NULL; - err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0); + return spl_load_simple_fit(&load, 0, header); + } else { + err = spl_parse_image_header(header); + if (err) + goto end; + + err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0); + } end: #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT diff --git a/common/spl/spl_fit.c b/common/spl/spl_fit.c index 26842ba..a828f72 100644 --- a/common/spl/spl_fit.c +++ b/common/spl/spl_fit.c @@ -67,9 +67,7 @@ static int spl_fit_select_fdt(const void *fdt, int images, int *fdt_offsetp) *fdt_offsetp = fdt_getprop_u32(fdt, fdt_node, "data-offset"); len = fdt_getprop_u32(fdt, fdt_node, "data-size"); -#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT - printf("FIT: Selected '%s'\n", name); -#endif + debug("FIT: Selected '%s'\n", name); return len; } @@ -87,6 +85,42 @@ static int spl_fit_select_fdt(const void *fdt, int images, int *fdt_offsetp) return -ENOENT; } +static int get_aligned_image_offset(struct spl_load_info *info, int offset) +{ + /* + * If it is a FS read, get the first address before offset which is + * aligned to ARCH_DMA_MINALIGN. If it is raw read return the + * block number to which offset belongs. + */ + if (info->filename) + return offset & ~(ARCH_DMA_MINALIGN - 1); + + return offset / info->bl_len; +} + +static int get_aligned_image_overhead(struct spl_load_info *info, int offset) +{ + /* + * If it is a FS read, get the difference between the offset and + * the first address before offset which is aligned to + * ARCH_DMA_MINALIGN. If it is raw read return the offset within the + * block. + */ + if (info->filename) + return offset & (ARCH_DMA_MINALIGN - 1); + + return offset % info->bl_len; +} + +static int get_aligned_image_size(struct spl_load_info *info, int data_size, + int offset) +{ + if (info->filename) + return data_size + get_aligned_image_overhead(info, offset); + + return (data_size + info->bl_len - 1) / info->bl_len; +} + int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit) { int sectors; @@ -96,7 +130,7 @@ int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit) void *load_ptr; int fdt_offset, fdt_len; int data_offset, data_size; - int base_offset; + int base_offset, align_len = ARCH_DMA_MINALIGN - 1; int src_sector; void *dst; @@ -123,7 +157,8 @@ int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit) * be before CONFIG_SYS_TEXT_BASE. */ fit = (void *)(CONFIG_SYS_TEXT_BASE - size - info->bl_len); - sectors = (size + info->bl_len - 1) / info->bl_len; + fit = (void *)ALIGN((ulong)fit, 8); + 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); @@ -156,19 +191,23 @@ int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit) * byte will be at 'load'. This may mean we need to load it starting * before then, since we can only read whole blocks. */ - sectors = (data_size + info->bl_len - 1) / info->bl_len; 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 - (data_offset % info->bl_len); + dst = load_ptr; /* Read the image */ - src_sector = sector + data_offset / info->bl_len; - debug("image: data_offset=%x, dst=%p, src_sector=%x, sectors=%x\n", - data_offset, dst, src_sector, sectors); + 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); @@ -178,13 +217,15 @@ int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit) /* * 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 = load_ptr + data_size; + dst = (void *)((load + data_size + align_len) & ~align_len); fdt_offset += base_offset; - count = info->read(info, sector + fdt_offset / info->bl_len, sectors, - dst); - debug("fit read %x sectors to %x, dst %p, data_offset %x\n", - sectors, spl_image.load_addr, dst, fdt_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; @@ -193,7 +234,10 @@ int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit) * After this we will have the U-Boot image and its device tree ready * for us to start. */ - memcpy(dst, dst + fdt_offset % info->bl_len, fdt_len); + 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; } diff --git a/common/spl/spl_mmc.c b/common/spl/spl_mmc.c index 6adfc9b..ef8583a 100644 --- a/common/spl/spl_mmc.c +++ b/common/spl/spl_mmc.c @@ -76,6 +76,7 @@ static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector) debug("Found FIT\n"); load.dev = mmc; load.priv = NULL; + load.filename = NULL; load.bl_len = mmc->read_bl_len; load.read = h_spl_load_read; ret = spl_load_simple_fit(&load, sector, header); diff --git a/common/spl/spl_nand.c b/common/spl/spl_nand.c index bbd9546..7cf0d1b 100644 --- a/common/spl/spl_nand.c +++ b/common/spl/spl_nand.c @@ -9,6 +9,8 @@ #include <spl.h> #include <asm/io.h> #include <nand.h> +#include <libfdt_env.h> +#include <fdt.h> #if defined(CONFIG_SPL_NAND_RAW_ONLY) int spl_nand_load_image(void) @@ -24,6 +26,19 @@ int spl_nand_load_image(void) return 0; } #else + +static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs, + ulong size, void *dst) +{ + int ret; + + ret = nand_spl_load_image(offs, size, dst); + if (!ret) + return size; + else + return 0; +} + static int spl_nand_load_element(int offset, struct image_header *header) { int err; @@ -32,12 +47,24 @@ static int spl_nand_load_element(int offset, struct image_header *header) if (err) return err; - err = spl_parse_image_header(header); - if (err) - return err; + if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && + image_get_magic(header) == FDT_MAGIC) { + struct spl_load_info load; - return nand_spl_load_image(offset, spl_image.size, - (void *)(unsigned long)spl_image.load_addr); + debug("Found FIT\n"); + load.dev = NULL; + load.priv = NULL; + load.filename = NULL; + load.bl_len = 1; + load.read = spl_nand_fit_read; + return spl_load_simple_fit(&load, offset, header); + } else { + err = spl_parse_image_header(header); + if (err) + return err; + return nand_spl_load_image(offset, spl_image.size, + (void *)(ulong)spl_image.load_addr); + } } int spl_nand_load_image(void) diff --git a/common/spl/spl_ymodem.c b/common/spl/spl_ymodem.c index 4f26ea5..5402301 100644 --- a/common/spl/spl_ymodem.c +++ b/common/spl/spl_ymodem.c @@ -14,15 +14,60 @@ #include <xyzModem.h> #include <asm/u-boot.h> #include <asm/utils.h> +#include <libfdt.h> #define BUF_SIZE 1024 +/* + * Information required to load image using ymodem. + * + * @image_read: Now of bytes read from the image. + * @buf: pointer to the previous read block. + */ +struct ymodem_fit_info { + int image_read; + char *buf; +}; + static int getcymodem(void) { if (tstc()) return (getc()); return -1; } +static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset, + ulong size, void *addr) +{ + int res, err; + struct ymodem_fit_info *info = load->priv; + char *buf = info->buf; + + while (info->image_read < offset) { + res = xyzModem_stream_read(buf, BUF_SIZE, &err); + if (res <= 0) + return res; + info->image_read += res; + } + + if (info->image_read > offset) { + res = info->image_read - offset; + memcpy(addr, &buf[BUF_SIZE - res], res); + addr = addr + res; + } + + while (info->image_read < offset + size) { + res = xyzModem_stream_read(buf, BUF_SIZE, &err); + if (res <= 0) + return res; + + memcpy(addr, buf, res); + info->image_read += res; + addr += res; + } + + return size; +} + int spl_ymodem_load_image(void) { int size = 0; @@ -31,30 +76,55 @@ int spl_ymodem_load_image(void) int ret; connection_info_t info; char buf[BUF_SIZE]; - ulong store_addr = ~0; ulong addr = 0; info.mode = xyzModem_ymodem; ret = xyzModem_stream_open(&info, &err); + if (ret) { + printf("spl: ymodem err - %s\n", xyzModem_error(err)); + return ret; + } + + res = xyzModem_stream_read(buf, BUF_SIZE, &err); + if (res <= 0) + goto end_stream; + + if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && + image_get_magic((struct image_header *)buf) == FDT_MAGIC) { + struct spl_load_info load; + struct ymodem_fit_info info; + + debug("Found FIT\n"); + load.dev = NULL; + load.priv = (void *)&info; + load.filename = NULL; + load.bl_len = 1; + info.buf = buf; + info.image_read = BUF_SIZE; + load.read = ymodem_read_fit; + ret = spl_load_simple_fit(&load, 0, (void *)buf); + size = info.image_read; - if (!ret) { - while ((res = - xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) { - if (addr == 0) { - ret = spl_parse_image_header((struct image_header *)buf); - if (ret) - return ret; - } - store_addr = addr + spl_image.load_addr; + while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) + size += res; + } else { + spl_parse_image_header((struct image_header *)buf); + ret = spl_parse_image_header((struct image_header *)buf); + if (ret) + return ret; + addr = spl_image.load_addr; + memcpy((void *)addr, buf, res); + size += res; + addr += res; + + while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) { + memcpy((void *)addr, buf, res); size += res; addr += res; - memcpy((char *)(store_addr), buf, res); } - } else { - printf("spl: ymodem err - %s\n", xyzModem_error(err)); - return ret; } +end_stream: xyzModem_stream_close(&err); xyzModem_stream_terminate(false, &getcymodem); |