load.c 25 KB

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