auto_update.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * (C) Copyright 2003-2004
  3. * Gary Jennejohn, DENX Software Engineering, gj@denx.de.
  4. * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <command.h>
  26. #include <image.h>
  27. #include <asm/byteorder.h>
  28. #if defined(CFG_NAND_LEGACY)
  29. #include <linux/mtd/nand_legacy.h>
  30. #endif
  31. #include <fat.h>
  32. #include <part.h>
  33. #include "auto_update.h"
  34. #ifdef CONFIG_AUTO_UPDATE
  35. #if !defined(CONFIG_CMD_FAT)
  36. #error "must define CONFIG_CMD_FAT"
  37. #endif
  38. extern au_image_t au_image[];
  39. extern int N_AU_IMAGES;
  40. #define AU_DEBUG
  41. #undef AU_DEBUG
  42. #undef debug
  43. #ifdef AU_DEBUG
  44. #define debug(fmt,args...) printf (fmt ,##args)
  45. #else
  46. #define debug(fmt,args...)
  47. #endif /* AU_DEBUG */
  48. #define LOAD_ADDR ((unsigned char *)0x100000) /* where to load files into memory */
  49. #define MAX_LOADSZ 0x1e00000
  50. /* externals */
  51. extern int fat_register_device(block_dev_desc_t *, int);
  52. extern int file_fat_detectfs(void);
  53. extern long file_fat_read(const char *, void *, unsigned long);
  54. long do_fat_read (const char *filename, void *buffer, unsigned long maxsize, int dols);
  55. #ifdef CONFIG_VFD
  56. extern int trab_vfd (ulong);
  57. extern int transfer_pic(unsigned char, unsigned char *, int, int);
  58. #endif
  59. extern int flash_sect_erase(ulong, ulong);
  60. extern int flash_sect_protect (int, ulong, ulong);
  61. extern int flash_write (char *, ulong, ulong);
  62. #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  63. /* references to names in cmd_nand.c */
  64. #define NANDRW_READ 0x01
  65. #define NANDRW_WRITE 0x00
  66. #define NANDRW_JFFS2 0x02
  67. #define NANDRW_JFFS2_SKIP 0x04
  68. extern struct nand_chip nand_dev_desc[];
  69. extern int nand_legacy_rw(struct nand_chip* nand, int cmd, size_t start, size_t len,
  70. size_t * retlen, u_char * buf);
  71. extern int nand_legacy_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean);
  72. #endif
  73. extern block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
  74. int au_check_cksum_valid(int i, long nbytes)
  75. {
  76. image_header_t *hdr;
  77. hdr = (image_header_t *)LOAD_ADDR;
  78. #if defined(CONFIG_FIT)
  79. if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
  80. puts ("Non legacy image format not supported\n");
  81. return -1;
  82. }
  83. #endif
  84. if ((au_image[i].type == AU_FIRMWARE) &&
  85. (au_image[i].size != image_get_data_size (hdr))) {
  86. printf ("Image %s has wrong size\n", au_image[i].name);
  87. return -1;
  88. }
  89. if (nbytes != (image_get_image_size (hdr))) {
  90. printf ("Image %s bad total SIZE\n", au_image[i].name);
  91. return -1;
  92. }
  93. /* check the data CRC */
  94. if (!image_check_dcrc (hdr)) {
  95. printf ("Image %s bad data checksum\n", au_image[i].name);
  96. return -1;
  97. }
  98. return 0;
  99. }
  100. int au_check_header_valid(int i, long nbytes)
  101. {
  102. image_header_t *hdr;
  103. unsigned long checksum;
  104. hdr = (image_header_t *)LOAD_ADDR;
  105. #if defined(CONFIG_FIT)
  106. if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
  107. puts ("Non legacy image format not supported\n");
  108. return -1;
  109. }
  110. #endif
  111. /* check the easy ones first */
  112. #undef CHECK_VALID_DEBUG
  113. #ifdef CHECK_VALID_DEBUG
  114. printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
  115. printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_PPC);
  116. printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
  117. printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
  118. #endif
  119. if (nbytes < image_get_header_size ())
  120. {
  121. printf ("Image %s bad header SIZE\n", au_image[i].name);
  122. return -1;
  123. }
  124. if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC))
  125. {
  126. printf ("Image %s bad MAGIC or ARCH\n", au_image[i].name);
  127. return -1;
  128. }
  129. if (!image_check_hcrc (hdr)) {
  130. printf ("Image %s bad header checksum\n", au_image[i].name);
  131. return -1;
  132. }
  133. /* check the type - could do this all in one gigantic if() */
  134. if ((au_image[i].type == AU_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
  135. printf ("Image %s wrong type\n", au_image[i].name);
  136. return -1;
  137. }
  138. if ((au_image[i].type == AU_SCRIPT) && !image_check_type (hdr, IH_TYPE_SCRIPT)) {
  139. printf ("Image %s wrong type\n", au_image[i].name);
  140. return -1;
  141. }
  142. /* recycle checksum */
  143. checksum = image_get_data_size (hdr);
  144. #if 0 /* test-only */
  145. /* for kernel and app the image header must also fit into flash */
  146. if (idx != IDX_DISK)
  147. checksum += image_get_header_size ();
  148. /* check the size does not exceed space in flash. HUSH scripts */
  149. /* all have ausize[] set to 0 */
  150. if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
  151. printf ("Image %s is bigger than FLASH\n", au_image[i].name);
  152. return -1;
  153. }
  154. #endif
  155. return 0;
  156. }
  157. int au_do_update(int i, long sz)
  158. {
  159. image_header_t *hdr;
  160. char *addr;
  161. long start, end;
  162. int off, rc;
  163. uint nbytes;
  164. int k;
  165. #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  166. int total;
  167. #endif
  168. hdr = (image_header_t *)LOAD_ADDR;
  169. #if defined(CONFIG_FIT)
  170. if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
  171. puts ("Non legacy image format not supported\n");
  172. return -1;
  173. }
  174. #endif
  175. switch (au_image[i].type) {
  176. case AU_SCRIPT:
  177. printf("Executing script %s\n", au_image[i].name);
  178. /* execute a script */
  179. if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
  180. addr = (char *)((char *)hdr + image_get_header_size ());
  181. /* stick a NULL at the end of the script, otherwise */
  182. /* parse_string_outer() runs off the end. */
  183. addr[image_get_data_size (hdr)] = 0;
  184. addr += 8;
  185. /*
  186. * Replace cr/lf with ;
  187. */
  188. k = 0;
  189. while (addr[k] != 0) {
  190. if ((addr[k] == 10) || (addr[k] == 13)) {
  191. addr[k] = ';';
  192. }
  193. k++;
  194. }
  195. run_command(addr, 0);
  196. return 0;
  197. }
  198. break;
  199. case AU_FIRMWARE:
  200. case AU_NOR:
  201. case AU_NAND:
  202. start = au_image[i].start;
  203. end = au_image[i].start + au_image[i].size - 1;
  204. /*
  205. * do not update firmware when image is already in flash.
  206. */
  207. if (au_image[i].type == AU_FIRMWARE) {
  208. char *orig = (char*)start;
  209. char *new = (char *)((char *)hdr + image_get_header_size ());
  210. nbytes = image_get_data_size (hdr);
  211. while(--nbytes) {
  212. if (*orig++ != *new++) {
  213. break;
  214. }
  215. }
  216. if (!nbytes) {
  217. printf("Skipping firmware update - images are identical\n");
  218. break;
  219. }
  220. }
  221. /* unprotect the address range */
  222. /* this assumes that ONLY the firmware is protected! */
  223. if (au_image[i].type == AU_FIRMWARE) {
  224. flash_sect_protect(0, start, end);
  225. }
  226. /*
  227. * erase the address range.
  228. */
  229. if (au_image[i].type != AU_NAND) {
  230. printf("Updating NOR FLASH with image %s\n", au_image[i].name);
  231. debug ("flash_sect_erase(%lx, %lx);\n", start, end);
  232. flash_sect_erase(start, end);
  233. } else {
  234. #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  235. printf("Updating NAND FLASH with image %s\n", au_image[i].name);
  236. debug ("nand_legacy_erase(%lx, %lx);\n", start, end);
  237. rc = nand_legacy_erase (nand_dev_desc, start, end - start + 1, 0);
  238. debug ("nand_legacy_erase returned %x\n", rc);
  239. #endif
  240. }
  241. udelay(10000);
  242. /* strip the header - except for the kernel and ramdisk */
  243. if (au_image[i].type != AU_FIRMWARE) {
  244. addr = (char *)hdr;
  245. off = image_get_header_size ();
  246. nbytes = image_get_image_size (hdr);
  247. } else {
  248. addr = (char *)((char *)hdr + image_get_header_size ());
  249. off = 0;
  250. nbytes = image_get_data_size (hdr);
  251. }
  252. /*
  253. * copy the data from RAM to FLASH
  254. */
  255. if (au_image[i].type != AU_NAND) {
  256. debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
  257. rc = flash_write((char *)addr, start, nbytes);
  258. } else {
  259. #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  260. debug ("nand_legacy_rw(%p, %lx %x)\n", addr, start, nbytes);
  261. rc = nand_legacy_rw(nand_dev_desc, NANDRW_WRITE | NANDRW_JFFS2,
  262. start, nbytes, (size_t *)&total, (uchar *)addr);
  263. debug ("nand_legacy_rw: ret=%x total=%d nbytes=%d\n", rc, total, nbytes);
  264. #else
  265. rc = -1;
  266. #endif
  267. }
  268. if (rc != 0) {
  269. printf("Flashing failed due to error %d\n", rc);
  270. return -1;
  271. }
  272. /*
  273. * check the dcrc of the copy
  274. */
  275. if (au_image[i].type != AU_NAND) {
  276. rc = crc32 (0, (uchar *)(start + off), image_get_data_size (hdr));
  277. } else {
  278. #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  279. rc = nand_legacy_rw(nand_dev_desc, NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP,
  280. start, nbytes, (size_t *)&total, (uchar *)addr);
  281. rc = crc32 (0, (uchar *)(addr + off), image_get_data_size (hdr));
  282. #endif
  283. }
  284. if (rc != image_get_dcrc (hdr)) {
  285. printf ("Image %s Bad Data Checksum After COPY\n", au_image[i].name);
  286. return -1;
  287. }
  288. /* protect the address range */
  289. /* this assumes that ONLY the firmware is protected! */
  290. if (au_image[i].type == AU_FIRMWARE) {
  291. flash_sect_protect(1, start, end);
  292. }
  293. break;
  294. default:
  295. printf("Wrong image type selected!\n");
  296. }
  297. return 0;
  298. }
  299. static void process_macros (const char *input, char *output)
  300. {
  301. char c, prev;
  302. const char *varname_start = NULL;
  303. int inputcnt = strlen (input);
  304. int outputcnt = CFG_CBSIZE;
  305. int state = 0; /* 0 = waiting for '$' */
  306. /* 1 = waiting for '(' or '{' */
  307. /* 2 = waiting for ')' or '}' */
  308. /* 3 = waiting for ''' */
  309. #ifdef DEBUG_PARSER
  310. char *output_start = output;
  311. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
  312. #endif
  313. prev = '\0'; /* previous character */
  314. while (inputcnt && outputcnt) {
  315. c = *input++;
  316. inputcnt--;
  317. if (state!=3) {
  318. /* remove one level of escape characters */
  319. if ((c == '\\') && (prev != '\\')) {
  320. if (inputcnt-- == 0)
  321. break;
  322. prev = c;
  323. c = *input++;
  324. }
  325. }
  326. switch (state) {
  327. case 0: /* Waiting for (unescaped) $ */
  328. if ((c == '\'') && (prev != '\\')) {
  329. state = 3;
  330. break;
  331. }
  332. if ((c == '$') && (prev != '\\')) {
  333. state++;
  334. } else {
  335. *(output++) = c;
  336. outputcnt--;
  337. }
  338. break;
  339. case 1: /* Waiting for ( */
  340. if (c == '(' || c == '{') {
  341. state++;
  342. varname_start = input;
  343. } else {
  344. state = 0;
  345. *(output++) = '$';
  346. outputcnt--;
  347. if (outputcnt) {
  348. *(output++) = c;
  349. outputcnt--;
  350. }
  351. }
  352. break;
  353. case 2: /* Waiting for ) */
  354. if (c == ')' || c == '}') {
  355. int i;
  356. char envname[CFG_CBSIZE], *envval;
  357. int envcnt = input-varname_start-1; /* Varname # of chars */
  358. /* Get the varname */
  359. for (i = 0; i < envcnt; i++) {
  360. envname[i] = varname_start[i];
  361. }
  362. envname[i] = 0;
  363. /* Get its value */
  364. envval = getenv (envname);
  365. /* Copy into the line if it exists */
  366. if (envval != NULL)
  367. while ((*envval) && outputcnt) {
  368. *(output++) = *(envval++);
  369. outputcnt--;
  370. }
  371. /* Look for another '$' */
  372. state = 0;
  373. }
  374. break;
  375. case 3: /* Waiting for ' */
  376. if ((c == '\'') && (prev != '\\')) {
  377. state = 0;
  378. } else {
  379. *(output++) = c;
  380. outputcnt--;
  381. }
  382. break;
  383. }
  384. prev = c;
  385. }
  386. if (outputcnt)
  387. *output = 0;
  388. #ifdef DEBUG_PARSER
  389. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  390. strlen(output_start), output_start);
  391. #endif
  392. }
  393. /*
  394. * this is called from board_init() after the hardware has been set up
  395. * and is usable. That seems like a good time to do this.
  396. * Right now the return value is ignored.
  397. */
  398. int do_auto_update(void)
  399. {
  400. block_dev_desc_t *stor_dev;
  401. long sz;
  402. int i, res, cnt, old_ctrlc, got_ctrlc;
  403. char buffer[32];
  404. char str[80];
  405. /*
  406. * Check whether a CompactFlash is inserted
  407. */
  408. if (ide_dev_desc[0].type == DEV_TYPE_UNKNOWN) {
  409. return -1; /* no disk detected! */
  410. }
  411. /* check whether it has a partition table */
  412. stor_dev = get_dev("ide", 0);
  413. if (stor_dev == NULL) {
  414. debug ("Uknown device type\n");
  415. return -1;
  416. }
  417. if (fat_register_device(stor_dev, 1) != 0) {
  418. debug ("Unable to register ide disk 0:1 for fatls\n");
  419. return -1;
  420. }
  421. /*
  422. * Check if magic file is present
  423. */
  424. if (do_fat_read(AU_MAGIC_FILE, buffer, sizeof(buffer), LS_NO) <= 0) {
  425. return -1;
  426. }
  427. #ifdef CONFIG_AUTO_UPDATE_SHOW
  428. board_auto_update_show(1);
  429. #endif
  430. puts("\nAutoUpdate Disk detected! Trying to update system...\n");
  431. /* make sure that we see CTRL-C and save the old state */
  432. old_ctrlc = disable_ctrlc(0);
  433. /* just loop thru all the possible files */
  434. for (i = 0; i < N_AU_IMAGES; i++) {
  435. /*
  436. * Try to expand the environment var in the fname
  437. */
  438. process_macros(au_image[i].name, str);
  439. strcpy(au_image[i].name, str);
  440. printf("Reading %s ...", au_image[i].name);
  441. /* just read the header */
  442. sz = do_fat_read(au_image[i].name, LOAD_ADDR, image_get_header_size (), LS_NO);
  443. debug ("read %s sz %ld hdr %d\n",
  444. au_image[i].name, sz, image_get_header_size ());
  445. if (sz <= 0 || sz < image_get_header_size ()) {
  446. puts(" not found\n");
  447. continue;
  448. }
  449. if (au_check_header_valid(i, sz) < 0) {
  450. puts(" header not valid\n");
  451. continue;
  452. }
  453. sz = do_fat_read(au_image[i].name, LOAD_ADDR, MAX_LOADSZ, LS_NO);
  454. debug ("read %s sz %ld hdr %d\n",
  455. au_image[i].name, sz, image_get_header_size ());
  456. if (sz <= 0 || sz <= image_get_header_size ()) {
  457. puts(" not found\n");
  458. continue;
  459. }
  460. if (au_check_cksum_valid(i, sz) < 0) {
  461. puts(" checksum not valid\n");
  462. continue;
  463. }
  464. puts(" done\n");
  465. do {
  466. res = au_do_update(i, sz);
  467. /* let the user break out of the loop */
  468. if (ctrlc() || had_ctrlc()) {
  469. clear_ctrlc();
  470. if (res < 0)
  471. got_ctrlc = 1;
  472. break;
  473. }
  474. cnt++;
  475. } while (res < 0);
  476. }
  477. /* restore the old state */
  478. disable_ctrlc(old_ctrlc);
  479. puts("AutoUpdate finished\n\n");
  480. #ifdef CONFIG_AUTO_UPDATE_SHOW
  481. board_auto_update_show(0);
  482. #endif
  483. return 0;
  484. }
  485. int auto_update(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  486. {
  487. do_auto_update();
  488. return 0;
  489. }
  490. U_BOOT_CMD(
  491. autoupd, 1, 1, auto_update,
  492. "autoupd - Automatically update images\n",
  493. NULL
  494. );
  495. #endif /* CONFIG_AUTO_UPDATE */