2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2004-06-09 10:15:00 +00: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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Multi Image extract
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
2019-11-14 19:57:39 +00:00
|
|
|
#include <cpu_func.h>
|
2019-08-01 15:46:46 +00:00
|
|
|
#include <env.h>
|
2019-08-01 15:46:36 +00:00
|
|
|
#include <gzip.h>
|
2004-06-09 10:15:00 +00:00
|
|
|
#include <image.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2015-03-22 22:08:59 +00:00
|
|
|
#include <mapmem.h>
|
2009-12-10 09:11:21 +00:00
|
|
|
#include <watchdog.h>
|
|
|
|
#if defined(CONFIG_BZIP2)
|
|
|
|
#include <bzlib.h>
|
|
|
|
#endif
|
2004-06-09 10:15:00 +00:00
|
|
|
#include <asm/byteorder.h>
|
2020-05-10 17:39:56 +00:00
|
|
|
#include <asm/cache.h>
|
2013-08-30 17:00:09 +00:00
|
|
|
#include <asm/io.h>
|
2004-06-09 10:15:00 +00:00
|
|
|
|
2009-12-10 09:11:21 +00:00
|
|
|
#ifndef CONFIG_SYS_XIMG_LEN
|
|
|
|
/* use 8MByte as default max gunzip size */
|
|
|
|
#define CONFIG_SYS_XIMG_LEN 0x800000
|
|
|
|
#endif
|
|
|
|
|
2012-10-29 13:34:31 +00:00
|
|
|
static int
|
2020-05-10 17:40:03 +00:00
|
|
|
do_imgextract(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
|
2004-06-09 10:15:00 +00:00
|
|
|
{
|
2019-12-28 17:45:02 +00:00
|
|
|
ulong addr = image_load_addr;
|
2008-03-12 09:33:00 +00:00
|
|
|
ulong dest = 0;
|
2014-05-28 09:33:33 +00:00
|
|
|
ulong data, len;
|
2008-03-20 18:38:45 +00:00
|
|
|
int verify;
|
2008-03-12 09:33:00 +00:00
|
|
|
int part = 0;
|
2019-05-23 11:14:07 +00:00
|
|
|
#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
|
2014-05-28 09:33:33 +00:00
|
|
|
ulong count;
|
2022-09-07 02:26:52 +00:00
|
|
|
struct legacy_img_hdr *hdr = NULL;
|
2014-05-28 09:33:33 +00:00
|
|
|
#endif
|
2008-03-12 09:33:00 +00:00
|
|
|
#if defined(CONFIG_FIT)
|
2008-03-20 18:38:45 +00:00
|
|
|
const char *uname = NULL;
|
2008-03-12 09:33:00 +00:00
|
|
|
const void* fit_hdr;
|
|
|
|
int noffset;
|
|
|
|
const void *fit_data;
|
|
|
|
size_t fit_len;
|
|
|
|
#endif
|
2013-04-17 16:13:45 +00:00
|
|
|
#ifdef CONFIG_GZIP
|
2009-12-10 09:11:21 +00:00
|
|
|
uint unc_len = CONFIG_SYS_XIMG_LEN;
|
2013-04-17 16:13:45 +00:00
|
|
|
#endif
|
2009-12-10 09:11:21 +00:00
|
|
|
uint8_t comp;
|
2004-06-09 10:15:00 +00:00
|
|
|
|
2017-08-03 18:22:13 +00:00
|
|
|
verify = env_get_yesno("verify");
|
2004-06-09 10:15:00 +00:00
|
|
|
|
|
|
|
if (argc > 1) {
|
2021-07-24 15:03:29 +00:00
|
|
|
addr = hextoul(argv[1], NULL);
|
2004-06-09 10:15:00 +00:00
|
|
|
}
|
|
|
|
if (argc > 2) {
|
2021-07-24 15:03:29 +00:00
|
|
|
part = hextoul(argv[2], NULL);
|
2008-03-12 09:33:00 +00:00
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
uname = argv[2];
|
|
|
|
#endif
|
2004-06-09 10:15:00 +00:00
|
|
|
}
|
|
|
|
if (argc > 3) {
|
2021-07-24 15:03:29 +00:00
|
|
|
dest = hextoul(argv[3], NULL);
|
2004-06-09 10:15:00 +00:00
|
|
|
}
|
|
|
|
|
2011-10-18 11:11:49 +00:00
|
|
|
switch (genimg_get_format((void *)addr)) {
|
2019-05-23 11:14:07 +00:00
|
|
|
#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
|
2008-02-04 07:28:09 +00:00
|
|
|
case IMAGE_FORMAT_LEGACY:
|
2004-06-09 10:15:00 +00:00
|
|
|
|
2008-03-12 09:33:00 +00:00
|
|
|
printf("## Copying part %d from legacy image "
|
|
|
|
"at %08lx ...\n", part, addr);
|
|
|
|
|
2022-09-07 02:26:52 +00:00
|
|
|
hdr = (struct legacy_img_hdr *)addr;
|
2011-10-18 11:11:49 +00:00
|
|
|
if (!image_check_magic(hdr)) {
|
2008-02-04 07:28:09 +00:00
|
|
|
printf("Bad Magic Number\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2004-06-09 10:15:00 +00:00
|
|
|
|
2011-10-18 11:11:49 +00:00
|
|
|
if (!image_check_hcrc(hdr)) {
|
2008-02-04 07:28:09 +00:00
|
|
|
printf("Bad Header Checksum\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-12 09:33:00 +00:00
|
|
|
#ifdef DEBUG
|
2011-10-18 11:11:49 +00:00
|
|
|
image_print_contents(hdr);
|
2008-03-12 09:33:00 +00:00
|
|
|
#endif
|
2004-06-09 10:15:00 +00:00
|
|
|
|
2015-09-16 06:29:11 +00:00
|
|
|
if (!image_check_type(hdr, IH_TYPE_MULTI) &&
|
|
|
|
!image_check_type(hdr, IH_TYPE_SCRIPT)) {
|
2008-02-04 07:28:09 +00:00
|
|
|
printf("Wrong Image Type for %s command\n",
|
|
|
|
cmdtp->name);
|
|
|
|
return 1;
|
|
|
|
}
|
2004-06-09 10:15:00 +00:00
|
|
|
|
2011-10-18 11:11:49 +00:00
|
|
|
comp = image_get_comp(hdr);
|
2009-12-10 09:11:21 +00:00
|
|
|
if ((comp != IH_COMP_NONE) && (argc < 4)) {
|
|
|
|
printf("Must specify load address for %s command "
|
|
|
|
"with compressed image\n",
|
2008-02-04 07:28:09 +00:00
|
|
|
cmdtp->name);
|
2004-06-09 10:15:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-04 07:28:09 +00:00
|
|
|
if (verify) {
|
|
|
|
printf(" Verifying Checksum ... ");
|
2011-10-18 11:11:49 +00:00
|
|
|
if (!image_check_dcrc(hdr)) {
|
2008-02-04 07:28:09 +00:00
|
|
|
printf("Bad Data CRC\n");
|
|
|
|
return 1;
|
2004-06-09 10:15:00 +00:00
|
|
|
}
|
2008-02-04 07:28:09 +00:00
|
|
|
printf("OK\n");
|
2004-06-09 10:15:00 +00:00
|
|
|
}
|
2008-02-04 07:28:09 +00:00
|
|
|
|
2011-10-18 11:11:49 +00:00
|
|
|
count = image_multi_count(hdr);
|
2008-03-12 09:33:00 +00:00
|
|
|
if (part >= count) {
|
2008-02-04 07:28:09 +00:00
|
|
|
printf("Bad Image Part\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-12 09:33:00 +00:00
|
|
|
|
2011-10-18 11:11:49 +00:00
|
|
|
image_multi_getimg(hdr, part, &data, &len);
|
2008-03-12 09:33:00 +00:00
|
|
|
break;
|
2014-05-28 09:33:33 +00:00
|
|
|
#endif
|
2008-02-04 07:28:09 +00:00
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
case IMAGE_FORMAT_FIT:
|
2008-03-12 09:33:00 +00:00
|
|
|
if (uname == NULL) {
|
2011-10-18 11:11:49 +00:00
|
|
|
puts("No FIT subimage unit name\n");
|
2008-03-12 09:33:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("## Copying '%s' subimage from FIT image "
|
|
|
|
"at %08lx ...\n", uname, addr);
|
|
|
|
|
|
|
|
fit_hdr = (const void *)addr;
|
2021-02-16 00:08:09 +00:00
|
|
|
if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
|
2011-10-18 11:11:49 +00:00
|
|
|
puts("Bad FIT image format\n");
|
2008-03-12 09:33:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get subimage node offset */
|
2011-10-18 11:11:49 +00:00
|
|
|
noffset = fit_image_get_node(fit_hdr, uname);
|
2008-03-12 09:33:00 +00:00
|
|
|
if (noffset < 0) {
|
2011-10-18 11:11:49 +00:00
|
|
|
printf("Can't find '%s' FIT subimage\n", uname);
|
2008-03-12 09:33:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-01-07 09:25:52 +00:00
|
|
|
if (!fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
|
2009-12-10 09:11:21 +00:00
|
|
|
&& (argc < 4)) {
|
|
|
|
printf("Must specify load address for %s command "
|
|
|
|
"with compressed image\n",
|
|
|
|
cmdtp->name);
|
2008-03-12 09:33:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify integrity */
|
|
|
|
if (verify) {
|
2013-05-07 06:11:57 +00:00
|
|
|
if (!fit_image_verify(fit_hdr, noffset)) {
|
2011-10-18 11:11:49 +00:00
|
|
|
puts("Bad Data Hash\n");
|
2008-03-12 09:33:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 12:49:30 +00:00
|
|
|
/* get subimage/external data address and length */
|
|
|
|
if (fit_image_get_data_and_size(fit_hdr, noffset,
|
|
|
|
&fit_data, &fit_len)) {
|
2011-10-18 11:11:49 +00:00
|
|
|
puts("Could not find script subimage data\n");
|
2008-03-12 09:33:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-08-27 03:17:28 +00:00
|
|
|
if (fit_image_get_comp(fit_hdr, noffset, &comp))
|
|
|
|
comp = IH_COMP_NONE;
|
2009-12-10 09:11:21 +00:00
|
|
|
|
2008-03-20 18:38:45 +00:00
|
|
|
data = (ulong)fit_data;
|
2008-03-12 09:33:00 +00:00
|
|
|
len = (ulong)fit_len;
|
|
|
|
break;
|
2008-02-04 07:28:09 +00:00
|
|
|
#endif
|
|
|
|
default:
|
2011-10-18 11:11:49 +00:00
|
|
|
puts("Invalid image type for imxtract\n");
|
2004-06-09 10:15:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 3) {
|
2009-12-10 09:11:21 +00: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 11:11:49 +00:00
|
|
|
printf(" Loading part %d ... ", part);
|
2009-12-10 09:11:21 +00:00
|
|
|
|
|
|
|
while (l > 0) {
|
|
|
|
tail = (l > CHUNKSZ) ? CHUNKSZ : l;
|
2022-09-02 12:10:46 +00:00
|
|
|
schedule();
|
2011-10-18 11:11:49 +00:00
|
|
|
memmove(to, from, tail);
|
2009-12-10 09:11:21 +00:00
|
|
|
to += tail;
|
|
|
|
from += tail;
|
|
|
|
l -= tail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
|
2011-10-18 11:11:49 +00:00
|
|
|
printf(" Loading part %d ... ", part);
|
|
|
|
memmove((char *) dest, (char *)data, len);
|
2009-12-10 09:11:21 +00:00
|
|
|
#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
|
|
|
|
break;
|
2011-05-24 05:48:26 +00:00
|
|
|
#ifdef CONFIG_GZIP
|
2009-12-10 09:11:21 +00:00
|
|
|
case IH_COMP_GZIP:
|
2011-10-18 11:11:49 +00: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 09:11:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2011-05-24 05:48:26 +00:00
|
|
|
#endif
|
2019-05-23 11:14:07 +00:00
|
|
|
#if defined(CONFIG_BZIP2) && defined(CONFIG_LEGACY_IMAGE_FORMAT)
|
2009-12-10 09:11:21 +00:00
|
|
|
case IH_COMP_BZIP2:
|
2010-01-31 20:51:43 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-10-18 11:11:49 +00:00
|
|
|
printf(" Uncompressing part %d ... ", part);
|
2010-01-31 20:51:43 +00:00
|
|
|
/*
|
2010-03-12 22:06:04 +00:00
|
|
|
* If we've got less than 4 MB of malloc()
|
2010-01-31 20:51:43 +00:00
|
|
|
* space, use slower decompression algorithm
|
|
|
|
* which requires at most 2300 KB of memory.
|
|
|
|
*/
|
|
|
|
i = BZ2_bzBuffToBuffDecompress(
|
2013-08-30 17:00:09 +00:00
|
|
|
map_sysmem(ntohl(hdr->ih_load), 0),
|
2010-01-31 20:51:43 +00:00
|
|
|
&unc_len, (char *)data, len,
|
|
|
|
CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
|
|
|
|
0);
|
|
|
|
if (i != BZ_OK) {
|
2011-10-18 11:11:49 +00:00
|
|
|
printf("BUNZIP2 ERROR %d - "
|
2010-01-31 20:51:43 +00:00
|
|
|
"image not loaded\n", i);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-12-10 09:11:21 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_BZIP2 */
|
|
|
|
default:
|
2011-10-18 11:11:49 +00:00
|
|
|
printf("Unimplemented compression type %d\n", comp);
|
2009-12-10 09:11:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-10-18 11:11:49 +00:00
|
|
|
puts("OK\n");
|
2004-06-09 10:15:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 12:39:10 +00:00
|
|
|
flush_cache(dest, ALIGN(len, ARCH_DMA_MINALIGN));
|
2015-01-12 15:23:18 +00:00
|
|
|
|
2017-08-03 18:22:10 +00:00
|
|
|
env_set_hex("fileaddr", data);
|
|
|
|
env_set_hex("filesize", len);
|
2004-06-09 10:15:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-29 13:34:31 +00:00
|
|
|
#ifdef CONFIG_SYS_LONGHELP
|
|
|
|
static char imgextract_help_text[] =
|
2009-05-24 15:06:54 +00:00
|
|
|
"addr part [dest]\n"
|
|
|
|
" - extract <part> from legacy image at <addr> and copy to <dest>"
|
2008-03-12 09:33:00 +00:00
|
|
|
#if defined(CONFIG_FIT)
|
2009-05-24 15:06:54 +00:00
|
|
|
"\n"
|
|
|
|
"addr uname [dest]\n"
|
|
|
|
" - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
|
2008-03-12 09:33:00 +00:00
|
|
|
#endif
|
2012-10-29 13:34:31 +00:00
|
|
|
"";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
U_BOOT_CMD(
|
|
|
|
imxtract, 4, 1, do_imgextract,
|
|
|
|
"extract a part of a multi-image", imgextract_help_text
|
2008-03-12 09:33:00 +00:00
|
|
|
);
|