auto_update.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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
  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 !(CONFIG_COMMANDS & CFG_CMD_FAT)
  43. #error "must define CFG_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 firmware.img is found load it into memory. If it is valid,
  53. * burn it into FLASH and update the EEPROM.
  54. * 4) if kernel.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 "firmware.img"
  74. #define AU_KERNEL "kernel.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 (uchar *, 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 block_dev_desc_t *get_dev (char*, int);
  188. extern int u_boot_hush_start(void);
  189. int
  190. au_check_valid(int idx, long nbytes)
  191. {
  192. image_header_t *hdr;
  193. unsigned long checksum;
  194. unsigned char buf[4];
  195. hdr = (image_header_t *)LOAD_ADDR;
  196. /* check the easy ones first */
  197. #undef CHECK_VALID_DEBUG
  198. #ifdef CHECK_VALID_DEBUG
  199. printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
  200. printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
  201. printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
  202. printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
  203. #endif
  204. if (ntohl(hdr->ih_magic) != IH_MAGIC ||
  205. hdr->ih_arch != IH_CPU_ARM ||
  206. nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size)))
  207. {
  208. printf ("Image %s bad MAGIC or ARCH or SIZE\n", aufile[idx]);
  209. return -1;
  210. }
  211. /* check the hdr CRC */
  212. checksum = ntohl(hdr->ih_hcrc);
  213. hdr->ih_hcrc = 0;
  214. if (crc32 (0, (char *)hdr, sizeof(*hdr)) != checksum) {
  215. printf ("Image %s bad header checksum\n", aufile[idx]);
  216. return -1;
  217. }
  218. hdr->ih_hcrc = htonl(checksum);
  219. /* check the data CRC */
  220. checksum = ntohl(hdr->ih_dcrc);
  221. if (crc32 (0, (char *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
  222. != checksum)
  223. {
  224. printf ("Image %s bad data checksum\n", aufile[idx]);
  225. return -1;
  226. }
  227. /* check the type - could do this all in one gigantic if() */
  228. if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
  229. printf ("Image %s wrong type\n", aufile[idx]);
  230. return -1;
  231. }
  232. if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
  233. printf ("Image %s wrong type\n", aufile[idx]);
  234. return -1;
  235. }
  236. if ((idx == IDX_DISK) && (hdr->ih_type != IH_TYPE_FILESYSTEM)) {
  237. printf ("Image %s wrong type\n", aufile[idx]);
  238. return -1;
  239. }
  240. if ((idx == IDX_APP) && (hdr->ih_type != IH_TYPE_RAMDISK)) {
  241. printf ("Image %s wrong type\n", aufile[idx]);
  242. return -1;
  243. }
  244. if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
  245. && (hdr->ih_type != IH_TYPE_SCRIPT))
  246. {
  247. printf ("Image %s wrong type\n", aufile[idx]);
  248. return -1;
  249. }
  250. /* special case for prepare.img */
  251. if (idx == IDX_PREPARE)
  252. return 0;
  253. /* recycle checksum */
  254. checksum = ntohl(hdr->ih_size);
  255. /* for kernel and app the image header must also fit into flash */
  256. if (idx != IDX_DISK)
  257. checksum += sizeof(*hdr);
  258. /* check the size does not exceed space in flash. HUSH scripts */
  259. /* all have ausize[] set to 0 */
  260. if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
  261. printf ("Image %s is bigger than FLASH\n", aufile[idx]);
  262. return -1;
  263. }
  264. /* check the time stamp from the EEPROM */
  265. /* read it in */
  266. i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
  267. #ifdef CHECK_VALID_DEBUG
  268. printf ("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x "
  269. "as int %#x time %#x\n",
  270. buf[0], buf[1], buf[2], buf[3],
  271. *((unsigned int *)buf), ntohl(hdr->ih_time));
  272. #endif
  273. /* check it */
  274. if (*((unsigned int *)buf) >= ntohl(hdr->ih_time)) {
  275. printf ("Image %s is too old\n", aufile[idx]);
  276. return -1;
  277. }
  278. return 0;
  279. }
  280. /* power control defines */
  281. #define CPLD_VFD_BK ((volatile char *)0x04038002)
  282. #define POWER_OFF (1 << 1)
  283. int
  284. au_do_update(int idx, long sz)
  285. {
  286. image_header_t *hdr;
  287. char *addr;
  288. long start, end;
  289. int off, rc;
  290. uint nbytes;
  291. hdr = (image_header_t *)LOAD_ADDR;
  292. /* disable the power switch */
  293. *CPLD_VFD_BK |= POWER_OFF;
  294. /* execute a script */
  295. if (hdr->ih_type == IH_TYPE_SCRIPT) {
  296. addr = (char *)((char *)hdr + sizeof(*hdr));
  297. /* stick a NULL at the end of the script, otherwise */
  298. /* parse_string_outer() runs off the end. */
  299. addr[ntohl(hdr->ih_size)] = 0;
  300. addr += 8;
  301. parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
  302. return 0;
  303. }
  304. start = aufl_layout[FIDX_TO_LIDX(idx)].start;
  305. end = aufl_layout[FIDX_TO_LIDX(idx)].end;
  306. /* unprotect the address range */
  307. /* this assumes that ONLY the firmware is protected! */
  308. if (idx == IDX_FIRMWARE) {
  309. #undef AU_UPDATE_TEST
  310. #ifdef AU_UPDATE_TEST
  311. /* erase it where Linux goes */
  312. start = aufl_layout[1].start;
  313. end = aufl_layout[1].end;
  314. #endif
  315. flash_sect_protect(0, start, end);
  316. }
  317. /*
  318. * erase the address range.
  319. */
  320. debug ("flash_sect_erase(%lx, %lx);\n", start, end);
  321. flash_sect_erase(start, end);
  322. wait_ms(100);
  323. /* strip the header - except for the kernel and app */
  324. if (idx == IDX_FIRMWARE || idx == IDX_DISK) {
  325. addr = (char *)((char *)hdr + sizeof(*hdr));
  326. #ifdef AU_UPDATE_TEST
  327. /* copy it to where Linux goes */
  328. if (idx == IDX_FIRMWARE)
  329. start = aufl_layout[1].start;
  330. #endif
  331. off = 0;
  332. nbytes = ntohl(hdr->ih_size);
  333. } else {
  334. addr = (char *)hdr;
  335. off = sizeof(*hdr);
  336. nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
  337. }
  338. /* copy the data from RAM to FLASH */
  339. debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
  340. rc = flash_write(addr, start, nbytes);
  341. if (rc != 0) {
  342. printf("Flashing failed due to error %d\n", rc);
  343. return -1;
  344. }
  345. /* check the dcrc of the copy */
  346. if (crc32 (0, (char *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
  347. printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
  348. return -1;
  349. }
  350. /* protect the address range */
  351. /* this assumes that ONLY the firmware is protected! */
  352. if (idx == IDX_FIRMWARE)
  353. flash_sect_protect(1, start, end);
  354. return 0;
  355. }
  356. int
  357. au_update_eeprom(int idx)
  358. {
  359. image_header_t *hdr;
  360. int off;
  361. uint32_t val;
  362. hdr = (image_header_t *)LOAD_ADDR;
  363. /* write the time field into EEPROM */
  364. off = auee_off[idx].time;
  365. val = ntohl(hdr->ih_time);
  366. i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
  367. /* write the size field into EEPROM */
  368. off = auee_off[idx].size;
  369. val = ntohl(hdr->ih_size);
  370. i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
  371. /* write the dcrc field into EEPROM */
  372. off = auee_off[idx].dcrc;
  373. val = ntohl(hdr->ih_dcrc);
  374. i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
  375. /* enable the power switch */
  376. *CPLD_VFD_BK &= ~POWER_OFF;
  377. return 0;
  378. }
  379. /*
  380. * this is called from board_init() after the hardware has been set up
  381. * and is usable. That seems like a good time to do this.
  382. * Right now the return value is ignored.
  383. */
  384. int
  385. do_auto_update(void)
  386. {
  387. block_dev_desc_t *stor_dev;
  388. long sz;
  389. int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc;
  390. char *env;
  391. long start, end;
  392. #undef ERASE_EEPROM
  393. #ifdef ERASE_EEPROM
  394. int arr[18];
  395. memset(arr, 0, sizeof(arr));
  396. i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr));
  397. #endif
  398. au_usb_stor_curr_dev = -1;
  399. /* start USB */
  400. if (usb_stop() < 0) {
  401. debug ("usb_stop failed\n");
  402. return -1;
  403. }
  404. if (usb_init() < 0) {
  405. debug ("usb_init failed\n");
  406. return -1;
  407. }
  408. /*
  409. * check whether a storage device is attached (assume that it's
  410. * a USB memory stick, since nothing else should be attached).
  411. */
  412. au_usb_stor_curr_dev = usb_stor_scan(1);
  413. if (au_usb_stor_curr_dev == -1) {
  414. debug ("No device found. Not initialized?\n");
  415. return -1;
  416. }
  417. /* check whether it has a partition table */
  418. stor_dev = get_dev("usb", 0);
  419. if (stor_dev == NULL) {
  420. debug ("uknown device type\n");
  421. return -1;
  422. }
  423. if (fat_register_device(stor_dev, 1) != 0) {
  424. debug ("Unable to use USB %d:%d for fatls\n",
  425. au_usb_stor_curr_dev, 1);
  426. return -1;
  427. }
  428. if (file_fat_detectfs() != 0) {
  429. debug ("file_fat_detectfs failed\n");
  430. }
  431. /* initialize the array of file names */
  432. memset(aufile, 0, sizeof(aufile));
  433. aufile[IDX_PREPARE] = AU_PREPARE;
  434. aufile[IDX_PREINST] = AU_PREINST;
  435. aufile[IDX_FIRMWARE] = AU_FIRMWARE;
  436. aufile[IDX_KERNEL] = AU_KERNEL;
  437. aufile[IDX_APP] = AU_APP;
  438. aufile[IDX_DISK] = AU_DISK;
  439. aufile[IDX_POSTINST] = AU_POSTINST;
  440. /* initialize the array of flash sizes */
  441. memset(ausize, 0, sizeof(ausize));
  442. ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
  443. ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
  444. ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST;
  445. ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST;
  446. /*
  447. * now check whether start and end are defined using environment
  448. * variables.
  449. */
  450. start = -1;
  451. end = 0;
  452. env = getenv("firmware_st");
  453. if (env != NULL)
  454. start = simple_strtoul(env, NULL, 16);
  455. env = getenv("firmware_nd");
  456. if (env != NULL)
  457. end = simple_strtoul(env, NULL, 16);
  458. if (start >= 0 && end && end > start) {
  459. ausize[IDX_FIRMWARE] = (end + 1) - start;
  460. aufl_layout[0].start = start;
  461. aufl_layout[0].end = end;
  462. }
  463. start = -1;
  464. end = 0;
  465. env = getenv("kernel_st");
  466. if (env != NULL)
  467. start = simple_strtoul(env, NULL, 16);
  468. env = getenv("kernel_nd");
  469. if (env != NULL)
  470. end = simple_strtoul(env, NULL, 16);
  471. if (start >= 0 && end && end > start) {
  472. ausize[IDX_KERNEL] = (end + 1) - start;
  473. aufl_layout[1].start = start;
  474. aufl_layout[1].end = end;
  475. }
  476. start = -1;
  477. end = 0;
  478. env = getenv("app_st");
  479. if (env != NULL)
  480. start = simple_strtoul(env, NULL, 16);
  481. env = getenv("app_nd");
  482. if (env != NULL)
  483. end = simple_strtoul(env, NULL, 16);
  484. if (start >= 0 && end && end > start) {
  485. ausize[IDX_APP] = (end + 1) - start;
  486. aufl_layout[2].start = start;
  487. aufl_layout[2].end = end;
  488. }
  489. start = -1;
  490. end = 0;
  491. env = getenv("disk_st");
  492. if (env != NULL)
  493. start = simple_strtoul(env, NULL, 16);
  494. env = getenv("disk_nd");
  495. if (env != NULL)
  496. end = simple_strtoul(env, NULL, 16);
  497. if (start >= 0 && end && end > start) {
  498. ausize[IDX_DISK] = (end + 1) - start;
  499. aufl_layout[3].start = start;
  500. aufl_layout[3].end = end;
  501. }
  502. /* make certain that HUSH is runnable */
  503. u_boot_hush_start();
  504. /* make sure that we see CTRL-C and save the old state */
  505. old_ctrlc = disable_ctrlc(0);
  506. bitmap_first = 0;
  507. /* just loop thru all the possible files */
  508. for (i = 0; i < AU_MAXFILES; i++) {
  509. sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
  510. debug ("read %s sz %ld hdr %d\n",
  511. aufile[i], sz, sizeof(image_header_t));
  512. if (sz <= 0 || sz <= sizeof(image_header_t)) {
  513. debug ("%s not found\n", aufile[i]);
  514. continue;
  515. }
  516. if (au_check_valid(i, sz) < 0) {
  517. debug ("%s not valid\n", aufile[i]);
  518. continue;
  519. }
  520. #ifdef CONFIG_VFD
  521. /* now that we have a valid file we can display the */
  522. /* bitmap. */
  523. if (bitmap_first == 0) {
  524. env = getenv("bitmap2");
  525. if (env == NULL) {
  526. trab_vfd(0);
  527. } else {
  528. /* not so simple - bitmap2 is supposed to */
  529. /* contain the address of the bitmap */
  530. env = (char *)simple_strtoul(env, NULL, 16);
  531. /* NOTE: these are taken from vfd_logo.h. If that file changes then */
  532. /* these defines MUST also be updated! These may be wrong for bitmap2. */
  533. #define VFD_LOGO_WIDTH 112
  534. #define VFD_LOGO_HEIGHT 72
  535. /* must call transfer_pic directly */
  536. transfer_pic(3, env, VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH);
  537. }
  538. bitmap_first = 1;
  539. }
  540. #endif
  541. /* this is really not a good idea, but it's what the */
  542. /* customer wants. */
  543. cnt = 0;
  544. got_ctrlc = 0;
  545. do {
  546. res = au_do_update(i, sz);
  547. /* let the user break out of the loop */
  548. if (ctrlc() || had_ctrlc()) {
  549. clear_ctrlc();
  550. if (res < 0)
  551. got_ctrlc = 1;
  552. break;
  553. }
  554. cnt++;
  555. #ifdef AU_TEST_ONLY
  556. } while (res < 0 && cnt < 3);
  557. if (cnt < 3)
  558. #else
  559. } while (res < 0);
  560. #endif
  561. /*
  562. * it doesn't make sense to update the EEPROM if the
  563. * update was interrupted by the user due to errors.
  564. */
  565. if (got_ctrlc == 0)
  566. au_update_eeprom(i);
  567. else
  568. /* enable the power switch */
  569. *CPLD_VFD_BK &= ~POWER_OFF;
  570. }
  571. usb_stop();
  572. /* restore the old state */
  573. disable_ctrlc(old_ctrlc);
  574. return 0;
  575. }
  576. #endif /* CONFIG_AUTO_UPDATE */