Merge branch '2019-10-28-azure-ci-support'
[oweals/u-boot.git] / cmd / mem.c
index a690957d0ff93b99296aea9acffd46306415a15e..c6b8038fc9dd61e7de81eb4a02c76d6805ec6d34 100644 (file)
--- a/cmd/mem.c
+++ b/cmd/mem.c
@@ -1,8 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2000
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /*
 #include <cli.h>
 #include <command.h>
 #include <console.h>
-#ifdef CONFIG_HAS_DATAFLASH
-#include <dataflash.h>
-#endif
 #include <hash.h>
-#include <inttypes.h>
 #include <mapmem.h>
 #include <watchdog.h>
 #include <asm/io.h>
@@ -52,10 +47,8 @@ static       ulong   base_address = 0;
 #define DISP_LINE_LEN  16
 static int do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-       ulong   addr, length;
-#if defined(CONFIG_HAS_DATAFLASH)
-       ulong   nbytes, linebytes;
-#endif
+       ulong   addr, length, bytes;
+       const void *buf;
        int     size;
        int rc = 0;
 
@@ -88,61 +81,13 @@ static int do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                        length = simple_strtoul(argv[2], NULL, 16);
        }
 
-#if defined(CONFIG_HAS_DATAFLASH)
-       /* Print the lines.
-        *
-        * We buffer all read data, so we can make sure data is read only
-        * once, and all accesses are with the specified bus width.
-        */
-       nbytes = length * size;
-       do {
-               char    linebuf[DISP_LINE_LEN];
-               void* p;
-               linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
-
-               rc = read_dataflash(addr, (linebytes/size)*size, linebuf);
-               p = (rc == DATAFLASH_OK) ? linebuf : (void*)addr;
-               print_buffer(addr, p, size, linebytes/size, DISP_LINE_LEN/size);
-
-               nbytes -= linebytes;
-               addr += linebytes;
-               if (ctrlc()) {
-                       rc = 1;
-                       break;
-               }
-       } while (nbytes > 0);
-#else
-
-# if defined(CONFIG_BLACKFIN)
-       /* See if we're trying to display L1 inst */
-       if (addr_bfin_on_chip_mem(addr)) {
-               char linebuf[DISP_LINE_LEN];
-               ulong linebytes, nbytes = length * size;
-               do {
-                       linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
-                       memcpy(linebuf, (void *)addr, linebytes);
-                       print_buffer(addr, linebuf, size, linebytes/size, DISP_LINE_LEN/size);
-
-                       nbytes -= linebytes;
-                       addr += linebytes;
-                       if (ctrlc()) {
-                               rc = 1;
-                               break;
-                       }
-               } while (nbytes > 0);
-       } else
-# endif
-
-       {
-               ulong bytes = size * length;
-               const void *buf = map_sysmem(addr, bytes);
+       bytes = size * length;
+       buf = map_sysmem(addr, bytes);
 
-               /* Print the lines. */
-               print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
-               addr += bytes;
-               unmap_sysmem(buf);
-       }
-#endif
+       /* Print the lines. */
+       print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
+       addr += bytes;
+       unmap_sysmem(buf);
 
        dp_last_addr = addr;
        dp_last_length = length;
@@ -307,20 +252,6 @@ static int do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
        count = simple_strtoul(argv[3], NULL, 16);
 
-#ifdef CONFIG_HAS_DATAFLASH
-       if (addr_dataflash(addr1) | addr_dataflash(addr2)){
-               puts ("Comparison with DataFlash space not supported.\n\r");
-               return 0;
-       }
-#endif
-
-#ifdef CONFIG_BLACKFIN
-       if (addr_bfin_on_chip_mem(addr1) || addr_bfin_on_chip_mem(addr2)) {
-               puts ("Comparison with L1 instruction memory not supported.\n\r");
-               return 0;
-       }
-#endif
-
        bytes = size * count;
        base = buf1 = map_sysmem(addr1, bytes);
        buf2 = map_sysmem(addr2, bytes);
@@ -343,8 +274,7 @@ static int do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                if (word1 != word2) {
                        ulong offset = buf1 - base;
 #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
-                       printf("%s at 0x%p (%#0*"PRIx64") != %s at 0x%p (%#0*"
-                              PRIx64 ")\n",
+                       printf("%s at 0x%p (%#0*llx) != %s at 0x%p (%#0*llx)\n",
                               type, (void *)(addr1 + offset), size, word1,
                               type, (void *)(addr2 + offset), size, word2);
 #else
@@ -372,10 +302,8 @@ static int do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
 static int do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-       ulong   addr, dest, count, bytes;
+       ulong   addr, dest, count;
        int     size;
-       const void *src;
-       void *buf;
 
        if (argc != 4)
                return CMD_RET_USAGE;
@@ -398,13 +326,9 @@ static int do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                return 1;
        }
 
-#ifndef CONFIG_SYS_NO_FLASH
+#ifdef CONFIG_MTD_NOR_FLASH
        /* check if we are copying to Flash */
-       if ( (addr2info(dest) != NULL)
-#ifdef CONFIG_HAS_DATAFLASH
-          && (!addr_dataflash(dest))
-#endif
-          ) {
+       if (addr2info(dest) != NULL) {
                int rc;
 
                puts ("Copy to Flash... ");
@@ -419,75 +343,7 @@ static int do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        }
 #endif
 
-#ifdef CONFIG_HAS_DATAFLASH
-       /* Check if we are copying from RAM or Flash to DataFlash */
-       if (addr_dataflash(dest) && !addr_dataflash(addr)){
-               int rc;
-
-               puts ("Copy to DataFlash... ");
-
-               rc = write_dataflash (dest, addr, count*size);
-
-               if (rc != 1) {
-                       dataflash_perror (rc);
-                       return (1);
-               }
-               puts ("done\n");
-               return 0;
-       }
-
-       /* Check if we are copying from DataFlash to RAM */
-       if (addr_dataflash(addr) && !addr_dataflash(dest)
-#ifndef CONFIG_SYS_NO_FLASH
-                                && (addr2info(dest) == NULL)
-#endif
-          ){
-               int rc;
-               rc = read_dataflash(addr, count * size, (char *) dest);
-               if (rc != 1) {
-                       dataflash_perror (rc);
-                       return (1);
-               }
-               return 0;
-       }
-
-       if (addr_dataflash(addr) && addr_dataflash(dest)){
-               puts ("Unsupported combination of source/destination.\n\r");
-               return 1;
-       }
-#endif
-
-#ifdef CONFIG_BLACKFIN
-       /* See if we're copying to/from L1 inst */
-       if (addr_bfin_on_chip_mem(dest) || addr_bfin_on_chip_mem(addr)) {
-               memcpy((void *)dest, (void *)addr, count * size);
-               return 0;
-       }
-#endif
-
-       bytes = size * count;
-       buf = map_sysmem(dest, bytes);
-       src = map_sysmem(addr, bytes);
-       while (count-- > 0) {
-               if (size == 4)
-                       *((u32 *)buf) = *((u32  *)src);
-#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
-               else if (size == 8)
-                       *((u64 *)buf) = *((u64 *)src);
-#endif
-               else if (size == 2)
-                       *((u16 *)buf) = *((u16 *)src);
-               else
-                       *((u8 *)buf) = *((u8 *)src);
-               src += size;
-               buf += size;
-
-               /* reset watchdog from time to time */
-               if ((count % (64 << 10)) == 0)
-                       WATCHDOG_RESET();
-       }
-       unmap_sysmem(buf);
-       unmap_sysmem(src);
+       memcpy((void *)dest, (void *)addr, count * size);
 
        return 0;
 }
@@ -1132,20 +988,6 @@ mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
                addr += base_address;
        }
 
-#ifdef CONFIG_HAS_DATAFLASH
-       if (addr_dataflash(addr)){
-               puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
-               return 0;
-       }
-#endif
-
-#ifdef CONFIG_BLACKFIN
-       if (addr_bfin_on_chip_mem(addr)) {
-               puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
-               return 0;
-       }
-#endif
-
        /* Print the address, followed by value.  Then accept input for
         * the next value.  A non-converted value exits.
         */
@@ -1156,7 +998,7 @@ mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
                        printf(" %08x", *((u32 *)ptr));
 #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
                else if (size == 8)
-                       printf(" %016" PRIx64, *((u64 *)ptr));
+                       printf(" %016llx", *((u64 *)ptr));
 #endif
                else if (size == 2)
                        printf(" %04x", *((u16 *)ptr));
@@ -1227,7 +1069,7 @@ static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
        av = argv + 1;
        ac = argc - 1;
-#ifdef CONFIG_HASH_VERIFY
+#ifdef CONFIG_CRC32_VERIFY
        if (strcmp(*av, "-v") == 0) {
                flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV;
                av++;
@@ -1240,6 +1082,49 @@ static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
 #endif
 
+#ifdef CONFIG_CMD_RANDOM
+static int do_random(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       unsigned long addr, len;
+       unsigned long seed; // NOT INITIALIZED ON PURPOSE
+       unsigned int *buf, *start;
+       unsigned char *buf8;
+       unsigned int i;
+
+       if (argc < 3 || argc > 4) {
+               printf("usage: %s <addr> <len> [<seed>]\n", argv[0]);
+               return 0;
+       }
+
+       len = simple_strtoul(argv[2], NULL, 16);
+       addr = simple_strtoul(argv[1], NULL, 16);
+
+       if (argc == 4) {
+               seed = simple_strtoul(argv[3], NULL, 16);
+               if (seed == 0) {
+                       printf("The seed cannot be 0. Using 0xDEADBEEF.\n");
+                       seed = 0xDEADBEEF;
+               }
+       } else {
+               seed = get_timer(0) ^ rand();
+       }
+
+       srand(seed);
+       start = map_sysmem(addr, len);
+       buf = start;
+       for (i = 0; i < (len / 4); i++)
+               *buf++ = rand();
+
+       buf8 = (unsigned char *)buf;
+       for (i = 0; i < (len % 4); i++)
+               *buf8++ = rand() & 0xFF;
+
+       unmap_sysmem(start);
+       printf("%lu bytes filled with random data\n", len);
+       return 1;
+}
+#endif
+
 /**************************************************/
 U_BOOT_CMD(
        md,     3,      1,      do_mem_md,
@@ -1305,7 +1190,7 @@ U_BOOT_CMD(
 
 #ifdef CONFIG_CMD_CRC32
 
-#ifndef CONFIG_HASH_VERIFY
+#ifndef CONFIG_CRC32_VERIFY
 
 U_BOOT_CMD(
        crc32,  4,      1,      do_mem_crc,
@@ -1313,7 +1198,7 @@ U_BOOT_CMD(
        "address count [addr]\n    - compute CRC32 checksum [save at addr]"
 );
 
-#else  /* CONFIG_HASH_VERIFY */
+#else  /* CONFIG_CRC32_VERIFY */
 
 U_BOOT_CMD(
        crc32,  5,      1,      do_mem_crc,
@@ -1322,7 +1207,7 @@ U_BOOT_CMD(
        "-v address count crc\n    - verify crc of memory area"
 );
 
-#endif /* CONFIG_HASH_VERIFY */
+#endif /* CONFIG_CRC32_VERIFY */
 
 #endif
 
@@ -1408,3 +1293,12 @@ U_BOOT_CMD(
        ""
 );
 #endif
+
+#ifdef CONFIG_CMD_RANDOM
+U_BOOT_CMD(
+       random, 4,      0,      do_random,
+       "fill memory with random pattern",
+       "<addr> <len> [<seed>]\n"
+       "   - Fill 'len' bytes of memory starting at 'addr' with random data\n"
+);
+#endif