cmd_boot.c 27 KB

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