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