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