]>
Commit | Line | Data |
---|---|---|
b97a2a0a MB |
1 | /* |
2 | * (C) Copyright 2008 Semihalf | |
3 | * | |
4 | * (C) Copyright 2000-2006 | |
5 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
6 | * | |
7 | * See file CREDITS for list of people who contributed to this | |
8 | * project. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License as | |
12 | * published by the Free Software Foundation; either version 2 of | |
13 | * the License, or (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
23 | * MA 02111-1307 USA | |
24 | */ | |
ceaed2b1 | 25 | |
b97a2a0a | 26 | #ifndef USE_HOSTCC |
5ad03eb3 MB |
27 | #include <common.h> |
28 | #include <watchdog.h> | |
29 | ||
30 | #ifdef CONFIG_SHOW_BOOT_PROGRESS | |
31 | #include <status_led.h> | |
32 | #endif | |
33 | ||
34 | #ifdef CONFIG_HAS_DATAFLASH | |
35 | #include <dataflash.h> | |
36 | #endif | |
37 | ||
95d449ad MB |
38 | #ifdef CONFIG_LOGBUFFER |
39 | #include <logbuff.h> | |
40 | #endif | |
41 | ||
2242f536 MB |
42 | #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) |
43 | #include <rtc.h> | |
44 | #endif | |
45 | ||
5dfb5213 MB |
46 | #include <image.h> |
47 | ||
c8779648 | 48 | #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT) |
fff888a1 MB |
49 | #include <fdt.h> |
50 | #include <libfdt.h> | |
51 | #include <fdt_support.h> | |
c8779648 MB |
52 | #endif |
53 | ||
54 | #if defined(CONFIG_FIT) | |
20a14a42 | 55 | #include <u-boot/md5.h> |
5dfb5213 | 56 | #include <sha1.h> |
c8779648 MB |
57 | |
58 | static int fit_check_ramdisk (const void *fit, int os_noffset, | |
59 | uint8_t arch, int verify); | |
fff888a1 MB |
60 | #endif |
61 | ||
b6b0fe64 MB |
62 | #ifdef CONFIG_CMD_BDI |
63 | extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); | |
64 | #endif | |
65 | ||
66 | DECLARE_GLOBAL_DATA_PTR; | |
8a5ea3e6 | 67 | |
d985c849 MB |
68 | static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch, |
69 | int verify); | |
b97a2a0a | 70 | #else |
5ad03eb3 | 71 | #include "mkimage.h" |
20a14a42 | 72 | #include <u-boot/md5.h> |
5dfb5213 | 73 | #include <time.h> |
b97a2a0a | 74 | #include <image.h> |
5dfb5213 | 75 | #endif /* !USE_HOSTCC*/ |
b97a2a0a | 76 | |
570abb0a MB |
77 | typedef struct table_entry { |
78 | int id; /* as defined in image.h */ | |
79 | char *sname; /* short (input) name */ | |
80 | char *lname; /* long (output) name */ | |
81 | } table_entry_t; | |
82 | ||
83 | static table_entry_t uimage_arch[] = { | |
84 | { IH_ARCH_INVALID, NULL, "Invalid ARCH", }, | |
85 | { IH_ARCH_ALPHA, "alpha", "Alpha", }, | |
86 | { IH_ARCH_ARM, "arm", "ARM", }, | |
87 | { IH_ARCH_I386, "x86", "Intel x86", }, | |
88 | { IH_ARCH_IA64, "ia64", "IA64", }, | |
89 | { IH_ARCH_M68K, "m68k", "M68K", }, | |
90 | { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", }, | |
91 | { IH_ARCH_MIPS, "mips", "MIPS", }, | |
92 | { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", }, | |
93 | { IH_ARCH_NIOS, "nios", "NIOS", }, | |
94 | { IH_ARCH_NIOS2, "nios2", "NIOS II", }, | |
e419e12d | 95 | { IH_ARCH_PPC, "powerpc", "PowerPC", }, |
570abb0a MB |
96 | { IH_ARCH_PPC, "ppc", "PowerPC", }, |
97 | { IH_ARCH_S390, "s390", "IBM S390", }, | |
98 | { IH_ARCH_SH, "sh", "SuperH", }, | |
99 | { IH_ARCH_SPARC, "sparc", "SPARC", }, | |
100 | { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", }, | |
101 | { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", }, | |
102 | { IH_ARCH_AVR32, "avr32", "AVR32", }, | |
103 | { -1, "", "", }, | |
104 | }; | |
105 | ||
106 | static table_entry_t uimage_os[] = { | |
107 | { IH_OS_INVALID, NULL, "Invalid OS", }, | |
108 | #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC) | |
109 | { IH_OS_ARTOS, "artos", "ARTOS", }, | |
110 | #endif | |
111 | { IH_OS_LINUX, "linux", "Linux", }, | |
112 | #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC) | |
113 | { IH_OS_LYNXOS, "lynxos", "LynxOS", }, | |
114 | #endif | |
115 | { IH_OS_NETBSD, "netbsd", "NetBSD", }, | |
116 | { IH_OS_RTEMS, "rtems", "RTEMS", }, | |
117 | { IH_OS_U_BOOT, "u-boot", "U-Boot", }, | |
118 | #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC) | |
119 | { IH_OS_QNX, "qnx", "QNX", }, | |
120 | { IH_OS_VXWORKS, "vxworks", "VxWorks", }, | |
121 | #endif | |
122 | #ifdef USE_HOSTCC | |
123 | { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", }, | |
124 | { IH_OS_DELL, "dell", "Dell", }, | |
125 | { IH_OS_ESIX, "esix", "Esix", }, | |
126 | { IH_OS_FREEBSD, "freebsd", "FreeBSD", }, | |
127 | { IH_OS_IRIX, "irix", "Irix", }, | |
128 | { IH_OS_NCR, "ncr", "NCR", }, | |
129 | { IH_OS_OPENBSD, "openbsd", "OpenBSD", }, | |
130 | { IH_OS_PSOS, "psos", "pSOS", }, | |
131 | { IH_OS_SCO, "sco", "SCO", }, | |
132 | { IH_OS_SOLARIS, "solaris", "Solaris", }, | |
133 | { IH_OS_SVR4, "svr4", "SVR4", }, | |
134 | #endif | |
135 | { -1, "", "", }, | |
136 | }; | |
137 | ||
138 | static table_entry_t uimage_type[] = { | |
139 | { IH_TYPE_INVALID, NULL, "Invalid Image", }, | |
140 | { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", }, | |
141 | { IH_TYPE_FIRMWARE, "firmware", "Firmware", }, | |
142 | { IH_TYPE_KERNEL, "kernel", "Kernel Image", }, | |
143 | { IH_TYPE_MULTI, "multi", "Multi-File Image", }, | |
144 | { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", }, | |
145 | { IH_TYPE_SCRIPT, "script", "Script", }, | |
146 | { IH_TYPE_STANDALONE, "standalone", "Standalone Program", }, | |
147 | { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", }, | |
148 | { -1, "", "", }, | |
149 | }; | |
150 | ||
151 | static table_entry_t uimage_comp[] = { | |
152 | { IH_COMP_NONE, "none", "uncompressed", }, | |
153 | { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", }, | |
154 | { IH_COMP_GZIP, "gzip", "gzip compressed", }, | |
155 | { -1, "", "", }, | |
156 | }; | |
157 | ||
89cdab78 | 158 | uint32_t crc32 (uint32_t, const unsigned char *, uint); |
7590378f | 159 | uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint); |
570abb0a MB |
160 | static void genimg_print_size (uint32_t size); |
161 | #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) | |
162 | static void genimg_print_time (time_t timestamp); | |
163 | #endif | |
b97a2a0a | 164 | |
9a4daad0 MB |
165 | /*****************************************************************************/ |
166 | /* Legacy format routines */ | |
167 | /*****************************************************************************/ | |
b97a2a0a MB |
168 | int image_check_hcrc (image_header_t *hdr) |
169 | { | |
170 | ulong hcrc; | |
171 | ulong len = image_get_header_size (); | |
172 | image_header_t header; | |
173 | ||
174 | /* Copy header so we can blank CRC field for re-calculation */ | |
175 | memmove (&header, (char *)hdr, image_get_header_size ()); | |
176 | image_set_hcrc (&header, 0); | |
177 | ||
178 | hcrc = crc32 (0, (unsigned char *)&header, len); | |
179 | ||
180 | return (hcrc == image_get_hcrc (hdr)); | |
181 | } | |
182 | ||
183 | int image_check_dcrc (image_header_t *hdr) | |
184 | { | |
185 | ulong data = image_get_data (hdr); | |
186 | ulong len = image_get_data_size (hdr); | |
7590378f | 187 | ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32); |
b97a2a0a MB |
188 | |
189 | return (dcrc == image_get_dcrc (hdr)); | |
190 | } | |
191 | ||
f13e7b2e MB |
192 | /** |
193 | * image_multi_count - get component (sub-image) count | |
194 | * @hdr: pointer to the header of the multi component image | |
195 | * | |
196 | * image_multi_count() returns number of components in a multi | |
197 | * component image. | |
198 | * | |
199 | * Note: no checking of the image type is done, caller must pass | |
200 | * a valid multi component image. | |
201 | * | |
202 | * returns: | |
203 | * number of components | |
204 | */ | |
205 | ulong image_multi_count (image_header_t *hdr) | |
206 | { | |
207 | ulong i, count = 0; | |
df6f1b89 | 208 | uint32_t *size; |
f13e7b2e MB |
209 | |
210 | /* get start of the image payload, which in case of multi | |
211 | * component images that points to a table of component sizes */ | |
df6f1b89 | 212 | size = (uint32_t *)image_get_data (hdr); |
f13e7b2e MB |
213 | |
214 | /* count non empty slots */ | |
215 | for (i = 0; size[i]; ++i) | |
216 | count++; | |
217 | ||
218 | return count; | |
219 | } | |
220 | ||
221 | /** | |
222 | * image_multi_getimg - get component data address and size | |
223 | * @hdr: pointer to the header of the multi component image | |
224 | * @idx: index of the requested component | |
225 | * @data: pointer to a ulong variable, will hold component data address | |
226 | * @len: pointer to a ulong variable, will hold component size | |
227 | * | |
228 | * image_multi_getimg() returns size and data address for the requested | |
229 | * component in a multi component image. | |
230 | * | |
231 | * Note: no checking of the image type is done, caller must pass | |
232 | * a valid multi component image. | |
233 | * | |
234 | * returns: | |
235 | * data address and size of the component, if idx is valid | |
236 | * 0 in data and len, if idx is out of range | |
237 | */ | |
238 | void image_multi_getimg (image_header_t *hdr, ulong idx, | |
239 | ulong *data, ulong *len) | |
240 | { | |
241 | int i; | |
df6f1b89 | 242 | uint32_t *size; |
02b9b224 | 243 | ulong offset, count, img_data; |
f13e7b2e MB |
244 | |
245 | /* get number of component */ | |
246 | count = image_multi_count (hdr); | |
247 | ||
248 | /* get start of the image payload, which in case of multi | |
249 | * component images that points to a table of component sizes */ | |
df6f1b89 | 250 | size = (uint32_t *)image_get_data (hdr); |
f13e7b2e MB |
251 | |
252 | /* get address of the proper component data start, which means | |
253 | * skipping sizes table (add 1 for last, null entry) */ | |
df6f1b89 | 254 | img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t); |
f13e7b2e MB |
255 | |
256 | if (idx < count) { | |
df6f1b89 | 257 | *len = uimage_to_cpu (size[idx]); |
f13e7b2e | 258 | offset = 0; |
f13e7b2e MB |
259 | |
260 | /* go over all indices preceding requested component idx */ | |
261 | for (i = 0; i < idx; i++) { | |
02b9b224 NS |
262 | /* add up i-th component size, rounding up to 4 bytes */ |
263 | offset += (uimage_to_cpu (size[i]) + 3) & ~3 ; | |
f13e7b2e MB |
264 | } |
265 | ||
266 | /* calculate idx-th component data address */ | |
02b9b224 | 267 | *data = img_data + offset; |
f13e7b2e MB |
268 | } else { |
269 | *len = 0; | |
270 | *data = 0; | |
271 | } | |
272 | } | |
42b73e8e | 273 | |
9a4daad0 MB |
274 | static void image_print_type (image_header_t *hdr) |
275 | { | |
276 | const char *os, *arch, *type, *comp; | |
277 | ||
278 | os = genimg_get_os_name (image_get_os (hdr)); | |
279 | arch = genimg_get_arch_name (image_get_arch (hdr)); | |
280 | type = genimg_get_type_name (image_get_type (hdr)); | |
281 | comp = genimg_get_comp_name (image_get_comp (hdr)); | |
282 | ||
570abb0a | 283 | printf ("%s %s %s (%s)\n", arch, os, type, comp); |
9a4daad0 MB |
284 | } |
285 | ||
5dfb5213 | 286 | /** |
edbed247 | 287 | * image_print_contents - prints out the contents of the legacy format image |
5dfb5213 MB |
288 | * @hdr: pointer to the legacy format image header |
289 | * @p: pointer to prefix string | |
290 | * | |
edbed247 | 291 | * image_print_contents() formats a multi line legacy image contents description. |
5dfb5213 MB |
292 | * The routine prints out all header fields followed by the size/offset data |
293 | * for MULTI/SCRIPT images. | |
294 | * | |
295 | * returns: | |
296 | * no returned results | |
297 | */ | |
edbed247 | 298 | void image_print_contents (image_header_t *hdr) |
9a4daad0 | 299 | { |
edbed247 BS |
300 | const char *p; |
301 | ||
302 | #ifdef USE_HOSTCC | |
303 | p = ""; | |
304 | #else | |
305 | p = " "; | |
306 | #endif | |
307 | ||
570abb0a MB |
308 | printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr)); |
309 | #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) | |
310 | printf ("%sCreated: ", p); | |
311 | genimg_print_time ((time_t)image_get_time (hdr)); | |
9a4daad0 | 312 | #endif |
570abb0a | 313 | printf ("%sImage Type: ", p); |
9a4daad0 | 314 | image_print_type (hdr); |
570abb0a MB |
315 | printf ("%sData Size: ", p); |
316 | genimg_print_size (image_get_data_size (hdr)); | |
317 | printf ("%sLoad Address: %08x\n", p, image_get_load (hdr)); | |
318 | printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr)); | |
9a4daad0 | 319 | |
570abb0a MB |
320 | if (image_check_type (hdr, IH_TYPE_MULTI) || |
321 | image_check_type (hdr, IH_TYPE_SCRIPT)) { | |
9a4daad0 MB |
322 | int i; |
323 | ulong data, len; | |
324 | ulong count = image_multi_count (hdr); | |
325 | ||
570abb0a | 326 | printf ("%sContents:\n", p); |
9a4daad0 MB |
327 | for (i = 0; i < count; i++) { |
328 | image_multi_getimg (hdr, i, &data, &len); | |
570abb0a MB |
329 | |
330 | printf ("%s Image %d: ", p, i); | |
331 | genimg_print_size (len); | |
332 | ||
333 | if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) { | |
334 | /* | |
335 | * the user may need to know offsets | |
336 | * if planning to do something with | |
337 | * multiple files | |
338 | */ | |
339 | printf ("%s Offset = 0x%08lx\n", p, data); | |
340 | } | |
9a4daad0 MB |
341 | } |
342 | } | |
343 | } | |
344 | ||
570abb0a MB |
345 | |
346 | #ifndef USE_HOSTCC | |
9a4daad0 MB |
347 | /** |
348 | * image_get_ramdisk - get and verify ramdisk image | |
9a4daad0 MB |
349 | * @rd_addr: ramdisk image start address |
350 | * @arch: expected ramdisk architecture | |
351 | * @verify: checksum verification flag | |
352 | * | |
353 | * image_get_ramdisk() returns a pointer to the verified ramdisk image | |
354 | * header. Routine receives image start address and expected architecture | |
355 | * flag. Verification done covers data and header integrity and os/type/arch | |
356 | * fields checking. | |
357 | * | |
358 | * If dataflash support is enabled routine checks for dataflash addresses | |
359 | * and handles required dataflash reads. | |
360 | * | |
361 | * returns: | |
362 | * pointer to a ramdisk image header, if image was found and valid | |
363 | * otherwise, return NULL | |
364 | */ | |
d985c849 MB |
365 | static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch, |
366 | int verify) | |
9a4daad0 | 367 | { |
1372cce2 | 368 | image_header_t *rd_hdr = (image_header_t *)rd_addr; |
9a4daad0 MB |
369 | |
370 | if (!image_check_magic (rd_hdr)) { | |
371 | puts ("Bad Magic Number\n"); | |
372 | show_boot_progress (-10); | |
373 | return NULL; | |
374 | } | |
375 | ||
376 | if (!image_check_hcrc (rd_hdr)) { | |
377 | puts ("Bad Header Checksum\n"); | |
378 | show_boot_progress (-11); | |
379 | return NULL; | |
380 | } | |
381 | ||
382 | show_boot_progress (10); | |
383 | image_print_contents (rd_hdr); | |
384 | ||
385 | if (verify) { | |
386 | puts(" Verifying Checksum ... "); | |
7590378f | 387 | if (!image_check_dcrc (rd_hdr)) { |
9a4daad0 MB |
388 | puts ("Bad Data CRC\n"); |
389 | show_boot_progress (-12); | |
390 | return NULL; | |
391 | } | |
392 | puts("OK\n"); | |
393 | } | |
394 | ||
395 | show_boot_progress (11); | |
396 | ||
397 | if (!image_check_os (rd_hdr, IH_OS_LINUX) || | |
398 | !image_check_arch (rd_hdr, arch) || | |
399 | !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) { | |
400 | printf ("No Linux %s Ramdisk Image\n", | |
401 | genimg_get_arch_name(arch)); | |
402 | show_boot_progress (-13); | |
403 | return NULL; | |
404 | } | |
405 | ||
406 | return rd_hdr; | |
407 | } | |
570abb0a | 408 | #endif /* !USE_HOSTCC */ |
9a4daad0 MB |
409 | |
410 | /*****************************************************************************/ | |
411 | /* Shared dual-format routines */ | |
412 | /*****************************************************************************/ | |
570abb0a | 413 | #ifndef USE_HOSTCC |
edbed247 | 414 | int getenv_yesno (char *var) |
9a4daad0 | 415 | { |
edbed247 | 416 | char *s = getenv (var); |
9a4daad0 MB |
417 | return (s && (*s == 'n')) ? 0 : 1; |
418 | } | |
419 | ||
420 | ulong getenv_bootm_low(void) | |
421 | { | |
422 | char *s = getenv ("bootm_low"); | |
423 | if (s) { | |
424 | ulong tmp = simple_strtoul (s, NULL, 16); | |
425 | return tmp; | |
426 | } | |
427 | ||
afe45c87 | 428 | #if defined(CFG_SDRAM_BASE) |
9a4daad0 | 429 | return CFG_SDRAM_BASE; |
afe45c87 MB |
430 | #elif defined(CONFIG_ARM) |
431 | return gd->bd->bi_dram[0].start; | |
9a4daad0 MB |
432 | #else |
433 | return 0; | |
434 | #endif | |
435 | } | |
436 | ||
391fd93a | 437 | phys_size_t getenv_bootm_size(void) |
9a4daad0 MB |
438 | { |
439 | char *s = getenv ("bootm_size"); | |
440 | if (s) { | |
391fd93a BB |
441 | phys_size_t tmp; |
442 | #ifdef CFG_64BIT_STRTOUL | |
443 | tmp = (phys_size_t)simple_strtoull (s, NULL, 16); | |
444 | #else | |
445 | tmp = (phys_size_t)simple_strtoul (s, NULL, 16); | |
446 | #endif | |
9a4daad0 MB |
447 | return tmp; |
448 | } | |
449 | ||
afe45c87 MB |
450 | #if defined(CONFIG_ARM) |
451 | return gd->bd->bi_dram[0].size; | |
452 | #else | |
9a4daad0 | 453 | return gd->bd->bi_memsize; |
afe45c87 | 454 | #endif |
9a4daad0 MB |
455 | } |
456 | ||
457 | void memmove_wd (void *to, void *from, size_t len, ulong chunksz) | |
458 | { | |
459 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
460 | while (len > 0) { | |
461 | size_t tail = (len > chunksz) ? chunksz : len; | |
462 | WATCHDOG_RESET (); | |
463 | memmove (to, from, tail); | |
464 | to += tail; | |
465 | from += tail; | |
466 | len -= tail; | |
467 | } | |
468 | #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */ | |
469 | memmove (to, from, len); | |
470 | #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ | |
471 | } | |
570abb0a | 472 | #endif /* !USE_HOSTCC */ |
9a4daad0 | 473 | |
570abb0a | 474 | static void genimg_print_size (uint32_t size) |
42b73e8e | 475 | { |
570abb0a MB |
476 | #ifndef USE_HOSTCC |
477 | printf ("%d Bytes = ", size); | |
478 | print_size (size, "\n"); | |
479 | #else | |
480 | printf ("%d Bytes = %.2f kB = %.2f MB\n", | |
481 | size, (double)size / 1.024e3, | |
482 | (double)size / 1.048576e6); | |
42b73e8e | 483 | #endif |
570abb0a MB |
484 | } |
485 | ||
486 | #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) | |
487 | static void genimg_print_time (time_t timestamp) | |
488 | { | |
489 | #ifndef USE_HOSTCC | |
490 | struct rtc_time tm; | |
491 | ||
492 | to_tm (timestamp, &tm); | |
493 | printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n", | |
494 | tm.tm_year, tm.tm_mon, tm.tm_mday, | |
495 | tm.tm_hour, tm.tm_min, tm.tm_sec); | |
496 | #else | |
497 | printf ("%s", ctime(×tamp)); | |
42b73e8e | 498 | #endif |
570abb0a MB |
499 | } |
500 | #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */ | |
42b73e8e | 501 | |
570abb0a MB |
502 | /** |
503 | * get_table_entry_name - translate entry id to long name | |
504 | * @table: pointer to a translation table for entries of a specific type | |
505 | * @msg: message to be returned when translation fails | |
506 | * @id: entry id to be translated | |
507 | * | |
508 | * get_table_entry_name() will go over translation table trying to find | |
509 | * entry that matches given id. If matching entry is found, its long | |
510 | * name is returned to the caller. | |
511 | * | |
512 | * returns: | |
513 | * long entry name if translation succeeds | |
514 | * msg otherwise | |
515 | */ | |
516 | static char *get_table_entry_name (table_entry_t *table, char *msg, int id) | |
517 | { | |
518 | for (; table->id >= 0; ++table) { | |
519 | if (table->id == id) | |
520 | return (table->lname); | |
42b73e8e | 521 | } |
570abb0a MB |
522 | return (msg); |
523 | } | |
42b73e8e | 524 | |
570abb0a MB |
525 | const char *genimg_get_os_name (uint8_t os) |
526 | { | |
527 | return (get_table_entry_name (uimage_os, "Unknown OS", os)); | |
42b73e8e MB |
528 | } |
529 | ||
570abb0a | 530 | const char *genimg_get_arch_name (uint8_t arch) |
42b73e8e | 531 | { |
570abb0a MB |
532 | return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch)); |
533 | } | |
42b73e8e | 534 | |
570abb0a MB |
535 | const char *genimg_get_type_name (uint8_t type) |
536 | { | |
537 | return (get_table_entry_name (uimage_type, "Unknown Image", type)); | |
538 | } | |
42b73e8e | 539 | |
570abb0a MB |
540 | const char *genimg_get_comp_name (uint8_t comp) |
541 | { | |
542 | return (get_table_entry_name (uimage_comp, "Unknown Compression", comp)); | |
42b73e8e MB |
543 | } |
544 | ||
570abb0a MB |
545 | /** |
546 | * get_table_entry_id - translate short entry name to id | |
547 | * @table: pointer to a translation table for entries of a specific type | |
548 | * @table_name: to be used in case of error | |
549 | * @name: entry short name to be translated | |
550 | * | |
551 | * get_table_entry_id() will go over translation table trying to find | |
552 | * entry that matches given short name. If matching entry is found, | |
553 | * its id returned to the caller. | |
554 | * | |
555 | * returns: | |
556 | * entry id if translation succeeds | |
557 | * -1 otherwise | |
558 | */ | |
559 | static int get_table_entry_id (table_entry_t *table, | |
560 | const char *table_name, const char *name) | |
42b73e8e | 561 | { |
570abb0a MB |
562 | table_entry_t *t; |
563 | #ifdef USE_HOSTCC | |
564 | int first = 1; | |
42b73e8e | 565 | |
570abb0a MB |
566 | for (t = table; t->id >= 0; ++t) { |
567 | if (t->sname && strcasecmp(t->sname, name) == 0) | |
568 | return (t->id); | |
42b73e8e MB |
569 | } |
570 | ||
570abb0a MB |
571 | fprintf (stderr, "\nInvalid %s Type - valid names are", table_name); |
572 | for (t = table; t->id >= 0; ++t) { | |
573 | if (t->sname == NULL) | |
574 | continue; | |
575 | fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname); | |
576 | first = 0; | |
577 | } | |
578 | fprintf (stderr, "\n"); | |
579 | #else | |
580 | for (t = table; t->id >= 0; ++t) { | |
581 | if (t->sname && strcmp(t->sname, name) == 0) | |
582 | return (t->id); | |
583 | } | |
584 | debug ("Invalid %s Type: %s\n", table_name, name); | |
585 | #endif /* USE_HOSTCC */ | |
586 | return (-1); | |
587 | } | |
588 | ||
589 | int genimg_get_os_id (const char *name) | |
590 | { | |
591 | return (get_table_entry_id (uimage_os, "OS", name)); | |
592 | } | |
593 | ||
594 | int genimg_get_arch_id (const char *name) | |
595 | { | |
596 | return (get_table_entry_id (uimage_arch, "CPU", name)); | |
42b73e8e | 597 | } |
5ad03eb3 | 598 | |
570abb0a MB |
599 | int genimg_get_type_id (const char *name) |
600 | { | |
601 | return (get_table_entry_id (uimage_type, "Image", name)); | |
602 | } | |
603 | ||
604 | int genimg_get_comp_id (const char *name) | |
605 | { | |
606 | return (get_table_entry_id (uimage_comp, "Compression", name)); | |
607 | } | |
608 | ||
609 | #ifndef USE_HOSTCC | |
fff888a1 | 610 | /** |
9a4daad0 | 611 | * genimg_get_format - get image format type |
fff888a1 MB |
612 | * @img_addr: image start address |
613 | * | |
9a4daad0 | 614 | * genimg_get_format() checks whether provided address points to a valid |
fff888a1 MB |
615 | * legacy or FIT image. |
616 | * | |
4efbe9db MB |
617 | * New uImage format and FDT blob are based on a libfdt. FDT blob |
618 | * may be passed directly or embedded in a FIT image. In both situations | |
9a4daad0 | 619 | * genimg_get_format() must be able to dectect libfdt header. |
4efbe9db | 620 | * |
fff888a1 MB |
621 | * returns: |
622 | * image format type or IMAGE_FORMAT_INVALID if no image is present | |
623 | */ | |
9a4daad0 | 624 | int genimg_get_format (void *img_addr) |
fff888a1 MB |
625 | { |
626 | ulong format = IMAGE_FORMAT_INVALID; | |
627 | image_header_t *hdr; | |
4efbe9db | 628 | #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT) |
fff888a1 MB |
629 | char *fit_hdr; |
630 | #endif | |
631 | ||
632 | hdr = (image_header_t *)img_addr; | |
633 | if (image_check_magic(hdr)) | |
634 | format = IMAGE_FORMAT_LEGACY; | |
4efbe9db | 635 | #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT) |
fff888a1 MB |
636 | else { |
637 | fit_hdr = (char *)img_addr; | |
638 | if (fdt_check_header (fit_hdr) == 0) | |
639 | format = IMAGE_FORMAT_FIT; | |
640 | } | |
641 | #endif | |
642 | ||
643 | return format; | |
644 | } | |
645 | ||
646 | /** | |
9a4daad0 | 647 | * genimg_get_image - get image from special storage (if necessary) |
fff888a1 MB |
648 | * @img_addr: image start address |
649 | * | |
9a4daad0 | 650 | * genimg_get_image() checks if provided image start adddress is located |
fff888a1 MB |
651 | * in a dataflash storage. If so, image is moved to a system RAM memory. |
652 | * | |
653 | * returns: | |
654 | * image start address after possible relocation from special storage | |
655 | */ | |
9a4daad0 | 656 | ulong genimg_get_image (ulong img_addr) |
fff888a1 | 657 | { |
6f0f9dfc | 658 | ulong ram_addr = img_addr; |
fff888a1 MB |
659 | |
660 | #ifdef CONFIG_HAS_DATAFLASH | |
6f0f9dfc MB |
661 | ulong h_size, d_size; |
662 | ||
fff888a1 | 663 | if (addr_dataflash (img_addr)){ |
6f0f9dfc | 664 | /* ger RAM address */ |
fff888a1 | 665 | ram_addr = CFG_LOAD_ADDR; |
6f0f9dfc MB |
666 | |
667 | /* get header size */ | |
668 | h_size = image_get_header_size (); | |
669 | #if defined(CONFIG_FIT) | |
670 | if (sizeof(struct fdt_header) > h_size) | |
671 | h_size = sizeof(struct fdt_header); | |
672 | #endif | |
673 | ||
674 | /* read in header */ | |
fff888a1 MB |
675 | debug (" Reading image header from dataflash address " |
676 | "%08lx to RAM address %08lx\n", img_addr, ram_addr); | |
fff888a1 | 677 | |
6f0f9dfc | 678 | read_dataflash (img_addr, h_size, (char *)ram_addr); |
fff888a1 | 679 | |
6f0f9dfc | 680 | /* get data size */ |
9a4daad0 | 681 | switch (genimg_get_format ((void *)ram_addr)) { |
6f0f9dfc MB |
682 | case IMAGE_FORMAT_LEGACY: |
683 | d_size = image_get_data_size ((image_header_t *)ram_addr); | |
684 | debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n", | |
685 | ram_addr, d_size); | |
686 | break; | |
fff888a1 | 687 | #if defined(CONFIG_FIT) |
6f0f9dfc | 688 | case IMAGE_FORMAT_FIT: |
5dfb5213 | 689 | d_size = fit_get_size ((const void *)ram_addr) - h_size; |
6f0f9dfc MB |
690 | debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n", |
691 | ram_addr, d_size); | |
692 | break; | |
fff888a1 | 693 | #endif |
6f0f9dfc MB |
694 | default: |
695 | printf (" No valid image found at 0x%08lx\n", img_addr); | |
696 | return ram_addr; | |
697 | } | |
fff888a1 | 698 | |
6f0f9dfc | 699 | /* read in image data */ |
fff888a1 MB |
700 | debug (" Reading image remaining data from dataflash address " |
701 | "%08lx to RAM address %08lx\n", img_addr + h_size, | |
702 | ram_addr + h_size); | |
703 | ||
704 | read_dataflash (img_addr + h_size, d_size, | |
705 | (char *)(ram_addr + h_size)); | |
6f0f9dfc | 706 | |
fff888a1 | 707 | } |
6f0f9dfc | 708 | #endif /* CONFIG_HAS_DATAFLASH */ |
fff888a1 MB |
709 | |
710 | return ram_addr; | |
711 | } | |
712 | ||
f773bea8 MB |
713 | /** |
714 | * fit_has_config - check if there is a valid FIT configuration | |
715 | * @images: pointer to the bootm command headers structure | |
716 | * | |
717 | * fit_has_config() checks if there is a FIT configuration in use | |
718 | * (if FTI support is present). | |
719 | * | |
720 | * returns: | |
721 | * 0, no FIT support or no configuration found | |
722 | * 1, configuration found | |
723 | */ | |
724 | int genimg_has_config (bootm_headers_t *images) | |
725 | { | |
726 | #if defined(CONFIG_FIT) | |
727 | if (images->fit_uname_cfg) | |
728 | return 1; | |
729 | #endif | |
730 | return 0; | |
731 | } | |
732 | ||
5ad03eb3 | 733 | /** |
9a4daad0 | 734 | * boot_get_ramdisk - main ramdisk handling routine |
5ad03eb3 MB |
735 | * @argc: command argument count |
736 | * @argv: command argument list | |
8a5ea3e6 | 737 | * @images: pointer to the bootm images structure |
5ad03eb3 MB |
738 | * @arch: expected ramdisk architecture |
739 | * @rd_start: pointer to a ulong variable, will hold ramdisk start address | |
740 | * @rd_end: pointer to a ulong variable, will hold ramdisk end | |
741 | * | |
9a4daad0 | 742 | * boot_get_ramdisk() is responsible for finding a valid ramdisk image. |
5ad03eb3 MB |
743 | * Curently supported are the following ramdisk sources: |
744 | * - multicomponent kernel/ramdisk image, | |
745 | * - commandline provided address of decicated ramdisk image. | |
746 | * | |
747 | * returns: | |
d985c849 | 748 | * 0, if ramdisk image was found and valid, or skiped |
5ad03eb3 MB |
749 | * rd_start and rd_end are set to ramdisk start/end addresses if |
750 | * ramdisk image is found and valid | |
d985c849 MB |
751 | * |
752 | * 1, if ramdisk image is found but corrupted | |
5ad03eb3 | 753 | * rd_start and rd_end are set to 0 if no ramdisk exists |
5ad03eb3 | 754 | */ |
d985c849 MB |
755 | int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images, |
756 | uint8_t arch, ulong *rd_start, ulong *rd_end) | |
5ad03eb3 | 757 | { |
d5934ad7 | 758 | ulong rd_addr, rd_load; |
5ad03eb3 MB |
759 | ulong rd_data, rd_len; |
760 | image_header_t *rd_hdr; | |
d5934ad7 MB |
761 | #if defined(CONFIG_FIT) |
762 | void *fit_hdr; | |
763 | const char *fit_uname_config = NULL; | |
764 | const char *fit_uname_ramdisk = NULL; | |
765 | ulong default_addr; | |
c8779648 | 766 | int rd_noffset; |
f773bea8 | 767 | int cfg_noffset; |
c8779648 MB |
768 | const void *data; |
769 | size_t size; | |
d5934ad7 | 770 | #endif |
5ad03eb3 | 771 | |
c8779648 MB |
772 | *rd_start = 0; |
773 | *rd_end = 0; | |
774 | ||
d5934ad7 MB |
775 | /* |
776 | * Look for a '-' which indicates to ignore the | |
777 | * ramdisk argument | |
778 | */ | |
779 | if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) { | |
780 | debug ("## Skipping init Ramdisk\n"); | |
781 | rd_len = rd_data = 0; | |
f773bea8 | 782 | } else if (argc >= 3 || genimg_has_config (images)) { |
d5934ad7 | 783 | #if defined(CONFIG_FIT) |
f773bea8 MB |
784 | if (argc >= 3) { |
785 | /* | |
786 | * If the init ramdisk comes from the FIT image and | |
787 | * the FIT image address is omitted in the command | |
788 | * line argument, try to use os FIT image address or | |
789 | * default load address. | |
790 | */ | |
791 | if (images->fit_uname_os) | |
792 | default_addr = (ulong)images->fit_hdr_os; | |
793 | else | |
794 | default_addr = load_addr; | |
795 | ||
796 | if (fit_parse_conf (argv[2], default_addr, | |
797 | &rd_addr, &fit_uname_config)) { | |
798 | debug ("* ramdisk: config '%s' from image at 0x%08lx\n", | |
799 | fit_uname_config, rd_addr); | |
800 | } else if (fit_parse_subimage (argv[2], default_addr, | |
801 | &rd_addr, &fit_uname_ramdisk)) { | |
802 | debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n", | |
803 | fit_uname_ramdisk, rd_addr); | |
804 | } else | |
d5934ad7 | 805 | #endif |
f773bea8 MB |
806 | { |
807 | rd_addr = simple_strtoul(argv[2], NULL, 16); | |
808 | debug ("* ramdisk: cmdline image address = 0x%08lx\n", | |
809 | rd_addr); | |
810 | } | |
811 | #if defined(CONFIG_FIT) | |
812 | } else { | |
813 | /* use FIT configuration provided in first bootm | |
814 | * command argument | |
815 | */ | |
816 | rd_addr = (ulong)images->fit_hdr_os; | |
817 | fit_uname_config = images->fit_uname_cfg; | |
818 | debug ("* ramdisk: using config '%s' from image at 0x%08lx\n", | |
819 | fit_uname_config, rd_addr); | |
820 | ||
821 | /* | |
822 | * Check whether configuration has ramdisk defined, | |
823 | * if not, don't try to use it, quit silently. | |
824 | */ | |
825 | fit_hdr = (void *)rd_addr; | |
826 | cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config); | |
827 | if (cfg_noffset < 0) { | |
828 | debug ("* ramdisk: no such config\n"); | |
c78fce69 | 829 | return 1; |
f773bea8 MB |
830 | } |
831 | ||
832 | rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset); | |
833 | if (rd_noffset < 0) { | |
834 | debug ("* ramdisk: no ramdisk in config\n"); | |
41266c9b | 835 | return 0; |
f773bea8 | 836 | } |
d5934ad7 | 837 | } |
f773bea8 | 838 | #endif |
d5934ad7 MB |
839 | |
840 | /* copy from dataflash if needed */ | |
9a4daad0 | 841 | rd_addr = genimg_get_image (rd_addr); |
d5934ad7 MB |
842 | |
843 | /* | |
844 | * Check if there is an initrd image at the | |
845 | * address provided in the second bootm argument | |
846 | * check image type, for FIT images get FIT node. | |
847 | */ | |
9a4daad0 | 848 | switch (genimg_get_format ((void *)rd_addr)) { |
d5934ad7 | 849 | case IMAGE_FORMAT_LEGACY: |
c8779648 MB |
850 | printf ("## Loading init Ramdisk from Legacy " |
851 | "Image at %08lx ...\n", rd_addr); | |
5ad03eb3 | 852 | |
1372cce2 | 853 | show_boot_progress (9); |
d985c849 MB |
854 | rd_hdr = image_get_ramdisk (rd_addr, arch, |
855 | images->verify); | |
5ad03eb3 | 856 | |
c8779648 | 857 | if (rd_hdr == NULL) |
274cea2b | 858 | return 1; |
274cea2b | 859 | |
5ad03eb3 MB |
860 | rd_data = image_get_data (rd_hdr); |
861 | rd_len = image_get_data_size (rd_hdr); | |
d5934ad7 MB |
862 | rd_load = image_get_load (rd_hdr); |
863 | break; | |
864 | #if defined(CONFIG_FIT) | |
865 | case IMAGE_FORMAT_FIT: | |
866 | fit_hdr = (void *)rd_addr; | |
c8779648 MB |
867 | printf ("## Loading init Ramdisk from FIT " |
868 | "Image at %08lx ...\n", rd_addr); | |
869 | ||
1372cce2 | 870 | show_boot_progress (120); |
c8779648 MB |
871 | if (!fit_check_format (fit_hdr)) { |
872 | puts ("Bad FIT ramdisk image format!\n"); | |
1372cce2 | 873 | show_boot_progress (-120); |
c78fce69 | 874 | return 1; |
c8779648 | 875 | } |
1372cce2 | 876 | show_boot_progress (121); |
c8779648 MB |
877 | |
878 | if (!fit_uname_ramdisk) { | |
879 | /* | |
880 | * no ramdisk image node unit name, try to get config | |
881 | * node first. If config unit node name is NULL | |
882 | * fit_conf_get_node() will try to find default config node | |
883 | */ | |
1372cce2 | 884 | show_boot_progress (122); |
f773bea8 MB |
885 | cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config); |
886 | if (cfg_noffset < 0) { | |
887 | puts ("Could not find configuration node\n"); | |
1372cce2 | 888 | show_boot_progress (-122); |
c78fce69 | 889 | return 1; |
1372cce2 | 890 | } |
f773bea8 MB |
891 | fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL); |
892 | printf (" Using '%s' configuration\n", fit_uname_config); | |
c8779648 | 893 | |
f773bea8 | 894 | rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset); |
c8779648 MB |
895 | fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL); |
896 | } else { | |
897 | /* get ramdisk component image node offset */ | |
1372cce2 | 898 | show_boot_progress (123); |
c8779648 MB |
899 | rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk); |
900 | } | |
1372cce2 | 901 | if (rd_noffset < 0) { |
f773bea8 | 902 | puts ("Could not find subimage node\n"); |
1372cce2 | 903 | show_boot_progress (-124); |
c78fce69 | 904 | return 1; |
1372cce2 | 905 | } |
c8779648 MB |
906 | |
907 | printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk); | |
908 | ||
1372cce2 | 909 | show_boot_progress (125); |
c8779648 | 910 | if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify)) |
c78fce69 | 911 | return 1; |
c8779648 MB |
912 | |
913 | /* get ramdisk image data address and length */ | |
914 | if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) { | |
915 | puts ("Could not find ramdisk subimage data!\n"); | |
1372cce2 | 916 | show_boot_progress (-127); |
c78fce69 | 917 | return 1; |
c8779648 | 918 | } |
1372cce2 | 919 | show_boot_progress (128); |
c8779648 MB |
920 | |
921 | rd_data = (ulong)data; | |
922 | rd_len = size; | |
923 | ||
924 | if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) { | |
925 | puts ("Can't get ramdisk subimage load address!\n"); | |
1372cce2 | 926 | show_boot_progress (-129); |
c78fce69 | 927 | return 1; |
c8779648 | 928 | } |
1372cce2 | 929 | show_boot_progress (129); |
c8779648 MB |
930 | |
931 | images->fit_hdr_rd = fit_hdr; | |
932 | images->fit_uname_rd = fit_uname_ramdisk; | |
3dfe1101 | 933 | images->fit_noffset_rd = rd_noffset; |
c8779648 | 934 | break; |
d5934ad7 MB |
935 | #endif |
936 | default: | |
d985c849 | 937 | puts ("Wrong Ramdisk Image Format\n"); |
c8779648 | 938 | rd_data = rd_len = rd_load = 0; |
d5934ad7 | 939 | } |
5ad03eb3 MB |
940 | |
941 | #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO) | |
d5934ad7 MB |
942 | /* |
943 | * We need to copy the ramdisk to SRAM to let Linux boot | |
944 | */ | |
945 | if (rd_data) { | |
946 | memmove ((void *)rd_load, (uchar *)rd_data, rd_len); | |
947 | rd_data = rd_load; | |
5ad03eb3 | 948 | } |
d5934ad7 | 949 | #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */ |
5ad03eb3 | 950 | |
d5934ad7 | 951 | } else if (images->legacy_hdr_valid && |
cb1c4896 | 952 | image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) { |
5ad03eb3 | 953 | /* |
d5934ad7 MB |
954 | * Now check if we have a legacy mult-component image, |
955 | * get second entry data start address and len. | |
5ad03eb3 MB |
956 | */ |
957 | show_boot_progress (13); | |
958 | printf ("## Loading init Ramdisk from multi component " | |
c8779648 | 959 | "Legacy Image at %08lx ...\n", |
d5934ad7 MB |
960 | (ulong)images->legacy_hdr_os); |
961 | ||
962 | image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len); | |
5ad03eb3 MB |
963 | } else { |
964 | /* | |
965 | * no initrd image | |
966 | */ | |
967 | show_boot_progress (14); | |
968 | rd_len = rd_data = 0; | |
969 | } | |
970 | ||
971 | if (!rd_data) { | |
972 | debug ("## No init Ramdisk\n"); | |
5ad03eb3 MB |
973 | } else { |
974 | *rd_start = rd_data; | |
975 | *rd_end = rd_data + rd_len; | |
976 | } | |
977 | debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n", | |
978 | *rd_start, *rd_end); | |
274cea2b KG |
979 | |
980 | return 0; | |
5ad03eb3 | 981 | } |
ceaed2b1 | 982 | |
bf3d8b31 | 983 | #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC) |
ceaed2b1 | 984 | /** |
9a4daad0 | 985 | * boot_ramdisk_high - relocate init ramdisk |
e822d7fc | 986 | * @lmb: pointer to lmb handle, will be used for memory mgmt |
ceaed2b1 MB |
987 | * @rd_data: ramdisk data start address |
988 | * @rd_len: ramdisk data length | |
ceaed2b1 MB |
989 | * @initrd_start: pointer to a ulong variable, will hold final init ramdisk |
990 | * start address (after possible relocation) | |
991 | * @initrd_end: pointer to a ulong variable, will hold final init ramdisk | |
992 | * end address (after possible relocation) | |
993 | * | |
9a4daad0 | 994 | * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement |
ceaed2b1 MB |
995 | * variable and if requested ramdisk data is moved to a specified location. |
996 | * | |
9a4daad0 MB |
997 | * Initrd_start and initrd_end are set to final (after relocation) ramdisk |
998 | * start/end addresses if ramdisk image start and len were provided, | |
999 | * otherwise set initrd_start and initrd_end set to zeros. | |
1000 | * | |
ceaed2b1 | 1001 | * returns: |
9a4daad0 MB |
1002 | * 0 - success |
1003 | * -1 - failure | |
ceaed2b1 | 1004 | */ |
9a4daad0 | 1005 | int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len, |
e822d7fc | 1006 | ulong *initrd_start, ulong *initrd_end) |
ceaed2b1 MB |
1007 | { |
1008 | char *s; | |
1009 | ulong initrd_high; | |
1010 | int initrd_copy_to_ram = 1; | |
1011 | ||
1012 | if ((s = getenv ("initrd_high")) != NULL) { | |
1013 | /* a value of "no" or a similar string will act like 0, | |
1014 | * turning the "load high" feature off. This is intentional. | |
1015 | */ | |
1016 | initrd_high = simple_strtoul (s, NULL, 16); | |
1017 | if (initrd_high == ~0) | |
1018 | initrd_copy_to_ram = 0; | |
1019 | } else { | |
1020 | /* not set, no restrictions to load high */ | |
1021 | initrd_high = ~0; | |
1022 | } | |
1023 | ||
95d449ad MB |
1024 | |
1025 | #ifdef CONFIG_LOGBUFFER | |
1026 | /* Prevent initrd from overwriting logbuffer */ | |
1027 | lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE); | |
1028 | #endif | |
1029 | ||
ceaed2b1 MB |
1030 | debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n", |
1031 | initrd_high, initrd_copy_to_ram); | |
1032 | ||
1033 | if (rd_data) { | |
1034 | if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */ | |
1035 | debug (" in-place initrd\n"); | |
1036 | *initrd_start = rd_data; | |
1037 | *initrd_end = rd_data + rd_len; | |
e822d7fc | 1038 | lmb_reserve(lmb, rd_data, rd_len); |
ceaed2b1 | 1039 | } else { |
e822d7fc | 1040 | if (initrd_high) |
391fd93a | 1041 | *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high); |
e822d7fc | 1042 | else |
391fd93a | 1043 | *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000); |
e822d7fc KG |
1044 | |
1045 | if (*initrd_start == 0) { | |
9a4daad0 | 1046 | puts ("ramdisk - allocation error\n"); |
e822d7fc | 1047 | goto error; |
ceaed2b1 | 1048 | } |
ceaed2b1 MB |
1049 | show_boot_progress (12); |
1050 | ||
1051 | *initrd_end = *initrd_start + rd_len; | |
1052 | printf (" Loading Ramdisk to %08lx, end %08lx ... ", | |
1053 | *initrd_start, *initrd_end); | |
1054 | ||
9a4daad0 | 1055 | memmove_wd ((void *)*initrd_start, |
ceaed2b1 MB |
1056 | (void *)rd_data, rd_len, CHUNKSZ); |
1057 | ||
1058 | puts ("OK\n"); | |
1059 | } | |
1060 | } else { | |
1061 | *initrd_start = 0; | |
1062 | *initrd_end = 0; | |
1063 | } | |
1064 | debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n", | |
1065 | *initrd_start, *initrd_end); | |
9a4daad0 | 1066 | |
e822d7fc | 1067 | return 0; |
b6b0fe64 | 1068 | |
e822d7fc KG |
1069 | error: |
1070 | return -1; | |
b6b0fe64 MB |
1071 | } |
1072 | ||
1073 | /** | |
9a4daad0 | 1074 | * boot_get_cmdline - allocate and initialize kernel cmdline |
e822d7fc | 1075 | * @lmb: pointer to lmb handle, will be used for memory mgmt |
b6b0fe64 MB |
1076 | * @cmd_start: pointer to a ulong variable, will hold cmdline start |
1077 | * @cmd_end: pointer to a ulong variable, will hold cmdline end | |
e822d7fc KG |
1078 | * @bootmap_base: ulong variable, holds offset in physical memory to |
1079 | * base of bootmap | |
b6b0fe64 | 1080 | * |
9a4daad0 | 1081 | * boot_get_cmdline() allocates space for kernel command line below |
e822d7fc | 1082 | * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt |
b6b0fe64 MB |
1083 | * variable is present its contents is copied to allocated kernel |
1084 | * command line. | |
1085 | * | |
1086 | * returns: | |
e822d7fc KG |
1087 | * 0 - success |
1088 | * -1 - failure | |
b6b0fe64 | 1089 | */ |
9a4daad0 | 1090 | int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end, |
e822d7fc | 1091 | ulong bootmap_base) |
b6b0fe64 MB |
1092 | { |
1093 | char *cmdline; | |
1094 | char *s; | |
1095 | ||
391fd93a | 1096 | cmdline = (char *)(ulong)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf, |
e822d7fc KG |
1097 | CFG_BOOTMAPSZ + bootmap_base); |
1098 | ||
1099 | if (cmdline == NULL) | |
1100 | return -1; | |
b6b0fe64 MB |
1101 | |
1102 | if ((s = getenv("bootargs")) == NULL) | |
1103 | s = ""; | |
1104 | ||
1105 | strcpy(cmdline, s); | |
1106 | ||
1107 | *cmd_start = (ulong) & cmdline[0]; | |
1108 | *cmd_end = *cmd_start + strlen(cmdline); | |
1109 | ||
1110 | debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end); | |
1111 | ||
e822d7fc | 1112 | return 0; |
b6b0fe64 MB |
1113 | } |
1114 | ||
1115 | /** | |
9a4daad0 | 1116 | * boot_get_kbd - allocate and initialize kernel copy of board info |
e822d7fc | 1117 | * @lmb: pointer to lmb handle, will be used for memory mgmt |
b6b0fe64 | 1118 | * @kbd: double pointer to board info data |
e822d7fc KG |
1119 | * @bootmap_base: ulong variable, holds offset in physical memory to |
1120 | * base of bootmap | |
b6b0fe64 | 1121 | * |
9a4daad0 | 1122 | * boot_get_kbd() allocates space for kernel copy of board info data below |
e822d7fc KG |
1123 | * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with |
1124 | * the current u-boot board info data. | |
b6b0fe64 MB |
1125 | * |
1126 | * returns: | |
e822d7fc KG |
1127 | * 0 - success |
1128 | * -1 - failure | |
b6b0fe64 | 1129 | */ |
9a4daad0 | 1130 | int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base) |
b6b0fe64 | 1131 | { |
391fd93a | 1132 | *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf, |
e822d7fc KG |
1133 | CFG_BOOTMAPSZ + bootmap_base); |
1134 | if (*kbd == NULL) | |
1135 | return -1; | |
1136 | ||
b6b0fe64 MB |
1137 | **kbd = *(gd->bd); |
1138 | ||
1139 | debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd); | |
1140 | ||
1141 | #if defined(DEBUG) && defined(CONFIG_CMD_BDI) | |
1142 | do_bdinfo(NULL, 0, 0, NULL); | |
1143 | #endif | |
1144 | ||
e822d7fc | 1145 | return 0; |
ceaed2b1 MB |
1146 | } |
1147 | #endif /* CONFIG_PPC || CONFIG_M68K */ | |
5dfb5213 | 1148 | #endif /* !USE_HOSTCC */ |
5ad03eb3 | 1149 | |
f50433d6 MB |
1150 | #if defined(CONFIG_FIT) |
1151 | /*****************************************************************************/ | |
1152 | /* New uImage format routines */ | |
1153 | /*****************************************************************************/ | |
5dfb5213 | 1154 | #ifndef USE_HOSTCC |
f50433d6 MB |
1155 | static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr, |
1156 | ulong *addr, const char **name) | |
1157 | { | |
1158 | const char *sep; | |
1159 | ||
1160 | *addr = addr_curr; | |
1161 | *name = NULL; | |
1162 | ||
1163 | sep = strchr (spec, sepc); | |
1164 | if (sep) { | |
1165 | if (sep - spec > 0) | |
1166 | *addr = simple_strtoul (spec, NULL, 16); | |
1167 | ||
1168 | *name = sep + 1; | |
1169 | return 1; | |
1170 | } | |
1171 | ||
1172 | return 0; | |
1173 | } | |
1174 | ||
1175 | /** | |
1176 | * fit_parse_conf - parse FIT configuration spec | |
1177 | * @spec: input string, containing configuration spec | |
1178 | * @add_curr: current image address (to be used as a possible default) | |
1179 | * @addr: pointer to a ulong variable, will hold FIT image address of a given | |
1180 | * configuration | |
1181 | * @conf_name double pointer to a char, will hold pointer to a configuration | |
1182 | * unit name | |
1183 | * | |
1184 | * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>, | |
1185 | * where <addr> is a FIT image address that contains configuration | |
1186 | * with a <conf> unit name. | |
1187 | * | |
1188 | * Address part is optional, and if omitted default add_curr will | |
1189 | * be used instead. | |
1190 | * | |
1191 | * returns: | |
1192 | * 1 if spec is a valid configuration string, | |
1193 | * addr and conf_name are set accordingly | |
1194 | * 0 otherwise | |
1195 | */ | |
1196 | inline int fit_parse_conf (const char *spec, ulong addr_curr, | |
1197 | ulong *addr, const char **conf_name) | |
1198 | { | |
1199 | return fit_parse_spec (spec, '#', addr_curr, addr, conf_name); | |
1200 | } | |
1201 | ||
1202 | /** | |
1203 | * fit_parse_subimage - parse FIT subimage spec | |
1204 | * @spec: input string, containing subimage spec | |
1205 | * @add_curr: current image address (to be used as a possible default) | |
1206 | * @addr: pointer to a ulong variable, will hold FIT image address of a given | |
1207 | * subimage | |
1208 | * @image_name: double pointer to a char, will hold pointer to a subimage name | |
1209 | * | |
1210 | * fit_parse_subimage() expects subimage spec in the for of | |
1211 | * [<addr>]:<subimage>, where <addr> is a FIT image address that contains | |
1212 | * subimage with a <subimg> unit name. | |
1213 | * | |
1214 | * Address part is optional, and if omitted default add_curr will | |
1215 | * be used instead. | |
1216 | * | |
1217 | * returns: | |
1218 | * 1 if spec is a valid subimage string, | |
1219 | * addr and image_name are set accordingly | |
1220 | * 0 otherwise | |
1221 | */ | |
1222 | inline int fit_parse_subimage (const char *spec, ulong addr_curr, | |
1223 | ulong *addr, const char **image_name) | |
1224 | { | |
1225 | return fit_parse_spec (spec, ':', addr_curr, addr, image_name); | |
1226 | } | |
5dfb5213 MB |
1227 | #endif /* !USE_HOSTCC */ |
1228 | ||
1229 | static void fit_get_debug (const void *fit, int noffset, | |
1230 | char *prop_name, int err) | |
1231 | { | |
1232 | debug ("Can't get '%s' property from FIT 0x%08lx, " | |
1233 | "node: offset %d, name %s (%s)\n", | |
1234 | prop_name, (ulong)fit, noffset, | |
1235 | fit_get_name (fit, noffset, NULL), | |
1236 | fdt_strerror (err)); | |
1237 | } | |
1238 | ||
1239 | /** | |
edbed247 | 1240 | * fit_print_contents - prints out the contents of the FIT format image |
5dfb5213 MB |
1241 | * @fit: pointer to the FIT format image header |
1242 | * @p: pointer to prefix string | |
1243 | * | |
edbed247 | 1244 | * fit_print_contents() formats a multi line FIT image contents description. |
5dfb5213 MB |
1245 | * The routine prints out FIT image properties (root node level) follwed by |
1246 | * the details of each component image. | |
1247 | * | |
1248 | * returns: | |
1249 | * no returned results | |
1250 | */ | |
edbed247 | 1251 | void fit_print_contents (const void *fit) |
5dfb5213 MB |
1252 | { |
1253 | char *desc; | |
1254 | char *uname; | |
1255 | int images_noffset; | |
1256 | int confs_noffset; | |
1257 | int noffset; | |
1258 | int ndepth; | |
1259 | int count = 0; | |
1260 | int ret; | |
edbed247 | 1261 | const char *p; |
5dfb5213 MB |
1262 | #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) |
1263 | time_t timestamp; | |
1264 | #endif | |
1265 | ||
edbed247 BS |
1266 | #ifdef USE_HOSTCC |
1267 | p = ""; | |
1268 | #else | |
1269 | p = " "; | |
1270 | #endif | |
1271 | ||
5dfb5213 MB |
1272 | /* Root node properties */ |
1273 | ret = fit_get_desc (fit, 0, &desc); | |
1274 | printf ("%sFIT description: ", p); | |
1275 | if (ret) | |
1276 | printf ("unavailable\n"); | |
1277 | else | |
1278 | printf ("%s\n", desc); | |
1279 | ||
1280 | #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) | |
1281 | ret = fit_get_timestamp (fit, 0, ×tamp); | |
1282 | printf ("%sCreated: ", p); | |
1283 | if (ret) | |
1284 | printf ("unavailable\n"); | |
1285 | else | |
1286 | genimg_print_time (timestamp); | |
1287 | #endif | |
1288 | ||
1289 | /* Find images parent node offset */ | |
1290 | images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH); | |
1291 | if (images_noffset < 0) { | |
1292 | printf ("Can't find images parent node '%s' (%s)\n", | |
1293 | FIT_IMAGES_PATH, fdt_strerror (images_noffset)); | |
1294 | return; | |
1295 | } | |
1296 | ||
1297 | /* Process its subnodes, print out component images details */ | |
1298 | for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth); | |
1299 | (noffset >= 0) && (ndepth > 0); | |
1300 | noffset = fdt_next_node (fit, noffset, &ndepth)) { | |
1301 | if (ndepth == 1) { | |
1302 | /* | |
1303 | * Direct child node of the images parent node, | |
1304 | * i.e. component image node. | |
1305 | */ | |
1306 | printf ("%s Image %u (%s)\n", p, count++, | |
1307 | fit_get_name(fit, noffset, NULL)); | |
1308 | ||
1309 | fit_image_print (fit, noffset, p); | |
1310 | } | |
1311 | } | |
1312 | ||
1313 | /* Find configurations parent node offset */ | |
1314 | confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH); | |
1315 | if (confs_noffset < 0) { | |
1316 | debug ("Can't get configurations parent node '%s' (%s)\n", | |
1317 | FIT_CONFS_PATH, fdt_strerror (confs_noffset)); | |
1318 | return; | |
1319 | } | |
1320 | ||
1321 | /* get default configuration unit name from default property */ | |
1322 | uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL); | |
1323 | if (uname) | |
1324 | printf ("%s Default Configuration: '%s'\n", p, uname); | |
1325 | ||
1326 | /* Process its subnodes, print out configurations details */ | |
1327 | for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth); | |
1328 | (noffset >= 0) && (ndepth > 0); | |
1329 | noffset = fdt_next_node (fit, noffset, &ndepth)) { | |
1330 | if (ndepth == 1) { | |
1331 | /* | |
1332 | * Direct child node of the configurations parent node, | |
1333 | * i.e. configuration node. | |
1334 | */ | |
1335 | printf ("%s Configuration %u (%s)\n", p, count++, | |
1336 | fit_get_name(fit, noffset, NULL)); | |
1337 | ||
1338 | fit_conf_print (fit, noffset, p); | |
1339 | } | |
1340 | } | |
1341 | } | |
1342 | ||
5dfb5213 MB |
1343 | /** |
1344 | * fit_image_print - prints out the FIT component image details | |
1345 | * @fit: pointer to the FIT format image header | |
1346 | * @image_noffset: offset of the component image node | |
1347 | * @p: pointer to prefix string | |
1348 | * | |
1349 | * fit_image_print() lists all mandatory properies for the processed component | |
1350 | * image. If present, hash nodes are printed out as well. | |
1351 | * | |
1352 | * returns: | |
1353 | * no returned results | |
1354 | */ | |
1355 | void fit_image_print (const void *fit, int image_noffset, const char *p) | |
1356 | { | |
1357 | char *desc; | |
1358 | uint8_t type, arch, os, comp; | |
1359 | size_t size; | |
1360 | ulong load, entry; | |
1361 | const void *data; | |
1362 | int noffset; | |
1363 | int ndepth; | |
1364 | int ret; | |
1365 | ||
1366 | /* Mandatory properties */ | |
1367 | ret = fit_get_desc (fit, image_noffset, &desc); | |
1368 | printf ("%s Description: ", p); | |
1369 | if (ret) | |
1370 | printf ("unavailable\n"); | |
1371 | else | |
1372 | printf ("%s\n", desc); | |
1373 | ||
1374 | fit_image_get_type (fit, image_noffset, &type); | |
1375 | printf ("%s Type: %s\n", p, genimg_get_type_name (type)); | |
1376 | ||
1377 | fit_image_get_comp (fit, image_noffset, &comp); | |
1378 | printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp)); | |
1379 | ||
1380 | ret = fit_image_get_data (fit, image_noffset, &data, &size); | |
1381 | ||
1382 | #ifndef USE_HOSTCC | |
1383 | printf ("%s Data Start: ", p); | |
1384 | if (ret) | |
1385 | printf ("unavailable\n"); | |
1386 | else | |
1387 | printf ("0x%08lx\n", (ulong)data); | |
1388 | #endif | |
1389 | ||
1390 | printf ("%s Data Size: ", p); | |
1391 | if (ret) | |
1392 | printf ("unavailable\n"); | |
1393 | else | |
1394 | genimg_print_size (size); | |
1395 | ||
1396 | /* Remaining, type dependent properties */ | |
1397 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || | |
1398 | (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) || | |
1399 | (type == IH_TYPE_FLATDT)) { | |
1400 | fit_image_get_arch (fit, image_noffset, &arch); | |
1401 | printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch)); | |
1402 | } | |
1403 | ||
1404 | if (type == IH_TYPE_KERNEL) { | |
1405 | fit_image_get_os (fit, image_noffset, &os); | |
1406 | printf ("%s OS: %s\n", p, genimg_get_os_name (os)); | |
1407 | } | |
1408 | ||
1409 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) { | |
1410 | ret = fit_image_get_load (fit, image_noffset, &load); | |
1411 | printf ("%s Load Address: ", p); | |
1412 | if (ret) | |
1413 | printf ("unavailable\n"); | |
1414 | else | |
1415 | printf ("0x%08lx\n", load); | |
1416 | ||
1417 | fit_image_get_entry (fit, image_noffset, &entry); | |
1418 | printf ("%s Entry Point: ", p); | |
1419 | if (ret) | |
1420 | printf ("unavailable\n"); | |
1421 | else | |
1422 | printf ("0x%08lx\n", entry); | |
1423 | } | |
1424 | ||
1425 | /* Process all hash subnodes of the component image node */ | |
1426 | for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth); | |
1427 | (noffset >= 0) && (ndepth > 0); | |
1428 | noffset = fdt_next_node (fit, noffset, &ndepth)) { | |
1429 | if (ndepth == 1) { | |
1430 | /* Direct child node of the component image node */ | |
1431 | fit_image_print_hash (fit, noffset, p); | |
1432 | } | |
1433 | } | |
1434 | } | |
1435 | ||
1436 | /** | |
1437 | * fit_image_print_hash - prints out the hash node details | |
1438 | * @fit: pointer to the FIT format image header | |
1439 | * @noffset: offset of the hash node | |
1440 | * @p: pointer to prefix string | |
1441 | * | |
1442 | * fit_image_print_hash() lists properies for the processed hash node | |
1443 | * | |
1444 | * returns: | |
1445 | * no returned results | |
1446 | */ | |
1447 | void fit_image_print_hash (const void *fit, int noffset, const char *p) | |
1448 | { | |
1449 | char *algo; | |
1450 | uint8_t *value; | |
1451 | int value_len; | |
1452 | int i, ret; | |
1453 | ||
1454 | /* | |
1455 | * Check subnode name, must be equal to "hash". | |
1456 | * Multiple hash nodes require unique unit node | |
1457 | * names, e.g. hash@1, hash@2, etc. | |
1458 | */ | |
1459 | if (strncmp (fit_get_name(fit, noffset, NULL), | |
1460 | FIT_HASH_NODENAME, | |
1461 | strlen(FIT_HASH_NODENAME)) != 0) | |
1462 | return; | |
1463 | ||
1464 | debug ("%s Hash node: '%s'\n", p, | |
1465 | fit_get_name (fit, noffset, NULL)); | |
1466 | ||
1467 | printf ("%s Hash algo: ", p); | |
1468 | if (fit_image_hash_get_algo (fit, noffset, &algo)) { | |
1469 | printf ("invalid/unsupported\n"); | |
1470 | return; | |
1471 | } | |
1472 | printf ("%s\n", algo); | |
1473 | ||
1474 | ret = fit_image_hash_get_value (fit, noffset, &value, | |
1475 | &value_len); | |
1476 | printf ("%s Hash value: ", p); | |
1477 | if (ret) { | |
1478 | printf ("unavailable\n"); | |
1479 | } else { | |
1480 | for (i = 0; i < value_len; i++) | |
1481 | printf ("%02x", value[i]); | |
1482 | printf ("\n"); | |
1483 | } | |
1484 | ||
1485 | debug ("%s Hash len: %d\n", p, value_len); | |
1486 | } | |
1487 | ||
1488 | /** | |
1489 | * fit_get_desc - get node description property | |
1490 | * @fit: pointer to the FIT format image header | |
1491 | * @noffset: node offset | |
1492 | * @desc: double pointer to the char, will hold pointer to the descrption | |
1493 | * | |
1494 | * fit_get_desc() reads description property from a given node, if | |
1495 | * description is found pointer to it is returened in third call argument. | |
1496 | * | |
1497 | * returns: | |
1498 | * 0, on success | |
1499 | * -1, on failure | |
1500 | */ | |
1501 | int fit_get_desc (const void *fit, int noffset, char **desc) | |
1502 | { | |
1503 | int len; | |
1504 | ||
1505 | *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len); | |
1506 | if (*desc == NULL) { | |
1507 | fit_get_debug (fit, noffset, FIT_DESC_PROP, len); | |
1508 | return -1; | |
1509 | } | |
1510 | ||
1511 | return 0; | |
1512 | } | |
1513 | ||
1514 | /** | |
1515 | * fit_get_timestamp - get node timestamp property | |
1516 | * @fit: pointer to the FIT format image header | |
1517 | * @noffset: node offset | |
1518 | * @timestamp: pointer to the time_t, will hold read timestamp | |
1519 | * | |
1520 | * fit_get_timestamp() reads timestamp poperty from given node, if timestamp | |
1521 | * is found and has a correct size its value is retured in third call | |
1522 | * argument. | |
1523 | * | |
1524 | * returns: | |
1525 | * 0, on success | |
1526 | * -1, on property read failure | |
1527 | * -2, on wrong timestamp size | |
1528 | */ | |
1529 | int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp) | |
1530 | { | |
1531 | int len; | |
1532 | const void *data; | |
1533 | ||
1534 | data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len); | |
1535 | if (data == NULL) { | |
1536 | fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len); | |
1537 | return -1; | |
1538 | } | |
1539 | if (len != sizeof (uint32_t)) { | |
1540 | debug ("FIT timestamp with incorrect size of (%u)\n", len); | |
1541 | return -2; | |
1542 | } | |
1543 | ||
1544 | *timestamp = uimage_to_cpu (*((uint32_t *)data)); | |
1545 | return 0; | |
1546 | } | |
1547 | ||
1548 | /** | |
1549 | * fit_image_get_node - get node offset for component image of a given unit name | |
1550 | * @fit: pointer to the FIT format image header | |
1551 | * @image_uname: component image node unit name | |
1552 | * | |
1553 | * fit_image_get_node() finds a component image (withing the '/images' | |
1554 | * node) of a provided unit name. If image is found its node offset is | |
1555 | * returned to the caller. | |
1556 | * | |
1557 | * returns: | |
1558 | * image node offset when found (>=0) | |
1559 | * negative number on failure (FDT_ERR_* code) | |
1560 | */ | |
1561 | int fit_image_get_node (const void *fit, const char *image_uname) | |
1562 | { | |
1563 | int noffset, images_noffset; | |
1564 | ||
1565 | images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH); | |
1566 | if (images_noffset < 0) { | |
1567 | debug ("Can't find images parent node '%s' (%s)\n", | |
1568 | FIT_IMAGES_PATH, fdt_strerror (images_noffset)); | |
1569 | return images_noffset; | |
1570 | } | |
1571 | ||
1572 | noffset = fdt_subnode_offset (fit, images_noffset, image_uname); | |
1573 | if (noffset < 0) { | |
1574 | debug ("Can't get node offset for image unit name: '%s' (%s)\n", | |
1575 | image_uname, fdt_strerror (noffset)); | |
1576 | } | |
1577 | ||
1578 | return noffset; | |
1579 | } | |
1580 | ||
1581 | /** | |
1582 | * fit_image_get_os - get os id for a given component image node | |
1583 | * @fit: pointer to the FIT format image header | |
1584 | * @noffset: component image node offset | |
1585 | * @os: pointer to the uint8_t, will hold os numeric id | |
1586 | * | |
1587 | * fit_image_get_os() finds os property in a given component image node. | |
1588 | * If the property is found, its (string) value is translated to the numeric | |
1589 | * id which is returned to the caller. | |
1590 | * | |
1591 | * returns: | |
1592 | * 0, on success | |
1593 | * -1, on failure | |
1594 | */ | |
1595 | int fit_image_get_os (const void *fit, int noffset, uint8_t *os) | |
1596 | { | |
1597 | int len; | |
1598 | const void *data; | |
1599 | ||
1600 | /* Get OS name from property data */ | |
1601 | data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len); | |
1602 | if (data == NULL) { | |
1603 | fit_get_debug (fit, noffset, FIT_OS_PROP, len); | |
1604 | *os = -1; | |
1605 | return -1; | |
1606 | } | |
1607 | ||
1608 | /* Translate OS name to id */ | |
1609 | *os = genimg_get_os_id (data); | |
1610 | return 0; | |
1611 | } | |
1612 | ||
1613 | /** | |
1614 | * fit_image_get_arch - get arch id for a given component image node | |
1615 | * @fit: pointer to the FIT format image header | |
1616 | * @noffset: component image node offset | |
1617 | * @arch: pointer to the uint8_t, will hold arch numeric id | |
1618 | * | |
1619 | * fit_image_get_arch() finds arch property in a given component image node. | |
1620 | * If the property is found, its (string) value is translated to the numeric | |
1621 | * id which is returned to the caller. | |
1622 | * | |
1623 | * returns: | |
1624 | * 0, on success | |
1625 | * -1, on failure | |
1626 | */ | |
1627 | int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch) | |
1628 | { | |
1629 | int len; | |
1630 | const void *data; | |
1631 | ||
1632 | /* Get architecture name from property data */ | |
1633 | data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len); | |
1634 | if (data == NULL) { | |
1635 | fit_get_debug (fit, noffset, FIT_ARCH_PROP, len); | |
1636 | *arch = -1; | |
1637 | return -1; | |
1638 | } | |
1639 | ||
1640 | /* Translate architecture name to id */ | |
1641 | *arch = genimg_get_arch_id (data); | |
1642 | return 0; | |
1643 | } | |
1644 | ||
1645 | /** | |
1646 | * fit_image_get_type - get type id for a given component image node | |
1647 | * @fit: pointer to the FIT format image header | |
1648 | * @noffset: component image node offset | |
1649 | * @type: pointer to the uint8_t, will hold type numeric id | |
1650 | * | |
1651 | * fit_image_get_type() finds type property in a given component image node. | |
1652 | * If the property is found, its (string) value is translated to the numeric | |
1653 | * id which is returned to the caller. | |
1654 | * | |
1655 | * returns: | |
1656 | * 0, on success | |
1657 | * -1, on failure | |
1658 | */ | |
1659 | int fit_image_get_type (const void *fit, int noffset, uint8_t *type) | |
1660 | { | |
1661 | int len; | |
1662 | const void *data; | |
1663 | ||
1664 | /* Get image type name from property data */ | |
1665 | data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len); | |
1666 | if (data == NULL) { | |
1667 | fit_get_debug (fit, noffset, FIT_TYPE_PROP, len); | |
1668 | *type = -1; | |
1669 | return -1; | |
1670 | } | |
1671 | ||
1672 | /* Translate image type name to id */ | |
1673 | *type = genimg_get_type_id (data); | |
1674 | return 0; | |
1675 | } | |
1676 | ||
1677 | /** | |
1678 | * fit_image_get_comp - get comp id for a given component image node | |
1679 | * @fit: pointer to the FIT format image header | |
1680 | * @noffset: component image node offset | |
1681 | * @comp: pointer to the uint8_t, will hold comp numeric id | |
1682 | * | |
1683 | * fit_image_get_comp() finds comp property in a given component image node. | |
1684 | * If the property is found, its (string) value is translated to the numeric | |
1685 | * id which is returned to the caller. | |
1686 | * | |
1687 | * returns: | |
1688 | * 0, on success | |
1689 | * -1, on failure | |
1690 | */ | |
1691 | int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp) | |
1692 | { | |
1693 | int len; | |
1694 | const void *data; | |
1695 | ||
1696 | /* Get compression name from property data */ | |
1697 | data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len); | |
1698 | if (data == NULL) { | |
1699 | fit_get_debug (fit, noffset, FIT_COMP_PROP, len); | |
1700 | *comp = -1; | |
1701 | return -1; | |
1702 | } | |
1703 | ||
1704 | /* Translate compression name to id */ | |
1705 | *comp = genimg_get_comp_id (data); | |
1706 | return 0; | |
1707 | } | |
1708 | ||
1709 | /** | |
1710 | * fit_image_get_load - get load address property for a given component image node | |
1711 | * @fit: pointer to the FIT format image header | |
1712 | * @noffset: component image node offset | |
1713 | * @load: pointer to the uint32_t, will hold load address | |
1714 | * | |
1715 | * fit_image_get_load() finds load address property in a given component image node. | |
1716 | * If the property is found, its value is returned to the caller. | |
1717 | * | |
1718 | * returns: | |
1719 | * 0, on success | |
1720 | * -1, on failure | |
1721 | */ | |
1722 | int fit_image_get_load (const void *fit, int noffset, ulong *load) | |
1723 | { | |
1724 | int len; | |
1725 | const uint32_t *data; | |
1726 | ||
1727 | data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len); | |
1728 | if (data == NULL) { | |
1729 | fit_get_debug (fit, noffset, FIT_LOAD_PROP, len); | |
1730 | return -1; | |
1731 | } | |
1732 | ||
1733 | *load = uimage_to_cpu (*data); | |
1734 | return 0; | |
1735 | } | |
1736 | ||
1737 | /** | |
1738 | * fit_image_get_entry - get entry point address property for a given component image node | |
1739 | * @fit: pointer to the FIT format image header | |
1740 | * @noffset: component image node offset | |
1741 | * @entry: pointer to the uint32_t, will hold entry point address | |
1742 | * | |
1743 | * fit_image_get_entry() finds entry point address property in a given component image node. | |
1744 | * If the property is found, its value is returned to the caller. | |
1745 | * | |
1746 | * returns: | |
1747 | * 0, on success | |
1748 | * -1, on failure | |
1749 | */ | |
1750 | int fit_image_get_entry (const void *fit, int noffset, ulong *entry) | |
1751 | { | |
1752 | int len; | |
1753 | const uint32_t *data; | |
1754 | ||
1755 | data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len); | |
1756 | if (data == NULL) { | |
1757 | fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len); | |
1758 | return -1; | |
1759 | } | |
1760 | ||
1761 | *entry = uimage_to_cpu (*data); | |
1762 | return 0; | |
1763 | } | |
1764 | ||
1765 | /** | |
1766 | * fit_image_get_data - get data property and its size for a given component image node | |
1767 | * @fit: pointer to the FIT format image header | |
1768 | * @noffset: component image node offset | |
1769 | * @data: double pointer to void, will hold data property's data address | |
1770 | * @size: pointer to size_t, will hold data property's data size | |
1771 | * | |
1772 | * fit_image_get_data() finds data property in a given component image node. | |
1773 | * If the property is found its data start address and size are returned to | |
1774 | * the caller. | |
1775 | * | |
1776 | * returns: | |
1777 | * 0, on success | |
1778 | * -1, on failure | |
1779 | */ | |
1780 | int fit_image_get_data (const void *fit, int noffset, | |
1781 | const void **data, size_t *size) | |
1782 | { | |
1783 | int len; | |
1784 | ||
1785 | *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len); | |
1786 | if (*data == NULL) { | |
1787 | fit_get_debug (fit, noffset, FIT_DATA_PROP, len); | |
1788 | *size = 0; | |
1789 | return -1; | |
1790 | } | |
1791 | ||
1792 | *size = len; | |
1793 | return 0; | |
1794 | } | |
1795 | ||
1796 | /** | |
1797 | * fit_image_hash_get_algo - get hash algorithm name | |
1798 | * @fit: pointer to the FIT format image header | |
1799 | * @noffset: hash node offset | |
1800 | * @algo: double pointer to char, will hold pointer to the algorithm name | |
1801 | * | |
1802 | * fit_image_hash_get_algo() finds hash algorithm property in a given hash node. | |
1803 | * If the property is found its data start address is returned to the caller. | |
1804 | * | |
1805 | * returns: | |
1806 | * 0, on success | |
1807 | * -1, on failure | |
1808 | */ | |
1809 | int fit_image_hash_get_algo (const void *fit, int noffset, char **algo) | |
1810 | { | |
1811 | int len; | |
1812 | ||
1813 | *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len); | |
1814 | if (*algo == NULL) { | |
1815 | fit_get_debug (fit, noffset, FIT_ALGO_PROP, len); | |
1816 | return -1; | |
1817 | } | |
1818 | ||
1819 | return 0; | |
1820 | } | |
1821 | ||
1822 | /** | |
1823 | * fit_image_hash_get_value - get hash value and length | |
1824 | * @fit: pointer to the FIT format image header | |
1825 | * @noffset: hash node offset | |
1826 | * @value: double pointer to uint8_t, will hold address of a hash value data | |
1827 | * @value_len: pointer to an int, will hold hash data length | |
1828 | * | |
1829 | * fit_image_hash_get_value() finds hash value property in a given hash node. | |
1830 | * If the property is found its data start address and size are returned to | |
1831 | * the caller. | |
1832 | * | |
1833 | * returns: | |
1834 | * 0, on success | |
1835 | * -1, on failure | |
1836 | */ | |
1837 | int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value, | |
1838 | int *value_len) | |
1839 | { | |
1840 | int len; | |
1841 | ||
1842 | *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len); | |
1843 | if (*value == NULL) { | |
1844 | fit_get_debug (fit, noffset, FIT_VALUE_PROP, len); | |
1845 | *value_len = 0; | |
1846 | return -1; | |
1847 | } | |
1848 | ||
1849 | *value_len = len; | |
1850 | return 0; | |
1851 | } | |
1852 | ||
1853 | /** | |
1854 | * fit_set_timestamp - set node timestamp property | |
1855 | * @fit: pointer to the FIT format image header | |
1856 | * @noffset: node offset | |
1857 | * @timestamp: timestamp value to be set | |
1858 | * | |
1859 | * fit_set_timestamp() attempts to set timestamp property in the requested | |
1860 | * node and returns operation status to the caller. | |
1861 | * | |
1862 | * returns: | |
1863 | * 0, on success | |
1864 | * -1, on property read failure | |
1865 | */ | |
1866 | int fit_set_timestamp (void *fit, int noffset, time_t timestamp) | |
1867 | { | |
1868 | uint32_t t; | |
1869 | int ret; | |
1870 | ||
1871 | t = cpu_to_uimage (timestamp); | |
1872 | ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t, | |
1873 | sizeof (uint32_t)); | |
1874 | if (ret) { | |
1875 | printf ("Can't set '%s' property for '%s' node (%s)\n", | |
1876 | FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL), | |
1877 | fdt_strerror (ret)); | |
1878 | return -1; | |
1879 | } | |
1880 | ||
1881 | return 0; | |
1882 | } | |
1883 | ||
1884 | /** | |
1885 | * calculate_hash - calculate and return hash for provided input data | |
1886 | * @data: pointer to the input data | |
1887 | * @data_len: data length | |
1888 | * @algo: requested hash algorithm | |
1889 | * @value: pointer to the char, will hold hash value data (caller must | |
1890 | * allocate enough free space) | |
1891 | * value_len: length of the calculated hash | |
1892 | * | |
1893 | * calculate_hash() computes input data hash according to the requested algorithm. | |
1894 | * Resulting hash value is placed in caller provided 'value' buffer, length | |
1895 | * of the calculated hash is returned via value_len pointer argument. | |
1896 | * | |
1897 | * returns: | |
1898 | * 0, on success | |
1899 | * -1, when algo is unsupported | |
1900 | */ | |
1901 | static int calculate_hash (const void *data, int data_len, const char *algo, | |
1902 | uint8_t *value, int *value_len) | |
1903 | { | |
1904 | if (strcmp (algo, "crc32") == 0 ) { | |
7590378f BS |
1905 | *((uint32_t *)value) = crc32_wd (0, data, data_len, |
1906 | CHUNKSZ_CRC32); | |
5dfb5213 MB |
1907 | *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value)); |
1908 | *value_len = 4; | |
1909 | } else if (strcmp (algo, "sha1") == 0 ) { | |
7590378f BS |
1910 | sha1_csum_wd ((unsigned char *) data, data_len, |
1911 | (unsigned char *) value, CHUNKSZ_SHA1); | |
5dfb5213 MB |
1912 | *value_len = 20; |
1913 | } else if (strcmp (algo, "md5") == 0 ) { | |
7590378f | 1914 | md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5); |
766529fc | 1915 | *value_len = 16; |
5dfb5213 MB |
1916 | } else { |
1917 | debug ("Unsupported hash alogrithm\n"); | |
1918 | return -1; | |
1919 | } | |
1920 | return 0; | |
1921 | } | |
1922 | ||
1923 | #ifdef USE_HOSTCC | |
1924 | /** | |
1925 | * fit_set_hashes - process FIT component image nodes and calculate hashes | |
1926 | * @fit: pointer to the FIT format image header | |
1927 | * | |
1928 | * fit_set_hashes() adds hash values for all component images in the FIT blob. | |
1929 | * Hashes are calculated for all component images which have hash subnodes | |
1930 | * with algorithm property set to one of the supported hash algorithms. | |
1931 | * | |
1932 | * returns | |
1933 | * 0, on success | |
1934 | * libfdt error code, on failure | |
1935 | */ | |
1936 | int fit_set_hashes (void *fit) | |
1937 | { | |
1938 | int images_noffset; | |
1939 | int noffset; | |
1940 | int ndepth; | |
1941 | int ret; | |
1942 | ||
1943 | /* Find images parent node offset */ | |
1944 | images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH); | |
1945 | if (images_noffset < 0) { | |
1946 | printf ("Can't find images parent node '%s' (%s)\n", | |
1947 | FIT_IMAGES_PATH, fdt_strerror (images_noffset)); | |
1948 | return images_noffset; | |
1949 | } | |
1950 | ||
1951 | /* Process its subnodes, print out component images details */ | |
1952 | for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth); | |
1953 | (noffset >= 0) && (ndepth > 0); | |
1954 | noffset = fdt_next_node (fit, noffset, &ndepth)) { | |
1955 | if (ndepth == 1) { | |
1956 | /* | |
1957 | * Direct child node of the images parent node, | |
1958 | * i.e. component image node. | |
1959 | */ | |
1960 | ret = fit_image_set_hashes (fit, noffset); | |
1961 | if (ret) | |
1962 | return ret; | |
1963 | } | |
1964 | } | |
1965 | ||
1966 | return 0; | |
1967 | } | |
1968 | ||
1969 | /** | |
1970 | * fit_image_set_hashes - calculate/set hashes for given component image node | |
1971 | * @fit: pointer to the FIT format image header | |
1972 | * @image_noffset: requested component image node | |
1973 | * | |
1974 | * fit_image_set_hashes() adds hash values for an component image node. All | |
1975 | * existing hash subnodes are checked, if algorithm property is set to one of | |
1976 | * the supported hash algorithms, hash value is computed and corresponding | |
1977 | * hash node property is set, for example: | |
1978 | * | |
1979 | * Input component image node structure: | |
1980 | * | |
1981 | * o image@1 (at image_noffset) | |
1982 | * | - data = [binary data] | |
1983 | * o hash@1 | |
1984 | * |- algo = "sha1" | |
1985 | * | |
1986 | * Output component image node structure: | |
1987 | * | |
1988 | * o image@1 (at image_noffset) | |
1989 | * | - data = [binary data] | |
1990 | * o hash@1 | |
1991 | * |- algo = "sha1" | |
1992 | * |- value = sha1(data) | |
1993 | * | |
1994 | * returns: | |
1995 | * 0 on sucess | |
1996 | * <0 on failure | |
1997 | */ | |
1998 | int fit_image_set_hashes (void *fit, int image_noffset) | |
1999 | { | |
2000 | const void *data; | |
2001 | size_t size; | |
2002 | char *algo; | |
2003 | uint8_t value[FIT_MAX_HASH_LEN]; | |
2004 | int value_len; | |
2005 | int noffset; | |
2006 | int ndepth; | |
2007 | ||
2008 | /* Get image data and data length */ | |
2009 | if (fit_image_get_data (fit, image_noffset, &data, &size)) { | |
2010 | printf ("Can't get image data/size\n"); | |
2011 | return -1; | |
2012 | } | |
2013 | ||
2014 | /* Process all hash subnodes of the component image node */ | |
2015 | for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth); | |
2016 | (noffset >= 0) && (ndepth > 0); | |
2017 | noffset = fdt_next_node (fit, noffset, &ndepth)) { | |
2018 | if (ndepth == 1) { | |
2019 | /* Direct child node of the component image node */ | |
2020 | ||
2021 | /* | |
2022 | * Check subnode name, must be equal to "hash". | |
2023 | * Multiple hash nodes require unique unit node | |
2024 | * names, e.g. hash@1, hash@2, etc. | |
2025 | */ | |
2026 | if (strncmp (fit_get_name(fit, noffset, NULL), | |
2027 | FIT_HASH_NODENAME, | |
2028 | strlen(FIT_HASH_NODENAME)) != 0) { | |
2029 | /* Not a hash subnode, skip it */ | |
2030 | continue; | |
2031 | } | |
2032 | ||
2033 | if (fit_image_hash_get_algo (fit, noffset, &algo)) { | |
2034 | printf ("Can't get hash algo property for " | |
2035 | "'%s' hash node in '%s' image node\n", | |
2036 | fit_get_name (fit, noffset, NULL), | |
2037 | fit_get_name (fit, image_noffset, NULL)); | |
2038 | return -1; | |
2039 | } | |
2040 | ||
2041 | if (calculate_hash (data, size, algo, value, &value_len)) { | |
2042 | printf ("Unsupported hash algorithm (%s) for " | |
2043 | "'%s' hash node in '%s' image node\n", | |
2044 | algo, fit_get_name (fit, noffset, NULL), | |
2045 | fit_get_name (fit, image_noffset, NULL)); | |
2046 | return -1; | |
2047 | } | |
2048 | ||
2049 | if (fit_image_hash_set_value (fit, noffset, value, | |
2050 | value_len)) { | |
2051 | printf ("Can't set hash value for " | |
2052 | "'%s' hash node in '%s' image node\n", | |
2053 | fit_get_name (fit, noffset, NULL), | |
2054 | fit_get_name (fit, image_noffset, NULL)); | |
2055 | return -1; | |
2056 | } | |
2057 | } | |
2058 | } | |
2059 | ||
2060 | return 0; | |
2061 | } | |
2062 | ||
2063 | /** | |
2064 | * fit_image_hash_set_value - set hash value in requested has node | |
2065 | * @fit: pointer to the FIT format image header | |
2066 | * @noffset: hash node offset | |
2067 | * @value: hash value to be set | |
2068 | * @value_len: hash value length | |
2069 | * | |
2070 | * fit_image_hash_set_value() attempts to set hash value in a node at offset | |
2071 | * given and returns operation status to the caller. | |
2072 | * | |
2073 | * returns | |
2074 | * 0, on success | |
2075 | * -1, on failure | |
2076 | */ | |
2077 | int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value, | |
2078 | int value_len) | |
2079 | { | |
2080 | int ret; | |
2081 | ||
2082 | ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len); | |
2083 | if (ret) { | |
2084 | printf ("Can't set hash '%s' property for '%s' node (%s)\n", | |
2085 | FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL), | |
2086 | fdt_strerror (ret)); | |
2087 | return -1; | |
2088 | } | |
2089 | ||
2090 | return 0; | |
2091 | } | |
2092 | #endif /* USE_HOSTCC */ | |
2093 | ||
2094 | /** | |
2095 | * fit_image_check_hashes - verify data intergity | |
2096 | * @fit: pointer to the FIT format image header | |
2097 | * @image_noffset: component image node offset | |
2098 | * | |
2099 | * fit_image_check_hashes() goes over component image hash nodes, | |
2100 | * re-calculates each data hash and compares with the value stored in hash | |
2101 | * node. | |
2102 | * | |
2103 | * returns: | |
2104 | * 1, if all hashes are valid | |
2105 | * 0, otherwise (or on error) | |
2106 | */ | |
2107 | int fit_image_check_hashes (const void *fit, int image_noffset) | |
2108 | { | |
2109 | const void *data; | |
2110 | size_t size; | |
2111 | char *algo; | |
2112 | uint8_t *fit_value; | |
2113 | int fit_value_len; | |
2114 | uint8_t value[FIT_MAX_HASH_LEN]; | |
2115 | int value_len; | |
2116 | int noffset; | |
2117 | int ndepth; | |
2118 | char *err_msg = ""; | |
2119 | ||
2120 | /* Get image data and data length */ | |
2121 | if (fit_image_get_data (fit, image_noffset, &data, &size)) { | |
2122 | printf ("Can't get image data/size\n"); | |
2123 | return 0; | |
2124 | } | |
2125 | ||
2126 | /* Process all hash subnodes of the component image node */ | |
2127 | for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth); | |
2128 | (noffset >= 0) && (ndepth > 0); | |
2129 | noffset = fdt_next_node (fit, noffset, &ndepth)) { | |
2130 | if (ndepth == 1) { | |
2131 | /* Direct child node of the component image node */ | |
2132 | ||
2133 | /* | |
2134 | * Check subnode name, must be equal to "hash". | |
2135 | * Multiple hash nodes require unique unit node | |
2136 | * names, e.g. hash@1, hash@2, etc. | |
2137 | */ | |
2138 | if (strncmp (fit_get_name(fit, noffset, NULL), | |
2139 | FIT_HASH_NODENAME, | |
2140 | strlen(FIT_HASH_NODENAME)) != 0) | |
2141 | continue; | |
2142 | ||
2143 | if (fit_image_hash_get_algo (fit, noffset, &algo)) { | |
2144 | err_msg = "Can't get hash algo property"; | |
2145 | goto error; | |
2146 | } | |
2147 | printf ("%s", algo); | |
2148 | ||
2149 | if (fit_image_hash_get_value (fit, noffset, &fit_value, | |
2150 | &fit_value_len)) { | |
2151 | err_msg = "Can't get hash value property"; | |
2152 | goto error; | |
2153 | } | |
2154 | ||
2155 | if (calculate_hash (data, size, algo, value, &value_len)) { | |
2156 | err_msg = "Unsupported hash algorithm"; | |
2157 | goto error; | |
2158 | } | |
2159 | ||
2160 | if (value_len != fit_value_len) { | |
2161 | err_msg = "Bad hash value len"; | |
2162 | goto error; | |
2163 | } else if (memcmp (value, fit_value, value_len) != 0) { | |
2164 | err_msg = "Bad hash value"; | |
2165 | goto error; | |
2166 | } | |
2167 | printf ("+ "); | |
2168 | } | |
2169 | } | |
2170 | ||
2171 | return 1; | |
2172 | ||
2173 | error: | |
2174 | printf ("%s for '%s' hash node in '%s' image node\n", | |
2175 | err_msg, fit_get_name (fit, noffset, NULL), | |
2176 | fit_get_name (fit, image_noffset, NULL)); | |
2177 | return 0; | |
2178 | } | |
2179 | ||
2180 | /** | |
2181 | * fit_image_check_os - check whether image node is of a given os type | |
2182 | * @fit: pointer to the FIT format image header | |
2183 | * @noffset: component image node offset | |
2184 | * @os: requested image os | |
2185 | * | |
2186 | * fit_image_check_os() reads image os property and compares its numeric | |
2187 | * id with the requested os. Comparison result is returned to the caller. | |
2188 | * | |
2189 | * returns: | |
2190 | * 1 if image is of given os type | |
2191 | * 0 otherwise (or on error) | |
2192 | */ | |
2193 | int fit_image_check_os (const void *fit, int noffset, uint8_t os) | |
2194 | { | |
2195 | uint8_t image_os; | |
2196 | ||
2197 | if (fit_image_get_os (fit, noffset, &image_os)) | |
2198 | return 0; | |
2199 | return (os == image_os); | |
2200 | } | |
2201 | ||
2202 | /** | |
2203 | * fit_image_check_arch - check whether image node is of a given arch | |
2204 | * @fit: pointer to the FIT format image header | |
2205 | * @noffset: component image node offset | |
2206 | * @arch: requested imagearch | |
2207 | * | |
2208 | * fit_image_check_arch() reads image arch property and compares its numeric | |
2209 | * id with the requested arch. Comparison result is returned to the caller. | |
2210 | * | |
2211 | * returns: | |
2212 | * 1 if image is of given arch | |
2213 | * 0 otherwise (or on error) | |
2214 | */ | |
2215 | int fit_image_check_arch (const void *fit, int noffset, uint8_t arch) | |
2216 | { | |
2217 | uint8_t image_arch; | |
2218 | ||
2219 | if (fit_image_get_arch (fit, noffset, &image_arch)) | |
2220 | return 0; | |
2221 | return (arch == image_arch); | |
2222 | } | |
2223 | ||
2224 | /** | |
2225 | * fit_image_check_type - check whether image node is of a given type | |
2226 | * @fit: pointer to the FIT format image header | |
2227 | * @noffset: component image node offset | |
2228 | * @type: requested image type | |
2229 | * | |
2230 | * fit_image_check_type() reads image type property and compares its numeric | |
2231 | * id with the requested type. Comparison result is returned to the caller. | |
2232 | * | |
2233 | * returns: | |
2234 | * 1 if image is of given type | |
2235 | * 0 otherwise (or on error) | |
2236 | */ | |
2237 | int fit_image_check_type (const void *fit, int noffset, uint8_t type) | |
2238 | { | |
2239 | uint8_t image_type; | |
2240 | ||
2241 | if (fit_image_get_type (fit, noffset, &image_type)) | |
2242 | return 0; | |
2243 | return (type == image_type); | |
2244 | } | |
2245 | ||
2246 | /** | |
2247 | * fit_image_check_comp - check whether image node uses given compression | |
2248 | * @fit: pointer to the FIT format image header | |
2249 | * @noffset: component image node offset | |
2250 | * @comp: requested image compression type | |
2251 | * | |
2252 | * fit_image_check_comp() reads image compression property and compares its | |
2253 | * numeric id with the requested compression type. Comparison result is | |
2254 | * returned to the caller. | |
2255 | * | |
2256 | * returns: | |
2257 | * 1 if image uses requested compression | |
2258 | * 0 otherwise (or on error) | |
2259 | */ | |
2260 | int fit_image_check_comp (const void *fit, int noffset, uint8_t comp) | |
2261 | { | |
2262 | uint8_t image_comp; | |
2263 | ||
2264 | if (fit_image_get_comp (fit, noffset, &image_comp)) | |
2265 | return 0; | |
2266 | return (comp == image_comp); | |
2267 | } | |
2268 | ||
2269 | /** | |
2270 | * fit_check_format - sanity check FIT image format | |
2271 | * @fit: pointer to the FIT format image header | |
2272 | * | |
2273 | * fit_check_format() runs a basic sanity FIT image verification. | |
2274 | * Routine checks for mandatory properties, nodes, etc. | |
2275 | * | |
2276 | * returns: | |
2277 | * 1, on success | |
2278 | * 0, on failure | |
2279 | */ | |
2280 | int fit_check_format (const void *fit) | |
2281 | { | |
2282 | /* mandatory / node 'description' property */ | |
2283 | if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) { | |
2284 | debug ("Wrong FIT format: no description\n"); | |
2285 | return 0; | |
2286 | } | |
2287 | ||
2288 | #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) | |
2289 | /* mandatory / node 'timestamp' property */ | |
2290 | if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) { | |
2291 | debug ("Wrong FIT format: no description\n"); | |
2292 | return 0; | |
2293 | } | |
2294 | #endif | |
2295 | ||
2296 | /* mandatory subimages parent '/images' node */ | |
2297 | if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) { | |
2298 | debug ("Wrong FIT format: no images parent node\n"); | |
2299 | return 0; | |
2300 | } | |
2301 | ||
2302 | return 1; | |
2303 | } | |
2304 | ||
2305 | /** | |
2306 | * fit_conf_get_node - get node offset for configuration of a given unit name | |
2307 | * @fit: pointer to the FIT format image header | |
2308 | * @conf_uname: configuration node unit name | |
2309 | * | |
2310 | * fit_conf_get_node() finds a configuration (withing the '/configurations' | |
2311 | * parant node) of a provided unit name. If configuration is found its node offset | |
2312 | * is returned to the caller. | |
2313 | * | |
2314 | * When NULL is provided in second argument fit_conf_get_node() will search | |
2315 | * for a default configuration node instead. Default configuration node unit name | |
2316 | * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node. | |
2317 | * | |
2318 | * returns: | |
2319 | * configuration node offset when found (>=0) | |
2320 | * negative number on failure (FDT_ERR_* code) | |
2321 | */ | |
2322 | int fit_conf_get_node (const void *fit, const char *conf_uname) | |
2323 | { | |
2324 | int noffset, confs_noffset; | |
2325 | int len; | |
2326 | ||
2327 | confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH); | |
2328 | if (confs_noffset < 0) { | |
2329 | debug ("Can't find configurations parent node '%s' (%s)\n", | |
2330 | FIT_CONFS_PATH, fdt_strerror (confs_noffset)); | |
2331 | return confs_noffset; | |
2332 | } | |
2333 | ||
2334 | if (conf_uname == NULL) { | |
2335 | /* get configuration unit name from the default property */ | |
2336 | debug ("No configuration specified, trying default...\n"); | |
2337 | conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len); | |
2338 | if (conf_uname == NULL) { | |
2339 | fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len); | |
2340 | return len; | |
2341 | } | |
2342 | debug ("Found default configuration: '%s'\n", conf_uname); | |
2343 | } | |
2344 | ||
2345 | noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname); | |
2346 | if (noffset < 0) { | |
2347 | debug ("Can't get node offset for configuration unit name: '%s' (%s)\n", | |
2348 | conf_uname, fdt_strerror (noffset)); | |
2349 | } | |
2350 | ||
2351 | return noffset; | |
2352 | } | |
2353 | ||
2354 | static int __fit_conf_get_prop_node (const void *fit, int noffset, | |
2355 | const char *prop_name) | |
2356 | { | |
2357 | char *uname; | |
2358 | int len; | |
2359 | ||
2360 | /* get kernel image unit name from configuration kernel property */ | |
2361 | uname = (char *)fdt_getprop (fit, noffset, prop_name, &len); | |
2362 | if (uname == NULL) | |
2363 | return len; | |
2364 | ||
2365 | return fit_image_get_node (fit, uname); | |
2366 | } | |
2367 | ||
2368 | /** | |
2369 | * fit_conf_get_kernel_node - get kernel image node offset that corresponds to | |
2370 | * a given configuration | |
2371 | * @fit: pointer to the FIT format image header | |
2372 | * @noffset: configuration node offset | |
2373 | * | |
2374 | * fit_conf_get_kernel_node() retrives kernel image node unit name from | |
2375 | * configuration FIT_KERNEL_PROP property and translates it to the node | |
2376 | * offset. | |
2377 | * | |
2378 | * returns: | |
2379 | * image node offset when found (>=0) | |
2380 | * negative number on failure (FDT_ERR_* code) | |
2381 | */ | |
2382 | int fit_conf_get_kernel_node (const void *fit, int noffset) | |
2383 | { | |
2384 | return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP); | |
2385 | } | |
2386 | ||
2387 | /** | |
2388 | * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to | |
2389 | * a given configuration | |
2390 | * @fit: pointer to the FIT format image header | |
2391 | * @noffset: configuration node offset | |
2392 | * | |
2393 | * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from | |
2394 | * configuration FIT_KERNEL_PROP property and translates it to the node | |
2395 | * offset. | |
2396 | * | |
2397 | * returns: | |
2398 | * image node offset when found (>=0) | |
2399 | * negative number on failure (FDT_ERR_* code) | |
2400 | */ | |
2401 | int fit_conf_get_ramdisk_node (const void *fit, int noffset) | |
2402 | { | |
2403 | return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP); | |
2404 | } | |
2405 | ||
2406 | /** | |
2407 | * fit_conf_get_fdt_node - get fdt image node offset that corresponds to | |
2408 | * a given configuration | |
2409 | * @fit: pointer to the FIT format image header | |
2410 | * @noffset: configuration node offset | |
2411 | * | |
2412 | * fit_conf_get_fdt_node() retrives fdt image node unit name from | |
2413 | * configuration FIT_KERNEL_PROP property and translates it to the node | |
2414 | * offset. | |
2415 | * | |
2416 | * returns: | |
2417 | * image node offset when found (>=0) | |
2418 | * negative number on failure (FDT_ERR_* code) | |
2419 | */ | |
2420 | int fit_conf_get_fdt_node (const void *fit, int noffset) | |
2421 | { | |
2422 | return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP); | |
2423 | } | |
d5934ad7 | 2424 | |
5dfb5213 MB |
2425 | /** |
2426 | * fit_conf_print - prints out the FIT configuration details | |
2427 | * @fit: pointer to the FIT format image header | |
f773bea8 | 2428 | * @noffset: offset of the configuration node |
5dfb5213 MB |
2429 | * @p: pointer to prefix string |
2430 | * | |
2431 | * fit_conf_print() lists all mandatory properies for the processed | |
2432 | * configuration node. | |
2433 | * | |
2434 | * returns: | |
2435 | * no returned results | |
2436 | */ | |
2437 | void fit_conf_print (const void *fit, int noffset, const char *p) | |
2438 | { | |
2439 | char *desc; | |
2440 | char *uname; | |
2441 | int ret; | |
2442 | ||
2443 | /* Mandatory properties */ | |
2444 | ret = fit_get_desc (fit, noffset, &desc); | |
2445 | printf ("%s Description: ", p); | |
2446 | if (ret) | |
2447 | printf ("unavailable\n"); | |
2448 | else | |
2449 | printf ("%s\n", desc); | |
2450 | ||
2451 | uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL); | |
2452 | printf ("%s Kernel: ", p); | |
2453 | if (uname == NULL) | |
2454 | printf ("unavailable\n"); | |
2455 | else | |
2456 | printf ("%s\n", uname); | |
2457 | ||
2458 | /* Optional properties */ | |
2459 | uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL); | |
2460 | if (uname) | |
2461 | printf ("%s Init Ramdisk: %s\n", p, uname); | |
2462 | ||
2463 | uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL); | |
2464 | if (uname) | |
2465 | printf ("%s FDT: %s\n", p, uname); | |
2466 | } | |
c8779648 MB |
2467 | |
2468 | /** | |
2469 | * fit_check_ramdisk - verify FIT format ramdisk subimage | |
2470 | * @fit_hdr: pointer to the FIT ramdisk header | |
2471 | * @rd_noffset: ramdisk subimage node offset within FIT image | |
2472 | * @arch: requested ramdisk image architecture type | |
2473 | * @verify: data CRC verification flag | |
2474 | * | |
2475 | * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from | |
2476 | * specified FIT image. | |
2477 | * | |
2478 | * returns: | |
2479 | * 1, on success | |
2480 | * 0, on failure | |
2481 | */ | |
2482 | #ifndef USE_HOSTCC | |
2483 | static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify) | |
2484 | { | |
2485 | fit_image_print (fit, rd_noffset, " "); | |
2486 | ||
2487 | if (verify) { | |
2488 | puts (" Verifying Hash Integrity ... "); | |
2489 | if (!fit_image_check_hashes (fit, rd_noffset)) { | |
2490 | puts ("Bad Data Hash\n"); | |
1372cce2 | 2491 | show_boot_progress (-125); |
c8779648 MB |
2492 | return 0; |
2493 | } | |
2494 | puts ("OK\n"); | |
2495 | } | |
2496 | ||
1372cce2 | 2497 | show_boot_progress (126); |
c8779648 MB |
2498 | if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) || |
2499 | !fit_image_check_arch (fit, rd_noffset, arch) || | |
2500 | !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) { | |
2501 | printf ("No Linux %s Ramdisk Image\n", | |
2502 | genimg_get_arch_name(arch)); | |
1372cce2 | 2503 | show_boot_progress (-126); |
c8779648 MB |
2504 | return 0; |
2505 | } | |
2506 | ||
1372cce2 | 2507 | show_boot_progress (127); |
c8779648 MB |
2508 | return 1; |
2509 | } | |
2510 | #endif /* USE_HOSTCC */ | |
f50433d6 | 2511 | #endif /* CONFIG_FIT */ |