1 // SPDX-License-Identifier: GPL-2.0+
15 #include <bootretry.h>
26 #include <linux/bitops.h>
27 #include <linux/compiler.h>
28 #include <linux/ctype.h>
29 #include <linux/delay.h>
31 DECLARE_GLOBAL_DATA_PTR;
33 #ifndef CONFIG_SYS_MEMTEST_SCRATCH
34 #define CONFIG_SYS_MEMTEST_SCRATCH 0
37 /* Create a compile-time value */
38 #ifdef MEM_SUPPORT_64BIT_DATA
39 #define SUPPORT_64BIT_DATA 1
42 #define SUPPORT_64BIT_DATA 0
46 static int mod_mem(struct cmd_tbl *, int, int, int, char * const []);
48 /* Display values from last command.
49 * Memory modify remembered values are different from display memory.
51 static ulong dp_last_addr, dp_last_size;
52 static ulong dp_last_length = 0x40;
53 static ulong mm_last_addr, mm_last_size;
55 static ulong base_address = 0;
56 #ifdef CONFIG_MEM_SEARCH
57 static u8 search_buf[64];
58 static uint search_len;
64 * md{.b, .w, .l, .q} {addr} {len}
66 #define DISP_LINE_LEN 16
67 static int do_mem_md(struct cmd_tbl *cmdtp, int flag, int argc,
70 ulong addr, length, bytes;
75 /* We use the last specified parameters, unless new ones are
80 length = dp_last_length;
85 if ((flag & CMD_FLAG_REPEAT) == 0) {
86 /* New command specified. Check for a size specification.
87 * Defaults to long if no or incorrect specification.
89 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
92 /* Address is specified since argc > 1
94 addr = simple_strtoul(argv[1], NULL, 16);
97 /* If another parameter, it is the length to display.
98 * Length is the number of objects, not number of bytes.
101 length = simple_strtoul(argv[2], NULL, 16);
104 bytes = size * length;
105 buf = map_sysmem(addr, bytes);
107 /* Print the lines. */
108 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
113 dp_last_length = length;
118 static int do_mem_mm(struct cmd_tbl *cmdtp, int flag, int argc,
121 return mod_mem (cmdtp, 1, flag, argc, argv);
124 static int do_mem_nm(struct cmd_tbl *cmdtp, int flag, int argc,
127 return mod_mem (cmdtp, 0, flag, argc, argv);
130 static int do_mem_mw(struct cmd_tbl *cmdtp, int flag, int argc,
133 ulong writeval; /* 64-bit if SUPPORT_64BIT_DATA */
139 if ((argc < 3) || (argc > 4))
140 return CMD_RET_USAGE;
142 /* Check for size specification.
144 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
147 /* Address is specified since argc > 1
149 addr = simple_strtoul(argv[1], NULL, 16);
150 addr += base_address;
152 /* Get the value to write.
154 if (SUPPORT_64BIT_DATA)
155 writeval = simple_strtoull(argv[2], NULL, 16);
157 writeval = simple_strtoul(argv[2], NULL, 16);
161 count = simple_strtoul(argv[3], NULL, 16);
166 bytes = size * count;
167 start = map_sysmem(addr, bytes);
169 while (count-- > 0) {
171 *((u32 *)buf) = (u32)writeval;
172 else if (SUPPORT_64BIT_DATA && size == 8)
173 *((ulong *)buf) = writeval;
175 *((u16 *)buf) = (u16)writeval;
177 *((u8 *)buf) = (u8)writeval;
184 #ifdef CONFIG_CMD_MX_CYCLIC
185 static int do_mem_mdc(struct cmd_tbl *cmdtp, int flag, int argc,
192 return CMD_RET_USAGE;
194 count = simple_strtoul(argv[3], NULL, 10);
197 do_mem_md (NULL, 0, 3, argv);
199 /* delay for <count> ms... */
200 for (i=0; i<count; i++)
203 /* check for ctrl-c to abort... */
213 static int do_mem_mwc(struct cmd_tbl *cmdtp, int flag, int argc,
220 return CMD_RET_USAGE;
222 count = simple_strtoul(argv[3], NULL, 10);
225 do_mem_mw (NULL, 0, 3, argv);
227 /* delay for <count> ms... */
228 for (i=0; i<count; i++)
231 /* check for ctrl-c to abort... */
240 #endif /* CONFIG_CMD_MX_CYCLIC */
242 static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc,
245 ulong addr1, addr2, count, ngood, bytes;
249 const void *buf1, *buf2, *base;
250 ulong word1, word2; /* 64-bit if SUPPORT_64BIT_DATA */
253 return CMD_RET_USAGE;
255 /* Check for size specification.
257 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
259 type = size == 8 ? "double word" :
261 size == 2 ? "halfword" : "byte";
263 addr1 = simple_strtoul(argv[1], NULL, 16);
264 addr1 += base_address;
266 addr2 = simple_strtoul(argv[2], NULL, 16);
267 addr2 += base_address;
269 count = simple_strtoul(argv[3], NULL, 16);
271 bytes = size * count;
272 base = buf1 = map_sysmem(addr1, bytes);
273 buf2 = map_sysmem(addr2, bytes);
274 for (ngood = 0; ngood < count; ++ngood) {
276 word1 = *(u32 *)buf1;
277 word2 = *(u32 *)buf2;
278 } else if (SUPPORT_64BIT_DATA && size == 8) {
279 word1 = *(ulong *)buf1;
280 word2 = *(ulong *)buf2;
281 } else if (size == 2) {
282 word1 = *(u16 *)buf1;
283 word2 = *(u16 *)buf2;
288 if (word1 != word2) {
289 ulong offset = buf1 - base;
290 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
291 type, (ulong)(addr1 + offset), size, word1,
292 type, (ulong)(addr2 + offset), size, word2);
300 /* reset watchdog from time to time */
301 if ((ngood % (64 << 10)) == 0)
307 printf("Total of %ld %s(s) were the same\n", ngood, type);
311 static int do_mem_cp(struct cmd_tbl *cmdtp, int flag, int argc,
314 ulong addr, dest, count;
319 return CMD_RET_USAGE;
321 /* Check for size specification.
323 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
326 addr = simple_strtoul(argv[1], NULL, 16);
327 addr += base_address;
329 dest = simple_strtoul(argv[2], NULL, 16);
330 dest += base_address;
332 count = simple_strtoul(argv[3], NULL, 16);
335 puts ("Zero length ???\n");
339 src = map_sysmem(addr, count * size);
340 dst = map_sysmem(dest, count * size);
342 #ifdef CONFIG_MTD_NOR_FLASH
343 /* check if we are copying to Flash */
344 if (addr2info((ulong)dst)) {
347 puts ("Copy to Flash... ");
349 rc = flash_write((char *)src, (ulong)dst, count * size);
363 memcpy(dst, src, count * size);
370 #ifdef CONFIG_MEM_SEARCH
371 static int do_mem_search(struct cmd_tbl *cmdtp, int flag, int argc,
374 ulong addr, length, bytes, offset;
377 ulong last_pos; /* Offset of last match in 'size' units*/
378 ulong last_addr; /* Address of last displayed line */
384 /* We use the last specified parameters, unless new ones are entered */
387 length = dp_last_length;
390 return CMD_RET_USAGE;
392 if ((!flag & CMD_FLAG_REPEAT)) {
394 * Check for a size specification.
395 * Defaults to long if no or incorrect specification.
397 size = cmd_get_data_size(argv[0], 4);
398 if (size < 0 && size != -2 /* string */)
402 while (argc && *argv[0] == '-') {
407 else if (ch == 'l' && isxdigit(argv[0][2]))
408 limit = simple_strtoul(argv[0] + 2, NULL, 16);
410 return CMD_RET_USAGE;
414 /* Address is specified since argc > 1 */
415 addr = simple_strtoul(argv[0], NULL, 16);
416 addr += base_address;
418 /* Length is the number of objects, not number of bytes */
419 length = simple_strtoul(argv[1], NULL, 16);
421 /* Read the bytes to search for */
422 end = search_buf + sizeof(search_buf);
423 for (i = 2, ptr = search_buf; i < argc && ptr < end; i++) {
424 if (SUPPORT_64BIT_DATA && size == 8) {
425 u64 val = simple_strtoull(argv[i], NULL, 16);
428 } else if (size == -2) { /* string */
429 int len = min(strlen(argv[i]),
430 (size_t)(end - ptr));
432 memcpy(ptr, argv[i], len);
436 u32 val = simple_strtoul(argv[i], NULL, 16);
452 search_len = ptr - search_buf;
458 bytes = size * length;
459 buf = map_sysmem(addr, bytes);
463 for (offset = 0; offset <= bytes - search_len && count < limit;
465 void *ptr = buf + offset;
467 if (!memcmp(ptr, search_buf, search_len)) {
468 uint align = (addr + offset) & 0xf;
469 ulong match = addr + offset;
471 if (!count || (last_addr & ~0xf) != (match & ~0xf)) {
475 print_buffer(match - align, ptr - align,
477 ALIGN(search_len + align,
481 last_pos = offset / size;
487 printf("%d match%s", count, count == 1 ? "" : "es");
489 printf(" (repeat command to check for more)");
492 env_set_hex("memmatches", count);
493 env_set_hex("memaddr", last_addr);
494 env_set_hex("mempos", last_pos);
498 dp_last_addr = addr + offset / size;
500 dp_last_length = length - offset / size;
502 return count ? 0 : CMD_RET_FAILURE;
506 static int do_mem_base(struct cmd_tbl *cmdtp, int flag, int argc,
510 /* Set new base address.
512 base_address = simple_strtoul(argv[1], NULL, 16);
514 /* Print the current base address.
516 printf("Base Address: 0x%08lx\n", base_address);
520 static int do_mem_loop(struct cmd_tbl *cmdtp, int flag, int argc,
523 ulong addr, length, i, bytes;
525 volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */
527 volatile u16 *shortp;
532 return CMD_RET_USAGE;
535 * Check for a size specification.
536 * Defaults to long if no or incorrect specification.
538 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
541 /* Address is always specified.
543 addr = simple_strtoul(argv[1], NULL, 16);
545 /* Length is the number of objects, not number of bytes.
547 length = simple_strtoul(argv[2], NULL, 16);
549 bytes = size * length;
550 buf = map_sysmem(addr, bytes);
552 /* We want to optimize the loops to run as fast as possible.
553 * If we have only one object, just run infinite loops.
556 if (SUPPORT_64BIT_DATA && size == 8) {
576 if (SUPPORT_64BIT_DATA && size == 8) {
612 static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc,
615 ulong addr, length, i, bytes;
617 volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */
618 ulong data; /* 64-bit if SUPPORT_64BIT_DATA */
620 volatile u16 *shortp;
625 return CMD_RET_USAGE;
628 * Check for a size specification.
629 * Defaults to long if no or incorrect specification.
631 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
634 /* Address is always specified.
636 addr = simple_strtoul(argv[1], NULL, 16);
638 /* Length is the number of objects, not number of bytes.
640 length = simple_strtoul(argv[2], NULL, 16);
643 if (SUPPORT_64BIT_DATA)
644 data = simple_strtoull(argv[3], NULL, 16);
646 data = simple_strtoul(argv[3], NULL, 16);
648 bytes = size * length;
649 buf = map_sysmem(addr, bytes);
651 /* We want to optimize the loops to run as fast as possible.
652 * If we have only one object, just run infinite loops.
655 if (SUPPORT_64BIT_DATA && size == 8) {
675 if (SUPPORT_64BIT_DATA && size == 8) {
706 #endif /* CONFIG_LOOPW */
708 #ifdef CONFIG_CMD_MEMTEST
709 static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr,
720 vu_long anti_pattern;
722 static const ulong bitpattern[] = {
723 0x00000001, /* single bit */
724 0x00000003, /* two adjacent bits */
725 0x00000007, /* three adjacent bits */
726 0x0000000F, /* four adjacent bits */
727 0x00000005, /* two non-adjacent bits */
728 0x00000015, /* three non-adjacent bits */
729 0x00000055, /* four non-adjacent bits */
730 0xaaaaaaaa, /* alternating 1/0 */
733 num_words = (end_addr - start_addr) / sizeof(vu_long);
736 * Data line test: write a pattern to the first
737 * location, write the 1's complement to a 'parking'
738 * address (changes the state of the data bus so a
739 * floating bus doesn't give a false OK), and then
740 * read the value back. Note that we read it back
741 * into a variable because the next time we read it,
742 * it might be right (been there, tough to explain to
743 * the quality guys why it prints a failure when the
744 * "is" and "should be" are obviously the same in the
747 * Rather than exhaustively testing, we test some
748 * patterns by shifting '1' bits through a field of
749 * '0's and '0' bits through a field of '1's (i.e.
750 * pattern and ~pattern).
753 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
755 for (; val != 0; val <<= 1) {
757 *dummy = ~val; /* clear the test data off the bus */
759 if (readback != val) {
760 printf("FAILURE (data line): "
761 "expected %08lx, actual %08lx\n",
770 if (readback != ~val) {
771 printf("FAILURE (data line): "
772 "Is %08lx, should be %08lx\n",
782 * Based on code whose Original Author and Copyright
783 * information follows: Copyright (c) 1998 by Michael
784 * Barr. This software is placed into the public
785 * domain and may be used for any purpose. However,
786 * this notice must not be changed or removed and no
787 * warranty is either expressed or implied by its
788 * publication or distribution.
794 * Description: Test the address bus wiring in a
795 * memory region by performing a walking
796 * 1's test on the relevant bits of the
797 * address and checking for aliasing.
798 * This test will find single-bit
799 * address failures such as stuck-high,
800 * stuck-low, and shorted pins. The base
801 * address and size of the region are
802 * selected by the caller.
804 * Notes: For best results, the selected base
805 * address should have enough LSB 0's to
806 * guarantee single address bit changes.
807 * For example, to test a 64-Kbyte
808 * region, select a base address on a
809 * 64-Kbyte boundary. Also, select the
810 * region size as a power-of-two if at
813 * Returns: 0 if the test succeeds, 1 if the test fails.
815 pattern = (vu_long) 0xaaaaaaaa;
816 anti_pattern = (vu_long) 0x55555555;
818 debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words);
820 * Write the default pattern at each of the
821 * power-of-two offsets.
823 for (offset = 1; offset < num_words; offset <<= 1)
824 addr[offset] = pattern;
827 * Check for address bits stuck high.
830 addr[test_offset] = anti_pattern;
832 for (offset = 1; offset < num_words; offset <<= 1) {
834 if (temp != pattern) {
835 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
836 " expected 0x%.8lx, actual 0x%.8lx\n",
837 start_addr + offset*sizeof(vu_long),
844 addr[test_offset] = pattern;
848 * Check for addr bits stuck low or shorted.
850 for (test_offset = 1; test_offset < num_words; test_offset <<= 1) {
851 addr[test_offset] = anti_pattern;
853 for (offset = 1; offset < num_words; offset <<= 1) {
855 if ((temp != pattern) && (offset != test_offset)) {
856 printf("\nFAILURE: Address bit stuck low or"
857 " shorted @ 0x%.8lx: expected 0x%.8lx,"
859 start_addr + offset*sizeof(vu_long),
866 addr[test_offset] = pattern;
870 * Description: Test the integrity of a physical
871 * memory device by performing an
872 * increment/decrement test over the
873 * entire region. In the process every
874 * storage bit in the device is tested
875 * as a zero and a one. The base address
876 * and the size of the region are
877 * selected by the caller.
879 * Returns: 0 if the test succeeds, 1 if the test fails.
884 * Fill memory with a known pattern.
886 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
888 addr[offset] = pattern;
892 * Check each location and invert it for the second pass.
894 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
897 if (temp != pattern) {
898 printf("\nFAILURE (read/write) @ 0x%.8lx:"
899 " expected 0x%.8lx, actual 0x%.8lx)\n",
900 start_addr + offset*sizeof(vu_long),
907 anti_pattern = ~pattern;
908 addr[offset] = anti_pattern;
912 * Check each location for the inverted pattern and zero it.
914 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
916 anti_pattern = ~pattern;
918 if (temp != anti_pattern) {
919 printf("\nFAILURE (read/write): @ 0x%.8lx:"
920 " expected 0x%.8lx, actual 0x%.8lx)\n",
921 start_addr + offset*sizeof(vu_long),
933 static int compare_regions(volatile unsigned long *bufa,
934 volatile unsigned long *bufb, size_t count)
936 volatile unsigned long *p1 = bufa;
937 volatile unsigned long *p2 = bufb;
941 for (i = 0; i < count; i++, p1++, p2++) {
943 printf("FAILURE: 0x%08lx != 0x%08lx (delta=0x%08lx -> bit %ld) at offset 0x%08lx\n",
944 (unsigned long)*p1, (unsigned long)*p2,
945 *p1 ^ *p2, __ffs(*p1 ^ *p2),
946 (unsigned long)(i * sizeof(unsigned long)));
954 static ulong test_bitflip_comparison(volatile unsigned long *bufa,
955 volatile unsigned long *bufb, size_t count)
957 volatile unsigned long *p1 = bufa;
958 volatile unsigned long *p2 = bufb;
965 max = sizeof(unsigned long) * 8;
966 for (k = 0; k < max; k++) {
967 q = 0x00000001L << k;
968 for (j = 0; j < 8; j++) {
971 p1 = (volatile unsigned long *)bufa;
972 p2 = (volatile unsigned long *)bufb;
973 for (i = 0; i < count; i++)
974 *p1++ = *p2++ = (i % 2) == 0 ? q : ~q;
976 errs += compare_regions(bufa, bufb, count);
986 static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr,
987 vu_long pattern, int iteration)
995 /* Alternate the pattern */
1000 * Flip the pattern each time to make lots of zeros and
1001 * then, the next time, lots of ones. We decrement
1002 * the "negative" patterns and increment the "positive"
1003 * patterns to preserve this feature.
1005 if (pattern & 0x80000000)
1006 pattern = -pattern; /* complement & increment */
1010 length = (end_addr - start_addr) / sizeof(ulong);
1012 printf("\rPattern %08lX Writing..."
1014 "\b\b\b\b\b\b\b\b\b\b",
1017 for (addr = buf, val = pattern; addr < end; addr++) {
1025 for (addr = buf, val = pattern; addr < end; addr++) {
1028 if (readback != val) {
1029 ulong offset = addr - buf;
1031 printf("\nMem error @ 0x%08X: "
1032 "found %08lX, expected %08lX\n",
1033 (uint)(uintptr_t)(start_addr + offset*sizeof(vu_long)),
1046 * Perform a memory test. A more complete alternative test can be
1047 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
1048 * interrupted by ctrl-c or by a failure of one of the sub-tests.
1050 static int do_mem_mtest(struct cmd_tbl *cmdtp, int flag, int argc,
1054 vu_long scratch_space;
1055 vu_long *buf, *dummy = &scratch_space;
1056 ulong iteration_limit = 0;
1058 ulong errs = 0; /* number of errors, or -1 if interrupted */
1062 start = CONFIG_SYS_MEMTEST_START;
1063 end = CONFIG_SYS_MEMTEST_END;
1066 if (strict_strtoul(argv[1], 16, &start) < 0)
1067 return CMD_RET_USAGE;
1070 if (strict_strtoul(argv[2], 16, &end) < 0)
1071 return CMD_RET_USAGE;
1074 if (strict_strtoul(argv[3], 16, &pattern) < 0)
1075 return CMD_RET_USAGE;
1078 if (strict_strtoul(argv[4], 16, &iteration_limit) < 0)
1079 return CMD_RET_USAGE;
1082 printf("Refusing to do empty test\n");
1086 printf("Testing %08lx ... %08lx:\n", start, end);
1087 debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
1090 buf = map_sysmem(start, end - start);
1092 !iteration_limit || iteration < iteration_limit;
1099 printf("Iteration: %6d\r", iteration + 1);
1101 if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST)) {
1102 errs = mem_test_alt(buf, start, end, dummy);
1106 errs = test_bitflip_comparison(buf,
1107 buf + (end - start) / 2,
1109 sizeof(unsigned long));
1111 errs = mem_test_quick(buf, start, end, pattern,
1119 unmap_sysmem((void *)buf);
1122 /* Memory test was aborted - write a newline to finish off */
1125 printf("Tested %d iteration(s) with %lu errors.\n", iteration, count);
1129 #endif /* CONFIG_CMD_MEMTEST */
1134 * mm{.b, .w, .l, .q} {addr}
1137 mod_mem(struct cmd_tbl *cmdtp, int incrflag, int flag, int argc,
1141 ulong i; /* 64-bit if SUPPORT_64BIT_DATA */
1146 return CMD_RET_USAGE;
1148 bootretry_reset_cmd_timeout(); /* got a good command to get here */
1149 /* We use the last specified parameters, unless new ones are
1152 addr = mm_last_addr;
1153 size = mm_last_size;
1155 if ((flag & CMD_FLAG_REPEAT) == 0) {
1156 /* New command specified. Check for a size specification.
1157 * Defaults to long if no or incorrect specification.
1159 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
1162 /* Address is specified since argc > 1
1164 addr = simple_strtoul(argv[1], NULL, 16);
1165 addr += base_address;
1168 /* Print the address, followed by value. Then accept input for
1169 * the next value. A non-converted value exits.
1172 ptr = map_sysmem(addr, size);
1173 printf("%08lx:", addr);
1175 printf(" %08x", *((u32 *)ptr));
1176 else if (SUPPORT_64BIT_DATA && size == 8)
1177 printf(" %0lx", *((ulong *)ptr));
1179 printf(" %04x", *((u16 *)ptr));
1181 printf(" %02x", *((u8 *)ptr));
1183 nbytes = cli_readline(" ? ");
1184 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1185 /* <CR> pressed as only input, don't modify current
1186 * location and move to next. "-" pressed will go back.
1189 addr += nbytes ? -size : size;
1191 /* good enough to not time out */
1192 bootretry_reset_cmd_timeout();
1194 #ifdef CONFIG_BOOT_RETRY_TIME
1195 else if (nbytes == -2) {
1196 break; /* timed out, exit the command */
1201 if (SUPPORT_64BIT_DATA)
1202 i = simple_strtoull(console_buffer, &endp, 16);
1204 i = simple_strtoul(console_buffer, &endp, 16);
1205 nbytes = endp - console_buffer;
1207 /* good enough to not time out
1209 bootretry_reset_cmd_timeout();
1212 else if (SUPPORT_64BIT_DATA && size == 8)
1213 *((ulong *)ptr) = i;
1226 mm_last_addr = addr;
1227 mm_last_size = size;
1231 #ifdef CONFIG_CMD_CRC32
1233 static int do_mem_crc(struct cmd_tbl *cmdtp, int flag, int argc,
1241 return CMD_RET_USAGE;
1245 #ifdef CONFIG_CRC32_VERIFY
1246 if (strcmp(*av, "-v") == 0) {
1247 flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV;
1253 return hash_command("crc32", flags, cmdtp, flag, ac, av);
1258 #ifdef CONFIG_CMD_RANDOM
1259 static int do_random(struct cmd_tbl *cmdtp, int flag, int argc,
1262 unsigned long addr, len;
1263 unsigned long seed; // NOT INITIALIZED ON PURPOSE
1264 unsigned int *buf, *start;
1265 unsigned char *buf8;
1268 if (argc < 3 || argc > 4)
1269 return CMD_RET_USAGE;
1271 len = simple_strtoul(argv[2], NULL, 16);
1272 addr = simple_strtoul(argv[1], NULL, 16);
1275 seed = simple_strtoul(argv[3], NULL, 16);
1277 printf("The seed cannot be 0. Using 0xDEADBEEF.\n");
1281 seed = get_timer(0) ^ rand();
1285 start = map_sysmem(addr, len);
1287 for (i = 0; i < (len / 4); i++)
1290 buf8 = (unsigned char *)buf;
1291 for (i = 0; i < (len % 4); i++)
1292 *buf8++ = rand() & 0xFF;
1294 unmap_sysmem(start);
1295 printf("%lu bytes filled with random data\n", len);
1297 return CMD_RET_SUCCESS;
1301 /**************************************************/
1303 md, 3, 1, do_mem_md,
1305 "[.b, .w, .l" HELP_Q "] address [# of objects]"
1310 mm, 2, 1, do_mem_mm,
1311 "memory modify (auto-incrementing address)",
1312 "[.b, .w, .l" HELP_Q "] address"
1317 nm, 2, 1, do_mem_nm,
1318 "memory modify (constant address)",
1319 "[.b, .w, .l" HELP_Q "] address"
1323 mw, 4, 1, do_mem_mw,
1324 "memory write (fill)",
1325 "[.b, .w, .l" HELP_Q "] address value [count]"
1329 cp, 4, 1, do_mem_cp,
1331 "[.b, .w, .l" HELP_Q "] source target count"
1335 cmp, 4, 1, do_mem_cmp,
1337 "[.b, .w, .l" HELP_Q "] addr1 addr2 count"
1340 #ifdef CONFIG_MEM_SEARCH
1341 /**************************************************/
1343 ms, 255, 1, do_mem_search,
1345 "[.b, .w, .l" HELP_Q ", .s] [-q | -<n>] address #-of-objects <value>..."
1346 " -q = quiet, -l<val> = match limit" :
1350 #ifdef CONFIG_CMD_CRC32
1352 #ifndef CONFIG_CRC32_VERIFY
1355 crc32, 4, 1, do_mem_crc,
1356 "checksum calculation",
1357 "address count [addr]\n - compute CRC32 checksum [save at addr]"
1360 #else /* CONFIG_CRC32_VERIFY */
1363 crc32, 5, 1, do_mem_crc,
1364 "checksum calculation",
1365 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
1366 "-v address count crc\n - verify crc of memory area"
1369 #endif /* CONFIG_CRC32_VERIFY */
1373 #ifdef CONFIG_CMD_MEMINFO
1374 static int do_mem_info(struct cmd_tbl *cmdtp, int flag, int argc,
1378 print_size(gd->ram_size, "\n");
1385 base, 2, 1, do_mem_base,
1386 "print or set address offset",
1387 "\n - print address offset for memory commands\n"
1388 "base off\n - set address offset for memory commands to 'off'"
1392 loop, 3, 1, do_mem_loop,
1393 "infinite loop on address range",
1394 "[.b, .w, .l" HELP_Q "] address number_of_objects"
1399 loopw, 4, 1, do_mem_loopw,
1400 "infinite write loop on address range",
1401 "[.b, .w, .l" HELP_Q "] address number_of_objects data_to_write"
1403 #endif /* CONFIG_LOOPW */
1405 #ifdef CONFIG_CMD_MEMTEST
1407 mtest, 5, 1, do_mem_mtest,
1408 "simple RAM read/write test",
1409 "[start [end [pattern [iterations]]]]"
1411 #endif /* CONFIG_CMD_MEMTEST */
1413 #ifdef CONFIG_CMD_MX_CYCLIC
1415 mdc, 4, 1, do_mem_mdc,
1416 "memory display cyclic",
1417 "[.b, .w, .l" HELP_Q "] address count delay(ms)"
1421 mwc, 4, 1, do_mem_mwc,
1422 "memory write cyclic",
1423 "[.b, .w, .l" HELP_Q "] address value delay(ms)"
1425 #endif /* CONFIG_CMD_MX_CYCLIC */
1427 #ifdef CONFIG_CMD_MEMINFO
1429 meminfo, 3, 1, do_mem_info,
1430 "display memory information",
1435 #ifdef CONFIG_CMD_RANDOM
1437 random, 4, 0, do_random,
1438 "fill memory with random pattern",
1439 "<addr> <len> [<seed>]\n"
1440 " - Fill 'len' bytes of memory starting at 'addr' with random data\n"