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