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 | * |
| 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 | 0359dde | 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> |
| 34 | #include <asm/byteorder.h> |
| 35 | |
| 36 | int |
| 37 | do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
| 38 | { |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 39 | ulong addr = load_addr; |
| 40 | ulong dest = 0; |
| 41 | ulong data, len, count; |
Bartlomiej Sieka | 3b8cc71 | 2008-03-20 19:38:45 +0100 | [diff] [blame] | 42 | int verify; |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 43 | int part = 0; |
| 44 | char pbuf[10]; |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 45 | image_header_t *hdr; |
| 46 | #if defined(CONFIG_FIT) |
Bartlomiej Sieka | 3b8cc71 | 2008-03-20 19:38:45 +0100 | [diff] [blame] | 47 | const char *uname = NULL; |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 48 | const void* fit_hdr; |
| 49 | int noffset; |
| 50 | const void *fit_data; |
| 51 | size_t fit_len; |
| 52 | #endif |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 53 | |
Bartlomiej Sieka | 4786859 | 2008-04-18 12:39:23 +0200 | [diff] [blame] | 54 | verify = getenv_yesno ("verify"); |
wdenk | 0359dde | 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 | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 61 | #if defined(CONFIG_FIT) |
| 62 | uname = argv[2]; |
| 63 | #endif |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 64 | } |
| 65 | if (argc > 3) { |
| 66 | dest = simple_strtoul(argv[3], NULL, 16); |
| 67 | } |
| 68 | |
Marian Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 69 | switch (genimg_get_format ((void *)addr)) { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 70 | case IMAGE_FORMAT_LEGACY: |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 71 | |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 72 | printf("## Copying part %d from legacy image " |
| 73 | "at %08lx ...\n", part, addr); |
| 74 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 75 | hdr = (image_header_t *)addr; |
| 76 | if (!image_check_magic (hdr)) { |
| 77 | printf("Bad Magic Number\n"); |
| 78 | return 1; |
| 79 | } |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 80 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 81 | if (!image_check_hcrc (hdr)) { |
| 82 | printf("Bad Header Checksum\n"); |
| 83 | return 1; |
| 84 | } |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 85 | #ifdef DEBUG |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 86 | image_print_contents (hdr); |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 87 | #endif |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 88 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 89 | if (!image_check_type (hdr, IH_TYPE_MULTI)) { |
| 90 | printf("Wrong Image Type for %s command\n", |
| 91 | cmdtp->name); |
| 92 | return 1; |
| 93 | } |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 94 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 95 | if (image_get_comp (hdr) != IH_COMP_NONE) { |
| 96 | printf("Wrong Compression Type for %s command\n", |
| 97 | cmdtp->name); |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 98 | return 1; |
| 99 | } |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 100 | |
| 101 | if (verify) { |
| 102 | printf(" Verifying Checksum ... "); |
| 103 | if (!image_check_dcrc (hdr)) { |
| 104 | printf("Bad Data CRC\n"); |
| 105 | return 1; |
| 106 | } |
| 107 | printf("OK\n"); |
| 108 | } |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 109 | |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 110 | count = image_multi_count (hdr); |
| 111 | if (part >= count) { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 112 | printf("Bad Image Part\n"); |
| 113 | return 1; |
| 114 | } |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 115 | |
| 116 | image_multi_getimg (hdr, part, &data, &len); |
| 117 | break; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 118 | #if defined(CONFIG_FIT) |
| 119 | case IMAGE_FORMAT_FIT: |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 120 | if (uname == NULL) { |
| 121 | puts ("No FIT subimage unit name\n"); |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | printf("## Copying '%s' subimage from FIT image " |
| 126 | "at %08lx ...\n", uname, addr); |
| 127 | |
| 128 | fit_hdr = (const void *)addr; |
| 129 | if (!fit_check_format (fit_hdr)) { |
| 130 | puts ("Bad FIT image format\n"); |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | /* get subimage node offset */ |
Bartlomiej Sieka | 3b8cc71 | 2008-03-20 19:38:45 +0100 | [diff] [blame] | 135 | noffset = fit_image_get_node (fit_hdr, uname); |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 136 | if (noffset < 0) { |
| 137 | printf ("Can't find '%s' FIT subimage\n", uname); |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | if (fit_image_check_comp (fit_hdr, noffset, IH_COMP_NONE)) { |
| 142 | printf("Wrong Compression Type for %s command\n", |
| 143 | cmdtp->name); |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | /* verify integrity */ |
| 148 | if (verify) { |
| 149 | if (!fit_image_check_hashes (fit_hdr, noffset)) { |
| 150 | puts ("Bad Data Hash\n"); |
| 151 | return 1; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* get subimage data address and length */ |
| 156 | if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) { |
| 157 | puts ("Could not find script subimage data\n"); |
| 158 | return 1; |
| 159 | } |
| 160 | |
Bartlomiej Sieka | 3b8cc71 | 2008-03-20 19:38:45 +0100 | [diff] [blame] | 161 | data = (ulong)fit_data; |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 162 | len = (ulong)fit_len; |
| 163 | break; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 164 | #endif |
| 165 | default: |
| 166 | puts ("Invalid image type for imxtract\n"); |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 167 | return 1; |
| 168 | } |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 169 | |
| 170 | if (argc > 3) { |
| 171 | memcpy((char *) dest, (char *) data, len); |
| 172 | } |
| 173 | |
| 174 | sprintf(pbuf, "%8lx", data); |
| 175 | setenv("fileaddr", pbuf); |
| 176 | sprintf(pbuf, "%8lx", len); |
| 177 | setenv("filesize", pbuf); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | U_BOOT_CMD(imxtract, 4, 1, do_imgextract, |
| 183 | "imxtract- extract a part of a multi-image\n", |
| 184 | "addr part [dest]\n" |
Marian Balakowicz | 2321ed0 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 185 | " - extract <part> from legacy image at <addr> and copy to <dest>\n" |
| 186 | #if defined(CONFIG_FIT) |
| 187 | "addr uname [dest]\n" |
| 188 | " - extract <uname> subimage from FIT image at <addr> and copy to <dest>\n" |
| 189 | #endif |
| 190 | ); |