1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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"
25
26#include "qemu/bswap.h"
27
28
29
30
31
32
33
34
35#define BIOS_LINKER_LOADER_FILESZ FW_CFG_MAX_FILE_PATH
36
37struct BiosLinkerLoaderEntry {
38 uint32_t command;
39 union {
40
41
42
43
44
45
46
47
48 struct {
49 char file[BIOS_LINKER_LOADER_FILESZ];
50 uint32_t align;
51 uint8_t zone;
52 } alloc;
53
54
55
56
57
58
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
69
70
71
72
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
81
82
83
84
85
86
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
96
97 char pad[124];
98 };
99} QEMU_PACKED;
100typedef struct BiosLinkerLoaderEntry BiosLinkerLoaderEntry;
101
102enum {
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,
107};
108
109enum {
110 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH = 0x1,
111 BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG = 0x2,
112};
113
114
115
116
117
118
119typedef struct BiosLinkerFileEntry {
120 char *name;
121 GArray *blob;
122} BiosLinkerFileEntry;
123
124
125
126
127
128
129
130BIOSLinker *bios_linker_loader_init(void)
131{
132 BIOSLinker *linker = g_new(BIOSLinker, 1);
133
134 linker->cmd_blob = g_array_new(false, true , 1);
135 linker->file_list = g_array_new(false, true ,
136 sizeof(BiosLinkerFileEntry));
137 return linker;
138}
139
140
141void bios_linker_loader_cleanup(BIOSLinker *linker)
142{
143 int i;
144 BiosLinkerFileEntry *entry;
145
146 g_array_free(linker->cmd_blob, true);
147
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);
153 g_free(linker);
154}
155
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
171
172
173
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
181
182
183
184
185
186
187
188
189
190
191
192void bios_linker_loader_alloc(BIOSLinker *linker,
193 const char *file_name,
194 GArray *file_blob,
195 uint32_t alloc_align,
196 bool alloc_fseg)
197{
198 BiosLinkerLoaderEntry entry;
199 BiosLinkerFileEntry file = { g_strdup(file_name), file_blob};
200
201 assert(!(alloc_align & (alloc_align - 1)));
202
203 assert(!bios_linker_find_file(linker, file_name));
204 g_array_append_val(linker->file_list, file);
205
206 memset(&entry, 0, sizeof entry);
207 strncpy(entry.alloc.file, file_name, sizeof entry.alloc.file - 1);
208 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ALLOCATE);
209 entry.alloc.align = cpu_to_le32(alloc_align);
210 entry.alloc.zone = alloc_fseg ? BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG :
211 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH;
212
213
214 g_array_prepend_vals(linker->cmd_blob, &entry, sizeof entry);
215}
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232void bios_linker_loader_add_checksum(BIOSLinker *linker, const char *file_name,
233 unsigned start_offset, unsigned size,
234 unsigned checksum_offset)
235{
236 BiosLinkerLoaderEntry entry;
237 const BiosLinkerFileEntry *file = bios_linker_find_file(linker, file_name);
238
239 assert(file);
240 assert(start_offset < file->blob->len);
241 assert(start_offset + size <= file->blob->len);
242 assert(checksum_offset >= start_offset);
243 assert(checksum_offset + 1 <= start_offset + size);
244
245 *(file->blob->data + checksum_offset) = 0;
246 memset(&entry, 0, sizeof entry);
247 strncpy(entry.cksum.file, file_name, sizeof entry.cksum.file - 1);
248 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM);
249 entry.cksum.offset = cpu_to_le32(checksum_offset);
250 entry.cksum.start = cpu_to_le32(start_offset);
251 entry.cksum.length = cpu_to_le32(size);
252
253 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry);
254}
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272void bios_linker_loader_add_pointer(BIOSLinker *linker,
273 const char *dest_file,
274 uint32_t dst_patched_offset,
275 uint8_t dst_patched_size,
276 const char *src_file,
277 uint32_t src_offset)
278{
279 uint64_t le_src_offset;
280 BiosLinkerLoaderEntry entry;
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);
285
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);
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);
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);
304
305 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry);
306}
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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}
351