load.c 25 KB

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