]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_ximg.c
Merge branch 'master' of git://git.denx.de/u-boot-i2c
[karo-tx-uboot.git] / common / cmd_ximg.c
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  * SPDX-License-Identifier:     GPL-2.0+
9  */
10
11
12 /*
13  * Multi Image extract
14  */
15 #include <common.h>
16 #include <command.h>
17 #include <image.h>
18 #include <watchdog.h>
19 #if defined(CONFIG_BZIP2)
20 #include <bzlib.h>
21 #endif
22 #include <asm/byteorder.h>
23
24 #ifndef CONFIG_SYS_XIMG_LEN
25 /* use 8MByte as default max gunzip size */
26 #define CONFIG_SYS_XIMG_LEN     0x800000
27 #endif
28
29 static int
30 do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
31 {
32         ulong           addr = load_addr;
33         ulong           dest = 0;
34         ulong           data, len, count;
35         int             verify;
36         int             part = 0;
37         image_header_t  *hdr;
38 #if defined(CONFIG_FIT)
39         const char      *uname = NULL;
40         const void*     fit_hdr;
41         int             noffset;
42         const void      *fit_data;
43         size_t          fit_len;
44 #endif
45 #ifdef CONFIG_GZIP
46         uint            unc_len = CONFIG_SYS_XIMG_LEN;
47 #endif
48         uint8_t         comp;
49
50         verify = getenv_yesno("verify");
51
52         if (argc > 1) {
53                 addr = simple_strtoul(argv[1], NULL, 16);
54         }
55         if (argc > 2) {
56                 part = simple_strtoul(argv[2], NULL, 16);
57 #if defined(CONFIG_FIT)
58                 uname = argv[2];
59 #endif
60         }
61         if (argc > 3) {
62                 dest = simple_strtoul(argv[3], NULL, 16);
63         }
64
65         switch (genimg_get_format((void *)addr)) {
66         case IMAGE_FORMAT_LEGACY:
67
68                 printf("## Copying part %d from legacy image "
69                         "at %08lx ...\n", part, addr);
70
71                 hdr = (image_header_t *)addr;
72                 if (!image_check_magic(hdr)) {
73                         printf("Bad Magic Number\n");
74                         return 1;
75                 }
76
77                 if (!image_check_hcrc(hdr)) {
78                         printf("Bad Header Checksum\n");
79                         return 1;
80                 }
81 #ifdef DEBUG
82                 image_print_contents(hdr);
83 #endif
84
85                 if (!image_check_type(hdr, IH_TYPE_MULTI)) {
86                         printf("Wrong Image Type for %s command\n",
87                                         cmdtp->name);
88                         return 1;
89                 }
90
91                 comp = image_get_comp(hdr);
92                 if ((comp != IH_COMP_NONE) && (argc < 4)) {
93                         printf("Must specify load address for %s command "
94                                         "with compressed image\n",
95                                         cmdtp->name);
96                         return 1;
97                 }
98
99                 if (verify) {
100                         printf("   Verifying Checksum ... ");
101                         if (!image_check_dcrc(hdr)) {
102                                 printf("Bad Data CRC\n");
103                                 return 1;
104                         }
105                         printf("OK\n");
106                 }
107
108                 count = image_multi_count(hdr);
109                 if (part >= count) {
110                         printf("Bad Image Part\n");
111                         return 1;
112                 }
113
114                 image_multi_getimg(hdr, part, &data, &len);
115                 break;
116 #if defined(CONFIG_FIT)
117         case IMAGE_FORMAT_FIT:
118                 if (uname == NULL) {
119                         puts("No FIT subimage unit name\n");
120                         return 1;
121                 }
122
123                 printf("## Copying '%s' subimage from FIT image "
124                         "at %08lx ...\n", uname, addr);
125
126                 fit_hdr = (const void *)addr;
127                 if (!fit_check_format(fit_hdr)) {
128                         puts("Bad FIT image format\n");
129                         return 1;
130                 }
131
132                 /* get subimage node offset */
133                 noffset = fit_image_get_node(fit_hdr, uname);
134                 if (noffset < 0) {
135                         printf("Can't find '%s' FIT subimage\n", uname);
136                         return 1;
137                 }
138
139                 if (fit_image_check_comp(fit_hdr, noffset, IH_COMP_NONE)
140                     && (argc < 4)) {
141                         printf("Must specify load address for %s command "
142                                 "with compressed image\n",
143                                 cmdtp->name);
144                         return 1;
145                 }
146
147                 /* verify integrity */
148                 if (verify) {
149                         if (!fit_image_verify(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,
157                                         &fit_data, &fit_len)) {
158                         puts("Could not find script subimage data\n");
159                         return 1;
160                 }
161
162                 if (fit_image_get_comp(fit_hdr, noffset, &comp)) {
163                         puts("Could not find script subimage "
164                                 "compression type\n");
165                         return 1;
166                 }
167
168                 data = (ulong)fit_data;
169                 len = (ulong)fit_len;
170                 break;
171 #endif
172         default:
173                 puts("Invalid image type for imxtract\n");
174                 return 1;
175         }
176
177         if (argc > 3) {
178                 switch (comp) {
179                 case IH_COMP_NONE:
180 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
181                         {
182                                 size_t l = len;
183                                 size_t tail;
184                                 void *to = (void *) dest;
185                                 void *from = (void *)data;
186
187                                 printf("   Loading part %d ... ", part);
188
189                                 while (l > 0) {
190                                         tail = (l > CHUNKSZ) ? CHUNKSZ : l;
191                                         WATCHDOG_RESET();
192                                         memmove(to, from, tail);
193                                         to += tail;
194                                         from += tail;
195                                         l -= tail;
196                                 }
197                         }
198 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
199                         printf("   Loading part %d ... ", part);
200                         memmove((char *) dest, (char *)data, len);
201 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
202                         break;
203 #ifdef CONFIG_GZIP
204                 case IH_COMP_GZIP:
205                         printf("   Uncompressing part %d ... ", part);
206                         if (gunzip((void *) dest, unc_len,
207                                    (uchar *) data, &len) != 0) {
208                                 puts("GUNZIP ERROR - image not loaded\n");
209                                 return 1;
210                         }
211                         break;
212 #endif
213 #if defined(CONFIG_BZIP2)
214                 case IH_COMP_BZIP2:
215                         {
216                                 int i;
217
218                                 printf("   Uncompressing part %d ... ", part);
219                                 /*
220                                  * If we've got less than 4 MB of malloc()
221                                  * space, use slower decompression algorithm
222                                  * which requires at most 2300 KB of memory.
223                                  */
224                                 i = BZ2_bzBuffToBuffDecompress(
225                                         (char *)ntohl(hdr->ih_load),
226                                         &unc_len, (char *)data, len,
227                                         CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
228                                         0);
229                                 if (i != BZ_OK) {
230                                         printf("BUNZIP2 ERROR %d - "
231                                                 "image not loaded\n", i);
232                                         return 1;
233                                 }
234                         }
235                         break;
236 #endif /* CONFIG_BZIP2 */
237                 default:
238                         printf("Unimplemented compression type %d\n", comp);
239                         return 1;
240                 }
241                 puts("OK\n");
242         }
243
244         setenv_hex("fileaddr", data);
245         setenv_hex("filesize", len);
246
247         return 0;
248 }
249
250 #ifdef CONFIG_SYS_LONGHELP
251 static char imgextract_help_text[] =
252         "addr part [dest]\n"
253         "    - extract <part> from legacy image at <addr> and copy to <dest>"
254 #if defined(CONFIG_FIT)
255         "\n"
256         "addr uname [dest]\n"
257         "    - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
258 #endif
259         "";
260 #endif
261
262 U_BOOT_CMD(
263         imxtract, 4, 1, do_imgextract,
264         "extract a part of a multi-image", imgextract_help_text
265 );