blob: 8359153b2b56d78d19cc8649495294696d0f90cd [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
27#if (CONFIG_COMMANDS & CFG_CMD_XIMG)
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
37int
38do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
39{
40 ulong addr = load_addr, dest = 0;
41 ulong data, len, checksum;
42 ulong *len_ptr;
43 int i, verify, part = 0;
44 char pbuf[10], *s;
45 image_header_t header;
46
47 s = getenv("verify");
48 verify = (s && (*s == 'n')) ? 0 : 1;
49
50 if (argc > 1) {
51 addr = simple_strtoul(argv[1], NULL, 16);
52 }
53 if (argc > 2) {
54 part = simple_strtoul(argv[2], NULL, 16);
55 }
56 if (argc > 3) {
57 dest = simple_strtoul(argv[3], NULL, 16);
58 }
59
60 printf("## Copying from image at %08lx ...\n", addr);
61
62 /* Copy header so we can blank CRC field for re-calculation */
63 memmove(&header, (char *) addr, sizeof (image_header_t));
64
65 if (ntohl(header.ih_magic) != IH_MAGIC) {
66 printf("Bad Magic Number\n");
67 return 1;
68 }
69
70 data = (ulong) & header;
71 len = sizeof (image_header_t);
72
73 checksum = ntohl(header.ih_hcrc);
74 header.ih_hcrc = 0;
75
76 if (crc32(0, (char *) data, len) != checksum) {
77 printf("Bad Header Checksum\n");
78 return 1;
79 }
80#ifdef DEBUG
81 print_image_hdr((image_header_t *) addr);
82#endif
83
84 data = addr + sizeof (image_header_t);
85 len = ntohl(header.ih_size);
86
87 if (header.ih_type != IH_TYPE_MULTI) {
88 printf("Wrong Image Type for %s command\n", cmdtp->name);
89 return 1;
90 }
91
92 if (header.ih_comp != IH_COMP_NONE) {
93 printf("Wrong Compression Type for %s command\n", cmdtp->name);
94 return 1;
95 }
96
97 if (verify) {
98 printf(" Verifying Checksum ... ");
99 if (crc32(0, (char *) data, len) != ntohl(header.ih_dcrc)) {
100 printf("Bad Data CRC\n");
101 return 1;
102 }
103 printf("OK\n");
104 }
105
106 len_ptr = (ulong *) data;
107
108 data += 4; /* terminator */
109 for (i = 0; len_ptr[i]; ++i) {
110 data += 4;
111 if (argc > 2 && part > i) {
112 u_long tail;
113 len = ntohl(len_ptr[i]);
114 tail = len % 4;
115 data += len;
116 if (tail) {
117 data += 4 - tail;
118 }
119 }
120 }
121 if (argc > 2 && part >= i) {
122 printf("Bad Image Part\n");
123 return 1;
124 }
125 len = ntohl(len_ptr[part]);
126
127 if (argc > 3) {
128 memcpy((char *) dest, (char *) data, len);
129 }
130
131 sprintf(pbuf, "%8lx", data);
132 setenv("fileaddr", pbuf);
133 sprintf(pbuf, "%8lx", len);
134 setenv("filesize", pbuf);
135
136 return 0;
137}
138
139U_BOOT_CMD(imxtract, 4, 1, do_imgextract,
140 "imxtract- extract a part of a multi-image\n",
141 "addr part [dest]\n"
142 " - extract <part> from image at <addr> and copy to <dest>\n");
143
144#endif /* CONFIG_COMMANDS & CFG_CMD_XIMG */