cmd_mem.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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. int rcode = 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) without errors.\n",
  604. iterations-1);
  605. return 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. }
  639. *addr = ~val;
  640. *dummy = val;
  641. readback = *addr;
  642. if(readback != ~val) {
  643. printf ("FAILURE (data line): "
  644. "Is %08lx, should be %08lx\n",
  645. readback, ~val);
  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. return 1;
  707. }
  708. }
  709. start[test_offset] = pattern;
  710. WATCHDOG_RESET();
  711. /*
  712. * Check for addr bits stuck low or shorted.
  713. */
  714. for (test_offset = 1; test_offset < len; test_offset <<= 1) {
  715. start[test_offset] = anti_pattern;
  716. for (offset = 1; offset < len; offset <<= 1) {
  717. temp = start[offset];
  718. if ((temp != pattern) && (offset != test_offset)) {
  719. printf ("\nFAILURE: Address bit stuck low or shorted @"
  720. " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
  721. (ulong)&start[offset], pattern, temp);
  722. return 1;
  723. }
  724. }
  725. start[test_offset] = pattern;
  726. }
  727. /*
  728. * Description: Test the integrity of a physical
  729. * memory device by performing an
  730. * increment/decrement test over the
  731. * entire region. In the process every
  732. * storage bit in the device is tested
  733. * as a zero and a one. The base address
  734. * and the size of the region are
  735. * selected by the caller.
  736. *
  737. * Returns: 0 if the test succeeds, 1 if the test fails.
  738. */
  739. num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
  740. /*
  741. * Fill memory with a known pattern.
  742. */
  743. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  744. WATCHDOG_RESET();
  745. start[offset] = pattern;
  746. }
  747. /*
  748. * Check each location and invert it for the second pass.
  749. */
  750. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  751. WATCHDOG_RESET();
  752. temp = start[offset];
  753. if (temp != pattern) {
  754. printf ("\nFAILURE (read/write) @ 0x%.8lx:"
  755. " expected 0x%.8lx, actual 0x%.8lx)\n",
  756. (ulong)&start[offset], pattern, temp);
  757. return 1;
  758. }
  759. anti_pattern = ~pattern;
  760. start[offset] = anti_pattern;
  761. }
  762. /*
  763. * Check each location for the inverted pattern and zero it.
  764. */
  765. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  766. WATCHDOG_RESET();
  767. anti_pattern = ~pattern;
  768. temp = start[offset];
  769. if (temp != anti_pattern) {
  770. printf ("\nFAILURE (read/write): @ 0x%.8lx:"
  771. " expected 0x%.8lx, actual 0x%.8lx)\n",
  772. (ulong)&start[offset], anti_pattern, temp);
  773. return 1;
  774. }
  775. start[offset] = 0;
  776. }
  777. }
  778. #else /* The original, quickie test */
  779. incr = 1;
  780. for (;;) {
  781. if (ctrlc()) {
  782. putc ('\n');
  783. return 1;
  784. }
  785. if (iteration_limit && iterations > iteration_limit) {
  786. printf("Tested %d iteration(s) without errors.\n",
  787. iterations-1);
  788. return 0;
  789. }
  790. ++iterations;
  791. printf ("\rPattern %08lX Writing..."
  792. "%12s"
  793. "\b\b\b\b\b\b\b\b\b\b",
  794. pattern, "");
  795. for (addr=start,val=pattern; addr<end; addr++) {
  796. WATCHDOG_RESET();
  797. *addr = val;
  798. val += incr;
  799. }
  800. puts ("Reading...");
  801. for (addr=start,val=pattern; addr<end; addr++) {
  802. WATCHDOG_RESET();
  803. readback = *addr;
  804. if (readback != val) {
  805. printf ("\nMem error @ 0x%08X: "
  806. "found %08lX, expected %08lX\n",
  807. (uint)addr, readback, val);
  808. rcode = 1;
  809. }
  810. val += incr;
  811. }
  812. /*
  813. * Flip the pattern each time to make lots of zeros and
  814. * then, the next time, lots of ones. We decrement
  815. * the "negative" patterns and increment the "positive"
  816. * patterns to preserve this feature.
  817. */
  818. if(pattern & 0x80000000) {
  819. pattern = -pattern; /* complement & increment */
  820. }
  821. else {
  822. pattern = ~pattern;
  823. }
  824. incr = -incr;
  825. }
  826. #endif
  827. return rcode;
  828. }
  829. /* Modify memory.
  830. *
  831. * Syntax:
  832. * mm{.b, .w, .l} {addr}
  833. * nm{.b, .w, .l} {addr}
  834. */
  835. static int
  836. mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
  837. {
  838. ulong addr, i;
  839. int nbytes, size;
  840. extern char console_buffer[];
  841. if (argc != 2) {
  842. cmd_usage(cmdtp);
  843. return 1;
  844. }
  845. #ifdef CONFIG_BOOT_RETRY_TIME
  846. reset_cmd_timeout(); /* got a good command to get here */
  847. #endif
  848. /* We use the last specified parameters, unless new ones are
  849. * entered.
  850. */
  851. addr = mm_last_addr;
  852. size = mm_last_size;
  853. if ((flag & CMD_FLAG_REPEAT) == 0) {
  854. /* New command specified. Check for a size specification.
  855. * Defaults to long if no or incorrect specification.
  856. */
  857. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  858. return 1;
  859. /* Address is specified since argc > 1
  860. */
  861. addr = simple_strtoul(argv[1], NULL, 16);
  862. addr += base_address;
  863. }
  864. #ifdef CONFIG_HAS_DATAFLASH
  865. if (addr_dataflash(addr)){
  866. puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
  867. return 0;
  868. }
  869. #endif
  870. #ifdef CONFIG_BLACKFIN
  871. if (addr_bfin_on_chip_mem(addr)) {
  872. puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
  873. return 0;
  874. }
  875. #endif
  876. /* Print the address, followed by value. Then accept input for
  877. * the next value. A non-converted value exits.
  878. */
  879. do {
  880. printf("%08lx:", addr);
  881. if (size == 4)
  882. printf(" %08x", *((uint *)addr));
  883. else if (size == 2)
  884. printf(" %04x", *((ushort *)addr));
  885. else
  886. printf(" %02x", *((u_char *)addr));
  887. nbytes = readline (" ? ");
  888. if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
  889. /* <CR> pressed as only input, don't modify current
  890. * location and move to next. "-" pressed will go back.
  891. */
  892. if (incrflag)
  893. addr += nbytes ? -size : size;
  894. nbytes = 1;
  895. #ifdef CONFIG_BOOT_RETRY_TIME
  896. reset_cmd_timeout(); /* good enough to not time out */
  897. #endif
  898. }
  899. #ifdef CONFIG_BOOT_RETRY_TIME
  900. else if (nbytes == -2) {
  901. break; /* timed out, exit the command */
  902. }
  903. #endif
  904. else {
  905. char *endp;
  906. i = simple_strtoul(console_buffer, &endp, 16);
  907. nbytes = endp - console_buffer;
  908. if (nbytes) {
  909. #ifdef CONFIG_BOOT_RETRY_TIME
  910. /* good enough to not time out
  911. */
  912. reset_cmd_timeout();
  913. #endif
  914. if (size == 4)
  915. *((uint *)addr) = i;
  916. else if (size == 2)
  917. *((ushort *)addr) = i;
  918. else
  919. *((u_char *)addr) = i;
  920. if (incrflag)
  921. addr += size;
  922. }
  923. }
  924. } while (nbytes);
  925. mm_last_addr = addr;
  926. mm_last_size = size;
  927. return 0;
  928. }
  929. #ifndef CONFIG_CRC32_VERIFY
  930. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  931. {
  932. ulong addr, length;
  933. ulong crc;
  934. ulong *ptr;
  935. if (argc < 3) {
  936. cmd_usage(cmdtp);
  937. return 1;
  938. }
  939. addr = simple_strtoul (argv[1], NULL, 16);
  940. addr += base_address;
  941. length = simple_strtoul (argv[2], NULL, 16);
  942. crc = crc32 (0, (const uchar *) addr, length);
  943. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  944. addr, addr + length - 1, crc);
  945. if (argc > 3) {
  946. ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
  947. *ptr = crc;
  948. }
  949. return 0;
  950. }
  951. #else /* CONFIG_CRC32_VERIFY */
  952. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  953. {
  954. ulong addr, length;
  955. ulong crc;
  956. ulong *ptr;
  957. ulong vcrc;
  958. int verify;
  959. int ac;
  960. char **av;
  961. if (argc < 3) {
  962. usage:
  963. cmd_usage(cmdtp);
  964. return 1;
  965. }
  966. av = argv + 1;
  967. ac = argc - 1;
  968. if (strcmp(*av, "-v") == 0) {
  969. verify = 1;
  970. av++;
  971. ac--;
  972. if (ac < 3)
  973. goto usage;
  974. } else
  975. verify = 0;
  976. addr = simple_strtoul(*av++, NULL, 16);
  977. addr += base_address;
  978. length = simple_strtoul(*av++, NULL, 16);
  979. crc = crc32(0, (const uchar *) addr, length);
  980. if (!verify) {
  981. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  982. addr, addr + length - 1, crc);
  983. if (ac > 2) {
  984. ptr = (ulong *) simple_strtoul (*av++, NULL, 16);
  985. *ptr = crc;
  986. }
  987. } else {
  988. vcrc = simple_strtoul(*av++, NULL, 16);
  989. if (vcrc != crc) {
  990. printf ("CRC32 for %08lx ... %08lx ==> %08lx != %08lx ** ERROR **\n",
  991. addr, addr + length - 1, crc, vcrc);
  992. return 1;
  993. }
  994. }
  995. return 0;
  996. }
  997. #endif /* CONFIG_CRC32_VERIFY */
  998. #ifdef CONFIG_CMD_MD5SUM
  999. int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1000. {
  1001. unsigned long addr, len;
  1002. unsigned int i;
  1003. u8 output[16];
  1004. if (argc < 3) {
  1005. cmd_usage(cmdtp);
  1006. return 1;
  1007. }
  1008. addr = simple_strtoul(argv[1], NULL, 16);
  1009. len = simple_strtoul(argv[2], NULL, 16);
  1010. md5((unsigned char *) addr, len, output);
  1011. printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
  1012. for (i = 0; i < 16; i++)
  1013. printf("%02x", output[i]);
  1014. printf("\n");
  1015. return 0;
  1016. }
  1017. #endif
  1018. #ifdef CONFIG_CMD_SHA1
  1019. int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1020. {
  1021. unsigned long addr, len;
  1022. unsigned int i;
  1023. u8 output[20];
  1024. if (argc < 3) {
  1025. cmd_usage(cmdtp);
  1026. return 1;
  1027. }
  1028. addr = simple_strtoul(argv[1], NULL, 16);
  1029. len = simple_strtoul(argv[2], NULL, 16);
  1030. sha1_csum((unsigned char *) addr, len, output);
  1031. printf("SHA1 for %08lx ... %08lx ==> ", addr, addr + len - 1);
  1032. for (i = 0; i < 20; i++)
  1033. printf("%02x", output[i]);
  1034. printf("\n");
  1035. return 0;
  1036. }
  1037. #endif
  1038. #ifdef CONFIG_CMD_UNZIP
  1039. int gunzip (void *, int, unsigned char *, unsigned long *);
  1040. int do_unzip ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1041. {
  1042. unsigned long src, dst;
  1043. unsigned long src_len = ~0UL, dst_len = ~0UL;
  1044. switch (argc) {
  1045. case 4:
  1046. dst_len = simple_strtoul(argv[3], NULL, 16);
  1047. /* fall through */
  1048. case 3:
  1049. src = simple_strtoul(argv[1], NULL, 16);
  1050. dst = simple_strtoul(argv[2], NULL, 16);
  1051. break;
  1052. default:
  1053. cmd_usage(cmdtp);
  1054. return 1;
  1055. }
  1056. return !!gunzip((void *) dst, dst_len, (void *) src, &src_len);
  1057. }
  1058. #endif /* CONFIG_CMD_UNZIP */
  1059. /**************************************************/
  1060. U_BOOT_CMD(
  1061. md, 3, 1, do_mem_md,
  1062. "memory display",
  1063. "[.b, .w, .l] address [# of objects]"
  1064. );
  1065. U_BOOT_CMD(
  1066. mm, 2, 1, do_mem_mm,
  1067. "memory modify (auto-incrementing address)",
  1068. "[.b, .w, .l] address"
  1069. );
  1070. U_BOOT_CMD(
  1071. nm, 2, 1, do_mem_nm,
  1072. "memory modify (constant address)",
  1073. "[.b, .w, .l] address"
  1074. );
  1075. U_BOOT_CMD(
  1076. mw, 4, 1, do_mem_mw,
  1077. "memory write (fill)",
  1078. "[.b, .w, .l] address value [count]"
  1079. );
  1080. U_BOOT_CMD(
  1081. cp, 4, 1, do_mem_cp,
  1082. "memory copy",
  1083. "[.b, .w, .l] source target count"
  1084. );
  1085. U_BOOT_CMD(
  1086. cmp, 4, 1, do_mem_cmp,
  1087. "memory compare",
  1088. "[.b, .w, .l] addr1 addr2 count"
  1089. );
  1090. #ifndef CONFIG_CRC32_VERIFY
  1091. U_BOOT_CMD(
  1092. crc32, 4, 1, do_mem_crc,
  1093. "checksum calculation",
  1094. "address count [addr]\n - compute CRC32 checksum [save at addr]"
  1095. );
  1096. #else /* CONFIG_CRC32_VERIFY */
  1097. U_BOOT_CMD(
  1098. crc32, 5, 1, do_mem_crc,
  1099. "checksum calculation",
  1100. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  1101. "-v address count crc\n - verify crc of memory area"
  1102. );
  1103. #endif /* CONFIG_CRC32_VERIFY */
  1104. U_BOOT_CMD(
  1105. base, 2, 1, do_mem_base,
  1106. "print or set address offset",
  1107. "\n - print address offset for memory commands\n"
  1108. "base off\n - set address offset for memory commands to 'off'"
  1109. );
  1110. U_BOOT_CMD(
  1111. loop, 3, 1, do_mem_loop,
  1112. "infinite loop on address range",
  1113. "[.b, .w, .l] address number_of_objects"
  1114. );
  1115. #ifdef CONFIG_LOOPW
  1116. U_BOOT_CMD(
  1117. loopw, 4, 1, do_mem_loopw,
  1118. "infinite write loop on address range",
  1119. "[.b, .w, .l] address number_of_objects data_to_write"
  1120. );
  1121. #endif /* CONFIG_LOOPW */
  1122. U_BOOT_CMD(
  1123. mtest, 5, 1, do_mem_mtest,
  1124. "simple RAM read/write test",
  1125. "[start [end [pattern [iterations]]]]"
  1126. );
  1127. #ifdef CONFIG_MX_CYCLIC
  1128. U_BOOT_CMD(
  1129. mdc, 4, 1, do_mem_mdc,
  1130. "memory display cyclic",
  1131. "[.b, .w, .l] address count delay(ms)"
  1132. );
  1133. U_BOOT_CMD(
  1134. mwc, 4, 1, do_mem_mwc,
  1135. "memory write cyclic",
  1136. "[.b, .w, .l] address value delay(ms)"
  1137. );
  1138. #endif /* CONFIG_MX_CYCLIC */
  1139. #ifdef CONFIG_CMD_MD5SUM
  1140. U_BOOT_CMD(
  1141. md5sum, 3, 1, do_md5sum,
  1142. "compute MD5 message digest",
  1143. "address count"
  1144. );
  1145. #endif
  1146. #ifdef CONFIG_CMD_SHA1SUM
  1147. U_BOOT_CMD(
  1148. sha1sum, 3, 1, do_sha1sum,
  1149. "compute SHA1 message digest",
  1150. "address count"
  1151. );
  1152. #endif /* CONFIG_CMD_SHA1 */
  1153. #ifdef CONFIG_CMD_UNZIP
  1154. U_BOOT_CMD(
  1155. unzip, 4, 1, do_unzip,
  1156. "unzip a memory region",
  1157. "srcaddr dstaddr [dstsize]"
  1158. );
  1159. #endif /* CONFIG_CMD_UNZIP */