load.c 25 KB

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