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