load.c 26 KB

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