load.c 27 KB

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