cmd_boot.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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_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)
  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)
  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. if (addr2info(store_addr)) {
  288. int rc;
  289. rc = flash_write((uchar *)binbuf,store_addr,binlen);
  290. if (rc != 0) {
  291. flash_perror (rc);
  292. return (~0);
  293. }
  294. } else {
  295. memcpy ((char *)(store_addr), binbuf, binlen);
  296. }
  297. if ((store_addr) < start_addr)
  298. start_addr = store_addr;
  299. if ((store_addr + binlen - 1) > end_addr)
  300. end_addr = store_addr + binlen - 1;
  301. break;
  302. case SREC_END2:
  303. case SREC_END3:
  304. case SREC_END4:
  305. udelay (10000);
  306. size = end_addr - start_addr + 1;
  307. printf ("\n"
  308. "## First Load Addr = 0x%08lX\n"
  309. "## Last Load Addr = 0x%08lX\n"
  310. "## Total Size = 0x%08lX = %ld Bytes\n",
  311. start_addr, end_addr, size, size
  312. );
  313. flush_cache (addr, size);
  314. sprintf(buf, "%lX", size);
  315. setenv("filesize", buf);
  316. return (addr);
  317. case SREC_START:
  318. break;
  319. default:
  320. break;
  321. }
  322. if (!do_echo) { /* print a '.' every 100 lines */
  323. if ((++line_count % 100) == 0)
  324. putc ('.');
  325. }
  326. }
  327. return (~0); /* Download aborted */
  328. }
  329. static int
  330. read_record (char *buf, ulong len)
  331. {
  332. char *p;
  333. char c;
  334. --len; /* always leave room for terminating '\0' byte */
  335. for (p=buf; p < buf+len; ++p) {
  336. c = serial_getc(); /* read character */
  337. if (do_echo)
  338. serial_putc (c); /* ... and echo it */
  339. switch (c) {
  340. case '\r':
  341. case '\n':
  342. *p = '\0';
  343. return (p - buf);
  344. case '\0':
  345. case 0x03: /* ^C - Control C */
  346. return (-1);
  347. default:
  348. *p = c;
  349. }
  350. /* Check for the console hangup (if any different from serial) */
  351. if (syscall_tbl[SYSCALL_GETC] != serial_getc) {
  352. if (ctrlc()) {
  353. return (-1);
  354. }
  355. }
  356. }
  357. /* line too long - truncate */
  358. *p = '\0';
  359. return (p - buf);
  360. }
  361. #if (CONFIG_COMMANDS & CFG_CMD_SAVES)
  362. int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  363. {
  364. ulong offset = 0;
  365. ulong size = 0;
  366. #ifdef CFG_LOADS_BAUD_CHANGE
  367. DECLARE_GLOBAL_DATA_PTR;
  368. int save_baudrate, current_baudrate;
  369. save_baudrate = current_baudrate = gd->baudrate;
  370. #endif
  371. if (argc >= 2) {
  372. offset = simple_strtoul(argv[1], NULL, 16);
  373. }
  374. #ifdef CFG_LOADS_BAUD_CHANGE
  375. if (argc >= 3) {
  376. size = simple_strtoul(argv[2], NULL, 16);
  377. }
  378. if (argc == 4) {
  379. save_baudrate = (int)simple_strtoul(argv[3], NULL, 10);
  380. /* default to current baudrate */
  381. if (save_baudrate == 0)
  382. save_baudrate = current_baudrate;
  383. }
  384. #else /* ! CFG_LOADS_BAUD_CHANGE */
  385. if (argc == 3) {
  386. size = simple_strtoul(argv[2], NULL, 16);
  387. }
  388. #endif /* CFG_LOADS_BAUD_CHANGE */
  389. #ifdef CFG_LOADS_BAUD_CHANGE
  390. if (save_baudrate != current_baudrate) {
  391. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  392. save_baudrate);
  393. udelay(50000);
  394. gd->baudrate = save_baudrate;
  395. serial_setbrg ();
  396. udelay(50000);
  397. for (;;) {
  398. if (getc() == '\r')
  399. break;
  400. }
  401. }
  402. #endif /* CFG_LOADS_BAUD_CHANGE */
  403. printf ("## Ready for S-Record upload, press ENTER to proceed ...\n");
  404. for (;;) {
  405. if (getc() == '\r')
  406. break;
  407. }
  408. if(save_serial (offset, size)) {
  409. printf ("## S-Record upload aborted\n");
  410. } else {
  411. printf ("## S-Record upload complete\n");
  412. }
  413. #ifdef CFG_LOADS_BAUD_CHANGE
  414. if (save_baudrate != current_baudrate) {
  415. printf ("## Switch baudrate to %d bps and press ESC ...\n",
  416. (int)current_baudrate);
  417. udelay (50000);
  418. gd->baudrate = current_baudrate;
  419. serial_setbrg ();
  420. udelay (50000);
  421. for (;;) {
  422. if (getc() == 0x1B) /* ESC */
  423. break;
  424. }
  425. }
  426. #endif
  427. return 0;
  428. }
  429. #define SREC3_START "S0030000FC\n"
  430. #define SREC3_FORMAT "S3%02X%08lX%s%02X\n"
  431. #define SREC3_END "S70500000000FA\n"
  432. #define SREC_BYTES_PER_RECORD 16
  433. static int save_serial (ulong address, ulong count)
  434. {
  435. int i, c, reclen, checksum, length;
  436. char *hex = "0123456789ABCDEF";
  437. char record[2*SREC_BYTES_PER_RECORD+16]; /* buffer for one S-Record */
  438. char data[2*SREC_BYTES_PER_RECORD+1]; /* buffer for hex data */
  439. reclen = 0;
  440. checksum = 0;
  441. if(write_record(SREC3_START)) /* write the header */
  442. return (-1);
  443. do {
  444. if(count) { /* collect hex data in the buffer */
  445. c = *(volatile uchar*)(address + reclen); /* get one byte */
  446. checksum += c; /* accumulate checksum */
  447. data[2*reclen] = hex[(c>>4)&0x0f];
  448. data[2*reclen+1] = hex[c & 0x0f];
  449. data[2*reclen+2] = '\0';
  450. ++reclen;
  451. --count;
  452. }
  453. if(reclen == SREC_BYTES_PER_RECORD || count == 0) {
  454. /* enough data collected for one record: dump it */
  455. if(reclen) { /* build & write a data record: */
  456. /* address + data + checksum */
  457. length = 4 + reclen + 1;
  458. /* accumulate length bytes into checksum */
  459. for(i = 0; i < 2; i++)
  460. checksum += (length >> (8*i)) & 0xff;
  461. /* accumulate address bytes into checksum: */
  462. for(i = 0; i < 4; i++)
  463. checksum += (address >> (8*i)) & 0xff;
  464. /* make proper checksum byte: */
  465. checksum = ~checksum & 0xff;
  466. /* output one record: */
  467. sprintf(record, SREC3_FORMAT, length, address, data, checksum);
  468. if(write_record(record))
  469. return (-1);
  470. }
  471. address += reclen; /* increment address */
  472. checksum = 0;
  473. reclen = 0;
  474. }
  475. }
  476. while(count);
  477. if(write_record(SREC3_END)) /* write the final record */
  478. return (-1);
  479. return(0);
  480. }
  481. static int
  482. write_record (char *buf)
  483. {
  484. char c;
  485. while((c = *buf++))
  486. serial_putc(c);
  487. /* Check for the console hangup (if any different from serial) */
  488. if (ctrlc()) {
  489. return (-1);
  490. }
  491. return (0);
  492. }
  493. # endif /* CFG_CMD_SAVES */
  494. #endif /* CFG_CMD_LOADS */
  495. #if (CONFIG_COMMANDS & CFG_CMD_LOADB) /* loadb command (load binary) included */
  496. #define XON_CHAR 17
  497. #define XOFF_CHAR 19
  498. #define START_CHAR 0x01
  499. #define ETX_CHAR 0x03
  500. #define END_CHAR 0x0D
  501. #define SPACE 0x20
  502. #define K_ESCAPE 0x23
  503. #define SEND_TYPE 'S'
  504. #define DATA_TYPE 'D'
  505. #define ACK_TYPE 'Y'
  506. #define NACK_TYPE 'N'
  507. #define BREAK_TYPE 'B'
  508. #define tochar(x) ((char) (((x) + SPACE) & 0xff))
  509. #define untochar(x) ((int) (((x) - SPACE) & 0xff))
  510. extern int os_data_count;
  511. extern int os_data_header[8];
  512. static void set_kerm_bin_mode(unsigned long *);
  513. static int k_recv(void);
  514. static ulong load_serial_bin (ulong offset);
  515. char his_eol; /* character he needs at end of packet */
  516. int his_pad_count; /* number of pad chars he needs */
  517. char his_pad_char; /* pad chars he needs */
  518. char his_quote; /* quote chars he'll use */
  519. int do_load_serial_bin (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  520. {
  521. DECLARE_GLOBAL_DATA_PTR;
  522. ulong offset = 0;
  523. ulong addr;
  524. int load_baudrate, current_baudrate;
  525. int rcode = 0;
  526. char *s;
  527. /* pre-set offset from CFG_LOAD_ADDR */
  528. offset = CFG_LOAD_ADDR;
  529. /* pre-set offset from $loadaddr */
  530. if ((s = getenv("loadaddr")) != NULL) {
  531. offset = simple_strtoul(s, NULL, 16);
  532. }
  533. load_baudrate = current_baudrate = gd->baudrate;
  534. if (argc >= 2) {
  535. offset = simple_strtoul(argv[1], NULL, 16);
  536. }
  537. if (argc == 3) {
  538. load_baudrate = (int)simple_strtoul(argv[2], NULL, 10);
  539. /* default to current baudrate */
  540. if (load_baudrate == 0)
  541. load_baudrate = current_baudrate;
  542. }
  543. if (load_baudrate != current_baudrate) {
  544. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  545. load_baudrate);
  546. udelay(50000);
  547. gd->baudrate = load_baudrate;
  548. serial_setbrg ();
  549. udelay(50000);
  550. for (;;) {
  551. if (getc() == '\r')
  552. break;
  553. }
  554. }
  555. printf ("## Ready for binary (kermit) download "
  556. "to 0x%08lX at %d bps...\n",
  557. offset,
  558. current_baudrate);
  559. addr = load_serial_bin (offset);
  560. if (addr == ~0) {
  561. load_addr = 0;
  562. printf ("## Binary (kermit) download aborted\n");
  563. rcode = 1;
  564. } else {
  565. printf ("## Start Addr = 0x%08lX\n", addr);
  566. load_addr = addr;
  567. }
  568. if (load_baudrate != current_baudrate) {
  569. printf ("## Switch baudrate to %d bps and press ESC ...\n",
  570. current_baudrate);
  571. udelay (50000);
  572. gd->baudrate = current_baudrate;
  573. serial_setbrg ();
  574. udelay (50000);
  575. for (;;) {
  576. if (getc() == 0x1B) /* ESC */
  577. break;
  578. }
  579. }
  580. #ifdef CONFIG_AUTOSCRIPT
  581. if (load_addr) {
  582. char *s;
  583. if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
  584. printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
  585. rcode = autoscript (load_addr);
  586. }
  587. }
  588. #endif
  589. return rcode;
  590. }
  591. static ulong load_serial_bin (ulong offset)
  592. {
  593. int size;
  594. char buf[32];
  595. set_kerm_bin_mode ((ulong *) offset);
  596. size = k_recv ();
  597. flush_cache (offset, size);
  598. printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
  599. sprintf(buf, "%X", size);
  600. setenv("filesize", buf);
  601. return offset;
  602. }
  603. void send_pad (void)
  604. {
  605. int count = his_pad_count;
  606. while (count-- > 0)
  607. serial_putc (his_pad_char);
  608. }
  609. /* converts escaped kermit char to binary char */
  610. char ktrans (char in)
  611. {
  612. if ((in & 0x60) == 0x40) {
  613. return (char) (in & ~0x40);
  614. } else if ((in & 0x7f) == 0x3f) {
  615. return (char) (in | 0x40);
  616. } else
  617. return in;
  618. }
  619. int chk1 (char *buffer)
  620. {
  621. int total = 0;
  622. while (*buffer) {
  623. total += *buffer++;
  624. }
  625. return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
  626. }
  627. void s1_sendpacket (char *packet)
  628. {
  629. send_pad ();
  630. while (*packet) {
  631. serial_putc (*packet++);
  632. }
  633. }
  634. static char a_b[24];
  635. void send_ack (int n)
  636. {
  637. a_b[0] = START_CHAR;
  638. a_b[1] = tochar (3);
  639. a_b[2] = tochar (n);
  640. a_b[3] = ACK_TYPE;
  641. a_b[4] = '\0';
  642. a_b[4] = tochar (chk1 (&a_b[1]));
  643. a_b[5] = his_eol;
  644. a_b[6] = '\0';
  645. s1_sendpacket (a_b);
  646. }
  647. void send_nack (int n)
  648. {
  649. a_b[0] = START_CHAR;
  650. a_b[1] = tochar (3);
  651. a_b[2] = tochar (n);
  652. a_b[3] = NACK_TYPE;
  653. a_b[4] = '\0';
  654. a_b[4] = tochar (chk1 (&a_b[1]));
  655. a_b[5] = his_eol;
  656. a_b[6] = '\0';
  657. s1_sendpacket (a_b);
  658. }
  659. /* os_data_* takes an OS Open image and puts it into memory, and
  660. puts the boot header in an array named os_data_header
  661. if image is binary, no header is stored in os_data_header.
  662. */
  663. void (*os_data_init) (void);
  664. void (*os_data_char) (char new_char);
  665. static int os_data_state, os_data_state_saved;
  666. int os_data_count;
  667. static int os_data_count_saved;
  668. static char *os_data_addr, *os_data_addr_saved;
  669. static char *bin_start_address;
  670. int os_data_header[8];
  671. static void bin_data_init (void)
  672. {
  673. os_data_state = 0;
  674. os_data_count = 0;
  675. os_data_addr = bin_start_address;
  676. }
  677. static void os_data_save (void)
  678. {
  679. os_data_state_saved = os_data_state;
  680. os_data_count_saved = os_data_count;
  681. os_data_addr_saved = os_data_addr;
  682. }
  683. static void os_data_restore (void)
  684. {
  685. os_data_state = os_data_state_saved;
  686. os_data_count = os_data_count_saved;
  687. os_data_addr = os_data_addr_saved;
  688. }
  689. static void bin_data_char (char new_char)
  690. {
  691. switch (os_data_state) {
  692. case 0: /* data */
  693. *os_data_addr++ = new_char;
  694. --os_data_count;
  695. break;
  696. }
  697. }
  698. static void set_kerm_bin_mode (unsigned long *addr)
  699. {
  700. bin_start_address = (char *) addr;
  701. os_data_init = bin_data_init;
  702. os_data_char = bin_data_char;
  703. }
  704. /* k_data_* simply handles the kermit escape translations */
  705. static int k_data_escape, k_data_escape_saved;
  706. void k_data_init (void)
  707. {
  708. k_data_escape = 0;
  709. os_data_init ();
  710. }
  711. void k_data_save (void)
  712. {
  713. k_data_escape_saved = k_data_escape;
  714. os_data_save ();
  715. }
  716. void k_data_restore (void)
  717. {
  718. k_data_escape = k_data_escape_saved;
  719. os_data_restore ();
  720. }
  721. void k_data_char (char new_char)
  722. {
  723. if (k_data_escape) {
  724. /* last char was escape - translate this character */
  725. os_data_char (ktrans (new_char));
  726. k_data_escape = 0;
  727. } else {
  728. if (new_char == his_quote) {
  729. /* this char is escape - remember */
  730. k_data_escape = 1;
  731. } else {
  732. /* otherwise send this char as-is */
  733. os_data_char (new_char);
  734. }
  735. }
  736. }
  737. #define SEND_DATA_SIZE 20
  738. char send_parms[SEND_DATA_SIZE];
  739. char *send_ptr;
  740. /* handle_send_packet interprits the protocol info and builds and
  741. sends an appropriate ack for what we can do */
  742. void handle_send_packet (int n)
  743. {
  744. int length = 3;
  745. int bytes;
  746. /* initialize some protocol parameters */
  747. his_eol = END_CHAR; /* default end of line character */
  748. his_pad_count = 0;
  749. his_pad_char = '\0';
  750. his_quote = K_ESCAPE;
  751. /* ignore last character if it filled the buffer */
  752. if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
  753. --send_ptr;
  754. bytes = send_ptr - send_parms; /* how many bytes we'll process */
  755. do {
  756. if (bytes-- <= 0)
  757. break;
  758. /* handle MAXL - max length */
  759. /* ignore what he says - most I'll take (here) is 94 */
  760. a_b[++length] = tochar (94);
  761. if (bytes-- <= 0)
  762. break;
  763. /* handle TIME - time you should wait for my packets */
  764. /* ignore what he says - don't wait for my ack longer than 1 second */
  765. a_b[++length] = tochar (1);
  766. if (bytes-- <= 0)
  767. break;
  768. /* handle NPAD - number of pad chars I need */
  769. /* remember what he says - I need none */
  770. his_pad_count = untochar (send_parms[2]);
  771. a_b[++length] = tochar (0);
  772. if (bytes-- <= 0)
  773. break;
  774. /* handle PADC - pad chars I need */
  775. /* remember what he says - I need none */
  776. his_pad_char = ktrans (send_parms[3]);
  777. a_b[++length] = 0x40; /* He should ignore this */
  778. if (bytes-- <= 0)
  779. break;
  780. /* handle EOL - end of line he needs */
  781. /* remember what he says - I need CR */
  782. his_eol = untochar (send_parms[4]);
  783. a_b[++length] = tochar (END_CHAR);
  784. if (bytes-- <= 0)
  785. break;
  786. /* handle QCTL - quote control char he'll use */
  787. /* remember what he says - I'll use '#' */
  788. his_quote = send_parms[5];
  789. a_b[++length] = '#';
  790. if (bytes-- <= 0)
  791. break;
  792. /* handle QBIN - 8-th bit prefixing */
  793. /* ignore what he says - I refuse */
  794. a_b[++length] = 'N';
  795. if (bytes-- <= 0)
  796. break;
  797. /* handle CHKT - the clock check type */
  798. /* ignore what he says - I do type 1 (for now) */
  799. a_b[++length] = '1';
  800. if (bytes-- <= 0)
  801. break;
  802. /* handle REPT - the repeat prefix */
  803. /* ignore what he says - I refuse (for now) */
  804. a_b[++length] = 'N';
  805. if (bytes-- <= 0)
  806. break;
  807. /* handle CAPAS - the capabilities mask */
  808. /* ignore what he says - I only do long packets - I don't do windows */
  809. a_b[++length] = tochar (2); /* only long packets */
  810. a_b[++length] = tochar (0); /* no windows */
  811. a_b[++length] = tochar (94); /* large packet msb */
  812. a_b[++length] = tochar (94); /* large packet lsb */
  813. } while (0);
  814. a_b[0] = START_CHAR;
  815. a_b[1] = tochar (length);
  816. a_b[2] = tochar (n);
  817. a_b[3] = ACK_TYPE;
  818. a_b[++length] = '\0';
  819. a_b[length] = tochar (chk1 (&a_b[1]));
  820. a_b[++length] = his_eol;
  821. a_b[++length] = '\0';
  822. s1_sendpacket (a_b);
  823. }
  824. /* k_recv receives a OS Open image file over kermit line */
  825. static int k_recv (void)
  826. {
  827. char new_char;
  828. char k_state, k_state_saved;
  829. int sum;
  830. int done;
  831. int length;
  832. int n, last_n;
  833. int z = 0;
  834. int len_lo, len_hi;
  835. /* initialize some protocol parameters */
  836. his_eol = END_CHAR; /* default end of line character */
  837. his_pad_count = 0;
  838. his_pad_char = '\0';
  839. his_quote = K_ESCAPE;
  840. /* initialize the k_recv and k_data state machine */
  841. done = 0;
  842. k_state = 0;
  843. k_data_init ();
  844. k_state_saved = k_state;
  845. k_data_save ();
  846. n = 0; /* just to get rid of a warning */
  847. last_n = -1;
  848. /* expect this "type" sequence (but don't check):
  849. S: send initiate
  850. F: file header
  851. D: data (multiple)
  852. Z: end of file
  853. B: break transmission
  854. */
  855. /* enter main loop */
  856. while (!done) {
  857. /* set the send packet pointer to begining of send packet parms */
  858. send_ptr = send_parms;
  859. /* With each packet, start summing the bytes starting with the length.
  860. Save the current sequence number.
  861. Note the type of the packet.
  862. If a character less than SPACE (0x20) is received - error.
  863. */
  864. #if 0
  865. /* OLD CODE, Prior to checking sequence numbers */
  866. /* first have all state machines save current states */
  867. k_state_saved = k_state;
  868. k_data_save ();
  869. #endif
  870. /* get a packet */
  871. /* wait for the starting character or ^C */
  872. for (;;) {
  873. switch (serial_getc ()) {
  874. case START_CHAR: /* start packet */
  875. break;
  876. case ETX_CHAR: /* ^C waiting for packet */
  877. return (0);
  878. default:
  879. ;
  880. }
  881. }
  882. /* get length of packet */
  883. sum = 0;
  884. new_char = serial_getc ();
  885. if ((new_char & 0xE0) == 0)
  886. goto packet_error;
  887. sum += new_char & 0xff;
  888. length = untochar (new_char);
  889. /* get sequence number */
  890. new_char = serial_getc ();
  891. if ((new_char & 0xE0) == 0)
  892. goto packet_error;
  893. sum += new_char & 0xff;
  894. n = untochar (new_char);
  895. --length;
  896. /* NEW CODE - check sequence numbers for retried packets */
  897. /* Note - this new code assumes that the sequence number is correctly
  898. * received. Handling an invalid sequence number adds another layer
  899. * of complexity that may not be needed - yet! At this time, I'm hoping
  900. * that I don't need to buffer the incoming data packets and can write
  901. * the data into memory in real time.
  902. */
  903. if (n == last_n) {
  904. /* same sequence number, restore the previous state */
  905. k_state = k_state_saved;
  906. k_data_restore ();
  907. } else {
  908. /* new sequence number, checkpoint the download */
  909. last_n = n;
  910. k_state_saved = k_state;
  911. k_data_save ();
  912. }
  913. /* END NEW CODE */
  914. /* get packet type */
  915. new_char = serial_getc ();
  916. if ((new_char & 0xE0) == 0)
  917. goto packet_error;
  918. sum += new_char & 0xff;
  919. k_state = new_char;
  920. --length;
  921. /* check for extended length */
  922. if (length == -2) {
  923. /* (length byte was 0, decremented twice) */
  924. /* get the two length bytes */
  925. new_char = serial_getc ();
  926. if ((new_char & 0xE0) == 0)
  927. goto packet_error;
  928. sum += new_char & 0xff;
  929. len_hi = untochar (new_char);
  930. new_char = serial_getc ();
  931. if ((new_char & 0xE0) == 0)
  932. goto packet_error;
  933. sum += new_char & 0xff;
  934. len_lo = untochar (new_char);
  935. length = len_hi * 95 + len_lo;
  936. /* check header checksum */
  937. new_char = serial_getc ();
  938. if ((new_char & 0xE0) == 0)
  939. goto packet_error;
  940. if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
  941. goto packet_error;
  942. sum += new_char & 0xff;
  943. /* --length; */ /* new length includes only data and block check to come */
  944. }
  945. /* bring in rest of packet */
  946. while (length > 1) {
  947. new_char = serial_getc ();
  948. if ((new_char & 0xE0) == 0)
  949. goto packet_error;
  950. sum += new_char & 0xff;
  951. --length;
  952. if (k_state == DATA_TYPE) {
  953. /* pass on the data if this is a data packet */
  954. k_data_char (new_char);
  955. } else if (k_state == SEND_TYPE) {
  956. /* save send pack in buffer as is */
  957. *send_ptr++ = new_char;
  958. /* if too much data, back off the pointer */
  959. if (send_ptr >= &send_parms[SEND_DATA_SIZE])
  960. --send_ptr;
  961. }
  962. }
  963. /* get and validate checksum character */
  964. new_char = serial_getc ();
  965. if ((new_char & 0xE0) == 0)
  966. goto packet_error;
  967. if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
  968. goto packet_error;
  969. /* get END_CHAR */
  970. new_char = serial_getc ();
  971. if (new_char != END_CHAR) {
  972. packet_error:
  973. /* restore state machines */
  974. k_state = k_state_saved;
  975. k_data_restore ();
  976. /* send a negative acknowledge packet in */
  977. send_nack (n);
  978. } else if (k_state == SEND_TYPE) {
  979. /* crack the protocol parms, build an appropriate ack packet */
  980. handle_send_packet (n);
  981. } else {
  982. /* send simple acknowledge packet in */
  983. send_ack (n);
  984. /* quit if end of transmission */
  985. if (k_state == BREAK_TYPE)
  986. done = 1;
  987. }
  988. ++z;
  989. }
  990. return ((ulong) os_data_addr - (ulong) bin_start_address);
  991. }
  992. #endif /* CFG_CMD_LOADB */
  993. #if (CONFIG_COMMANDS & CFG_CMD_HWFLOW)
  994. int do_hwflow (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  995. {
  996. extern int hwflow_onoff(int);
  997. if (argc == 2) {
  998. if (strcmp(argv[1], "off") == 0)
  999. hwflow_onoff(-1);
  1000. else
  1001. if (strcmp(argv[1], "on") == 0)
  1002. hwflow_onoff(1);
  1003. else
  1004. printf("Usage: %s\n", cmdtp->usage);
  1005. }
  1006. printf("RTS/CTS hardware flow control: %s\n", hwflow_onoff(0) ? "on" : "off");
  1007. return 0;
  1008. }
  1009. #endif /* CFG_CMD_HWFLOW */