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