xref: /openbmc/qemu/include/hw/loader.h (revision 7c08eefc)
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 /**
15  * load_image_size: load an image file into specified buffer
16  * @filename: Path to the image file
17  * @addr: Buffer to load image into
18  * @size: Size of buffer in bytes
19  *
20  * Load an image file from disk into the specified buffer.
21  * If the image is larger than the specified buffer, only
22  * @size bytes are read (this is not considered an error).
23  *
24  * Prefer to use the GLib function g_file_get_contents() rather
25  * than a "get_image_size()/g_malloc()/load_image_size()" sequence.
26  *
27  * Returns the number of bytes read, or -1 on error. On error,
28  * errno is also set as appropriate.
29  */
30 ssize_t load_image_size(const char *filename, void *addr, size_t size);
31 
32 /**load_image_targphys_as:
33  * @filename: Path to the image file
34  * @addr: Address to load the image to
35  * @max_sz: The maximum size of the image to load
36  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
37  *      is used if nothing is supplied here.
38  *
39  * Load a fixed image into memory.
40  *
41  * Returns the size of the loaded image on success, -1 otherwise.
42  */
43 ssize_t load_image_targphys_as(const char *filename,
44                                hwaddr addr, uint64_t max_sz, AddressSpace *as);
45 
46 /**load_targphys_hex_as:
47  * @filename: Path to the .hex file
48  * @entry: Store the entry point given by the .hex file
49  * @as: The AddressSpace to load the .hex file to. The value of
50  *      address_space_memory is used if nothing is supplied here.
51  *
52  * Load a fixed .hex file into memory.
53  *
54  * Returns the size of the loaded .hex file on success, -1 otherwise.
55  */
56 ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
57                              AddressSpace *as);
58 
59 /** load_image_targphys:
60  * Same as load_image_targphys_as(), but doesn't allow the caller to specify
61  * an AddressSpace.
62  */
63 ssize_t load_image_targphys(const char *filename, hwaddr,
64                             uint64_t max_sz);
65 
66 /**
67  * load_image_mr: load an image into a memory region
68  * @filename: Path to the image file
69  * @mr: Memory Region to load into
70  *
71  * Load the specified file into the memory region.
72  * The file loaded is registered as a ROM, so its contents will be
73  * reinstated whenever the system is reset.
74  * If the file is larger than the memory region's size the call will fail.
75  * Returns -1 on failure, or the size of the file.
76  */
77 ssize_t load_image_mr(const char *filename, MemoryRegion *mr);
78 
79 /* This is the limit on the maximum uncompressed image size that
80  * load_image_gzipped_buffer() and load_image_gzipped() will read. It prevents
81  * g_malloc() in those functions from allocating a huge amount of memory.
82  */
83 #define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20)
84 
85 ssize_t load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
86                                   uint8_t **buffer);
87 ssize_t load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz);
88 
89 /**
90  * unpack_efi_zboot_image:
91  * @buffer: pointer to a variable holding the address of a buffer containing the
92  *          image
93  * @size: pointer to a variable holding the size of the buffer
94  *
95  * Check whether the buffer contains a EFI zboot image, and if it does, extract
96  * the compressed payload and decompress it into a new buffer. If successful,
97  * the old buffer is freed, and the *buffer and size variables pointed to by the
98  * function arguments are updated to refer to the newly populated buffer.
99  *
100  * Returns 0 if the image could not be identified as a EFI zboot image.
101  * Returns -1 if the buffer contents were identified as a EFI zboot image, but
102  * unpacking failed for any reason.
103  * Returns the size of the decompressed payload if decompression was performed
104  * successfully.
105  */
106 ssize_t unpack_efi_zboot_image(uint8_t **buffer, int *size);
107 
108 #define ELF_LOAD_FAILED       -1
109 #define ELF_LOAD_NOT_ELF      -2
110 #define ELF_LOAD_WRONG_ARCH   -3
111 #define ELF_LOAD_WRONG_ENDIAN -4
112 #define ELF_LOAD_TOO_BIG      -5
113 const char *load_elf_strerror(ssize_t error);
114 
115 /** load_elf_ram_sym:
116  * @filename: Path of ELF file
117  * @elf_note_fn: optional function to parse ELF Note type
118  *               passed via @translate_opaque
119  * @translate_fn: optional function to translate load addresses
120  * @translate_opaque: opaque data passed to @translate_fn
121  * @pentry: Populated with program entry point. Ignored if NULL.
122  * @lowaddr: Populated with lowest loaded address. Ignored if NULL.
123  * @highaddr: Populated with highest loaded address. Ignored if NULL.
124  * @pflags: Populated with ELF processor-specific flags. Ignore if NULL.
125  * @bigendian: Expected ELF endianness. 0 for LE otherwise BE
126  * @elf_machine: Expected ELF machine type
127  * @clear_lsb: Set to mask off LSB of addresses (Some architectures use
128  *             this for non-address data)
129  * @data_swab: Set to order of byte swapping for data. 0 for no swap, 1
130  *             for swapping bytes within halfwords, 2 for bytes within
131  *             words and 3 for within doublewords.
132  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
133  *      is used if nothing is supplied here.
134  * @load_rom : Load ELF binary as ROM
135  * @sym_cb: Callback function for symbol table entries
136  *
137  * Load an ELF file's contents to the emulated system's address space.
138  * Clients may optionally specify a callback to perform address
139  * translations. @pentry, @lowaddr and @highaddr are optional pointers
140  * which will be populated with various load information. @bigendian and
141  * @elf_machine give the expected endianness and machine for the ELF the
142  * load will fail if the target ELF does not match. Some architectures
143  * have some architecture-specific behaviours that come into effect when
144  * their particular values for @elf_machine are set.
145  * If @elf_machine is EM_NONE then the machine type will be read from the
146  * ELF header and no checks will be carried out against the machine type.
147  */
148 typedef void (*symbol_fn_t)(const char *st_name, int st_info,
149                             uint64_t st_value, uint64_t st_size);
150 
151 ssize_t load_elf_ram_sym(const char *filename,
152                          uint64_t (*elf_note_fn)(void *, void *, bool),
153                          uint64_t (*translate_fn)(void *, uint64_t),
154                          void *translate_opaque, uint64_t *pentry,
155                          uint64_t *lowaddr, uint64_t *highaddr,
156                          uint32_t *pflags, int big_endian, int elf_machine,
157                          int clear_lsb, int data_swab,
158                          AddressSpace *as, bool load_rom, symbol_fn_t sym_cb);
159 
160 /** load_elf_ram:
161  * Same as load_elf_ram_sym(), but doesn't allow the caller to specify a
162  * symbol callback function
163  */
164 ssize_t load_elf_ram(const char *filename,
165                      uint64_t (*elf_note_fn)(void *, void *, bool),
166                      uint64_t (*translate_fn)(void *, uint64_t),
167                      void *translate_opaque, uint64_t *pentry,
168                      uint64_t *lowaddr, uint64_t *highaddr, uint32_t *pflags,
169                      int big_endian, int elf_machine, int clear_lsb,
170                      int data_swab, AddressSpace *as, bool load_rom);
171 
172 /** load_elf_as:
173  * Same as load_elf_ram(), but always loads the elf as ROM
174  */
175 ssize_t load_elf_as(const char *filename,
176                     uint64_t (*elf_note_fn)(void *, void *, bool),
177                     uint64_t (*translate_fn)(void *, uint64_t),
178                     void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
179                     uint64_t *highaddr, uint32_t *pflags, int big_endian,
180                     int elf_machine, int clear_lsb, int data_swab,
181                     AddressSpace *as);
182 
183 /** load_elf:
184  * Same as load_elf_as(), but doesn't allow the caller to specify an
185  * AddressSpace.
186  */
187 ssize_t load_elf(const char *filename,
188                  uint64_t (*elf_note_fn)(void *, void *, bool),
189                  uint64_t (*translate_fn)(void *, uint64_t),
190                  void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
191                  uint64_t *highaddr, uint32_t *pflags, int big_endian,
192                  int elf_machine, int clear_lsb, int data_swab);
193 
194 /** load_elf_hdr:
195  * @filename: Path of ELF file
196  * @hdr: Buffer to populate with header data. Header data will not be
197  * filled if set to NULL.
198  * @is64: Set to true if the ELF is 64bit. Ignored if set to NULL
199  * @errp: Populated with an error in failure cases
200  *
201  * Inspect an ELF file's header. Read its full header contents into a
202  * buffer and/or determine if the ELF is 64bit.
203  */
204 void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp);
205 
206 ssize_t load_aout(const char *filename, hwaddr addr, int max_sz,
207                   int bswap_needed, hwaddr target_page_size);
208 
209 #define LOAD_UIMAGE_LOADADDR_INVALID (-1)
210 
211 /** load_uimage_as:
212  * @filename: Path of uimage file
213  * @ep: Populated with program entry point. Ignored if NULL.
214  * @loadaddr: load address if none specified in the image or when loading a
215  *            ramdisk. Populated with the load address. Ignored if NULL or
216  *            LOAD_UIMAGE_LOADADDR_INVALID (images which do not specify a load
217  *            address will not be loadable).
218  * @is_linux: Is set to true if the image loaded is Linux. Ignored if NULL.
219  * @translate_fn: optional function to translate load addresses
220  * @translate_opaque: opaque data passed to @translate_fn
221  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
222  *      is used if nothing is supplied here.
223  *
224  * Loads a u-boot image into memory.
225  *
226  * Returns the size of the loaded image on success, -1 otherwise.
227  */
228 ssize_t load_uimage_as(const char *filename, hwaddr *ep,
229                        hwaddr *loadaddr, int *is_linux,
230                        uint64_t (*translate_fn)(void *, uint64_t),
231                        void *translate_opaque, AddressSpace *as);
232 
233 /** load_uimage:
234  * Same as load_uimage_as(), but doesn't allow the caller to specify an
235  * AddressSpace.
236  */
237 ssize_t load_uimage(const char *filename, hwaddr *ep,
238                     hwaddr *loadaddr, int *is_linux,
239                     uint64_t (*translate_fn)(void *, uint64_t),
240                     void *translate_opaque);
241 
242 /**
243  * load_ramdisk_as:
244  * @filename: Path to the ramdisk image
245  * @addr: Memory address to load the ramdisk to
246  * @max_sz: Maximum allowed ramdisk size (for non-u-boot ramdisks)
247  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
248  *      is used if nothing is supplied here.
249  *
250  * Load a ramdisk image with U-Boot header to the specified memory
251  * address.
252  *
253  * Returns the size of the loaded image on success, -1 otherwise.
254  */
255 ssize_t load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz,
256                         AddressSpace *as);
257 
258 /**
259  * load_ramdisk:
260  * Same as load_ramdisk_as(), but doesn't allow the caller to specify
261  * an AddressSpace.
262  */
263 ssize_t load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz);
264 
265 ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen);
266 
267 ssize_t read_targphys(const char *name,
268                       int fd, hwaddr dst_addr, size_t nbytes);
269 void pstrcpy_targphys(const char *name,
270                       hwaddr dest, int buf_size,
271                       const char *source);
272 
273 ssize_t rom_add_file(const char *file, const char *fw_dir,
274                      hwaddr addr, int32_t bootindex,
275                      bool has_option_rom, MemoryRegion *mr, AddressSpace *as);
276 MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
277                            size_t max_len, hwaddr addr,
278                            const char *fw_file_name,
279                            FWCfgCallback fw_callback,
280                            void *callback_opaque, AddressSpace *as,
281                            bool read_only);
282 int rom_add_elf_program(const char *name, GMappedFile *mapped_file, void *data,
283                         size_t datasize, size_t romsize, hwaddr addr,
284                         AddressSpace *as);
285 int rom_check_and_register_reset(void);
286 void rom_set_fw(FWCfgState *f);
287 void rom_set_order_override(int order);
288 void rom_reset_order_override(void);
289 
290 /**
291  * rom_transaction_begin:
292  *
293  * Call this before of a series of rom_add_*() calls.  Call
294  * rom_transaction_end() afterwards to commit or abort.  These functions are
295  * useful for undoing a series of rom_add_*() calls if image file loading fails
296  * partway through.
297  */
298 void rom_transaction_begin(void);
299 
300 /**
301  * rom_transaction_end:
302  * @commit: true to commit added roms, false to drop added roms
303  *
304  * Call this after a series of rom_add_*() calls.  See rom_transaction_begin().
305  */
306 void rom_transaction_end(bool commit);
307 
308 int rom_copy(uint8_t *dest, hwaddr addr, size_t size);
309 void *rom_ptr(hwaddr addr, size_t size);
310 /**
311  * rom_ptr_for_as: Return a pointer to ROM blob data for the address
312  * @as: AddressSpace to look for the ROM blob in
313  * @addr: Address within @as
314  * @size: size of data required in bytes
315  *
316  * Returns: pointer into the data which backs the matching ROM blob,
317  * or NULL if no blob covers the address range.
318  *
319  * This function looks for a ROM blob which covers the specified range
320  * of bytes of length @size starting at @addr within the address space
321  * @as. This is useful for code which runs as part of board
322  * initialization or CPU reset which wants to read data that is part
323  * of a user-supplied guest image or other guest memory contents, but
324  * which runs before the ROM loader's reset function has copied the
325  * blobs into guest memory.
326  *
327  * rom_ptr_for_as() will look not just for blobs loaded directly to
328  * the specified address, but also for blobs which were loaded to an
329  * alias of the region at a different location in the AddressSpace.
330  * In other words, if a machine model has RAM at address 0x0000_0000
331  * which is aliased to also appear at 0x1000_0000, rom_ptr_for_as()
332  * will return the correct data whether the guest image was linked and
333  * loaded at 0x0000_0000 or 0x1000_0000.  Contrast rom_ptr(), which
334  * will only return data if the image load address is an exact match
335  * with the queried address.
336  *
337  * New code should prefer to use rom_ptr_for_as() instead of
338  * rom_ptr().
339  */
340 void *rom_ptr_for_as(AddressSpace *as, hwaddr addr, size_t size);
341 
342 #define rom_add_file_fixed(_f, _a, _i)          \
343     rom_add_file(_f, NULL, _a, _i, false, NULL, NULL)
344 #define rom_add_blob_fixed(_f, _b, _l, _a)      \
345     rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, NULL, true)
346 #define rom_add_file_mr(_f, _mr, _i)            \
347     rom_add_file(_f, NULL, 0, _i, false, _mr, NULL)
348 #define rom_add_file_as(_f, _as, _i)            \
349     rom_add_file(_f, NULL, 0, _i, false, NULL, _as)
350 #define rom_add_file_fixed_as(_f, _a, _i, _as)          \
351     rom_add_file(_f, NULL, _a, _i, false, NULL, _as)
352 #define rom_add_blob_fixed_as(_f, _b, _l, _a, _as)      \
353     rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, _as, true)
354 
355 ssize_t rom_add_vga(const char *file);
356 ssize_t rom_add_option(const char *file, int32_t bootindex);
357 
358 /* This is the usual maximum in uboot, so if a uImage overflows this, it would
359  * overflow on real hardware too. */
360 #define UBOOT_MAX_GUNZIP_BYTES (64 << 20)
361 
362 typedef struct RomGap {
363     hwaddr base;
364     size_t size;
365 } RomGap;
366 
367 /**
368  * rom_find_largest_gap_between: return largest gap between ROMs in given range
369  *
370  * Given a range of addresses, this function finds the largest
371  * contiguous subrange which has no ROMs loaded to it. That is,
372  * it finds the biggest gap which is free for use for other things.
373  */
374 RomGap rom_find_largest_gap_between(hwaddr base, size_t size);
375 
376 #endif
377