wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2004 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2003 |
| 6 | * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de> |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * Multi Image extract |
| 14 | */ |
| 15 | #include <common.h> |
| 16 | #include <command.h> |
| 17 | #include <image.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 18 | #include <mapmem.h> |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 19 | #include <watchdog.h> |
| 20 | #if defined(CONFIG_BZIP2) |
| 21 | #include <bzlib.h> |
| 22 | #endif |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 23 | #include <asm/byteorder.h> |
Simon Glass | 628af17 | 2013-08-30 11:00:09 -0600 | [diff] [blame] | 24 | #include <asm/io.h> |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 25 | |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 26 | #ifndef CONFIG_SYS_XIMG_LEN |
| 27 | /* use 8MByte as default max gunzip size */ |
| 28 | #define CONFIG_SYS_XIMG_LEN 0x800000 |
| 29 | #endif |
| 30 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 31 | static int |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 32 | do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 33 | { |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 34 | ulong addr = load_addr; |
| 35 | ulong dest = 0; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 36 | ulong data, len; |
Bartlomiej Sieka | fbe7a15 | 2008-03-20 19:38:45 +0100 | [diff] [blame] | 37 | int verify; |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 38 | int part = 0; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 39 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
| 40 | ulong count; |
Simon Glass | 628af17 | 2013-08-30 11:00:09 -0600 | [diff] [blame] | 41 | image_header_t *hdr = NULL; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 42 | #endif |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 43 | #if defined(CONFIG_FIT) |
Bartlomiej Sieka | fbe7a15 | 2008-03-20 19:38:45 +0100 | [diff] [blame] | 44 | const char *uname = NULL; |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 45 | const void* fit_hdr; |
| 46 | int noffset; |
| 47 | const void *fit_data; |
| 48 | size_t fit_len; |
| 49 | #endif |
Simon Glass | a92181b | 2013-04-17 16:13:45 +0000 | [diff] [blame] | 50 | #ifdef CONFIG_GZIP |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 51 | uint unc_len = CONFIG_SYS_XIMG_LEN; |
Simon Glass | a92181b | 2013-04-17 16:13:45 +0000 | [diff] [blame] | 52 | #endif |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 53 | uint8_t comp; |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 54 | |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 55 | verify = getenv_yesno("verify"); |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 56 | |
| 57 | if (argc > 1) { |
| 58 | addr = simple_strtoul(argv[1], NULL, 16); |
| 59 | } |
| 60 | if (argc > 2) { |
| 61 | part = simple_strtoul(argv[2], NULL, 16); |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 62 | #if defined(CONFIG_FIT) |
| 63 | uname = argv[2]; |
| 64 | #endif |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 65 | } |
| 66 | if (argc > 3) { |
| 67 | dest = simple_strtoul(argv[3], NULL, 16); |
| 68 | } |
| 69 | |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 70 | switch (genimg_get_format((void *)addr)) { |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 71 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 72 | case IMAGE_FORMAT_LEGACY: |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 73 | |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 74 | printf("## Copying part %d from legacy image " |
| 75 | "at %08lx ...\n", part, addr); |
| 76 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 77 | hdr = (image_header_t *)addr; |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 78 | if (!image_check_magic(hdr)) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 79 | printf("Bad Magic Number\n"); |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 80 | return 1; |
| 81 | } |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 82 | |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 83 | if (!image_check_hcrc(hdr)) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 84 | printf("Bad Header Checksum\n"); |
| 85 | return 1; |
| 86 | } |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 87 | #ifdef DEBUG |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 88 | image_print_contents(hdr); |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 89 | #endif |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 90 | |
Pierre Aubert | 83636fa | 2015-09-16 08:29:11 +0200 | [diff] [blame] | 91 | if (!image_check_type(hdr, IH_TYPE_MULTI) && |
| 92 | !image_check_type(hdr, IH_TYPE_SCRIPT)) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 93 | printf("Wrong Image Type for %s command\n", |
| 94 | cmdtp->name); |
| 95 | return 1; |
| 96 | } |
| 97 | |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 98 | comp = image_get_comp(hdr); |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 99 | if ((comp != IH_COMP_NONE) && (argc < 4)) { |
| 100 | printf("Must specify load address for %s command " |
| 101 | "with compressed image\n", |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 102 | cmdtp->name); |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | if (verify) { |
| 107 | printf(" Verifying Checksum ... "); |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 108 | if (!image_check_dcrc(hdr)) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 109 | printf("Bad Data CRC\n"); |
| 110 | return 1; |
| 111 | } |
| 112 | printf("OK\n"); |
| 113 | } |
| 114 | |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 115 | count = image_multi_count(hdr); |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 116 | if (part >= count) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 117 | printf("Bad Image Part\n"); |
| 118 | return 1; |
| 119 | } |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 120 | |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 121 | image_multi_getimg(hdr, part, &data, &len); |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 122 | break; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 123 | #endif |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 124 | #if defined(CONFIG_FIT) |
| 125 | case IMAGE_FORMAT_FIT: |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 126 | if (uname == NULL) { |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 127 | puts("No FIT subimage unit name\n"); |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | printf("## Copying '%s' subimage from FIT image " |
| 132 | "at %08lx ...\n", uname, addr); |
| 133 | |
| 134 | fit_hdr = (const void *)addr; |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 135 | if (!fit_check_format(fit_hdr)) { |
| 136 | puts("Bad FIT image format\n"); |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | /* get subimage node offset */ |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 141 | noffset = fit_image_get_node(fit_hdr, uname); |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 142 | if (noffset < 0) { |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 143 | printf("Can't find '%s' FIT subimage\n", uname); |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 144 | return 1; |
| 145 | } |
| 146 | |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 147 | if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE) |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 148 | && (argc < 4)) { |
| 149 | printf("Must specify load address for %s command " |
| 150 | "with compressed image\n", |
| 151 | cmdtp->name); |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | /* verify integrity */ |
| 156 | if (verify) { |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame] | 157 | if (!fit_image_verify(fit_hdr, noffset)) { |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 158 | puts("Bad Data Hash\n"); |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 159 | return 1; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* get subimage data address and length */ |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 164 | if (fit_image_get_data(fit_hdr, noffset, |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 165 | &fit_data, &fit_len)) { |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 166 | puts("Could not find script subimage data\n"); |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 167 | return 1; |
| 168 | } |
| 169 | |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 170 | if (fit_image_get_comp(fit_hdr, noffset, &comp)) { |
| 171 | puts("Could not find script subimage " |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 172 | "compression type\n"); |
| 173 | return 1; |
| 174 | } |
| 175 | |
Bartlomiej Sieka | fbe7a15 | 2008-03-20 19:38:45 +0100 | [diff] [blame] | 176 | data = (ulong)fit_data; |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 177 | len = (ulong)fit_len; |
| 178 | break; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 179 | #endif |
| 180 | default: |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 181 | puts("Invalid image type for imxtract\n"); |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 182 | return 1; |
| 183 | } |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 184 | |
| 185 | if (argc > 3) { |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 186 | switch (comp) { |
| 187 | case IH_COMP_NONE: |
| 188 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
| 189 | { |
| 190 | size_t l = len; |
| 191 | size_t tail; |
| 192 | void *to = (void *) dest; |
| 193 | void *from = (void *)data; |
| 194 | |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 195 | printf(" Loading part %d ... ", part); |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 196 | |
| 197 | while (l > 0) { |
| 198 | tail = (l > CHUNKSZ) ? CHUNKSZ : l; |
| 199 | WATCHDOG_RESET(); |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 200 | memmove(to, from, tail); |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 201 | to += tail; |
| 202 | from += tail; |
| 203 | l -= tail; |
| 204 | } |
| 205 | } |
| 206 | #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */ |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 207 | printf(" Loading part %d ... ", part); |
| 208 | memmove((char *) dest, (char *)data, len); |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 209 | #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ |
| 210 | break; |
Matthew McClintock | 0e0996e | 2011-05-24 05:48:26 +0000 | [diff] [blame] | 211 | #ifdef CONFIG_GZIP |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 212 | case IH_COMP_GZIP: |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 213 | printf(" Uncompressing part %d ... ", part); |
| 214 | if (gunzip((void *) dest, unc_len, |
| 215 | (uchar *) data, &len) != 0) { |
| 216 | puts("GUNZIP ERROR - image not loaded\n"); |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 217 | return 1; |
| 218 | } |
| 219 | break; |
Matthew McClintock | 0e0996e | 2011-05-24 05:48:26 +0000 | [diff] [blame] | 220 | #endif |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 221 | #if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY) |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 222 | case IH_COMP_BZIP2: |
Wolfgang Denk | 5f566f4 | 2010-01-31 21:51:43 +0100 | [diff] [blame] | 223 | { |
| 224 | int i; |
| 225 | |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 226 | printf(" Uncompressing part %d ... ", part); |
Wolfgang Denk | 5f566f4 | 2010-01-31 21:51:43 +0100 | [diff] [blame] | 227 | /* |
Wolfgang Denk | 93910ed | 2010-03-12 23:06:04 +0100 | [diff] [blame] | 228 | * If we've got less than 4 MB of malloc() |
Wolfgang Denk | 5f566f4 | 2010-01-31 21:51:43 +0100 | [diff] [blame] | 229 | * space, use slower decompression algorithm |
| 230 | * which requires at most 2300 KB of memory. |
| 231 | */ |
| 232 | i = BZ2_bzBuffToBuffDecompress( |
Simon Glass | 628af17 | 2013-08-30 11:00:09 -0600 | [diff] [blame] | 233 | map_sysmem(ntohl(hdr->ih_load), 0), |
Wolfgang Denk | 5f566f4 | 2010-01-31 21:51:43 +0100 | [diff] [blame] | 234 | &unc_len, (char *)data, len, |
| 235 | CONFIG_SYS_MALLOC_LEN < (4096 * 1024), |
| 236 | 0); |
| 237 | if (i != BZ_OK) { |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 238 | printf("BUNZIP2 ERROR %d - " |
Wolfgang Denk | 5f566f4 | 2010-01-31 21:51:43 +0100 | [diff] [blame] | 239 | "image not loaded\n", i); |
| 240 | return 1; |
| 241 | } |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 242 | } |
| 243 | break; |
| 244 | #endif /* CONFIG_BZIP2 */ |
| 245 | default: |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 246 | printf("Unimplemented compression type %d\n", comp); |
Wolfgang Wegner | 5912d36 | 2009-12-10 10:11:21 +0100 | [diff] [blame] | 247 | return 1; |
| 248 | } |
Stephen Warren | 712fbcf | 2011-10-18 11:11:49 +0000 | [diff] [blame] | 249 | puts("OK\n"); |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Pieter Voorthuijsen | c72b65c | 2015-01-12 16:23:18 +0100 | [diff] [blame] | 252 | flush_cache(dest, len); |
| 253 | |
Simon Glass | 41ef372 | 2013-02-24 17:33:22 +0000 | [diff] [blame] | 254 | setenv_hex("fileaddr", data); |
| 255 | setenv_hex("filesize", len); |
wdenk | 48abe7b | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 260 | #ifdef CONFIG_SYS_LONGHELP |
| 261 | static char imgextract_help_text[] = |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 262 | "addr part [dest]\n" |
| 263 | " - extract <part> from legacy image at <addr> and copy to <dest>" |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 264 | #if defined(CONFIG_FIT) |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 265 | "\n" |
| 266 | "addr uname [dest]\n" |
| 267 | " - extract <uname> subimage from FIT image at <addr> and copy to <dest>" |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 268 | #endif |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 269 | ""; |
| 270 | #endif |
| 271 | |
| 272 | U_BOOT_CMD( |
| 273 | imxtract, 4, 1, do_imgextract, |
| 274 | "extract a part of a multi-image", imgextract_help_text |
Marian Balakowicz | 1b7897f | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 275 | ); |