load.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. } else if (strcmp(argv[0],"loadx")==0) {
  416. printf("## Ready for binary (xmodem) download "
  417. "to 0x%08lX at %d bps...\n",
  418. offset,
  419. load_baudrate);
  420. addr = load_serial_ymodem(offset, xyzModem_xmodem);
  421. } else {
  422. printf("## Ready for binary (kermit) download "
  423. "to 0x%08lX at %d bps...\n",
  424. offset,
  425. load_baudrate);
  426. addr = load_serial_bin(offset);
  427. if (addr == ~0) {
  428. image_load_addr = 0;
  429. printf("## Binary (kermit) download aborted\n");
  430. rcode = 1;
  431. } else {
  432. printf("## Start Addr = 0x%08lX\n", addr);
  433. image_load_addr = addr;
  434. }
  435. }
  436. if (load_baudrate != current_baudrate) {
  437. printf("## Switch baudrate to %d bps and press ESC ...\n",
  438. current_baudrate);
  439. udelay(50000);
  440. gd->baudrate = current_baudrate;
  441. serial_setbrg();
  442. udelay(50000);
  443. for (;;) {
  444. if (getchar() == 0x1B) /* ESC */
  445. break;
  446. }
  447. }
  448. return rcode;
  449. }
  450. static ulong load_serial_bin(ulong offset)
  451. {
  452. int size, i;
  453. set_kerm_bin_mode((ulong *) offset);
  454. size = k_recv();
  455. /*
  456. * Gather any trailing characters (for instance, the ^D which
  457. * is sent by 'cu' after sending a file), and give the
  458. * box some time (100 * 1 ms)
  459. */
  460. for (i=0; i<100; ++i) {
  461. if (tstc()) {
  462. getchar();
  463. }
  464. udelay(1000);
  465. }
  466. flush_cache(offset, size);
  467. printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
  468. env_set_hex("filesize", size);
  469. return offset;
  470. }
  471. static void send_pad(void)
  472. {
  473. int count = his_pad_count;
  474. while (count-- > 0)
  475. putc(his_pad_char);
  476. }
  477. /* converts escaped kermit char to binary char */
  478. static char ktrans(char in)
  479. {
  480. if ((in & 0x60) == 0x40) {
  481. return (char) (in & ~0x40);
  482. } else if ((in & 0x7f) == 0x3f) {
  483. return (char) (in | 0x40);
  484. } else
  485. return in;
  486. }
  487. static int chk1(char *buffer)
  488. {
  489. int total = 0;
  490. while (*buffer) {
  491. total += *buffer++;
  492. }
  493. return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
  494. }
  495. static void s1_sendpacket(char *packet)
  496. {
  497. send_pad();
  498. while (*packet) {
  499. putc(*packet++);
  500. }
  501. }
  502. static char a_b[24];
  503. static void send_ack(int n)
  504. {
  505. a_b[0] = START_CHAR;
  506. a_b[1] = tochar(3);
  507. a_b[2] = tochar(n);
  508. a_b[3] = ACK_TYPE;
  509. a_b[4] = '\0';
  510. a_b[4] = tochar(chk1(&a_b[1]));
  511. a_b[5] = his_eol;
  512. a_b[6] = '\0';
  513. s1_sendpacket(a_b);
  514. }
  515. static void send_nack(int n)
  516. {
  517. a_b[0] = START_CHAR;
  518. a_b[1] = tochar(3);
  519. a_b[2] = tochar(n);
  520. a_b[3] = NACK_TYPE;
  521. a_b[4] = '\0';
  522. a_b[4] = tochar(chk1(&a_b[1]));
  523. a_b[5] = his_eol;
  524. a_b[6] = '\0';
  525. s1_sendpacket(a_b);
  526. }
  527. static void (*os_data_init)(void);
  528. static void (*os_data_char)(char new_char);
  529. static int os_data_state, os_data_state_saved;
  530. static char *os_data_addr, *os_data_addr_saved;
  531. static char *bin_start_address;
  532. static void bin_data_init(void)
  533. {
  534. os_data_state = 0;
  535. os_data_addr = bin_start_address;
  536. }
  537. static void os_data_save(void)
  538. {
  539. os_data_state_saved = os_data_state;
  540. os_data_addr_saved = os_data_addr;
  541. }
  542. static void os_data_restore(void)
  543. {
  544. os_data_state = os_data_state_saved;
  545. os_data_addr = os_data_addr_saved;
  546. }
  547. static void bin_data_char(char new_char)
  548. {
  549. switch (os_data_state) {
  550. case 0: /* data */
  551. *os_data_addr++ = new_char;
  552. break;
  553. }
  554. }
  555. static void set_kerm_bin_mode(unsigned long *addr)
  556. {
  557. bin_start_address = (char *) addr;
  558. os_data_init = bin_data_init;
  559. os_data_char = bin_data_char;
  560. }
  561. /* k_data_* simply handles the kermit escape translations */
  562. static int k_data_escape, k_data_escape_saved;
  563. static void k_data_init(void)
  564. {
  565. k_data_escape = 0;
  566. os_data_init();
  567. }
  568. static void k_data_save(void)
  569. {
  570. k_data_escape_saved = k_data_escape;
  571. os_data_save();
  572. }
  573. static void k_data_restore(void)
  574. {
  575. k_data_escape = k_data_escape_saved;
  576. os_data_restore();
  577. }
  578. static void k_data_char(char new_char)
  579. {
  580. if (k_data_escape) {
  581. /* last char was escape - translate this character */
  582. os_data_char(ktrans(new_char));
  583. k_data_escape = 0;
  584. } else {
  585. if (new_char == his_quote) {
  586. /* this char is escape - remember */
  587. k_data_escape = 1;
  588. } else {
  589. /* otherwise send this char as-is */
  590. os_data_char(new_char);
  591. }
  592. }
  593. }
  594. #define SEND_DATA_SIZE 20
  595. static char send_parms[SEND_DATA_SIZE];
  596. static char *send_ptr;
  597. /* handle_send_packet interprits the protocol info and builds and
  598. sends an appropriate ack for what we can do */
  599. static void handle_send_packet(int n)
  600. {
  601. int length = 3;
  602. int bytes;
  603. /* initialize some protocol parameters */
  604. his_eol = END_CHAR; /* default end of line character */
  605. his_pad_count = 0;
  606. his_pad_char = '\0';
  607. his_quote = K_ESCAPE;
  608. /* ignore last character if it filled the buffer */
  609. if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
  610. --send_ptr;
  611. bytes = send_ptr - send_parms; /* how many bytes we'll process */
  612. do {
  613. if (bytes-- <= 0)
  614. break;
  615. /* handle MAXL - max length */
  616. /* ignore what he says - most I'll take (here) is 94 */
  617. a_b[++length] = tochar(94);
  618. if (bytes-- <= 0)
  619. break;
  620. /* handle TIME - time you should wait for my packets */
  621. /* ignore what he says - don't wait for my ack longer than 1 second */
  622. a_b[++length] = tochar(1);
  623. if (bytes-- <= 0)
  624. break;
  625. /* handle NPAD - number of pad chars I need */
  626. /* remember what he says - I need none */
  627. his_pad_count = untochar(send_parms[2]);
  628. a_b[++length] = tochar(0);
  629. if (bytes-- <= 0)
  630. break;
  631. /* handle PADC - pad chars I need */
  632. /* remember what he says - I need none */
  633. his_pad_char = ktrans(send_parms[3]);
  634. a_b[++length] = 0x40; /* He should ignore this */
  635. if (bytes-- <= 0)
  636. break;
  637. /* handle EOL - end of line he needs */
  638. /* remember what he says - I need CR */
  639. his_eol = untochar(send_parms[4]);
  640. a_b[++length] = tochar(END_CHAR);
  641. if (bytes-- <= 0)
  642. break;
  643. /* handle QCTL - quote control char he'll use */
  644. /* remember what he says - I'll use '#' */
  645. his_quote = send_parms[5];
  646. a_b[++length] = '#';
  647. if (bytes-- <= 0)
  648. break;
  649. /* handle QBIN - 8-th bit prefixing */
  650. /* ignore what he says - I refuse */
  651. a_b[++length] = 'N';
  652. if (bytes-- <= 0)
  653. break;
  654. /* handle CHKT - the clock check type */
  655. /* ignore what he says - I do type 1 (for now) */
  656. a_b[++length] = '1';
  657. if (bytes-- <= 0)
  658. break;
  659. /* handle REPT - the repeat prefix */
  660. /* ignore what he says - I refuse (for now) */
  661. a_b[++length] = 'N';
  662. if (bytes-- <= 0)
  663. break;
  664. /* handle CAPAS - the capabilities mask */
  665. /* ignore what he says - I only do long packets - I don't do windows */
  666. a_b[++length] = tochar(2); /* only long packets */
  667. a_b[++length] = tochar(0); /* no windows */
  668. a_b[++length] = tochar(94); /* large packet msb */
  669. a_b[++length] = tochar(94); /* large packet lsb */
  670. } while (0);
  671. a_b[0] = START_CHAR;
  672. a_b[1] = tochar(length);
  673. a_b[2] = tochar(n);
  674. a_b[3] = ACK_TYPE;
  675. a_b[++length] = '\0';
  676. a_b[length] = tochar(chk1(&a_b[1]));
  677. a_b[++length] = his_eol;
  678. a_b[++length] = '\0';
  679. s1_sendpacket(a_b);
  680. }
  681. /* k_recv receives a OS Open image file over kermit line */
  682. static int k_recv(void)
  683. {
  684. char new_char;
  685. char k_state, k_state_saved;
  686. int sum;
  687. int done;
  688. int length;
  689. int n, last_n;
  690. int len_lo, len_hi;
  691. /* initialize some protocol parameters */
  692. his_eol = END_CHAR; /* default end of line character */
  693. his_pad_count = 0;
  694. his_pad_char = '\0';
  695. his_quote = K_ESCAPE;
  696. /* initialize the k_recv and k_data state machine */
  697. done = 0;
  698. k_state = 0;
  699. k_data_init();
  700. k_state_saved = k_state;
  701. k_data_save();
  702. n = 0; /* just to get rid of a warning */
  703. last_n = -1;
  704. /* expect this "type" sequence (but don't check):
  705. S: send initiate
  706. F: file header
  707. D: data (multiple)
  708. Z: end of file
  709. B: break transmission
  710. */
  711. /* enter main loop */
  712. while (!done) {
  713. /* set the send packet pointer to begining of send packet parms */
  714. send_ptr = send_parms;
  715. /* With each packet, start summing the bytes starting with the length.
  716. Save the current sequence number.
  717. Note the type of the packet.
  718. If a character less than SPACE (0x20) is received - error.
  719. */
  720. #if 0
  721. /* OLD CODE, Prior to checking sequence numbers */
  722. /* first have all state machines save current states */
  723. k_state_saved = k_state;
  724. k_data_save ();
  725. #endif
  726. /* get a packet */
  727. /* wait for the starting character or ^C */
  728. for (;;) {
  729. switch (getchar()) {
  730. case START_CHAR: /* start packet */
  731. goto START;
  732. case ETX_CHAR: /* ^C waiting for packet */
  733. return (0);
  734. default:
  735. ;
  736. }
  737. }
  738. START:
  739. /* get length of packet */
  740. sum = 0;
  741. new_char = getchar();
  742. if ((new_char & 0xE0) == 0)
  743. goto packet_error;
  744. sum += new_char & 0xff;
  745. length = untochar(new_char);
  746. /* get sequence number */
  747. new_char = getchar();
  748. if ((new_char & 0xE0) == 0)
  749. goto packet_error;
  750. sum += new_char & 0xff;
  751. n = untochar(new_char);
  752. --length;
  753. /* NEW CODE - check sequence numbers for retried packets */
  754. /* Note - this new code assumes that the sequence number is correctly
  755. * received. Handling an invalid sequence number adds another layer
  756. * of complexity that may not be needed - yet! At this time, I'm hoping
  757. * that I don't need to buffer the incoming data packets and can write
  758. * the data into memory in real time.
  759. */
  760. if (n == last_n) {
  761. /* same sequence number, restore the previous state */
  762. k_state = k_state_saved;
  763. k_data_restore();
  764. } else {
  765. /* new sequence number, checkpoint the download */
  766. last_n = n;
  767. k_state_saved = k_state;
  768. k_data_save();
  769. }
  770. /* END NEW CODE */
  771. /* get packet type */
  772. new_char = getchar();
  773. if ((new_char & 0xE0) == 0)
  774. goto packet_error;
  775. sum += new_char & 0xff;
  776. k_state = new_char;
  777. --length;
  778. /* check for extended length */
  779. if (length == -2) {
  780. /* (length byte was 0, decremented twice) */
  781. /* get the two length bytes */
  782. new_char = getchar();
  783. if ((new_char & 0xE0) == 0)
  784. goto packet_error;
  785. sum += new_char & 0xff;
  786. len_hi = untochar(new_char);
  787. new_char = getchar();
  788. if ((new_char & 0xE0) == 0)
  789. goto packet_error;
  790. sum += new_char & 0xff;
  791. len_lo = untochar(new_char);
  792. length = len_hi * 95 + len_lo;
  793. /* check header checksum */
  794. new_char = getchar();
  795. if ((new_char & 0xE0) == 0)
  796. goto packet_error;
  797. if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
  798. goto packet_error;
  799. sum += new_char & 0xff;
  800. /* --length; */ /* new length includes only data and block check to come */
  801. }
  802. /* bring in rest of packet */
  803. while (length > 1) {
  804. new_char = getchar();
  805. if ((new_char & 0xE0) == 0)
  806. goto packet_error;
  807. sum += new_char & 0xff;
  808. --length;
  809. if (k_state == DATA_TYPE) {
  810. /* pass on the data if this is a data packet */
  811. k_data_char (new_char);
  812. } else if (k_state == SEND_TYPE) {
  813. /* save send pack in buffer as is */
  814. *send_ptr++ = new_char;
  815. /* if too much data, back off the pointer */
  816. if (send_ptr >= &send_parms[SEND_DATA_SIZE])
  817. --send_ptr;
  818. }
  819. }
  820. /* get and validate checksum character */
  821. new_char = getchar();
  822. if ((new_char & 0xE0) == 0)
  823. goto packet_error;
  824. if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f))
  825. goto packet_error;
  826. /* get END_CHAR */
  827. new_char = getchar();
  828. if (new_char != END_CHAR) {
  829. packet_error:
  830. /* restore state machines */
  831. k_state = k_state_saved;
  832. k_data_restore();
  833. /* send a negative acknowledge packet in */
  834. send_nack(n);
  835. } else if (k_state == SEND_TYPE) {
  836. /* crack the protocol parms, build an appropriate ack packet */
  837. handle_send_packet(n);
  838. } else {
  839. /* send simple acknowledge packet in */
  840. send_ack(n);
  841. /* quit if end of transmission */
  842. if (k_state == BREAK_TYPE)
  843. done = 1;
  844. }
  845. }
  846. return ((ulong) os_data_addr - (ulong) bin_start_address);
  847. }
  848. static int getcxmodem(void) {
  849. if (tstc())
  850. return (getchar());
  851. return -1;
  852. }
  853. static ulong load_serial_ymodem(ulong offset, int mode)
  854. {
  855. int size;
  856. int err;
  857. int res;
  858. connection_info_t info;
  859. char ymodemBuf[1024];
  860. ulong store_addr = ~0;
  861. ulong addr = 0;
  862. size = 0;
  863. info.mode = mode;
  864. res = xyzModem_stream_open(&info, &err);
  865. if (!res) {
  866. while ((res =
  867. xyzModem_stream_read(ymodemBuf, 1024, &err)) > 0) {
  868. store_addr = addr + offset;
  869. size += res;
  870. addr += res;
  871. #ifdef CONFIG_MTD_NOR_FLASH
  872. if (addr2info(store_addr)) {
  873. int rc;
  874. rc = flash_write((char *) ymodemBuf,
  875. store_addr, res);
  876. if (rc != 0) {
  877. flash_perror(rc);
  878. return (~0);
  879. }
  880. } else
  881. #endif
  882. {
  883. memcpy((char *)(store_addr), ymodemBuf,
  884. res);
  885. }
  886. }
  887. if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
  888. efi_set_bootdev("Uart", "", "",
  889. map_sysmem(offset, 0), size);
  890. } else {
  891. printf("%s\n", xyzModem_error(err));
  892. }
  893. xyzModem_stream_close(&err);
  894. xyzModem_stream_terminate(false, &getcxmodem);
  895. flush_cache(offset, ALIGN(size, ARCH_DMA_MINALIGN));
  896. printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
  897. env_set_hex("filesize", size);
  898. return offset;
  899. }
  900. #endif
  901. /* -------------------------------------------------------------------- */
  902. #if defined(CONFIG_CMD_LOADS)
  903. #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
  904. U_BOOT_CMD(
  905. loads, 3, 0, do_load_serial,
  906. "load S-Record file over serial line",
  907. "[ off ] [ baud ]\n"
  908. " - load S-Record file over serial line"
  909. " with offset 'off' and baudrate 'baud'"
  910. );
  911. #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
  912. U_BOOT_CMD(
  913. loads, 2, 0, do_load_serial,
  914. "load S-Record file over serial line",
  915. "[ off ]\n"
  916. " - load S-Record file over serial line with offset 'off'"
  917. );
  918. #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
  919. /*
  920. * SAVES always requires LOADS support, but not vice versa
  921. */
  922. #if defined(CONFIG_CMD_SAVES)
  923. #ifdef CONFIG_SYS_LOADS_BAUD_CHANGE
  924. U_BOOT_CMD(
  925. saves, 4, 0, do_save_serial,
  926. "save S-Record file over serial line",
  927. "[ off ] [size] [ baud ]\n"
  928. " - save S-Record file over serial line"
  929. " with offset 'off', size 'size' and baudrate 'baud'"
  930. );
  931. #else /* ! CONFIG_SYS_LOADS_BAUD_CHANGE */
  932. U_BOOT_CMD(
  933. saves, 3, 0, do_save_serial,
  934. "save S-Record file over serial line",
  935. "[ off ] [size]\n"
  936. " - save S-Record file over serial line with offset 'off' and size 'size'"
  937. );
  938. #endif /* CONFIG_SYS_LOADS_BAUD_CHANGE */
  939. #endif /* CONFIG_CMD_SAVES */
  940. #endif /* CONFIG_CMD_LOADS */
  941. #if defined(CONFIG_CMD_LOADB)
  942. U_BOOT_CMD(
  943. loadb, 3, 0, do_load_serial_bin,
  944. "load binary file over serial line (kermit mode)",
  945. "[ addr [ baud ] ]\n"
  946. " - load binary file over serial line"
  947. " at address 'addr' with baudrate 'baud'"
  948. );
  949. U_BOOT_CMD(
  950. loadx, 3, 0, do_load_serial_bin,
  951. "load binary file over serial line (xmodem mode)",
  952. "[ addr [ baud ] ]\n"
  953. " - load binary file over serial line"
  954. " at address 'addr' with baudrate 'baud'"
  955. );
  956. U_BOOT_CMD(
  957. loady, 3, 0, do_load_serial_bin,
  958. "load binary file over serial line (ymodem mode)",
  959. "[ addr [ baud ] ]\n"
  960. " - load binary file over serial line"
  961. " at address 'addr' with baudrate 'baud'"
  962. );
  963. #endif /* CONFIG_CMD_LOADB */