auto_update.c 19 KB

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