auto_update.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 ((au_image[i].type == AU_FIRMWARE) &&
  79. (au_image[i].size != image_get_data_size (hdr))) {
  80. printf ("Image %s has wrong size\n", au_image[i].name);
  81. return -1;
  82. }
  83. if (nbytes != (image_get_image_size (hdr))) {
  84. printf ("Image %s bad total SIZE\n", au_image[i].name);
  85. return -1;
  86. }
  87. /* check the data CRC */
  88. if (!image_check_dcrc (hdr)) {
  89. printf ("Image %s bad data checksum\n", au_image[i].name);
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. int au_check_header_valid(int i, long nbytes)
  95. {
  96. image_header_t *hdr;
  97. unsigned long checksum;
  98. hdr = (image_header_t *)LOAD_ADDR;
  99. /* check the easy ones first */
  100. #undef CHECK_VALID_DEBUG
  101. #ifdef CHECK_VALID_DEBUG
  102. printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
  103. printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_PPC);
  104. printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
  105. printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
  106. #endif
  107. if (nbytes < image_get_header_size ())
  108. {
  109. printf ("Image %s bad header SIZE\n", au_image[i].name);
  110. return -1;
  111. }
  112. if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC))
  113. {
  114. printf ("Image %s bad MAGIC or ARCH\n", au_image[i].name);
  115. return -1;
  116. }
  117. if (!image_check_hcrc (hdr)) {
  118. printf ("Image %s bad header checksum\n", au_image[i].name);
  119. return -1;
  120. }
  121. /* check the type - could do this all in one gigantic if() */
  122. if ((au_image[i].type == AU_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
  123. printf ("Image %s wrong type\n", au_image[i].name);
  124. return -1;
  125. }
  126. if ((au_image[i].type == AU_SCRIPT) && !image_check_type (hdr, IH_TYPE_SCRIPT)) {
  127. printf ("Image %s wrong type\n", au_image[i].name);
  128. return -1;
  129. }
  130. /* recycle checksum */
  131. checksum = image_get_data_size (hdr);
  132. #if 0 /* test-only */
  133. /* for kernel and app the image header must also fit into flash */
  134. if (idx != IDX_DISK)
  135. checksum += image_get_header_size ();
  136. /* check the size does not exceed space in flash. HUSH scripts */
  137. /* all have ausize[] set to 0 */
  138. if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
  139. printf ("Image %s is bigger than FLASH\n", au_image[i].name);
  140. return -1;
  141. }
  142. #endif
  143. return 0;
  144. }
  145. int au_do_update(int i, long sz)
  146. {
  147. image_header_t *hdr;
  148. char *addr;
  149. long start, end;
  150. int off, rc;
  151. uint nbytes;
  152. int k;
  153. #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  154. int total;
  155. #endif
  156. hdr = (image_header_t *)LOAD_ADDR;
  157. switch (au_image[i].type) {
  158. case AU_SCRIPT:
  159. printf("Executing script %s\n", au_image[i].name);
  160. /* execute a script */
  161. if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
  162. addr = (char *)((char *)hdr + image_get_header_size ());
  163. /* stick a NULL at the end of the script, otherwise */
  164. /* parse_string_outer() runs off the end. */
  165. addr[image_get_data_size (hdr)] = 0;
  166. addr += 8;
  167. /*
  168. * Replace cr/lf with ;
  169. */
  170. k = 0;
  171. while (addr[k] != 0) {
  172. if ((addr[k] == 10) || (addr[k] == 13)) {
  173. addr[k] = ';';
  174. }
  175. k++;
  176. }
  177. run_command(addr, 0);
  178. return 0;
  179. }
  180. break;
  181. case AU_FIRMWARE:
  182. case AU_NOR:
  183. case AU_NAND:
  184. start = au_image[i].start;
  185. end = au_image[i].start + au_image[i].size - 1;
  186. /*
  187. * do not update firmware when image is already in flash.
  188. */
  189. if (au_image[i].type == AU_FIRMWARE) {
  190. char *orig = (char*)start;
  191. char *new = (char *)((char *)hdr + image_get_header_size ());
  192. nbytes = image_get_data_size (hdr);
  193. while(--nbytes) {
  194. if (*orig++ != *new++) {
  195. break;
  196. }
  197. }
  198. if (!nbytes) {
  199. printf("Skipping firmware update - images are identical\n");
  200. break;
  201. }
  202. }
  203. /* unprotect the address range */
  204. /* this assumes that ONLY the firmware is protected! */
  205. if (au_image[i].type == AU_FIRMWARE) {
  206. flash_sect_protect(0, start, end);
  207. }
  208. /*
  209. * erase the address range.
  210. */
  211. if (au_image[i].type != AU_NAND) {
  212. printf("Updating NOR FLASH with image %s\n", au_image[i].name);
  213. debug ("flash_sect_erase(%lx, %lx);\n", start, end);
  214. flash_sect_erase(start, end);
  215. } else {
  216. #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  217. printf("Updating NAND FLASH with image %s\n", au_image[i].name);
  218. debug ("nand_legacy_erase(%lx, %lx);\n", start, end);
  219. rc = nand_legacy_erase (nand_dev_desc, start, end - start + 1, 0);
  220. debug ("nand_legacy_erase returned %x\n", rc);
  221. #endif
  222. }
  223. udelay(10000);
  224. /* strip the header - except for the kernel and ramdisk */
  225. if (au_image[i].type != AU_FIRMWARE) {
  226. addr = (char *)hdr;
  227. off = image_get_header_size ();
  228. nbytes = image_get_image_size (hdr);
  229. } else {
  230. addr = (char *)((char *)hdr + image_get_header_size ());
  231. off = 0;
  232. nbytes = image_get_data_size (hdr);
  233. }
  234. /*
  235. * copy the data from RAM to FLASH
  236. */
  237. if (au_image[i].type != AU_NAND) {
  238. debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
  239. rc = flash_write((char *)addr, start, nbytes);
  240. } else {
  241. #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  242. debug ("nand_legacy_rw(%p, %lx %x)\n", addr, start, nbytes);
  243. rc = nand_legacy_rw(nand_dev_desc, NANDRW_WRITE | NANDRW_JFFS2,
  244. start, nbytes, (size_t *)&total, (uchar *)addr);
  245. debug ("nand_legacy_rw: ret=%x total=%d nbytes=%d\n", rc, total, nbytes);
  246. #else
  247. rc = -1;
  248. #endif
  249. }
  250. if (rc != 0) {
  251. printf("Flashing failed due to error %d\n", rc);
  252. return -1;
  253. }
  254. /*
  255. * check the dcrc of the copy
  256. */
  257. if (au_image[i].type != AU_NAND) {
  258. rc = crc32 (0, (uchar *)(start + off), image_get_data_size (hdr));
  259. } else {
  260. #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  261. rc = nand_legacy_rw(nand_dev_desc, NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP,
  262. start, nbytes, (size_t *)&total, (uchar *)addr);
  263. rc = crc32 (0, (uchar *)(addr + off), image_get_data_size (hdr));
  264. #endif
  265. }
  266. if (rc != image_get_dcrc (hdr)) {
  267. printf ("Image %s Bad Data Checksum After COPY\n", au_image[i].name);
  268. return -1;
  269. }
  270. /* protect the address range */
  271. /* this assumes that ONLY the firmware is protected! */
  272. if (au_image[i].type == AU_FIRMWARE) {
  273. flash_sect_protect(1, start, end);
  274. }
  275. break;
  276. default:
  277. printf("Wrong image type selected!\n");
  278. }
  279. return 0;
  280. }
  281. static void process_macros (const char *input, char *output)
  282. {
  283. char c, prev;
  284. const char *varname_start = NULL;
  285. int inputcnt = strlen (input);
  286. int outputcnt = CFG_CBSIZE;
  287. int state = 0; /* 0 = waiting for '$' */
  288. /* 1 = waiting for '(' or '{' */
  289. /* 2 = waiting for ')' or '}' */
  290. /* 3 = waiting for ''' */
  291. #ifdef DEBUG_PARSER
  292. char *output_start = output;
  293. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
  294. #endif
  295. prev = '\0'; /* previous character */
  296. while (inputcnt && outputcnt) {
  297. c = *input++;
  298. inputcnt--;
  299. if (state!=3) {
  300. /* remove one level of escape characters */
  301. if ((c == '\\') && (prev != '\\')) {
  302. if (inputcnt-- == 0)
  303. break;
  304. prev = c;
  305. c = *input++;
  306. }
  307. }
  308. switch (state) {
  309. case 0: /* Waiting for (unescaped) $ */
  310. if ((c == '\'') && (prev != '\\')) {
  311. state = 3;
  312. break;
  313. }
  314. if ((c == '$') && (prev != '\\')) {
  315. state++;
  316. } else {
  317. *(output++) = c;
  318. outputcnt--;
  319. }
  320. break;
  321. case 1: /* Waiting for ( */
  322. if (c == '(' || c == '{') {
  323. state++;
  324. varname_start = input;
  325. } else {
  326. state = 0;
  327. *(output++) = '$';
  328. outputcnt--;
  329. if (outputcnt) {
  330. *(output++) = c;
  331. outputcnt--;
  332. }
  333. }
  334. break;
  335. case 2: /* Waiting for ) */
  336. if (c == ')' || c == '}') {
  337. int i;
  338. char envname[CFG_CBSIZE], *envval;
  339. int envcnt = input-varname_start-1; /* Varname # of chars */
  340. /* Get the varname */
  341. for (i = 0; i < envcnt; i++) {
  342. envname[i] = varname_start[i];
  343. }
  344. envname[i] = 0;
  345. /* Get its value */
  346. envval = getenv (envname);
  347. /* Copy into the line if it exists */
  348. if (envval != NULL)
  349. while ((*envval) && outputcnt) {
  350. *(output++) = *(envval++);
  351. outputcnt--;
  352. }
  353. /* Look for another '$' */
  354. state = 0;
  355. }
  356. break;
  357. case 3: /* Waiting for ' */
  358. if ((c == '\'') && (prev != '\\')) {
  359. state = 0;
  360. } else {
  361. *(output++) = c;
  362. outputcnt--;
  363. }
  364. break;
  365. }
  366. prev = c;
  367. }
  368. if (outputcnt)
  369. *output = 0;
  370. #ifdef DEBUG_PARSER
  371. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  372. strlen(output_start), output_start);
  373. #endif
  374. }
  375. /*
  376. * this is called from board_init() after the hardware has been set up
  377. * and is usable. That seems like a good time to do this.
  378. * Right now the return value is ignored.
  379. */
  380. int do_auto_update(void)
  381. {
  382. block_dev_desc_t *stor_dev;
  383. long sz;
  384. int i, res, cnt, old_ctrlc, got_ctrlc;
  385. char buffer[32];
  386. char str[80];
  387. /*
  388. * Check whether a CompactFlash is inserted
  389. */
  390. if (ide_dev_desc[0].type == DEV_TYPE_UNKNOWN) {
  391. return -1; /* no disk detected! */
  392. }
  393. /* check whether it has a partition table */
  394. stor_dev = get_dev("ide", 0);
  395. if (stor_dev == NULL) {
  396. debug ("Uknown device type\n");
  397. return -1;
  398. }
  399. if (fat_register_device(stor_dev, 1) != 0) {
  400. debug ("Unable to register ide disk 0:1 for fatls\n");
  401. return -1;
  402. }
  403. /*
  404. * Check if magic file is present
  405. */
  406. if (do_fat_read(AU_MAGIC_FILE, buffer, sizeof(buffer), LS_NO) <= 0) {
  407. return -1;
  408. }
  409. #ifdef CONFIG_AUTO_UPDATE_SHOW
  410. board_auto_update_show(1);
  411. #endif
  412. puts("\nAutoUpdate Disk detected! Trying to update system...\n");
  413. /* make sure that we see CTRL-C and save the old state */
  414. old_ctrlc = disable_ctrlc(0);
  415. /* just loop thru all the possible files */
  416. for (i = 0; i < N_AU_IMAGES; i++) {
  417. /*
  418. * Try to expand the environment var in the fname
  419. */
  420. process_macros(au_image[i].name, str);
  421. strcpy(au_image[i].name, str);
  422. printf("Reading %s ...", au_image[i].name);
  423. /* just read the header */
  424. sz = do_fat_read(au_image[i].name, LOAD_ADDR, image_get_header_size (), LS_NO);
  425. debug ("read %s sz %ld hdr %d\n",
  426. au_image[i].name, sz, image_get_header_size ());
  427. if (sz <= 0 || sz < image_get_header_size ()) {
  428. puts(" not found\n");
  429. continue;
  430. }
  431. if (au_check_header_valid(i, sz) < 0) {
  432. puts(" header not valid\n");
  433. continue;
  434. }
  435. sz = do_fat_read(au_image[i].name, LOAD_ADDR, MAX_LOADSZ, LS_NO);
  436. debug ("read %s sz %ld hdr %d\n",
  437. au_image[i].name, sz, image_get_header_size ());
  438. if (sz <= 0 || sz <= image_get_header_size ()) {
  439. puts(" not found\n");
  440. continue;
  441. }
  442. if (au_check_cksum_valid(i, sz) < 0) {
  443. puts(" checksum not valid\n");
  444. continue;
  445. }
  446. puts(" done\n");
  447. do {
  448. res = au_do_update(i, sz);
  449. /* let the user break out of the loop */
  450. if (ctrlc() || had_ctrlc()) {
  451. clear_ctrlc();
  452. if (res < 0)
  453. got_ctrlc = 1;
  454. break;
  455. }
  456. cnt++;
  457. } while (res < 0);
  458. }
  459. /* restore the old state */
  460. disable_ctrlc(old_ctrlc);
  461. puts("AutoUpdate finished\n\n");
  462. #ifdef CONFIG_AUTO_UPDATE_SHOW
  463. board_auto_update_show(0);
  464. #endif
  465. return 0;
  466. }
  467. int auto_update(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  468. {
  469. do_auto_update();
  470. return 0;
  471. }
  472. U_BOOT_CMD(
  473. autoupd, 1, 1, auto_update,
  474. "autoupd - Automatically update images\n",
  475. NULL
  476. );
  477. #endif /* CONFIG_AUTO_UPDATE */