auto_update.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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) try to do an MSDOS ls
  49. * 2) if preinst.img is found and is not 0 length, load it into
  50. * memory and run it (how to check beforehand if it's a
  51. * valid HUSH command file?)
  52. * 3)
  53. * 4)
  54. * 5)
  55. * 6)
  56. * 7)
  57. * 8)
  58. * 9)
  59. * 10)
  60. * 11)
  61. * 12)
  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. /* layout of the FLASH. ST = start address, ND = end address. */
  79. #ifndef CONFIG_OLD_VERSION /* 16 MB Flash, 32 MB RAM */
  80. #define AU_FL_FIRMWARE_ST 0x00000000
  81. #define AU_FL_FIRMWARE_ND 0x0009FFFF
  82. #define AU_FL_VFD_ST 0x000A0000
  83. #define AU_FL_VFD_ND 0x000BFFFF
  84. #define AU_FL_KERNEL_ST 0x000C0000
  85. #define AU_FL_KERNEL_ND 0x001BFFFF
  86. #define AU_FL_APP_ST 0x001C0000
  87. #define AU_FL_APP_ND 0x005BFFFF
  88. #define AU_FL_DISK_ST 0x005C0000
  89. #define AU_FL_DISK_ND 0x00FFFFFF
  90. #else /* 8 MB Flash, 16 MB RAM */
  91. #define AU_FL_FIRMWARE_ST 0x00000000
  92. #define AU_FL_FIRMWARE_ND 0x0003FFFF
  93. #define AU_FL_KERNEL_ST 0x00040000
  94. #define AU_FL_KERNEL_ND 0x0011FFFF
  95. #define AU_FL_APP_ST 0x00120000
  96. #define AU_FL_APP_ND 0x003FFFFF
  97. #define AU_FL_DISK_ST 0x00400000
  98. #define AU_FL_DISK_ND 0x007DFFFF
  99. #define AU_FL_VFD_ST 0x007E0000
  100. #define AU_FL_VFD_ND 0x007FFFFF
  101. #endif /* CONFIG_OLD_VERSION */
  102. /* a structure with the offsets to values in the EEPROM */
  103. struct eeprom_layout
  104. {
  105. unsigned int time;
  106. unsigned int size;
  107. unsigned int dcrc;
  108. };
  109. /* layout of the EEPROM - offset from the start. All entries are 32 bit. */
  110. #define AU_EEPROM_TIME_PREINST 64
  111. #define AU_EEPROM_SIZE_PREINST 68
  112. #define AU_EEPROM_DCRC_PREINST 72
  113. #define AU_EEPROM_TIME_FIRMWARE 76
  114. #define AU_EEPROM_SIZE_FIRMWARE 80
  115. #define AU_EEPROM_DCRC_FIRMWARE 84
  116. #define AU_EEPROM_TIME_KERNEL 88
  117. #define AU_EEPROM_SIZE_KERNEL 92
  118. #define AU_EEPROM_DCRC_KERNEL 96
  119. #define AU_EEPROM_TIME_APP 100
  120. #define AU_EEPROM_SIZE_APP 104
  121. #define AU_EEPROM_DCRC_APP 108
  122. #define AU_EEPROM_TIME_DISK 112
  123. #define AU_EEPROM_SIZE_DISK 116
  124. #define AU_EEPROM_DCRC_DISK 120
  125. #define AU_EEPROM_TIME_POSTINST 124
  126. #define AU_EEPROM_SIZE_POSTINST 128
  127. #define AU_EEPROM_DCRC_POSTINST 132
  128. static int au_usb_stor_curr_dev; /* current device */
  129. /* max. number of files which could interest us */
  130. #define AU_MAXFILES 7
  131. /* pointers to file names */
  132. char *aufile[AU_MAXFILES];
  133. /* sizes of flash areas for each file */
  134. long ausize[AU_MAXFILES];
  135. /* offsets into the EEEPROM */
  136. struct eeprom_layout auee_off[AU_MAXFILES] = { \
  137. {0}, \
  138. {AU_EEPROM_TIME_PREINST, AU_EEPROM_SIZE_PREINST, AU_EEPROM_DCRC_PREINST,}, \
  139. {AU_EEPROM_TIME_FIRMWARE, AU_EEPROM_SIZE_FIRMWARE, AU_EEPROM_DCRC_FIRMWARE,}, \
  140. {AU_EEPROM_TIME_KERNEL, AU_EEPROM_SIZE_KERNEL, AU_EEPROM_DCRC_KERNEL,}, \
  141. {AU_EEPROM_TIME_APP, AU_EEPROM_SIZE_APP, AU_EEPROM_DCRC_APP,}, \
  142. {AU_EEPROM_TIME_DISK, AU_EEPROM_SIZE_DISK, AU_EEPROM_DCRC_DISK,}, \
  143. {AU_EEPROM_TIME_POSTINST, AU_EEPROM_SIZE_POSTINST, AU_EEPROM_DCRC_POSTINST,} \
  144. };
  145. /* index of each file in the above array */
  146. #define IDX_PREPARE 0
  147. #define IDX_PREINST 1
  148. #define IDX_FIRMWARE 2
  149. #define IDX_KERNEL 3
  150. #define IDX_APP 4
  151. #define IDX_DISK 5
  152. #define IDX_POSTINST 6
  153. /* where to load files into memory */
  154. #define LOAD_ADDR ((unsigned char *)0x0c100000)
  155. /* the disk is the largest image */
  156. #define MAX_LOADSZ ausize[IDX_DISK]
  157. /* externals */
  158. extern int fat_register_device(block_dev_desc_t *, int);
  159. extern int file_fat_detectfs(void);
  160. extern long file_fat_read(const char *, void *, unsigned long);
  161. extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
  162. extern int i2c_write (uchar, uint, int , uchar* , int);
  163. #ifdef CONFIG_VFD
  164. extern int trab_vfd (ulong);
  165. extern int transfer_pic(unsigned char, unsigned char *, int, int);
  166. #endif
  167. extern int i2c_write_multiple (uchar, uint, int, uchar *, int);
  168. extern int i2c_read_multiple (uchar, uint, int, uchar *, int);
  169. int
  170. au_check_valid(int idx, long nbytes)
  171. {
  172. image_header_t *hdr;
  173. unsigned long checksum;
  174. unsigned char buf[4];
  175. hdr = (image_header_t *)LOAD_ADDR;
  176. /* check the easy ones first */
  177. #ifdef CHECK_VALID_DEBUG
  178. printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
  179. printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
  180. printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
  181. printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
  182. #endif
  183. if (ntohl(hdr->ih_magic) != IH_MAGIC ||
  184. hdr->ih_arch != IH_CPU_ARM ||
  185. nbytes < ntohl(hdr->ih_size)) {
  186. printf ("Image %s Bad MAGIC or ARCH or SIZE\n", aufile[idx]);
  187. return -1;
  188. }
  189. /* check the hdr CRC */
  190. checksum = ntohl(hdr->ih_hcrc);
  191. hdr->ih_hcrc = 0;
  192. if (crc32 (0, (char *)hdr, sizeof(*hdr)) != checksum) {
  193. printf ("Image %s Bad Header Checksum\n", aufile[idx]);
  194. return -1;
  195. }
  196. /* check the data CRC */
  197. checksum = ntohl(hdr->ih_dcrc);
  198. if (crc32 (0, (char *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
  199. != checksum)
  200. {
  201. printf ("Image %s Bad Data Checksum\n", aufile[idx]);
  202. return -1;
  203. }
  204. /* check the type - could do this all in one gigantic if() */
  205. if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
  206. printf ("Image %s Wrong Type\n", aufile[idx]);
  207. return -1;
  208. }
  209. if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
  210. printf ("Image %s Wrong Type\n", aufile[idx]);
  211. return -1;
  212. }
  213. if ((idx == IDX_DISK || idx == IDX_APP)
  214. && (hdr->ih_type != IH_TYPE_RAMDISK))
  215. {
  216. printf ("Image %s Wrong Type\n", aufile[idx]);
  217. return -1;
  218. }
  219. if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
  220. && (hdr->ih_type != IH_TYPE_SCRIPT))
  221. {
  222. printf ("Image %s Wrong Type\n", aufile[idx]);
  223. return -1;
  224. }
  225. /* special case for prepare.img */
  226. if (idx == IDX_PREPARE)
  227. return 0;
  228. /* check the size does not exceed space in flash */
  229. if ((ausize[idx] != 0) && (ausize[idx] < ntohl(hdr->ih_size))) {
  230. printf ("Image %s is bigger than FLASH\n", aufile[idx]);
  231. return -1;
  232. }
  233. /* check the time stamp from the EEPROM */
  234. /* read it in */
  235. i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
  236. #ifdef CHECK_VALID_DEBUG
  237. printf("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x as int %#x time %#x\n",
  238. buf[0], buf[1], buf[2], buf[3], *((unsigned int *)buf), ntohl(hdr->ih_time));
  239. #endif
  240. /* check it */
  241. if (*((unsigned int *)buf) >= ntohl(hdr->ih_time)) {
  242. printf ("Image %s is too old\n", aufile[idx]);
  243. return -1;
  244. }
  245. return 0;
  246. }
  247. /* power control defines */
  248. #define CPLD_VFD_BK ((volatile char *)0x04038002)
  249. #define POWER_OFF (1 << 1)
  250. int
  251. au_do_update(int idx)
  252. {
  253. image_header_t *hdr;
  254. char *addr;
  255. hdr = (image_header_t *)LOAD_ADDR;
  256. /* disable the power switch */
  257. *CPLD_VFD_BK |= POWER_OFF;
  258. /* execute a script */
  259. if (hdr->ih_type == IH_TYPE_SCRIPT) {
  260. addr = (char *)((char *)hdr + sizeof(*hdr) + 8);
  261. parse_string_outer(addr,
  262. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
  263. return 0;
  264. }
  265. return 0;
  266. }
  267. int
  268. au_update_eeprom(int idx)
  269. {
  270. image_header_t *hdr;
  271. hdr = (image_header_t *)LOAD_ADDR;
  272. /* enable the power switch */
  273. *CPLD_VFD_BK &= ~POWER_OFF;
  274. return 0;
  275. }
  276. /*
  277. * this is called from board_init() after the hardware has been set up
  278. * and is usable. That seems like a good time to do this.
  279. * Right now the return value is ignored.
  280. */
  281. int
  282. do_auto_update(void)
  283. {
  284. block_dev_desc_t *stor_dev;
  285. long sz;
  286. int i, res, bitmap_first;
  287. char *env;
  288. #ifdef ERASE_EEPROM
  289. int arr[18];
  290. memset(arr, 0, sizeof(arr));
  291. i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr));
  292. #endif
  293. au_usb_stor_curr_dev = -1;
  294. /* start USB */
  295. if (usb_stop() < 0) {
  296. debug ("usb_stop failed\n");
  297. return -1;
  298. }
  299. if (usb_init() < 0) {
  300. debug ("usb_init failed\n");
  301. return -1;
  302. }
  303. /*
  304. * check whether a storage device is attached (assume that it's
  305. * a USB memory stick, since nothing else should be attached).
  306. */
  307. au_usb_stor_curr_dev = usb_stor_scan(1);
  308. if (au_usb_stor_curr_dev == -1) {
  309. debug ("No device found. Not initialized?\n");
  310. return -1;
  311. }
  312. /* check whether it has a partition table */
  313. stor_dev = usb_stor_get_dev(au_usb_stor_curr_dev);
  314. if (stor_dev->type == DEV_TYPE_UNKNOWN) {
  315. debug ("uknown device type\n");
  316. return -1;
  317. }
  318. if (fat_register_device(stor_dev, 1) != 0) {
  319. debug ("Unable to use USB %d:%d for fatls\n",
  320. au_usb_stor_curr_dev, 1);
  321. return -1;
  322. }
  323. if (file_fat_detectfs() != 0) {
  324. debug ("file_fat_detectfs failed\n");
  325. }
  326. /* initialize the array of file names */
  327. memset(aufile, 0, sizeof(aufile));
  328. aufile[IDX_PREPARE] = AU_PREPARE;
  329. aufile[IDX_PREINST] = AU_PREINST;
  330. aufile[IDX_FIRMWARE] = AU_FIRMWARE;
  331. aufile[IDX_KERNEL] = AU_KERNEL;
  332. aufile[IDX_APP] = AU_APP;
  333. aufile[IDX_DISK] = AU_DISK;
  334. aufile[IDX_POSTINST] = AU_POSTINST;
  335. /* initialize the array of flash sizes */
  336. memset(ausize, 0, sizeof(ausize));
  337. ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
  338. ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
  339. ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST;
  340. ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST;
  341. bitmap_first = 0;
  342. /* just loop thru all the possible files */
  343. for (i = 0; i < AU_MAXFILES; i++) {
  344. sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
  345. debug ("read %s sz %ld hdr %d\n",
  346. aufile[i], sz, sizeof(image_header_t));
  347. if (sz <= 0 || sz <= sizeof(image_header_t)) {
  348. debug ("%s not found\n", aufile[i]);
  349. continue;
  350. }
  351. if (au_check_valid(i, sz) < 0) {
  352. debug ("%s not valid\n", aufile[i]);
  353. continue;
  354. }
  355. #ifdef CONFIG_VFD
  356. /* now that we have a valid file we can display the */
  357. /* bitmap. */
  358. if (bitmap_first == 0) {
  359. env = getenv("bitmap2");
  360. if (env == NULL) {
  361. trab_vfd(0);
  362. } else {
  363. /* not so simple - bitmap2 is supposed to */
  364. /* contain the address of the bitmap */
  365. env = (char *)simple_strtoul(env, NULL, 16);
  366. /* NOTE: these are taken from vfd_logo.h. If that file changes then */
  367. /* these defines MUST also be updated! These may be wrong for bitmap2. */
  368. #define VFD_LOGO_WIDTH 112
  369. #define VFD_LOGO_HEIGHT 72
  370. /* must call transfer_pic directly */
  371. transfer_pic(3, env, VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH);
  372. }
  373. bitmap_first = 1;
  374. }
  375. #endif
  376. /* this is really not a good idea, but it's what the */
  377. /* customer wants. */
  378. do {
  379. res = au_do_update(i);
  380. } while (res < 0);
  381. au_update_eeprom(i);
  382. }
  383. return 0;
  384. }
  385. #endif /* CONFIG_AUTO_UPDATE */