int image_check_hcrc(const image_header_t *hdr) { ulong hcrc; ulong len = image_get_header_size(); image_header_t header; /* Copy header so we can blank CRC field for re-calculation */ memmove(&header, (char *)hdr, image_get_header_size()); image_set_hcrc(&header, 0); hcrc = crc32(0, (unsigned char *)&header, len); return (hcrc == image_get_hcrc(hdr)); }
/** * image_print_contents - prints out the contents of the legacy format image * @ptr: pointer to the legacy format image header * @p: pointer to prefix string * * image_print_contents() formats a multi line legacy image contents description. * The routine prints out all header fields followed by the size/offset data * for MULTI/SCRIPT images. * * returns: * no returned results */ void image_print_contents(const void *ptr) { const image_header_t *hdr = (const image_header_t *)ptr; const char __maybe_unused *p; p = IMAGE_INDENT_STRING; printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr)); if (IMAGE_ENABLE_TIMESTAMP) { printf("%sCreated: ", p); genimg_print_time((time_t)image_get_time(hdr)); } printf("%sImage Type: ", p); image_print_type(hdr); printf("%sData Size: ", p); genimg_print_size(image_get_data_size(hdr)); printf("%sLoad Address: %08x\n", p, image_get_load(hdr)); printf("%sEntry Point: %08x\n", p, image_get_ep(hdr)); if (image_check_type(hdr, IH_TYPE_MULTI) || image_check_type(hdr, IH_TYPE_SCRIPT)) { int i; ulong data, len; ulong count = image_multi_count(hdr); printf("%sContents:\n", p); for (i = 0; i < count; i++) { image_multi_getimg(hdr, i, &data, &len); printf("%s Image %d: ", p, i); genimg_print_size(len); if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) { /* * the user may need to know offsets * if planning to do something with * multiple files */ printf("%s Offset = 0x%08lx\n", p, data); } } } else if (image_check_type(hdr, IH_TYPE_FIRMWARE_IVT)) { printf("HAB Blocks: 0x%08x 0x0000 0x%08x\n", image_get_load(hdr) - image_get_header_size(), image_get_size(hdr) + image_get_header_size() - 0x1FE0); } }
static int image_verify_header(char *ptr, int fd) { int len, nread; char *data; uint32_t checksum; image_header_t *hdr = (image_header_t *)ptr; char buf[PAGE_SIZE]; if (image_get_magic(hdr) != IH_MAGIC) return 0; data = (char *)hdr; len = image_get_header_size(); checksum = image_get_hcrc(hdr); hdr->ih_hcrc = htonl(0); /* clear for re-calculation */ if (crc32(0, data, len) != checksum) { fprintf(stderr, "%s: Maybe image found but it has bad header checksum!\n", cmdname); return 0; } len = image_get_size(hdr); checksum = 0; while (len > 0) { nread = read(fd, buf, MIN(len,PAGE_SIZE)); if (nread != MIN(len,PAGE_SIZE)) { fprintf(stderr, "%s: Error while reading: %s\n", cmdname, strerror(errno)); exit(EXIT_FAILURE); } checksum = crc32(checksum, buf, nread); len -= nread; } if (checksum != image_get_dcrc(hdr)) { fprintf (stderr, "%s: Maybe image found but it has corrupted data!\n", cmdname); return 0; } return 1; }
/* ------------------------------------------------------------------------- */ int misc_init_r (void) { char *s, *e; image_header_t *hdr; time_t timestamp; struct rtc_time tm; char bootcmd[32]; hdr = (image_header_t *) (CONFIG_SYS_MONITOR_BASE - image_get_header_size ()); #if defined(CONFIG_FIT) if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { puts ("Non legacy image format not supported\n"); return -1; } #endif timestamp = (time_t)image_get_time (hdr); to_tm (timestamp, &tm); printf ("Welcome to U-Boot on Cray L1. Compiled %4d-%02d-%02d %2d:%02d:%02d (UTC)\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); #define FACTORY_SETTINGS 0xFFFC0000 if ((s = getenv ("ethaddr")) == NULL) { e = (char *) (FACTORY_SETTINGS); if (*(e + 0) != '0' || *(e + 1) != '0' || *(e + 2) != ':' || *(e + 3) != '4' || *(e + 4) != '0' || *(e + 17) != '\0') { printf ("No valid MAC address in flash location 0x3C0000!\n"); } else { printf ("Factory MAC: %s\n", e); setenv ("ethaddr", e); } } sprintf (bootcmd,"source %X",(unsigned)bootscript); setenv ("bootcmd", bootcmd); return (0); }
/* * this is called from board_init() after the hardware has been set up * and is usable. That seems like a good time to do this. * Right now the return value is ignored. */ int do_auto_update(void) { block_dev_desc_t *stor_dev; long sz; int i, res = 0, bitmap_first, cnt, old_ctrlc, got_ctrlc; char *env; long start, end; #undef ERASE_EEPROM #ifdef ERASE_EEPROM int arr[18]; memset(arr, 0, sizeof(arr)); i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr)); #endif au_usb_stor_curr_dev = -1; /* start USB */ if (usb_stop() < 0) { debug ("usb_stop failed\n"); return -1; } if (usb_init() < 0) { debug ("usb_init failed\n"); return -1; } /* * check whether a storage device is attached (assume that it's * a USB memory stick, since nothing else should be attached). */ au_usb_stor_curr_dev = usb_stor_scan(0); if (au_usb_stor_curr_dev == -1) { debug ("No device found. Not initialized?\n"); res = -1; goto xit; } /* check whether it has a partition table */ stor_dev = get_dev("usb", 0); if (stor_dev == NULL) { debug ("uknown device type\n"); res = -1; goto xit; } if (fat_register_device(stor_dev, 1) != 0) { debug ("Unable to use USB %d:%d for fatls\n", au_usb_stor_curr_dev, 1); res = -1; goto xit; } if (file_fat_detectfs() != 0) { debug ("file_fat_detectfs failed\n"); } /* initialize the array of file names */ memset(aufile, 0, sizeof(aufile)); aufile[IDX_PREPARE] = AU_PREPARE; aufile[IDX_PREINST] = AU_PREINST; aufile[IDX_FIRMWARE] = AU_FIRMWARE; aufile[IDX_KERNEL] = AU_KERNEL; aufile[IDX_APP] = AU_APP; aufile[IDX_DISK] = AU_DISK; aufile[IDX_POSTINST] = AU_POSTINST; /* initialize the array of flash sizes */ memset(ausize, 0, sizeof(ausize)); ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST; ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST; ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST; ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST; /* * now check whether start and end are defined using environment * variables. */ start = -1; end = 0; env = getenv("firmware_st"); if (env != NULL) start = simple_strtoul(env, NULL, 16); env = getenv("firmware_nd"); if (env != NULL) end = simple_strtoul(env, NULL, 16); if (start >= 0 && end && end > start) { ausize[IDX_FIRMWARE] = (end + 1) - start; aufl_layout[0].start = start; aufl_layout[0].end = end; } start = -1; end = 0; env = getenv("kernel_st"); if (env != NULL) start = simple_strtoul(env, NULL, 16); env = getenv("kernel_nd"); if (env != NULL) end = simple_strtoul(env, NULL, 16); if (start >= 0 && end && end > start) { ausize[IDX_KERNEL] = (end + 1) - start; aufl_layout[1].start = start; aufl_layout[1].end = end; } start = -1; end = 0; env = getenv("app_st"); if (env != NULL) start = simple_strtoul(env, NULL, 16); env = getenv("app_nd"); if (env != NULL) end = simple_strtoul(env, NULL, 16); if (start >= 0 && end && end > start) { ausize[IDX_APP] = (end + 1) - start; aufl_layout[2].start = start; aufl_layout[2].end = end; } start = -1; end = 0; env = getenv("disk_st"); if (env != NULL) start = simple_strtoul(env, NULL, 16); env = getenv("disk_nd"); if (env != NULL) end = simple_strtoul(env, NULL, 16); if (start >= 0 && end && end > start) { ausize[IDX_DISK] = (end + 1) - start; aufl_layout[3].start = start; aufl_layout[3].end = end; } /* make certain that HUSH is runnable */ u_boot_hush_start(); /* make sure that we see CTRL-C and save the old state */ old_ctrlc = disable_ctrlc(0); bitmap_first = 0; /* just loop thru all the possible files */ for (i = 0; i < AU_MAXFILES; i++) { /* just read the header */ sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ()); debug ("read %s sz %ld hdr %d\n", aufile[i], sz, image_get_header_size ()); if (sz <= 0 || sz < image_get_header_size ()) { debug ("%s not found\n", aufile[i]); continue; } if (au_check_header_valid(i, sz) < 0) { debug ("%s header not valid\n", aufile[i]); continue; } sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ); debug ("read %s sz %ld hdr %d\n", aufile[i], sz, image_get_header_size ()); if (sz <= 0 || sz <= image_get_header_size ()) { debug ("%s not found\n", aufile[i]); continue; } if (au_check_cksum_valid(i, sz) < 0) { debug ("%s checksum not valid\n", aufile[i]); continue; } #ifdef CONFIG_VFD /* now that we have a valid file we can display the */ /* bitmap. */ if (bitmap_first == 0) { env = getenv("bitmap2"); if (env == NULL) { trab_vfd(0); } else { /* not so simple - bitmap2 is supposed to */ /* contain the address of the bitmap */ env = (char *)simple_strtoul(env, NULL, 16); /* NOTE: these are taken from vfd_logo.h. If that file changes then */ /* these defines MUST also be updated! These may be wrong for bitmap2. */ #define VFD_LOGO_WIDTH 112 #define VFD_LOGO_HEIGHT 72 /* must call transfer_pic directly */ transfer_pic(3, (unsigned char *)env, VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH); } bitmap_first = 1; } #endif /* this is really not a good idea, but it's what the */ /* customer wants. */ cnt = 0; got_ctrlc = 0; do { res = au_do_update(i, sz); /* let the user break out of the loop */ if (ctrlc() || had_ctrlc()) { clear_ctrlc(); if (res < 0) got_ctrlc = 1; break; } cnt++; #ifdef AU_TEST_ONLY } while (res < 0 && cnt < 3); if (cnt < 3) #else } while (res < 0); #endif /* * it doesn't make sense to update the EEPROM if the * update was interrupted by the user due to errors. */ if (got_ctrlc == 0) au_update_eeprom(i); else /* enable the power switch */ *CPLD_VFD_BK &= ~POWER_OFF; }
int au_do_update(int idx, long sz) { image_header_t *hdr; char *addr; long start, end; int off, rc; uint nbytes; hdr = (image_header_t *)LOAD_ADDR; #if defined(CONFIG_FIT) if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { puts ("Non legacy image format not supported\n"); return -1; } #endif /* disable the power switch */ *CPLD_VFD_BK |= POWER_OFF; /* execute a script */ if (image_check_type (hdr, IH_TYPE_SCRIPT)) { addr = (char *)((char *)hdr + image_get_header_size ()); /* stick a NULL at the end of the script, otherwise */ /* parse_string_outer() runs off the end. */ addr[image_get_data_size (hdr)] = 0; addr += 8; parse_string_outer(addr, FLAG_PARSE_SEMICOLON); return 0; } start = aufl_layout[FIDX_TO_LIDX(idx)].start; end = aufl_layout[FIDX_TO_LIDX(idx)].end; /* unprotect the address range */ /* this assumes that ONLY the firmware is protected! */ if (idx == IDX_FIRMWARE) { #undef AU_UPDATE_TEST #ifdef AU_UPDATE_TEST /* erase it where Linux goes */ start = aufl_layout[1].start; end = aufl_layout[1].end; #endif flash_sect_protect(0, start, end); } /* * erase the address range. */ debug ("flash_sect_erase(%lx, %lx);\n", start, end); flash_sect_erase(start, end); wait_ms(100); /* strip the header - except for the kernel and ramdisk */ if (image_check_type (hdr, IH_TYPE_KERNEL) || image_check_type (hdr, IH_TYPE_RAMDISK)) { addr = (char *)hdr; off = image_get_header_size (); nbytes = image_get_image_size (hdr); } else { addr = (char *)((char *)hdr + image_get_header_size ()); #ifdef AU_UPDATE_TEST /* copy it to where Linux goes */ if (idx == IDX_FIRMWARE) start = aufl_layout[1].start; #endif off = 0; nbytes = image_get_data_size (hdr); } /* copy the data from RAM to FLASH */ debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes); rc = flash_write(addr, start, nbytes); if (rc != 0) { printf("Flashing failed due to error %d\n", rc); return -1; } /* check the dcrc of the copy */ if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) != image_get_dcrc (hdr)) { printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]); return -1; } /* protect the address range */ /* this assumes that ONLY the firmware is protected! */ if (idx == IDX_FIRMWARE) flash_sect_protect(1, start, end); return 0; }
int au_check_header_valid(int idx, long nbytes) { image_header_t *hdr; unsigned long checksum; unsigned char buf[4]; hdr = (image_header_t *)LOAD_ADDR; #if defined(CONFIG_FIT) if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { puts ("Non legacy image format not supported\n"); return -1; } #endif /* check the easy ones first */ #undef CHECK_VALID_DEBUG #ifdef CHECK_VALID_DEBUG printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC); printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM); printf("size %#x %#lx ", image_get_data_size (hdr), nbytes); printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL); #endif if (nbytes < image_get_header_size ()) { printf ("Image %s bad header SIZE\n", aufile[idx]); return -1; } if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_ARM)) { printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]); return -1; } /* check the hdr CRC */ if (!image_check_hcrc (hdr)) { printf ("Image %s bad header checksum\n", aufile[idx]); return -1; } /* check the type - could do this all in one gigantic if() */ if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) { printf ("Image %s wrong type\n", aufile[idx]); return -1; } if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) { printf ("Image %s wrong type\n", aufile[idx]); return -1; } if ((idx == IDX_DISK) && !image_check_type (hdr, IH_TYPE_FILESYSTEM)) { printf ("Image %s wrong type\n", aufile[idx]); return -1; } if ((idx == IDX_APP) && !image_check_type (hdr, IH_TYPE_RAMDISK) && !image_check_type (hdr, IH_TYPE_FILESYSTEM)) { printf ("Image %s wrong type\n", aufile[idx]); return -1; } if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST) && !image_check_type (hdr, IH_TYPE_SCRIPT)) { printf ("Image %s wrong type\n", aufile[idx]); return -1; } /* special case for prepare.img */ if (idx == IDX_PREPARE) return 0; /* recycle checksum */ checksum = image_get_data_size (hdr); /* for kernel and app the image header must also fit into flash */ if ((idx != IDX_DISK) && (idx != IDX_FIRMWARE)) checksum += image_get_header_size (); /* check the size does not exceed space in flash. HUSH scripts */ /* all have ausize[] set to 0 */ if ((ausize[idx] != 0) && (ausize[idx] < checksum)) { printf ("Image %s is bigger than FLASH\n", aufile[idx]); return -1; } /* check the time stamp from the EEPROM */ /* read it in */ i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf)); #ifdef CHECK_VALID_DEBUG printf ("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x " "as int %#x time %#x\n", buf[0], buf[1], buf[2], buf[3], *((unsigned int *)buf), image_get_time (hdr)); #endif /* check it */ if (*((unsigned int *)buf) >= image_get_time (hdr)) { printf ("Image %s is too old\n", aufile[idx]); return -1; } return 0; }
/** * genimg_get_image - get image from special storage (if necessary) * @img_addr: image start address * * genimg_get_image() checks if provided image start adddress is located * in a dataflash storage. If so, image is moved to a system RAM memory. * * returns: * image start address after possible relocation from special storage */ ulong genimg_get_image(ulong img_addr) { ulong ram_addr = img_addr; #ifdef CONFIG_HAS_DATAFLASH ulong h_size, d_size; if (addr_dataflash(img_addr)) { void *buf; /* ger RAM address */ ram_addr = CONFIG_SYS_LOAD_ADDR; /* get header size */ h_size = image_get_header_size(); #if defined(CONFIG_FIT) if (sizeof(struct fdt_header) > h_size) h_size = sizeof(struct fdt_header); #endif /* read in header */ debug(" Reading image header from dataflash address " "%08lx to RAM address %08lx\n", img_addr, ram_addr); buf = map_sysmem(ram_addr, 0); read_dataflash(img_addr, h_size, buf); /* get data size */ switch (genimg_get_format(buf)) { #if defined(CONFIG_IMAGE_FORMAT_LEGACY) case IMAGE_FORMAT_LEGACY: d_size = image_get_data_size(buf); debug(" Legacy format image found at 0x%08lx, " "size 0x%08lx\n", ram_addr, d_size); break; #endif #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: d_size = fit_get_size(buf) - h_size; debug(" FIT/FDT format image found at 0x%08lx, " "size 0x%08lx\n", ram_addr, d_size); break; #endif default: printf(" No valid image found at 0x%08lx\n", img_addr); return ram_addr; } /* read in image data */ debug(" Reading image remaining data from dataflash address " "%08lx to RAM address %08lx\n", img_addr + h_size, ram_addr + h_size); read_dataflash(img_addr + h_size, d_size, (char *)(buf + h_size)); } #endif /* CONFIG_HAS_DATAFLASH */ return ram_addr; }
int main (int argc, char **argv) { int ifd = -1; uint32_t checksum; uint32_t addr; uint32_t ep; struct stat sbuf; unsigned char *ptr; char *name = ""; cmdname = *argv; addr = ep = 0; while (--argc > 0 && **++argv == '-') { while (*++*argv) { switch (**argv) { case 'l': lflag = 1; break; case 'A': if ((--argc <= 0) || (opt_arch = genimg_get_arch_id (*++argv)) < 0) usage (); goto NXTARG; case 'C': if ((--argc <= 0) || (opt_comp = genimg_get_comp_id (*++argv)) < 0) usage (); goto NXTARG; case 'D': if (--argc <= 0) usage (); opt_dtc = *++argv; goto NXTARG; case 'O': if ((--argc <= 0) || (opt_os = genimg_get_os_id (*++argv)) < 0) usage (); goto NXTARG; case 'T': if ((--argc <= 0) || (opt_type = genimg_get_type_id (*++argv)) < 0) usage (); goto NXTARG; case 'a': if (--argc <= 0) usage (); addr = strtoul (*++argv, (char **)&ptr, 16); if (*ptr) { fprintf (stderr, "%s: invalid load address %s\n", cmdname, *argv); exit (EXIT_FAILURE); } goto NXTARG; case 'd': if (--argc <= 0) usage (); datafile = *++argv; dflag = 1; goto NXTARG; case 'e': if (--argc <= 0) usage (); ep = strtoul (*++argv, (char **)&ptr, 16); if (*ptr) { fprintf (stderr, "%s: invalid entry point %s\n", cmdname, *argv); exit (EXIT_FAILURE); } eflag = 1; goto NXTARG; case 'f': if (--argc <= 0) usage (); datafile = *++argv; fflag = 1; goto NXTARG; case 'n': if (--argc <= 0) usage (); name = *++argv; goto NXTARG; case 'v': vflag++; break; case 'x': xflag++; break; default: usage (); } } NXTARG: ; } if ((argc != 1) || (dflag && (fflag || lflag)) || (fflag && (dflag || lflag)) || (lflag && (dflag || fflag))) usage(); if (!eflag) { ep = addr; /* If XIP, entry point must be after the U-Boot header */ if (xflag) ep += image_get_header_size (); } /* * If XIP, ensure the entry point is equal to the load address plus * the size of the U-Boot header. */ if (xflag) { if (ep != addr + image_get_header_size ()) { fprintf (stderr, "%s: For XIP, the entry point must be the load addr + %lu\n", cmdname, (unsigned long)image_get_header_size ()); exit (EXIT_FAILURE); } } imagefile = *argv; if (!fflag){ if (lflag) { ifd = open (imagefile, O_RDONLY|O_BINARY); } else { ifd = open (imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); } if (ifd < 0) { fprintf (stderr, "%s: Can't open %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } } if (lflag) { /* * list header information of existing image */ if (fstat(ifd, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } if ((unsigned)sbuf.st_size < image_get_header_size ()) { fprintf (stderr, "%s: Bad size: \"%s\" is no valid image\n", cmdname, imagefile); exit (EXIT_FAILURE); } ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); if (ptr == MAP_FAILED) { fprintf (stderr, "%s: Can't read %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } if (fdt_check_header (ptr)) { /* old-style image */ image_verify_header ((char *)ptr, sbuf.st_size); image_print_contents ((image_header_t *)ptr); } else { /* FIT image */ fit_print_contents (ptr); } (void) munmap((void *)ptr, sbuf.st_size); (void) close (ifd); exit (EXIT_SUCCESS); } else if (fflag) { /* Flattened Image Tree (FIT) format handling */ debug ("FIT format handling\n"); fit_handle_file (); exit (EXIT_SUCCESS); } /* * Must be -w then: * * write dummy header, to be fixed later */ memset (hdr, 0, image_get_header_size ()); if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) { fprintf (stderr, "%s: Write error on %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) { char *file = datafile; uint32_t size; for (;;) { char *sep = NULL; if (file) { if ((sep = strchr(file, ':')) != NULL) { *sep = '\0'; } if (stat (file, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", cmdname, file, strerror(errno)); exit (EXIT_FAILURE); } size = cpu_to_uimage (sbuf.st_size); } else { size = 0; } if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { fprintf (stderr, "%s: Write error on %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } if (!file) { break; } if (sep) { *sep = ':'; file = sep + 1; } else { file = NULL; } } file = datafile; for (;;) { char *sep = strchr(file, ':'); if (sep) { *sep = '\0'; copy_file (ifd, file, 1); *sep++ = ':'; file = sep; } else { copy_file (ifd, file, 0); break; } } } else { copy_file (ifd, datafile, 0); } /* We're a bit of paranoid */ #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__) (void) fdatasync (ifd); #else (void) fsync (ifd); #endif if (fstat(ifd, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); if (ptr == MAP_FAILED) { fprintf (stderr, "%s: Can't map %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } hdr = (image_header_t *)ptr; checksum = crc32 (0, (const char *)(ptr + image_get_header_size ()), sbuf.st_size - image_get_header_size () ); /* Build new header */ image_set_magic (hdr, IH_MAGIC); image_set_time (hdr, sbuf.st_mtime); image_set_size (hdr, sbuf.st_size - image_get_header_size ()); image_set_load (hdr, addr); image_set_ep (hdr, ep); image_set_dcrc (hdr, checksum); image_set_os (hdr, opt_os); image_set_arch (hdr, opt_arch); image_set_type (hdr, opt_type); image_set_comp (hdr, opt_comp); image_set_name (hdr, name); checksum = crc32 (0, (const char *)hdr, image_get_header_size ()); image_set_hcrc (hdr, checksum); image_print_contents (hdr); (void) munmap((void *)ptr, sbuf.st_size); /* We're a bit of paranoid */ #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__) (void) fdatasync (ifd); #else (void) fsync (ifd); #endif if (close(ifd)) { fprintf (stderr, "%s: Write error on %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } exit (EXIT_SUCCESS); }
static void copy_file (int ifd, const char *datafile, int pad) { int dfd; struct stat sbuf; unsigned char *ptr; int tail; int zero = 0; int offset = 0; int size; if (vflag) { fprintf (stderr, "Adding Image %s\n", datafile); } if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { fprintf (stderr, "%s: Can't open %s: %s\n", cmdname, datafile, strerror(errno)); exit (EXIT_FAILURE); } if (fstat(dfd, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", cmdname, datafile, strerror(errno)); exit (EXIT_FAILURE); } ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); if (ptr == MAP_FAILED) { fprintf (stderr, "%s: Can't read %s: %s\n", cmdname, datafile, strerror(errno)); exit (EXIT_FAILURE); } if (xflag) { unsigned char *p = NULL; /* * XIP: do not append the image_header_t at the * beginning of the file, but consume the space * reserved for it. */ if ((unsigned)sbuf.st_size < image_get_header_size ()) { fprintf (stderr, "%s: Bad size: \"%s\" is too small for XIP\n", cmdname, datafile); exit (EXIT_FAILURE); } for (p = ptr; p < ptr + image_get_header_size (); p++) { if ( *p != 0xff ) { fprintf (stderr, "%s: Bad file: \"%s\" has invalid buffer for XIP\n", cmdname, datafile); exit (EXIT_FAILURE); } } offset = image_get_header_size (); } size = sbuf.st_size - offset; if (write(ifd, ptr + offset, size) != size) { fprintf (stderr, "%s: Write error on %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } if (pad && ((tail = size % 4) != 0)) { if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { fprintf (stderr, "%s: Write error on %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } } (void) munmap((void *)ptr, sbuf.st_size); (void) close (dfd); }
/* * this is called from board_init() after the hardware has been set up * and is usable. That seems like a good time to do this. * Right now the return value is ignored. */ int do_auto_update(void) { block_dev_desc_t *stor_dev; long sz; int i, res = 0, cnt, old_ctrlc; char *env; long start, end; #if 0 /* disable key-press detection to speed up boot-up time */ uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0}; /* * Read keypad status */ i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2); mdelay(500); i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2); /* * Check keypad */ if ( !(keypad_status1[1] & KEYPAD_MASK_LO) || (keypad_status1[1] != keypad_status2[1])) { return 0; } #endif au_usb_stor_curr_dev = -1; /* start USB */ if (usb_stop() < 0) { debug ("usb_stop failed\n"); return -1; } if (usb_init() < 0) { debug ("usb_init failed\n"); return -1; } /* * check whether a storage device is attached (assume that it's * a USB memory stick, since nothing else should be attached). */ au_usb_stor_curr_dev = usb_stor_scan(0); if (au_usb_stor_curr_dev == -1) { debug ("No device found. Not initialized?\n"); res = -1; goto xit; } /* check whether it has a partition table */ stor_dev = get_dev("usb", 0); if (stor_dev == NULL) { debug ("uknown device type\n"); res = -1; goto xit; } if (fat_register_device(stor_dev, 1) != 0) { debug ("Unable to use USB %d:%d for fatls\n", au_usb_stor_curr_dev, 1); res = -1; goto xit; } if (file_fat_detectfs() != 0) { debug ("file_fat_detectfs failed\n"); } /* * now check whether start and end are defined using environment * variables. */ start = -1; end = 0; env = getenv("firmware_st"); if (env != NULL) start = simple_strtoul(env, NULL, 16); env = getenv("firmware_nd"); if (env != NULL) end = simple_strtoul(env, NULL, 16); if (start >= 0 && end && end > start) { ausize[IDX_FIRMWARE] = (end + 1) - start; aufl_layout[IDX_FIRMWARE].start = start; aufl_layout[IDX_FIRMWARE].end = end; } start = -1; end = 0; env = getenv("kernel_st"); if (env != NULL) start = simple_strtoul(env, NULL, 16); env = getenv("kernel_nd"); if (env != NULL) end = simple_strtoul(env, NULL, 16); if (start >= 0 && end && end > start) { ausize[IDX_KERNEL] = (end + 1) - start; aufl_layout[IDX_KERNEL].start = start; aufl_layout[IDX_KERNEL].end = end; } start = -1; end = 0; env = getenv("rootfs_st"); if (env != NULL) start = simple_strtoul(env, NULL, 16); env = getenv("rootfs_nd"); if (env != NULL) end = simple_strtoul(env, NULL, 16); if (start >= 0 && end && end > start) { ausize[IDX_ROOTFS] = (end + 1) - start; aufl_layout[IDX_ROOTFS].start = start; aufl_layout[IDX_ROOTFS].end = end; } /* make certain that HUSH is runnable */ u_boot_hush_start(); /* make sure that we see CTRL-C and save the old state */ old_ctrlc = disable_ctrlc(0); /* validate the images first */ for (i = 0; i < AU_MAXFILES; i++) { ulong imsize; /* just read the header */ sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ()); debug ("read %s sz %ld hdr %d\n", aufile[i], sz, image_get_header_size ()); if (sz <= 0 || sz < image_get_header_size ()) { debug ("%s not found\n", aufile[i]); ausize[i] = 0; continue; } /* au_check_header_valid() updates ausize[] */ if ((imsize = au_check_header_valid(i, sz)) < 0) { debug ("%s header not valid\n", aufile[i]); continue; } /* totsize accounts for image size and flash erase size */ totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start)); } #ifdef CONFIG_PROGRESSBAR if (totsize) { lcd_puts(" Update in progress\n"); lcd_enable(); } #endif /* just loop thru all the possible files */ for (i = 0; i < AU_MAXFILES && totsize; i++) { if (!ausize[i]) { continue; } sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]); debug ("read %s sz %ld hdr %d\n", aufile[i], sz, image_get_header_size ()); if (sz != ausize[i]) { printf ("%s: size %ld read %ld?\n", aufile[i], ausize[i], sz); continue; } if (sz <= 0 || sz <= image_get_header_size ()) { debug ("%s not found\n", aufile[i]); continue; } if (au_check_cksum_valid(i, sz) < 0) { debug ("%s checksum not valid\n", aufile[i]); continue; } /* this is really not a good idea, but it's what the */ /* customer wants. */ cnt = 0; do { res = au_do_update(i, sz); /* let the user break out of the loop */ if (ctrlc() || had_ctrlc()) { clear_ctrlc(); break; } cnt++; #ifdef AU_TEST_ONLY } while (res < 0 && cnt < (AU_MAXFILES + 1)); if (cnt < (AU_MAXFILES + 1)) #else } while (res < 0); #endif }
int au_check_header_valid(int idx, long nbytes) { image_header_t *hdr; unsigned long checksum, fsize; hdr = (image_header_t *)LOAD_ADDR; #if defined(CONFIG_FIT) if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { puts ("Non legacy image format not supported\n"); return -1; } #endif /* check the easy ones first */ #undef CHECK_VALID_DEBUG #ifdef CHECK_VALID_DEBUG printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC); printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM); printf("size %#x %#lx ", image_get_data_size (hdr), nbytes); printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL); #endif if (nbytes < image_get_header_size ()) { printf ("Image %s bad header SIZE\n", aufile[idx]); ausize[idx] = 0; return -1; } if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) { printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]); ausize[idx] = 0; return -1; } /* check the hdr CRC */ if (!image_check_hcrc (hdr)) { printf ("Image %s bad header checksum\n", aufile[idx]); ausize[idx] = 0; return -1; } /* check the type - could do this all in one gigantic if() */ if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) { printf ("Image %s wrong type\n", aufile[idx]); ausize[idx] = 0; return -1; } if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) { printf ("Image %s wrong type\n", aufile[idx]); ausize[idx] = 0; return -1; } if ((idx == IDX_ROOTFS) && (!image_check_type (hdr, IH_TYPE_RAMDISK) && !image_check_type (hdr, IH_TYPE_FILESYSTEM))) { printf ("Image %s wrong type\n", aufile[idx]); ausize[idx] = 0; return -1; } /* recycle checksum */ checksum = image_get_data_size (hdr); fsize = checksum + image_get_header_size (); /* for kernel and ramdisk the image header must also fit into flash */ if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK)) checksum += image_get_header_size (); /* check the size does not exceed space in flash. HUSH scripts */ if ((ausize[idx] != 0) && (ausize[idx] < checksum)) { printf ("Image %s is bigger than FLASH\n", aufile[idx]); ausize[idx] = 0; return -1; } /* Update with the real filesize */ ausize[idx] = fsize; return checksum; /* return size to be written to flash */ }