]> Git Repo - qemu.git/blame - hw/acpi/bios-linker-loader.c
hw/intc/arm_gicv3: Fix secure-GIC NS ICC_PMR and ICC_RPR accesses
[qemu.git] / hw / acpi / bios-linker-loader.c
CommitLineData
bc702329
MT
1/* Dynamic linker/loader of ACPI tables
2 *
3 * Copyright (C) 2013 Red Hat Inc
4 *
5 * Author: Michael S. Tsirkin <[email protected]>
6 *
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.
11
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.
16
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/>.
19 */
20
b6a0aa05 21#include "qemu/osdep.h"
c428c5a2 22#include "qemu-common.h"
0058ae1d 23#include "hw/acpi/bios-linker-loader.h"
bc702329
MT
24#include "hw/nvram/fw_cfg.h"
25
bc702329
MT
26#include "qemu/bswap.h"
27
b54ca0c3
MT
28/*
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
34 */
bc702329
MT
35#define BIOS_LINKER_LOADER_FILESZ FW_CFG_MAX_FILE_PATH
36
37struct BiosLinkerLoaderEntry {
38 uint32_t command;
39 union {
40 /*
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.
44 *
45 * Must appear exactly once for each file, and before
46 * this file is referenced by any other command.
47 */
48 struct {
49 char file[BIOS_LINKER_LOADER_FILESZ];
50 uint32_t align;
51 uint8_t zone;
52 } alloc;
53
54 /*
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.
59 */
60 struct {
61 char dest_file[BIOS_LINKER_LOADER_FILESZ];
62 char src_file[BIOS_LINKER_LOADER_FILESZ];
63 uint32_t offset;
64 uint8_t size;
65 } pointer;
66
67 /*
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
72 * using 8-bit math.
73 */
74 struct {
75 char file[BIOS_LINKER_LOADER_FILESZ];
76 uint32_t offset;
77 uint32_t start;
78 uint32_t length;
79 } cksum;
80
489886d1
BW
81 /*
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
86 * @wr_pointer.size.
87 */
88 struct {
89 char dest_file[BIOS_LINKER_LOADER_FILESZ];
90 char src_file[BIOS_LINKER_LOADER_FILESZ];
91 uint32_t dst_offset;
92 uint32_t src_offset;
93 uint8_t size;
94 } wr_pointer;
95
bc702329
MT
96 /* padding */
97 char pad[124];
98 };
99} QEMU_PACKED;
100typedef struct BiosLinkerLoaderEntry BiosLinkerLoaderEntry;
101
102enum {
489886d1
BW
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,
bc702329
MT
107};
108
109enum {
110 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH = 0x1,
111 BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG = 0x2,
112};
113
ad9671b8
IM
114/*
115 * BiosLinkerFileEntry:
116 *
117 * An internal type used for book-keeping file entries
118 */
119typedef struct BiosLinkerFileEntry {
120 char *name; /* file name */
121 GArray *blob; /* data accosiated with @name */
122} BiosLinkerFileEntry;
123
b54ca0c3 124/*
0e9b9eda 125 * bios_linker_loader_init: allocate a new linker object instance.
b54ca0c3
MT
126 *
127 * After initialization, linker commands can be added, and will
0e9b9eda 128 * be stored in the linker.cmd_blob array.
b54ca0c3 129 */
0e9b9eda 130BIOSLinker *bios_linker_loader_init(void)
bc702329 131{
0e9b9eda
IM
132 BIOSLinker *linker = g_new(BIOSLinker, 1);
133
134 linker->cmd_blob = g_array_new(false, true /* clear */, 1);
ad9671b8
IM
135 linker->file_list = g_array_new(false, true /* clear */,
136 sizeof(BiosLinkerFileEntry));
0e9b9eda 137 return linker;
bc702329
MT
138}
139
8cc87c31
IM
140/* Free linker wrapper */
141void bios_linker_loader_cleanup(BIOSLinker *linker)
bc702329 142{
ad9671b8
IM
143 int i;
144 BiosLinkerFileEntry *entry;
8cc87c31
IM
145
146 g_array_free(linker->cmd_blob, true);
0e9b9eda 147
ad9671b8
IM
148 for (i = 0; i < linker->file_list->len; i++) {
149 entry = &g_array_index(linker->file_list, BiosLinkerFileEntry, i);
150 g_free(entry->name);
151 }
152 g_array_free(linker->file_list, true);
0e9b9eda 153 g_free(linker);
bc702329
MT
154}
155
ad9671b8
IM
156static const BiosLinkerFileEntry *
157bios_linker_find_file(const BIOSLinker *linker, const char *name)
158{
159 int i;
160 BiosLinkerFileEntry *entry;
161
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)) {
165 return entry;
166 }
167 }
168 return NULL;
169}
170
c8389550
MAL
171/*
172 * board code must realize fw_cfg first, as a fixed device, before
173 * another device realize function call bios_linker_loader_can_write_pointer()
174 */
175bool bios_linker_loader_can_write_pointer(void)
176{
177 FWCfgState *fw_cfg = fw_cfg_find();
178 return fw_cfg && fw_cfg_dma_enabled(fw_cfg);
179}
180
b54ca0c3
MT
181/*
182 * bios_linker_loader_alloc: ask guest to load file into guest memory.
183 *
0e9b9eda 184 * @linker: linker object instance
ad9671b8
IM
185 * @file_name: name of the file blob to be loaded
186 * @file_blob: pointer to blob corresponding to @file_name
b54ca0c3
MT
187 * @alloc_align: required minimal alignment in bytes. Must be a power of 2.
188 * @alloc_fseg: request allocation in FSEG zone (useful for the RSDP ACPI table)
189 *
190 * Note: this command must precede any other linker command using this file.
191 */
0e9b9eda 192void bios_linker_loader_alloc(BIOSLinker *linker,
ad9671b8
IM
193 const char *file_name,
194 GArray *file_blob,
bc702329
MT
195 uint32_t alloc_align,
196 bool alloc_fseg)
197{
198 BiosLinkerLoaderEntry entry;
ad9671b8 199 BiosLinkerFileEntry file = { g_strdup(file_name), file_blob};
bc702329 200
b54ca0c3
MT
201 assert(!(alloc_align & (alloc_align - 1)));
202
ad9671b8
IM
203 assert(!bios_linker_find_file(linker, file_name));
204 g_array_append_val(linker->file_list, file);
205
bc702329 206 memset(&entry, 0, sizeof entry);
ad9671b8 207 strncpy(entry.alloc.file, file_name, sizeof entry.alloc.file - 1);
bc702329
MT
208 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ALLOCATE);
209 entry.alloc.align = cpu_to_le32(alloc_align);
1dbfd789
IM
210 entry.alloc.zone = alloc_fseg ? BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG :
211 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH;
bc702329
MT
212
213 /* Alloc entries must come first, so prepend them */
0e9b9eda 214 g_array_prepend_vals(linker->cmd_blob, &entry, sizeof entry);
bc702329
MT
215}
216
b54ca0c3 217/*
28213cb6
IM
218 * bios_linker_loader_add_checksum: ask guest to add checksum of ACPI
219 * table in the specified file at the specified offset.
b54ca0c3
MT
220 *
221 * Checksum calculation simply sums -X for each byte X in the range
222 * using 8-bit math (i.e. ACPI checksum).
223 *
0e9b9eda 224 * @linker: linker object instance
b54ca0c3
MT
225 * @file: file that includes the checksum to be calculated
226 * and the data to be checksummed
28213cb6
IM
227 * @start_offset, @size: range of data in the file to checksum,
228 * relative to the start of file blob
229 * @checksum_offset: location of the checksum to be patched within file blob,
230 * relative to the start of file blob
b54ca0c3 231 */
ad9671b8 232void bios_linker_loader_add_checksum(BIOSLinker *linker, const char *file_name,
28213cb6
IM
233 unsigned start_offset, unsigned size,
234 unsigned checksum_offset)
bc702329
MT
235{
236 BiosLinkerLoaderEntry entry;
ad9671b8 237 const BiosLinkerFileEntry *file = bios_linker_find_file(linker, file_name);
b54ca0c3 238
28213cb6
IM
239 assert(file);
240 assert(start_offset < file->blob->len);
ad9671b8 241 assert(start_offset + size <= file->blob->len);
28213cb6
IM
242 assert(checksum_offset >= start_offset);
243 assert(checksum_offset + 1 <= start_offset + size);
bc702329 244
28213cb6 245 *(file->blob->data + checksum_offset) = 0;
bc702329 246 memset(&entry, 0, sizeof entry);
ad9671b8 247 strncpy(entry.cksum.file, file_name, sizeof entry.cksum.file - 1);
bc702329 248 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM);
b54ca0c3
MT
249 entry.cksum.offset = cpu_to_le32(checksum_offset);
250 entry.cksum.start = cpu_to_le32(start_offset);
bc702329
MT
251 entry.cksum.length = cpu_to_le32(size);
252
0e9b9eda 253 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry);
bc702329
MT
254}
255
b54ca0c3 256/*
4678124b
IM
257 * bios_linker_loader_add_pointer: ask guest to patch address in
258 * destination file with a pointer to source file
b54ca0c3 259 *
0e9b9eda 260 * @linker: linker object instance
b54ca0c3 261 * @dest_file: destination file that must be changed
4678124b
IM
262 * @dst_patched_offset: location within destination file blob to be patched
263 * with the pointer to @src_file+@src_offset (i.e. source
264 * blob allocated in guest memory + @src_offset), in bytes
265 * @dst_patched_offset_size: size of the pointer to be patched
266 * at @dst_patched_offset in @dest_file blob, in bytes
b54ca0c3 267 * @src_file: source file who's address must be taken
4678124b
IM
268 * @src_offset: location within source file blob to which
269 * @dest_file+@dst_patched_offset will point to after
270 * firmware's executed ADD_POINTER command
b54ca0c3 271 */
0e9b9eda 272void bios_linker_loader_add_pointer(BIOSLinker *linker,
bc702329 273 const char *dest_file,
4678124b
IM
274 uint32_t dst_patched_offset,
275 uint8_t dst_patched_size,
bc702329 276 const char *src_file,
4678124b 277 uint32_t src_offset)
bc702329 278{
4678124b 279 uint64_t le_src_offset;
bc702329 280 BiosLinkerLoaderEntry entry;
4678124b
IM
281 const BiosLinkerFileEntry *dst_file =
282 bios_linker_find_file(linker, dest_file);
283 const BiosLinkerFileEntry *source_file =
284 bios_linker_find_file(linker, src_file);
b54ca0c3 285
4678124b
IM
286 assert(dst_patched_offset < dst_file->blob->len);
287 assert(dst_patched_offset + dst_patched_size <= dst_file->blob->len);
288 assert(src_offset < source_file->blob->len);
bc702329
MT
289
290 memset(&entry, 0, sizeof entry);
291 strncpy(entry.pointer.dest_file, dest_file,
292 sizeof entry.pointer.dest_file - 1);
293 strncpy(entry.pointer.src_file, src_file,
294 sizeof entry.pointer.src_file - 1);
295 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_POINTER);
4678124b
IM
296 entry.pointer.offset = cpu_to_le32(dst_patched_offset);
297 entry.pointer.size = dst_patched_size;
298 assert(dst_patched_size == 1 || dst_patched_size == 2 ||
299 dst_patched_size == 4 || dst_patched_size == 8);
300
301 le_src_offset = cpu_to_le64(src_offset);
302 memcpy(dst_file->blob->data + dst_patched_offset,
303 &le_src_offset, dst_patched_size);
bc702329 304
0e9b9eda 305 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry);
bc702329 306}
489886d1
BW
307
308/*
309 * bios_linker_loader_write_pointer: ask guest to write a pointer to the
310 * source file into the destination file, and write it back to QEMU via
311 * fw_cfg DMA.
312 *
313 * @linker: linker object instance
314 * @dest_file: destination file that must be written
315 * @dst_patched_offset: location within destination file blob to be patched
316 * with the pointer to @src_file, in bytes
317 * @dst_patched_offset_size: size of the pointer to be patched
318 * at @dst_patched_offset in @dest_file blob, in bytes
319 * @src_file: source file who's address must be taken
320 * @src_offset: location within source file blob to which
321 * @dest_file+@dst_patched_offset will point to after
322 * firmware's executed WRITE_POINTER command
323 */
324void bios_linker_loader_write_pointer(BIOSLinker *linker,
325 const char *dest_file,
326 uint32_t dst_patched_offset,
327 uint8_t dst_patched_size,
328 const char *src_file,
329 uint32_t src_offset)
330{
331 BiosLinkerLoaderEntry entry;
332 const BiosLinkerFileEntry *source_file =
333 bios_linker_find_file(linker, src_file);
334
335 assert(source_file);
336 assert(src_offset < source_file->blob->len);
337 memset(&entry, 0, sizeof entry);
338 strncpy(entry.wr_pointer.dest_file, dest_file,
339 sizeof entry.wr_pointer.dest_file - 1);
340 strncpy(entry.wr_pointer.src_file, src_file,
341 sizeof entry.wr_pointer.src_file - 1);
342 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_WRITE_POINTER);
343 entry.wr_pointer.dst_offset = cpu_to_le32(dst_patched_offset);
344 entry.wr_pointer.src_offset = cpu_to_le32(src_offset);
345 entry.wr_pointer.size = dst_patched_size;
346 assert(dst_patched_size == 1 || dst_patched_size == 2 ||
347 dst_patched_size == 4 || dst_patched_size == 8);
348
349 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry);
350}
This page took 0.257041 seconds and 4 git commands to generate.