auto_update.c 12 KB

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