auto_update.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 !(CONFIG_COMMANDS & CFG_CMD_FAT)
  41. #error "must define CFG_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. /* where to load files into memory */
  91. #define LOAD_ADDR ((unsigned char *)0x00200000)
  92. /* the app is the largest image */
  93. #define MAX_LOADSZ ausize[IDX_ROOTFS]
  94. /*i2c address of the keypad status*/
  95. #define I2C_PSOC_KEYPAD_ADDR 0x53
  96. /* keypad mask */
  97. #define KEYPAD_ROW 2
  98. #define KEYPAD_COL 2
  99. #define KEYPAD_MASK_LO ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))&0xFF)
  100. #define KEYPAD_MASK_HI ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))>>8)
  101. /* externals */
  102. extern int fat_register_device(block_dev_desc_t *, int);
  103. extern int file_fat_detectfs(void);
  104. extern long file_fat_read(const char *, void *, unsigned long);
  105. extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
  106. extern int flash_sect_erase(ulong, ulong);
  107. extern int flash_sect_protect (int, ulong, ulong);
  108. extern int flash_write (char *, ulong, ulong);
  109. extern int u_boot_hush_start(void);
  110. int au_check_cksum_valid(int idx, long nbytes)
  111. {
  112. image_header_t *hdr;
  113. unsigned long checksum;
  114. hdr = (image_header_t *)LOAD_ADDR;
  115. if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) {
  116. printf ("Image %s bad total SIZE\n", aufile[idx]);
  117. return -1;
  118. }
  119. /* check the data CRC */
  120. checksum = ntohl(hdr->ih_dcrc);
  121. if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size)) != checksum) {
  122. printf ("Image %s bad data checksum\n", aufile[idx]);
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. int au_check_header_valid(int idx, long nbytes)
  128. {
  129. image_header_t *hdr;
  130. unsigned long checksum;
  131. hdr = (image_header_t *)LOAD_ADDR;
  132. /* check the easy ones first */
  133. #undef CHECK_VALID_DEBUG
  134. #ifdef CHECK_VALID_DEBUG
  135. printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
  136. printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
  137. printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
  138. printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
  139. #endif
  140. if (nbytes < sizeof(*hdr)) {
  141. printf ("Image %s bad header SIZE\n", aufile[idx]);
  142. return -1;
  143. }
  144. if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_PPC) {
  145. printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
  146. return -1;
  147. }
  148. /* check the hdr CRC */
  149. checksum = ntohl(hdr->ih_hcrc);
  150. hdr->ih_hcrc = 0;
  151. if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
  152. printf ("Image %s bad header checksum\n", aufile[idx]);
  153. return -1;
  154. }
  155. hdr->ih_hcrc = htonl(checksum);
  156. /* check the type - could do this all in one gigantic if() */
  157. if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
  158. printf ("Image %s wrong type\n", aufile[idx]);
  159. return -1;
  160. }
  161. if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
  162. printf ("Image %s wrong type\n", aufile[idx]);
  163. return -1;
  164. }
  165. if ((idx == IDX_ROOTFS) && (hdr->ih_type != IH_TYPE_RAMDISK)) {
  166. printf ("Image %s wrong type\n", aufile[idx]);
  167. return -1;
  168. }
  169. /* recycle checksum */
  170. checksum = ntohl(hdr->ih_size);
  171. /* for kernel and app the image header must also fit into flash */
  172. if (idx != IDX_FIRMWARE)
  173. checksum += sizeof(*hdr);
  174. /* check the size does not exceed space in flash. HUSH scripts */
  175. /* all have ausize[] set to 0 */
  176. if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
  177. printf ("Image %s is bigger than FLASH\n", aufile[idx]);
  178. return -1;
  179. }
  180. return 0;
  181. }
  182. int au_do_update(int idx, long sz)
  183. {
  184. image_header_t *hdr;
  185. char *addr;
  186. long start, end;
  187. int off, rc;
  188. uint nbytes;
  189. hdr = (image_header_t *)LOAD_ADDR;
  190. /* execute a script */
  191. if (hdr->ih_type == IH_TYPE_SCRIPT) {
  192. addr = (char *)((char *)hdr + sizeof(*hdr));
  193. /* stick a NULL at the end of the script, otherwise */
  194. /* parse_string_outer() runs off the end. */
  195. addr[ntohl(hdr->ih_size)] = 0;
  196. addr += 8;
  197. parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
  198. return 0;
  199. }
  200. start = aufl_layout[idx].start;
  201. end = aufl_layout[idx].end;
  202. /* unprotect the address range */
  203. /* this assumes that ONLY the firmware is protected! */
  204. if (idx == IDX_FIRMWARE) {
  205. #undef AU_UPDATE_TEST
  206. #ifdef AU_UPDATE_TEST
  207. /* erase it where Linux goes */
  208. start = aufl_layout[1].start;
  209. end = aufl_layout[1].end;
  210. #endif
  211. flash_sect_protect(0, start, end);
  212. }
  213. /*
  214. * erase the address range.
  215. */
  216. debug ("flash_sect_erase(%lx, %lx);\n", start, end);
  217. flash_sect_erase(start, end);
  218. wait_ms(100);
  219. /* strip the header - except for the kernel and ramdisk */
  220. if (hdr->ih_type == IH_TYPE_KERNEL || hdr->ih_type == IH_TYPE_RAMDISK) {
  221. addr = (char *)hdr;
  222. off = sizeof(*hdr);
  223. nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
  224. } else {
  225. addr = (char *)((char *)hdr + sizeof(*hdr));
  226. #ifdef AU_UPDATE_TEST
  227. /* copy it to where Linux goes */
  228. if (idx == IDX_FIRMWARE)
  229. start = aufl_layout[1].start;
  230. #endif
  231. off = 0;
  232. nbytes = ntohl(hdr->ih_size);
  233. }
  234. /* copy the data from RAM to FLASH */
  235. debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
  236. rc = flash_write(addr, start, nbytes);
  237. if (rc != 0) {
  238. printf("Flashing failed due to error %d\n", rc);
  239. return -1;
  240. }
  241. /* check the dcrc of the copy */
  242. if (crc32 (0, (uchar *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
  243. printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
  244. return -1;
  245. }
  246. /* protect the address range */
  247. /* this assumes that ONLY the firmware is protected! */
  248. if (idx == IDX_FIRMWARE)
  249. flash_sect_protect(1, start, end);
  250. return 0;
  251. }
  252. /*
  253. * this is called from board_init() after the hardware has been set up
  254. * and is usable. That seems like a good time to do this.
  255. * Right now the return value is ignored.
  256. */
  257. int do_auto_update(void)
  258. {
  259. block_dev_desc_t *stor_dev;
  260. long sz;
  261. int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc;
  262. char *env;
  263. long start, end;
  264. uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
  265. /*
  266. * Read keypad status
  267. */
  268. i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
  269. wait_ms(500);
  270. i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
  271. /*
  272. * Check keypad
  273. */
  274. if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
  275. (keypad_status1[1] != keypad_status2[1])) {
  276. return 0;
  277. }
  278. au_usb_stor_curr_dev = -1;
  279. /* start USB */
  280. if (usb_stop() < 0) {
  281. debug ("usb_stop failed\n");
  282. return -1;
  283. }
  284. if (usb_init() < 0) {
  285. debug ("usb_init failed\n");
  286. return -1;
  287. }
  288. /*
  289. * check whether a storage device is attached (assume that it's
  290. * a USB memory stick, since nothing else should be attached).
  291. */
  292. au_usb_stor_curr_dev = usb_stor_scan(0);
  293. if (au_usb_stor_curr_dev == -1) {
  294. debug ("No device found. Not initialized?\n");
  295. return -1;
  296. }
  297. /* check whether it has a partition table */
  298. stor_dev = get_dev("usb", 0);
  299. if (stor_dev == NULL) {
  300. debug ("uknown device type\n");
  301. return -1;
  302. }
  303. if (fat_register_device(stor_dev, 1) != 0) {
  304. debug ("Unable to use USB %d:%d for fatls\n",
  305. au_usb_stor_curr_dev, 1);
  306. return -1;
  307. }
  308. if (file_fat_detectfs() != 0) {
  309. debug ("file_fat_detectfs failed\n");
  310. }
  311. /*
  312. * now check whether start and end are defined using environment
  313. * variables.
  314. */
  315. start = -1;
  316. end = 0;
  317. env = getenv("firmware_st");
  318. if (env != NULL)
  319. start = simple_strtoul(env, NULL, 16);
  320. env = getenv("firmware_nd");
  321. if (env != NULL)
  322. end = simple_strtoul(env, NULL, 16);
  323. if (start >= 0 && end && end > start) {
  324. ausize[IDX_FIRMWARE] = (end + 1) - start;
  325. aufl_layout[IDX_FIRMWARE].start = start;
  326. aufl_layout[IDX_FIRMWARE].end = end;
  327. }
  328. start = -1;
  329. end = 0;
  330. env = getenv("kernel_st");
  331. if (env != NULL)
  332. start = simple_strtoul(env, NULL, 16);
  333. env = getenv("kernel_nd");
  334. if (env != NULL)
  335. end = simple_strtoul(env, NULL, 16);
  336. if (start >= 0 && end && end > start) {
  337. ausize[IDX_KERNEL] = (end + 1) - start;
  338. aufl_layout[IDX_KERNEL].start = start;
  339. aufl_layout[IDX_KERNEL].end = end;
  340. }
  341. start = -1;
  342. end = 0;
  343. env = getenv("rootfs_st");
  344. if (env != NULL)
  345. start = simple_strtoul(env, NULL, 16);
  346. env = getenv("rootfs_nd");
  347. if (env != NULL)
  348. end = simple_strtoul(env, NULL, 16);
  349. if (start >= 0 && end && end > start) {
  350. ausize[IDX_ROOTFS] = (end + 1) - start;
  351. aufl_layout[IDX_ROOTFS].start = start;
  352. aufl_layout[IDX_ROOTFS].end = end;
  353. }
  354. /* make certain that HUSH is runnable */
  355. u_boot_hush_start();
  356. /* make sure that we see CTRL-C and save the old state */
  357. old_ctrlc = disable_ctrlc(0);
  358. bitmap_first = 0;
  359. /* just loop thru all the possible files */
  360. for (i = 0; i < AU_MAXFILES; i++) {
  361. /* just read the header */
  362. sz = file_fat_read(aufile[i], LOAD_ADDR, sizeof(image_header_t));
  363. debug ("read %s sz %ld hdr %d\n",
  364. aufile[i], sz, sizeof(image_header_t));
  365. if (sz <= 0 || sz < sizeof(image_header_t)) {
  366. debug ("%s not found\n", aufile[i]);
  367. continue;
  368. }
  369. if (au_check_header_valid(i, sz) < 0) {
  370. debug ("%s header not valid\n", aufile[i]);
  371. continue;
  372. }
  373. sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
  374. debug ("read %s sz %ld hdr %d\n",
  375. aufile[i], sz, sizeof(image_header_t));
  376. if (sz <= 0 || sz <= sizeof(image_header_t)) {
  377. debug ("%s not found\n", aufile[i]);
  378. continue;
  379. }
  380. if (au_check_cksum_valid(i, sz) < 0) {
  381. debug ("%s checksum not valid\n", aufile[i]);
  382. continue;
  383. }
  384. /* this is really not a good idea, but it's what the */
  385. /* customer wants. */
  386. cnt = 0;
  387. got_ctrlc = 0;
  388. do {
  389. res = au_do_update(i, sz);
  390. /* let the user break out of the loop */
  391. if (ctrlc() || had_ctrlc()) {
  392. clear_ctrlc();
  393. if (res < 0)
  394. got_ctrlc = 1;
  395. break;
  396. }
  397. cnt++;
  398. #ifdef AU_TEST_ONLY
  399. } while (res < 0 && cnt < (AU_MAXFILES + 1));
  400. if (cnt < (AU_MAXFILES + 1))
  401. #else
  402. } while (res < 0);
  403. #endif
  404. }
  405. usb_stop();
  406. /* restore the old state */
  407. disable_ctrlc(old_ctrlc);
  408. return 0;
  409. }
  410. #endif /* CONFIG_AUTO_UPDATE */