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