auto_update.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. #include <part.h>
  27. #ifdef CFG_HUSH_PARSER
  28. #include <hush.h>
  29. #endif
  30. #ifdef CONFIG_AUTO_UPDATE
  31. #ifndef CONFIG_USB_OHCI
  32. #error "must define CONFIG_USB_OHCI"
  33. #endif
  34. #ifndef CONFIG_USB_STORAGE
  35. #error "must define CONFIG_USB_STORAGE"
  36. #endif
  37. #ifndef CFG_HUSH_PARSER
  38. #error "must define CFG_HUSH_PARSER"
  39. #endif
  40. #if !defined(CONFIG_CMD_FAT)
  41. #error "must define CONFIG_CMD_FAT"
  42. #endif
  43. #undef AU_DEBUG
  44. #undef debug
  45. #ifdef AU_DEBUG
  46. #define debug(fmt,args...) printf (fmt ,##args)
  47. #else
  48. #define debug(fmt,args...)
  49. #endif /* AU_DEBUG */
  50. /* possible names of files on the USB stick. */
  51. #define AU_FIRMWARE "u-boot.img"
  52. #define AU_KERNEL "kernel.img"
  53. #define AU_ROOTFS "rootfs.img"
  54. struct flash_layout {
  55. long start;
  56. long end;
  57. };
  58. /* layout of the FLASH. ST = start address, ND = end address. */
  59. #define AU_FL_FIRMWARE_ST 0xfC000000
  60. #define AU_FL_FIRMWARE_ND 0xfC03FFFF
  61. #define AU_FL_KERNEL_ST 0xfC0C0000
  62. #define AU_FL_KERNEL_ND 0xfC1BFFFF
  63. #define AU_FL_ROOTFS_ST 0xFC1C0000
  64. #define AU_FL_ROOTFS_ND 0xFCFBFFFF
  65. static int au_usb_stor_curr_dev; /* current device */
  66. /* index of each file in the following arrays */
  67. #define IDX_FIRMWARE 0
  68. #define IDX_KERNEL 1
  69. #define IDX_ROOTFS 2
  70. /* max. number of files which could interest us */
  71. #define AU_MAXFILES 3
  72. /* pointers to file names */
  73. char *aufile[AU_MAXFILES] = {
  74. AU_FIRMWARE,
  75. AU_KERNEL,
  76. AU_ROOTFS
  77. };
  78. /* sizes of flash areas for each file */
  79. long ausize[AU_MAXFILES] = {
  80. (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST,
  81. (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST,
  82. (AU_FL_ROOTFS_ND + 1) - AU_FL_ROOTFS_ST,
  83. };
  84. /* array of flash areas start and end addresses */
  85. struct flash_layout aufl_layout[AU_MAXFILES] = {
  86. { AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND, },
  87. { AU_FL_KERNEL_ST, AU_FL_KERNEL_ND, },
  88. { AU_FL_ROOTFS_ST, AU_FL_ROOTFS_ND, },
  89. };
  90. ulong totsize;
  91. /* where to load files into memory */
  92. #define LOAD_ADDR ((unsigned char *)0x00200000)
  93. /* the root file system is the largest image */
  94. #define MAX_LOADSZ ausize[IDX_ROOTFS]
  95. /*i2c address of the keypad status*/
  96. #define I2C_PSOC_KEYPAD_ADDR 0x53
  97. /* keypad mask */
  98. #define KEYPAD_ROW 2
  99. #define KEYPAD_COL 2
  100. #define KEYPAD_MASK_LO ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))&0xFF)
  101. #define KEYPAD_MASK_HI ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))>>8)
  102. /* externals */
  103. extern int fat_register_device(block_dev_desc_t *, int);
  104. extern int file_fat_detectfs(void);
  105. extern long file_fat_read(const char *, void *, unsigned long);
  106. extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
  107. extern int flash_sect_erase(ulong, ulong);
  108. extern int flash_sect_protect (int, ulong, ulong);
  109. extern int flash_write (char *, ulong, ulong);
  110. extern int u_boot_hush_start(void);
  111. #ifdef CONFIG_PROGRESSBAR
  112. extern void show_progress(int, int);
  113. extern void lcd_puts (char *);
  114. extern void lcd_enable(void);
  115. #endif
  116. int au_check_cksum_valid(int idx, long nbytes)
  117. {
  118. image_header_t *hdr;
  119. hdr = (image_header_t *)LOAD_ADDR;
  120. #if defined(CONFIG_FIT)
  121. if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
  122. puts ("Non legacy image format not supported\n");
  123. return -1;
  124. }
  125. #endif
  126. if (nbytes != image_get_image_size (hdr)) {
  127. printf ("Image %s bad total SIZE\n", aufile[idx]);
  128. return -1;
  129. }
  130. /* check the data CRC */
  131. if (!image_check_dcrc (hdr)) {
  132. printf ("Image %s bad data checksum\n", aufile[idx]);
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. int au_check_header_valid(int idx, long nbytes)
  138. {
  139. image_header_t *hdr;
  140. unsigned long checksum, fsize;
  141. hdr = (image_header_t *)LOAD_ADDR;
  142. #if defined(CONFIG_FIT)
  143. if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
  144. puts ("Non legacy image format not supported\n");
  145. return -1;
  146. }
  147. #endif
  148. /* check the easy ones first */
  149. #undef CHECK_VALID_DEBUG
  150. #ifdef CHECK_VALID_DEBUG
  151. printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
  152. printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM);
  153. printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
  154. printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
  155. #endif
  156. if (nbytes < image_get_header_size ()) {
  157. printf ("Image %s bad header SIZE\n", aufile[idx]);
  158. ausize[idx] = 0;
  159. return -1;
  160. }
  161. if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) {
  162. printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
  163. ausize[idx] = 0;
  164. return -1;
  165. }
  166. /* check the hdr CRC */
  167. if (!image_check_hcrc (hdr)) {
  168. printf ("Image %s bad header checksum\n", aufile[idx]);
  169. ausize[idx] = 0;
  170. return -1;
  171. }
  172. /* check the type - could do this all in one gigantic if() */
  173. if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
  174. printf ("Image %s wrong type\n", aufile[idx]);
  175. ausize[idx] = 0;
  176. return -1;
  177. }
  178. if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
  179. printf ("Image %s wrong type\n", aufile[idx]);
  180. ausize[idx] = 0;
  181. return -1;
  182. }
  183. if ((idx == IDX_ROOTFS) &&
  184. (!image_check_type (hdr, IH_TYPE_RAMDISK) &&
  185. !image_check_type (hdr, IH_TYPE_FILESYSTEM))) {
  186. printf ("Image %s wrong type\n", aufile[idx]);
  187. ausize[idx] = 0;
  188. return -1;
  189. }
  190. /* recycle checksum */
  191. checksum = image_get_data_size (hdr);
  192. fsize = checksum + image_get_header_size ();
  193. /* for kernel and ramdisk the image header must also fit into flash */
  194. if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK))
  195. checksum += image_get_header_size ();
  196. /* check the size does not exceed space in flash. HUSH scripts */
  197. if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
  198. printf ("Image %s is bigger than FLASH\n", aufile[idx]);
  199. ausize[idx] = 0;
  200. return -1;
  201. }
  202. /* Update with the real filesize */
  203. ausize[idx] = fsize;
  204. return checksum; /* return size to be written to flash */
  205. }
  206. int au_do_update(int idx, long sz)
  207. {
  208. image_header_t *hdr;
  209. char *addr;
  210. long start, end;
  211. int off, rc;
  212. uint nbytes;
  213. hdr = (image_header_t *)LOAD_ADDR;
  214. #if defined(CONFIG_FIT)
  215. if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
  216. puts ("Non legacy image format not supported\n");
  217. return -1;
  218. }
  219. #endif
  220. /* execute a script */
  221. if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
  222. addr = (char *)((char *)hdr + image_get_header_size ());
  223. /* stick a NULL at the end of the script, otherwise */
  224. /* parse_string_outer() runs off the end. */
  225. addr[image_get_data_size (hdr)] = 0;
  226. addr += 8;
  227. parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
  228. return 0;
  229. }
  230. start = aufl_layout[idx].start;
  231. end = aufl_layout[idx].end;
  232. /* unprotect the address range */
  233. /* this assumes that ONLY the firmware is protected! */
  234. if (idx == IDX_FIRMWARE) {
  235. #undef AU_UPDATE_TEST
  236. #ifdef AU_UPDATE_TEST
  237. /* erase it where Linux goes */
  238. start = aufl_layout[1].start;
  239. end = aufl_layout[1].end;
  240. #endif
  241. flash_sect_protect(0, start, end);
  242. }
  243. /*
  244. * erase the address range.
  245. */
  246. debug ("flash_sect_erase(%lx, %lx);\n", start, end);
  247. flash_sect_erase(start, end);
  248. wait_ms(100);
  249. #ifdef CONFIG_PROGRESSBAR
  250. show_progress(end - start, totsize);
  251. #endif
  252. /* strip the header - except for the kernel and ramdisk */
  253. if (image_check_type (hdr, IH_TYPE_KERNEL) ||
  254. image_check_type (hdr, IH_TYPE_RAMDISK)) {
  255. addr = (char *)hdr;
  256. off = image_get_header_size ();
  257. nbytes = image_get_image_size (hdr);
  258. } else {
  259. addr = (char *)((char *)hdr + image_get_header_size ());
  260. #ifdef AU_UPDATE_TEST
  261. /* copy it to where Linux goes */
  262. if (idx == IDX_FIRMWARE)
  263. start = aufl_layout[1].start;
  264. #endif
  265. off = 0;
  266. nbytes = image_get_data_size (hdr);
  267. }
  268. /* copy the data from RAM to FLASH */
  269. debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
  270. rc = flash_write(addr, start, nbytes);
  271. if (rc != 0) {
  272. printf("Flashing failed due to error %d\n", rc);
  273. return -1;
  274. }
  275. #ifdef CONFIG_PROGRESSBAR
  276. show_progress(nbytes, totsize);
  277. #endif
  278. /* check the data CRC of the copy */
  279. if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
  280. image_get_dcrc (hdr)) {
  281. printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
  282. return -1;
  283. }
  284. /* protect the address range */
  285. /* this assumes that ONLY the firmware is protected! */
  286. if (idx == IDX_FIRMWARE)
  287. flash_sect_protect(1, start, end);
  288. return 0;
  289. }
  290. /*
  291. * this is called from board_init() after the hardware has been set up
  292. * and is usable. That seems like a good time to do this.
  293. * Right now the return value is ignored.
  294. */
  295. int do_auto_update(void)
  296. {
  297. block_dev_desc_t *stor_dev;
  298. long sz;
  299. int i, res = 0, bitmap_first, cnt, old_ctrlc, got_ctrlc;
  300. char *env;
  301. long start, end;
  302. #if 0 /* disable key-press detection to speed up boot-up time */
  303. uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
  304. /*
  305. * Read keypad status
  306. */
  307. i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
  308. wait_ms(500);
  309. i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
  310. /*
  311. * Check keypad
  312. */
  313. if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
  314. (keypad_status1[1] != keypad_status2[1])) {
  315. return 0;
  316. }
  317. #endif
  318. au_usb_stor_curr_dev = -1;
  319. /* start USB */
  320. if (usb_stop() < 0) {
  321. debug ("usb_stop failed\n");
  322. return -1;
  323. }
  324. if (usb_init() < 0) {
  325. debug ("usb_init failed\n");
  326. return -1;
  327. }
  328. /*
  329. * check whether a storage device is attached (assume that it's
  330. * a USB memory stick, since nothing else should be attached).
  331. */
  332. au_usb_stor_curr_dev = usb_stor_scan(0);
  333. if (au_usb_stor_curr_dev == -1) {
  334. debug ("No device found. Not initialized?\n");
  335. res = -1;
  336. goto xit;
  337. }
  338. /* check whether it has a partition table */
  339. stor_dev = get_dev("usb", 0);
  340. if (stor_dev == NULL) {
  341. debug ("uknown device type\n");
  342. res = -1;
  343. goto xit;
  344. }
  345. if (fat_register_device(stor_dev, 1) != 0) {
  346. debug ("Unable to use USB %d:%d for fatls\n",
  347. au_usb_stor_curr_dev, 1);
  348. res = -1;
  349. goto xit;
  350. }
  351. if (file_fat_detectfs() != 0) {
  352. debug ("file_fat_detectfs failed\n");
  353. }
  354. /*
  355. * now check whether start and end are defined using environment
  356. * variables.
  357. */
  358. start = -1;
  359. end = 0;
  360. env = getenv("firmware_st");
  361. if (env != NULL)
  362. start = simple_strtoul(env, NULL, 16);
  363. env = getenv("firmware_nd");
  364. if (env != NULL)
  365. end = simple_strtoul(env, NULL, 16);
  366. if (start >= 0 && end && end > start) {
  367. ausize[IDX_FIRMWARE] = (end + 1) - start;
  368. aufl_layout[IDX_FIRMWARE].start = start;
  369. aufl_layout[IDX_FIRMWARE].end = end;
  370. }
  371. start = -1;
  372. end = 0;
  373. env = getenv("kernel_st");
  374. if (env != NULL)
  375. start = simple_strtoul(env, NULL, 16);
  376. env = getenv("kernel_nd");
  377. if (env != NULL)
  378. end = simple_strtoul(env, NULL, 16);
  379. if (start >= 0 && end && end > start) {
  380. ausize[IDX_KERNEL] = (end + 1) - start;
  381. aufl_layout[IDX_KERNEL].start = start;
  382. aufl_layout[IDX_KERNEL].end = end;
  383. }
  384. start = -1;
  385. end = 0;
  386. env = getenv("rootfs_st");
  387. if (env != NULL)
  388. start = simple_strtoul(env, NULL, 16);
  389. env = getenv("rootfs_nd");
  390. if (env != NULL)
  391. end = simple_strtoul(env, NULL, 16);
  392. if (start >= 0 && end && end > start) {
  393. ausize[IDX_ROOTFS] = (end + 1) - start;
  394. aufl_layout[IDX_ROOTFS].start = start;
  395. aufl_layout[IDX_ROOTFS].end = end;
  396. }
  397. /* make certain that HUSH is runnable */
  398. u_boot_hush_start();
  399. /* make sure that we see CTRL-C and save the old state */
  400. old_ctrlc = disable_ctrlc(0);
  401. bitmap_first = 0;
  402. /* validate the images first */
  403. for (i = 0; i < AU_MAXFILES; i++) {
  404. ulong imsize;
  405. /* just read the header */
  406. sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
  407. debug ("read %s sz %ld hdr %d\n",
  408. aufile[i], sz, image_get_header_size ());
  409. if (sz <= 0 || sz < image_get_header_size ()) {
  410. debug ("%s not found\n", aufile[i]);
  411. ausize[i] = 0;
  412. continue;
  413. }
  414. /* au_check_header_valid() updates ausize[] */
  415. if ((imsize = au_check_header_valid(i, sz)) < 0) {
  416. debug ("%s header not valid\n", aufile[i]);
  417. continue;
  418. }
  419. /* totsize accounts for image size and flash erase size */
  420. totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start));
  421. }
  422. #ifdef CONFIG_PROGRESSBAR
  423. if (totsize) {
  424. lcd_puts(" Update in progress\n");
  425. lcd_enable();
  426. }
  427. #endif
  428. /* just loop thru all the possible files */
  429. for (i = 0; i < AU_MAXFILES && totsize; i++) {
  430. if (!ausize[i]) {
  431. continue;
  432. }
  433. sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]);
  434. debug ("read %s sz %ld hdr %d\n",
  435. aufile[i], sz, image_get_header_size ());
  436. if (sz != ausize[i]) {
  437. printf ("%s: size %d read %d?\n", aufile[i], ausize[i], sz);
  438. continue;
  439. }
  440. if (sz <= 0 || sz <= image_get_header_size ()) {
  441. debug ("%s not found\n", aufile[i]);
  442. continue;
  443. }
  444. if (au_check_cksum_valid(i, sz) < 0) {
  445. debug ("%s checksum not valid\n", aufile[i]);
  446. continue;
  447. }
  448. /* this is really not a good idea, but it's what the */
  449. /* customer wants. */
  450. cnt = 0;
  451. got_ctrlc = 0;
  452. do {
  453. res = au_do_update(i, sz);
  454. /* let the user break out of the loop */
  455. if (ctrlc() || had_ctrlc()) {
  456. clear_ctrlc();
  457. if (res < 0)
  458. got_ctrlc = 1;
  459. break;
  460. }
  461. cnt++;
  462. #ifdef AU_TEST_ONLY
  463. } while (res < 0 && cnt < (AU_MAXFILES + 1));
  464. if (cnt < (AU_MAXFILES + 1))
  465. #else
  466. } while (res < 0);
  467. #endif
  468. }
  469. /* restore the old state */
  470. disable_ctrlc(old_ctrlc);
  471. #ifdef CONFIG_PROGRESSBAR
  472. if (totsize) {
  473. if (!res) {
  474. lcd_puts("\n Update completed\n");
  475. } else {
  476. lcd_puts("\n Update error\n");
  477. }
  478. lcd_enable();
  479. }
  480. #endif
  481. xit:
  482. usb_stop();
  483. return res;
  484. }
  485. #endif /* CONFIG_AUTO_UPDATE */