load.c 25 KB

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