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 "hw/acpi/bios-linker-loader.h"
23#include "hw/nvram/fw_cfg.h"
24
25#include "qemu/bswap.h"
26
27
28
29
30
31
32
33
34#define BIOS_LINKER_LOADER_FILESZ FW_CFG_MAX_FILE_PATH
35
36struct BiosLinkerLoaderEntry {
37 uint32_t command;
38 union {
39
40
41
42
43
44
45
46
47 struct {
48 char file[BIOS_LINKER_LOADER_FILESZ];
49 uint32_t align;
50 uint8_t zone;
51 } alloc;
52
53
54
55
56
57
58
59 struct {
60 char dest_file[BIOS_LINKER_LOADER_FILESZ];
61 char src_file[BIOS_LINKER_LOADER_FILESZ];
62 uint32_t offset;
63 uint8_t size;
64 } pointer;
65
66
67
68
69
70
71
72
73 struct {
74 char file[BIOS_LINKER_LOADER_FILESZ];
75 uint32_t offset;
76 uint32_t start;
77 uint32_t length;
78 } cksum;
79
80
81
82
83
84
85
86
87 struct {
88 char dest_file[BIOS_LINKER_LOADER_FILESZ];
89 char src_file[BIOS_LINKER_LOADER_FILESZ];
90 uint32_t dst_offset;
91 uint32_t src_offset;
92 uint8_t size;
93 } wr_pointer;
94
95
96 char pad[124];
97 };
98} QEMU_PACKED;
99typedef struct BiosLinkerLoaderEntry BiosLinkerLoaderEntry;
100
101enum {
102 BIOS_LINKER_LOADER_COMMAND_ALLOCATE = 0x1,
103 BIOS_LINKER_LOADER_COMMAND_ADD_POINTER = 0x2,
104 BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM = 0x3,
105 BIOS_LINKER_LOADER_COMMAND_WRITE_POINTER = 0x4,
106};
107
108enum {
109 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH = 0x1,
110 BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG = 0x2,
111};
112
113
114
115
116
117
118typedef struct BiosLinkerFileEntry {
119 char *name;
120 GArray *blob;
121} BiosLinkerFileEntry;
122
123
124
125
126
127
128
129BIOSLinker *bios_linker_loader_init(void)
130{
131 BIOSLinker *linker = g_new(BIOSLinker, 1);
132
133 linker->cmd_blob = g_array_new(false, true , 1);
134 linker->file_list = g_array_new(false, true ,
135 sizeof(BiosLinkerFileEntry));
136 return linker;
137}
138
139
140void bios_linker_loader_cleanup(BIOSLinker *linker)
141{
142 int i;
143 BiosLinkerFileEntry *entry;
144
145 g_array_free(linker->cmd_blob, true);
146
147 for (i = 0; i < linker->file_list->len; i++) {
148 entry = &g_array_index(linker->file_list, BiosLinkerFileEntry, i);
149 g_free(entry->name);
150 }
151 g_array_free(linker->file_list, true);
152 g_free(linker);
153}
154
155static const BiosLinkerFileEntry *
156bios_linker_find_file(const BIOSLinker *linker, const char *name)
157{
158 int i;
159 BiosLinkerFileEntry *entry;
160
161 for (i = 0; i < linker->file_list->len; i++) {
162 entry = &g_array_index(linker->file_list, BiosLinkerFileEntry, i);
163 if (!strcmp(entry->name, name)) {
164 return entry;
165 }
166 }
167 return NULL;
168}
169
170
171
172
173
174bool bios_linker_loader_can_write_pointer(void)
175{
176 FWCfgState *fw_cfg = fw_cfg_find();
177 return fw_cfg && fw_cfg_dma_enabled(fw_cfg);
178}
179
180
181
182
183
184
185
186
187
188
189
190
191void bios_linker_loader_alloc(BIOSLinker *linker,
192 const char *file_name,
193 GArray *file_blob,
194 uint32_t alloc_align,
195 bool alloc_fseg)
196{
197 BiosLinkerLoaderEntry entry;
198 BiosLinkerFileEntry file = { g_strdup(file_name), file_blob};
199
200 assert(!(alloc_align & (alloc_align - 1)));
201
202 assert(!bios_linker_find_file(linker, file_name));
203 g_array_append_val(linker->file_list, file);
204
205 memset(&entry, 0, sizeof entry);
206 strncpy(entry.alloc.file, file_name, sizeof entry.alloc.file - 1);
207 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ALLOCATE);
208 entry.alloc.align = cpu_to_le32(alloc_align);
209 entry.alloc.zone = alloc_fseg ? BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG :
210 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH;
211
212
213 g_array_prepend_vals(linker->cmd_blob, &entry, sizeof entry);
214}
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231void bios_linker_loader_add_checksum(BIOSLinker *linker, const char *file_name,
232 unsigned start_offset, unsigned size,
233 unsigned checksum_offset)
234{
235 BiosLinkerLoaderEntry entry;
236 const BiosLinkerFileEntry *file = bios_linker_find_file(linker, file_name);
237
238 assert(file);
239 assert(start_offset < file->blob->len);
240 assert(start_offset + size <= file->blob->len);
241 assert(checksum_offset >= start_offset);
242 assert(checksum_offset + 1 <= start_offset + size);
243
244 *(file->blob->data + checksum_offset) = 0;
245 memset(&entry, 0, sizeof entry);
246 strncpy(entry.cksum.file, file_name, sizeof entry.cksum.file - 1);
247 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM);
248 entry.cksum.offset = cpu_to_le32(checksum_offset);
249 entry.cksum.start = cpu_to_le32(start_offset);
250 entry.cksum.length = cpu_to_le32(size);
251
252 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry);
253}
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271void bios_linker_loader_add_pointer(BIOSLinker *linker,
272 const char *dest_file,
273 uint32_t dst_patched_offset,
274 uint8_t dst_patched_size,
275 const char *src_file,
276 uint32_t src_offset)
277{
278 uint64_t le_src_offset;
279 BiosLinkerLoaderEntry entry;
280 const BiosLinkerFileEntry *dst_file =
281 bios_linker_find_file(linker, dest_file);
282 const BiosLinkerFileEntry *source_file =
283 bios_linker_find_file(linker, src_file);
284
285 assert(dst_file);
286 assert(source_file);
287 assert(dst_patched_offset < dst_file->blob->len);
288 assert(dst_patched_offset + dst_patched_size <= dst_file->blob->len);
289 assert(src_offset < source_file->blob->len);
290
291 memset(&entry, 0, sizeof entry);
292 strncpy(entry.pointer.dest_file, dest_file,
293 sizeof entry.pointer.dest_file - 1);
294 strncpy(entry.pointer.src_file, src_file,
295 sizeof entry.pointer.src_file - 1);
296 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_POINTER);
297 entry.pointer.offset = cpu_to_le32(dst_patched_offset);
298 entry.pointer.size = dst_patched_size;
299 assert(dst_patched_size == 1 || dst_patched_size == 2 ||
300 dst_patched_size == 4 || dst_patched_size == 8);
301
302 le_src_offset = cpu_to_le64(src_offset);
303 memcpy(dst_file->blob->data + dst_patched_offset,
304 &le_src_offset, dst_patched_size);
305
306 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry);
307}
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325void bios_linker_loader_write_pointer(BIOSLinker *linker,
326 const char *dest_file,
327 uint32_t dst_patched_offset,
328 uint8_t dst_patched_size,
329 const char *src_file,
330 uint32_t src_offset)
331{
332 BiosLinkerLoaderEntry entry;
333 const BiosLinkerFileEntry *source_file =
334 bios_linker_find_file(linker, src_file);
335
336 assert(source_file);
337 assert(src_offset < source_file->blob->len);
338 memset(&entry, 0, sizeof entry);
339 strncpy(entry.wr_pointer.dest_file, dest_file,
340 sizeof entry.wr_pointer.dest_file - 1);
341 strncpy(entry.wr_pointer.src_file, src_file,
342 sizeof entry.wr_pointer.src_file - 1);
343 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_WRITE_POINTER);
344 entry.wr_pointer.dst_offset = cpu_to_le32(dst_patched_offset);
345 entry.wr_pointer.src_offset = cpu_to_le32(src_offset);
346 entry.wr_pointer.size = dst_patched_size;
347 assert(dst_patched_size == 1 || dst_patched_size == 2 ||
348 dst_patched_size == 4 || dst_patched_size == 8);
349
350 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry);
351}
352