blob: ab579cd2c831ce35353087644caceb9945881824 [file] [log] [blame]
wdenk0359dde2004-06-09 10:15:00 +00001/*
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 Loeliger4c176342007-07-08 18:05:39 -050027#if defined(CONFIG_CMD_XIMG)
wdenk0359dde2004-06-09 10:15:00 +000028
29/*
30 * Multi Image extract
31 */
32#include <common.h>
33#include <command.h>
34#include <image.h>
35#include <asm/byteorder.h>
36
37int
38do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
39{
40 ulong addr = load_addr, dest = 0;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010041 ulong data, len;
wdenk0359dde2004-06-09 10:15:00 +000042 ulong *len_ptr;
43 int i, verify, part = 0;
44 char pbuf[10], *s;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010045 image_header_t *hdr;
wdenk0359dde2004-06-09 10:15:00 +000046
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010047 verify = getenv_verify ();
wdenk0359dde2004-06-09 10:15:00 +000048
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
59 printf("## Copying from image at %08lx ...\n", addr);
60
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010061 hdr = (image_header_t *)addr;
wdenk0359dde2004-06-09 10:15:00 +000062
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010063 if (!image_check_magic (hdr)) {
wdenk0359dde2004-06-09 10:15:00 +000064 printf("Bad Magic Number\n");
65 return 1;
66 }
67
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010068 if (!image_check_hcrc (hdr)) {
wdenk0359dde2004-06-09 10:15:00 +000069 printf("Bad Header Checksum\n");
70 return 1;
71 }
72#ifdef DEBUG
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010073 print_image_hdr (hdr);
wdenk0359dde2004-06-09 10:15:00 +000074#endif
75
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010076 if (!image_check_type (hdr, IH_TYPE_MULTI)) {
wdenk0359dde2004-06-09 10:15:00 +000077 printf("Wrong Image Type for %s command\n", cmdtp->name);
78 return 1;
79 }
80
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010081 if (image_get_comp (hdr) != IH_COMP_NONE) {
wdenk0359dde2004-06-09 10:15:00 +000082 printf("Wrong Compression Type for %s command\n", cmdtp->name);
83 return 1;
84 }
85
86 if (verify) {
87 printf(" Verifying Checksum ... ");
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010088 if (!image_check_dcrc (hdr)) {
wdenk0359dde2004-06-09 10:15:00 +000089 printf("Bad Data CRC\n");
90 return 1;
91 }
92 printf("OK\n");
93 }
94
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010095 data = image_get_data (hdr);
wdenk0359dde2004-06-09 10:15:00 +000096 len_ptr = (ulong *) data;
97
98 data += 4; /* terminator */
99 for (i = 0; len_ptr[i]; ++i) {
100 data += 4;
101 if (argc > 2 && part > i) {
102 u_long tail;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100103 len = image_to_cpu (len_ptr[i]);
wdenk0359dde2004-06-09 10:15:00 +0000104 tail = len % 4;
105 data += len;
106 if (tail) {
107 data += 4 - tail;
108 }
109 }
110 }
111 if (argc > 2 && part >= i) {
112 printf("Bad Image Part\n");
113 return 1;
114 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100115 len = image_to_cpu (len_ptr[part]);
wdenk0359dde2004-06-09 10:15:00 +0000116
117 if (argc > 3) {
118 memcpy((char *) dest, (char *) data, len);
119 }
120
121 sprintf(pbuf, "%8lx", data);
122 setenv("fileaddr", pbuf);
123 sprintf(pbuf, "%8lx", len);
124 setenv("filesize", pbuf);
125
126 return 0;
127}
128
129U_BOOT_CMD(imxtract, 4, 1, do_imgextract,
130 "imxtract- extract a part of a multi-image\n",
131 "addr part [dest]\n"
132 " - extract <part> from image at <addr> and copy to <dest>\n");
133
Jon Loeliger4c176342007-07-08 18:05:39 -0500134#endif