cmd_boot.c 27 KB

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