auto_update.c 13 KB

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