auto_update.c 14 KB

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