1 /* Dynamic linker/loader of ACPI tables
3 * Copyright (C) 2013 Red Hat Inc
5 * Author: Michael S. Tsirkin <mst@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu-common.h"
23 #include "hw/acpi/bios-linker-loader.h"
24 #include "hw/nvram/fw_cfg.h"
26 #include "qemu/bswap.h"
29 * Linker/loader is a paravirtualized interface that passes commands to guest.
30 * The commands can be used to request guest to
31 * - allocate memory chunks and initialize them from QEMU FW CFG files
32 * - link allocated chunks by storing pointer to one chunk into another
33 * - calculate ACPI checksum of part of the chunk and store into same chunk
35 #define BIOS_LINKER_LOADER_FILESZ FW_CFG_MAX_FILE_PATH
37 struct BiosLinkerLoaderEntry
{
41 * COMMAND_ALLOCATE - allocate a table from @alloc.file
42 * subject to @alloc.align alignment (must be power of 2)
43 * and @alloc.zone (can be HIGH or FSEG) requirements.
45 * Must appear exactly once for each file, and before
46 * this file is referenced by any other command.
49 char file
[BIOS_LINKER_LOADER_FILESZ
];
55 * COMMAND_ADD_POINTER - patch the table (originating from
56 * @dest_file) at @pointer.offset, by adding a pointer to the table
57 * originating from @src_file. 1,2,4 or 8 byte unsigned
58 * addition is used depending on @pointer.size.
61 char dest_file
[BIOS_LINKER_LOADER_FILESZ
];
62 char src_file
[BIOS_LINKER_LOADER_FILESZ
];
68 * COMMAND_ADD_CHECKSUM - calculate checksum of the range specified by
69 * @cksum_start and @cksum_length fields,
70 * and then add the value at @cksum.offset.
71 * Checksum simply sums -X for each byte X in the range
75 char file
[BIOS_LINKER_LOADER_FILESZ
];
82 * COMMAND_WRITE_POINTER - write the fw_cfg file (originating from
83 * @dest_file) at @wr_pointer.offset, by adding a pointer to
84 * @src_offset within the table originating from @src_file.
85 * 1,2,4 or 8 byte unsigned addition is used depending on
89 char dest_file
[BIOS_LINKER_LOADER_FILESZ
];
90 char src_file
[BIOS_LINKER_LOADER_FILESZ
];
100 typedef struct BiosLinkerLoaderEntry BiosLinkerLoaderEntry
;
103 BIOS_LINKER_LOADER_COMMAND_ALLOCATE
= 0x1,
104 BIOS_LINKER_LOADER_COMMAND_ADD_POINTER
= 0x2,
105 BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM
= 0x3,
106 BIOS_LINKER_LOADER_COMMAND_WRITE_POINTER
= 0x4,
110 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH
= 0x1,
111 BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG
= 0x2,
115 * BiosLinkerFileEntry:
117 * An internal type used for book-keeping file entries
119 typedef struct BiosLinkerFileEntry
{
120 char *name
; /* file name */
121 GArray
*blob
; /* data accosiated with @name */
122 } BiosLinkerFileEntry
;
125 * bios_linker_loader_init: allocate a new linker object instance.
127 * After initialization, linker commands can be added, and will
128 * be stored in the linker.cmd_blob array.
130 BIOSLinker
*bios_linker_loader_init(void)
132 BIOSLinker
*linker
= g_new(BIOSLinker
, 1);
134 linker
->cmd_blob
= g_array_new(false, true /* clear */, 1);
135 linker
->file_list
= g_array_new(false, true /* clear */,
136 sizeof(BiosLinkerFileEntry
));
140 /* Free linker wrapper */
141 void bios_linker_loader_cleanup(BIOSLinker
*linker
)
144 BiosLinkerFileEntry
*entry
;
146 g_array_free(linker
->cmd_blob
, true);
148 for (i
= 0; i
< linker
->file_list
->len
; i
++) {
149 entry
= &g_array_index(linker
->file_list
, BiosLinkerFileEntry
, i
);
152 g_array_free(linker
->file_list
, true);
156 static const BiosLinkerFileEntry
*
157 bios_linker_find_file(const BIOSLinker
*linker
, const char *name
)
160 BiosLinkerFileEntry
*entry
;
162 for (i
= 0; i
< linker
->file_list
->len
; i
++) {
163 entry
= &g_array_index(linker
->file_list
, BiosLinkerFileEntry
, i
);
164 if (!strcmp(entry
->name
, name
)) {
172 * bios_linker_loader_alloc: ask guest to load file into guest memory.
174 * @linker: linker object instance
175 * @file_name: name of the file blob to be loaded
176 * @file_blob: pointer to blob corresponding to @file_name
177 * @alloc_align: required minimal alignment in bytes. Must be a power of 2.
178 * @alloc_fseg: request allocation in FSEG zone (useful for the RSDP ACPI table)
180 * Note: this command must precede any other linker command using this file.
182 void bios_linker_loader_alloc(BIOSLinker
*linker
,
183 const char *file_name
,
185 uint32_t alloc_align
,
188 BiosLinkerLoaderEntry entry
;
189 BiosLinkerFileEntry file
= { g_strdup(file_name
), file_blob
};
191 assert(!(alloc_align
& (alloc_align
- 1)));
193 assert(!bios_linker_find_file(linker
, file_name
));
194 g_array_append_val(linker
->file_list
, file
);
196 memset(&entry
, 0, sizeof entry
);
197 strncpy(entry
.alloc
.file
, file_name
, sizeof entry
.alloc
.file
- 1);
198 entry
.command
= cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ALLOCATE
);
199 entry
.alloc
.align
= cpu_to_le32(alloc_align
);
200 entry
.alloc
.zone
= alloc_fseg
? BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG
:
201 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH
;
203 /* Alloc entries must come first, so prepend them */
204 g_array_prepend_vals(linker
->cmd_blob
, &entry
, sizeof entry
);
208 * bios_linker_loader_add_checksum: ask guest to add checksum of ACPI
209 * table in the specified file at the specified offset.
211 * Checksum calculation simply sums -X for each byte X in the range
212 * using 8-bit math (i.e. ACPI checksum).
214 * @linker: linker object instance
215 * @file: file that includes the checksum to be calculated
216 * and the data to be checksummed
217 * @start_offset, @size: range of data in the file to checksum,
218 * relative to the start of file blob
219 * @checksum_offset: location of the checksum to be patched within file blob,
220 * relative to the start of file blob
222 void bios_linker_loader_add_checksum(BIOSLinker
*linker
, const char *file_name
,
223 unsigned start_offset
, unsigned size
,
224 unsigned checksum_offset
)
226 BiosLinkerLoaderEntry entry
;
227 const BiosLinkerFileEntry
*file
= bios_linker_find_file(linker
, file_name
);
230 assert(start_offset
< file
->blob
->len
);
231 assert(start_offset
+ size
<= file
->blob
->len
);
232 assert(checksum_offset
>= start_offset
);
233 assert(checksum_offset
+ 1 <= start_offset
+ size
);
235 *(file
->blob
->data
+ checksum_offset
) = 0;
236 memset(&entry
, 0, sizeof entry
);
237 strncpy(entry
.cksum
.file
, file_name
, sizeof entry
.cksum
.file
- 1);
238 entry
.command
= cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM
);
239 entry
.cksum
.offset
= cpu_to_le32(checksum_offset
);
240 entry
.cksum
.start
= cpu_to_le32(start_offset
);
241 entry
.cksum
.length
= cpu_to_le32(size
);
243 g_array_append_vals(linker
->cmd_blob
, &entry
, sizeof entry
);
247 * bios_linker_loader_add_pointer: ask guest to patch address in
248 * destination file with a pointer to source file
250 * @linker: linker object instance
251 * @dest_file: destination file that must be changed
252 * @dst_patched_offset: location within destination file blob to be patched
253 * with the pointer to @src_file+@src_offset (i.e. source
254 * blob allocated in guest memory + @src_offset), in bytes
255 * @dst_patched_offset_size: size of the pointer to be patched
256 * at @dst_patched_offset in @dest_file blob, in bytes
257 * @src_file: source file who's address must be taken
258 * @src_offset: location within source file blob to which
259 * @dest_file+@dst_patched_offset will point to after
260 * firmware's executed ADD_POINTER command
262 void bios_linker_loader_add_pointer(BIOSLinker
*linker
,
263 const char *dest_file
,
264 uint32_t dst_patched_offset
,
265 uint8_t dst_patched_size
,
266 const char *src_file
,
269 uint64_t le_src_offset
;
270 BiosLinkerLoaderEntry entry
;
271 const BiosLinkerFileEntry
*dst_file
=
272 bios_linker_find_file(linker
, dest_file
);
273 const BiosLinkerFileEntry
*source_file
=
274 bios_linker_find_file(linker
, src_file
);
276 assert(dst_patched_offset
< dst_file
->blob
->len
);
277 assert(dst_patched_offset
+ dst_patched_size
<= dst_file
->blob
->len
);
278 assert(src_offset
< source_file
->blob
->len
);
280 memset(&entry
, 0, sizeof entry
);
281 strncpy(entry
.pointer
.dest_file
, dest_file
,
282 sizeof entry
.pointer
.dest_file
- 1);
283 strncpy(entry
.pointer
.src_file
, src_file
,
284 sizeof entry
.pointer
.src_file
- 1);
285 entry
.command
= cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_POINTER
);
286 entry
.pointer
.offset
= cpu_to_le32(dst_patched_offset
);
287 entry
.pointer
.size
= dst_patched_size
;
288 assert(dst_patched_size
== 1 || dst_patched_size
== 2 ||
289 dst_patched_size
== 4 || dst_patched_size
== 8);
291 le_src_offset
= cpu_to_le64(src_offset
);
292 memcpy(dst_file
->blob
->data
+ dst_patched_offset
,
293 &le_src_offset
, dst_patched_size
);
295 g_array_append_vals(linker
->cmd_blob
, &entry
, sizeof entry
);
299 * bios_linker_loader_write_pointer: ask guest to write a pointer to the
300 * source file into the destination file, and write it back to QEMU via
303 * @linker: linker object instance
304 * @dest_file: destination file that must be written
305 * @dst_patched_offset: location within destination file blob to be patched
306 * with the pointer to @src_file, in bytes
307 * @dst_patched_offset_size: size of the pointer to be patched
308 * at @dst_patched_offset in @dest_file blob, in bytes
309 * @src_file: source file who's address must be taken
310 * @src_offset: location within source file blob to which
311 * @dest_file+@dst_patched_offset will point to after
312 * firmware's executed WRITE_POINTER command
314 void bios_linker_loader_write_pointer(BIOSLinker
*linker
,
315 const char *dest_file
,
316 uint32_t dst_patched_offset
,
317 uint8_t dst_patched_size
,
318 const char *src_file
,
321 BiosLinkerLoaderEntry entry
;
322 const BiosLinkerFileEntry
*source_file
=
323 bios_linker_find_file(linker
, src_file
);
326 assert(src_offset
< source_file
->blob
->len
);
327 memset(&entry
, 0, sizeof entry
);
328 strncpy(entry
.wr_pointer
.dest_file
, dest_file
,
329 sizeof entry
.wr_pointer
.dest_file
- 1);
330 strncpy(entry
.wr_pointer
.src_file
, src_file
,
331 sizeof entry
.wr_pointer
.src_file
- 1);
332 entry
.command
= cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_WRITE_POINTER
);
333 entry
.wr_pointer
.dst_offset
= cpu_to_le32(dst_patched_offset
);
334 entry
.wr_pointer
.src_offset
= cpu_to_le32(src_offset
);
335 entry
.wr_pointer
.size
= dst_patched_size
;
336 assert(dst_patched_size
== 1 || dst_patched_size
== 2 ||
337 dst_patched_size
== 4 || dst_patched_size
== 8);
339 g_array_append_vals(linker
->cmd_blob
, &entry
, sizeof entry
);