cmd_mem.c 29 KB

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