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