xref: /openbmc/qemu/include/hw/loader.h (revision 0b84b662)
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(int 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  * @bigendian: Expected ELF endianness. 0 for LE otherwise BE
105  * @elf_machine: Expected ELF machine type
106  * @clear_lsb: Set to mask off LSB of addresses (Some architectures use
107  *             this for non-address data)
108  * @data_swab: Set to order of byte swapping for data. 0 for no swap, 1
109  *             for swapping bytes within halfwords, 2 for bytes within
110  *             words and 3 for within doublewords.
111  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
112  *      is used if nothing is supplied here.
113  * @load_rom : Load ELF binary as ROM
114  * @sym_cb: Callback function for symbol table entries
115  *
116  * Load an ELF file's contents to the emulated system's address space.
117  * Clients may optionally specify a callback to perform address
118  * translations. @pentry, @lowaddr and @highaddr are optional pointers
119  * which will be populated with various load information. @bigendian and
120  * @elf_machine give the expected endianness and machine for the ELF the
121  * load will fail if the target ELF does not match. Some architectures
122  * have some architecture-specific behaviours that come into effect when
123  * their particular values for @elf_machine are set.
124  * If @elf_machine is EM_NONE then the machine type will be read from the
125  * ELF header and no checks will be carried out against the machine type.
126  */
127 typedef void (*symbol_fn_t)(const char *st_name, int st_info,
128                             uint64_t st_value, uint64_t st_size);
129 
130 int load_elf_ram_sym(const char *filename,
131                      uint64_t (*elf_note_fn)(void *, void *, bool),
132                      uint64_t (*translate_fn)(void *, uint64_t),
133                      void *translate_opaque, uint64_t *pentry,
134                      uint64_t *lowaddr, uint64_t *highaddr, int big_endian,
135                      int elf_machine, int clear_lsb, int data_swab,
136                      AddressSpace *as, bool load_rom, symbol_fn_t sym_cb);
137 
138 /** load_elf_ram:
139  * Same as load_elf_ram_sym(), but doesn't allow the caller to specify a
140  * symbol callback function
141  */
142 int load_elf_ram(const char *filename,
143                  uint64_t (*elf_note_fn)(void *, void *, bool),
144                  uint64_t (*translate_fn)(void *, uint64_t),
145                  void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
146                  uint64_t *highaddr, int big_endian, int elf_machine,
147                  int clear_lsb, int data_swab, AddressSpace *as,
148                  bool load_rom);
149 
150 /** load_elf_as:
151  * Same as load_elf_ram(), but always loads the elf as ROM
152  */
153 int load_elf_as(const char *filename,
154                 uint64_t (*elf_note_fn)(void *, void *, bool),
155                 uint64_t (*translate_fn)(void *, uint64_t),
156                 void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
157                 uint64_t *highaddr, int big_endian, int elf_machine,
158                 int clear_lsb, int data_swab, AddressSpace *as);
159 
160 /** load_elf:
161  * Same as load_elf_as(), but doesn't allow the caller to specify an
162  * AddressSpace.
163  */
164 int load_elf(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, uint64_t *lowaddr,
168              uint64_t *highaddr, int big_endian, int elf_machine,
169              int clear_lsb, int data_swab);
170 
171 /** load_elf_hdr:
172  * @filename: Path of ELF file
173  * @hdr: Buffer to populate with header data. Header data will not be
174  * filled if set to NULL.
175  * @is64: Set to true if the ELF is 64bit. Ignored if set to NULL
176  * @errp: Populated with an error in failure cases
177  *
178  * Inspect an ELF file's header. Read its full header contents into a
179  * buffer and/or determine if the ELF is 64bit.
180  */
181 void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp);
182 
183 int load_aout(const char *filename, hwaddr addr, int max_sz,
184               int bswap_needed, hwaddr target_page_size);
185 
186 #define LOAD_UIMAGE_LOADADDR_INVALID (-1)
187 
188 /** load_uimage_as:
189  * @filename: Path of uimage file
190  * @ep: Populated with program entry point. Ignored if NULL.
191  * @loadaddr: load address if none specified in the image or when loading a
192  *            ramdisk. Populated with the load address. Ignored if NULL or
193  *            LOAD_UIMAGE_LOADADDR_INVALID (images which do not specify a load
194  *            address will not be loadable).
195  * @is_linux: Is set to true if the image loaded is Linux. Ignored if NULL.
196  * @translate_fn: optional function to translate load addresses
197  * @translate_opaque: opaque data passed to @translate_fn
198  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
199  *      is used if nothing is supplied here.
200  *
201  * Loads a u-boot image into memory.
202  *
203  * Returns the size of the loaded image on success, -1 otherwise.
204  */
205 int load_uimage_as(const char *filename, hwaddr *ep,
206                    hwaddr *loadaddr, int *is_linux,
207                    uint64_t (*translate_fn)(void *, uint64_t),
208                    void *translate_opaque, AddressSpace *as);
209 
210 /** load_uimage:
211  * Same as load_uimage_as(), but doesn't allow the caller to specify an
212  * AddressSpace.
213  */
214 int load_uimage(const char *filename, hwaddr *ep,
215                 hwaddr *loadaddr, int *is_linux,
216                 uint64_t (*translate_fn)(void *, uint64_t),
217                 void *translate_opaque);
218 
219 /**
220  * load_ramdisk_as:
221  * @filename: Path to the ramdisk image
222  * @addr: Memory address to load the ramdisk to
223  * @max_sz: Maximum allowed ramdisk size (for non-u-boot ramdisks)
224  * @as: The AddressSpace to load the ELF to. The value of address_space_memory
225  *      is used if nothing is supplied here.
226  *
227  * Load a ramdisk image with U-Boot header to the specified memory
228  * address.
229  *
230  * Returns the size of the loaded image on success, -1 otherwise.
231  */
232 int load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz,
233                     AddressSpace *as);
234 
235 /**
236  * load_ramdisk:
237  * Same as load_ramdisk_as(), but doesn't allow the caller to specify
238  * an AddressSpace.
239  */
240 int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz);
241 
242 ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen);
243 
244 ssize_t read_targphys(const char *name,
245                       int fd, hwaddr dst_addr, size_t nbytes);
246 void pstrcpy_targphys(const char *name,
247                       hwaddr dest, int buf_size,
248                       const char *source);
249 
250 extern bool option_rom_has_mr;
251 extern bool rom_file_has_mr;
252 
253 int rom_add_file(const char *file, const char *fw_dir,
254                  hwaddr addr, int32_t bootindex,
255                  bool option_rom, MemoryRegion *mr, AddressSpace *as);
256 MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
257                            size_t max_len, hwaddr addr,
258                            const char *fw_file_name,
259                            FWCfgCallback fw_callback,
260                            void *callback_opaque, AddressSpace *as,
261                            bool read_only);
262 int rom_add_elf_program(const char *name, GMappedFile *mapped_file, void *data,
263                         size_t datasize, size_t romsize, hwaddr addr,
264                         AddressSpace *as);
265 int rom_check_and_register_reset(void);
266 void rom_set_fw(FWCfgState *f);
267 void rom_set_order_override(int order);
268 void rom_reset_order_override(void);
269 
270 /**
271  * rom_transaction_begin:
272  *
273  * Call this before of a series of rom_add_*() calls.  Call
274  * rom_transaction_end() afterwards to commit or abort.  These functions are
275  * useful for undoing a series of rom_add_*() calls if image file loading fails
276  * partway through.
277  */
278 void rom_transaction_begin(void);
279 
280 /**
281  * rom_transaction_end:
282  * @commit: true to commit added roms, false to drop added roms
283  *
284  * Call this after a series of rom_add_*() calls.  See rom_transaction_begin().
285  */
286 void rom_transaction_end(bool commit);
287 
288 int rom_copy(uint8_t *dest, hwaddr addr, size_t size);
289 void *rom_ptr(hwaddr addr, size_t size);
290 void hmp_info_roms(Monitor *mon, const QDict *qdict);
291 
292 #define rom_add_file_fixed(_f, _a, _i)          \
293     rom_add_file(_f, NULL, _a, _i, false, NULL, NULL)
294 #define rom_add_blob_fixed(_f, _b, _l, _a)      \
295     rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, NULL, true)
296 #define rom_add_file_mr(_f, _mr, _i)            \
297     rom_add_file(_f, NULL, 0, _i, false, _mr, NULL)
298 #define rom_add_file_as(_f, _as, _i)            \
299     rom_add_file(_f, NULL, 0, _i, false, NULL, _as)
300 #define rom_add_file_fixed_as(_f, _a, _i, _as)          \
301     rom_add_file(_f, NULL, _a, _i, false, NULL, _as)
302 #define rom_add_blob_fixed_as(_f, _b, _l, _a, _as)      \
303     rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, _as, true)
304 
305 #define PC_ROM_MIN_VGA     0xc0000
306 #define PC_ROM_MIN_OPTION  0xc8000
307 #define PC_ROM_MAX         0xe0000
308 #define PC_ROM_ALIGN       0x800
309 #define PC_ROM_SIZE        (PC_ROM_MAX - PC_ROM_MIN_VGA)
310 
311 int rom_add_vga(const char *file);
312 int rom_add_option(const char *file, int32_t bootindex);
313 
314 /* This is the usual maximum in uboot, so if a uImage overflows this, it would
315  * overflow on real hardware too. */
316 #define UBOOT_MAX_GUNZIP_BYTES (64 << 20)
317 
318 #endif
319