/* verify and prepare for booting of a legacy kernel */ int legacy_boot(void *kernel, const char *cmd_line_buf) { const image_header_t *hdr = kernel; bootm_header_t bootm_header; memset(&bootm_header, 0, sizeof(bootm_header)); if (image_get_magic(hdr) != IH_MAGIC) return 1; if (!image_check_hcrc(hdr)) { printf("Bad Header CRC\n"); return 1; } if (!image_check_dcrc(hdr)) { printf("Bad Data CRC\n"); return 1; } bootm_header.os.type = image_get_type(hdr); bootm_header.os.comp = image_get_comp(hdr); bootm_header.os.end = (uint32_t)hdr + image_get_size(hdr) + sizeof(*hdr); bootm_header.os.load = image_get_load(hdr); bootm_header.os.start = (uint32_t) hdr; bootm_header.os.image_start = (uint32_t)(hdr + 1); bootm_header.os.image_len = image_get_size(hdr); bootm_header.ep = image_get_ep(hdr); bootm_header.cmdline = cmd_line_buf; return start_legacy_kernel(&bootm_header); }
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; }
int image_check_header(image_header_t *hdr, const char *name) { if (image_check_magic(hdr) && (!strncmp(image_get_name(hdr), name, strlen(name))) && image_check_hcrc(hdr)) { return 1; } return 0; }
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"); 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; }
/** * 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; }
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; }
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 */ }
// 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 autoscript (ulong addr, const char *fit_uname) { ulong len; image_header_t *hdr; ulong *data; char *cmd; int rcode = 0; int verify; #if defined(CONFIG_FIT) const void* fit_hdr; int noffset; const void *fit_data; size_t fit_len; #endif verify = getenv_yesno ("verify"); switch (genimg_get_format ((void *)addr)) { case IMAGE_FORMAT_LEGACY: hdr = (image_header_t *)addr; if (!image_check_magic (hdr)) { puts ("Bad magic number\n"); return 1; } if (!image_check_hcrc (hdr)) { puts ("Bad header crc\n"); return 1; } if (verify) { if (!image_check_dcrc (hdr)) { puts ("Bad data crc\n"); return 1; } } if (!image_check_type (hdr, IH_TYPE_SCRIPT)) { puts ("Bad image type\n"); return 1; } /* get length of script */ data = (ulong *)image_get_data (hdr); if ((len = uimage_to_cpu (*data)) == 0) { puts ("Empty Script\n"); return 1; } /* * scripts are just multi-image files with one component, seek * past the zero-terminated sequence of image lengths to get * to the actual image data */ while (*data++); break; #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: if (fit_uname == NULL) { puts ("No FIT subimage unit name\n"); return 1; } fit_hdr = (const void *)addr; if (!fit_check_format (fit_hdr)) { puts ("Bad FIT image format\n"); return 1; } /* get script component image node offset */ noffset = fit_image_get_node (fit_hdr, fit_uname); if (noffset < 0) { printf ("Can't find '%s' FIT subimage\n", fit_uname); return 1; } if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) { puts ("Not a image image\n"); return 1; } /* verify integrity */ if (verify) { if (!fit_image_check_hashes (fit_hdr, noffset)) { puts ("Bad Data Hash\n"); return 1; } } /* get script subimage data address and length */ if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) { puts ("Could not find script subimage data\n"); return 1; } data = (ulong *)fit_data; len = (ulong)fit_len; break; #endif default: puts ("Wrong image format for autoscript\n"); return 1; } debug ("** Script length: %ld\n", len); if ((cmd = malloc (len + 1)) == NULL) { return 1; } /* make sure cmd is null terminated */ memmove (cmd, (char *)data, len); *(cmd + len) = 0; #ifdef CONFIG_SYS_HUSH_PARSER /*?? */ rcode = parse_string_outer (cmd, FLAG_PARSE_SEMICOLON); #else { char *line = cmd; char *next = cmd; /* * break into individual lines, * and execute each line; * terminate on error. */ while (*next) { if (*next == '\n') { *next = '\0'; /* run only non-empty commands */ if (*line) { debug ("** exec: \"%s\"\n", line); if (run_command (line, 0) < 0) { rcode = 1; break; } } line = next + 1; } ++next; } if (rcode == 0 && *line) rcode = (run_command(line, 0) >= 0); } #endif free (cmd); return rcode; }
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 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; }
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); }
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]); }
/****************************************************************************** * 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]); }
int source (ulong addr, const char *fit_uname) { ulong len; #if defined(CONFIG_IMAGE_FORMAT_LEGACY) const image_header_t *hdr; #endif ulong *data; int verify; void *buf; #if defined(CONFIG_FIT) const void* fit_hdr; int noffset; const void *fit_data; size_t fit_len; #endif verify = getenv_yesno ("verify"); buf = map_sysmem(addr, 0); switch (genimg_get_format(buf)) { #if defined(CONFIG_IMAGE_FORMAT_LEGACY) case IMAGE_FORMAT_LEGACY: hdr = buf; if (!image_check_magic (hdr)) { puts ("Bad magic number\n"); return 1; } if (!image_check_hcrc (hdr)) { puts ("Bad header crc\n"); return 1; } if (verify) { if (!image_check_dcrc (hdr)) { puts ("Bad data crc\n"); return 1; } } if (!image_check_type (hdr, IH_TYPE_SCRIPT)) { puts ("Bad image type\n"); return 1; } /* get length of script */ data = (ulong *)image_get_data (hdr); if ((len = uimage_to_cpu (*data)) == 0) { puts ("Empty Script\n"); return 1; } /* * scripts are just multi-image files with one component, seek * past the zero-terminated sequence of image lengths to get * to the actual image data */ while (*data++); break; #endif #if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: if (fit_uname == NULL) { puts ("No FIT subimage unit name\n"); return 1; } fit_hdr = buf; if (!fit_check_format (fit_hdr)) { puts ("Bad FIT image format\n"); return 1; } /* get script component image node offset */ noffset = fit_image_get_node (fit_hdr, fit_uname); if (noffset < 0) { printf ("Can't find '%s' FIT subimage\n", fit_uname); return 1; } if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) { puts ("Not a image image\n"); return 1; } /* verify integrity */ if (verify) { if (!fit_image_verify(fit_hdr, noffset)) { puts ("Bad Data Hash\n"); return 1; } } /* get script subimage data address and length */ if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) { puts ("Could not find script subimage data\n"); return 1; } data = (ulong *)fit_data; len = (ulong)fit_len; break; #endif default: puts ("Wrong image format for \"source\" command\n"); return 1; } debug ("** Script length: %ld\n", len); return run_command_list((char *)data, len, 0); }
static int fpga_load (fpga_t* fpga, ulong addr, int checkall) { volatile uchar *fpga_addr = (volatile uchar *)fpga->conf_base; image_header_t *hdr = (image_header_t *)addr; ulong len; uchar *data; char msg[32]; int verify, i; #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 image header and data of the net-list */ if (!image_check_magic (hdr)) { strcpy (msg, "Bad Image Magic Number"); goto failure; } if (!image_check_hcrc (hdr)) { strcpy (msg, "Bad Image Header CRC"); goto failure; } data = (uchar*)image_get_data (hdr); len = image_get_data_size (hdr); verify = getenv_yesno ("verify"); if (verify) { if (!image_check_dcrc (hdr)) { strcpy (msg, "Bad Image Data CRC"); goto failure; } } if (checkall && fpga_get_version(fpga, image_get_name (hdr)) < 0) return 1; /* align length */ if (len & 1) ++len; /* * Reset FPGA and wait for completion */ if (fpga_reset(fpga)) { strcpy (msg, "Reset Timeout"); goto failure; } printf ("(%s)... ", image_get_name (hdr)); /* * Copy data to FPGA */ fpga_control (fpga, FPGA_LOAD_MODE); while (len--) { *fpga_addr = *data++; } fpga_control (fpga, FPGA_READ_MODE); /* * Wait for completion and check error status if timeout */ for (i = 0; i < FPGA_LOAD_TIMEOUT; i++) { udelay (100); if (fpga_control (fpga, FPGA_DONE_IS_HIGH)) break; } if (i == FPGA_LOAD_TIMEOUT) { if (fpga_control(fpga, FPGA_INIT_IS_HIGH)) strcpy(msg, "Invalid Size"); else strcpy(msg, "CRC Error"); goto failure; } printf("done\n"); return 0; failure: printf("ERROR: %s\n", msg); return 1; }