load.c 25 KB

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