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