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 | |
Jon Loeliger | 4c17634 | 2007-07-08 18:05:39 -0500 | [diff] [blame] | 27 | #if defined(CONFIG_CMD_XIMG) |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Multi Image extract |
| 31 | */ |
| 32 | #include <common.h> |
| 33 | #include <command.h> |
| 34 | #include <image.h> |
| 35 | #include <asm/byteorder.h> |
| 36 | |
| 37 | int |
| 38 | do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
| 39 | { |
| 40 | ulong addr = load_addr, dest = 0; |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 41 | ulong data, len; |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 42 | ulong *len_ptr; |
| 43 | int i, verify, part = 0; |
| 44 | char pbuf[10], *s; |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 45 | image_header_t *hdr; |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 46 | |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 47 | verify = getenv_verify (); |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 48 | |
| 49 | if (argc > 1) { |
| 50 | addr = simple_strtoul(argv[1], NULL, 16); |
| 51 | } |
| 52 | if (argc > 2) { |
| 53 | part = simple_strtoul(argv[2], NULL, 16); |
| 54 | } |
| 55 | if (argc > 3) { |
| 56 | dest = simple_strtoul(argv[3], NULL, 16); |
| 57 | } |
| 58 | |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 59 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame^] | 60 | switch (gen_image_get_format ((void *)addr)) { |
| 61 | case IMAGE_FORMAT_LEGACY: |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 62 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame^] | 63 | printf("## Copying from legacy image at %08lx ...\n", addr); |
| 64 | hdr = (image_header_t *)addr; |
| 65 | if (!image_check_magic (hdr)) { |
| 66 | printf("Bad Magic Number\n"); |
| 67 | return 1; |
| 68 | } |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 69 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame^] | 70 | if (!image_check_hcrc (hdr)) { |
| 71 | printf("Bad Header Checksum\n"); |
| 72 | return 1; |
| 73 | } |
| 74 | #ifdef DEBUG |
| 75 | image_print_contents (hdr); |
| 76 | #endif |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 77 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame^] | 78 | if (!image_check_type (hdr, IH_TYPE_MULTI)) { |
| 79 | printf("Wrong Image Type for %s command\n", |
| 80 | cmdtp->name); |
| 81 | return 1; |
| 82 | } |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 83 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame^] | 84 | if (image_get_comp (hdr) != IH_COMP_NONE) { |
| 85 | printf("Wrong Compression Type for %s command\n", |
| 86 | cmdtp->name); |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 87 | return 1; |
| 88 | } |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame^] | 89 | |
| 90 | if (verify) { |
| 91 | printf(" Verifying Checksum ... "); |
| 92 | if (!image_check_dcrc (hdr)) { |
| 93 | printf("Bad Data CRC\n"); |
| 94 | return 1; |
| 95 | } |
| 96 | printf("OK\n"); |
| 97 | } |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 98 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame^] | 99 | data = image_get_data (hdr); |
| 100 | len_ptr = (ulong *) data; |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 101 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame^] | 102 | data += 4; /* terminator */ |
| 103 | for (i = 0; len_ptr[i]; ++i) { |
| 104 | data += 4; |
| 105 | if (argc > 2 && part > i) { |
| 106 | u_long tail; |
| 107 | len = image_to_cpu (len_ptr[i]); |
| 108 | tail = len % 4; |
| 109 | data += len; |
| 110 | if (tail) { |
| 111 | data += 4 - tail; |
| 112 | } |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame^] | 115 | if (argc > 2 && part >= i) { |
| 116 | printf("Bad Image Part\n"); |
| 117 | return 1; |
| 118 | } |
| 119 | len = image_to_cpu (len_ptr[part]); |
| 120 | #if defined(CONFIG_FIT) |
| 121 | case IMAGE_FORMAT_FIT: |
| 122 | fit_unsupported ("imxtract"); |
| 123 | return 1; |
| 124 | #endif |
| 125 | default: |
| 126 | puts ("Invalid image type for imxtract\n"); |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 127 | return 1; |
| 128 | } |
wdenk | 0359dde | 2004-06-09 10:15:00 +0000 | [diff] [blame] | 129 | |
| 130 | if (argc > 3) { |
| 131 | memcpy((char *) dest, (char *) data, len); |
| 132 | } |
| 133 | |
| 134 | sprintf(pbuf, "%8lx", data); |
| 135 | setenv("fileaddr", pbuf); |
| 136 | sprintf(pbuf, "%8lx", len); |
| 137 | setenv("filesize", pbuf); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | U_BOOT_CMD(imxtract, 4, 1, do_imgextract, |
| 143 | "imxtract- extract a part of a multi-image\n", |
| 144 | "addr part [dest]\n" |
| 145 | " - extract <part> from image at <addr> and copy to <dest>\n"); |
| 146 | |
Jon Loeliger | 4c17634 | 2007-07-08 18:05:39 -0500 | [diff] [blame] | 147 | #endif |