cmd_mem.c 29 KB

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