cmd_boot.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. /*
  2. * (C) Copyright 2000-2002
  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. * Boot support
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <cmd_boot.h>
  29. #include <cmd_autoscript.h>
  30. #include <s_record.h>
  31. #include <net.h>
  32. #include <syscall.h>
  33. #if (CONFIG_COMMANDS & CFG_CMD_LOADS)
  34. static ulong load_serial (ulong offset);
  35. static int read_record (char *buf, ulong len);
  36. # if (CONFIG_COMMANDS & CFG_CMD_SAVES)
  37. static int save_serial (ulong offset, ulong size);
  38. static int write_record (char *buf);
  39. # endif /* CFG_CMD_SAVES */
  40. static int do_echo = 1;
  41. #endif /* CFG_CMD_LOADS */
  42. #if (CONFIG_COMMANDS & CFG_CMD_BDI)
  43. static void print_num(const char *, ulong);
  44. #ifndef CONFIG_ARM /* PowerPC and other */
  45. #ifdef CONFIG_PPC
  46. static void print_str(const char *, const char *);
  47. int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  48. {
  49. DECLARE_GLOBAL_DATA_PTR;
  50. int i;
  51. bd_t *bd = gd->bd;
  52. char buf[32];
  53. #ifdef DEBUG
  54. print_num ("bd address", (ulong)bd );
  55. #endif
  56. print_num ("memstart", bd->bi_memstart );
  57. print_num ("memsize", bd->bi_memsize );
  58. print_num ("flashstart", bd->bi_flashstart );
  59. print_num ("flashsize", bd->bi_flashsize );
  60. print_num ("flashoffset", bd->bi_flashoffset );
  61. print_num ("sramstart", bd->bi_sramstart );
  62. print_num ("sramsize", bd->bi_sramsize );
  63. #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260)
  64. print_num ("immr_base", bd->bi_immr_base );
  65. #endif
  66. print_num ("bootflags", bd->bi_bootflags );
  67. #if defined(CONFIG_405GP) || defined(CONFIG_405CR) || defined(CONFIG_405EP)
  68. print_str ("procfreq", strmhz(buf, bd->bi_procfreq));
  69. print_str ("plb_busfreq", strmhz(buf, bd->bi_plb_busfreq));
  70. #if defined(CONFIG_405GP) || defined(CONFIG_405EP)
  71. print_str ("pci_busfreq", strmhz(buf, bd->bi_pci_busfreq));
  72. #endif
  73. #else
  74. #if defined(CONFIG_8260)
  75. print_str ("vco", strmhz(buf, bd->bi_vco));
  76. print_str ("sccfreq", strmhz(buf, bd->bi_sccfreq));
  77. print_str ("brgfreq", strmhz(buf, bd->bi_brgfreq));
  78. #endif
  79. print_str ("intfreq", strmhz(buf, bd->bi_intfreq));
  80. #if defined(CONFIG_8260)
  81. print_str ("cpmfreq", strmhz(buf, bd->bi_cpmfreq));
  82. #endif
  83. print_str ("busfreq", strmhz(buf, bd->bi_busfreq));
  84. #endif /* defined(CONFIG_405GP) || defined(CONFIG_405CR) */
  85. printf ("ethaddr =");
  86. for (i=0; i<6; ++i) {
  87. printf ("%c%02X", i ? ':' : ' ', bd->bi_enetaddr[i]);
  88. }
  89. #ifdef CONFIG_PN62
  90. printf ("\neth1addr =");
  91. for (i=0; i<6; ++i) {
  92. printf ("%c%02X", i ? ':' : ' ', bd->bi_enet1addr[i]);
  93. }
  94. #endif /* CONFIG_PN62 */
  95. #ifdef CONFIG_HERMES
  96. print_str ("ethspeed", strmhz(buf, bd->bi_ethspeed));
  97. #endif
  98. printf ("\nIP addr = "); print_IPaddr (bd->bi_ip_addr);
  99. printf ("\nbaudrate = %6ld bps\n", bd->bi_baudrate );
  100. return 0;
  101. }
  102. #else /* MIPS */
  103. int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  104. {
  105. DECLARE_GLOBAL_DATA_PTR;
  106. int i;
  107. bd_t *bd = gd->bd;
  108. print_num ("boot_params", (ulong)bd->bi_boot_params);
  109. print_num ("memstart", (ulong)bd->bi_memstart);
  110. print_num ("memsize", (ulong)bd->bi_memsize);
  111. print_num ("flashstart", (ulong)bd->bi_flashstart);
  112. print_num ("flashsize", (ulong)bd->bi_flashsize);
  113. print_num ("flashoffset", (ulong)bd->bi_flashoffset);
  114. printf ("ethaddr =");
  115. for (i=0; i<6; ++i) {
  116. printf ("%c%02X", i ? ':' : ' ', bd->bi_enetaddr[i]);
  117. }
  118. printf ("\nip_addr = ");
  119. print_IPaddr (bd->bi_ip_addr);
  120. printf ("\nbaudrate = %d bps\n", bd->bi_baudrate);
  121. return 0;
  122. }
  123. #endif /* MIPS */
  124. #else /* ARM */
  125. int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  126. {
  127. DECLARE_GLOBAL_DATA_PTR;
  128. int i;
  129. bd_t *bd = gd->bd;
  130. print_num ("arch_number", bd->bi_arch_number);
  131. print_num ("env_t", (ulong)bd->bi_env);
  132. print_num ("boot_params", (ulong)bd->bi_boot_params);
  133. for (i=0; i<CONFIG_NR_DRAM_BANKS; ++i) {
  134. print_num("DRAM bank", i);
  135. print_num("-> start", bd->bi_dram[i].start);
  136. print_num("-> size", bd->bi_dram[i].size);
  137. }
  138. printf ("ethaddr =");
  139. for (i=0; i<6; ++i) {
  140. printf ("%c%02X", i ? ':' : ' ', bd->bi_enetaddr[i]);
  141. }
  142. printf ("\n"
  143. "ip_addr = ");
  144. print_IPaddr (bd->bi_ip_addr);
  145. printf ("\n"
  146. "baudrate = %d bps\n", bd->bi_baudrate);
  147. return 0;
  148. }
  149. #endif /* CONFIG_ARM XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
  150. static void print_num(const char *name, ulong value)
  151. {
  152. printf ("%-12s= 0x%08lX\n", name, value);
  153. }
  154. #ifdef CONFIG_PPC
  155. static void print_str(const char *name, const char *str)
  156. {
  157. printf ("%-12s= %6s MHz\n", name, str);
  158. }
  159. #endif /* CONFIG_PPC */
  160. #endif /* CFG_CMD_BDI */
  161. int do_go (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  162. {
  163. ulong addr, rc;
  164. int rcode = 0;
  165. if (argc < 2) {
  166. printf ("Usage:\n%s\n", cmdtp->usage);
  167. return 1;
  168. }
  169. addr = simple_strtoul(argv[1], NULL, 16);
  170. printf ("## Starting application at 0x%08lX ...\n", addr);
  171. /*
  172. * pass address parameter as argv[0] (aka command name),
  173. * and all remaining args
  174. */
  175. rc = ((ulong (*)(int, char *[]))addr) (--argc, &argv[1]);
  176. if (rc != 0) rcode = 1;
  177. printf ("## Application terminated, rc = 0x%lX\n", rc);
  178. return rcode;
  179. }
  180. #if (CONFIG_COMMANDS & CFG_CMD_LOADS)
  181. int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  182. {
  183. ulong offset = 0;
  184. ulong addr;
  185. int i;
  186. char *env_echo;
  187. int rcode = 0;
  188. #ifdef CFG_LOADS_BAUD_CHANGE
  189. DECLARE_GLOBAL_DATA_PTR;
  190. int load_baudrate, current_baudrate;
  191. load_baudrate = current_baudrate = gd->baudrate;
  192. #endif
  193. if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
  194. do_echo = 1;
  195. } else {
  196. do_echo = 0;
  197. }
  198. #ifdef CFG_LOADS_BAUD_CHANGE
  199. if (argc >= 2) {
  200. offset = simple_strtoul(argv[1], NULL, 16);
  201. }
  202. if (argc == 3) {
  203. load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
  204. /* default to current baudrate */
  205. if (load_baudrate == 0)
  206. load_baudrate = current_baudrate;
  207. }
  208. #else /* ! CFG_LOADS_BAUD_CHANGE */
  209. if (argc == 2) {
  210. offset = simple_strtoul(argv[1], NULL, 16);
  211. }
  212. #endif /* CFG_LOADS_BAUD_CHANGE */
  213. #ifdef CFG_LOADS_BAUD_CHANGE
  214. if (load_baudrate != current_baudrate) {
  215. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  216. load_baudrate);
  217. udelay(50000);
  218. gd->baudrate = load_baudrate;
  219. serial_setbrg ();
  220. udelay(50000);
  221. for (;;) {
  222. if (getc() == '\r')
  223. break;
  224. }
  225. }
  226. #endif /* CFG_LOADS_BAUD_CHANGE */
  227. printf ("## Ready for S-Record download ...\n");
  228. addr = load_serial (offset);
  229. /*
  230. * Gather any trailing characters (for instance, the ^D which
  231. * is sent by 'cu' after sending a file), and give the
  232. * box some time (100 * 1 ms)
  233. */
  234. for (i=0; i<100; ++i) {
  235. if (serial_tstc()) {
  236. (void) serial_getc();
  237. }
  238. udelay(1000);
  239. }
  240. if (addr == ~0) {
  241. printf ("## S-Record download aborted\n");
  242. rcode = 1;
  243. } else {
  244. printf ("## Start Addr = 0x%08lX\n", addr);
  245. load_addr = addr;
  246. }
  247. #ifdef CFG_LOADS_BAUD_CHANGE
  248. if (load_baudrate != current_baudrate) {
  249. printf ("## Switch baudrate to %d bps and press ESC ...\n",
  250. current_baudrate);
  251. udelay (50000);
  252. gd->baudrate = current_baudrate;
  253. serial_setbrg ();
  254. udelay (50000);
  255. for (;;) {
  256. if (getc() == 0x1B) /* ESC */
  257. break;
  258. }
  259. }
  260. #endif
  261. return rcode;
  262. }
  263. static ulong
  264. load_serial (ulong offset)
  265. {
  266. char record[SREC_MAXRECLEN + 1]; /* buffer for one S-Record */
  267. char binbuf[SREC_MAXBINLEN]; /* buffer for binary data */
  268. int binlen; /* no. of data bytes in S-Rec. */
  269. int type; /* return code for record type */
  270. ulong addr; /* load address from S-Record */
  271. ulong size; /* number of bytes transferred */
  272. char buf[32];
  273. ulong store_addr;
  274. ulong start_addr = ~0;
  275. ulong end_addr = 0;
  276. int line_count = 0;
  277. while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
  278. type = srec_decode (record, &binlen, &addr, binbuf);
  279. if (type < 0) {
  280. return (~0); /* Invalid S-Record */
  281. }
  282. switch (type) {
  283. case SREC_DATA2:
  284. case SREC_DATA3:
  285. case SREC_DATA4:
  286. store_addr = addr + offset;
  287. #ifndef CFG_NO_FLASH
  288. if (addr2info(store_addr)) {
  289. int rc;
  290. rc = flash_write((uchar *)binbuf,store_addr,binlen);
  291. if (rc != 0) {
  292. flash_perror (rc);
  293. return (~0);
  294. }
  295. } else
  296. #endif
  297. {
  298. memcpy ((char *)(store_addr), binbuf, binlen);
  299. }
  300. if ((store_addr) < start_addr)
  301. start_addr = store_addr;
  302. if ((store_addr + binlen - 1) > end_addr)
  303. end_addr = store_addr + binlen - 1;
  304. break;
  305. case SREC_END2:
  306. case SREC_END3:
  307. case SREC_END4:
  308. udelay (10000);
  309. size = end_addr - start_addr + 1;
  310. printf ("\n"
  311. "## First Load Addr = 0x%08lX\n"
  312. "## Last Load Addr = 0x%08lX\n"
  313. "## Total Size = 0x%08lX = %ld Bytes\n",
  314. start_addr, end_addr, size, size
  315. );
  316. flush_cache (addr, size);
  317. sprintf(buf, "%lX", size);
  318. setenv("filesize", buf);
  319. return (addr);
  320. case SREC_START:
  321. break;
  322. default:
  323. break;
  324. }
  325. if (!do_echo) { /* print a '.' every 100 lines */
  326. if ((++line_count % 100) == 0)
  327. putc ('.');
  328. }
  329. }
  330. return (~0); /* Download aborted */
  331. }
  332. static int
  333. read_record (char *buf, ulong len)
  334. {
  335. char *p;
  336. char c;
  337. --len; /* always leave room for terminating '\0' byte */
  338. for (p=buf; p < buf+len; ++p) {
  339. c = serial_getc(); /* read character */
  340. if (do_echo)
  341. serial_putc (c); /* ... and echo it */
  342. switch (c) {
  343. case '\r':
  344. case '\n':
  345. *p = '\0';
  346. return (p - buf);
  347. case '\0':
  348. case 0x03: /* ^C - Control C */
  349. return (-1);
  350. default:
  351. *p = c;
  352. }
  353. /* Check for the console hangup (if any different from serial) */
  354. #ifdef CONFIG_PPC /* we don't have syscall_tbl anywhere else */
  355. if (syscall_tbl[SYSCALL_GETC] != serial_getc) {
  356. if (ctrlc()) {
  357. return (-1);
  358. }
  359. }
  360. #endif
  361. }
  362. /* line too long - truncate */
  363. *p = '\0';
  364. return (p - buf);
  365. }
  366. #if (CONFIG_COMMANDS & CFG_CMD_SAVES)
  367. int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  368. {
  369. ulong offset = 0;
  370. ulong size = 0;
  371. #ifdef CFG_LOADS_BAUD_CHANGE
  372. DECLARE_GLOBAL_DATA_PTR;
  373. int save_baudrate, current_baudrate;
  374. save_baudrate = current_baudrate = gd->baudrate;
  375. #endif
  376. if (argc >= 2) {
  377. offset = simple_strtoul(argv[1], NULL, 16);
  378. }
  379. #ifdef CFG_LOADS_BAUD_CHANGE
  380. if (argc >= 3) {
  381. size = simple_strtoul(argv[2], NULL, 16);
  382. }
  383. if (argc == 4) {
  384. save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
  385. /* default to current baudrate */
  386. if (save_baudrate == 0)
  387. save_baudrate = current_baudrate;
  388. }
  389. #else /* ! CFG_LOADS_BAUD_CHANGE */
  390. if (argc == 3) {
  391. size = simple_strtoul(argv[2], NULL, 16);
  392. }
  393. #endif /* CFG_LOADS_BAUD_CHANGE */
  394. #ifdef CFG_LOADS_BAUD_CHANGE
  395. if (save_baudrate != current_baudrate) {
  396. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  397. save_baudrate);
  398. udelay(50000);
  399. gd->baudrate = save_baudrate;
  400. serial_setbrg ();
  401. udelay(50000);
  402. for (;;) {
  403. if (getc() == '\r')
  404. break;
  405. }
  406. }
  407. #endif /* CFG_LOADS_BAUD_CHANGE */
  408. printf ("## Ready for S-Record upload, press ENTER to proceed ...\n");
  409. for (;;) {
  410. if (getc() == '\r')
  411. break;
  412. }
  413. if(save_serial (offset, size)) {
  414. printf ("## S-Record upload aborted\n");
  415. } else {
  416. printf ("## S-Record upload complete\n");
  417. }
  418. #ifdef CFG_LOADS_BAUD_CHANGE
  419. if (save_baudrate != current_baudrate) {
  420. printf ("## Switch baudrate to %d bps and press ESC ...\n",
  421. (int)current_baudrate);
  422. udelay (50000);
  423. gd->baudrate = current_baudrate;
  424. serial_setbrg ();
  425. udelay (50000);
  426. for (;;) {
  427. if (getc() == 0x1B) /* ESC */
  428. break;
  429. }
  430. }
  431. #endif
  432. return 0;
  433. }
  434. #define SREC3_START "S0030000FC\n"
  435. #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
  436. #define SREC3_END "S70500000000FA\n"
  437. #define SREC_BYTES_PER_RECORD 16
  438. static int save_serial (ulong address, ulong count)
  439. {
  440. int i, c, reclen, checksum, length;
  441. char *hex = "0123456789ABCDEF";
  442. char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
  443. char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
  444. reclen = 0;
  445. checksum = 0;
  446. if(write_record(SREC3_START)) /* write the header */
  447. return (-1);
  448. do {
  449. if(count) { /* collect hex data in the buffer */
  450. c = *(volatile uchar*)(address + reclen); /* get one byte */
  451. checksum += c; /* accumulate checksum */
  452. data[2*reclen] = hex[(c>>4)&0x0f];
  453. data[2*reclen+1] = hex[c & 0x0f];
  454. data[2*reclen+2] = '\0';
  455. ++reclen;
  456. --count;
  457. }
  458. if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
  459. /* enough data collected for one record: dump it */
  460. if(reclen) { /* build & write a data record: */
  461. /* address + data + checksum */
  462. length = 4 + reclen + 1;
  463. /* accumulate length bytes into checksum */
  464. for(i = 0; i < 2; i++)
  465. checksum += (length >> (8*i)) & 0xff;
  466. /* accumulate address bytes into checksum: */
  467. for(i = 0; i < 4; i++)
  468. checksum += (address >> (8*i)) & 0xff;
  469. /* make proper checksum byte: */
  470. checksum = ~checksum & 0xff;
  471. /* output one record: */
  472. sprintf(record, SREC3_FORMAT, length, address, data, checksum);
  473. if(write_record(record))
  474. return (-1);
  475. }
  476. address += reclen; /* increment address */
  477. checksum = 0;
  478. reclen = 0;
  479. }
  480. }
  481. while(count);
  482. if(write_record(SREC3_END)) /* write the final record */
  483. return (-1);
  484. return(0);
  485. }
  486. static int
  487. write_record (char *buf)
  488. {
  489. char c;
  490. while((c = *buf++))
  491. serial_putc(c);
  492. /* Check for the console hangup (if any different from serial) */
  493. if (ctrlc()) {
  494. return (-1);
  495. }
  496. return (0);
  497. }
  498. # endif /* CFG_CMD_SAVES */
  499. #endif /* CFG_CMD_LOADS */
  500. #if (CONFIG_COMMANDS & CFG_CMD_LOADB) /* loadb command (load binary) included */
  501. #define XON_CHAR 17
  502. #define XOFF_CHAR 19
  503. #define START_CHAR 0x01
  504. #define ETX_CHAR 0x03
  505. #define END_CHAR 0x0D
  506. #define SPACE 0x20
  507. #define K_ESCAPE 0x23
  508. #define SEND_TYPE 'S'
  509. #define DATA_TYPE 'D'
  510. #define ACK_TYPE 'Y'
  511. #define NACK_TYPE 'N'
  512. #define BREAK_TYPE 'B'
  513. #define tochar(x) ((char) (((x) + SPACE) & 0xff))
  514. #define untochar(x) ((int) (((x) - SPACE) & 0xff))
  515. extern int os_data_count;
  516. extern int os_data_header[8];
  517. static void set_kerm_bin_mode(unsigned long *);
  518. static int k_recv(void);
  519. static ulong load_serial_bin (ulong offset);
  520. char his_eol; /* character he needs at end of packet */
  521. int his_pad_count; /* number of pad chars he needs */
  522. char his_pad_char; /* pad chars he needs */
  523. char his_quote; /* quote chars he'll use */
  524. int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  525. {
  526. DECLARE_GLOBAL_DATA_PTR;
  527. ulong offset = 0;
  528. ulong addr;
  529. int load_baudrate, current_baudrate;
  530. int rcode = 0;
  531. char *s;
  532. /* pre-set offset from CFG_LOAD_ADDR */
  533. offset = CFG_LOAD_ADDR;
  534. /* pre-set offset from $loadaddr */
  535. if ((s = getenv("loadaddr")) != NULL) {
  536. offset = simple_strtoul(s, NULL, 16);
  537. }
  538. load_baudrate = current_baudrate = gd->baudrate;
  539. if (argc >= 2) {
  540. offset = simple_strtoul(argv[1], NULL, 16);
  541. }
  542. if (argc == 3) {
  543. load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
  544. /* default to current baudrate */
  545. if (load_baudrate == 0)
  546. load_baudrate = current_baudrate;
  547. }
  548. if (load_baudrate != current_baudrate) {
  549. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  550. load_baudrate);
  551. udelay(50000);
  552. gd->baudrate = load_baudrate;
  553. serial_setbrg ();
  554. udelay(50000);
  555. for (;;) {
  556. if (getc() == '\r')
  557. break;
  558. }
  559. }
  560. printf ("## Ready for binary (kermit) download "
  561. "to 0x%08lX at %d bps...\n",
  562. offset,
  563. current_baudrate);
  564. addr = load_serial_bin (offset);
  565. if (addr == ~0) {
  566. load_addr = 0;
  567. printf ("## Binary (kermit) download aborted\n");
  568. rcode = 1;
  569. } else {
  570. printf ("## Start Addr = 0x%08lX\n", addr);
  571. load_addr = addr;
  572. }
  573. if (load_baudrate != current_baudrate) {
  574. printf ("## Switch baudrate to %d bps and press ESC ...\n",
  575. current_baudrate);
  576. udelay (50000);
  577. gd->baudrate = current_baudrate;
  578. serial_setbrg ();
  579. udelay (50000);
  580. for (;;) {
  581. if (getc() == 0x1B) /* ESC */
  582. break;
  583. }
  584. }
  585. #ifdef CONFIG_AUTOSCRIPT
  586. if (load_addr) {
  587. char *s;
  588. if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
  589. printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
  590. rcode = autoscript (load_addr);
  591. }
  592. }
  593. #endif
  594. return rcode;
  595. }
  596. static ulong load_serial_bin (ulong offset)
  597. {
  598. int size, i;
  599. char buf[32];
  600. set_kerm_bin_mode ((ulong *) offset);
  601. size = k_recv ();
  602. /*
  603. * Gather any trailing characters (for instance, the ^D which
  604. * is sent by 'cu' after sending a file), and give the
  605. * box some time (100 * 1 ms)
  606. */
  607. for (i=0; i<100; ++i) {
  608. if (serial_tstc()) {
  609. (void) serial_getc();
  610. }
  611. udelay(1000);
  612. }
  613. flush_cache (offset, size);
  614. printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
  615. sprintf(buf, "%X", size);
  616. setenv("filesize", buf);
  617. return offset;
  618. }
  619. void send_pad (void)
  620. {
  621. int count = his_pad_count;
  622. while (count-- > 0)
  623. serial_putc (his_pad_char);
  624. }
  625. /* converts escaped kermit char to binary char */
  626. char ktrans (char in)
  627. {
  628. if ((in & 0x60) == 0x40) {
  629. return (char) (in & ~0x40);
  630. } else if ((in & 0x7f) == 0x3f) {
  631. return (char) (in | 0x40);
  632. } else
  633. return in;
  634. }
  635. int chk1 (char *buffer)
  636. {
  637. int total = 0;
  638. while (*buffer) {
  639. total += *buffer++;
  640. }
  641. return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
  642. }
  643. void s1_sendpacket (char *packet)
  644. {
  645. send_pad ();
  646. while (*packet) {
  647. serial_putc (*packet++);
  648. }
  649. }
  650. static char a_b[24];
  651. void send_ack (int n)
  652. {
  653. a_b[0] = START_CHAR;
  654. a_b[1] = tochar (3);
  655. a_b[2] = tochar (n);
  656. a_b[3] = ACK_TYPE;
  657. a_b[4] = '\0';
  658. a_b[4] = tochar (chk1 (&a_b[1]));
  659. a_b[5] = his_eol;
  660. a_b[6] = '\0';
  661. s1_sendpacket (a_b);
  662. }
  663. void send_nack (int n)
  664. {
  665. a_b[0] = START_CHAR;
  666. a_b[1] = tochar (3);
  667. a_b[2] = tochar (n);
  668. a_b[3] = NACK_TYPE;
  669. a_b[4] = '\0';
  670. a_b[4] = tochar (chk1 (&a_b[1]));
  671. a_b[5] = his_eol;
  672. a_b[6] = '\0';
  673. s1_sendpacket (a_b);
  674. }
  675. /* os_data_* takes an OS Open image and puts it into memory, and
  676. puts the boot header in an array named os_data_header
  677. if image is binary, no header is stored in os_data_header.
  678. */
  679. void (*os_data_init) (void);
  680. void (*os_data_char) (char new_char);
  681. static int os_data_state, os_data_state_saved;
  682. int os_data_count;
  683. static int os_data_count_saved;
  684. static char *os_data_addr, *os_data_addr_saved;
  685. static char *bin_start_address;
  686. int os_data_header[8];
  687. static void bin_data_init (void)
  688. {
  689. os_data_state = 0;
  690. os_data_count = 0;
  691. os_data_addr = bin_start_address;
  692. }
  693. static void os_data_save (void)
  694. {
  695. os_data_state_saved = os_data_state;
  696. os_data_count_saved = os_data_count;
  697. os_data_addr_saved = os_data_addr;
  698. }
  699. static void os_data_restore (void)
  700. {
  701. os_data_state = os_data_state_saved;
  702. os_data_count = os_data_count_saved;
  703. os_data_addr = os_data_addr_saved;
  704. }
  705. static void bin_data_char (char new_char)
  706. {
  707. switch (os_data_state) {
  708. case 0: /* data */
  709. *os_data_addr++ = new_char;
  710. --os_data_count;
  711. break;
  712. }
  713. }
  714. static void set_kerm_bin_mode (unsigned long *addr)
  715. {
  716. bin_start_address = (char *) addr;
  717. os_data_init = bin_data_init;
  718. os_data_char = bin_data_char;
  719. }
  720. /* k_data_* simply handles the kermit escape translations */
  721. static int k_data_escape, k_data_escape_saved;
  722. void k_data_init (void)
  723. {
  724. k_data_escape = 0;
  725. os_data_init ();
  726. }
  727. void k_data_save (void)
  728. {
  729. k_data_escape_saved = k_data_escape;
  730. os_data_save ();
  731. }
  732. void k_data_restore (void)
  733. {
  734. k_data_escape = k_data_escape_saved;
  735. os_data_restore ();
  736. }
  737. void k_data_char (char new_char)
  738. {
  739. if (k_data_escape) {
  740. /* last char was escape - translate this character */
  741. os_data_char (ktrans (new_char));
  742. k_data_escape = 0;
  743. } else {
  744. if (new_char == his_quote) {
  745. /* this char is escape - remember */
  746. k_data_escape = 1;
  747. } else {
  748. /* otherwise send this char as-is */
  749. os_data_char (new_char);
  750. }
  751. }
  752. }
  753. #define SEND_DATA_SIZE 20
  754. char send_parms[SEND_DATA_SIZE];
  755. char *send_ptr;
  756. /* handle_send_packet interprits the protocol info and builds and
  757. sends an appropriate ack for what we can do */
  758. void handle_send_packet (int n)
  759. {
  760. int length = 3;
  761. int bytes;
  762. /* initialize some protocol parameters */
  763. his_eol = END_CHAR; /* default end of line character */
  764. his_pad_count = 0;
  765. his_pad_char = '\0';
  766. his_quote = K_ESCAPE;
  767. /* ignore last character if it filled the buffer */
  768. if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
  769. --send_ptr;
  770. bytes = send_ptr - send_parms; /* how many bytes we'll process */
  771. do {
  772. if (bytes-- <= 0)
  773. break;
  774. /* handle MAXL - max length */
  775. /* ignore what he says - most I'll take (here) is 94 */
  776. a_b[++length] = tochar (94);
  777. if (bytes-- <= 0)
  778. break;
  779. /* handle TIME - time you should wait for my packets */
  780. /* ignore what he says - don't wait for my ack longer than 1 second */
  781. a_b[++length] = tochar (1);
  782. if (bytes-- <= 0)
  783. break;
  784. /* handle NPAD - number of pad chars I need */
  785. /* remember what he says - I need none */
  786. his_pad_count = untochar (send_parms[2]);
  787. a_b[++length] = tochar (0);
  788. if (bytes-- <= 0)
  789. break;
  790. /* handle PADC - pad chars I need */
  791. /* remember what he says - I need none */
  792. his_pad_char = ktrans (send_parms[3]);
  793. a_b[++length] = 0x40; /* He should ignore this */
  794. if (bytes-- <= 0)
  795. break;
  796. /* handle EOL - end of line he needs */
  797. /* remember what he says - I need CR */
  798. his_eol = untochar (send_parms[4]);
  799. a_b[++length] = tochar (END_CHAR);
  800. if (bytes-- <= 0)
  801. break;
  802. /* handle QCTL - quote control char he'll use */
  803. /* remember what he says - I'll use '#' */
  804. his_quote = send_parms[5];
  805. a_b[++length] = '#';
  806. if (bytes-- <= 0)
  807. break;
  808. /* handle QBIN - 8-th bit prefixing */
  809. /* ignore what he says - I refuse */
  810. a_b[++length] = 'N';
  811. if (bytes-- <= 0)
  812. break;
  813. /* handle CHKT - the clock check type */
  814. /* ignore what he says - I do type 1 (for now) */
  815. a_b[++length] = '1';
  816. if (bytes-- <= 0)
  817. break;
  818. /* handle REPT - the repeat prefix */
  819. /* ignore what he says - I refuse (for now) */
  820. a_b[++length] = 'N';
  821. if (bytes-- <= 0)
  822. break;
  823. /* handle CAPAS - the capabilities mask */
  824. /* ignore what he says - I only do long packets - I don't do windows */
  825. a_b[++length] = tochar (2); /* only long packets */
  826. a_b[++length] = tochar (0); /* no windows */
  827. a_b[++length] = tochar (94); /* large packet msb */
  828. a_b[++length] = tochar (94); /* large packet lsb */
  829. } while (0);
  830. a_b[0] = START_CHAR;
  831. a_b[1] = tochar (length);
  832. a_b[2] = tochar (n);
  833. a_b[3] = ACK_TYPE;
  834. a_b[++length] = '\0';
  835. a_b[length] = tochar (chk1 (&a_b[1]));
  836. a_b[++length] = his_eol;
  837. a_b[++length] = '\0';
  838. s1_sendpacket (a_b);
  839. }
  840. /* k_recv receives a OS Open image file over kermit line */
  841. static int k_recv (void)
  842. {
  843. char new_char;
  844. char k_state, k_state_saved;
  845. int sum;
  846. int done;
  847. int length;
  848. int n, last_n;
  849. int z = 0;
  850. int len_lo, len_hi;
  851. /* initialize some protocol parameters */
  852. his_eol = END_CHAR; /* default end of line character */
  853. his_pad_count = 0;
  854. his_pad_char = '\0';
  855. his_quote = K_ESCAPE;
  856. /* initialize the k_recv and k_data state machine */
  857. done = 0;
  858. k_state = 0;
  859. k_data_init ();
  860. k_state_saved = k_state;
  861. k_data_save ();
  862. n = 0; /* just to get rid of a warning */
  863. last_n = -1;
  864. /* expect this "type" sequence (but don't check):
  865. S: send initiate
  866. F: file header
  867. D: data (multiple)
  868. Z: end of file
  869. B: break transmission
  870. */
  871. /* enter main loop */
  872. while (!done) {
  873. /* set the send packet pointer to begining of send packet parms */
  874. send_ptr = send_parms;
  875. /* With each packet, start summing the bytes starting with the length.
  876. Save the current sequence number.
  877. Note the type of the packet.
  878. If a character less than SPACE (0x20) is received - error.
  879. */
  880. #if 0
  881. /* OLD CODE, Prior to checking sequence numbers */
  882. /* first have all state machines save current states */
  883. k_state_saved = k_state;
  884. k_data_save ();
  885. #endif
  886. /* get a packet */
  887. /* wait for the starting character or ^C */
  888. for (;;) {
  889. switch (serial_getc ()) {
  890. case START_CHAR: /* start packet */
  891. goto START;
  892. case ETX_CHAR: /* ^C waiting for packet */
  893. return (0);
  894. default:
  895. ;
  896. }
  897. }
  898. START:
  899. /* get length of packet */
  900. sum = 0;
  901. new_char = serial_getc ();
  902. if ((new_char & 0xE0) == 0)
  903. goto packet_error;
  904. sum += new_char & 0xff;
  905. length = untochar (new_char);
  906. /* get sequence number */
  907. new_char = serial_getc ();
  908. if ((new_char & 0xE0) == 0)
  909. goto packet_error;
  910. sum += new_char & 0xff;
  911. n = untochar (new_char);
  912. --length;
  913. /* NEW CODE - check sequence numbers for retried packets */
  914. /* Note - this new code assumes that the sequence number is correctly
  915. * received. Handling an invalid sequence number adds another layer
  916. * of complexity that may not be needed - yet! At this time, I'm hoping
  917. * that I don't need to buffer the incoming data packets and can write
  918. * the data into memory in real time.
  919. */
  920. if (n == last_n) {
  921. /* same sequence number, restore the previous state */
  922. k_state = k_state_saved;
  923. k_data_restore ();
  924. } else {
  925. /* new sequence number, checkpoint the download */
  926. last_n = n;
  927. k_state_saved = k_state;
  928. k_data_save ();
  929. }
  930. /* END NEW CODE */
  931. /* get packet type */
  932. new_char = serial_getc ();
  933. if ((new_char & 0xE0) == 0)
  934. goto packet_error;
  935. sum += new_char & 0xff;
  936. k_state = new_char;
  937. --length;
  938. /* check for extended length */
  939. if (length == -2) {
  940. /* (length byte was 0, decremented twice) */
  941. /* get the two length bytes */
  942. new_char = serial_getc ();
  943. if ((new_char & 0xE0) == 0)
  944. goto packet_error;
  945. sum += new_char & 0xff;
  946. len_hi = untochar (new_char);
  947. new_char = serial_getc ();
  948. if ((new_char & 0xE0) == 0)
  949. goto packet_error;
  950. sum += new_char & 0xff;
  951. len_lo = untochar (new_char);
  952. length = len_hi * 95 + len_lo;
  953. /* check header checksum */
  954. new_char = serial_getc ();
  955. if ((new_char & 0xE0) == 0)
  956. goto packet_error;
  957. if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
  958. goto packet_error;
  959. sum += new_char & 0xff;
  960. /* --length; */ /* new length includes only data and block check to come */
  961. }
  962. /* bring in rest of packet */
  963. while (length > 1) {
  964. new_char = serial_getc ();
  965. if ((new_char & 0xE0) == 0)
  966. goto packet_error;
  967. sum += new_char & 0xff;
  968. --length;
  969. if (k_state == DATA_TYPE) {
  970. /* pass on the data if this is a data packet */
  971. k_data_char (new_char);
  972. } else if (k_state == SEND_TYPE) {
  973. /* save send pack in buffer as is */
  974. *send_ptr++ = new_char;
  975. /* if too much data, back off the pointer */
  976. if (send_ptr >= &send_parms[SEND_DATA_SIZE])
  977. --send_ptr;
  978. }
  979. }
  980. /* get and validate checksum character */
  981. new_char = serial_getc ();
  982. if ((new_char & 0xE0) == 0)
  983. goto packet_error;
  984. if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
  985. goto packet_error;
  986. /* get END_CHAR */
  987. new_char = serial_getc ();
  988. if (new_char != END_CHAR) {
  989. packet_error:
  990. /* restore state machines */
  991. k_state = k_state_saved;
  992. k_data_restore ();
  993. /* send a negative acknowledge packet in */
  994. send_nack (n);
  995. } else if (k_state == SEND_TYPE) {
  996. /* crack the protocol parms, build an appropriate ack packet */
  997. handle_send_packet (n);
  998. } else {
  999. /* send simple acknowledge packet in */
  1000. send_ack (n);
  1001. /* quit if end of transmission */
  1002. if (k_state == BREAK_TYPE)
  1003. done = 1;
  1004. }
  1005. ++z;
  1006. }
  1007. return ((ulong) os_data_addr - (ulong) bin_start_address);
  1008. }
  1009. #endif /* CFG_CMD_LOADB */
  1010. #if (CONFIG_COMMANDS & CFG_CMD_HWFLOW)
  1011. int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1012. {
  1013. extern int hwflow_onoff(int);
  1014. if (argc == 2) {
  1015. if (strcmp(argv[1], "off") == 0)
  1016. hwflow_onoff(-1);
  1017. else
  1018. if (strcmp(argv[1], "on") == 0)
  1019. hwflow_onoff(1);
  1020. else
  1021. printf("Usage: %s\n", cmdtp->usage);
  1022. }
  1023. printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
  1024. return 0;
  1025. }
  1026. #endif /* CFG_CMD_HWFLOW */