int au_check_cksum_valid(int idx, long nbytes) { image_header_t *hdr; 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 if (nbytes != image_get_image_size (hdr)) { printf ("Image %s bad total SIZE\n", aufile[idx]); return -1; } /* check the data CRC */ if (!image_check_dcrc (hdr)) { printf ("Image %s bad data checksum\n", aufile[idx]); return -1; } return 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); }
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 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); }
static int check_crcs(const char *crc_vars[][5], int num) { int success = 1; char start_var[11], hdr_var[11], *part_size_var; unsigned int i, start_off, hdr_off, part_size, crc_in_image; const unsigned int hdr_len = sizeof(image_header_t); static unsigned char *ddr = NULL; image_header_t *hdr_ddr; #ifdef CONFIG_SPI_FLASH static struct spi_flash *spiflash = NULL; #endif // Use the start of DDR memory for temporary storage. if(!ddr) { if(!(ddr = map_physmem(XPAR_DDR2_CONTROL_MPMC_BASEADDR, XPAR_DDR2_CONTROL_MPMC_HIGHADDR - XPAR_DDR2_CONTROL_MPMC_BASEADDR, MAP_WRBACK))) { printf("Failed to map physical memory at 0x%08X\n", XPAR_DDR2_CONTROL_MPMC_BASEADDR); return 0; } else { hdr_ddr = (image_header_t*)ddr; } } #ifdef CONFIG_SPI_FLASH // Probe for flash device. if(!spiflash) { if(!(spiflash = spi_flash_probe(0, 0, 40000000, 3))) { puts("Failed to initialize SPI flash device at 0:0.\n"); return 0; } } #endif // Loop over each flash image, copy it and its // header from flash to DDR, and perform the CRC // check based on the image and the CRC stored // within the header. for(i = 0; i < num; i++) { printf("Checking CRC for %s image... ", crc_vars[i][0]); if(getenv_r(crc_vars[i][1], start_var, sizeof(start_var)) == -1) { printf("required environment variable \"%s\" not defined.\n", crc_vars[i][1]); continue; } else { start_off = simple_strtoul(start_var, NULL, 16); } crc_in_image = (unsigned int)crc_vars[i][4]; // Header and image locations. if(!crc_in_image) { if(getenv_r(crc_vars[i][3], hdr_var, sizeof(hdr_var)) == -1) { printf("required environment variable \"%s\" not defined.\n", crc_vars[i][3]); continue; } else { hdr_off = simple_strtoul(hdr_var, NULL, 16); } } else { hdr_off = start_off; start_off += hdr_len; } // Header. #ifdef CONFIG_SPI_FLASH spi_flash_read(spiflash, hdr_off, hdr_len, ddr); #else memcpy(ddr, hdr_off, hdr_len); #endif // Sanity checks. if(!(part_size_var = getenv(crc_vars[i][2]))) { printf("required environment variable \"%s\" not defined.\n", crc_vars[i][2]); success = 0; continue; } else { part_size = simple_strtoul(part_size_var, NULL, 16); } if(part_size > 0x800000 || hdr_ddr->ih_size > part_size) { puts("failed sanity checks: exuberant sizes reported.\n"); success = 0; continue; } else if(hdr_ddr->ih_size == 0) { puts("failed sanity checks: zero size reported.\n"); success = 0; continue; } // Data image. #ifdef CONFIG_SPI_FLASH spi_flash_read(spiflash, start_off, hdr_ddr->ih_size, ddr + hdr_len); #else memcpy(ddr + hdr_len, (const void*)(start_off), hdr_ddr->ih_size); #endif // Perform CRC check. if(image_check_dcrc(hdr_ddr)) { puts("OK\n"); } else { puts("Failed\n"); success = 0; } } // CRC check status. return success; }
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 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; }
// 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 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 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; }