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