load.c 25 KB

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