]> Git Repo - u-boot.git/blame - cmd/mem.c
x86: coreboot: Allow building an expo for editing CMOS config
[u-boot.git] / cmd / mem.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
3863585b
WD
2/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, [email protected].
3863585b
WD
5 */
6
7/*
8 * Memory Functions
9 *
10 * Copied from FADS ROM, Dan Malek ([email protected])
11 */
12
24b852a7 13#include <console.h>
0098e179 14#include <bootretry.h>
18d66533 15#include <cli.h>
3863585b 16#include <command.h>
24b852a7 17#include <console.h>
4e4bf944 18#include <display_options.h>
17ead040 19#ifdef CONFIG_MTD_NOR_FLASH
0ee48252 20#include <flash.h>
17ead040 21#endif
d20a40de 22#include <hash.h>
f7ae49fc 23#include <log.h>
0eb25b61 24#include <mapmem.h>
90526e9f 25#include <rand.h>
03de305e 26#include <time.h>
a6e6fc61 27#include <watchdog.h>
401d1c4f 28#include <asm/global_data.h>
0628ab8e 29#include <asm/io.h>
cd93d625 30#include <linux/bitops.h>
15a33e49 31#include <linux/compiler.h>
bdded201 32#include <linux/ctype.h>
c05ed00a 33#include <linux/delay.h>
15a33e49
SG
34
35DECLARE_GLOBAL_DATA_PTR;
3863585b 36
76be8f75 37/* Create a compile-time value */
4762c515 38#if MEM_SUPPORT_64BIT_DATA
76be8f75
SG
39#define HELP_Q ", .q"
40#else
41#define HELP_Q ""
42#endif
43
09140113 44static int mod_mem(struct cmd_tbl *, int, int, int, char * const []);
3863585b
WD
45
46/* Display values from last command.
47 * Memory modify remembered values are different from display memory.
48 */
581508bd
SW
49static ulong dp_last_addr, dp_last_size;
50static ulong dp_last_length = 0x40;
51static ulong mm_last_addr, mm_last_size;
3863585b
WD
52
53static ulong base_address = 0;
550a9e79
SG
54#ifdef CONFIG_CMD_MEM_SEARCH
55static ulong dp_last_ms_length;
bdded201
SG
56static u8 search_buf[64];
57static uint search_len;
58#endif
3863585b
WD
59
60/* Memory Display
61 *
62 * Syntax:
4d1fd7f1 63 * md{.b, .w, .l, .q} {addr} {len}
3863585b
WD
64 */
65#define DISP_LINE_LEN 16
09140113
SG
66static int do_mem_md(struct cmd_tbl *cmdtp, int flag, int argc,
67 char *const argv[])
3863585b 68{
c68c03f5
TT
69 ulong addr, length, bytes;
70 const void *buf;
27b207fd 71 int size;
3863585b
WD
72 int rc = 0;
73
74 /* We use the last specified parameters, unless new ones are
75 * entered.
76 */
77 addr = dp_last_addr;
78 size = dp_last_size;
79 length = dp_last_length;
80
47e26b1b 81 if (argc < 2)
4c12eeb8 82 return CMD_RET_USAGE;
3863585b
WD
83
84 if ((flag & CMD_FLAG_REPEAT) == 0) {
85 /* New command specified. Check for a size specification.
86 * Defaults to long if no or incorrect specification.
87 */
27b207fd
WD
88 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
89 return 1;
3863585b
WD
90
91 /* Address is specified since argc > 1
92 */
7e5f460e 93 addr = hextoul(argv[1], NULL);
3863585b
WD
94 addr += base_address;
95
96 /* If another parameter, it is the length to display.
97 * Length is the number of objects, not number of bytes.
98 */
99 if (argc > 2)
7e5f460e 100 length = hextoul(argv[2], NULL);
3863585b
WD
101 }
102
c68c03f5
TT
103 bytes = size * length;
104 buf = map_sysmem(addr, bytes);
0628ab8e 105
c68c03f5
TT
106 /* Print the lines. */
107 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
108 addr += bytes;
109 unmap_sysmem(buf);
3863585b
WD
110
111 dp_last_addr = addr;
112 dp_last_length = length;
113 dp_last_size = size;
114 return (rc);
115}
116
09140113
SG
117static int do_mem_mm(struct cmd_tbl *cmdtp, int flag, int argc,
118 char *const argv[])
3863585b
WD
119{
120 return mod_mem (cmdtp, 1, flag, argc, argv);
121}
09140113
SG
122
123static int do_mem_nm(struct cmd_tbl *cmdtp, int flag, int argc,
124 char *const argv[])
3863585b
WD
125{
126 return mod_mem (cmdtp, 0, flag, argc, argv);
127}
128
09140113
SG
129static int do_mem_mw(struct cmd_tbl *cmdtp, int flag, int argc,
130 char *const argv[])
3863585b 131{
4762c515 132 ulong writeval; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
4d1fd7f1 133 ulong addr, count;
27b207fd 134 int size;
cc5e196e 135 void *buf, *start;
0628ab8e 136 ulong bytes;
3863585b 137
47e26b1b 138 if ((argc < 3) || (argc > 4))
4c12eeb8 139 return CMD_RET_USAGE;
3863585b
WD
140
141 /* Check for size specification.
142 */
27b207fd
WD
143 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
144 return 1;
3863585b
WD
145
146 /* Address is specified since argc > 1
147 */
7e5f460e 148 addr = hextoul(argv[1], NULL);
3863585b
WD
149 addr += base_address;
150
151 /* Get the value to write.
152 */
4762c515 153 if (MEM_SUPPORT_64BIT_DATA)
4680976f
SG
154 writeval = simple_strtoull(argv[2], NULL, 16);
155 else
7e5f460e 156 writeval = hextoul(argv[2], NULL);
3863585b
WD
157
158 /* Count ? */
159 if (argc == 4) {
7e5f460e 160 count = hextoul(argv[3], NULL);
3863585b
WD
161 } else {
162 count = 1;
163 }
164
0628ab8e 165 bytes = size * count;
cc5e196e
SG
166 start = map_sysmem(addr, bytes);
167 buf = start;
3863585b
WD
168 while (count-- > 0) {
169 if (size == 4)
76698b4e 170 *((u32 *)buf) = (u32)writeval;
4762c515 171 else if (MEM_SUPPORT_64BIT_DATA && size == 8)
4680976f 172 *((ulong *)buf) = writeval;
3863585b 173 else if (size == 2)
76698b4e 174 *((u16 *)buf) = (u16)writeval;
3863585b 175 else
76698b4e 176 *((u8 *)buf) = (u8)writeval;
0628ab8e 177 buf += size;
3863585b 178 }
cc5e196e 179 unmap_sysmem(start);
3863585b
WD
180 return 0;
181}
182
72732318 183#ifdef CONFIG_CMD_MX_CYCLIC
09140113
SG
184static int do_mem_mdc(struct cmd_tbl *cmdtp, int flag, int argc,
185 char *const argv[])
4aaf29b2
SR
186{
187 int i;
188 ulong count;
189
47e26b1b 190 if (argc < 4)
4c12eeb8 191 return CMD_RET_USAGE;
4aaf29b2 192
0b1284eb 193 count = dectoul(argv[3], NULL);
4aaf29b2
SR
194
195 for (;;) {
196 do_mem_md (NULL, 0, 3, argv);
197
198 /* delay for <count> ms... */
199 for (i=0; i<count; i++)
07e11146 200 udelay(1000);
4aaf29b2
SR
201
202 /* check for ctrl-c to abort... */
203 if (ctrlc()) {
204 puts("Abort\n");
205 return 0;
206 }
207 }
208
209 return 0;
210}
211
09140113
SG
212static int do_mem_mwc(struct cmd_tbl *cmdtp, int flag, int argc,
213 char *const argv[])
4aaf29b2
SR
214{
215 int i;
216 ulong count;
217
47e26b1b 218 if (argc < 4)
4c12eeb8 219 return CMD_RET_USAGE;
4aaf29b2 220
0b1284eb 221 count = dectoul(argv[3], NULL);
4aaf29b2
SR
222
223 for (;;) {
224 do_mem_mw (NULL, 0, 3, argv);
225
226 /* delay for <count> ms... */
227 for (i=0; i<count; i++)
07e11146 228 udelay(1000);
4aaf29b2
SR
229
230 /* check for ctrl-c to abort... */
231 if (ctrlc()) {
232 puts("Abort\n");
233 return 0;
234 }
235 }
236
237 return 0;
238}
72732318 239#endif /* CONFIG_CMD_MX_CYCLIC */
4aaf29b2 240
09140113
SG
241static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc,
242 char *const argv[])
3863585b 243{
0628ab8e 244 ulong addr1, addr2, count, ngood, bytes;
27b207fd 245 int size;
3863585b 246 int rcode = 0;
054ea170 247 const char *type;
7086a894 248 const void *buf1, *buf2, *base, *ptr1, *ptr2;
4762c515 249 ulong word1, word2; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
3863585b 250
47e26b1b 251 if (argc != 4)
4c12eeb8 252 return CMD_RET_USAGE;
3863585b
WD
253
254 /* Check for size specification.
255 */
27b207fd
WD
256 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
257 return 1;
4d1fd7f1
YS
258 type = size == 8 ? "double word" :
259 size == 4 ? "word" :
260 size == 2 ? "halfword" : "byte";
3863585b 261
7e5f460e 262 addr1 = hextoul(argv[1], NULL);
3863585b
WD
263 addr1 += base_address;
264
7e5f460e 265 addr2 = hextoul(argv[2], NULL);
3863585b
WD
266 addr2 += base_address;
267
7e5f460e 268 count = hextoul(argv[3], NULL);
3863585b 269
0628ab8e
SG
270 bytes = size * count;
271 base = buf1 = map_sysmem(addr1, bytes);
272 buf2 = map_sysmem(addr2, bytes);
7086a894 273 for (ngood = 0, ptr1 = buf1, ptr2 = buf2; ngood < count; ++ngood) {
3863585b 274 if (size == 4) {
7086a894
SG
275 word1 = *(u32 *)ptr1;
276 word2 = *(u32 *)ptr2;
4762c515 277 } else if (MEM_SUPPORT_64BIT_DATA && size == 8) {
7086a894
SG
278 word1 = *(ulong *)ptr1;
279 word2 = *(ulong *)ptr2;
054ea170 280 } else if (size == 2) {
7086a894
SG
281 word1 = *(u16 *)ptr1;
282 word2 = *(u16 *)ptr2;
054ea170 283 } else {
7086a894
SG
284 word1 = *(u8 *)ptr1;
285 word2 = *(u8 *)ptr2;
3863585b 286 }
054ea170 287 if (word1 != word2) {
7086a894 288 ulong offset = ptr1 - base;
054ea170 289 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
0628ab8e
SG
290 type, (ulong)(addr1 + offset), size, word1,
291 type, (ulong)(addr2 + offset), size, word2);
054ea170
MF
292 rcode = 1;
293 break;
3863585b 294 }
054ea170 295
7086a894
SG
296 ptr1 += size;
297 ptr2 += size;
eaadb44e
SR
298
299 /* reset watchdog from time to time */
feb12a1f 300 if ((ngood % (64 << 10)) == 0)
29caf930 301 schedule();
3863585b 302 }
0628ab8e
SG
303 unmap_sysmem(buf1);
304 unmap_sysmem(buf2);
3863585b 305
054ea170 306 printf("Total of %ld %s(s) were the same\n", ngood, type);
3863585b
WD
307 return rcode;
308}
309
09140113
SG
310static int do_mem_cp(struct cmd_tbl *cmdtp, int flag, int argc,
311 char *const argv[])
3863585b 312{
c2538421 313 ulong addr, dest, count;
787f10a9 314 void *src, *dst;
27b207fd 315 int size;
3863585b 316
47e26b1b 317 if (argc != 4)
4c12eeb8 318 return CMD_RET_USAGE;
3863585b
WD
319
320 /* Check for size specification.
321 */
27b207fd
WD
322 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
323 return 1;
3863585b 324
7e5f460e 325 addr = hextoul(argv[1], NULL);
3863585b
WD
326 addr += base_address;
327
7e5f460e 328 dest = hextoul(argv[2], NULL);
3863585b
WD
329 dest += base_address;
330
7e5f460e 331 count = hextoul(argv[3], NULL);
3863585b
WD
332
333 if (count == 0) {
334 puts ("Zero length ???\n");
335 return 1;
336 }
337
787f10a9
PR
338 src = map_sysmem(addr, count * size);
339 dst = map_sysmem(dest, count * size);
340
e856bdcf 341#ifdef CONFIG_MTD_NOR_FLASH
3863585b 342 /* check if we are copying to Flash */
787f10a9 343 if (addr2info((ulong)dst)) {
3863585b
WD
344 int rc;
345
4b9206ed 346 puts ("Copy to Flash... ");
3863585b 347
787f10a9 348 rc = flash_write((char *)src, (ulong)dst, count * size);
3863585b 349 if (rc != 0) {
0ee48252 350 flash_perror(rc);
787f10a9
PR
351 unmap_sysmem(src);
352 unmap_sysmem(dst);
3863585b
WD
353 return (1);
354 }
355 puts ("done\n");
787f10a9
PR
356 unmap_sysmem(src);
357 unmap_sysmem(dst);
3863585b
WD
358 return 0;
359 }
360#endif
361
d631681e 362 memmove(dst, src, count * size);
a3a4749d 363
787f10a9
PR
364 unmap_sysmem(src);
365 unmap_sysmem(dst);
3863585b
WD
366 return 0;
367}
368
550a9e79 369#ifdef CONFIG_CMD_MEM_SEARCH
bdded201
SG
370static int do_mem_search(struct cmd_tbl *cmdtp, int flag, int argc,
371 char *const argv[])
372{
373 ulong addr, length, bytes, offset;
374 u8 *ptr, *end, *buf;
375 bool quiet = false;
376 ulong last_pos; /* Offset of last match in 'size' units*/
377 ulong last_addr; /* Address of last displayed line */
378 int limit = 10;
550a9e79 379 int used_len;
bdded201
SG
380 int count;
381 int size;
382 int i;
383
384 /* We use the last specified parameters, unless new ones are entered */
385 addr = dp_last_addr;
386 size = dp_last_size;
550a9e79 387 length = dp_last_ms_length;
bdded201
SG
388
389 if (argc < 3)
390 return CMD_RET_USAGE;
391
550a9e79 392 if (!(flag & CMD_FLAG_REPEAT)) {
bdded201
SG
393 /*
394 * Check for a size specification.
395 * Defaults to long if no or incorrect specification.
396 */
397 size = cmd_get_data_size(argv[0], 4);
7526deec 398 if (size < 0 && size != CMD_DATA_SIZE_STR)
bdded201
SG
399 return 1;
400
550a9e79
SG
401 argc--;
402 argv++;
bdded201
SG
403 while (argc && *argv[0] == '-') {
404 int ch = argv[0][1];
405
406 if (ch == 'q')
407 quiet = true;
408 else if (ch == 'l' && isxdigit(argv[0][2]))
7e5f460e 409 limit = hextoul(argv[0] + 2, NULL);
bdded201
SG
410 else
411 return CMD_RET_USAGE;
550a9e79
SG
412 argc--;
413 argv++;
bdded201
SG
414 }
415
416 /* Address is specified since argc > 1 */
7e5f460e 417 addr = hextoul(argv[0], NULL);
bdded201
SG
418 addr += base_address;
419
420 /* Length is the number of objects, not number of bytes */
7e5f460e 421 length = hextoul(argv[1], NULL);
bdded201
SG
422
423 /* Read the bytes to search for */
424 end = search_buf + sizeof(search_buf);
425 for (i = 2, ptr = search_buf; i < argc && ptr < end; i++) {
550a9e79 426 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
bdded201
SG
427 u64 val = simple_strtoull(argv[i], NULL, 16);
428
429 *(u64 *)ptr = val;
430 } else if (size == -2) { /* string */
431 int len = min(strlen(argv[i]),
432 (size_t)(end - ptr));
433
434 memcpy(ptr, argv[i], len);
435 ptr += len;
436 continue;
437 } else {
7e5f460e 438 u32 val = hextoul(argv[i], NULL);
bdded201
SG
439
440 switch (size) {
441 case 1:
442 *ptr = val;
443 break;
444 case 2:
445 *(u16 *)ptr = val;
446 break;
447 case 4:
448 *(u32 *)ptr = val;
449 break;
450 }
451 }
452 ptr += size;
453 }
454 search_len = ptr - search_buf;
455 }
456
457 /* Do the search */
458 if (size == -2)
459 size = 1;
460 bytes = size * length;
461 buf = map_sysmem(addr, bytes);
462 last_pos = 0;
463 last_addr = 0;
464 count = 0;
550a9e79
SG
465 for (offset = 0;
466 offset < bytes && offset <= bytes - search_len && count < limit;
bdded201
SG
467 offset += size) {
468 void *ptr = buf + offset;
469
470 if (!memcmp(ptr, search_buf, search_len)) {
471 uint align = (addr + offset) & 0xf;
472 ulong match = addr + offset;
473
474 if (!count || (last_addr & ~0xf) != (match & ~0xf)) {
475 if (!quiet) {
476 if (count)
477 printf("--\n");
478 print_buffer(match - align, ptr - align,
479 size,
480 ALIGN(search_len + align,
481 16) / size, 0);
482 }
483 last_addr = match;
484 last_pos = offset / size;
485 }
486 count++;
487 }
488 }
489 if (!quiet) {
490 printf("%d match%s", count, count == 1 ? "" : "es");
491 if (count == limit)
492 printf(" (repeat command to check for more)");
493 printf("\n");
494 }
495 env_set_hex("memmatches", count);
496 env_set_hex("memaddr", last_addr);
497 env_set_hex("mempos", last_pos);
498
499 unmap_sysmem(buf);
500
550a9e79
SG
501 used_len = offset / size;
502 dp_last_addr = addr + used_len;
bdded201 503 dp_last_size = size;
550a9e79 504 dp_last_ms_length = length < used_len ? 0 : length - used_len;
bdded201
SG
505
506 return count ? 0 : CMD_RET_FAILURE;
507}
508#endif
509
09140113
SG
510static int do_mem_base(struct cmd_tbl *cmdtp, int flag, int argc,
511 char *const argv[])
3863585b
WD
512{
513 if (argc > 1) {
514 /* Set new base address.
515 */
7e5f460e 516 base_address = hextoul(argv[1], NULL);
3863585b
WD
517 }
518 /* Print the current base address.
519 */
520 printf("Base Address: 0x%08lx\n", base_address);
521 return 0;
522}
523
09140113
SG
524static int do_mem_loop(struct cmd_tbl *cmdtp, int flag, int argc,
525 char *const argv[])
3863585b 526{
0628ab8e 527 ulong addr, length, i, bytes;
27b207fd 528 int size;
4762c515 529 volatile ulong *llp; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
76698b4e
YS
530 volatile u32 *longp;
531 volatile u16 *shortp;
532 volatile u8 *cp;
0628ab8e 533 const void *buf;
3863585b 534
47e26b1b 535 if (argc < 3)
4c12eeb8 536 return CMD_RET_USAGE;
3863585b 537
85de63e2
RD
538 /*
539 * Check for a size specification.
3863585b
WD
540 * Defaults to long if no or incorrect specification.
541 */
27b207fd
WD
542 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
543 return 1;
3863585b
WD
544
545 /* Address is always specified.
546 */
7e5f460e 547 addr = hextoul(argv[1], NULL);
3863585b
WD
548
549 /* Length is the number of objects, not number of bytes.
550 */
7e5f460e 551 length = hextoul(argv[2], NULL);
3863585b 552
0628ab8e
SG
553 bytes = size * length;
554 buf = map_sysmem(addr, bytes);
555
3863585b
WD
556 /* We want to optimize the loops to run as fast as possible.
557 * If we have only one object, just run infinite loops.
558 */
559 if (length == 1) {
4762c515 560 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
4680976f 561 llp = (ulong *)buf;
4d1fd7f1
YS
562 for (;;)
563 i = *llp;
564 }
3863585b 565 if (size == 4) {
76698b4e 566 longp = (u32 *)buf;
3863585b
WD
567 for (;;)
568 i = *longp;
569 }
570 if (size == 2) {
76698b4e 571 shortp = (u16 *)buf;
3863585b
WD
572 for (;;)
573 i = *shortp;
574 }
76698b4e 575 cp = (u8 *)buf;
3863585b
WD
576 for (;;)
577 i = *cp;
578 }
579
4762c515 580 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
4d1fd7f1 581 for (;;) {
4680976f 582 llp = (ulong *)buf;
4d1fd7f1
YS
583 i = length;
584 while (i-- > 0)
585 *llp++;
586 }
587 }
3863585b
WD
588 if (size == 4) {
589 for (;;) {
76698b4e 590 longp = (u32 *)buf;
3863585b
WD
591 i = length;
592 while (i-- > 0)
f3b3c3df 593 *longp++;
3863585b
WD
594 }
595 }
596 if (size == 2) {
597 for (;;) {
76698b4e 598 shortp = (u16 *)buf;
3863585b
WD
599 i = length;
600 while (i-- > 0)
f3b3c3df 601 *shortp++;
3863585b
WD
602 }
603 }
604 for (;;) {
76698b4e 605 cp = (u8 *)buf;
3863585b
WD
606 i = length;
607 while (i-- > 0)
f3b3c3df 608 *cp++;
3863585b 609 }
0628ab8e 610 unmap_sysmem(buf);
92765f42
SG
611
612 return 0;
3863585b
WD
613}
614
56523f12 615#ifdef CONFIG_LOOPW
09140113
SG
616static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc,
617 char *const argv[])
56523f12 618{
4d1fd7f1 619 ulong addr, length, i, bytes;
56523f12 620 int size;
4762c515
RV
621 volatile ulong *llp; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
622 ulong data; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
76698b4e
YS
623 volatile u32 *longp;
624 volatile u16 *shortp;
625 volatile u8 *cp;
0628ab8e 626 void *buf;
81050926 627
47e26b1b 628 if (argc < 4)
4c12eeb8 629 return CMD_RET_USAGE;
56523f12 630
85de63e2
RD
631 /*
632 * Check for a size specification.
56523f12
WD
633 * Defaults to long if no or incorrect specification.
634 */
635 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
636 return 1;
637
638 /* Address is always specified.
639 */
7e5f460e 640 addr = hextoul(argv[1], NULL);
56523f12
WD
641
642 /* Length is the number of objects, not number of bytes.
643 */
7e5f460e 644 length = hextoul(argv[2], NULL);
56523f12
WD
645
646 /* data to write */
4762c515 647 if (MEM_SUPPORT_64BIT_DATA)
4680976f
SG
648 data = simple_strtoull(argv[3], NULL, 16);
649 else
7e5f460e 650 data = hextoul(argv[3], NULL);
81050926 651
0628ab8e
SG
652 bytes = size * length;
653 buf = map_sysmem(addr, bytes);
654
56523f12
WD
655 /* We want to optimize the loops to run as fast as possible.
656 * If we have only one object, just run infinite loops.
657 */
658 if (length == 1) {
4762c515 659 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
4680976f 660 llp = (ulong *)buf;
4d1fd7f1
YS
661 for (;;)
662 *llp = data;
663 }
56523f12 664 if (size == 4) {
76698b4e 665 longp = (u32 *)buf;
56523f12
WD
666 for (;;)
667 *longp = data;
4d1fd7f1 668 }
56523f12 669 if (size == 2) {
76698b4e 670 shortp = (u16 *)buf;
56523f12
WD
671 for (;;)
672 *shortp = data;
673 }
76698b4e 674 cp = (u8 *)buf;
56523f12
WD
675 for (;;)
676 *cp = data;
677 }
678
4762c515 679 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
4d1fd7f1 680 for (;;) {
4680976f 681 llp = (ulong *)buf;
4d1fd7f1
YS
682 i = length;
683 while (i-- > 0)
684 *llp++ = data;
685 }
686 }
56523f12
WD
687 if (size == 4) {
688 for (;;) {
76698b4e 689 longp = (u32 *)buf;
56523f12
WD
690 i = length;
691 while (i-- > 0)
692 *longp++ = data;
693 }
694 }
695 if (size == 2) {
696 for (;;) {
76698b4e 697 shortp = (u16 *)buf;
56523f12
WD
698 i = length;
699 while (i-- > 0)
700 *shortp++ = data;
701 }
702 }
703 for (;;) {
76698b4e 704 cp = (u8 *)buf;
56523f12
WD
705 i = length;
706 while (i-- > 0)
707 *cp++ = data;
708 }
709}
710#endif /* CONFIG_LOOPW */
711
68149e94 712#ifdef CONFIG_CMD_MEMTEST
5512d5b0
SG
713static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr,
714 vu_long *dummy)
3863585b 715{
c9638f50 716 vu_long *addr;
c9638f50
SG
717 ulong errs = 0;
718 ulong val, readback;
719 int j;
c9638f50
SG
720 vu_long offset;
721 vu_long test_offset;
722 vu_long pattern;
723 vu_long temp;
724 vu_long anti_pattern;
725 vu_long num_words;
3863585b
WD
726 static const ulong bitpattern[] = {
727 0x00000001, /* single bit */
728 0x00000003, /* two adjacent bits */
729 0x00000007, /* three adjacent bits */
730 0x0000000F, /* four adjacent bits */
731 0x00000005, /* two non-adjacent bits */
732 0x00000015, /* three non-adjacent bits */
733 0x00000055, /* four non-adjacent bits */
734 0xaaaaaaaa, /* alternating 1/0 */
735 };
3863585b 736
5512d5b0 737 num_words = (end_addr - start_addr) / sizeof(vu_long);
8c86bbe0 738
7ecbd4d7
SG
739 /*
740 * Data line test: write a pattern to the first
741 * location, write the 1's complement to a 'parking'
742 * address (changes the state of the data bus so a
743 * floating bus doesn't give a false OK), and then
744 * read the value back. Note that we read it back
745 * into a variable because the next time we read it,
746 * it might be right (been there, tough to explain to
747 * the quality guys why it prints a failure when the
748 * "is" and "should be" are obviously the same in the
749 * error message).
750 *
751 * Rather than exhaustively testing, we test some
752 * patterns by shifting '1' bits through a field of
753 * '0's and '0' bits through a field of '1's (i.e.
754 * pattern and ~pattern).
755 */
5512d5b0 756 addr = buf;
7ecbd4d7
SG
757 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
758 val = bitpattern[j];
759 for (; val != 0; val <<= 1) {
5512d5b0 760 *addr = val;
c9638f50 761 *dummy = ~val; /* clear the test data off the bus */
3863585b 762 readback = *addr;
7ecbd4d7 763 if (readback != val) {
c9638f50
SG
764 printf("FAILURE (data line): "
765 "expected %08lx, actual %08lx\n",
766 val, readback);
767 errs++;
c44d4386 768 if (ctrlc())
51209b1f 769 return -1;
3863585b
WD
770 }
771 *addr = ~val;
772 *dummy = val;
773 readback = *addr;
c9638f50
SG
774 if (readback != ~val) {
775 printf("FAILURE (data line): "
776 "Is %08lx, should be %08lx\n",
777 readback, ~val);
778 errs++;
c44d4386 779 if (ctrlc())
51209b1f 780 return -1;
3863585b 781 }
3863585b 782 }
7ecbd4d7 783 }
3863585b 784
7ecbd4d7
SG
785 /*
786 * Based on code whose Original Author and Copyright
787 * information follows: Copyright (c) 1998 by Michael
788 * Barr. This software is placed into the public
789 * domain and may be used for any purpose. However,
790 * this notice must not be changed or removed and no
791 * warranty is either expressed or implied by its
792 * publication or distribution.
793 */
3863585b 794
7ecbd4d7
SG
795 /*
796 * Address line test
797
798 * Description: Test the address bus wiring in a
799 * memory region by performing a walking
800 * 1's test on the relevant bits of the
801 * address and checking for aliasing.
802 * This test will find single-bit
803 * address failures such as stuck-high,
804 * stuck-low, and shorted pins. The base
805 * address and size of the region are
806 * selected by the caller.
807
808 * Notes: For best results, the selected base
809 * address should have enough LSB 0's to
810 * guarantee single address bit changes.
811 * For example, to test a 64-Kbyte
812 * region, select a base address on a
813 * 64-Kbyte boundary. Also, select the
814 * region size as a power-of-two if at
815 * all possible.
816 *
817 * Returns: 0 if the test succeeds, 1 if the test fails.
818 */
5c3ea29e
HS
819 pattern = (vu_long)0xaaaaaaaaaaaaaaaa;
820 anti_pattern = (vu_long)0x5555555555555555;
3863585b 821
5512d5b0 822 debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words);
7ecbd4d7
SG
823 /*
824 * Write the default pattern at each of the
825 * power-of-two offsets.
826 */
5512d5b0
SG
827 for (offset = 1; offset < num_words; offset <<= 1)
828 addr[offset] = pattern;
3863585b 829
7ecbd4d7
SG
830 /*
831 * Check for address bits stuck high.
832 */
833 test_offset = 0;
5512d5b0 834 addr[test_offset] = anti_pattern;
3863585b 835
5512d5b0
SG
836 for (offset = 1; offset < num_words; offset <<= 1) {
837 temp = addr[offset];
7ecbd4d7 838 if (temp != pattern) {
c9638f50 839 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
3863585b 840 " expected 0x%.8lx, actual 0x%.8lx\n",
102c051f
DF
841 start_addr + offset*sizeof(vu_long),
842 pattern, temp);
87b22b77 843 errs++;
c44d4386 844 if (ctrlc())
7ecbd4d7 845 return -1;
3863585b 846 }
7ecbd4d7 847 }
5512d5b0 848 addr[test_offset] = pattern;
29caf930 849 schedule();
3863585b 850
7ecbd4d7
SG
851 /*
852 * Check for addr bits stuck low or shorted.
853 */
5512d5b0
SG
854 for (test_offset = 1; test_offset < num_words; test_offset <<= 1) {
855 addr[test_offset] = anti_pattern;
3863585b 856
5512d5b0
SG
857 for (offset = 1; offset < num_words; offset <<= 1) {
858 temp = addr[offset];
3863585b 859 if ((temp != pattern) && (offset != test_offset)) {
7ecbd4d7
SG
860 printf("\nFAILURE: Address bit stuck low or"
861 " shorted @ 0x%.8lx: expected 0x%.8lx,"
862 " actual 0x%.8lx\n",
102c051f
DF
863 start_addr + offset*sizeof(vu_long),
864 pattern, temp);
7ecbd4d7 865 errs++;
c44d4386 866 if (ctrlc())
7ecbd4d7 867 return -1;
3863585b 868 }
3863585b 869 }
5512d5b0 870 addr[test_offset] = pattern;
7ecbd4d7 871 }
3863585b 872
7ecbd4d7
SG
873 /*
874 * Description: Test the integrity of a physical
875 * memory device by performing an
876 * increment/decrement test over the
877 * entire region. In the process every
878 * storage bit in the device is tested
879 * as a zero and a one. The base address
880 * and the size of the region are
881 * selected by the caller.
882 *
883 * Returns: 0 if the test succeeds, 1 if the test fails.
884 */
5512d5b0 885 num_words++;
3863585b 886
7ecbd4d7
SG
887 /*
888 * Fill memory with a known pattern.
889 */
890 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
29caf930 891 schedule();
5512d5b0 892 addr[offset] = pattern;
7ecbd4d7 893 }
3863585b 894
7ecbd4d7
SG
895 /*
896 * Check each location and invert it for the second pass.
897 */
898 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
29caf930 899 schedule();
5512d5b0 900 temp = addr[offset];
7ecbd4d7 901 if (temp != pattern) {
c9638f50 902 printf("\nFAILURE (read/write) @ 0x%.8lx:"
3863585b 903 " expected 0x%.8lx, actual 0x%.8lx)\n",
102c051f
DF
904 start_addr + offset*sizeof(vu_long),
905 pattern, temp);
87b22b77 906 errs++;
c44d4386 907 if (ctrlc())
51209b1f 908 return -1;
3863585b
WD
909 }
910
7ecbd4d7 911 anti_pattern = ~pattern;
5512d5b0 912 addr[offset] = anti_pattern;
7ecbd4d7
SG
913 }
914
915 /*
916 * Check each location for the inverted pattern and zero it.
917 */
918 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
29caf930 919 schedule();
7ecbd4d7 920 anti_pattern = ~pattern;
5512d5b0 921 temp = addr[offset];
7ecbd4d7 922 if (temp != anti_pattern) {
c9638f50 923 printf("\nFAILURE (read/write): @ 0x%.8lx:"
3863585b 924 " expected 0x%.8lx, actual 0x%.8lx)\n",
102c051f
DF
925 start_addr + offset*sizeof(vu_long),
926 anti_pattern, temp);
87b22b77 927 errs++;
c44d4386 928 if (ctrlc())
51209b1f 929 return -1;
3863585b 930 }
5512d5b0 931 addr[offset] = 0;
7ecbd4d7 932 }
51209b1f 933
fea6730e 934 return errs;
c9638f50
SG
935}
936
8e434cb7
SR
937static int compare_regions(volatile unsigned long *bufa,
938 volatile unsigned long *bufb, size_t count)
939{
940 volatile unsigned long *p1 = bufa;
941 volatile unsigned long *p2 = bufb;
942 int errs = 0;
943 size_t i;
944
945 for (i = 0; i < count; i++, p1++, p2++) {
946 if (*p1 != *p2) {
947 printf("FAILURE: 0x%08lx != 0x%08lx (delta=0x%08lx -> bit %ld) at offset 0x%08lx\n",
948 (unsigned long)*p1, (unsigned long)*p2,
949 *p1 ^ *p2, __ffs(*p1 ^ *p2),
950 (unsigned long)(i * sizeof(unsigned long)));
951 errs++;
952 }
953 }
954
955 return errs;
956}
957
958static ulong test_bitflip_comparison(volatile unsigned long *bufa,
959 volatile unsigned long *bufb, size_t count)
960{
961 volatile unsigned long *p1 = bufa;
962 volatile unsigned long *p2 = bufb;
963 unsigned int j, k;
964 unsigned long q;
965 size_t i;
966 int max;
967 int errs = 0;
968
969 max = sizeof(unsigned long) * 8;
970 for (k = 0; k < max; k++) {
5c3ea29e 971 q = 1UL << k;
8e434cb7 972 for (j = 0; j < 8; j++) {
29caf930 973 schedule();
8e434cb7
SR
974 q = ~q;
975 p1 = (volatile unsigned long *)bufa;
976 p2 = (volatile unsigned long *)bufb;
977 for (i = 0; i < count; i++)
978 *p1++ = *p2++ = (i % 2) == 0 ? q : ~q;
979
980 errs += compare_regions(bufa, bufb, count);
981 }
982
983 if (ctrlc())
984 return -1UL;
985 }
986
987 return errs;
988}
989
9989fb18
RS
990static ulong mem_test_bitflip(vu_long *buf, ulong start, ulong end)
991{
992 /*
993 * Split the specified range into two halves.
994 * Note that mtest range is inclusive of start,end.
995 * Bitflip test instead uses a count (of 32-bit words).
996 */
997 ulong half_size = (end - start + 1) / 2 / sizeof(unsigned long);
998
999 return test_bitflip_comparison(buf, buf + half_size, half_size);
1000}
1001
5512d5b0
SG
1002static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr,
1003 vu_long pattern, int iteration)
c9638f50 1004{
5512d5b0 1005 vu_long *end;
c9638f50 1006 vu_long *addr;
c9638f50 1007 ulong errs = 0;
5512d5b0 1008 ulong incr, length;
c9638f50 1009 ulong val, readback;
5c3ea29e 1010 const int plen = 2 * sizeof(ulong);
3863585b 1011
51209b1f 1012 /* Alternate the pattern */
3863585b 1013 incr = 1;
51209b1f
SG
1014 if (iteration & 1) {
1015 incr = -incr;
1016 /*
1017 * Flip the pattern each time to make lots of zeros and
1018 * then, the next time, lots of ones. We decrement
1019 * the "negative" patterns and increment the "positive"
1020 * patterns to preserve this feature.
1021 */
5c3ea29e 1022 if (pattern > (ulong)LONG_MAX)
51209b1f
SG
1023 pattern = -pattern; /* complement & increment */
1024 else
1025 pattern = ~pattern;
1026 }
5512d5b0
SG
1027 length = (end_addr - start_addr) / sizeof(ulong);
1028 end = buf + length;
5c3ea29e 1029 printf("\rPattern %0*lX Writing..."
7ecbd4d7
SG
1030 "%12s"
1031 "\b\b\b\b\b\b\b\b\b\b",
5c3ea29e 1032 plen, pattern, "");
3863585b 1033
5512d5b0 1034 for (addr = buf, val = pattern; addr < end; addr++) {
29caf930 1035 schedule();
7ecbd4d7
SG
1036 *addr = val;
1037 val += incr;
1038 }
3863585b 1039
7ecbd4d7 1040 puts("Reading...");
3863585b 1041
5512d5b0 1042 for (addr = buf, val = pattern; addr < end; addr++) {
29caf930 1043 schedule();
7ecbd4d7
SG
1044 readback = *addr;
1045 if (readback != val) {
5512d5b0
SG
1046 ulong offset = addr - buf;
1047
5c3ea29e
HS
1048 printf("\nMem error @ 0x%0*lX: found %0*lX, expected %0*lX\n",
1049 plen, start_addr + offset * sizeof(vu_long),
1050 plen, readback, plen, val);
7ecbd4d7 1051 errs++;
c44d4386 1052 if (ctrlc())
7ecbd4d7 1053 return -1;
3863585b 1054 }
7ecbd4d7
SG
1055 val += incr;
1056 }
3863585b 1057
fea6730e 1058 return errs;
c9638f50
SG
1059}
1060
1061/*
1062 * Perform a memory test. A more complete alternative test can be
1063 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
1064 * interrupted by ctrl-c or by a failure of one of the sub-tests.
1065 */
09140113
SG
1066static int do_mem_mtest(struct cmd_tbl *cmdtp, int flag, int argc,
1067 char *const argv[])
c9638f50 1068{
8c86bbe0 1069 ulong start, end;
e519f03a
MS
1070 vu_long scratch_space;
1071 vu_long *buf, *dummy = &scratch_space;
adcc5705 1072 ulong iteration_limit = 0;
a8c708ea 1073 ulong count = 0;
51209b1f 1074 ulong errs = 0; /* number of errors, or -1 if interrupted */
e37f1eb4 1075 ulong pattern = 0;
51209b1f 1076 int iteration;
c9638f50 1077
e37f1eb4
PM
1078 start = CONFIG_SYS_MEMTEST_START;
1079 end = CONFIG_SYS_MEMTEST_END;
1080
c9638f50 1081 if (argc > 1)
e37f1eb4
PM
1082 if (strict_strtoul(argv[1], 16, &start) < 0)
1083 return CMD_RET_USAGE;
c9638f50
SG
1084
1085 if (argc > 2)
e37f1eb4
PM
1086 if (strict_strtoul(argv[2], 16, &end) < 0)
1087 return CMD_RET_USAGE;
c9638f50
SG
1088
1089 if (argc > 3)
e37f1eb4
PM
1090 if (strict_strtoul(argv[3], 16, &pattern) < 0)
1091 return CMD_RET_USAGE;
c9638f50
SG
1092
1093 if (argc > 4)
e37f1eb4
PM
1094 if (strict_strtoul(argv[4], 16, &iteration_limit) < 0)
1095 return CMD_RET_USAGE;
1096
1097 if (end < start) {
1098 printf("Refusing to do empty test\n");
1099 return -1;
1100 }
c9638f50 1101
dfe461d6 1102 printf("Testing %08lx ... %08lx:\n", start, end);
8c86bbe0
SG
1103 debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
1104 start, end);
51209b1f 1105
5512d5b0 1106 buf = map_sysmem(start, end - start);
51209b1f
SG
1107 for (iteration = 0;
1108 !iteration_limit || iteration < iteration_limit;
1109 iteration++) {
1110 if (ctrlc()) {
51209b1f
SG
1111 errs = -1UL;
1112 break;
1113 }
1114
1115 printf("Iteration: %6d\r", iteration + 1);
1116 debug("\n");
f14bfa7e 1117 if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST)) {
5512d5b0 1118 errs = mem_test_alt(buf, start, end, dummy);
8e434cb7
SR
1119 if (errs == -1UL)
1120 break;
9989fb18
RS
1121 if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST_BITFLIP)) {
1122 count += errs;
1123 errs = mem_test_bitflip(buf, start, end);
1124 }
5512d5b0
SG
1125 } else {
1126 errs = mem_test_quick(buf, start, end, pattern,
1127 iteration);
1128 }
1129 if (errs == -1UL)
1130 break;
a8c708ea 1131 count += errs;
5512d5b0
SG
1132 }
1133
54de244c 1134 unmap_sysmem((void *)buf);
51209b1f 1135
5c3ea29e 1136 printf("\nTested %d iteration(s) with %lu errors.\n", iteration, count);
c9638f50 1137
a8c708ea 1138 return errs != 0;
3863585b 1139}
a2681707 1140#endif /* CONFIG_CMD_MEMTEST */
3863585b
WD
1141
1142/* Modify memory.
1143 *
1144 * Syntax:
4d1fd7f1 1145 * mm{.b, .w, .l, .q} {addr}
3863585b
WD
1146 */
1147static int
09140113
SG
1148mod_mem(struct cmd_tbl *cmdtp, int incrflag, int flag, int argc,
1149 char *const argv[])
3863585b 1150{
4d1fd7f1 1151 ulong addr;
4762c515 1152 ulong i; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
27b207fd 1153 int nbytes, size;
0628ab8e 1154 void *ptr = NULL;
3863585b 1155
47e26b1b 1156 if (argc != 2)
4c12eeb8 1157 return CMD_RET_USAGE;
3863585b 1158
b26440f1 1159 bootretry_reset_cmd_timeout(); /* got a good command to get here */
3863585b
WD
1160 /* We use the last specified parameters, unless new ones are
1161 * entered.
1162 */
1163 addr = mm_last_addr;
1164 size = mm_last_size;
1165
1166 if ((flag & CMD_FLAG_REPEAT) == 0) {
1167 /* New command specified. Check for a size specification.
1168 * Defaults to long if no or incorrect specification.
1169 */
27b207fd
WD
1170 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
1171 return 1;
3863585b
WD
1172
1173 /* Address is specified since argc > 1
1174 */
7e5f460e 1175 addr = hextoul(argv[1], NULL);
3863585b
WD
1176 addr += base_address;
1177 }
1178
1179 /* Print the address, followed by value. Then accept input for
1180 * the next value. A non-converted value exits.
1181 */
1182 do {
0628ab8e 1183 ptr = map_sysmem(addr, size);
3863585b
WD
1184 printf("%08lx:", addr);
1185 if (size == 4)
76698b4e 1186 printf(" %08x", *((u32 *)ptr));
4762c515 1187 else if (MEM_SUPPORT_64BIT_DATA && size == 8)
4680976f 1188 printf(" %0lx", *((ulong *)ptr));
3863585b 1189 else if (size == 2)
76698b4e 1190 printf(" %04x", *((u16 *)ptr));
3863585b 1191 else
76698b4e 1192 printf(" %02x", *((u8 *)ptr));
3863585b 1193
e1bf824d 1194 nbytes = cli_readline(" ? ");
3863585b
WD
1195 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1196 /* <CR> pressed as only input, don't modify current
1197 * location and move to next. "-" pressed will go back.
1198 */
1199 if (incrflag)
1200 addr += nbytes ? -size : size;
1201 nbytes = 1;
b26440f1
SG
1202 /* good enough to not time out */
1203 bootretry_reset_cmd_timeout();
3863585b
WD
1204 }
1205#ifdef CONFIG_BOOT_RETRY_TIME
1206 else if (nbytes == -2) {
1207 break; /* timed out, exit the command */
1208 }
1209#endif
1210 else {
1211 char *endp;
4762c515 1212 if (MEM_SUPPORT_64BIT_DATA)
4680976f
SG
1213 i = simple_strtoull(console_buffer, &endp, 16);
1214 else
7e5f460e 1215 i = hextoul(console_buffer, &endp);
3863585b
WD
1216 nbytes = endp - console_buffer;
1217 if (nbytes) {
3863585b
WD
1218 /* good enough to not time out
1219 */
b26440f1 1220 bootretry_reset_cmd_timeout();
3863585b 1221 if (size == 4)
76698b4e 1222 *((u32 *)ptr) = i;
4762c515 1223 else if (MEM_SUPPORT_64BIT_DATA && size == 8)
4680976f 1224 *((ulong *)ptr) = i;
3863585b 1225 else if (size == 2)
76698b4e 1226 *((u16 *)ptr) = i;
3863585b 1227 else
76698b4e 1228 *((u8 *)ptr) = i;
3863585b
WD
1229 if (incrflag)
1230 addr += size;
1231 }
1232 }
1233 } while (nbytes);
0628ab8e
SG
1234 if (ptr)
1235 unmap_sysmem(ptr);
3863585b
WD
1236
1237 mm_last_addr = addr;
1238 mm_last_size = size;
1239 return 0;
1240}
1241
710b9938
MF
1242#ifdef CONFIG_CMD_CRC32
1243
09140113
SG
1244static int do_mem_crc(struct cmd_tbl *cmdtp, int flag, int argc,
1245 char *const argv[])
3863585b 1246{
d20a40de 1247 int flags = 0;
c26e454d 1248 int ac;
54841ab5 1249 char * const *av;
c26e454d 1250
d20a40de 1251 if (argc < 3)
4c12eeb8 1252 return CMD_RET_USAGE;
c26e454d
WD
1253
1254 av = argv + 1;
1255 ac = argc - 1;
221a949e 1256#ifdef CONFIG_CRC32_VERIFY
c26e454d 1257 if (strcmp(*av, "-v") == 0) {
a69bdba9 1258 flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV;
c26e454d
WD
1259 av++;
1260 ac--;
c26e454d 1261 }
d20a40de 1262#endif
c26e454d 1263
d20a40de 1264 return hash_command("crc32", flags, cmdtp, flag, ac, av);
c26e454d 1265}
c26e454d 1266
710b9938
MF
1267#endif
1268
803e1a3d 1269#ifdef CONFIG_CMD_RANDOM
09140113
SG
1270static int do_random(struct cmd_tbl *cmdtp, int flag, int argc,
1271 char *const argv[])
803e1a3d
JJH
1272{
1273 unsigned long addr, len;
1274 unsigned long seed; // NOT INITIALIZED ON PURPOSE
1275 unsigned int *buf, *start;
1276 unsigned char *buf8;
1277 unsigned int i;
1278
5f2c4e01
EP
1279 if (argc < 3 || argc > 4)
1280 return CMD_RET_USAGE;
803e1a3d 1281
7e5f460e
SG
1282 len = hextoul(argv[2], NULL);
1283 addr = hextoul(argv[1], NULL);
803e1a3d
JJH
1284
1285 if (argc == 4) {
7e5f460e 1286 seed = hextoul(argv[3], NULL);
803e1a3d
JJH
1287 if (seed == 0) {
1288 printf("The seed cannot be 0. Using 0xDEADBEEF.\n");
1289 seed = 0xDEADBEEF;
1290 }
1291 } else {
1292 seed = get_timer(0) ^ rand();
1293 }
1294
1295 srand(seed);
1296 start = map_sysmem(addr, len);
1297 buf = start;
1298 for (i = 0; i < (len / 4); i++)
1299 *buf++ = rand();
1300
1301 buf8 = (unsigned char *)buf;
1302 for (i = 0; i < (len % 4); i++)
1303 *buf8++ = rand() & 0xFF;
1304
1305 unmap_sysmem(start);
1306 printf("%lu bytes filled with random data\n", len);
5f2c4e01
EP
1307
1308 return CMD_RET_SUCCESS;
803e1a3d
JJH
1309}
1310#endif
1311
8bde7f77 1312/**************************************************/
0d498393 1313U_BOOT_CMD(
53677ef1 1314 md, 3, 1, do_mem_md,
2fb2604d 1315 "memory display",
76be8f75 1316 "[.b, .w, .l" HELP_Q "] address [# of objects]"
8bde7f77
WD
1317);
1318
0d498393 1319U_BOOT_CMD(
53677ef1 1320 mm, 2, 1, do_mem_mm,
a89c33db 1321 "memory modify (auto-incrementing address)",
76be8f75 1322 "[.b, .w, .l" HELP_Q "] address"
8bde7f77
WD
1323);
1324
0d498393 1325U_BOOT_CMD(
53677ef1 1326 nm, 2, 1, do_mem_nm,
2fb2604d 1327 "memory modify (constant address)",
76be8f75 1328 "[.b, .w, .l" HELP_Q "] address"
8bde7f77
WD
1329);
1330
0d498393 1331U_BOOT_CMD(
53677ef1 1332 mw, 4, 1, do_mem_mw,
2fb2604d 1333 "memory write (fill)",
76be8f75 1334 "[.b, .w, .l" HELP_Q "] address value [count]"
8bde7f77
WD
1335);
1336
0d498393 1337U_BOOT_CMD(
53677ef1 1338 cp, 4, 1, do_mem_cp,
2fb2604d 1339 "memory copy",
76be8f75 1340 "[.b, .w, .l" HELP_Q "] source target count"
8bde7f77
WD
1341);
1342
0d498393 1343U_BOOT_CMD(
53677ef1 1344 cmp, 4, 1, do_mem_cmp,
2fb2604d 1345 "memory compare",
76be8f75 1346 "[.b, .w, .l" HELP_Q "] addr1 addr2 count"
8bde7f77
WD
1347);
1348
550a9e79 1349#ifdef CONFIG_CMD_MEM_SEARCH
bdded201
SG
1350/**************************************************/
1351U_BOOT_CMD(
1352 ms, 255, 1, do_mem_search,
1353 "memory search",
1354 "[.b, .w, .l" HELP_Q ", .s] [-q | -<n>] address #-of-objects <value>..."
550a9e79 1355 " -q = quiet, -l<val> = match limit"
bdded201
SG
1356);
1357#endif
1358
710b9938
MF
1359#ifdef CONFIG_CMD_CRC32
1360
221a949e 1361#ifndef CONFIG_CRC32_VERIFY
c26e454d 1362
0d498393 1363U_BOOT_CMD(
53677ef1 1364 crc32, 4, 1, do_mem_crc,
2fb2604d 1365 "checksum calculation",
a89c33db 1366 "address count [addr]\n - compute CRC32 checksum [save at addr]"
8bde7f77
WD
1367);
1368
221a949e 1369#else /* CONFIG_CRC32_VERIFY */
c26e454d
WD
1370
1371U_BOOT_CMD(
53677ef1 1372 crc32, 5, 1, do_mem_crc,
2fb2604d 1373 "checksum calculation",
c26e454d 1374 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
a89c33db 1375 "-v address count crc\n - verify crc of memory area"
c26e454d
WD
1376);
1377
221a949e 1378#endif /* CONFIG_CRC32_VERIFY */
c26e454d 1379
710b9938
MF
1380#endif
1381
0d498393 1382U_BOOT_CMD(
53677ef1 1383 base, 2, 1, do_mem_base,
2fb2604d 1384 "print or set address offset",
8bde7f77 1385 "\n - print address offset for memory commands\n"
a89c33db 1386 "base off\n - set address offset for memory commands to 'off'"
8bde7f77
WD
1387);
1388
0d498393 1389U_BOOT_CMD(
53677ef1 1390 loop, 3, 1, do_mem_loop,
2fb2604d 1391 "infinite loop on address range",
76be8f75 1392 "[.b, .w, .l" HELP_Q "] address number_of_objects"
8bde7f77
WD
1393);
1394
56523f12
WD
1395#ifdef CONFIG_LOOPW
1396U_BOOT_CMD(
53677ef1 1397 loopw, 4, 1, do_mem_loopw,
2fb2604d 1398 "infinite write loop on address range",
76be8f75 1399 "[.b, .w, .l" HELP_Q "] address number_of_objects data_to_write"
56523f12
WD
1400);
1401#endif /* CONFIG_LOOPW */
1402
a2681707 1403#ifdef CONFIG_CMD_MEMTEST
0d498393 1404U_BOOT_CMD(
b6fc6fd4 1405 mtest, 5, 1, do_mem_mtest,
a89c33db
WD
1406 "simple RAM read/write test",
1407 "[start [end [pattern [iterations]]]]"
8bde7f77 1408);
a2681707 1409#endif /* CONFIG_CMD_MEMTEST */
8bde7f77 1410
72732318 1411#ifdef CONFIG_CMD_MX_CYCLIC
4aaf29b2 1412U_BOOT_CMD(
53677ef1 1413 mdc, 4, 1, do_mem_mdc,
2fb2604d 1414 "memory display cyclic",
76be8f75 1415 "[.b, .w, .l" HELP_Q "] address count delay(ms)"
4aaf29b2
SR
1416);
1417
1418U_BOOT_CMD(
53677ef1 1419 mwc, 4, 1, do_mem_mwc,
2fb2604d 1420 "memory write cyclic",
76be8f75 1421 "[.b, .w, .l" HELP_Q "] address value delay(ms)"
4aaf29b2 1422);
72732318 1423#endif /* CONFIG_CMD_MX_CYCLIC */
15a33e49 1424
803e1a3d
JJH
1425#ifdef CONFIG_CMD_RANDOM
1426U_BOOT_CMD(
1427 random, 4, 0, do_random,
1428 "fill memory with random pattern",
1429 "<addr> <len> [<seed>]\n"
1430 " - Fill 'len' bytes of memory starting at 'addr' with random data\n"
1431);
1432#endif
This page took 0.615203 seconds and 4 git commands to generate.