xref: /openbmc/qemu/include/hw/loader.h (revision 2933f6980b82ed3dcf1289c8f729fb9ac63e52fa)
1 #ifndef LOADER_H
2 #define LOADER_H
3 #include "hw/nvram/fw_cfg.h"
4 
5 /* loader.c */
6 /**
7  * get_image_size: retrieve size of an image file
8  * @filename: Path to the image file
9  *
10  * Returns the size of the image file on success, -1 otherwise.
11  * On error, errno is also set as appropriate.
12  */
13 int64_t get_image_size(const char *filename);
14 ssize_t load_image_size(const char *filename, void *addr, size_t size);
15 
16 /**load_image_targphys_as:
17  * @filename: Path to the image file
18  * @addr: Address to load the image to
19  * @max_sz: The maximum size of the image to load
20  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
21  *      is used if nothing is supplied here.
22  *
23  * Load a fixed image into memory.
24  *
25  * Returns the size of the loaded image on success, -1 otherwise.
26  */
27 int load_image_targphys_as(const char *filename,
28                            hwaddr addr, uint64_t max_sz, AddressSpace *as);
29 
30 /**load_targphys_hex_as:
31  * @filename: Path to the .hex file
32  * @entry: Store the entry point given by the .hex file
33  * @as: The AddressSpace to load the .hex file to. The value of
34  *      address_space_memory is used if nothing is supplied here.
35  *
36  * Load a fixed .hex file into memory.
37  *
38  * Returns the size of the loaded .hex file on success, -1 otherwise.
39  */
40 int load_targphys_hex_as(const char *filename, hwaddr *entry, AddressSpace *as);
41 
42 /** load_image_targphys:
43  * Same as load_image_targphys_as(), but doesn't allow the caller to specify
44  * an AddressSpace.
45  */
46 int load_image_targphys(const char *filename, hwaddr,
47                         uint64_t max_sz);
48 
49 /**
50  * load_image_mr: load an image into a memory region
51  * @filename: Path to the image file
52  * @mr: Memory Region to load into
53  *
54  * Load the specified file into the memory region.
55  * The file loaded is registered as a ROM, so its contents will be
56  * reinstated whenever the system is reset.
57  * If the file is larger than the memory region's size the call will fail.
58  * Returns -1 on failure, or the size of the file.
59  */
60 int load_image_mr(const char *filename, MemoryRegion *mr);
61 
62 /* This is the limit on the maximum uncompressed image size that
63  * load_image_gzipped_buffer() and load_image_gzipped() will read. It prevents
64  * g_malloc() in those functions from allocating a huge amount of memory.
65  */
66 #define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20)
67 
68 int load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
69                               uint8_t **buffer);
70 int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz);
71 
72 #define ELF_LOAD_FAILED       -1
73 #define ELF_LOAD_NOT_ELF      -2
74 #define ELF_LOAD_WRONG_ARCH   -3
75 #define ELF_LOAD_WRONG_ENDIAN -4
76 const char *load_elf_strerror(int error);
77 
78 /** load_elf_ram_sym:
79  * @filename: Path of ELF file
80  * @translate_fn: optional function to translate load addresses
81  * @translate_opaque: opaque data passed to @translate_fn
82  * @pentry: Populated with program entry point. Ignored if NULL.
83  * @lowaddr: Populated with lowest loaded address. Ignored if NULL.
84  * @highaddr: Populated with highest loaded address. Ignored if NULL.
85  * @bigendian: Expected ELF endianness. 0 for LE otherwise BE
86  * @elf_machine: Expected ELF machine type
87  * @clear_lsb: Set to mask off LSB of addresses (Some architectures use
88  *             this for non-address data)
89  * @data_swab: Set to order of byte swapping for data. 0 for no swap, 1
90  *             for swapping bytes within halfwords, 2 for bytes within
91  *             words and 3 for within doublewords.
92  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
93  *      is used if nothing is supplied here.
94  * @load_rom : Load ELF binary as ROM
95  * @sym_cb: Callback function for symbol table entries
96  *
97  * Load an ELF file's contents to the emulated system's address space.
98  * Clients may optionally specify a callback to perform address
99  * translations. @pentry, @lowaddr and @highaddr are optional pointers
100  * which will be populated with various load information. @bigendian and
101  * @elf_machine give the expected endianness and machine for the ELF the
102  * load will fail if the target ELF does not match. Some architectures
103  * have some architecture-specific behaviours that come into effect when
104  * their particular values for @elf_machine are set.
105  * If @elf_machine is EM_NONE then the machine type will be read from the
106  * ELF header and no checks will be carried out against the machine type.
107  */
108 typedef void (*symbol_fn_t)(const char *st_name, int st_info,
109                             uint64_t st_value, uint64_t st_size);
110 
111 int load_elf_ram_sym(const char *filename,
112                      uint64_t (*translate_fn)(void *, uint64_t),
113                      void *translate_opaque, uint64_t *pentry,
114                      uint64_t *lowaddr, uint64_t *highaddr, int big_endian,
115                      int elf_machine, int clear_lsb, int data_swab,
116                      AddressSpace *as, bool load_rom, symbol_fn_t sym_cb);
117 
118 /** load_elf_ram:
119  * Same as load_elf_ram_sym(), but doesn't allow the caller to specify a
120  * symbol callback function
121  */
122 int load_elf_ram(const char *filename,
123                  uint64_t (*translate_fn)(void *, uint64_t),
124                  void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
125                  uint64_t *highaddr, int big_endian, int elf_machine,
126                  int clear_lsb, int data_swab, AddressSpace *as,
127                  bool load_rom);
128 
129 /** load_elf_as:
130  * Same as load_elf_ram(), but always loads the elf as ROM
131  */
132 int load_elf_as(const char *filename,
133                 uint64_t (*translate_fn)(void *, uint64_t),
134                 void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
135                 uint64_t *highaddr, int big_endian, int elf_machine,
136                 int clear_lsb, int data_swab, AddressSpace *as);
137 
138 /** load_elf:
139  * Same as load_elf_as(), but doesn't allow the caller to specify an
140  * AddressSpace.
141  */
142 int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t),
143              void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
144              uint64_t *highaddr, int big_endian, int elf_machine,
145              int clear_lsb, int data_swab);
146 
147 /** load_elf_hdr:
148  * @filename: Path of ELF file
149  * @hdr: Buffer to populate with header data. Header data will not be
150  * filled if set to NULL.
151  * @is64: Set to true if the ELF is 64bit. Ignored if set to NULL
152  * @errp: Populated with an error in failure cases
153  *
154  * Inspect an ELF file's header. Read its full header contents into a
155  * buffer and/or determine if the ELF is 64bit.
156  */
157 void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp);
158 
159 int load_aout(const char *filename, hwaddr addr, int max_sz,
160               int bswap_needed, hwaddr target_page_size);
161 
162 /** load_uimage_as:
163  * @filename: Path of uimage file
164  * @ep: Populated with program entry point. Ignored if NULL.
165  * @loadaddr: Populated with the load address. Ignored if NULL.
166  * @is_linux: Is set to true if the image loaded is Linux. Ignored if NULL.
167  * @translate_fn: optional function to translate load addresses
168  * @translate_opaque: opaque data passed to @translate_fn
169  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
170  *      is used if nothing is supplied here.
171  *
172  * Loads a u-boot image into memory.
173  *
174  * Returns the size of the loaded image on success, -1 otherwise.
175  */
176 int load_uimage_as(const char *filename, hwaddr *ep,
177                    hwaddr *loadaddr, int *is_linux,
178                    uint64_t (*translate_fn)(void *, uint64_t),
179                    void *translate_opaque, AddressSpace *as);
180 
181 /** load_uimage:
182  * Same as load_uimage_as(), but doesn't allow the caller to specify an
183  * AddressSpace.
184  */
185 int load_uimage(const char *filename, hwaddr *ep,
186                 hwaddr *loadaddr, int *is_linux,
187                 uint64_t (*translate_fn)(void *, uint64_t),
188                 void *translate_opaque);
189 
190 /**
191  * load_ramdisk_as:
192  * @filename: Path to the ramdisk image
193  * @addr: Memory address to load the ramdisk to
194  * @max_sz: Maximum allowed ramdisk size (for non-u-boot ramdisks)
195  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
196  *      is used if nothing is supplied here.
197  *
198  * Load a ramdisk image with U-Boot header to the specified memory
199  * address.
200  *
201  * Returns the size of the loaded image on success, -1 otherwise.
202  */
203 int load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz,
204                     AddressSpace *as);
205 
206 /**
207  * load_ramdisk:
208  * Same as load_ramdisk_as(), but doesn't allow the caller to specify
209  * an AddressSpace.
210  */
211 int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz);
212 
213 ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen);
214 
215 ssize_t read_targphys(const char *name,
216                       int fd, hwaddr dst_addr, size_t nbytes);
217 void pstrcpy_targphys(const char *name,
218                       hwaddr dest, int buf_size,
219                       const char *source);
220 
221 extern bool option_rom_has_mr;
222 extern bool rom_file_has_mr;
223 
224 int rom_add_file(const char *file, const char *fw_dir,
225                  hwaddr addr, int32_t bootindex,
226                  bool option_rom, MemoryRegion *mr, AddressSpace *as);
227 MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
228                            size_t max_len, hwaddr addr,
229                            const char *fw_file_name,
230                            FWCfgCallback fw_callback,
231                            void *callback_opaque, AddressSpace *as,
232                            bool read_only);
233 int rom_add_elf_program(const char *name, void *data, size_t datasize,
234                         size_t romsize, hwaddr addr, AddressSpace *as);
235 int rom_check_and_register_reset(void);
236 void rom_set_fw(FWCfgState *f);
237 void rom_set_order_override(int order);
238 void rom_reset_order_override(void);
239 
240 /**
241  * rom_transaction_begin:
242  *
243  * Call this before of a series of rom_add_*() calls.  Call
244  * rom_transaction_end() afterwards to commit or abort.  These functions are
245  * useful for undoing a series of rom_add_*() calls if image file loading fails
246  * partway through.
247  */
248 void rom_transaction_begin(void);
249 
250 /**
251  * rom_transaction_end:
252  * @commit: true to commit added roms, false to drop added roms
253  *
254  * Call this after a series of rom_add_*() calls.  See rom_transaction_begin().
255  */
256 void rom_transaction_end(bool commit);
257 
258 int rom_copy(uint8_t *dest, hwaddr addr, size_t size);
259 void *rom_ptr(hwaddr addr, size_t size);
260 void hmp_info_roms(Monitor *mon, const QDict *qdict);
261 
262 #define rom_add_file_fixed(_f, _a, _i)          \
263     rom_add_file(_f, NULL, _a, _i, false, NULL, NULL)
264 #define rom_add_blob_fixed(_f, _b, _l, _a)      \
265     rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, NULL, true)
266 #define rom_add_file_mr(_f, _mr, _i)            \
267     rom_add_file(_f, NULL, 0, _i, false, _mr, NULL)
268 #define rom_add_file_as(_f, _as, _i)            \
269     rom_add_file(_f, NULL, 0, _i, false, NULL, _as)
270 #define rom_add_file_fixed_as(_f, _a, _i, _as)          \
271     rom_add_file(_f, NULL, _a, _i, false, NULL, _as)
272 #define rom_add_blob_fixed_as(_f, _b, _l, _a, _as)      \
273     rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, _as, true)
274 
275 #define PC_ROM_MIN_VGA     0xc0000
276 #define PC_ROM_MIN_OPTION  0xc8000
277 #define PC_ROM_MAX         0xe0000
278 #define PC_ROM_ALIGN       0x800
279 #define PC_ROM_SIZE        (PC_ROM_MAX - PC_ROM_MIN_VGA)
280 
281 int rom_add_vga(const char *file);
282 int rom_add_option(const char *file, int32_t bootindex);
283 
284 /* This is the usual maximum in uboot, so if a uImage overflows this, it would
285  * overflow on real hardware too. */
286 #define UBOOT_MAX_GUNZIP_BYTES (64 << 20)
287 
288 #endif
289