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