auto_update.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 (CONFIG_COMMANDS & CFG_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. #include <linux/mtd/nand_legacy.h>
  32. #include <fat.h>
  33. #include "auto_update.h"
  34. #ifdef CONFIG_AUTO_UPDATE
  35. #if !(CONFIG_COMMANDS & CFG_CMD_FAT)
  36. #error "must define CFG_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. /* change char* to void* to shutup the compiler */
  63. extern block_dev_desc_t *get_dev (char*, int);
  64. #if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  65. /* references to names in cmd_nand.c */
  66. #define NANDRW_READ 0x01
  67. #define NANDRW_WRITE 0x00
  68. #define NANDRW_JFFS2 0x02
  69. #define NANDRW_JFFS2_SKIP 0x04
  70. extern struct nand_chip nand_dev_desc[];
  71. extern int nand_legacy_rw(struct nand_chip* nand, int cmd, size_t start, size_t len,
  72. size_t * retlen, u_char * buf);
  73. extern int nand_legacy_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean);
  74. #endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY) */
  75. extern block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
  76. int au_check_cksum_valid(int i, long nbytes)
  77. {
  78. image_header_t *hdr;
  79. unsigned long checksum;
  80. hdr = (image_header_t *)LOAD_ADDR;
  81. if ((au_image[i].type == AU_FIRMWARE) && (au_image[i].size != ntohl(hdr->ih_size))) {
  82. printf ("Image %s has wrong size\n", au_image[i].name);
  83. return -1;
  84. }
  85. if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) {
  86. printf ("Image %s bad total SIZE\n", au_image[i].name);
  87. return -1;
  88. }
  89. /* check the data CRC */
  90. checksum = ntohl(hdr->ih_dcrc);
  91. if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
  92. != checksum) {
  93. printf ("Image %s bad data checksum\n", au_image[i].name);
  94. return -1;
  95. }
  96. return 0;
  97. }
  98. int au_check_header_valid(int i, long nbytes)
  99. {
  100. image_header_t *hdr;
  101. unsigned long checksum;
  102. hdr = (image_header_t *)LOAD_ADDR;
  103. /* check the easy ones first */
  104. #undef CHECK_VALID_DEBUG
  105. #ifdef CHECK_VALID_DEBUG
  106. printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
  107. printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_PPC);
  108. printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
  109. printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
  110. #endif
  111. if (nbytes < sizeof(*hdr))
  112. {
  113. printf ("Image %s bad header SIZE\n", au_image[i].name);
  114. return -1;
  115. }
  116. if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_PPC)
  117. {
  118. printf ("Image %s bad MAGIC or ARCH\n", au_image[i].name);
  119. return -1;
  120. }
  121. /* check the hdr CRC */
  122. checksum = ntohl(hdr->ih_hcrc);
  123. hdr->ih_hcrc = 0;
  124. if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
  125. printf ("Image %s bad header checksum\n", au_image[i].name);
  126. return -1;
  127. }
  128. hdr->ih_hcrc = htonl(checksum);
  129. /* check the type - could do this all in one gigantic if() */
  130. if ((au_image[i].type == AU_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
  131. printf ("Image %s wrong type\n", au_image[i].name);
  132. return -1;
  133. }
  134. if ((au_image[i].type == AU_SCRIPT) && (hdr->ih_type != IH_TYPE_SCRIPT)) {
  135. printf ("Image %s wrong type\n", au_image[i].name);
  136. return -1;
  137. }
  138. /* recycle checksum */
  139. checksum = ntohl(hdr->ih_size);
  140. #if 0 /* test-only */
  141. /* for kernel and app the image header must also fit into flash */
  142. if (idx != IDX_DISK)
  143. checksum += sizeof(*hdr);
  144. /* check the size does not exceed space in flash. HUSH scripts */
  145. /* all have ausize[] set to 0 */
  146. if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
  147. printf ("Image %s is bigger than FLASH\n", au_image[i].name);
  148. return -1;
  149. }
  150. #endif
  151. return 0;
  152. }
  153. int au_do_update(int i, long sz)
  154. {
  155. image_header_t *hdr;
  156. char *addr;
  157. long start, end;
  158. int off, rc;
  159. uint nbytes;
  160. int k;
  161. #if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  162. int total;
  163. #endif
  164. hdr = (image_header_t *)LOAD_ADDR;
  165. switch (au_image[i].type) {
  166. case AU_SCRIPT:
  167. printf("Executing script %s\n", au_image[i].name);
  168. /* execute a script */
  169. if (hdr->ih_type == IH_TYPE_SCRIPT) {
  170. addr = (char *)((char *)hdr + sizeof(*hdr));
  171. /* stick a NULL at the end of the script, otherwise */
  172. /* parse_string_outer() runs off the end. */
  173. addr[ntohl(hdr->ih_size)] = 0;
  174. addr += 8;
  175. /*
  176. * Replace cr/lf with ;
  177. */
  178. k = 0;
  179. while (addr[k] != 0) {
  180. if ((addr[k] == 10) || (addr[k] == 13)) {
  181. addr[k] = ';';
  182. }
  183. k++;
  184. }
  185. run_command(addr, 0);
  186. return 0;
  187. }
  188. break;
  189. case AU_FIRMWARE:
  190. case AU_NOR:
  191. case AU_NAND:
  192. start = au_image[i].start;
  193. end = au_image[i].start + au_image[i].size - 1;
  194. /*
  195. * do not update firmware when image is already in flash.
  196. */
  197. if (au_image[i].type == AU_FIRMWARE) {
  198. char *orig = (char*)start;
  199. char *new = (char *)((char *)hdr + sizeof(*hdr));
  200. nbytes = ntohl(hdr->ih_size);
  201. while(--nbytes) {
  202. if (*orig++ != *new++) {
  203. break;
  204. }
  205. }
  206. if (!nbytes) {
  207. printf("Skipping firmware update - images are identical\n");
  208. break;
  209. }
  210. }
  211. /* unprotect the address range */
  212. /* this assumes that ONLY the firmware is protected! */
  213. if (au_image[i].type == AU_FIRMWARE) {
  214. flash_sect_protect(0, start, end);
  215. }
  216. /*
  217. * erase the address range.
  218. */
  219. if (au_image[i].type != AU_NAND) {
  220. printf("Updating NOR FLASH with image %s\n", au_image[i].name);
  221. debug ("flash_sect_erase(%lx, %lx);\n", start, end);
  222. flash_sect_erase(start, end);
  223. } else {
  224. #if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  225. printf("Updating NAND FLASH with image %s\n", au_image[i].name);
  226. debug ("nand_legacy_erase(%lx, %lx);\n", start, end);
  227. rc = nand_legacy_erase (nand_dev_desc, start, end - start + 1, 0);
  228. debug ("nand_legacy_erase returned %x\n", rc);
  229. #endif
  230. }
  231. udelay(10000);
  232. /* strip the header - except for the kernel and ramdisk */
  233. if (au_image[i].type != AU_FIRMWARE) {
  234. addr = (char *)hdr;
  235. off = sizeof(*hdr);
  236. nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
  237. } else {
  238. addr = (char *)((char *)hdr + sizeof(*hdr));
  239. off = 0;
  240. nbytes = ntohl(hdr->ih_size);
  241. }
  242. /*
  243. * copy the data from RAM to FLASH
  244. */
  245. if (au_image[i].type != AU_NAND) {
  246. debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
  247. rc = flash_write((char *)addr, start, nbytes);
  248. } else {
  249. #if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  250. debug ("nand_legacy_rw(%p, %lx %x)\n", addr, start, nbytes);
  251. rc = nand_legacy_rw(nand_dev_desc, NANDRW_WRITE | NANDRW_JFFS2,
  252. start, nbytes, (size_t *)&total, (uchar *)addr);
  253. debug ("nand_legacy_rw: ret=%x total=%d nbytes=%d\n", rc, total, nbytes);
  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 (CONFIG_COMMANDS & CFG_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 */