Convert CONFIG_SYS_BOOTM_LEN to Kconfig
[platform/kernel/u-boot.git] / cmd / load.c
index 549e563..1224a7f 100644 (file)
@@ -16,6 +16,7 @@
 #include <exports.h>
 #include <flash.h>
 #include <image.h>
+#include <lmb.h>
 #include <mapmem.h>
 #include <net.h>
 #include <s_record.h>
@@ -137,6 +138,7 @@ static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
 
 static ulong load_serial(long offset)
 {
+       struct lmb lmb;
        char    record[SREC_MAXRECLEN + 1];     /* buffer for one S-Record      */
        char    binbuf[SREC_MAXBINLEN];         /* buffer for binary data       */
        int     binlen;                         /* no. of data bytes in S-Rec.  */
@@ -147,6 +149,9 @@ static ulong load_serial(long offset)
        ulong   start_addr = ~0;
        ulong   end_addr   =  0;
        int     line_count =  0;
+       long ret;
+
+       lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
 
        while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
                type = srec_decode(record, &binlen, &addr, binbuf);
@@ -172,7 +177,14 @@ static ulong load_serial(long offset)
                    } else
 #endif
                    {
+                       ret = lmb_reserve(&lmb, store_addr, binlen);
+                       if (ret) {
+                               printf("\nCannot overwrite reserved area (%08lx..%08lx)\n",
+                                       store_addr, store_addr + binlen);
+                               return ret;
+                       }
                        memcpy((char *)(store_addr), binbuf, binlen);
+                       lmb_free(&lmb, store_addr, binlen);
                    }
                    if ((store_addr) < start_addr)
                        start_addr = store_addr;
@@ -474,6 +486,14 @@ static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
 
                addr = load_serial_ymodem(offset, xyzModem_ymodem);
 
+               if (addr == ~0) {
+                       image_load_addr = 0;
+                       printf("## Binary (ymodem) download aborted\n");
+                       rcode = 1;
+               } else {
+                       printf("## Start Addr      = 0x%08lX\n", addr);
+                       image_load_addr = addr;
+               }
        } else if (strcmp(argv[0],"loadx")==0) {
                printf("## Ready for binary (xmodem) download "
                        "to 0x%08lX at %d bps...\n",
@@ -482,6 +502,14 @@ static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,
 
                addr = load_serial_ymodem(offset, xyzModem_xmodem);
 
+               if (addr == ~0) {
+                       image_load_addr = 0;
+                       printf("## Binary (xmodem) download aborted\n");
+                       rcode = 1;
+               } else {
+                       printf("## Start Addr      = 0x%08lX\n", addr);
+                       image_load_addr = addr;
+               }
        } else {
 
                printf("## Ready for binary (kermit) download "
@@ -1035,6 +1063,44 @@ static ulong load_serial_ymodem(ulong offset, int mode)
 
 #endif
 
+#if defined(CONFIG_CMD_LOADM)
+static int do_load_memory_bin(struct cmd_tbl *cmdtp, int flag, int argc,
+                             char *const argv[])
+{
+       ulong   addr, dest, size;
+       void    *src, *dst;
+
+       if (argc != 4)
+               return CMD_RET_USAGE;
+
+       addr = simple_strtoul(argv[1], NULL, 16);
+
+       dest = simple_strtoul(argv[2], NULL, 16);
+
+       size = simple_strtoul(argv[3], NULL, 16);
+
+       if (!size) {
+               printf("loadm: can not load zero bytes\n");
+               return 1;
+       }
+
+       src = map_sysmem(addr, size);
+       dst = map_sysmem(dest, size);
+
+       memcpy(dst, src, size);
+
+       unmap_sysmem(src);
+       unmap_sysmem(dst);
+
+       if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
+               efi_set_bootdev("Mem", "", "", map_sysmem(dest, 0), size);
+
+       printf("loaded bin to memory: size: %lu\n", size);
+
+       return 0;
+}
+#endif
+
 /* -------------------------------------------------------------------- */
 
 #if defined(CONFIG_CMD_LOADS)
@@ -1109,3 +1175,13 @@ U_BOOT_CMD(
 );
 
 #endif /* CONFIG_CMD_LOADB */
+
+#if defined(CONFIG_CMD_LOADM)
+U_BOOT_CMD(
+       loadm, 4, 0,    do_load_memory_bin,
+       "load binary blob from source address to destination address",
+       "[src_addr] [dst_addr] [size]\n"
+       "     - load a binary blob from one memory location to other"
+       " from src_addr to dst_addr by size bytes"
+);
+#endif /* CONFIG_CMD_LOADM */