cmd_mem.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Memory Functions
  25. *
  26. * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
  27. */
  28. #include <common.h>
  29. #include <command.h>
  30. #ifdef CONFIG_HAS_DATAFLASH
  31. #include <dataflash.h>
  32. #endif
  33. #include <watchdog.h>
  34. #include <u-boot/md5.h>
  35. #include <sha1.h>
  36. #ifdef CMD_MEM_DEBUG
  37. #define PRINTF(fmt,args...) printf (fmt ,##args)
  38. #else
  39. #define PRINTF(fmt,args...)
  40. #endif
  41. static int mod_mem(cmd_tbl_t *, int, int, int, char * const []);
  42. /* Display values from last command.
  43. * Memory modify remembered values are different from display memory.
  44. */
  45. uint dp_last_addr, dp_last_size;
  46. uint dp_last_length = 0x40;
  47. uint mm_last_addr, mm_last_size;
  48. static ulong base_address = 0;
  49. /* Memory Display
  50. *
  51. * Syntax:
  52. * md{.b, .w, .l} {addr} {len}
  53. */
  54. #define DISP_LINE_LEN 16
  55. int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  56. {
  57. ulong addr, length;
  58. #if defined(CONFIG_HAS_DATAFLASH)
  59. ulong nbytes, linebytes;
  60. #endif
  61. int size;
  62. int rc = 0;
  63. /* We use the last specified parameters, unless new ones are
  64. * entered.
  65. */
  66. addr = dp_last_addr;
  67. size = dp_last_size;
  68. length = dp_last_length;
  69. if (argc < 2)
  70. return cmd_usage(cmdtp);
  71. if ((flag & CMD_FLAG_REPEAT) == 0) {
  72. /* New command specified. Check for a size specification.
  73. * Defaults to long if no or incorrect specification.
  74. */
  75. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  76. return 1;
  77. /* Address is specified since argc > 1
  78. */
  79. addr = simple_strtoul(argv[1], NULL, 16);
  80. addr += base_address;
  81. /* If another parameter, it is the length to display.
  82. * Length is the number of objects, not number of bytes.
  83. */
  84. if (argc > 2)
  85. length = simple_strtoul(argv[2], NULL, 16);
  86. }
  87. #if defined(CONFIG_HAS_DATAFLASH)
  88. /* Print the lines.
  89. *
  90. * We buffer all read data, so we can make sure data is read only
  91. * once, and all accesses are with the specified bus width.
  92. */
  93. nbytes = length * size;
  94. do {
  95. char linebuf[DISP_LINE_LEN];
  96. void* p;
  97. linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
  98. rc = read_dataflash(addr, (linebytes/size)*size, linebuf);
  99. p = (rc == DATAFLASH_OK) ? linebuf : (void*)addr;
  100. print_buffer(addr, p, size, linebytes/size, DISP_LINE_LEN/size);
  101. nbytes -= linebytes;
  102. addr += linebytes;
  103. if (ctrlc()) {
  104. rc = 1;
  105. break;
  106. }
  107. } while (nbytes > 0);
  108. #else
  109. # if defined(CONFIG_BLACKFIN)
  110. /* See if we're trying to display L1 inst */
  111. if (addr_bfin_on_chip_mem(addr)) {
  112. char linebuf[DISP_LINE_LEN];
  113. ulong linebytes, nbytes = length * size;
  114. do {
  115. linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
  116. memcpy(linebuf, (void *)addr, linebytes);
  117. print_buffer(addr, linebuf, size, linebytes/size, DISP_LINE_LEN/size);
  118. nbytes -= linebytes;
  119. addr += linebytes;
  120. if (ctrlc()) {
  121. rc = 1;
  122. break;
  123. }
  124. } while (nbytes > 0);
  125. } else
  126. # endif
  127. {
  128. /* Print the lines. */
  129. print_buffer(addr, (void*)addr, size, length, DISP_LINE_LEN/size);
  130. addr += size*length;
  131. }
  132. #endif
  133. dp_last_addr = addr;
  134. dp_last_length = length;
  135. dp_last_size = size;
  136. return (rc);
  137. }
  138. int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  139. {
  140. return mod_mem (cmdtp, 1, flag, argc, argv);
  141. }
  142. int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  143. {
  144. return mod_mem (cmdtp, 0, flag, argc, argv);
  145. }
  146. int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  147. {
  148. ulong addr, writeval, count;
  149. int size;
  150. if ((argc < 3) || (argc > 4))
  151. return cmd_usage(cmdtp);
  152. /* Check for size specification.
  153. */
  154. if ((size = cmd_get_data_size(argv[0], 4)) < 1)
  155. return 1;
  156. /* Address is specified since argc > 1
  157. */
  158. addr = simple_strtoul(argv[1], NULL, 16);
  159. addr += base_address;
  160. /* Get the value to write.
  161. */
  162. writeval = simple_strtoul(argv[2], NULL, 16);
  163. /* Count ? */
  164. if (argc == 4) {
  165. count = simple_strtoul(argv[3], NULL, 16);
  166. } else {
  167. count = 1;
  168. }
  169. while (count-- > 0) {
  170. if (size == 4)
  171. *((ulong *)addr) = (ulong )writeval;
  172. else if (size == 2)
  173. *((ushort *)addr) = (ushort)writeval;
  174. else
  175. *((u_char *)addr) = (u_char)writeval;
  176. addr += size;
  177. }
  178. return 0;
  179. }
  180. #ifdef CONFIG_MX_CYCLIC
  181. int do_mem_mdc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  182. {
  183. int i;
  184. ulong count;
  185. if (argc < 4)
  186. return cmd_usage(cmdtp);
  187. count = simple_strtoul(argv[3], NULL, 10);
  188. for (;;) {
  189. do_mem_md (NULL, 0, 3, argv);
  190. /* delay for <count> ms... */
  191. for (i=0; i<count; i++)
  192. udelay (1000);
  193. /* check for ctrl-c to abort... */
  194. if (ctrlc()) {
  195. puts("Abort\n");
  196. return 0;
  197. }
  198. }
  199. return 0;
  200. }
  201. int do_mem_mwc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  202. {
  203. int i;
  204. ulong count;
  205. if (argc < 4)
  206. return cmd_usage(cmdtp);
  207. count = simple_strtoul(argv[3], NULL, 10);
  208. for (;;) {
  209. do_mem_mw (NULL, 0, 3, argv);
  210. /* delay for <count> ms... */
  211. for (i=0; i<count; i++)
  212. udelay (1000);
  213. /* check for ctrl-c to abort... */
  214. if (ctrlc()) {
  215. puts("Abort\n");
  216. return 0;
  217. }
  218. }
  219. return 0;
  220. }
  221. #endif /* CONFIG_MX_CYCLIC */
  222. int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  223. {
  224. ulong addr1, addr2, count, ngood;
  225. int size;
  226. int rcode = 0;
  227. if (argc != 4)
  228. return cmd_usage(cmdtp);
  229. /* Check for size specification.
  230. */
  231. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  232. return 1;
  233. addr1 = simple_strtoul(argv[1], NULL, 16);
  234. addr1 += base_address;
  235. addr2 = simple_strtoul(argv[2], NULL, 16);
  236. addr2 += base_address;
  237. count = simple_strtoul(argv[3], NULL, 16);
  238. #ifdef CONFIG_HAS_DATAFLASH
  239. if (addr_dataflash(addr1) | addr_dataflash(addr2)){
  240. puts ("Comparison with DataFlash space not supported.\n\r");
  241. return 0;
  242. }
  243. #endif
  244. #ifdef CONFIG_BLACKFIN
  245. if (addr_bfin_on_chip_mem(addr1) || addr_bfin_on_chip_mem(addr2)) {
  246. puts ("Comparison with L1 instruction memory not supported.\n\r");
  247. return 0;
  248. }
  249. #endif
  250. ngood = 0;
  251. while (count-- > 0) {
  252. if (size == 4) {
  253. ulong word1 = *(ulong *)addr1;
  254. ulong word2 = *(ulong *)addr2;
  255. if (word1 != word2) {
  256. printf("word at 0x%08lx (0x%08lx) "
  257. "!= word at 0x%08lx (0x%08lx)\n",
  258. addr1, word1, addr2, word2);
  259. rcode = 1;
  260. break;
  261. }
  262. }
  263. else if (size == 2) {
  264. ushort hword1 = *(ushort *)addr1;
  265. ushort hword2 = *(ushort *)addr2;
  266. if (hword1 != hword2) {
  267. printf("halfword at 0x%08lx (0x%04x) "
  268. "!= halfword at 0x%08lx (0x%04x)\n",
  269. addr1, hword1, addr2, hword2);
  270. rcode = 1;
  271. break;
  272. }
  273. }
  274. else {
  275. u_char byte1 = *(u_char *)addr1;
  276. u_char byte2 = *(u_char *)addr2;
  277. if (byte1 != byte2) {
  278. printf("byte at 0x%08lx (0x%02x) "
  279. "!= byte at 0x%08lx (0x%02x)\n",
  280. addr1, byte1, addr2, byte2);
  281. rcode = 1;
  282. break;
  283. }
  284. }
  285. ngood++;
  286. addr1 += size;
  287. addr2 += size;
  288. /* reset watchdog from time to time */
  289. if ((count % (64 << 10)) == 0)
  290. WATCHDOG_RESET();
  291. }
  292. printf("Total of %ld %s%s were the same\n",
  293. ngood, size == 4 ? "word" : size == 2 ? "halfword" : "byte",
  294. ngood == 1 ? "" : "s");
  295. return rcode;
  296. }
  297. int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  298. {
  299. ulong addr, dest, count;
  300. int size;
  301. if (argc != 4)
  302. return cmd_usage(cmdtp);
  303. /* Check for size specification.
  304. */
  305. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  306. return 1;
  307. addr = simple_strtoul(argv[1], NULL, 16);
  308. addr += base_address;
  309. dest = simple_strtoul(argv[2], NULL, 16);
  310. dest += base_address;
  311. count = simple_strtoul(argv[3], NULL, 16);
  312. if (count == 0) {
  313. puts ("Zero length ???\n");
  314. return 1;
  315. }
  316. #ifndef CONFIG_SYS_NO_FLASH
  317. /* check if we are copying to Flash */
  318. if ( (addr2info(dest) != NULL)
  319. #ifdef CONFIG_HAS_DATAFLASH
  320. && (!addr_dataflash(dest))
  321. #endif
  322. ) {
  323. int rc;
  324. puts ("Copy to Flash... ");
  325. rc = flash_write ((char *)addr, dest, count*size);
  326. if (rc != 0) {
  327. flash_perror (rc);
  328. return (1);
  329. }
  330. puts ("done\n");
  331. return 0;
  332. }
  333. #endif
  334. #ifdef CONFIG_HAS_DATAFLASH
  335. /* Check if we are copying from RAM or Flash to DataFlash */
  336. if (addr_dataflash(dest) && !addr_dataflash(addr)){
  337. int rc;
  338. puts ("Copy to DataFlash... ");
  339. rc = write_dataflash (dest, addr, count*size);
  340. if (rc != 1) {
  341. dataflash_perror (rc);
  342. return (1);
  343. }
  344. puts ("done\n");
  345. return 0;
  346. }
  347. /* Check if we are copying from DataFlash to RAM */
  348. if (addr_dataflash(addr) && !addr_dataflash(dest)
  349. #ifndef CONFIG_SYS_NO_FLASH
  350. && (addr2info(dest) == NULL)
  351. #endif
  352. ){
  353. int rc;
  354. rc = read_dataflash(addr, count * size, (char *) dest);
  355. if (rc != 1) {
  356. dataflash_perror (rc);
  357. return (1);
  358. }
  359. return 0;
  360. }
  361. if (addr_dataflash(addr) && addr_dataflash(dest)){
  362. puts ("Unsupported combination of source/destination.\n\r");
  363. return 1;
  364. }
  365. #endif
  366. #ifdef CONFIG_BLACKFIN
  367. /* See if we're copying to/from L1 inst */
  368. if (addr_bfin_on_chip_mem(dest) || addr_bfin_on_chip_mem(addr)) {
  369. memcpy((void *)dest, (void *)addr, count * size);
  370. return 0;
  371. }
  372. #endif
  373. while (count-- > 0) {
  374. if (size == 4)
  375. *((ulong *)dest) = *((ulong *)addr);
  376. else if (size == 2)
  377. *((ushort *)dest) = *((ushort *)addr);
  378. else
  379. *((u_char *)dest) = *((u_char *)addr);
  380. addr += size;
  381. dest += size;
  382. /* reset watchdog from time to time */
  383. if ((count % (64 << 10)) == 0)
  384. WATCHDOG_RESET();
  385. }
  386. return 0;
  387. }
  388. int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  389. {
  390. if (argc > 1) {
  391. /* Set new base address.
  392. */
  393. base_address = simple_strtoul(argv[1], NULL, 16);
  394. }
  395. /* Print the current base address.
  396. */
  397. printf("Base Address: 0x%08lx\n", base_address);
  398. return 0;
  399. }
  400. int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  401. {
  402. ulong addr, length, i, junk;
  403. int size;
  404. volatile uint *longp;
  405. volatile ushort *shortp;
  406. volatile u_char *cp;
  407. if (argc < 3)
  408. return cmd_usage(cmdtp);
  409. /* Check for a size spefication.
  410. * Defaults to long if no or incorrect specification.
  411. */
  412. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  413. return 1;
  414. /* Address is always specified.
  415. */
  416. addr = simple_strtoul(argv[1], NULL, 16);
  417. /* Length is the number of objects, not number of bytes.
  418. */
  419. length = simple_strtoul(argv[2], NULL, 16);
  420. /* We want to optimize the loops to run as fast as possible.
  421. * If we have only one object, just run infinite loops.
  422. */
  423. if (length == 1) {
  424. if (size == 4) {
  425. longp = (uint *)addr;
  426. for (;;)
  427. i = *longp;
  428. }
  429. if (size == 2) {
  430. shortp = (ushort *)addr;
  431. for (;;)
  432. i = *shortp;
  433. }
  434. cp = (u_char *)addr;
  435. for (;;)
  436. i = *cp;
  437. }
  438. if (size == 4) {
  439. for (;;) {
  440. longp = (uint *)addr;
  441. i = length;
  442. while (i-- > 0)
  443. junk = *longp++;
  444. }
  445. }
  446. if (size == 2) {
  447. for (;;) {
  448. shortp = (ushort *)addr;
  449. i = length;
  450. while (i-- > 0)
  451. junk = *shortp++;
  452. }
  453. }
  454. for (;;) {
  455. cp = (u_char *)addr;
  456. i = length;
  457. while (i-- > 0)
  458. junk = *cp++;
  459. }
  460. }
  461. #ifdef CONFIG_LOOPW
  462. int do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  463. {
  464. ulong addr, length, i, data;
  465. int size;
  466. volatile uint *longp;
  467. volatile ushort *shortp;
  468. volatile u_char *cp;
  469. if (argc < 4)
  470. return cmd_usage(cmdtp);
  471. /* Check for a size spefication.
  472. * Defaults to long if no or incorrect specification.
  473. */
  474. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  475. return 1;
  476. /* Address is always specified.
  477. */
  478. addr = simple_strtoul(argv[1], NULL, 16);
  479. /* Length is the number of objects, not number of bytes.
  480. */
  481. length = simple_strtoul(argv[2], NULL, 16);
  482. /* data to write */
  483. data = simple_strtoul(argv[3], NULL, 16);
  484. /* We want to optimize the loops to run as fast as possible.
  485. * If we have only one object, just run infinite loops.
  486. */
  487. if (length == 1) {
  488. if (size == 4) {
  489. longp = (uint *)addr;
  490. for (;;)
  491. *longp = data;
  492. }
  493. if (size == 2) {
  494. shortp = (ushort *)addr;
  495. for (;;)
  496. *shortp = data;
  497. }
  498. cp = (u_char *)addr;
  499. for (;;)
  500. *cp = data;
  501. }
  502. if (size == 4) {
  503. for (;;) {
  504. longp = (uint *)addr;
  505. i = length;
  506. while (i-- > 0)
  507. *longp++ = data;
  508. }
  509. }
  510. if (size == 2) {
  511. for (;;) {
  512. shortp = (ushort *)addr;
  513. i = length;
  514. while (i-- > 0)
  515. *shortp++ = data;
  516. }
  517. }
  518. for (;;) {
  519. cp = (u_char *)addr;
  520. i = length;
  521. while (i-- > 0)
  522. *cp++ = data;
  523. }
  524. }
  525. #endif /* CONFIG_LOOPW */
  526. /*
  527. * Perform a memory test. A more complete alternative test can be
  528. * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
  529. * interrupted by ctrl-c or by a failure of one of the sub-tests.
  530. */
  531. int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  532. {
  533. vu_long *addr, *start, *end;
  534. ulong val;
  535. ulong readback;
  536. ulong errs = 0;
  537. int iterations = 1;
  538. int iteration_limit;
  539. #if defined(CONFIG_SYS_ALT_MEMTEST)
  540. vu_long len;
  541. vu_long offset;
  542. vu_long test_offset;
  543. vu_long pattern;
  544. vu_long temp;
  545. vu_long anti_pattern;
  546. vu_long num_words;
  547. #if defined(CONFIG_SYS_MEMTEST_SCRATCH)
  548. vu_long *dummy = (vu_long*)CONFIG_SYS_MEMTEST_SCRATCH;
  549. #else
  550. vu_long *dummy = 0; /* yes, this is address 0x0, not NULL */
  551. #endif
  552. int j;
  553. static const ulong bitpattern[] = {
  554. 0x00000001, /* single bit */
  555. 0x00000003, /* two adjacent bits */
  556. 0x00000007, /* three adjacent bits */
  557. 0x0000000F, /* four adjacent bits */
  558. 0x00000005, /* two non-adjacent bits */
  559. 0x00000015, /* three non-adjacent bits */
  560. 0x00000055, /* four non-adjacent bits */
  561. 0xaaaaaaaa, /* alternating 1/0 */
  562. };
  563. #else
  564. ulong incr;
  565. ulong pattern;
  566. #endif
  567. if (argc > 1)
  568. start = (ulong *)simple_strtoul(argv[1], NULL, 16);
  569. else
  570. start = (ulong *)CONFIG_SYS_MEMTEST_START;
  571. if (argc > 2)
  572. end = (ulong *)simple_strtoul(argv[2], NULL, 16);
  573. else
  574. end = (ulong *)(CONFIG_SYS_MEMTEST_END);
  575. if (argc > 3)
  576. pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
  577. else
  578. pattern = 0;
  579. if (argc > 4)
  580. iteration_limit = (ulong)simple_strtoul(argv[4], NULL, 16);
  581. else
  582. iteration_limit = 0;
  583. #if defined(CONFIG_SYS_ALT_MEMTEST)
  584. printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
  585. PRINTF("%s:%d: start 0x%p end 0x%p\n",
  586. __FUNCTION__, __LINE__, start, end);
  587. for (;;) {
  588. if (ctrlc()) {
  589. putc ('\n');
  590. return 1;
  591. }
  592. if (iteration_limit && iterations > iteration_limit) {
  593. printf("Tested %d iteration(s) with %lu errors.\n",
  594. iterations-1, errs);
  595. return errs != 0;
  596. }
  597. printf("Iteration: %6d\r", iterations);
  598. PRINTF("\n");
  599. iterations++;
  600. /*
  601. * Data line test: write a pattern to the first
  602. * location, write the 1's complement to a 'parking'
  603. * address (changes the state of the data bus so a
  604. * floating bus doen't give a false OK), and then
  605. * read the value back. Note that we read it back
  606. * into a variable because the next time we read it,
  607. * it might be right (been there, tough to explain to
  608. * the quality guys why it prints a failure when the
  609. * "is" and "should be" are obviously the same in the
  610. * error message).
  611. *
  612. * Rather than exhaustively testing, we test some
  613. * patterns by shifting '1' bits through a field of
  614. * '0's and '0' bits through a field of '1's (i.e.
  615. * pattern and ~pattern).
  616. */
  617. addr = start;
  618. for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
  619. val = bitpattern[j];
  620. for(; val != 0; val <<= 1) {
  621. *addr = val;
  622. *dummy = ~val; /* clear the test data off of the bus */
  623. readback = *addr;
  624. if(readback != val) {
  625. printf ("FAILURE (data line): "
  626. "expected %08lx, actual %08lx\n",
  627. val, readback);
  628. errs++;
  629. if (ctrlc()) {
  630. putc ('\n');
  631. return 1;
  632. }
  633. }
  634. *addr = ~val;
  635. *dummy = val;
  636. readback = *addr;
  637. if(readback != ~val) {
  638. printf ("FAILURE (data line): "
  639. "Is %08lx, should be %08lx\n",
  640. readback, ~val);
  641. errs++;
  642. if (ctrlc()) {
  643. putc ('\n');
  644. return 1;
  645. }
  646. }
  647. }
  648. }
  649. /*
  650. * Based on code whose Original Author and Copyright
  651. * information follows: Copyright (c) 1998 by Michael
  652. * Barr. This software is placed into the public
  653. * domain and may be used for any purpose. However,
  654. * this notice must not be changed or removed and no
  655. * warranty is either expressed or implied by its
  656. * publication or distribution.
  657. */
  658. /*
  659. * Address line test
  660. *
  661. * Description: Test the address bus wiring in a
  662. * memory region by performing a walking
  663. * 1's test on the relevant bits of the
  664. * address and checking for aliasing.
  665. * This test will find single-bit
  666. * address failures such as stuck -high,
  667. * stuck-low, and shorted pins. The base
  668. * address and size of the region are
  669. * selected by the caller.
  670. *
  671. * Notes: For best results, the selected base
  672. * address should have enough LSB 0's to
  673. * guarantee single address bit changes.
  674. * For example, to test a 64-Kbyte
  675. * region, select a base address on a
  676. * 64-Kbyte boundary. Also, select the
  677. * region size as a power-of-two if at
  678. * all possible.
  679. *
  680. * Returns: 0 if the test succeeds, 1 if the test fails.
  681. */
  682. len = ((ulong)end - (ulong)start)/sizeof(vu_long);
  683. pattern = (vu_long) 0xaaaaaaaa;
  684. anti_pattern = (vu_long) 0x55555555;
  685. PRINTF("%s:%d: length = 0x%.8lx\n",
  686. __FUNCTION__, __LINE__,
  687. len);
  688. /*
  689. * Write the default pattern at each of the
  690. * power-of-two offsets.
  691. */
  692. for (offset = 1; offset < len; offset <<= 1) {
  693. start[offset] = pattern;
  694. }
  695. /*
  696. * Check for address bits stuck high.
  697. */
  698. test_offset = 0;
  699. start[test_offset] = anti_pattern;
  700. for (offset = 1; offset < len; offset <<= 1) {
  701. temp = start[offset];
  702. if (temp != pattern) {
  703. printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
  704. " expected 0x%.8lx, actual 0x%.8lx\n",
  705. (ulong)&start[offset], pattern, temp);
  706. errs++;
  707. if (ctrlc()) {
  708. putc ('\n');
  709. return 1;
  710. }
  711. }
  712. }
  713. start[test_offset] = pattern;
  714. WATCHDOG_RESET();
  715. /*
  716. * Check for addr bits stuck low or shorted.
  717. */
  718. for (test_offset = 1; test_offset < len; test_offset <<= 1) {
  719. start[test_offset] = anti_pattern;
  720. for (offset = 1; offset < len; offset <<= 1) {
  721. temp = start[offset];
  722. if ((temp != pattern) && (offset != test_offset)) {
  723. printf ("\nFAILURE: Address bit stuck low or shorted @"
  724. " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
  725. (ulong)&start[offset], pattern, temp);
  726. errs++;
  727. if (ctrlc()) {
  728. putc ('\n');
  729. return 1;
  730. }
  731. }
  732. }
  733. start[test_offset] = pattern;
  734. }
  735. /*
  736. * Description: Test the integrity of a physical
  737. * memory device by performing an
  738. * increment/decrement test over the
  739. * entire region. In the process every
  740. * storage bit in the device is tested
  741. * as a zero and a one. The base address
  742. * and the size of the region are
  743. * selected by the caller.
  744. *
  745. * Returns: 0 if the test succeeds, 1 if the test fails.
  746. */
  747. num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
  748. /*
  749. * Fill memory with a known pattern.
  750. */
  751. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  752. WATCHDOG_RESET();
  753. start[offset] = pattern;
  754. }
  755. /*
  756. * Check each location and invert it for the second pass.
  757. */
  758. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  759. WATCHDOG_RESET();
  760. temp = start[offset];
  761. if (temp != pattern) {
  762. printf ("\nFAILURE (read/write) @ 0x%.8lx:"
  763. " expected 0x%.8lx, actual 0x%.8lx)\n",
  764. (ulong)&start[offset], pattern, temp);
  765. errs++;
  766. if (ctrlc()) {
  767. putc ('\n');
  768. return 1;
  769. }
  770. }
  771. anti_pattern = ~pattern;
  772. start[offset] = anti_pattern;
  773. }
  774. /*
  775. * Check each location for the inverted pattern and zero it.
  776. */
  777. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  778. WATCHDOG_RESET();
  779. anti_pattern = ~pattern;
  780. temp = start[offset];
  781. if (temp != anti_pattern) {
  782. printf ("\nFAILURE (read/write): @ 0x%.8lx:"
  783. " expected 0x%.8lx, actual 0x%.8lx)\n",
  784. (ulong)&start[offset], anti_pattern, temp);
  785. errs++;
  786. if (ctrlc()) {
  787. putc ('\n');
  788. return 1;
  789. }
  790. }
  791. start[offset] = 0;
  792. }
  793. }
  794. #else /* The original, quickie test */
  795. incr = 1;
  796. for (;;) {
  797. if (ctrlc()) {
  798. putc ('\n');
  799. return 1;
  800. }
  801. if (iteration_limit && iterations > iteration_limit) {
  802. printf("Tested %d iteration(s) with %lu errors.\n",
  803. iterations-1, errs);
  804. return errs != 0;
  805. }
  806. ++iterations;
  807. printf ("\rPattern %08lX Writing..."
  808. "%12s"
  809. "\b\b\b\b\b\b\b\b\b\b",
  810. pattern, "");
  811. for (addr=start,val=pattern; addr<end; addr++) {
  812. WATCHDOG_RESET();
  813. *addr = val;
  814. val += incr;
  815. }
  816. puts ("Reading...");
  817. for (addr=start,val=pattern; addr<end; addr++) {
  818. WATCHDOG_RESET();
  819. readback = *addr;
  820. if (readback != val) {
  821. printf ("\nMem error @ 0x%08X: "
  822. "found %08lX, expected %08lX\n",
  823. (uint)addr, readback, val);
  824. errs++;
  825. if (ctrlc()) {
  826. putc ('\n');
  827. return 1;
  828. }
  829. }
  830. val += incr;
  831. }
  832. /*
  833. * Flip the pattern each time to make lots of zeros and
  834. * then, the next time, lots of ones. We decrement
  835. * the "negative" patterns and increment the "positive"
  836. * patterns to preserve this feature.
  837. */
  838. if(pattern & 0x80000000) {
  839. pattern = -pattern; /* complement & increment */
  840. }
  841. else {
  842. pattern = ~pattern;
  843. }
  844. incr = -incr;
  845. }
  846. #endif
  847. return 0; /* not reached */
  848. }
  849. /* Modify memory.
  850. *
  851. * Syntax:
  852. * mm{.b, .w, .l} {addr}
  853. * nm{.b, .w, .l} {addr}
  854. */
  855. static int
  856. mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
  857. {
  858. ulong addr, i;
  859. int nbytes, size;
  860. extern char console_buffer[];
  861. if (argc != 2)
  862. return cmd_usage(cmdtp);
  863. #ifdef CONFIG_BOOT_RETRY_TIME
  864. reset_cmd_timeout(); /* got a good command to get here */
  865. #endif
  866. /* We use the last specified parameters, unless new ones are
  867. * entered.
  868. */
  869. addr = mm_last_addr;
  870. size = mm_last_size;
  871. if ((flag & CMD_FLAG_REPEAT) == 0) {
  872. /* New command specified. Check for a size specification.
  873. * Defaults to long if no or incorrect specification.
  874. */
  875. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  876. return 1;
  877. /* Address is specified since argc > 1
  878. */
  879. addr = simple_strtoul(argv[1], NULL, 16);
  880. addr += base_address;
  881. }
  882. #ifdef CONFIG_HAS_DATAFLASH
  883. if (addr_dataflash(addr)){
  884. puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
  885. return 0;
  886. }
  887. #endif
  888. #ifdef CONFIG_BLACKFIN
  889. if (addr_bfin_on_chip_mem(addr)) {
  890. puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
  891. return 0;
  892. }
  893. #endif
  894. /* Print the address, followed by value. Then accept input for
  895. * the next value. A non-converted value exits.
  896. */
  897. do {
  898. printf("%08lx:", addr);
  899. if (size == 4)
  900. printf(" %08x", *((uint *)addr));
  901. else if (size == 2)
  902. printf(" %04x", *((ushort *)addr));
  903. else
  904. printf(" %02x", *((u_char *)addr));
  905. nbytes = readline (" ? ");
  906. if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
  907. /* <CR> pressed as only input, don't modify current
  908. * location and move to next. "-" pressed will go back.
  909. */
  910. if (incrflag)
  911. addr += nbytes ? -size : size;
  912. nbytes = 1;
  913. #ifdef CONFIG_BOOT_RETRY_TIME
  914. reset_cmd_timeout(); /* good enough to not time out */
  915. #endif
  916. }
  917. #ifdef CONFIG_BOOT_RETRY_TIME
  918. else if (nbytes == -2) {
  919. break; /* timed out, exit the command */
  920. }
  921. #endif
  922. else {
  923. char *endp;
  924. i = simple_strtoul(console_buffer, &endp, 16);
  925. nbytes = endp - console_buffer;
  926. if (nbytes) {
  927. #ifdef CONFIG_BOOT_RETRY_TIME
  928. /* good enough to not time out
  929. */
  930. reset_cmd_timeout();
  931. #endif
  932. if (size == 4)
  933. *((uint *)addr) = i;
  934. else if (size == 2)
  935. *((ushort *)addr) = i;
  936. else
  937. *((u_char *)addr) = i;
  938. if (incrflag)
  939. addr += size;
  940. }
  941. }
  942. } while (nbytes);
  943. mm_last_addr = addr;
  944. mm_last_size = size;
  945. return 0;
  946. }
  947. #ifndef CONFIG_CRC32_VERIFY
  948. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  949. {
  950. ulong addr, length;
  951. ulong crc;
  952. ulong *ptr;
  953. if (argc < 3)
  954. return cmd_usage(cmdtp);
  955. addr = simple_strtoul (argv[1], NULL, 16);
  956. addr += base_address;
  957. length = simple_strtoul (argv[2], NULL, 16);
  958. crc = crc32 (0, (const uchar *) addr, length);
  959. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  960. addr, addr + length - 1, crc);
  961. if (argc > 3) {
  962. ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
  963. *ptr = crc;
  964. }
  965. return 0;
  966. }
  967. #else /* CONFIG_CRC32_VERIFY */
  968. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  969. {
  970. ulong addr, length;
  971. ulong crc;
  972. ulong *ptr;
  973. ulong vcrc;
  974. int verify;
  975. int ac;
  976. char * const *av;
  977. if (argc < 3) {
  978. usage:
  979. return cmd_usage(cmdtp);
  980. }
  981. av = argv + 1;
  982. ac = argc - 1;
  983. if (strcmp(*av, "-v") == 0) {
  984. verify = 1;
  985. av++;
  986. ac--;
  987. if (ac < 3)
  988. goto usage;
  989. } else
  990. verify = 0;
  991. addr = simple_strtoul(*av++, NULL, 16);
  992. addr += base_address;
  993. length = simple_strtoul(*av++, NULL, 16);
  994. crc = crc32(0, (const uchar *) addr, length);
  995. if (!verify) {
  996. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  997. addr, addr + length - 1, crc);
  998. if (ac > 2) {
  999. ptr = (ulong *) simple_strtoul (*av++, NULL, 16);
  1000. *ptr = crc;
  1001. }
  1002. } else {
  1003. vcrc = simple_strtoul(*av++, NULL, 16);
  1004. if (vcrc != crc) {
  1005. printf ("CRC32 for %08lx ... %08lx ==> %08lx != %08lx ** ERROR **\n",
  1006. addr, addr + length - 1, crc, vcrc);
  1007. return 1;
  1008. }
  1009. }
  1010. return 0;
  1011. }
  1012. #endif /* CONFIG_CRC32_VERIFY */
  1013. #ifdef CONFIG_CMD_MD5SUM
  1014. int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1015. {
  1016. unsigned long addr, len;
  1017. unsigned int i;
  1018. u8 output[16];
  1019. if (argc < 3)
  1020. return cmd_usage(cmdtp);
  1021. addr = simple_strtoul(argv[1], NULL, 16);
  1022. len = simple_strtoul(argv[2], NULL, 16);
  1023. md5((unsigned char *) addr, len, output);
  1024. printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
  1025. for (i = 0; i < 16; i++)
  1026. printf("%02x", output[i]);
  1027. printf("\n");
  1028. return 0;
  1029. }
  1030. #endif
  1031. #ifdef CONFIG_CMD_SHA1
  1032. int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1033. {
  1034. unsigned long addr, len;
  1035. unsigned int i;
  1036. u8 output[20];
  1037. if (argc < 3)
  1038. return cmd_usage(cmdtp);
  1039. addr = simple_strtoul(argv[1], NULL, 16);
  1040. len = simple_strtoul(argv[2], NULL, 16);
  1041. sha1_csum((unsigned char *) addr, len, output);
  1042. printf("SHA1 for %08lx ... %08lx ==> ", addr, addr + len - 1);
  1043. for (i = 0; i < 20; i++)
  1044. printf("%02x", output[i]);
  1045. printf("\n");
  1046. return 0;
  1047. }
  1048. #endif
  1049. #ifdef CONFIG_CMD_UNZIP
  1050. int do_unzip ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1051. {
  1052. unsigned long src, dst;
  1053. unsigned long src_len = ~0UL, dst_len = ~0UL;
  1054. switch (argc) {
  1055. case 4:
  1056. dst_len = simple_strtoul(argv[3], NULL, 16);
  1057. /* fall through */
  1058. case 3:
  1059. src = simple_strtoul(argv[1], NULL, 16);
  1060. dst = simple_strtoul(argv[2], NULL, 16);
  1061. break;
  1062. default:
  1063. return cmd_usage(cmdtp);
  1064. }
  1065. return !!gunzip((void *) dst, dst_len, (void *) src, &src_len);
  1066. }
  1067. #endif /* CONFIG_CMD_UNZIP */
  1068. /**************************************************/
  1069. U_BOOT_CMD(
  1070. md, 3, 1, do_mem_md,
  1071. "memory display",
  1072. "[.b, .w, .l] address [# of objects]"
  1073. );
  1074. U_BOOT_CMD(
  1075. mm, 2, 1, do_mem_mm,
  1076. "memory modify (auto-incrementing address)",
  1077. "[.b, .w, .l] address"
  1078. );
  1079. U_BOOT_CMD(
  1080. nm, 2, 1, do_mem_nm,
  1081. "memory modify (constant address)",
  1082. "[.b, .w, .l] address"
  1083. );
  1084. U_BOOT_CMD(
  1085. mw, 4, 1, do_mem_mw,
  1086. "memory write (fill)",
  1087. "[.b, .w, .l] address value [count]"
  1088. );
  1089. U_BOOT_CMD(
  1090. cp, 4, 1, do_mem_cp,
  1091. "memory copy",
  1092. "[.b, .w, .l] source target count"
  1093. );
  1094. U_BOOT_CMD(
  1095. cmp, 4, 1, do_mem_cmp,
  1096. "memory compare",
  1097. "[.b, .w, .l] addr1 addr2 count"
  1098. );
  1099. #ifndef CONFIG_CRC32_VERIFY
  1100. U_BOOT_CMD(
  1101. crc32, 4, 1, do_mem_crc,
  1102. "checksum calculation",
  1103. "address count [addr]\n - compute CRC32 checksum [save at addr]"
  1104. );
  1105. #else /* CONFIG_CRC32_VERIFY */
  1106. U_BOOT_CMD(
  1107. crc32, 5, 1, do_mem_crc,
  1108. "checksum calculation",
  1109. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  1110. "-v address count crc\n - verify crc of memory area"
  1111. );
  1112. #endif /* CONFIG_CRC32_VERIFY */
  1113. U_BOOT_CMD(
  1114. base, 2, 1, do_mem_base,
  1115. "print or set address offset",
  1116. "\n - print address offset for memory commands\n"
  1117. "base off\n - set address offset for memory commands to 'off'"
  1118. );
  1119. U_BOOT_CMD(
  1120. loop, 3, 1, do_mem_loop,
  1121. "infinite loop on address range",
  1122. "[.b, .w, .l] address number_of_objects"
  1123. );
  1124. #ifdef CONFIG_LOOPW
  1125. U_BOOT_CMD(
  1126. loopw, 4, 1, do_mem_loopw,
  1127. "infinite write loop on address range",
  1128. "[.b, .w, .l] address number_of_objects data_to_write"
  1129. );
  1130. #endif /* CONFIG_LOOPW */
  1131. U_BOOT_CMD(
  1132. mtest, 5, 1, do_mem_mtest,
  1133. "simple RAM read/write test",
  1134. "[start [end [pattern [iterations]]]]"
  1135. );
  1136. #ifdef CONFIG_MX_CYCLIC
  1137. U_BOOT_CMD(
  1138. mdc, 4, 1, do_mem_mdc,
  1139. "memory display cyclic",
  1140. "[.b, .w, .l] address count delay(ms)"
  1141. );
  1142. U_BOOT_CMD(
  1143. mwc, 4, 1, do_mem_mwc,
  1144. "memory write cyclic",
  1145. "[.b, .w, .l] address value delay(ms)"
  1146. );
  1147. #endif /* CONFIG_MX_CYCLIC */
  1148. #ifdef CONFIG_CMD_MD5SUM
  1149. U_BOOT_CMD(
  1150. md5sum, 3, 1, do_md5sum,
  1151. "compute MD5 message digest",
  1152. "address count"
  1153. );
  1154. #endif
  1155. #ifdef CONFIG_CMD_SHA1SUM
  1156. U_BOOT_CMD(
  1157. sha1sum, 3, 1, do_sha1sum,
  1158. "compute SHA1 message digest",
  1159. "address count"
  1160. );
  1161. #endif /* CONFIG_CMD_SHA1 */
  1162. #ifdef CONFIG_CMD_UNZIP
  1163. U_BOOT_CMD(
  1164. unzip, 4, 1, do_unzip,
  1165. "unzip a memory region",
  1166. "srcaddr dstaddr [dstsize]"
  1167. );
  1168. #endif /* CONFIG_CMD_UNZIP */