Root/qiboot/include/image.h

1/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2005
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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 *
25 ********************************************************************
26 * NOTE: This header file defines an interface to U-Boot. Including
27 * this (unmodified) header file in another file is considered normal
28 * use of U-Boot, and does *not* fall under the heading of "derived
29 * work".
30 ********************************************************************
31 */
32
33#ifndef __IMAGE_H__
34#define __IMAGE_H__
35
36/*
37 * Operating System Codes
38 */
39#define IH_OS_INVALID 0 /* Invalid OS */
40#define IH_OS_OPENBSD 1 /* OpenBSD */
41#define IH_OS_NETBSD 2 /* NetBSD */
42#define IH_OS_FREEBSD 3 /* FreeBSD */
43#define IH_OS_4_4BSD 4 /* 4.4BSD */
44#define IH_OS_LINUX 5 /* Linux */
45#define IH_OS_SVR4 6 /* SVR4 */
46#define IH_OS_ESIX 7 /* Esix */
47#define IH_OS_SOLARIS 8 /* Solaris */
48#define IH_OS_IRIX 9 /* Irix */
49#define IH_OS_SCO 10 /* SCO */
50#define IH_OS_DELL 11 /* Dell */
51#define IH_OS_NCR 12 /* NCR */
52#define IH_OS_LYNXOS 13 /* LynxOS */
53#define IH_OS_VXWORKS 14 /* VxWorks */
54#define IH_OS_PSOS 15 /* pSOS */
55#define IH_OS_QNX 16 /* QNX */
56#define IH_OS_U_BOOT 17 /* Firmware */
57#define IH_OS_RTEMS 18 /* RTEMS */
58#define IH_OS_ARTOS 19 /* ARTOS */
59#define IH_OS_UNITY 20 /* Unity OS */
60
61/*
62 * CPU Architecture Codes (supported by Linux)
63 */
64#define IH_ARCH_INVALID 0 /* Invalid CPU */
65#define IH_ARCH_ALPHA 1 /* Alpha */
66#define IH_ARCH_ARM 2 /* ARM */
67#define IH_ARCH_I386 3 /* Intel x86 */
68#define IH_ARCH_IA64 4 /* IA64 */
69#define IH_ARCH_MIPS 5 /* MIPS */
70#define IH_ARCH_MIPS64 6 /* MIPS 64 Bit */
71#define IH_ARCH_PPC 7 /* PowerPC */
72#define IH_ARCH_S390 8 /* IBM S390 */
73#define IH_ARCH_SH 9 /* SuperH */
74#define IH_ARCH_SPARC 10 /* Sparc */
75#define IH_ARCH_SPARC64 11 /* Sparc 64 Bit */
76#define IH_ARCH_M68K 12 /* M68K */
77#define IH_ARCH_NIOS 13 /* Nios-32 */
78#define IH_ARCH_MICROBLAZE 14 /* MicroBlaze */
79#define IH_ARCH_NIOS2 15 /* Nios-II */
80#define IH_ARCH_BLACKFIN 16 /* Blackfin */
81#define IH_ARCH_AVR32 17 /* AVR32 */
82#define IH_ARCH_ST200 18 /* STMicroelectronics ST200 */
83
84/*
85 * Image Types
86 *
87 * "Standalone Programs" are directly runnable in the environment
88 * provided by U-Boot; it is expected that (if they behave
89 * well) you can continue to work in U-Boot after return from
90 * the Standalone Program.
91 * "OS Kernel Images" are usually images of some Embedded OS which
92 * will take over control completely. Usually these programs
93 * will install their own set of exception handlers, device
94 * drivers, set up the MMU, etc. - this means, that you cannot
95 * expect to re-enter U-Boot except by resetting the CPU.
96 * "RAMDisk Images" are more or less just data blocks, and their
97 * parameters (address, size) are passed to an OS kernel that is
98 * being started.
99 * "Multi-File Images" contain several images, typically an OS
100 * (Linux) kernel image and one or more data images like
101 * RAMDisks. This construct is useful for instance when you want
102 * to boot over the network using BOOTP etc., where the boot
103 * server provides just a single image file, but you want to get
104 * for instance an OS kernel and a RAMDisk image.
105 *
106 * "Multi-File Images" start with a list of image sizes, each
107 * image size (in bytes) specified by an "uint32_t" in network
108 * byte order. This list is terminated by an "(uint32_t)0".
109 * Immediately after the terminating 0 follow the images, one by
110 * one, all aligned on "uint32_t" boundaries (size rounded up to
111 * a multiple of 4 bytes - except for the last file).
112 *
113 * "Firmware Images" are binary images containing firmware (like
114 * U-Boot or FPGA images) which usually will be programmed to
115 * flash memory.
116 *
117 * "Script files" are command sequences that will be executed by
118 * U-Boot's command interpreter; this feature is especially
119 * useful when you configure U-Boot to use a real shell (hush)
120 * as command interpreter (=> Shell Scripts).
121 */
122
123#define IH_TYPE_INVALID 0 /* Invalid Image */
124#define IH_TYPE_STANDALONE 1 /* Standalone Program */
125#define IH_TYPE_KERNEL 2 /* OS Kernel Image */
126#define IH_TYPE_RAMDISK 3 /* RAMDisk Image */
127#define IH_TYPE_MULTI 4 /* Multi-File Image */
128#define IH_TYPE_FIRMWARE 5 /* Firmware Image */
129#define IH_TYPE_SCRIPT 6 /* Script file */
130#define IH_TYPE_FILESYSTEM 7 /* Filesystem Image (any type) */
131#define IH_TYPE_FLATDT 8 /* Binary Flat Device Tree Blob */
132
133/*
134 * Compression Types
135 */
136#define IH_COMP_NONE 0 /* No Compression Used */
137#define IH_COMP_GZIP 1 /* gzip Compression Used */
138#define IH_COMP_BZIP2 2 /* bzip2 Compression Used */
139
140#define IH_MAGIC 0x27051956 /* Image Magic Number */
141#define IH_NMLEN 32 /* Image Name Length */
142
143/*
144 * Legacy format image header,
145 * all data in network byte order (aka natural aka bigendian).
146 */
147typedef struct image_header {
148    uint32_t ih_magic; /* Image Header Magic Number */
149    uint32_t ih_hcrc; /* Image Header CRC Checksum */
150    uint32_t ih_time; /* Image Creation Timestamp */
151    uint32_t ih_size; /* Image Data Size */
152    uint32_t ih_load; /* Data Load Address */
153    uint32_t ih_ep; /* Entry Point Address */
154    uint32_t ih_dcrc; /* Image Data CRC Checksum */
155    uint8_t ih_os; /* Operating System */
156    uint8_t ih_arch; /* CPU architecture */
157    uint8_t ih_type; /* Image Type */
158    uint8_t ih_comp; /* Compression Type */
159    uint8_t ih_name[IH_NMLEN]; /* Image Name */
160} image_header_t;
161
162/*
163 * Legacy and FIT format headers used by do_bootm() and do_bootm_<os>()
164 * routines.
165 */
166typedef struct bootm_headers {
167    /*
168     * Legacy os image header, if it is a multi component image
169     * then boot_get_ramdisk() and get_fdt() will attempt to get
170     * data from second and third component accordingly.
171     */
172    image_header_t *legacy_hdr_os;
173    ulong legacy_hdr_valid;
174
175#if defined(CONFIG_FIT)
176    const char *fit_uname_cfg; /* configuration node unit name */
177
178    void *fit_hdr_os; /* os FIT image header */
179    const char *fit_uname_os; /* os subimage node unit name */
180    int fit_noffset_os; /* os subimage node offset */
181
182    void *fit_hdr_rd; /* init ramdisk FIT image header */
183    const char *fit_uname_rd; /* init ramdisk subimage node unit name */
184    int fit_noffset_rd; /* init ramdisk subimage node offset */
185
186#if defined(CONFIG_PPC)
187    void *fit_hdr_fdt; /* FDT blob FIT image header */
188    const char *fit_uname_fdt; /* FDT blob subimage node unit name */
189    int fit_noffset_fdt;/* FDT blob subimage node offset */
190#endif
191#endif
192
193    int verify; /* getenv("verify")[0] != 'n' */
194    int autostart; /* getenv("autostart")[0] != 'n' */
195    struct lmb *lmb; /* for memory mgmt */
196} bootm_headers_t;
197
198/*
199 * Some systems (for example LWMON) have very short watchdog periods;
200 * we must make sure to split long operations like memmove() or
201 * crc32() into reasonable chunks.
202 */
203#define CHUNKSZ (64 * 1024)
204
205#define uimage_to_cpu(x) __be32_to_cpu(x)
206#define cpu_to_uimage(x) __cpu_to_be32(x)
207
208const char *genimg_get_os_name (uint8_t os);
209const char *genimg_get_arch_name (uint8_t arch);
210const char *genimg_get_type_name (uint8_t type);
211const char *genimg_get_comp_name (uint8_t comp);
212int genimg_get_os_id (const char *name);
213int genimg_get_arch_id (const char *name);
214int genimg_get_type_id (const char *name);
215int genimg_get_comp_id (const char *name);
216
217#ifndef USE_HOSTCC
218/* Image format types, returned by _get_format() routine */
219#define IMAGE_FORMAT_INVALID 0x00
220#define IMAGE_FORMAT_LEGACY 0x01 /* legacy image_header based format */
221#define IMAGE_FORMAT_FIT 0x02 /* new, libfdt based format */
222
223int genimg_get_format (void *img_addr);
224int genimg_has_config (bootm_headers_t *images);
225ulong genimg_get_image (ulong img_addr);
226
227int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
228        uint8_t arch, ulong *rd_start, ulong *rd_end);
229
230#if defined(CONFIG_PPC) || defined(CONFIG_M68K)
231int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
232          ulong *initrd_start, ulong *initrd_end);
233
234int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
235            ulong bootmap_base);
236int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base);
237#endif /* CONFIG_PPC || CONFIG_M68K */
238#endif /* !USE_HOSTCC */
239
240/*******************************************************************/
241/* Legacy format specific code (prefixed with image_) */
242/*******************************************************************/
243static inline uint32_t image_get_header_size (void)
244{
245    return (sizeof (image_header_t));
246}
247
248#define image_get_hdr_l(f) \
249    static inline uint32_t image_get_##f(image_header_t *hdr) \
250    { \
251        return uimage_to_cpu (hdr->ih_##f); \
252    }
253image_get_hdr_l (magic);
254image_get_hdr_l (hcrc);
255image_get_hdr_l (time);
256image_get_hdr_l (size);
257image_get_hdr_l (load);
258image_get_hdr_l (ep);
259image_get_hdr_l (dcrc);
260
261#define image_get_hdr_b(f) \
262    static inline uint8_t image_get_##f(image_header_t *hdr) \
263    { \
264        return hdr->ih_##f; \
265    }
266image_get_hdr_b (os);
267image_get_hdr_b (arch);
268image_get_hdr_b (type);
269image_get_hdr_b (comp);
270
271static inline char *image_get_name (image_header_t *hdr)
272{
273    return (char *)hdr->ih_name;
274}
275
276static inline uint32_t image_get_data_size (image_header_t *hdr)
277{
278    return image_get_size (hdr);
279}
280
281/**
282 * image_get_data - get image payload start address
283 * @hdr: image header
284 *
285 * image_get_data() returns address of the image payload. For single
286 * component images it is image data start. For multi component
287 * images it points to the null terminated table of sub-images sizes.
288 *
289 * returns:
290 * image payload data start address
291 */
292static inline ulong image_get_data (image_header_t *hdr)
293{
294    return ((ulong)hdr + image_get_header_size ());
295}
296
297static inline uint32_t image_get_image_size (image_header_t *hdr)
298{
299    return (image_get_size (hdr) + image_get_header_size ());
300}
301static inline ulong image_get_image_end (image_header_t *hdr)
302{
303    return ((ulong)hdr + image_get_image_size (hdr));
304}
305
306#define image_set_hdr_l(f) \
307    static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
308    { \
309        hdr->ih_##f = cpu_to_uimage (val); \
310    }
311image_set_hdr_l (magic);
312image_set_hdr_l (hcrc);
313image_set_hdr_l (time);
314image_set_hdr_l (size);
315image_set_hdr_l (load);
316image_set_hdr_l (ep);
317image_set_hdr_l (dcrc);
318
319#define image_set_hdr_b(f) \
320    static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
321    { \
322        hdr->ih_##f = val; \
323    }
324image_set_hdr_b (os);
325image_set_hdr_b (arch);
326image_set_hdr_b (type);
327image_set_hdr_b (comp);
328
329static inline void image_set_name (image_header_t *hdr, const char *name)
330{
331    strncpy (image_get_name (hdr), name, IH_NMLEN);
332}
333
334int image_check_hcrc (image_header_t *hdr);
335int image_check_dcrc (image_header_t *hdr);
336#ifndef USE_HOSTCC
337int image_check_dcrc_wd (image_header_t *hdr, ulong chunksize);
338int getenv_verify (void);
339int getenv_autostart (void);
340ulong getenv_bootm_low(void);
341ulong getenv_bootm_size(void);
342void memmove_wd (void *to, void *from, size_t len, ulong chunksz);
343#endif
344
345static inline int image_check_magic (image_header_t *hdr)
346{
347    return (image_get_magic (hdr) == IH_MAGIC);
348}
349static inline int image_check_type (image_header_t *hdr, uint8_t type)
350{
351    return (image_get_type (hdr) == type);
352}
353static inline int image_check_arch (image_header_t *hdr, uint8_t arch)
354{
355    return (image_get_arch (hdr) == arch);
356}
357static inline int image_check_os (image_header_t *hdr, uint8_t os)
358{
359    return (image_get_os (hdr) == os);
360}
361
362ulong image_multi_count (image_header_t *hdr);
363void image_multi_getimg (image_header_t *hdr, ulong idx,
364            ulong *data, ulong *len);
365
366inline void image_print_contents (image_header_t *hdr);
367inline void image_print_contents_noindent (image_header_t *hdr);
368
369#ifndef USE_HOSTCC
370static inline int image_check_target_arch (image_header_t *hdr)
371{
372#if defined(__ARM__)
373    if (!image_check_arch (hdr, IH_ARCH_ARM))
374#elif defined(__avr32__)
375    if (!image_check_arch (hdr, IH_ARCH_AVR32))
376#elif defined(__bfin__)
377    if (!image_check_arch (hdr, IH_ARCH_BLACKFIN))
378#elif defined(__I386__)
379    if (!image_check_arch (hdr, IH_ARCH_I386))
380#elif defined(__M68K__)
381    if (!image_check_arch (hdr, IH_ARCH_M68K))
382#elif defined(__microblaze__)
383    if (!image_check_arch (hdr, IH_ARCH_MICROBLAZE))
384#elif defined(__mips__)
385    if (!image_check_arch (hdr, IH_ARCH_MIPS))
386#elif defined(__nios__)
387    if (!image_check_arch (hdr, IH_ARCH_NIOS))
388#elif defined(__nios2__)
389    if (!image_check_arch (hdr, IH_ARCH_NIOS2))
390#elif defined(__PPC__)
391    if (!image_check_arch (hdr, IH_ARCH_PPC))
392#elif defined(__sh__)
393    if (!image_check_arch (hdr, IH_ARCH_SH))
394#else
395# error Unknown CPU type
396#endif
397        return 0;
398
399    return 1;
400}
401#endif /* USE_HOSTCC */
402
403/*******************************************************************/
404/* New uImage format specific code (prefixed with fit_) */
405/*******************************************************************/
406#if defined(CONFIG_FIT)
407
408#define FIT_IMAGES_PATH "/images"
409#define FIT_CONFS_PATH "/configurations"
410
411/* hash node */
412#define FIT_HASH_NODENAME "hash"
413#define FIT_ALGO_PROP "algo"
414#define FIT_VALUE_PROP "value"
415
416/* image node */
417#define FIT_DATA_PROP "data"
418#define FIT_TIMESTAMP_PROP "timestamp"
419#define FIT_DESC_PROP "description"
420#define FIT_ARCH_PROP "arch"
421#define FIT_TYPE_PROP "type"
422#define FIT_OS_PROP "os"
423#define FIT_COMP_PROP "compression"
424#define FIT_ENTRY_PROP "entry"
425#define FIT_LOAD_PROP "load"
426
427/* configuration node */
428#define FIT_KERNEL_PROP "kernel"
429#define FIT_RAMDISK_PROP "ramdisk"
430#define FIT_FDT_PROP "fdt"
431#define FIT_DEFAULT_PROP "default"
432
433#define FIT_MAX_HASH_LEN 20 /* max(crc32_len(4), sha1_len(20)) */
434
435/* cmdline argument format parsing */
436inline int fit_parse_conf (const char *spec, ulong addr_curr,
437        ulong *addr, const char **conf_name);
438inline int fit_parse_subimage (const char *spec, ulong addr_curr,
439        ulong *addr, const char **image_name);
440
441inline void fit_print_contents (const void *fit);
442inline void fit_print_contents_noindent (const void *fit);
443void fit_image_print (const void *fit, int noffset, const char *p);
444void fit_image_print_hash (const void *fit, int noffset, const char *p);
445
446/**
447 * fit_get_end - get FIT image size
448 * @fit: pointer to the FIT format image header
449 *
450 * returns:
451 * size of the FIT image (blob) in memory
452 */
453static inline ulong fit_get_size (const void *fit)
454{
455    return fdt_totalsize (fit);
456}
457
458/**
459 * fit_get_end - get FIT image end
460 * @fit: pointer to the FIT format image header
461 *
462 * returns:
463 * end address of the FIT image (blob) in memory
464 */
465static inline ulong fit_get_end (const void *fit)
466{
467    return (ulong)fit + fdt_totalsize (fit);
468}
469
470/**
471 * fit_get_name - get FIT node name
472 * @fit: pointer to the FIT format image header
473 *
474 * returns:
475 * NULL, on error
476 * pointer to node name, on success
477 */
478static inline const char *fit_get_name (const void *fit_hdr,
479        int noffset, int *len)
480{
481    return fdt_get_name (fit_hdr, noffset, len);
482}
483
484int fit_get_desc (const void *fit, int noffset, char **desc);
485int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp);
486
487int fit_image_get_node (const void *fit, const char *image_uname);
488int fit_image_get_os (const void *fit, int noffset, uint8_t *os);
489int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch);
490int fit_image_get_type (const void *fit, int noffset, uint8_t *type);
491int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp);
492int fit_image_get_load (const void *fit, int noffset, ulong *load);
493int fit_image_get_entry (const void *fit, int noffset, ulong *entry);
494int fit_image_get_data (const void *fit, int noffset,
495                const void **data, size_t *size);
496
497int fit_image_hash_get_algo (const void *fit, int noffset, char **algo);
498int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
499                int *value_len);
500
501int fit_set_timestamp (void *fit, int noffset, time_t timestamp);
502int fit_set_hashes (void *fit);
503int fit_image_set_hashes (void *fit, int image_noffset);
504int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
505                int value_len);
506
507int fit_image_check_hashes (const void *fit, int noffset);
508int fit_image_check_os (const void *fit, int noffset, uint8_t os);
509int fit_image_check_arch (const void *fit, int noffset, uint8_t arch);
510int fit_image_check_type (const void *fit, int noffset, uint8_t type);
511int fit_image_check_comp (const void *fit, int noffset, uint8_t comp);
512int fit_check_format (const void *fit);
513
514int fit_conf_get_node (const void *fit, const char *conf_uname);
515int fit_conf_get_kernel_node (const void *fit, int noffset);
516int fit_conf_get_ramdisk_node (const void *fit, int noffset);
517int fit_conf_get_fdt_node (const void *fit, int noffset);
518
519void fit_conf_print (const void *fit, int noffset, const char *p);
520
521#ifndef USE_HOSTCC
522static inline int fit_image_check_target_arch (const void *fdt, int node)
523{
524#if defined(__ARM__)
525    if (!fit_image_check_arch (fdt, node, IH_ARCH_ARM))
526#elif defined(__avr32__)
527    if (!fit_image_check_arch (fdt, node, IH_ARCH_AVR32))
528#elif defined(__bfin__)
529    if (!fit_image_check_arch (fdt, node, IH_ARCH_BLACKFIN))
530#elif defined(__I386__)
531    if (!fit_image_check_arch (fdt, node, IH_ARCH_I386))
532#elif defined(__M68K__)
533    if (!fit_image_check_arch (fdt, node, IH_ARCH_M68K))
534#elif defined(__microblaze__)
535    if (!fit_image_check_arch (fdt, node, IH_ARCH_MICROBLAZE))
536#elif defined(__mips__)
537    if (!fit_image_check_arch (fdt, node, IH_ARCH_MIPS))
538#elif defined(__nios__)
539    if (!fit_image_check_arch (fdt, node, IH_ARCH_NIOS))
540#elif defined(__nios2__)
541    if (!fit_image_check_arch (fdt, node, IH_ARCH_NIOS2))
542#elif defined(__PPC__)
543    if (!fit_image_check_arch (fdt, node, IH_ARCH_PPC))
544#elif defined(__sh__)
545    if (!fit_image_check_arch (fdt, node, IH_ARCH_SH))
546#else
547# error Unknown CPU type
548#endif
549        return 0;
550
551    return 1;
552}
553#endif /* USE_HOSTCC */
554
555#ifdef CONFIG_FIT_VERBOSE
556#define fit_unsupported(msg) printf ("! %s:%d " \
557                "FIT images not supported for '%s'\n", \
558                __FILE__, __LINE__, (msg))
559
560#define fit_unsupported_reset(msg) printf ("! %s:%d " \
561                "FIT images not supported for '%s' " \
562                "- must reset board to recover!\n", \
563                __FILE__, __LINE__, (msg))
564#else
565#define fit_unsupported(msg)
566#define fit_unsupported_reset(msg)
567#endif /* CONFIG_FIT_VERBOSE */
568#endif /* CONFIG_FIT */
569
570#endif /* __IMAGE_H__ */
571

Archive Download this file



interactive