xref: /qemu/include/hw/loader.h (revision 061923298fe34e1bf5f32006f8d725a547fc4118)
1ca20cf32SBlue Swirl #ifndef LOADER_H
2ca20cf32SBlue Swirl #define LOADER_H
3a88b362cSLaszlo Ersek #include "hw/nvram/fw_cfg.h"
4ca20cf32SBlue Swirl 
5ca20cf32SBlue Swirl /* loader.c */
6a1483f88SMichael S. Tsirkin /**
7a1483f88SMichael S. Tsirkin  * get_image_size: retrieve size of an image file
8a1483f88SMichael S. Tsirkin  * @filename: Path to the image file
9a1483f88SMichael S. Tsirkin  *
10a1483f88SMichael S. Tsirkin  * Returns the size of the image file on success, -1 otherwise.
11a1483f88SMichael S. Tsirkin  * On error, errno is also set as appropriate.
12a1483f88SMichael S. Tsirkin  */
13f3839fdaSLi Zhijian int64_t get_image_size(const char *filename);
14*06192329SPeter Maydell /**
15*06192329SPeter Maydell  * load_image_size: load an image file into specified buffer
16*06192329SPeter Maydell  * @filename: Path to the image file
17*06192329SPeter Maydell  * @addr: Buffer to load image into
18*06192329SPeter Maydell  * @size: Size of buffer in bytes
19*06192329SPeter Maydell  *
20*06192329SPeter Maydell  * Load an image file from disk into the specified buffer.
21*06192329SPeter Maydell  * If the image is larger than the specified buffer, only
22*06192329SPeter Maydell  * @size bytes are read (this is not considered an error).
23*06192329SPeter Maydell  *
24*06192329SPeter Maydell  * Prefer to use the GLib function g_file_get_contents() rather
25*06192329SPeter Maydell  * than a "get_image_size()/g_malloc()/load_image_size()" sequence.
26*06192329SPeter Maydell  *
27*06192329SPeter Maydell  * Returns the number of bytes read, or -1 on error. On error,
28*06192329SPeter Maydell  * errno is also set as appropriate.
29*06192329SPeter Maydell  */
30ea87616dSBenjamin Herrenschmidt ssize_t load_image_size(const char *filename, void *addr, size_t size);
3193ffc7c7SAlistair Francis 
3293ffc7c7SAlistair Francis /**load_image_targphys_as:
3393ffc7c7SAlistair Francis  * @filename: Path to the image file
3493ffc7c7SAlistair Francis  * @addr: Address to load the image to
3593ffc7c7SAlistair Francis  * @max_sz: The maximum size of the image to load
3693ffc7c7SAlistair Francis  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
3793ffc7c7SAlistair Francis  *      is used if nothing is supplied here.
3893ffc7c7SAlistair Francis  *
3993ffc7c7SAlistair Francis  * Load a fixed image into memory.
4093ffc7c7SAlistair Francis  *
4193ffc7c7SAlistair Francis  * Returns the size of the loaded image on success, -1 otherwise.
4293ffc7c7SAlistair Francis  */
4393ffc7c7SAlistair Francis int load_image_targphys_as(const char *filename,
4493ffc7c7SAlistair Francis                            hwaddr addr, uint64_t max_sz, AddressSpace *as);
4593ffc7c7SAlistair Francis 
46e4a25ed9SSu Hang /**load_targphys_hex_as:
47e4a25ed9SSu Hang  * @filename: Path to the .hex file
48e4a25ed9SSu Hang  * @entry: Store the entry point given by the .hex file
49e4a25ed9SSu Hang  * @as: The AddressSpace to load the .hex file to. The value of
50e4a25ed9SSu Hang  *      address_space_memory is used if nothing is supplied here.
51e4a25ed9SSu Hang  *
52e4a25ed9SSu Hang  * Load a fixed .hex file into memory.
53e4a25ed9SSu Hang  *
54e4a25ed9SSu Hang  * Returns the size of the loaded .hex file on success, -1 otherwise.
55e4a25ed9SSu Hang  */
56e4a25ed9SSu Hang int load_targphys_hex_as(const char *filename, hwaddr *entry, AddressSpace *as);
57e4a25ed9SSu Hang 
5893ffc7c7SAlistair Francis /** load_image_targphys:
5993ffc7c7SAlistair Francis  * Same as load_image_targphys_as(), but doesn't allow the caller to specify
6093ffc7c7SAlistair Francis  * an AddressSpace.
6193ffc7c7SAlistair Francis  */
62a8170e5eSAvi Kivity int load_image_targphys(const char *filename, hwaddr,
6380a2ba3dSMark Langsdorf                         uint64_t max_sz);
6493ffc7c7SAlistair Francis 
6576151cacSPeter Maydell /**
6676151cacSPeter Maydell  * load_image_mr: load an image into a memory region
6776151cacSPeter Maydell  * @filename: Path to the image file
6876151cacSPeter Maydell  * @mr: Memory Region to load into
6976151cacSPeter Maydell  *
7076151cacSPeter Maydell  * Load the specified file into the memory region.
7176151cacSPeter Maydell  * The file loaded is registered as a ROM, so its contents will be
7276151cacSPeter Maydell  * reinstated whenever the system is reset.
7376151cacSPeter Maydell  * If the file is larger than the memory region's size the call will fail.
7476151cacSPeter Maydell  * Returns -1 on failure, or the size of the file.
7576151cacSPeter Maydell  */
7676151cacSPeter Maydell int load_image_mr(const char *filename, MemoryRegion *mr);
777d48a0f7SLaszlo Ersek 
787d48a0f7SLaszlo Ersek /* This is the limit on the maximum uncompressed image size that
797d48a0f7SLaszlo Ersek  * load_image_gzipped_buffer() and load_image_gzipped() will read. It prevents
807d48a0f7SLaszlo Ersek  * g_malloc() in those functions from allocating a huge amount of memory.
817d48a0f7SLaszlo Ersek  */
827d48a0f7SLaszlo Ersek #define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20)
837d48a0f7SLaszlo Ersek 
847d48a0f7SLaszlo Ersek int load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
857d48a0f7SLaszlo Ersek                               uint8_t **buffer);
86235e74afSRichard W.M. Jones int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz);
8718674b26SAlexey Kardashevskiy 
8818674b26SAlexey Kardashevskiy #define ELF_LOAD_FAILED       -1
8918674b26SAlexey Kardashevskiy #define ELF_LOAD_NOT_ELF      -2
9018674b26SAlexey Kardashevskiy #define ELF_LOAD_WRONG_ARCH   -3
9118674b26SAlexey Kardashevskiy #define ELF_LOAD_WRONG_ENDIAN -4
9218674b26SAlexey Kardashevskiy const char *load_elf_strerror(int error);
93140b7ce5SPeter Crosthwaite 
94a2480ffaSMichael Clark /** load_elf_ram_sym:
95140b7ce5SPeter Crosthwaite  * @filename: Path of ELF file
96140b7ce5SPeter Crosthwaite  * @translate_fn: optional function to translate load addresses
97140b7ce5SPeter Crosthwaite  * @translate_opaque: opaque data passed to @translate_fn
98140b7ce5SPeter Crosthwaite  * @pentry: Populated with program entry point. Ignored if NULL.
99140b7ce5SPeter Crosthwaite  * @lowaddr: Populated with lowest loaded address. Ignored if NULL.
100140b7ce5SPeter Crosthwaite  * @highaddr: Populated with highest loaded address. Ignored if NULL.
101140b7ce5SPeter Crosthwaite  * @bigendian: Expected ELF endianness. 0 for LE otherwise BE
102140b7ce5SPeter Crosthwaite  * @elf_machine: Expected ELF machine type
103140b7ce5SPeter Crosthwaite  * @clear_lsb: Set to mask off LSB of addresses (Some architectures use
104140b7ce5SPeter Crosthwaite  *             this for non-address data)
1057ef295eaSPeter Crosthwaite  * @data_swab: Set to order of byte swapping for data. 0 for no swap, 1
1067ef295eaSPeter Crosthwaite  *             for swapping bytes within halfwords, 2 for bytes within
1077ef295eaSPeter Crosthwaite  *             words and 3 for within doublewords.
10870bb1d16SAlistair Francis  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
10970bb1d16SAlistair Francis  *      is used if nothing is supplied here.
11034f1b23fSFarhan Ali  * @load_rom : Load ELF binary as ROM
111a2480ffaSMichael Clark  * @sym_cb: Callback function for symbol table entries
112140b7ce5SPeter Crosthwaite  *
113140b7ce5SPeter Crosthwaite  * Load an ELF file's contents to the emulated system's address space.
114140b7ce5SPeter Crosthwaite  * Clients may optionally specify a callback to perform address
115140b7ce5SPeter Crosthwaite  * translations. @pentry, @lowaddr and @highaddr are optional pointers
116140b7ce5SPeter Crosthwaite  * which will be populated with various load information. @bigendian and
117140b7ce5SPeter Crosthwaite  * @elf_machine give the expected endianness and machine for the ELF the
118140b7ce5SPeter Crosthwaite  * load will fail if the target ELF does not match. Some architectures
119140b7ce5SPeter Crosthwaite  * have some architecture-specific behaviours that come into effect when
120140b7ce5SPeter Crosthwaite  * their particular values for @elf_machine are set.
1218cf6e9daSAlistair Francis  * If @elf_machine is EM_NONE then the machine type will be read from the
1228cf6e9daSAlistair Francis  * ELF header and no checks will be carried out against the machine type.
123140b7ce5SPeter Crosthwaite  */
124a2480ffaSMichael Clark typedef void (*symbol_fn_t)(const char *st_name, int st_info,
125a2480ffaSMichael Clark                             uint64_t st_value, uint64_t st_size);
126a2480ffaSMichael Clark 
127a2480ffaSMichael Clark int load_elf_ram_sym(const char *filename,
128a2480ffaSMichael Clark                      uint64_t (*translate_fn)(void *, uint64_t),
129a2480ffaSMichael Clark                      void *translate_opaque, uint64_t *pentry,
130a2480ffaSMichael Clark                      uint64_t *lowaddr, uint64_t *highaddr, int big_endian,
131a2480ffaSMichael Clark                      int elf_machine, int clear_lsb, int data_swab,
132a2480ffaSMichael Clark                      AddressSpace *as, bool load_rom, symbol_fn_t sym_cb);
133a2480ffaSMichael Clark 
134a2480ffaSMichael Clark /** load_elf_ram:
135a2480ffaSMichael Clark  * Same as load_elf_ram_sym(), but doesn't allow the caller to specify a
136a2480ffaSMichael Clark  * symbol callback function
137a2480ffaSMichael Clark  */
13834f1b23fSFarhan Ali int load_elf_ram(const char *filename,
13934f1b23fSFarhan Ali                  uint64_t (*translate_fn)(void *, uint64_t),
14034f1b23fSFarhan Ali                  void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
14134f1b23fSFarhan Ali                  uint64_t *highaddr, int big_endian, int elf_machine,
14234f1b23fSFarhan Ali                  int clear_lsb, int data_swab, AddressSpace *as,
14334f1b23fSFarhan Ali                  bool load_rom);
14434f1b23fSFarhan Ali 
14534f1b23fSFarhan Ali /** load_elf_as:
14634f1b23fSFarhan Ali  * Same as load_elf_ram(), but always loads the elf as ROM
14734f1b23fSFarhan Ali  */
14870bb1d16SAlistair Francis int load_elf_as(const char *filename,
14970bb1d16SAlistair Francis                 uint64_t (*translate_fn)(void *, uint64_t),
15070bb1d16SAlistair Francis                 void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
15170bb1d16SAlistair Francis                 uint64_t *highaddr, int big_endian, int elf_machine,
15270bb1d16SAlistair Francis                 int clear_lsb, int data_swab, AddressSpace *as);
153140b7ce5SPeter Crosthwaite 
15470bb1d16SAlistair Francis /** load_elf:
15570bb1d16SAlistair Francis  * Same as load_elf_as(), but doesn't allow the caller to specify an
15670bb1d16SAlistair Francis  * AddressSpace.
15770bb1d16SAlistair Francis  */
158409dbce5SAurelien Jarno int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t),
159409dbce5SAurelien Jarno              void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
160409dbce5SAurelien Jarno              uint64_t *highaddr, int big_endian, int elf_machine,
1617ef295eaSPeter Crosthwaite              int clear_lsb, int data_swab);
16204ae712aSPeter Crosthwaite 
16304ae712aSPeter Crosthwaite /** load_elf_hdr:
16404ae712aSPeter Crosthwaite  * @filename: Path of ELF file
16504ae712aSPeter Crosthwaite  * @hdr: Buffer to populate with header data. Header data will not be
16604ae712aSPeter Crosthwaite  * filled if set to NULL.
16704ae712aSPeter Crosthwaite  * @is64: Set to true if the ELF is 64bit. Ignored if set to NULL
16804ae712aSPeter Crosthwaite  * @errp: Populated with an error in failure cases
16904ae712aSPeter Crosthwaite  *
17004ae712aSPeter Crosthwaite  * Inspect an ELF file's header. Read its full header contents into a
17104ae712aSPeter Crosthwaite  * buffer and/or determine if the ELF is 64bit.
17204ae712aSPeter Crosthwaite  */
17304ae712aSPeter Crosthwaite void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp);
17404ae712aSPeter Crosthwaite 
175a8170e5eSAvi Kivity int load_aout(const char *filename, hwaddr addr, int max_sz,
176a8170e5eSAvi Kivity               int bswap_needed, hwaddr target_page_size);
1775e774eb3SAlistair Francis 
1785e774eb3SAlistair Francis /** load_uimage_as:
1795e774eb3SAlistair Francis  * @filename: Path of uimage file
1805e774eb3SAlistair Francis  * @ep: Populated with program entry point. Ignored if NULL.
1815e774eb3SAlistair Francis  * @loadaddr: Populated with the load address. Ignored if NULL.
1825e774eb3SAlistair Francis  * @is_linux: Is set to true if the image loaded is Linux. Ignored if NULL.
1835e774eb3SAlistair Francis  * @translate_fn: optional function to translate load addresses
1845e774eb3SAlistair Francis  * @translate_opaque: opaque data passed to @translate_fn
1855e774eb3SAlistair Francis  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
1865e774eb3SAlistair Francis  *      is used if nothing is supplied here.
1875e774eb3SAlistair Francis  *
1885e774eb3SAlistair Francis  * Loads a u-boot image into memory.
1895e774eb3SAlistair Francis  *
1905e774eb3SAlistair Francis  * Returns the size of the loaded image on success, -1 otherwise.
1915e774eb3SAlistair Francis  */
1925e774eb3SAlistair Francis int load_uimage_as(const char *filename, hwaddr *ep,
1935e774eb3SAlistair Francis                    hwaddr *loadaddr, int *is_linux,
1945e774eb3SAlistair Francis                    uint64_t (*translate_fn)(void *, uint64_t),
1955e774eb3SAlistair Francis                    void *translate_opaque, AddressSpace *as);
1965e774eb3SAlistair Francis 
1975e774eb3SAlistair Francis /** load_uimage:
1985e774eb3SAlistair Francis  * Same as load_uimage_as(), but doesn't allow the caller to specify an
1995e774eb3SAlistair Francis  * AddressSpace.
2005e774eb3SAlistair Francis  */
201a8170e5eSAvi Kivity int load_uimage(const char *filename, hwaddr *ep,
20225bda50aSMax Filippov                 hwaddr *loadaddr, int *is_linux,
20325bda50aSMax Filippov                 uint64_t (*translate_fn)(void *, uint64_t),
20425bda50aSMax Filippov                 void *translate_opaque);
205ca20cf32SBlue Swirl 
20684aee0deSSoren Brinkmann /**
20797df5feeSPeter Maydell  * load_ramdisk_as:
20884aee0deSSoren Brinkmann  * @filename: Path to the ramdisk image
20984aee0deSSoren Brinkmann  * @addr: Memory address to load the ramdisk to
21084aee0deSSoren Brinkmann  * @max_sz: Maximum allowed ramdisk size (for non-u-boot ramdisks)
21197df5feeSPeter Maydell  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
21297df5feeSPeter Maydell  *      is used if nothing is supplied here.
21384aee0deSSoren Brinkmann  *
21484aee0deSSoren Brinkmann  * Load a ramdisk image with U-Boot header to the specified memory
21584aee0deSSoren Brinkmann  * address.
21684aee0deSSoren Brinkmann  *
21784aee0deSSoren Brinkmann  * Returns the size of the loaded image on success, -1 otherwise.
21884aee0deSSoren Brinkmann  */
21997df5feeSPeter Maydell int load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz,
22097df5feeSPeter Maydell                     AddressSpace *as);
22197df5feeSPeter Maydell 
22297df5feeSPeter Maydell /**
22397df5feeSPeter Maydell  * load_ramdisk:
22497df5feeSPeter Maydell  * Same as load_ramdisk_as(), but doesn't allow the caller to specify
22597df5feeSPeter Maydell  * an AddressSpace.
22697df5feeSPeter Maydell  */
22784aee0deSSoren Brinkmann int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz);
22884aee0deSSoren Brinkmann 
22951b58561SPaul Burton ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen);
23051b58561SPaul Burton 
231725e14e9SMarkus Armbruster ssize_t read_targphys(const char *name,
232a8170e5eSAvi Kivity                       int fd, hwaddr dst_addr, size_t nbytes);
2333c178e72SGerd Hoffmann void pstrcpy_targphys(const char *name,
234a8170e5eSAvi Kivity                       hwaddr dest, int buf_size,
235ca20cf32SBlue Swirl                       const char *source);
23645a50b16SGerd Hoffmann 
237ac41881bSMichael S. Tsirkin extern bool option_rom_has_mr;
23898bc3ab0SMichael S. Tsirkin extern bool rom_file_has_mr;
239bdb5ee30SGerd Hoffmann 
240bdb5ee30SGerd Hoffmann int rom_add_file(const char *file, const char *fw_dir,
241ac41881bSMichael S. Tsirkin                  hwaddr addr, int32_t bootindex,
2423e76099aSAlistair Francis                  bool option_rom, MemoryRegion *mr, AddressSpace *as);
243339240b5SPaolo Bonzini MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
244339240b5SPaolo Bonzini                            size_t max_len, hwaddr addr,
245339240b5SPaolo Bonzini                            const char *fw_file_name,
2466f6f4aecSMarc-André Lureau                            FWCfgCallback fw_callback,
247baf2d5bfSMichael S. Tsirkin                            void *callback_opaque, AddressSpace *as,
248baf2d5bfSMichael S. Tsirkin                            bool read_only);
249d60fa42eSFabien Chouteau int rom_add_elf_program(const char *name, void *data, size_t datasize,
2503e76099aSAlistair Francis                         size_t romsize, hwaddr addr, AddressSpace *as);
2516b3f7f63SEric Auger int rom_check_and_register_reset(void);
252a88b362cSLaszlo Ersek void rom_set_fw(FWCfgState *f);
253bab47d9aSGerd Hoffmann void rom_set_order_override(int order);
254bab47d9aSGerd Hoffmann void rom_reset_order_override(void);
255e2336043SStefan Hajnoczi 
256e2336043SStefan Hajnoczi /**
257e2336043SStefan Hajnoczi  * rom_transaction_begin:
258e2336043SStefan Hajnoczi  *
259e2336043SStefan Hajnoczi  * Call this before of a series of rom_add_*() calls.  Call
260e2336043SStefan Hajnoczi  * rom_transaction_end() afterwards to commit or abort.  These functions are
261e2336043SStefan Hajnoczi  * useful for undoing a series of rom_add_*() calls if image file loading fails
262e2336043SStefan Hajnoczi  * partway through.
263e2336043SStefan Hajnoczi  */
264e2336043SStefan Hajnoczi void rom_transaction_begin(void);
265e2336043SStefan Hajnoczi 
266e2336043SStefan Hajnoczi /**
267e2336043SStefan Hajnoczi  * rom_transaction_end:
268e2336043SStefan Hajnoczi  * @commit: true to commit added roms, false to drop added roms
269e2336043SStefan Hajnoczi  *
270e2336043SStefan Hajnoczi  * Call this after a series of rom_add_*() calls.  See rom_transaction_begin().
271e2336043SStefan Hajnoczi  */
272e2336043SStefan Hajnoczi void rom_transaction_end(bool commit);
273e2336043SStefan Hajnoczi 
274a8170e5eSAvi Kivity int rom_copy(uint8_t *dest, hwaddr addr, size_t size);
2750f0f8b61SThomas Huth void *rom_ptr(hwaddr addr, size_t size);
2761ce6be24SMarkus Armbruster void hmp_info_roms(Monitor *mon, const QDict *qdict);
27745a50b16SGerd Hoffmann 
2782e55e842SGleb Natapov #define rom_add_file_fixed(_f, _a, _i)          \
2793e76099aSAlistair Francis     rom_add_file(_f, NULL, _a, _i, false, NULL, NULL)
28045a50b16SGerd Hoffmann #define rom_add_blob_fixed(_f, _b, _l, _a)      \
281baf2d5bfSMichael S. Tsirkin     rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, NULL, true)
28276151cacSPeter Maydell #define rom_add_file_mr(_f, _mr, _i)            \
2833e76099aSAlistair Francis     rom_add_file(_f, NULL, 0, _i, false, _mr, NULL)
2843e76099aSAlistair Francis #define rom_add_file_as(_f, _as, _i)            \
2853e76099aSAlistair Francis     rom_add_file(_f, NULL, 0, _i, false, NULL, _as)
28693ffc7c7SAlistair Francis #define rom_add_file_fixed_as(_f, _a, _i, _as)          \
28793ffc7c7SAlistair Francis     rom_add_file(_f, NULL, _a, _i, false, NULL, _as)
2885e774eb3SAlistair Francis #define rom_add_blob_fixed_as(_f, _b, _l, _a, _as)      \
289baf2d5bfSMichael S. Tsirkin     rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, _as, true)
29045a50b16SGerd Hoffmann 
29145a50b16SGerd Hoffmann #define PC_ROM_MIN_VGA     0xc0000
29245a50b16SGerd Hoffmann #define PC_ROM_MIN_OPTION  0xc8000
29345a50b16SGerd Hoffmann #define PC_ROM_MAX         0xe0000
29445a50b16SGerd Hoffmann #define PC_ROM_ALIGN       0x800
29545a50b16SGerd Hoffmann #define PC_ROM_SIZE        (PC_ROM_MAX - PC_ROM_MIN_VGA)
29645a50b16SGerd Hoffmann 
297de2aff17SGerd Hoffmann int rom_add_vga(const char *file);
2982e55e842SGleb Natapov int rom_add_option(const char *file, int32_t bootindex);
29945a50b16SGerd Hoffmann 
30051b58561SPaul Burton /* This is the usual maximum in uboot, so if a uImage overflows this, it would
30151b58561SPaul Burton  * overflow on real hardware too. */
30251b58561SPaul Burton #define UBOOT_MAX_GUNZIP_BYTES (64 << 20)
30351b58561SPaul Burton 
304ca20cf32SBlue Swirl #endif
305