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