auto_update.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * (C) Copyright 2006
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #include <command.h>
  22. #include <malloc.h>
  23. #include <image.h>
  24. #include <asm/byteorder.h>
  25. #include <usb.h>
  26. #ifdef CFG_HUSH_PARSER
  27. #include <hush.h>
  28. #endif
  29. #ifdef CONFIG_AUTO_UPDATE
  30. #ifndef CONFIG_USB_OHCI
  31. #error "must define CONFIG_USB_OHCI"
  32. #endif
  33. #ifndef CONFIG_USB_STORAGE
  34. #error "must define CONFIG_USB_STORAGE"
  35. #endif
  36. #ifndef CFG_HUSH_PARSER
  37. #error "must define CFG_HUSH_PARSER"
  38. #endif
  39. #if !(CONFIG_COMMANDS & CFG_CMD_FAT)
  40. #error "must define CFG_CMD_FAT"
  41. #endif
  42. /*
  43. * Check whether a USB memory stick is plugged in.
  44. * If one is found:
  45. * 1) if prepare.img ist found load it into memory. If it is
  46. * valid then run it.
  47. * 2) if preinst.img is found load it into memory. If it is
  48. * valid then run it. Update the EEPROM.
  49. * 3) if firmw_01.img is found load it into memory. If it is valid,
  50. * burn it into FLASH and update the EEPROM.
  51. * 4) if kernl_01.img is found load it into memory. If it is valid,
  52. * burn it into FLASH and update the EEPROM.
  53. * 5) if app.img is found load it into memory. If it is valid,
  54. * burn it into FLASH and update the EEPROM.
  55. * 6) if disk.img is found load it into memory. If it is valid,
  56. * burn it into FLASH and update the EEPROM.
  57. * 7) if postinst.img is found load it into memory. If it is
  58. * valid then run it. Update the EEPROM.
  59. */
  60. #undef AU_DEBUG
  61. #undef debug
  62. #ifdef AU_DEBUG
  63. #define debug(fmt,args...) printf (fmt ,##args)
  64. #else
  65. #define debug(fmt,args...)
  66. #endif /* AU_DEBUG */
  67. /* possible names of files on the USB stick. */
  68. #define AU_FIRMWARE "u-boot.img"
  69. #define AU_KERNEL "kernel.img"
  70. struct flash_layout
  71. {
  72. long start;
  73. long end;
  74. };
  75. /* layout of the FLASH. ST = start address, ND = end address. */
  76. #define AU_FL_FIRMWARE_ST 0xfC000000
  77. #define AU_FL_FIRMWARE_ND 0xfC03FFFF
  78. #define AU_FL_KERNEL_ST 0xfC0C0000
  79. #define AU_FL_KERNEL_ND 0xfC1BFFFF
  80. static int au_usb_stor_curr_dev; /* current device */
  81. /* index of each file in the following arrays */
  82. #define IDX_FIRMWARE 0
  83. #define IDX_KERNEL 1
  84. /* max. number of files which could interest us */
  85. #define AU_MAXFILES 2
  86. /* pointers to file names */
  87. char *aufile[AU_MAXFILES];
  88. /* sizes of flash areas for each file */
  89. long ausize[AU_MAXFILES];
  90. /* array of flash areas start and end addresses */
  91. struct flash_layout aufl_layout[AU_MAXFILES] = { \
  92. {AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND,}, \
  93. {AU_FL_KERNEL_ST, AU_FL_KERNEL_ND,}, \
  94. };
  95. /* where to load files into memory */
  96. #define LOAD_ADDR ((unsigned char *)0x00200000)
  97. /* the app is the largest image */
  98. #define MAX_LOADSZ ausize[IDX_KERNEL]
  99. /*i2c address of the keypad status*/
  100. #define I2C_PSOC_KEYPAD_ADDR 0x53
  101. /* keypad mask */
  102. #define KEYPAD_ROW 3
  103. #define KEYPAD_COL 3
  104. #define KEYPAD_MASK_LO ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*4-4)))&0xFF)
  105. #define KEYPAD_MASK_HI ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*4-4)))>>8)
  106. /* externals */
  107. extern int fat_register_device(block_dev_desc_t *, int);
  108. extern int file_fat_detectfs(void);
  109. extern long file_fat_read(const char *, void *, unsigned long);
  110. extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
  111. extern int flash_sect_erase(ulong, ulong);
  112. extern int flash_sect_protect (int, ulong, ulong);
  113. extern int flash_write (char *, ulong, ulong);
  114. /* change char* to void* to shutup the compiler */
  115. extern block_dev_desc_t *get_dev (char*, int);
  116. extern int u_boot_hush_start(void);
  117. int
  118. au_check_cksum_valid(int idx, long nbytes)
  119. {
  120. image_header_t *hdr;
  121. unsigned long checksum;
  122. hdr = (image_header_t *)LOAD_ADDR;
  123. if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size)))
  124. {
  125. printf ("Image %s bad total SIZE\n", aufile[idx]);
  126. return -1;
  127. }
  128. /* check the data CRC */
  129. checksum = ntohl(hdr->ih_dcrc);
  130. if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
  131. != checksum)
  132. {
  133. printf ("Image %s bad data checksum\n", aufile[idx]);
  134. return -1;
  135. }
  136. return 0;
  137. }
  138. int
  139. au_check_header_valid(int idx, long nbytes)
  140. {
  141. image_header_t *hdr;
  142. unsigned long checksum;
  143. unsigned char buf[4];
  144. hdr = (image_header_t *)LOAD_ADDR;
  145. /* check the easy ones first */
  146. #undef CHECK_VALID_DEBUG
  147. #ifdef CHECK_VALID_DEBUG
  148. printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
  149. printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
  150. printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
  151. printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
  152. #endif
  153. if (nbytes < sizeof(*hdr))
  154. {
  155. printf ("Image %s bad header SIZE\n", aufile[idx]);
  156. return -1;
  157. }
  158. if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_PPC)
  159. {
  160. printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
  161. return -1;
  162. }
  163. /* check the hdr CRC */
  164. checksum = ntohl(hdr->ih_hcrc);
  165. hdr->ih_hcrc = 0;
  166. if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
  167. printf ("Image %s bad header checksum\n", aufile[idx]);
  168. return -1;
  169. }
  170. hdr->ih_hcrc = htonl(checksum);
  171. /* check the type - could do this all in one gigantic if() */
  172. if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
  173. printf ("Image %s wrong type\n", aufile[idx]);
  174. return -1;
  175. }
  176. if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
  177. printf ("Image %s wrong type\n", aufile[idx]);
  178. return -1;
  179. }
  180. /* recycle checksum */
  181. checksum = ntohl(hdr->ih_size);
  182. /* for kernel and app the image header must also fit into flash */
  183. if (idx != IDX_FIRMWARE)
  184. checksum += sizeof(*hdr);
  185. /* check the size does not exceed space in flash. HUSH scripts */
  186. /* all have ausize[] set to 0 */
  187. if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
  188. printf ("Image %s is bigger than FLASH\n", aufile[idx]);
  189. return -1;
  190. }
  191. return 0;
  192. }
  193. int
  194. au_do_update(int idx, long sz)
  195. {
  196. image_header_t *hdr;
  197. char *addr;
  198. long start, end;
  199. int off, rc;
  200. uint nbytes;
  201. hdr = (image_header_t *)LOAD_ADDR;
  202. /* execute a script */
  203. if (hdr->ih_type == IH_TYPE_SCRIPT) {
  204. addr = (char *)((char *)hdr + sizeof(*hdr));
  205. /* stick a NULL at the end of the script, otherwise */
  206. /* parse_string_outer() runs off the end. */
  207. addr[ntohl(hdr->ih_size)] = 0;
  208. addr += 8;
  209. parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
  210. return 0;
  211. }
  212. start = aufl_layout[idx].start;
  213. end = aufl_layout[idx].end;
  214. /* unprotect the address range */
  215. /* this assumes that ONLY the firmware is protected! */
  216. if (idx == IDX_FIRMWARE) {
  217. #undef AU_UPDATE_TEST
  218. #ifdef AU_UPDATE_TEST
  219. /* erase it where Linux goes */
  220. start = aufl_layout[1].start;
  221. end = aufl_layout[1].end;
  222. #endif
  223. flash_sect_protect(0, start, end);
  224. }
  225. /*
  226. * erase the address range.
  227. */
  228. debug ("flash_sect_erase(%lx, %lx);\n", start, end);
  229. flash_sect_erase(start, end);
  230. wait_ms(100);
  231. /* strip the header - except for the kernel and ramdisk */
  232. if (hdr->ih_type == IH_TYPE_KERNEL) {
  233. addr = (char *)hdr;
  234. off = sizeof(*hdr);
  235. nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
  236. } else {
  237. addr = (char *)((char *)hdr + sizeof(*hdr));
  238. #ifdef AU_UPDATE_TEST
  239. /* copy it to where Linux goes */
  240. if (idx == IDX_FIRMWARE)
  241. start = aufl_layout[1].start;
  242. #endif
  243. off = 0;
  244. nbytes = ntohl(hdr->ih_size);
  245. }
  246. /* copy the data from RAM to FLASH */
  247. debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
  248. rc = flash_write(addr, start, nbytes);
  249. if (rc != 0) {
  250. printf("Flashing failed due to error %d\n", rc);
  251. return -1;
  252. }
  253. /* check the dcrc of the copy */
  254. if (crc32 (0, (uchar *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
  255. printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
  256. return -1;
  257. }
  258. /* protect the address range */
  259. /* this assumes that ONLY the firmware is protected! */
  260. if (idx == IDX_FIRMWARE)
  261. flash_sect_protect(1, start, end);
  262. return 0;
  263. }
  264. /*
  265. * this is called from board_init() after the hardware has been set up
  266. * and is usable. That seems like a good time to do this.
  267. * Right now the return value is ignored.
  268. */
  269. int
  270. do_auto_update(void)
  271. {
  272. block_dev_desc_t *stor_dev;
  273. long sz;
  274. int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc;
  275. char *env;
  276. long start, end;
  277. char keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
  278. /*
  279. * Read keypad status
  280. */
  281. i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
  282. wait_ms(500);
  283. i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
  284. /*
  285. * Check keypad
  286. */
  287. if ( !(keypad_status1[0] & KEYPAD_MASK_HI) ||
  288. (keypad_status1[0] != keypad_status2[0])) {
  289. return 0;
  290. }
  291. if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
  292. (keypad_status1[1] != keypad_status2[1])) {
  293. return 0;
  294. }
  295. au_usb_stor_curr_dev = -1;
  296. /* start USB */
  297. if (usb_stop() < 0) {
  298. debug ("usb_stop failed\n");
  299. return -1;
  300. }
  301. if (usb_init() < 0) {
  302. debug ("usb_init failed\n");
  303. return -1;
  304. }
  305. /*
  306. * check whether a storage device is attached (assume that it's
  307. * a USB memory stick, since nothing else should be attached).
  308. */
  309. au_usb_stor_curr_dev = usb_stor_scan(0);
  310. if (au_usb_stor_curr_dev == -1) {
  311. debug ("No device found. Not initialized?\n");
  312. return -1;
  313. }
  314. /* check whether it has a partition table */
  315. stor_dev = get_dev("usb", 0);
  316. if (stor_dev == NULL) {
  317. debug ("uknown device type\n");
  318. return -1;
  319. }
  320. if (fat_register_device(stor_dev, 1) != 0) {
  321. debug ("Unable to use USB %d:%d for fatls\n",
  322. au_usb_stor_curr_dev, 1);
  323. return -1;
  324. }
  325. if (file_fat_detectfs() != 0) {
  326. debug ("file_fat_detectfs failed\n");
  327. }
  328. /* initialize the array of file names */
  329. memset(aufile, 0, sizeof(aufile));
  330. aufile[IDX_FIRMWARE] = AU_FIRMWARE;
  331. aufile[IDX_KERNEL] = AU_KERNEL;
  332. /* initialize the array of flash sizes */
  333. memset(ausize, 0, sizeof(ausize));
  334. ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
  335. ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
  336. /*
  337. * now check whether start and end are defined using environment
  338. * variables.
  339. */
  340. start = -1;
  341. end = 0;
  342. env = getenv("firmware_st");
  343. if (env != NULL)
  344. start = simple_strtoul(env, NULL, 16);
  345. env = getenv("firmware_nd");
  346. if (env != NULL)
  347. end = simple_strtoul(env, NULL, 16);
  348. if (start >= 0 && end && end > start) {
  349. ausize[IDX_FIRMWARE] = (end + 1) - start;
  350. aufl_layout[0].start = start;
  351. aufl_layout[0].end = end;
  352. }
  353. start = -1;
  354. end = 0;
  355. env = getenv("kernel_st");
  356. if (env != NULL)
  357. start = simple_strtoul(env, NULL, 16);
  358. env = getenv("kernel_nd");
  359. if (env != NULL)
  360. end = simple_strtoul(env, NULL, 16);
  361. if (start >= 0 && end && end > start) {
  362. ausize[IDX_KERNEL] = (end + 1) - start;
  363. aufl_layout[1].start = start;
  364. aufl_layout[1].end = end;
  365. }
  366. /* make certain that HUSH is runnable */
  367. u_boot_hush_start();
  368. /* make sure that we see CTRL-C and save the old state */
  369. old_ctrlc = disable_ctrlc(0);
  370. bitmap_first = 0;
  371. /* just loop thru all the possible files */
  372. for (i = 0; i < AU_MAXFILES; i++) {
  373. /* just read the header */
  374. sz = file_fat_read(aufile[i], LOAD_ADDR, sizeof(image_header_t));
  375. debug ("read %s sz %ld hdr %d\n",
  376. aufile[i], sz, sizeof(image_header_t));
  377. if (sz <= 0 || sz < sizeof(image_header_t)) {
  378. debug ("%s not found\n", aufile[i]);
  379. continue;
  380. }
  381. if (au_check_header_valid(i, sz) < 0) {
  382. debug ("%s header not valid\n", aufile[i]);
  383. continue;
  384. }
  385. sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
  386. debug ("read %s sz %ld hdr %d\n",
  387. aufile[i], sz, sizeof(image_header_t));
  388. if (sz <= 0 || sz <= sizeof(image_header_t)) {
  389. debug ("%s not found\n", aufile[i]);
  390. continue;
  391. }
  392. if (au_check_cksum_valid(i, sz) < 0) {
  393. debug ("%s checksum not valid\n", aufile[i]);
  394. continue;
  395. }
  396. /* this is really not a good idea, but it's what the */
  397. /* customer wants. */
  398. cnt = 0;
  399. got_ctrlc = 0;
  400. do {
  401. res = au_do_update(i, sz);
  402. /* let the user break out of the loop */
  403. if (ctrlc() || had_ctrlc()) {
  404. clear_ctrlc();
  405. if (res < 0)
  406. got_ctrlc = 1;
  407. break;
  408. }
  409. cnt++;
  410. #ifdef AU_TEST_ONLY
  411. } while (res < 0 && cnt < 3);
  412. if (cnt < 3)
  413. #else
  414. } while (res < 0);
  415. #endif
  416. }
  417. usb_stop();
  418. /* restore the old state */
  419. disable_ctrlc(old_ctrlc);
  420. return 0;
  421. }
  422. #endif /* CONFIG_AUTO_UPDATE */