auto_update.c 14 KB

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