load.c 26 KB

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