auto_update.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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, (char *)(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, (char *)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. /* unprotect the address range */
  192. /* this assumes that ONLY the firmware is protected! */
  193. if (au_image[i].type == AU_FIRMWARE) {
  194. flash_sect_protect(0, start, end);
  195. }
  196. /*
  197. * erase the address range.
  198. */
  199. if (au_image[i].type != AU_NAND) {
  200. printf("Updating NOR FLASH with image %s\n", au_image[i].name);
  201. debug ("flash_sect_erase(%lx, %lx);\n", start, end);
  202. flash_sect_erase(start, end);
  203. } else {
  204. #if (CONFIG_COMMANDS & CFG_CMD_NAND)
  205. printf("Updating NAND FLASH with image %s\n", au_image[i].name);
  206. debug ("nand_erase(%lx, %lx);\n", start, end);
  207. rc = nand_erase (nand_dev_desc, start, end - start + 1, 0);
  208. debug ("nand_erase returned %x\n", rc);
  209. #endif
  210. }
  211. udelay(10000);
  212. /* strip the header - except for the kernel and ramdisk */
  213. if (au_image[i].type != AU_FIRMWARE) {
  214. addr = (char *)hdr;
  215. off = sizeof(*hdr);
  216. nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
  217. } else {
  218. addr = (char *)((char *)hdr + sizeof(*hdr));
  219. off = 0;
  220. nbytes = ntohl(hdr->ih_size);
  221. }
  222. /*
  223. * copy the data from RAM to FLASH
  224. */
  225. if (au_image[i].type != AU_NAND) {
  226. debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
  227. rc = flash_write(addr, start, nbytes);
  228. } else {
  229. #if (CONFIG_COMMANDS & CFG_CMD_NAND)
  230. debug ("nand_rw(%p, %lx %x)\n", addr, start, nbytes);
  231. rc = nand_rw(nand_dev_desc, NANDRW_WRITE | NANDRW_JFFS2,
  232. start, nbytes, &total, addr);
  233. debug ("nand_rw: ret=%x total=%d nbytes=%d\n", rc, total, nbytes);
  234. #endif
  235. }
  236. if (rc != 0) {
  237. printf("Flashing failed due to error %d\n", rc);
  238. return -1;
  239. }
  240. /*
  241. * check the dcrc of the copy
  242. */
  243. if (au_image[i].type != AU_NAND) {
  244. rc = crc32 (0, (char *)(start + off), ntohl(hdr->ih_size));
  245. } else {
  246. #if (CONFIG_COMMANDS & CFG_CMD_NAND)
  247. rc = nand_rw(nand_dev_desc, NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP,
  248. start, nbytes, &total, addr);
  249. rc = crc32 (0, (char *)(addr + off), ntohl(hdr->ih_size));
  250. #endif
  251. }
  252. if (rc != ntohl(hdr->ih_dcrc)) {
  253. printf ("Image %s Bad Data Checksum After COPY\n", au_image[i].name);
  254. return -1;
  255. }
  256. /* protect the address range */
  257. /* this assumes that ONLY the firmware is protected! */
  258. if (au_image[i].type == AU_FIRMWARE) {
  259. flash_sect_protect(1, start, end);
  260. }
  261. break;
  262. default:
  263. printf("Wrong image type selected!\n");
  264. }
  265. return 0;
  266. }
  267. static void process_macros (const char *input, char *output)
  268. {
  269. char c, prev;
  270. const char *varname_start = NULL;
  271. int inputcnt = strlen (input);
  272. int outputcnt = CFG_CBSIZE;
  273. int state = 0; /* 0 = waiting for '$' */
  274. /* 1 = waiting for '(' or '{' */
  275. /* 2 = waiting for ')' or '}' */
  276. /* 3 = waiting for ''' */
  277. #ifdef DEBUG_PARSER
  278. char *output_start = output;
  279. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
  280. #endif
  281. prev = '\0'; /* previous character */
  282. while (inputcnt && outputcnt) {
  283. c = *input++;
  284. inputcnt--;
  285. if (state!=3) {
  286. /* remove one level of escape characters */
  287. if ((c == '\\') && (prev != '\\')) {
  288. if (inputcnt-- == 0)
  289. break;
  290. prev = c;
  291. c = *input++;
  292. }
  293. }
  294. switch (state) {
  295. case 0: /* Waiting for (unescaped) $ */
  296. if ((c == '\'') && (prev != '\\')) {
  297. state = 3;
  298. break;
  299. }
  300. if ((c == '$') && (prev != '\\')) {
  301. state++;
  302. } else {
  303. *(output++) = c;
  304. outputcnt--;
  305. }
  306. break;
  307. case 1: /* Waiting for ( */
  308. if (c == '(' || c == '{') {
  309. state++;
  310. varname_start = input;
  311. } else {
  312. state = 0;
  313. *(output++) = '$';
  314. outputcnt--;
  315. if (outputcnt) {
  316. *(output++) = c;
  317. outputcnt--;
  318. }
  319. }
  320. break;
  321. case 2: /* Waiting for ) */
  322. if (c == ')' || c == '}') {
  323. int i;
  324. char envname[CFG_CBSIZE], *envval;
  325. int envcnt = input-varname_start-1; /* Varname # of chars */
  326. /* Get the varname */
  327. for (i = 0; i < envcnt; i++) {
  328. envname[i] = varname_start[i];
  329. }
  330. envname[i] = 0;
  331. /* Get its value */
  332. envval = getenv (envname);
  333. /* Copy into the line if it exists */
  334. if (envval != NULL)
  335. while ((*envval) && outputcnt) {
  336. *(output++) = *(envval++);
  337. outputcnt--;
  338. }
  339. /* Look for another '$' */
  340. state = 0;
  341. }
  342. break;
  343. case 3: /* Waiting for ' */
  344. if ((c == '\'') && (prev != '\\')) {
  345. state = 0;
  346. } else {
  347. *(output++) = c;
  348. outputcnt--;
  349. }
  350. break;
  351. }
  352. prev = c;
  353. }
  354. if (outputcnt)
  355. *output = 0;
  356. #ifdef DEBUG_PARSER
  357. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  358. strlen(output_start), output_start);
  359. #endif
  360. }
  361. /*
  362. * this is called from board_init() after the hardware has been set up
  363. * and is usable. That seems like a good time to do this.
  364. * Right now the return value is ignored.
  365. */
  366. int do_auto_update(void)
  367. {
  368. block_dev_desc_t *stor_dev;
  369. long sz;
  370. int i, res, cnt, old_ctrlc, got_ctrlc;
  371. char buffer[32];
  372. char str[80];
  373. /*
  374. * Check whether a CompactFlash is inserted
  375. */
  376. if (ide_dev_desc[0].type == DEV_TYPE_UNKNOWN) {
  377. return -1; /* no disk detected! */
  378. }
  379. /* check whether it has a partition table */
  380. stor_dev = get_dev("ide", 0);
  381. if (stor_dev == NULL) {
  382. debug ("Uknown device type\n");
  383. return -1;
  384. }
  385. if (fat_register_device(stor_dev, 1) != 0) {
  386. debug ("Unable to register ide disk 0:1 for fatls\n");
  387. return -1;
  388. }
  389. /*
  390. * Check if magic file is present
  391. */
  392. if (do_fat_read(AU_MAGIC_FILE, buffer, sizeof(buffer), LS_NO) <= 0) {
  393. return -1;
  394. }
  395. #ifdef CONFIG_AUTO_UPDATE_SHOW
  396. board_auto_update_show(1);
  397. #endif
  398. puts("\nAutoUpdate Disk detected! Trying to update system...\n");
  399. /* make sure that we see CTRL-C and save the old state */
  400. old_ctrlc = disable_ctrlc(0);
  401. /* just loop thru all the possible files */
  402. for (i = 0; i < N_AU_IMAGES; i++) {
  403. /*
  404. * Try to expand the environment var in the fname
  405. */
  406. process_macros(au_image[i].name, str);
  407. strcpy(au_image[i].name, str);
  408. printf("Reading %s ...", au_image[i].name);
  409. /* just read the header */
  410. sz = do_fat_read(au_image[i].name, LOAD_ADDR, sizeof(image_header_t), LS_NO);
  411. debug ("read %s sz %ld hdr %d\n",
  412. au_image[i].name, sz, sizeof(image_header_t));
  413. if (sz <= 0 || sz < sizeof(image_header_t)) {
  414. puts(" not found\n");
  415. continue;
  416. }
  417. if (au_check_header_valid(i, sz) < 0) {
  418. puts(" header not valid\n");
  419. continue;
  420. }
  421. sz = do_fat_read(au_image[i].name, LOAD_ADDR, MAX_LOADSZ, LS_NO);
  422. debug ("read %s sz %ld hdr %d\n",
  423. au_image[i].name, sz, sizeof(image_header_t));
  424. if (sz <= 0 || sz <= sizeof(image_header_t)) {
  425. puts(" not found\n");
  426. continue;
  427. }
  428. if (au_check_cksum_valid(i, sz) < 0) {
  429. puts(" checksum not valid\n");
  430. continue;
  431. }
  432. puts(" done\n");
  433. do {
  434. res = au_do_update(i, sz);
  435. /* let the user break out of the loop */
  436. if (ctrlc() || had_ctrlc()) {
  437. clear_ctrlc();
  438. if (res < 0)
  439. got_ctrlc = 1;
  440. break;
  441. }
  442. cnt++;
  443. } while (res < 0);
  444. }
  445. /* restore the old state */
  446. disable_ctrlc(old_ctrlc);
  447. puts("AutoUpdate finished\n\n");
  448. #ifdef CONFIG_AUTO_UPDATE_SHOW
  449. board_auto_update_show(0);
  450. #endif
  451. return 0;
  452. }
  453. int auto_update(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  454. {
  455. do_auto_update();
  456. return 0;
  457. }
  458. U_BOOT_CMD(
  459. autoupd, 1, 1, auto_update,
  460. "autoupd - Automatically update images\n",
  461. NULL
  462. );
  463. #endif /* CONFIG_AUTO_UPDATE */