static const image_header_t *image_get_fdt(ulong fdt_addr) { const image_header_t *fdt_hdr = map_sysmem(fdt_addr, 0); image_print_contents(fdt_hdr); puts(" Verifying Checksum ... "); if (!image_check_hcrc(fdt_hdr)) { fdt_error("fdt header checksum invalid"); return NULL; } if (!image_check_dcrc(fdt_hdr)) { fdt_error("fdt checksum invalid"); return NULL; } puts("OK\n"); if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) { fdt_error("uImage is not a fdt"); return NULL; } if (image_get_comp(fdt_hdr) != IH_COMP_NONE) { fdt_error("uImage is compressed"); return NULL; } if (fdt_check_header((void *)image_get_data(fdt_hdr)) != 0) { fdt_error("uImage data is not a fdt"); return NULL; } return fdt_hdr; }
static int image_info (ulong addr) { void *hdr = (void *)addr; printf ("\n## Checking Image at %08lx ...\n", addr); switch (genimg_get_format (hdr)) { case IMAGE_FORMAT_LEGACY: puts (" Legacy image found\n"); if (!image_check_magic (hdr)) { puts (" Bad Magic Number\n"); return 1; } if (!image_check_hcrc (hdr)) { puts (" Bad Header Checksum\n"); return 1; } image_print_contents (hdr); /* Zoya puts (" Verifying Checksum ... "); if (!image_check_dcrc (hdr)) { puts (" Bad Data CRC\n"); return 1; } puts ("OK\n"); */ return 0; #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: puts (" FIT image found\n"); if (!fit_check_format (hdr)) { puts ("Bad FIT image format!\n"); return 1; } fit_print_contents (hdr); if (!fit_all_image_check_hashes (hdr)) { puts ("Bad hash in FIT image!\n"); return 1; } return 0; #endif default: puts ("Unknown image format!\n"); break; } return 1; }
static int do_imls_nor(void) { flash_info_t *info; int i, j; void *hdr; for (i = 0, info = &flash_info[0]; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) { if (info->flash_id == FLASH_UNKNOWN) goto next_bank; for (j = 0; j < info->sector_count; ++j) { hdr = (void *)info->start[j]; if (!hdr) goto next_sector; switch (genimg_get_format(hdr)) { #if defined(CONFIG_IMAGE_FORMAT_LEGACY) case IMAGE_FORMAT_LEGACY: if (!image_check_hcrc(hdr)) goto next_sector; printf("Legacy Image at %08lX:\n", (ulong)hdr); image_print_contents(hdr); puts(" Verifying Checksum ... "); if (!image_check_dcrc(hdr)) { puts("Bad Data CRC\n"); } else { puts("OK\n"); } break; #endif #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: if (!fit_check_format(hdr)) goto next_sector; printf("FIT Image at %08lX:\n", (ulong)hdr); fit_print_contents(hdr); break; #endif default: goto next_sector; } next_sector: ; } next_bank: ; } return 0; }
/** * image_get_kernel - verify legacy format kernel image * @img_addr: in RAM address of the legacy format image to be verified * @verify: data CRC verification flag * * image_get_kernel() verifies legacy image integrity and returns pointer to * legacy image header if image verification was completed successfully. * * returns: * pointer to a legacy image header if valid image was found * otherwise return NULL */ static image_header_t *image_get_kernel (ulong img_addr, int verify) { image_header_t *hdr = (image_header_t *)img_addr; if (!image_check_magic(hdr)) { puts ("Bad Magic Number\n"); show_boot_progress (-1); return NULL; } show_boot_progress (2); if (!image_check_hcrc (hdr)) { puts ("Bad Header Checksum\n"); show_boot_progress (-2); return NULL; } #if defined(CONFIG_MX51_BBG) || defined(CONFIG_MX51_3DS) if (image_get_load(hdr) < 0x90000000) image_set_load(hdr, image_get_load(hdr)+0x20000000); if (image_get_ep(hdr) < 0x90000000) image_set_ep(hdr, image_get_ep(hdr)+0x20000000); #endif #if defined(CONFIG_MX6SL) if (image_get_load(hdr) < 0x80000000) image_set_load(hdr, image_get_load(hdr)+0x70000000); if (image_get_ep(hdr) < 0x80000000) image_set_ep(hdr, image_get_ep(hdr)+0x70000000); #endif show_boot_progress (3); image_print_contents (hdr); if (verify) { puts (" Verifying Checksum ... "); if (!image_check_dcrc (hdr)) { printf ("Bad Data CRC\n"); show_boot_progress (-3); return NULL; } puts ("OK\n"); } show_boot_progress (4); if (!image_check_target_arch (hdr)) { printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr)); show_boot_progress (-4); return NULL; } return hdr; }
/** * image_get_ramdisk - get and verify ramdisk image * @rd_addr: ramdisk image start address * @arch: expected ramdisk architecture * @verify: checksum verification flag * * image_get_ramdisk() returns a pointer to the verified ramdisk image * header. Routine receives image start address and expected architecture * flag. Verification done covers data and header integrity and os/type/arch * fields checking. * * If dataflash support is enabled routine checks for dataflash addresses * and handles required dataflash reads. * * returns: * pointer to a ramdisk image header, if image was found and valid * otherwise, return NULL */ static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch, int verify) { const image_header_t *rd_hdr = (const image_header_t *)rd_addr; if (!image_check_magic(rd_hdr)) { puts("Bad Magic Number\n"); bootstage_error(BOOTSTAGE_ID_RD_MAGIC); return NULL; } if (!image_check_hcrc(rd_hdr)) { puts("Bad Header Checksum\n"); bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM); return NULL; } bootstage_mark(BOOTSTAGE_ID_RD_MAGIC); image_print_contents(rd_hdr); if (verify) { puts(" Verifying Checksum ... "); if (!image_check_dcrc(rd_hdr)) { puts("Bad Data CRC\n"); bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM); return NULL; } puts("OK\n"); } bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM); if (!image_check_os(rd_hdr, IH_OS_LINUX) || !image_check_arch(rd_hdr, arch) || !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) { printf("No Linux %s Ramdisk Image\n", genimg_get_arch_name(arch)); bootstage_error(BOOTSTAGE_ID_RAMDISK); return NULL; } return rd_hdr; }
static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off, size_t len) { void *imgdata; int ret; imgdata = malloc(len); if (!imgdata) { printf("May be a Legacy Image at NAND device %d offset %08llX:\n", nand_dev, off); printf(" Low memory(cannot allocate memory for image)\n"); return -ENOMEM; } ret = nand_read_skip_bad(nand, off, &len, imgdata); if (ret < 0 && ret != -EUCLEAN) { free(imgdata); return ret; } if (!image_check_hcrc(imgdata)) { free(imgdata); return 0; } printf("Legacy Image at NAND device %d offset %08llX:\n", nand_dev, off); image_print_contents(imgdata); puts(" Verifying Checksum ... "); if (!image_check_dcrc(imgdata)) puts("Bad Data CRC\n"); else puts("OK\n"); free(imgdata); return 0; }
/** * image_get_kernel - verify legacy format kernel image * @img_addr: in RAM address of the legacy format image to be verified * @verify: data CRC verification flag * * image_get_kernel() verifies legacy image integrity and returns pointer to * legacy image header if image verification was completed successfully. * * returns: * pointer to a legacy image header if valid image was found * otherwise return NULL */ static image_header_t *image_get_kernel(ulong img_addr, int verify) { image_header_t *hdr = (image_header_t *)img_addr; if (!image_check_magic(hdr)) { puts("Bad Magic Number\n"); bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC); return NULL; } bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER); if (!image_check_hcrc(hdr)) { puts("Bad Header Checksum\n"); bootstage_error(BOOTSTAGE_ID_CHECK_HEADER); return NULL; } bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM); image_print_contents(hdr); if (verify) { puts(" Verifying Checksum ... "); if (!image_check_dcrc(hdr)) { printf("Bad Data CRC\n"); bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM); return NULL; } puts("OK\n"); } bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH); if (!image_check_target_arch(hdr)) { printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr)); bootstage_error(BOOTSTAGE_ID_CHECK_ARCH); return NULL; } return hdr; }
/** * image_get_kernel - verify legacy format kernel image * @img_addr: in RAM address of the legacy format image to be verified * @verify: data CRC verification flag * * image_get_kernel() verifies legacy image integrity and returns pointer to * legacy image header if image verification was completed successfully. * * returns: * pointer to a legacy image header if valid image was found * otherwise return NULL */ static image_header_t *image_get_kernel (ulong img_addr, int verify) { image_header_t *hdr = (image_header_t *)img_addr; if (!image_check_magic(hdr)) { puts ("Bad Magic Number\n"); show_boot_progress (-1); return NULL; } show_boot_progress (2); if (!image_check_hcrc (hdr)) { puts ("Bad Header Checksum\n"); show_boot_progress (-2); return NULL; } show_boot_progress (3); image_print_contents (hdr); if (verify) { puts (" Verifying Checksum ... "); if (!image_check_dcrc (hdr)) { printf ("Bad Data CRC\n"); show_boot_progress (-3); return NULL; } puts ("OK\n"); } show_boot_progress (4); if (!image_check_target_arch (hdr)) { printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr)); show_boot_progress (-4); return NULL; } return hdr; }
int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc, char *const argv[]) { int dev, part; ulong addr = CONFIG_SYS_LOAD_ADDR; ulong cnt; disk_partition_t info; #if defined(CONFIG_IMAGE_FORMAT_LEGACY) image_header_t *hdr; #endif block_dev_desc_t *dev_desc; #if defined(CONFIG_FIT) const void *fit_hdr = NULL; #endif bootstage_mark(BOOTSTAGE_ID_IDE_START); if (argc > 3) { bootstage_error(BOOTSTAGE_ID_IDE_ADDR); return CMD_RET_USAGE; } bootstage_mark(BOOTSTAGE_ID_IDE_ADDR); if (argc > 1) addr = simple_strtoul(argv[1], NULL, 16); bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE); part = get_device_and_partition(intf, (argc == 3) ? argv[2] : NULL, &dev_desc, &info, 1); if (part < 0) { bootstage_error(BOOTSTAGE_ID_IDE_TYPE); return 1; } dev = dev_desc->dev; bootstage_mark(BOOTSTAGE_ID_IDE_TYPE); printf("\nLoading from %s device %d, partition %d: " "Name: %.32s Type: %.32s\n", intf, dev, part, info.name, info.type); debug("First Block: " LBAFU ", # of blocks: " LBAFU ", Block Size: %ld\n", info.start, info.size, info.blksz); if (dev_desc->block_read(dev, info.start, 1, (ulong *) addr) != 1) { printf("** Read error on %d:%d\n", dev, part); bootstage_error(BOOTSTAGE_ID_IDE_PART_READ); return 1; } bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ); switch (genimg_get_format((void *) addr)) { #if defined(CONFIG_IMAGE_FORMAT_LEGACY) case IMAGE_FORMAT_LEGACY: hdr = (image_header_t *) addr; bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT); if (!image_check_hcrc(hdr)) { puts("\n** Bad Header Checksum **\n"); bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM); return 1; } bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM); image_print_contents(hdr); cnt = image_get_image_size(hdr); break; #endif #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: fit_hdr = (const void *) addr; puts("Fit image detected...\n"); cnt = fit_get_size(fit_hdr); break; #endif default: bootstage_error(BOOTSTAGE_ID_IDE_FORMAT); puts("** Unknown image type\n"); return 1; } cnt += info.blksz - 1; cnt /= info.blksz; cnt -= 1; if (dev_desc->block_read(dev, info.start + 1, cnt, (ulong *)(addr + info.blksz)) != cnt) { printf("** Read error on %d:%d\n", dev, part); bootstage_error(BOOTSTAGE_ID_IDE_READ); return 1; } bootstage_mark(BOOTSTAGE_ID_IDE_READ); #if defined(CONFIG_FIT) /* This cannot be done earlier, * we need complete FIT image in RAM first */ if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) { if (!fit_check_format(fit_hdr)) { bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ); puts("** Bad FIT image format\n"); return 1; } bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK); fit_print_contents(fit_hdr); } #endif flush_cache(addr, (cnt+1)*info.blksz); /* Loading ok, update default load address */ load_addr = addr; return bootm_maybe_autostart(cmdtp, argv[0]); }
static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand, ulong offset, ulong addr, char *cmd) { int r; char *ep, *s; size_t cnt; image_header_t *hdr; #if defined(CONFIG_FIT) const void *fit_hdr = NULL; #endif s = strchr(cmd, '.'); if (s != NULL && (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) { printf("Unknown nand load suffix '%s'\n", s); show_boot_progress(-53); return 1; } printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset); cnt = nand->writesize; r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr); if (r) { puts("** Read error\n"); show_boot_progress (-56); return 1; } show_boot_progress (56); switch (genimg_get_format ((void *)addr)) { case IMAGE_FORMAT_LEGACY: hdr = (image_header_t *)addr; show_boot_progress (57); image_print_contents (hdr); cnt = image_get_image_size (hdr); break; #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: fit_hdr = (const void *)addr; puts ("Fit image detected...\n"); cnt = fit_get_size (fit_hdr); break; #endif default: show_boot_progress (-57); puts ("** Unknown image type\n"); return 1; } show_boot_progress (57); r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr); if (r) { puts("** Read error\n"); show_boot_progress (-58); return 1; } show_boot_progress (58); #if defined(CONFIG_FIT) /* This cannot be done earlier, we need complete FIT image in RAM first */ if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { if (!fit_check_format (fit_hdr)) { show_boot_progress (-150); puts ("** Bad FIT image format\n"); return 1; } show_boot_progress (151); fit_print_contents (fit_hdr); } #endif /* Loading ok, update default load address */ load_addr = addr; /* Check if we should attempt an auto-start */ if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) { char *local_args[2]; extern int do_bootm(cmd_tbl_t *, int, int, char *[]); local_args[0] = cmd; local_args[1] = NULL; printf("Automatic boot of image at addr 0x%08lx ...\n", addr); do_bootm(cmdtp, 0, 1, local_args); return 1; } return 0; }
// test the kernel static int kernel_crc_test (void) { // copy kernel from nand to ram to work on int r; size_t cnt; nand_info_t *nand; ulong offset, addr; // !!!!!!!!! for debug until working // return(0); //!!!!!!!!!! remove when nand_read working correctly image_header_t *hdr; hdr = (image_header_t *)KERNELCRCTESTSPACE; // get the header address printf("Verify kernel checksums\n"); offset = (ulong) KERNELNANDADDR; // where the kernel resides in NAND addr = (ulong) KERNELCRCTESTSPACE; // where the kernel is loaded into RAM nand = &nand_info[KERNELCRCTESTDEVICE]; // get the nand device block printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset); cnt = nand->writesize; printf("1.nand %x, offset %x, cnt %x, addr %x\n",(unsigned int)nand,(unsigned int) offset, (unsigned int) cnt, (unsigned int)addr); r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr); if (r) { puts("** Read error\n"); return (1); } switch (genimg_get_format ((void *)addr)) { case IMAGE_FORMAT_LEGACY: hdr = (image_header_t *)addr; image_print_contents (hdr); cnt = image_get_image_size (hdr) + nand->writesize; break; default: puts ("** Unknown image type\n"); return 1; } /* FIXME: skip bad blocks */ // printf("2.nand %x, offset %x, cnt %x, addr %x\n",nand, offset, cnt, addr); // cnt = 1937496; // cnt = 1937920; // debug printf("2.nand %x, offset %x, cnt %x, addr %x\n",(unsigned int)nand, (unsigned int)offset, (unsigned int)cnt, (unsigned int)addr); r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr); if (r) { printf("** Read error r = %d\n",r); return 1; } /* Loading ok, update default load address */ load_addr = addr; hdr = (image_header_t *)addr; image_print_contents (hdr); if (!image_check_hcrc (hdr)) { printf("Kernel header checksum failed\n"); printf("kernel data checksum failed\n"); return 1; } else { printf("Kernel header checksum passed\n"); if (!image_check_dcrc (hdr)) { printf ("Kernel data checksum failed\n"); return 1; } else { printf ("Kernel data checksum passed\n"); return 0; } } }
int do_docboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { char *boot_device = NULL; char *ep; int dev; ulong cnt; ulong addr; ulong offset = 0; image_header_t *hdr; int rcode = 0; #if defined(CONFIG_FIT) const void *fit_hdr = NULL; #endif show_boot_progress (34); switch (argc) { case 1: addr = CONFIG_SYS_LOAD_ADDR; boot_device = getenv ("bootdevice"); break; case 2: addr = simple_strtoul(argv[1], NULL, 16); boot_device = getenv ("bootdevice"); break; case 3: addr = simple_strtoul(argv[1], NULL, 16); boot_device = argv[2]; break; case 4: addr = simple_strtoul(argv[1], NULL, 16); boot_device = argv[2]; offset = simple_strtoul(argv[3], NULL, 16); break; default: cmd_usage(cmdtp); show_boot_progress (-35); return 1; } show_boot_progress (35); if (!boot_device) { puts ("\n** No boot device **\n"); show_boot_progress (-36); return 1; } show_boot_progress (36); dev = simple_strtoul(boot_device, &ep, 16); if ((dev >= CONFIG_SYS_MAX_DOC_DEVICE) || (doc_dev_desc[dev].ChipID == DOC_ChipID_UNKNOWN)) { printf ("\n** Device %d not available\n", dev); show_boot_progress (-37); return 1; } show_boot_progress (37); printf ("\nLoading from device %d: %s at 0x%lX (offset 0x%lX)\n", dev, doc_dev_desc[dev].name, doc_dev_desc[dev].physadr, offset); if (doc_rw (doc_dev_desc + dev, 1, offset, SECTORSIZE, NULL, (u_char *)addr)) { printf ("** Read error on %d\n", dev); show_boot_progress (-38); return 1; } show_boot_progress (38); switch (genimg_get_format ((void *)addr)) { case IMAGE_FORMAT_LEGACY: hdr = (image_header_t *)addr; image_print_contents (hdr); cnt = image_get_image_size (hdr); break; #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: fit_hdr = (const void *)addr; puts ("Fit image detected...\n"); cnt = fit_get_size (fit_hdr); break; #endif default: show_boot_progress (-39); puts ("** Unknown image type\n"); return 1; } show_boot_progress (39); cnt -= SECTORSIZE; if (doc_rw (doc_dev_desc + dev, 1, offset + SECTORSIZE, cnt, NULL, (u_char *)(addr+SECTORSIZE))) { printf ("** Read error on %d\n", dev); show_boot_progress (-40); return 1; } show_boot_progress (40); #if defined(CONFIG_FIT) /* This cannot be done earlier, we need complete FIT image in RAM first */ if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { if (!fit_check_format (fit_hdr)) { show_boot_progress (-130); puts ("** Bad FIT image format\n"); return 1; } show_boot_progress (131); fit_print_contents (fit_hdr); } #endif /* Loading ok, update default load address */ load_addr = addr; /* Check if we should attempt an auto-start */ if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) { char *local_args[2]; extern int do_bootm (cmd_tbl_t *, int, int, char *[]); local_args[0] = argv[0]; local_args[1] = NULL; printf ("Automatic boot of image at addr 0x%08lX ...\n", addr); do_bootm (cmdtp, 0, 1, local_args); rcode = 1; } return rcode; }
/****************************************************************************** * scsi boot command intepreter. Derived from diskboot */ int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { char *boot_device = NULL; char *ep; int dev, part = 0; ulong addr, cnt; disk_partition_t info; image_header_t *hdr; #if defined(CONFIG_FIT) const void *fit_hdr = NULL; #endif switch (argc) { case 1: addr = CONFIG_SYS_LOAD_ADDR; boot_device = getenv ("bootdevice"); break; case 2: addr = simple_strtoul(argv[1], NULL, 16); boot_device = getenv ("bootdevice"); break; case 3: addr = simple_strtoul(argv[1], NULL, 16); boot_device = argv[2]; break; default: return CMD_RET_USAGE; } if (!boot_device) { puts ("\n** No boot device **\n"); return 1; } dev = simple_strtoul(boot_device, &ep, 16); printf("booting from dev %d\n",dev); if (scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) { printf ("\n** Device %d not available\n", dev); return 1; } if (*ep) { if (*ep != ':') { puts ("\n** Invalid boot device, use `dev[:part]' **\n"); return 1; } part = simple_strtoul(++ep, NULL, 16); } if (get_partition_info (&scsi_dev_desc[dev], part, &info)) { printf("error reading partinfo\n"); return 1; } if ((strncmp((char *)(info.type), BOOT_PART_TYPE, sizeof(info.type)) != 0) && (strncmp((char *)(info.type), BOOT_PART_COMP, sizeof(info.type)) != 0)) { printf ("\n** Invalid partition type \"%.32s\"" " (expect \"" BOOT_PART_TYPE "\")\n", info.type); return 1; } printf ("\nLoading from SCSI device %d, partition %d: " "Name: %.32s Type: %.32s\n", dev, part, info.name, info.type); debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n", info.start, info.size, info.blksz); if (scsi_read (dev, info.start, 1, (ulong *)addr) != 1) { printf ("** Read error on %d:%d\n", dev, part); return 1; } switch (genimg_get_format ((void *)addr)) { case IMAGE_FORMAT_LEGACY: hdr = (image_header_t *)addr; if (!image_check_hcrc (hdr)) { puts ("\n** Bad Header Checksum **\n"); return 1; } image_print_contents (hdr); cnt = image_get_image_size (hdr); break; #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: fit_hdr = (const void *)addr; puts ("Fit image detected...\n"); cnt = fit_get_size (fit_hdr); break; #endif default: puts ("** Unknown image type\n"); return 1; } cnt += info.blksz - 1; cnt /= info.blksz; cnt -= 1; if (scsi_read (dev, info.start+1, cnt, (ulong *)(addr+info.blksz)) != cnt) { printf ("** Read error on %d:%d\n", dev, part); return 1; } #if defined(CONFIG_FIT) /* This cannot be done earlier, we need complete FIT image in RAM first */ if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { if (!fit_check_format (fit_hdr)) { puts ("** Bad FIT image format\n"); return 1; } fit_print_contents (fit_hdr); } #endif /* Loading ok, update default load address */ load_addr = addr; flush_cache (addr, (cnt+1)*info.blksz); return bootm_maybe_autostart(cmdtp, argv[0]); }
static int do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) { ulong addr = load_addr; ulong dest = 0; ulong data, len; int verify; int part = 0; #if defined(CONFIG_IMAGE_FORMAT_LEGACY) ulong count; image_header_t *hdr = NULL; #endif #if defined(CONFIG_FIT) const char *uname = NULL; const void* fit_hdr; int noffset; const void *fit_data; size_t fit_len; #endif #ifdef CONFIG_GZIP uint unc_len = CONFIG_SYS_XIMG_LEN; #endif uint8_t comp; verify = env_get_yesno("verify"); if (argc > 1) { addr = simple_strtoul(argv[1], NULL, 16); } if (argc > 2) { part = simple_strtoul(argv[2], NULL, 16); #if defined(CONFIG_FIT) uname = argv[2]; #endif } if (argc > 3) { dest = simple_strtoul(argv[3], NULL, 16); } switch (genimg_get_format((void *)addr)) { #if defined(CONFIG_IMAGE_FORMAT_LEGACY) case IMAGE_FORMAT_LEGACY: printf("## Copying part %d from legacy image " "at %08lx ...\n", part, addr); hdr = (image_header_t *)addr; if (!image_check_magic(hdr)) { printf("Bad Magic Number\n"); return 1; } if (!image_check_hcrc(hdr)) { printf("Bad Header Checksum\n"); return 1; } #ifdef DEBUG image_print_contents(hdr); #endif if (!image_check_type(hdr, IH_TYPE_MULTI) && !image_check_type(hdr, IH_TYPE_SCRIPT)) { printf("Wrong Image Type for %s command\n", cmdtp->name); return 1; } comp = image_get_comp(hdr); if ((comp != IH_COMP_NONE) && (argc < 4)) { printf("Must specify load address for %s command " "with compressed image\n", cmdtp->name); return 1; } if (verify) { printf(" Verifying Checksum ... "); if (!image_check_dcrc(hdr)) { printf("Bad Data CRC\n"); return 1; } printf("OK\n"); } count = image_multi_count(hdr); if (part >= count) { printf("Bad Image Part\n"); return 1; } image_multi_getimg(hdr, part, &data, &len); break; #endif #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: if (uname == NULL) { puts("No FIT subimage unit name\n"); return 1; } printf("## Copying '%s' subimage from FIT image " "at %08lx ...\n", uname, addr); fit_hdr = (const void *)addr; if (!fit_check_format(fit_hdr)) { puts("Bad FIT image format\n"); return 1; } /* get subimage node offset */ noffset = fit_image_get_node(fit_hdr, uname); if (noffset < 0) { printf("Can't find '%s' FIT subimage\n", uname); return 1; } if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE) && (argc < 4)) { printf("Must specify load address for %s command " "with compressed image\n", cmdtp->name); return 1; } /* verify integrity */ if (verify) { if (!fit_image_verify(fit_hdr, noffset)) { puts("Bad Data Hash\n"); return 1; } } /* get subimage/external data address and length */ if (fit_image_get_data_and_size(fit_hdr, noffset, &fit_data, &fit_len)) { puts("Could not find script subimage data\n"); return 1; } if (fit_image_get_comp(fit_hdr, noffset, &comp)) { puts("Could not find script subimage " "compression type\n"); return 1; } data = (ulong)fit_data; len = (ulong)fit_len; break; #endif default: puts("Invalid image type for imxtract\n"); return 1; } if (argc > 3) { switch (comp) { case IH_COMP_NONE: #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) { size_t l = len; size_t tail; void *to = (void *) dest; void *from = (void *)data; printf(" Loading part %d ... ", part); while (l > 0) { tail = (l > CHUNKSZ) ? CHUNKSZ : l; WATCHDOG_RESET(); memmove(to, from, tail); to += tail; from += tail; l -= tail; } } #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */ printf(" Loading part %d ... ", part); memmove((char *) dest, (char *)data, len); #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ break; #ifdef CONFIG_GZIP case IH_COMP_GZIP: printf(" Uncompressing part %d ... ", part); if (gunzip((void *) dest, unc_len, (uchar *) data, &len) != 0) { puts("GUNZIP ERROR - image not loaded\n"); return 1; } break; #endif #if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY) case IH_COMP_BZIP2: { int i; printf(" Uncompressing part %d ... ", part); /* * If we've got less than 4 MB of malloc() * space, use slower decompression algorithm * which requires at most 2300 KB of memory. */ i = BZ2_bzBuffToBuffDecompress( map_sysmem(ntohl(hdr->ih_load), 0), &unc_len, (char *)data, len, CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0); if (i != BZ_OK) { printf("BUNZIP2 ERROR %d - " "image not loaded\n", i); return 1; } } break; #endif /* CONFIG_BZIP2 */ default: printf("Unimplemented compression type %d\n", comp); return 1; } puts("OK\n"); } flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN)); env_set_hex("fileaddr", data); env_set_hex("filesize", len); return 0; }
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); }
int bootm_image(const image_header_t *header) { const char * failure = NULL; const char * type_name = NULL; uint32_t load, image_start, image_len; /* Display to standard output the image contents. */ image_print_contents(header); /* Validate the image header and image data CRCs */ puts(" Verifying Checksum ... "); { if (!image_check_hcrc(header)) { failure = "Header Invalid\n"; goto fail; } if (!image_check_dcrc(header)) { failure = "Data Invalid\n"; goto fail; } } puts("OK\n"); /* We ONLY support uncompressed ARM U-Boot firmware images. Check * to make sure that's what we are going to boot. */ if (!image_check_type(header, IH_TYPE_FIRMWARE)) { failure = "Image is not a firmware image\n"; goto fail; } if (!image_check_os(header, IH_OS_U_BOOT)) { failure = "Image is not u-boot firmware\n"; goto fail; } if (image_get_comp(header) != IH_COMP_NONE) { failure = "Image is compressed\n"; goto fail; } if (!image_check_target_arch(header)) { failure = "Image is not built for this processor\n"; goto fail; } type_name = genimg_get_type_name(image_get_type(header)); printf(" Loading %s ... ", type_name); { load = image_get_load(header); image_start = image_get_data(header); image_len = image_get_data_size(header); memmove_wd((void *)load, (void *)image_start, image_len, CHUNKSZ); } puts("OK\n"); /* This should never return. */ exec(load, type_name); /* However, if it does, return failed status. */ fail: puts(failure); return (BOOTM_STATUS_FAILURE); }
static int image_info (ulong addr) { void *hdr = (void *)addr; #if defined(CONFIG_ANDROID_IMG) boot_img_hdr *hdr_andr = (void *) addr; #endif printf ("\n## Checking Image at %08lx ...\n", addr); switch (genimg_get_format (hdr)) { case IMAGE_FORMAT_LEGACY: puts (" Legacy image found\n"); if (!image_check_magic (hdr)) { puts (" Bad Magic Number\n"); return 1; } if (!image_check_hcrc (hdr)) { puts (" Bad Header Checksum\n"); return 1; } image_print_contents (hdr); puts (" Verifying Checksum ... "); if (!image_check_dcrc (hdr)) { puts (" Bad Data CRC\n"); return 1; } puts ("OK\n"); return 0; #if defined(CONFIG_ANDROID_IMG) case IMAGE_FORMAT_ANDROID: puts("ANDROID!\n"); printf ("kernel addr = 0x%x\n",hdr_andr->kernel_addr); printf ("kernel size = 0x%x\n",hdr_andr->kernel_size); printf ("ramdisk addr = 0x%x\n",hdr_andr->ramdisk_addr); printf ("ramdisk size = 0x%x\n",hdr_andr->ramdisk_size); printf ("page size = 0x%x\n", hdr_andr->page_size); return 0; #endif #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: puts (" FIT image found\n"); if (!fit_check_format (hdr)) { puts ("Bad FIT image format!\n"); return 1; } fit_print_contents (hdr); if (!fit_all_image_check_hashes (hdr)) { puts ("Bad hash in FIT image!\n"); return 1; } return 0; #endif default: puts ("Unknown image format!\n"); break; } return 1; }
/**************************************************************************** * main routine do_fdcboot */ int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type; FDC_COMMAND_STRUCT *pCMD = &cmd; unsigned long addr,imsize; #if defined(CONFIG_IMAGE_FORMAT_LEGACY) image_header_t *hdr; /* used for fdc boot */ #endif unsigned char boot_drive; int i,nrofblk; #if defined(CONFIG_FIT) const void *fit_hdr = NULL; #endif switch (argc) { case 1: addr = CONFIG_SYS_LOAD_ADDR; boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER; break; case 2: addr = simple_strtoul(argv[1], NULL, 16); boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER; break; case 3: addr = simple_strtoul(argv[1], NULL, 16); boot_drive=simple_strtoul(argv[2], NULL, 10); break; default: return CMD_RET_USAGE; } /* setup FDC and scan for drives */ if (fdc_setup(boot_drive, pCMD, pFG) == false) { printf("\n** Error in setup FDC **\n"); return 1; } if (fdc_check_drive(pCMD, pFG) == false) { printf("\n** Error in check_drives **\n"); return 1; } if((pCMD->flags&(1<<boot_drive))==0) { /* drive not available */ printf("\n** Drive %d not availabe **\n",boot_drive); return 1; } if((pCMD->flags&(0x10<<boot_drive))==0) { /* no disk inserted */ printf("\n** No disk inserted in drive %d **\n",boot_drive); return 1; } /* ok, we have a valid source */ pCMD->drive=boot_drive; /* read first block */ pCMD->blnr=0; if (fdc_read_data((unsigned char *)addr, 1, pCMD, pFG) == false) { printf("\nRead error:"); for(i=0;i<7;i++) printf("result%d: 0x%02X\n",i,pCMD->result[i]); return 1; } switch (genimg_get_format ((void *)addr)) { #if defined(CONFIG_IMAGE_FORMAT_LEGACY) case IMAGE_FORMAT_LEGACY: hdr = (image_header_t *)addr; image_print_contents (hdr); imsize = image_get_image_size (hdr); break; #endif #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: fit_hdr = (const void *)addr; puts ("Fit image detected...\n"); imsize = fit_get_size (fit_hdr); break; #endif default: puts ("** Unknown image type\n"); return 1; } nrofblk=imsize/512; if((imsize%512)>0) nrofblk++; printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr); pCMD->blnr=0; if (fdc_read_data((unsigned char *)addr, nrofblk, pCMD, pFG) == false) { /* read image block */ printf("\nRead error:"); for(i=0;i<7;i++) printf("result%d: 0x%02X\n",i,pCMD->result[i]); return 1; } printf("OK %ld Bytes loaded.\n",imsize); flush_cache (addr, imsize); #if defined(CONFIG_FIT) /* This cannot be done earlier, we need complete FIT image in RAM first */ if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { if (!fit_check_format (fit_hdr)) { puts ("** Bad FIT image format\n"); return 1; } fit_print_contents (fit_hdr); } #endif /* Loading ok, update default load address */ load_addr = addr; return bootm_maybe_autostart(cmdtp, argv[0]); }
static int mpl_prg_image(uchar *ld_addr) { unsigned long len; uchar *data; image_header_t *hdr = (image_header_t *)ld_addr; int rc; #if defined(CONFIG_FIT) if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { puts ("Non legacy image format not supported\n"); return -1; } #endif if (!image_check_magic (hdr)) { puts("Bad Magic Number\n"); return 1; } image_print_contents (hdr); if (!image_check_os (hdr, IH_OS_U_BOOT)) { puts("No U-Boot Image\n"); return 1; } if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) { puts("No Firmware Image\n"); return 1; } if (!image_check_hcrc (hdr)) { puts("Bad Header Checksum\n"); return 1; } puts("Verifying Checksum ... "); if (!image_check_dcrc (hdr)) { puts("Bad Data CRC\n"); return 1; } puts("OK\n"); data = (uchar *)image_get_data (hdr); len = image_get_data_size (hdr); if (image_get_comp (hdr) != IH_COMP_NONE) { uchar *buf; /* reserve space for uncompressed image */ if ((buf = malloc(IMAGE_SIZE)) == NULL) { puts("Insufficient space for decompression\n"); return 1; } switch (image_get_comp (hdr)) { case IH_COMP_GZIP: puts("Uncompressing (GZIP) ... "); rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len); if (rc != 0) { puts("GUNZIP ERROR\n"); free(buf); return 1; } puts("OK\n"); break; #ifdef CONFIG_BZIP2 case IH_COMP_BZIP2: puts("Uncompressing (BZIP2) ... "); { uint retlen = IMAGE_SIZE; rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen, (char *)data, len, 0, 0); len = retlen; } if (rc != BZ_OK) { printf ("BUNZIP2 ERROR: %d\n", rc); free(buf); return 1; } puts("OK\n"); break; #endif default: printf ("Unimplemented compression type %d\n", image_get_comp (hdr)); free(buf); return 1; } rc = mpl_prg(buf, len); free(buf); } else { rc = mpl_prg(data, len); } return(rc); }
/**************************************************************************** * main routine do_fdcboot */ int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type; FDC_COMMAND_STRUCT *pCMD = &cmd; unsigned long addr,imsize; image_header_t *hdr; /* used for fdc boot */ unsigned char boot_drive; int i,nrofblk; char *ep; int rcode = 0; #if defined(CONFIG_FIT) const void *fit_hdr; #endif switch (argc) { case 1: addr = CFG_LOAD_ADDR; boot_drive=CFG_FDC_DRIVE_NUMBER; break; case 2: addr = simple_strtoul(argv[1], NULL, 16); boot_drive=CFG_FDC_DRIVE_NUMBER; break; case 3: addr = simple_strtoul(argv[1], NULL, 16); boot_drive=simple_strtoul(argv[2], NULL, 10); break; default: printf ("Usage:\n%s\n", cmdtp->usage); return 1; } /* setup FDC and scan for drives */ if(fdc_setup(boot_drive,pCMD,pFG)==FALSE) { printf("\n** Error in setup FDC **\n"); return 1; } if(fdc_check_drive(pCMD,pFG)==FALSE) { printf("\n** Error in check_drives **\n"); return 1; } if((pCMD->flags&(1<<boot_drive))==0) { /* drive not available */ printf("\n** Drive %d not availabe **\n",boot_drive); return 1; } if((pCMD->flags&(0x10<<boot_drive))==0) { /* no disk inserted */ printf("\n** No disk inserted in drive %d **\n",boot_drive); return 1; } /* ok, we have a valid source */ pCMD->drive=boot_drive; /* read first block */ pCMD->blnr=0; if(fdc_read_data((unsigned char *)addr,1,pCMD,pFG)==FALSE) { printf("\nRead error:"); for(i=0;i<7;i++) printf("result%d: 0x%02X\n",i,pCMD->result[i]); return 1; } switch (genimg_get_format ((void *)addr)) { case IMAGE_FORMAT_LEGACY: hdr = (image_header_t *)addr; image_print_contents (hdr); imsize = image_get_image_size (hdr); break; #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: fit_hdr = (const void *)addr; if (!fit_check_format (fit_hdr)) { puts ("** Bad FIT image format\n"); return 1; } puts ("Fit image detected...\n"); imsize = fit_get_size (fit_hdr); break; #endif default: puts ("** Unknown image type\n"); return 1; } nrofblk=imsize/512; if((imsize%512)>0) nrofblk++; printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr); pCMD->blnr=0; if(fdc_read_data((unsigned char *)addr,nrofblk,pCMD,pFG)==FALSE) { /* read image block */ printf("\nRead error:"); for(i=0;i<7;i++) printf("result%d: 0x%02X\n",i,pCMD->result[i]); return 1; } printf("OK %ld Bytes loaded.\n",imsize); flush_cache (addr, imsize); #if defined(CONFIG_FIT) /* This cannot be done earlier, we need complete FIT image in RAM first */ if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) fit_print_contents ((const void *)addr); #endif /* Loading ok, update default load address */ load_addr = addr; /* Check if we should attempt an auto-start */ if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) { char *local_args[2]; extern int do_bootm (cmd_tbl_t *, int, int, char *[]); local_args[0] = argv[0]; local_args[1] = NULL; printf ("Automatic boot of image at addr 0x%08lX ...\n", addr); do_bootm (cmdtp, 0, 1, local_args); rcode ++; } return rcode; }
int main (int argc, char **argv) { int fd = -1, err = 0, readbyte = 0, j; struct mtd_info_user mtdinfo; char buf[sizeof(image_header_t)]; int found = 0; cmdname = *argv; while (--argc > 0 && **++argv == '-') { while (*++*argv) { switch (**argv) { case 'c': if (--argc <= 0) usage (); sectorcount = (unsigned int)atoi(*++argv); cflag = 1; goto NXTARG; case 'o': if (--argc <= 0) usage (); sectoroffset = (unsigned int)atoi(*++argv); goto NXTARG; case 's': if (--argc <= 0) usage (); sectorsize = (unsigned int)atoi(*++argv); sflag = 1; goto NXTARG; default: usage (); } } NXTARG: ; } if (argc != 1 || cflag == 0 || sflag == 0) usage(); devicefile = *argv; fd = open(devicefile, O_RDONLY); if (fd < 0) { fprintf (stderr, "%s: Can't open %s: %s\n", cmdname, devicefile, strerror(errno)); exit(EXIT_FAILURE); } err = ioctl(fd, MEMGETINFO, &mtdinfo); if (err < 0) { fprintf(stderr, "%s: Cannot get MTD information: %s\n",cmdname, strerror(errno)); exit(EXIT_FAILURE); } if (mtdinfo.type != MTD_NORFLASH && mtdinfo.type != MTD_NANDFLASH) { fprintf(stderr, "%s: Unsupported flash type %u\n", cmdname, mtdinfo.type); exit(EXIT_FAILURE); } if (sectorsize * sectorcount != mtdinfo.size) { fprintf(stderr, "%s: Partition size (%d) incompatible with " "sector size and count\n", cmdname, mtdinfo.size); exit(EXIT_FAILURE); } if (sectorsize * sectoroffset >= mtdinfo.size) { fprintf(stderr, "%s: Partition size (%d) incompatible with " "sector offset given\n", cmdname, mtdinfo.size); exit(EXIT_FAILURE); } if (sectoroffset > sectorcount - 1) { fprintf(stderr, "%s: Sector offset cannot be grater than " "sector count minus one\n", cmdname); exit(EXIT_FAILURE); } printf("Searching....\n"); for (j = sectoroffset; j < sectorcount; ++j) { if (lseek(fd, j*sectorsize, SEEK_SET) != j*sectorsize) { fprintf(stderr, "%s: lseek failure: %s\n", cmdname, strerror(errno)); exit(EXIT_FAILURE); } err = flash_bad_block(fd, mtdinfo.type, j*sectorsize); if (err < 0) exit(EXIT_FAILURE); if (err) continue; /* Skip and jump to next */ readbyte = read(fd, buf, sizeof(image_header_t)); if (readbyte != sizeof(image_header_t)) { fprintf(stderr, "%s: Can't read from device: %s\n", cmdname, strerror(errno)); exit(EXIT_FAILURE); } if (fdt_check_header(buf)) { /* old-style image */ if (image_verify_header(buf, fd)) { found = 1; image_print_contents((image_header_t *)buf); } } else { /* FIT image */ fit_print_contents(buf); } } close(fd); if(!found) printf("No images found\n"); exit(EXIT_SUCCESS); }