2004-06-09 18:15:00 +08:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2000-2004
|
|
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
|
|
|
*
|
|
|
|
* (C) Copyright 2003
|
|
|
|
* Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
|
|
|
|
*
|
2013-07-08 15:37:19 +08:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2004-06-09 18:15:00 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Multi Image extract
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <image.h>
|
2009-12-10 17:11:21 +08:00
|
|
|
#include <watchdog.h>
|
|
|
|
#if defined(CONFIG_BZIP2)
|
|
|
|
#include <bzlib.h>
|
|
|
|
#endif
|
2004-06-09 18:15:00 +08:00
|
|
|
#include <asm/byteorder.h>
|
2013-08-31 01:00:09 +08:00
|
|
|
#include <asm/io.h>
|
2004-06-09 18:15:00 +08:00
|
|
|
|
2009-12-10 17:11:21 +08:00
|
|
|
#ifndef CONFIG_SYS_XIMG_LEN
|
|
|
|
/* use 8MByte as default max gunzip size */
|
|
|
|
#define CONFIG_SYS_XIMG_LEN 0x800000
|
|
|
|
#endif
|
|
|
|
|
2012-10-29 21:34:31 +08:00
|
|
|
static int
|
2010-06-29 04:00:46 +08:00
|
|
|
do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
|
2004-06-09 18:15:00 +08:00
|
|
|
{
|
2008-03-12 17:33:00 +08:00
|
|
|
ulong addr = load_addr;
|
|
|
|
ulong dest = 0;
|
2014-05-28 17:33:33 +08:00
|
|
|
ulong data, len;
|
2008-03-21 02:38:45 +08:00
|
|
|
int verify;
|
2008-03-12 17:33:00 +08:00
|
|
|
int part = 0;
|
2014-05-28 17:33:33 +08:00
|
|
|
#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
|
|
|
|
ulong count;
|
2013-08-31 01:00:09 +08:00
|
|
|
image_header_t *hdr = NULL;
|
2014-05-28 17:33:33 +08:00
|
|
|
#endif
|
2008-03-12 17:33:00 +08:00
|
|
|
#if defined(CONFIG_FIT)
|
2008-03-21 02:38:45 +08:00
|
|
|
const char *uname = NULL;
|
2008-03-12 17:33:00 +08:00
|
|
|
const void* fit_hdr;
|
|
|
|
int noffset;
|
|
|
|
const void *fit_data;
|
|
|
|
size_t fit_len;
|
|
|
|
#endif
|
2013-04-18 00:13:45 +08:00
|
|
|
#ifdef CONFIG_GZIP
|
2009-12-10 17:11:21 +08:00
|
|
|
uint unc_len = CONFIG_SYS_XIMG_LEN;
|
2013-04-18 00:13:45 +08:00
|
|
|
#endif
|
2009-12-10 17:11:21 +08:00
|
|
|
uint8_t comp;
|
2004-06-09 18:15:00 +08:00
|
|
|
|
2011-10-18 19:11:49 +08:00
|
|
|
verify = getenv_yesno("verify");
|
2004-06-09 18:15:00 +08:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
addr = simple_strtoul(argv[1], NULL, 16);
|
|
|
|
}
|
|
|
|
if (argc > 2) {
|
|
|
|
part = simple_strtoul(argv[2], NULL, 16);
|
2008-03-12 17:33:00 +08:00
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
uname = argv[2];
|
|
|
|
#endif
|
2004-06-09 18:15:00 +08:00
|
|
|
}
|
|
|
|
if (argc > 3) {
|
|
|
|
dest = simple_strtoul(argv[3], NULL, 16);
|
|
|
|
}
|
|
|
|
|
2011-10-18 19:11:49 +08:00
|
|
|
switch (genimg_get_format((void *)addr)) {
|
2014-05-28 17:33:33 +08:00
|
|
|
#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
|
2008-02-04 15:28:09 +08:00
|
|
|
case IMAGE_FORMAT_LEGACY:
|
2004-06-09 18:15:00 +08:00
|
|
|
|
2008-03-12 17:33:00 +08:00
|
|
|
printf("## Copying part %d from legacy image "
|
|
|
|
"at %08lx ...\n", part, addr);
|
|
|
|
|
2008-02-04 15:28:09 +08:00
|
|
|
hdr = (image_header_t *)addr;
|
2011-10-18 19:11:49 +08:00
|
|
|
if (!image_check_magic(hdr)) {
|
2008-02-04 15:28:09 +08:00
|
|
|
printf("Bad Magic Number\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2004-06-09 18:15:00 +08:00
|
|
|
|
2011-10-18 19:11:49 +08:00
|
|
|
if (!image_check_hcrc(hdr)) {
|
2008-02-04 15:28:09 +08:00
|
|
|
printf("Bad Header Checksum\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-12 17:33:00 +08:00
|
|
|
#ifdef DEBUG
|
2011-10-18 19:11:49 +08:00
|
|
|
image_print_contents(hdr);
|
2008-03-12 17:33:00 +08:00
|
|
|
#endif
|
2004-06-09 18:15:00 +08:00
|
|
|
|
2011-10-18 19:11:49 +08:00
|
|
|
if (!image_check_type(hdr, IH_TYPE_MULTI)) {
|
2008-02-04 15:28:09 +08:00
|
|
|
printf("Wrong Image Type for %s command\n",
|
|
|
|
cmdtp->name);
|
|
|
|
return 1;
|
|
|
|
}
|
2004-06-09 18:15:00 +08:00
|
|
|
|
2011-10-18 19:11:49 +08:00
|
|
|
comp = image_get_comp(hdr);
|
2009-12-10 17:11:21 +08:00
|
|
|
if ((comp != IH_COMP_NONE) && (argc < 4)) {
|
|
|
|
printf("Must specify load address for %s command "
|
|
|
|
"with compressed image\n",
|
2008-02-04 15:28:09 +08:00
|
|
|
cmdtp->name);
|
2004-06-09 18:15:00 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-04 15:28:09 +08:00
|
|
|
if (verify) {
|
|
|
|
printf(" Verifying Checksum ... ");
|
2011-10-18 19:11:49 +08:00
|
|
|
if (!image_check_dcrc(hdr)) {
|
2008-02-04 15:28:09 +08:00
|
|
|
printf("Bad Data CRC\n");
|
|
|
|
return 1;
|
2004-06-09 18:15:00 +08:00
|
|
|
}
|
2008-02-04 15:28:09 +08:00
|
|
|
printf("OK\n");
|
2004-06-09 18:15:00 +08:00
|
|
|
}
|
2008-02-04 15:28:09 +08:00
|
|
|
|
2011-10-18 19:11:49 +08:00
|
|
|
count = image_multi_count(hdr);
|
2008-03-12 17:33:00 +08:00
|
|
|
if (part >= count) {
|
2008-02-04 15:28:09 +08:00
|
|
|
printf("Bad Image Part\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-12 17:33:00 +08:00
|
|
|
|
2011-10-18 19:11:49 +08:00
|
|
|
image_multi_getimg(hdr, part, &data, &len);
|
2008-03-12 17:33:00 +08:00
|
|
|
break;
|
2014-05-28 17:33:33 +08:00
|
|
|
#endif
|
2008-02-04 15:28:09 +08:00
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
case IMAGE_FORMAT_FIT:
|
2008-03-12 17:33:00 +08:00
|
|
|
if (uname == NULL) {
|
2011-10-18 19:11:49 +08:00
|
|
|
puts("No FIT subimage unit name\n");
|
2008-03-12 17:33:00 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("## Copying '%s' subimage from FIT image "
|
|
|
|
"at %08lx ...\n", uname, addr);
|
|
|
|
|
|
|
|
fit_hdr = (const void *)addr;
|
2011-10-18 19:11:49 +08:00
|
|
|
if (!fit_check_format(fit_hdr)) {
|
|
|
|
puts("Bad FIT image format\n");
|
2008-03-12 17:33:00 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get subimage node offset */
|
2011-10-18 19:11:49 +08:00
|
|
|
noffset = fit_image_get_node(fit_hdr, uname);
|
2008-03-12 17:33:00 +08:00
|
|
|
if (noffset < 0) {
|
2011-10-18 19:11:49 +08:00
|
|
|
printf("Can't find '%s' FIT subimage\n", uname);
|
2008-03-12 17:33:00 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-10-18 19:11:49 +08:00
|
|
|
if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
|
2009-12-10 17:11:21 +08:00
|
|
|
&& (argc < 4)) {
|
|
|
|
printf("Must specify load address for %s command "
|
|
|
|
"with compressed image\n",
|
|
|
|
cmdtp->name);
|
2008-03-12 17:33:00 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify integrity */
|
|
|
|
if (verify) {
|
2013-05-07 14:11:57 +08:00
|
|
|
if (!fit_image_verify(fit_hdr, noffset)) {
|
2011-10-18 19:11:49 +08:00
|
|
|
puts("Bad Data Hash\n");
|
2008-03-12 17:33:00 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get subimage data address and length */
|
2011-10-18 19:11:49 +08:00
|
|
|
if (fit_image_get_data(fit_hdr, noffset,
|
2009-12-10 17:11:21 +08:00
|
|
|
&fit_data, &fit_len)) {
|
2011-10-18 19:11:49 +08:00
|
|
|
puts("Could not find script subimage data\n");
|
2008-03-12 17:33:00 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-10-18 19:11:49 +08:00
|
|
|
if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
|
|
|
|
puts("Could not find script subimage "
|
2009-12-10 17:11:21 +08:00
|
|
|
"compression type\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-03-21 02:38:45 +08:00
|
|
|
data = (ulong)fit_data;
|
2008-03-12 17:33:00 +08:00
|
|
|
len = (ulong)fit_len;
|
|
|
|
break;
|
2008-02-04 15:28:09 +08:00
|
|
|
#endif
|
|
|
|
default:
|
2011-10-18 19:11:49 +08:00
|
|
|
puts("Invalid image type for imxtract\n");
|
2004-06-09 18:15:00 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 3) {
|
2009-12-10 17:11:21 +08:00
|
|
|
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;
|
|
|
|
|
2011-10-18 19:11:49 +08:00
|
|
|
printf(" Loading part %d ... ", part);
|
2009-12-10 17:11:21 +08:00
|
|
|
|
|
|
|
while (l > 0) {
|
|
|
|
tail = (l > CHUNKSZ) ? CHUNKSZ : l;
|
|
|
|
WATCHDOG_RESET();
|
2011-10-18 19:11:49 +08:00
|
|
|
memmove(to, from, tail);
|
2009-12-10 17:11:21 +08:00
|
|
|
to += tail;
|
|
|
|
from += tail;
|
|
|
|
l -= tail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
|
2011-10-18 19:11:49 +08:00
|
|
|
printf(" Loading part %d ... ", part);
|
|
|
|
memmove((char *) dest, (char *)data, len);
|
2009-12-10 17:11:21 +08:00
|
|
|
#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
|
|
|
|
break;
|
2011-05-24 13:48:26 +08:00
|
|
|
#ifdef CONFIG_GZIP
|
2009-12-10 17:11:21 +08:00
|
|
|
case IH_COMP_GZIP:
|
2011-10-18 19:11:49 +08:00
|
|
|
printf(" Uncompressing part %d ... ", part);
|
|
|
|
if (gunzip((void *) dest, unc_len,
|
|
|
|
(uchar *) data, &len) != 0) {
|
|
|
|
puts("GUNZIP ERROR - image not loaded\n");
|
2009-12-10 17:11:21 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2011-05-24 13:48:26 +08:00
|
|
|
#endif
|
2014-05-28 17:33:33 +08:00
|
|
|
#if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY)
|
2009-12-10 17:11:21 +08:00
|
|
|
case IH_COMP_BZIP2:
|
2010-02-01 04:51:43 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-10-18 19:11:49 +08:00
|
|
|
printf(" Uncompressing part %d ... ", part);
|
2010-02-01 04:51:43 +08:00
|
|
|
/*
|
2010-03-13 06:06:04 +08:00
|
|
|
* If we've got less than 4 MB of malloc()
|
2010-02-01 04:51:43 +08:00
|
|
|
* space, use slower decompression algorithm
|
|
|
|
* which requires at most 2300 KB of memory.
|
|
|
|
*/
|
|
|
|
i = BZ2_bzBuffToBuffDecompress(
|
2013-08-31 01:00:09 +08:00
|
|
|
map_sysmem(ntohl(hdr->ih_load), 0),
|
2010-02-01 04:51:43 +08:00
|
|
|
&unc_len, (char *)data, len,
|
|
|
|
CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
|
|
|
|
0);
|
|
|
|
if (i != BZ_OK) {
|
2011-10-18 19:11:49 +08:00
|
|
|
printf("BUNZIP2 ERROR %d - "
|
2010-02-01 04:51:43 +08:00
|
|
|
"image not loaded\n", i);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-12-10 17:11:21 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_BZIP2 */
|
|
|
|
default:
|
2011-10-18 19:11:49 +08:00
|
|
|
printf("Unimplemented compression type %d\n", comp);
|
2009-12-10 17:11:21 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2011-10-18 19:11:49 +08:00
|
|
|
puts("OK\n");
|
2004-06-09 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 01:33:22 +08:00
|
|
|
setenv_hex("fileaddr", data);
|
|
|
|
setenv_hex("filesize", len);
|
2004-06-09 18:15:00 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-29 21:34:31 +08:00
|
|
|
#ifdef CONFIG_SYS_LONGHELP
|
|
|
|
static char imgextract_help_text[] =
|
2009-05-24 23:06:54 +08:00
|
|
|
"addr part [dest]\n"
|
|
|
|
" - extract <part> from legacy image at <addr> and copy to <dest>"
|
2008-03-12 17:33:00 +08:00
|
|
|
#if defined(CONFIG_FIT)
|
2009-05-24 23:06:54 +08:00
|
|
|
"\n"
|
|
|
|
"addr uname [dest]\n"
|
|
|
|
" - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
|
2008-03-12 17:33:00 +08:00
|
|
|
#endif
|
2012-10-29 21:34:31 +08:00
|
|
|
"";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
U_BOOT_CMD(
|
|
|
|
imxtract, 4, 1, do_imgextract,
|
|
|
|
"extract a part of a multi-image", imgextract_help_text
|
2008-03-12 17:33:00 +08:00
|
|
|
);
|