auto_update.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * (C) Copyright 2003
  3. * Gary Jennejohn, DENX Software Engineering, gj@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <malloc.h>
  26. #include <image.h>
  27. #include <asm/byteorder.h>
  28. #include <usb.h>
  29. #ifdef CFG_HUSH_PARSER
  30. #include <hush.h>
  31. #endif
  32. #ifdef CONFIG_AUTO_UPDATE
  33. #ifndef CONFIG_USB_OHCI_NEW
  34. #error "must define CONFIG_USB_OHCI"
  35. #endif
  36. #ifndef CONFIG_USB_STORAGE
  37. #error "must define CONFIG_USB_STORAGE"
  38. #endif
  39. #ifndef CFG_HUSH_PARSER
  40. #error "must define CFG_HUSH_PARSER"
  41. #endif
  42. #if !defined(CONFIG_CMD_FAT)
  43. #error "must define CONFIG_CMD_FAT"
  44. #endif
  45. /*
  46. * Check whether a USB memory stick is plugged in.
  47. * If one is found:
  48. * 1) if prepare.img ist found load it into memory. If it is
  49. * valid then run it.
  50. * 2) if preinst.img is found load it into memory. If it is
  51. * valid then run it. Update the EEPROM.
  52. * 3) if firmw_01.img is found load it into memory. If it is valid,
  53. * burn it into FLASH and update the EEPROM.
  54. * 4) if kernl_01.img is found load it into memory. If it is valid,
  55. * burn it into FLASH and update the EEPROM.
  56. * 5) if app.img is found load it into memory. If it is valid,
  57. * burn it into FLASH and update the EEPROM.
  58. * 6) if disk.img is found load it into memory. If it is valid,
  59. * burn it into FLASH and update the EEPROM.
  60. * 7) if postinst.img is found load it into memory. If it is
  61. * valid then run it. Update the EEPROM.
  62. */
  63. #undef AU_DEBUG
  64. #undef debug
  65. #ifdef AU_DEBUG
  66. #define debug(fmt,args...) printf (fmt ,##args)
  67. #else
  68. #define debug(fmt,args...)
  69. #endif /* AU_DEBUG */
  70. /* possible names of files on the USB stick. */
  71. #define AU_PREPARE "prepare.img"
  72. #define AU_PREINST "preinst.img"
  73. #define AU_FIRMWARE "firmw_01.img"
  74. #define AU_KERNEL "kernl_01.img"
  75. #define AU_APP "app.img"
  76. #define AU_DISK "disk.img"
  77. #define AU_POSTINST "postinst.img"
  78. struct flash_layout
  79. {
  80. long start;
  81. long end;
  82. };
  83. /* layout of the FLASH. ST = start address, ND = end address. */
  84. #ifndef CONFIG_FLASH_8MB /* 16 MB Flash, 32 MB RAM */
  85. #define AU_FL_FIRMWARE_ST 0x00000000
  86. #define AU_FL_FIRMWARE_ND 0x0009FFFF
  87. #define AU_FL_VFD_ST 0x000A0000
  88. #define AU_FL_VFD_ND 0x000BFFFF
  89. #define AU_FL_KERNEL_ST 0x000C0000
  90. #define AU_FL_KERNEL_ND 0x001BFFFF
  91. #define AU_FL_APP_ST 0x001C0000
  92. #define AU_FL_APP_ND 0x005BFFFF
  93. #define AU_FL_DISK_ST 0x005C0000
  94. #define AU_FL_DISK_ND 0x00FFFFFF
  95. #else /* 8 MB Flash, 32 MB RAM */
  96. #define AU_FL_FIRMWARE_ST 0x00000000
  97. #define AU_FL_FIRMWARE_ND 0x0005FFFF
  98. #define AU_FL_KERNEL_ST 0x00060000
  99. #define AU_FL_KERNEL_ND 0x0013FFFF
  100. #define AU_FL_APP_ST 0x00140000
  101. #define AU_FL_APP_ND 0x0067FFFF
  102. #define AU_FL_DISK_ST 0x00680000
  103. #define AU_FL_DISK_ND 0x007DFFFF
  104. #define AU_FL_VFD_ST 0x007E0000
  105. #define AU_FL_VFD_ND 0x007FFFFF
  106. #endif /* CONFIG_FLASH_8MB */
  107. /* a structure with the offsets to values in the EEPROM */
  108. struct eeprom_layout
  109. {
  110. int time;
  111. int size;
  112. int dcrc;
  113. };
  114. /* layout of the EEPROM - offset from the start. All entries are 32 bit. */
  115. #define AU_EEPROM_TIME_PREINST 64
  116. #define AU_EEPROM_SIZE_PREINST 68
  117. #define AU_EEPROM_DCRC_PREINST 72
  118. #define AU_EEPROM_TIME_FIRMWARE 76
  119. #define AU_EEPROM_SIZE_FIRMWARE 80
  120. #define AU_EEPROM_DCRC_FIRMWARE 84
  121. #define AU_EEPROM_TIME_KERNEL 88
  122. #define AU_EEPROM_SIZE_KERNEL 92
  123. #define AU_EEPROM_DCRC_KERNEL 96
  124. #define AU_EEPROM_TIME_APP 100
  125. #define AU_EEPROM_SIZE_APP 104
  126. #define AU_EEPROM_DCRC_APP 108
  127. #define AU_EEPROM_TIME_DISK 112
  128. #define AU_EEPROM_SIZE_DISK 116
  129. #define AU_EEPROM_DCRC_DISK 120
  130. #define AU_EEPROM_TIME_POSTINST 124
  131. #define AU_EEPROM_SIZE_POSTINST 128
  132. #define AU_EEPROM_DCRC_POSTINST 132
  133. static int au_usb_stor_curr_dev; /* current device */
  134. /* index of each file in the following arrays */
  135. #define IDX_PREPARE 0
  136. #define IDX_PREINST 1
  137. #define IDX_FIRMWARE 2
  138. #define IDX_KERNEL 3
  139. #define IDX_APP 4
  140. #define IDX_DISK 5
  141. #define IDX_POSTINST 6
  142. /* max. number of files which could interest us */
  143. #define AU_MAXFILES 7
  144. /* pointers to file names */
  145. char *aufile[AU_MAXFILES];
  146. /* sizes of flash areas for each file */
  147. long ausize[AU_MAXFILES];
  148. /* offsets into the EEEPROM */
  149. struct eeprom_layout auee_off[AU_MAXFILES] = { \
  150. {0}, \
  151. {AU_EEPROM_TIME_PREINST, AU_EEPROM_SIZE_PREINST, AU_EEPROM_DCRC_PREINST,}, \
  152. {AU_EEPROM_TIME_FIRMWARE, AU_EEPROM_SIZE_FIRMWARE, AU_EEPROM_DCRC_FIRMWARE,}, \
  153. {AU_EEPROM_TIME_KERNEL, AU_EEPROM_SIZE_KERNEL, AU_EEPROM_DCRC_KERNEL,}, \
  154. {AU_EEPROM_TIME_APP, AU_EEPROM_SIZE_APP, AU_EEPROM_DCRC_APP,}, \
  155. {AU_EEPROM_TIME_DISK, AU_EEPROM_SIZE_DISK, AU_EEPROM_DCRC_DISK,}, \
  156. {AU_EEPROM_TIME_POSTINST, AU_EEPROM_SIZE_POSTINST, AU_EEPROM_DCRC_POSTINST,} \
  157. };
  158. /* array of flash areas start and end addresses */
  159. struct flash_layout aufl_layout[AU_MAXFILES - 3] = { \
  160. {AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND,}, \
  161. {AU_FL_KERNEL_ST, AU_FL_KERNEL_ND,}, \
  162. {AU_FL_APP_ST, AU_FL_APP_ND,}, \
  163. {AU_FL_DISK_ST, AU_FL_DISK_ND,}, \
  164. };
  165. /* convert the index into aufile[] to an index into aufl_layout[] */
  166. #define FIDX_TO_LIDX(idx) ((idx) - 2)
  167. /* where to load files into memory */
  168. #define LOAD_ADDR ((unsigned char *)0x0C100000)
  169. /* the app is the largest image */
  170. #define MAX_LOADSZ ausize[IDX_APP]
  171. /* externals */
  172. extern int fat_register_device(block_dev_desc_t *, int);
  173. extern int file_fat_detectfs(void);
  174. extern long file_fat_read(const char *, void *, unsigned long);
  175. extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
  176. extern int i2c_write (uchar, uint, int , uchar* , int);
  177. #ifdef CONFIG_VFD
  178. extern int trab_vfd (ulong);
  179. extern int transfer_pic(unsigned char, unsigned char *, int, int);
  180. #endif
  181. extern int flash_sect_erase(ulong, ulong);
  182. extern int flash_sect_protect (int, ulong, ulong);
  183. extern int flash_write (char *, ulong, ulong);
  184. /* change char* to void* to shutup the compiler */
  185. extern int i2c_write_multiple (uchar, uint, int, void *, int);
  186. extern int i2c_read_multiple (uchar, uint, int, void *, int);
  187. extern int u_boot_hush_start(void);
  188. int
  189. au_check_cksum_valid(int idx, long nbytes)
  190. {
  191. image_header_t *hdr;
  192. hdr = (image_header_t *)LOAD_ADDR;
  193. if (nbytes != image_get_image_size (hdr))
  194. {
  195. printf ("Image %s bad total SIZE\n", aufile[idx]);
  196. return -1;
  197. }
  198. /* check the data CRC */
  199. if (!image_check_dcrc (hdr)) {
  200. {
  201. printf ("Image %s bad data checksum\n", aufile[idx]);
  202. return -1;
  203. }
  204. return 0;
  205. }
  206. int
  207. au_check_header_valid(int idx, long nbytes)
  208. {
  209. image_header_t *hdr;
  210. unsigned long checksum;
  211. unsigned char buf[4];
  212. hdr = (image_header_t *)LOAD_ADDR;
  213. /* check the easy ones first */
  214. #undef CHECK_VALID_DEBUG
  215. #ifdef CHECK_VALID_DEBUG
  216. printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
  217. printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM);
  218. printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
  219. printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
  220. #endif
  221. if (nbytes < image_get_header_size ())
  222. {
  223. printf ("Image %s bad header SIZE\n", aufile[idx]);
  224. return -1;
  225. }
  226. if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_ARM))
  227. {
  228. printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
  229. return -1;
  230. }
  231. /* check the hdr CRC */
  232. if (!image_check_hcrc (hdr)) {
  233. printf ("Image %s bad header checksum\n", aufile[idx]);
  234. return -1;
  235. }
  236. /* check the type - could do this all in one gigantic if() */
  237. if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
  238. printf ("Image %s wrong type\n", aufile[idx]);
  239. return -1;
  240. }
  241. if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
  242. printf ("Image %s wrong type\n", aufile[idx]);
  243. return -1;
  244. }
  245. if ((idx == IDX_DISK) && !image_check_type (hdr, IH_TYPE_FILESYSTEM)) {
  246. printf ("Image %s wrong type\n", aufile[idx]);
  247. return -1;
  248. }
  249. if ((idx == IDX_APP) && !image_check_type (hdr, IH_TYPE_RAMDISK)
  250. && !image_check_type (hdr, FILESYSTEM)) {
  251. printf ("Image %s wrong type\n", aufile[idx]);
  252. return -1;
  253. }
  254. if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
  255. && !image_check_type (hdr, IH_TYPE_SCRIPT))
  256. {
  257. printf ("Image %s wrong type\n", aufile[idx]);
  258. return -1;
  259. }
  260. /* special case for prepare.img */
  261. if (idx == IDX_PREPARE)
  262. return 0;
  263. /* recycle checksum */
  264. checksum = image_get_data_size (hdr);
  265. /* for kernel and app the image header must also fit into flash */
  266. if ((idx != IDX_DISK) && (idx != IDX_FIRMWARE))
  267. checksum += image_get_header_size ();
  268. /* check the size does not exceed space in flash. HUSH scripts */
  269. /* all have ausize[] set to 0 */
  270. if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
  271. printf ("Image %s is bigger than FLASH\n", aufile[idx]);
  272. return -1;
  273. }
  274. /* check the time stamp from the EEPROM */
  275. /* read it in */
  276. i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
  277. #ifdef CHECK_VALID_DEBUG
  278. printf ("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x "
  279. "as int %#x time %#x\n",
  280. buf[0], buf[1], buf[2], buf[3],
  281. *((unsigned int *)buf), image_get_time (hdr));
  282. #endif
  283. /* check it */
  284. if (*((unsigned int *)buf) >= image_get_time (hdr)) {
  285. printf ("Image %s is too old\n", aufile[idx]);
  286. return -1;
  287. }
  288. return 0;
  289. }
  290. /* power control defines */
  291. #define CPLD_VFD_BK ((volatile char *)0x04038002)
  292. #define POWER_OFF (1 << 1)
  293. int
  294. au_do_update(int idx, long sz)
  295. {
  296. image_header_t *hdr;
  297. char *addr;
  298. long start, end;
  299. int off, rc;
  300. uint nbytes;
  301. hdr = (image_header_t *)LOAD_ADDR;
  302. /* disable the power switch */
  303. *CPLD_VFD_BK |= POWER_OFF;
  304. /* execute a script */
  305. if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
  306. addr = (char *)((char *)hdr + image_get_header_size ());
  307. /* stick a NULL at the end of the script, otherwise */
  308. /* parse_string_outer() runs off the end. */
  309. addr[image_get_data_size (hdr)] = 0;
  310. addr += 8;
  311. parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
  312. return 0;
  313. }
  314. start = aufl_layout[FIDX_TO_LIDX(idx)].start;
  315. end = aufl_layout[FIDX_TO_LIDX(idx)].end;
  316. /* unprotect the address range */
  317. /* this assumes that ONLY the firmware is protected! */
  318. if (idx == IDX_FIRMWARE) {
  319. #undef AU_UPDATE_TEST
  320. #ifdef AU_UPDATE_TEST
  321. /* erase it where Linux goes */
  322. start = aufl_layout[1].start;
  323. end = aufl_layout[1].end;
  324. #endif
  325. flash_sect_protect(0, start, end);
  326. }
  327. /*
  328. * erase the address range.
  329. */
  330. debug ("flash_sect_erase(%lx, %lx);\n", start, end);
  331. flash_sect_erase(start, end);
  332. wait_ms(100);
  333. /* strip the header - except for the kernel and ramdisk */
  334. if (image_check_type (hdr, IH_TYPE_KERNEL) ||
  335. image_check_type (hdr, IH_TYPE_RAMDISK)) {
  336. addr = (char *)hdr;
  337. off = image_get_header_size ();
  338. nbytes = image_get_image_size (hdr);
  339. } else {
  340. addr = (char *)((char *)hdr + image_get_header_size ());
  341. #ifdef AU_UPDATE_TEST
  342. /* copy it to where Linux goes */
  343. if (idx == IDX_FIRMWARE)
  344. start = aufl_layout[1].start;
  345. #endif
  346. off = 0;
  347. nbytes = image_get_data_size (hdr);
  348. }
  349. /* copy the data from RAM to FLASH */
  350. debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
  351. rc = flash_write(addr, start, nbytes);
  352. if (rc != 0) {
  353. printf("Flashing failed due to error %d\n", rc);
  354. return -1;
  355. }
  356. /* check the dcrc of the copy */
  357. if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
  358. image_get_dcrc (hdr)) {
  359. printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
  360. return -1;
  361. }
  362. /* protect the address range */
  363. /* this assumes that ONLY the firmware is protected! */
  364. if (idx == IDX_FIRMWARE)
  365. flash_sect_protect(1, start, end);
  366. return 0;
  367. }
  368. int
  369. au_update_eeprom(int idx)
  370. {
  371. image_header_t *hdr;
  372. int off;
  373. uint32_t val;
  374. /* special case for prepare.img */
  375. if (idx == IDX_PREPARE) {
  376. /* enable the power switch */
  377. *CPLD_VFD_BK &= ~POWER_OFF;
  378. return 0;
  379. }
  380. hdr = (image_header_t *)LOAD_ADDR;
  381. /* write the time field into EEPROM */
  382. off = auee_off[idx].time;
  383. val = image_get_time (hdr);
  384. i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
  385. /* write the size field into EEPROM */
  386. off = auee_off[idx].size;
  387. val = image_get_data_size (hdr);
  388. i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
  389. /* write the dcrc field into EEPROM */
  390. off = auee_off[idx].dcrc;
  391. val = image_get_dcrc (hdr);
  392. i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
  393. /* enable the power switch */
  394. *CPLD_VFD_BK &= ~POWER_OFF;
  395. return 0;
  396. }
  397. /*
  398. * this is called from board_init() after the hardware has been set up
  399. * and is usable. That seems like a good time to do this.
  400. * Right now the return value is ignored.
  401. */
  402. int
  403. do_auto_update(void)
  404. {
  405. block_dev_desc_t *stor_dev;
  406. long sz;
  407. int i, res = 0, bitmap_first, cnt, old_ctrlc, got_ctrlc;
  408. char *env;
  409. long start, end;
  410. #undef ERASE_EEPROM
  411. #ifdef ERASE_EEPROM
  412. int arr[18];
  413. memset(arr, 0, sizeof(arr));
  414. i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr));
  415. #endif
  416. au_usb_stor_curr_dev = -1;
  417. /* start USB */
  418. if (usb_stop() < 0) {
  419. debug ("usb_stop failed\n");
  420. return -1;
  421. }
  422. if (usb_init() < 0) {
  423. debug ("usb_init failed\n");
  424. return -1;
  425. }
  426. /*
  427. * check whether a storage device is attached (assume that it's
  428. * a USB memory stick, since nothing else should be attached).
  429. */
  430. au_usb_stor_curr_dev = usb_stor_scan(0);
  431. if (au_usb_stor_curr_dev == -1) {
  432. debug ("No device found. Not initialized?\n");
  433. res = -1;
  434. goto xit;
  435. }
  436. /* check whether it has a partition table */
  437. stor_dev = get_dev("usb", 0);
  438. if (stor_dev == NULL) {
  439. debug ("uknown device type\n");
  440. res = -1;
  441. goto xit;
  442. }
  443. if (fat_register_device(stor_dev, 1) != 0) {
  444. debug ("Unable to use USB %d:%d for fatls\n",
  445. au_usb_stor_curr_dev, 1);
  446. res = -1;
  447. goto xit;
  448. }
  449. if (file_fat_detectfs() != 0) {
  450. debug ("file_fat_detectfs failed\n");
  451. }
  452. /* initialize the array of file names */
  453. memset(aufile, 0, sizeof(aufile));
  454. aufile[IDX_PREPARE] = AU_PREPARE;
  455. aufile[IDX_PREINST] = AU_PREINST;
  456. aufile[IDX_FIRMWARE] = AU_FIRMWARE;
  457. aufile[IDX_KERNEL] = AU_KERNEL;
  458. aufile[IDX_APP] = AU_APP;
  459. aufile[IDX_DISK] = AU_DISK;
  460. aufile[IDX_POSTINST] = AU_POSTINST;
  461. /* initialize the array of flash sizes */
  462. memset(ausize, 0, sizeof(ausize));
  463. ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
  464. ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
  465. ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST;
  466. ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST;
  467. /*
  468. * now check whether start and end are defined using environment
  469. * variables.
  470. */
  471. start = -1;
  472. end = 0;
  473. env = getenv("firmware_st");
  474. if (env != NULL)
  475. start = simple_strtoul(env, NULL, 16);
  476. env = getenv("firmware_nd");
  477. if (env != NULL)
  478. end = simple_strtoul(env, NULL, 16);
  479. if (start >= 0 && end && end > start) {
  480. ausize[IDX_FIRMWARE] = (end + 1) - start;
  481. aufl_layout[0].start = start;
  482. aufl_layout[0].end = end;
  483. }
  484. start = -1;
  485. end = 0;
  486. env = getenv("kernel_st");
  487. if (env != NULL)
  488. start = simple_strtoul(env, NULL, 16);
  489. env = getenv("kernel_nd");
  490. if (env != NULL)
  491. end = simple_strtoul(env, NULL, 16);
  492. if (start >= 0 && end && end > start) {
  493. ausize[IDX_KERNEL] = (end + 1) - start;
  494. aufl_layout[1].start = start;
  495. aufl_layout[1].end = end;
  496. }
  497. start = -1;
  498. end = 0;
  499. env = getenv("app_st");
  500. if (env != NULL)
  501. start = simple_strtoul(env, NULL, 16);
  502. env = getenv("app_nd");
  503. if (env != NULL)
  504. end = simple_strtoul(env, NULL, 16);
  505. if (start >= 0 && end && end > start) {
  506. ausize[IDX_APP] = (end + 1) - start;
  507. aufl_layout[2].start = start;
  508. aufl_layout[2].end = end;
  509. }
  510. start = -1;
  511. end = 0;
  512. env = getenv("disk_st");
  513. if (env != NULL)
  514. start = simple_strtoul(env, NULL, 16);
  515. env = getenv("disk_nd");
  516. if (env != NULL)
  517. end = simple_strtoul(env, NULL, 16);
  518. if (start >= 0 && end && end > start) {
  519. ausize[IDX_DISK] = (end + 1) - start;
  520. aufl_layout[3].start = start;
  521. aufl_layout[3].end = end;
  522. }
  523. /* make certain that HUSH is runnable */
  524. u_boot_hush_start();
  525. /* make sure that we see CTRL-C and save the old state */
  526. old_ctrlc = disable_ctrlc(0);
  527. bitmap_first = 0;
  528. /* just loop thru all the possible files */
  529. for (i = 0; i < AU_MAXFILES; i++) {
  530. /* just read the header */
  531. sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
  532. debug ("read %s sz %ld hdr %d\n",
  533. aufile[i], sz, image_get_header_size ());
  534. if (sz <= 0 || sz < image_get_header_size ()) {
  535. debug ("%s not found\n", aufile[i]);
  536. continue;
  537. }
  538. if (au_check_header_valid(i, sz) < 0) {
  539. debug ("%s header not valid\n", aufile[i]);
  540. continue;
  541. }
  542. sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
  543. debug ("read %s sz %ld hdr %d\n",
  544. aufile[i], sz, image_get_header_size ());
  545. if (sz <= 0 || sz <= image_get_header_size ()) {
  546. debug ("%s not found\n", aufile[i]);
  547. continue;
  548. }
  549. if (au_check_cksum_valid(i, sz) < 0) {
  550. debug ("%s checksum not valid\n", aufile[i]);
  551. continue;
  552. }
  553. #ifdef CONFIG_VFD
  554. /* now that we have a valid file we can display the */
  555. /* bitmap. */
  556. if (bitmap_first == 0) {
  557. env = getenv("bitmap2");
  558. if (env == NULL) {
  559. trab_vfd(0);
  560. } else {
  561. /* not so simple - bitmap2 is supposed to */
  562. /* contain the address of the bitmap */
  563. env = (char *)simple_strtoul(env, NULL, 16);
  564. /* NOTE: these are taken from vfd_logo.h. If that file changes then */
  565. /* these defines MUST also be updated! These may be wrong for bitmap2. */
  566. #define VFD_LOGO_WIDTH 112
  567. #define VFD_LOGO_HEIGHT 72
  568. /* must call transfer_pic directly */
  569. transfer_pic(3, (unsigned char *)env,
  570. VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH);
  571. }
  572. bitmap_first = 1;
  573. }
  574. #endif
  575. /* this is really not a good idea, but it's what the */
  576. /* customer wants. */
  577. cnt = 0;
  578. got_ctrlc = 0;
  579. do {
  580. res = au_do_update(i, sz);
  581. /* let the user break out of the loop */
  582. if (ctrlc() || had_ctrlc()) {
  583. clear_ctrlc();
  584. if (res < 0)
  585. got_ctrlc = 1;
  586. break;
  587. }
  588. cnt++;
  589. #ifdef AU_TEST_ONLY
  590. } while (res < 0 && cnt < 3);
  591. if (cnt < 3)
  592. #else
  593. } while (res < 0);
  594. #endif
  595. /*
  596. * it doesn't make sense to update the EEPROM if the
  597. * update was interrupted by the user due to errors.
  598. */
  599. if (got_ctrlc == 0)
  600. au_update_eeprom(i);
  601. else
  602. /* enable the power switch */
  603. *CPLD_VFD_BK &= ~POWER_OFF;
  604. }
  605. /* restore the old state */
  606. disable_ctrlc(old_ctrlc);
  607. xit:
  608. usb_stop();
  609. return res;
  610. }
  611. #endif /* CONFIG_AUTO_UPDATE */