auto_update.c 13 KB

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