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 char pad[124];
83 };
84} QEMU_PACKED;
85typedef struct BiosLinkerLoaderEntry BiosLinkerLoaderEntry;
86
87enum {
88 BIOS_LINKER_LOADER_COMMAND_ALLOCATE = 0x1,
89 BIOS_LINKER_LOADER_COMMAND_ADD_POINTER = 0x2,
90 BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM = 0x3,
91};
92
93enum {
94 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH = 0x1,
95 BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG = 0x2,
96};
97
98
99
100
101
102
103typedef struct BiosLinkerFileEntry {
104 char *name;
105 GArray *blob;
106} BiosLinkerFileEntry;
107
108
109
110
111
112
113
114BIOSLinker *bios_linker_loader_init(void)
115{
116 BIOSLinker *linker = g_new(BIOSLinker, 1);
117
118 linker->cmd_blob = g_array_new(false, true , 1);
119 linker->file_list = g_array_new(false, true ,
120 sizeof(BiosLinkerFileEntry));
121 return linker;
122}
123
124
125void bios_linker_loader_cleanup(BIOSLinker *linker)
126{
127 int i;
128 BiosLinkerFileEntry *entry;
129
130 g_array_free(linker->cmd_blob, true);
131
132 for (i = 0; i < linker->file_list->len; i++) {
133 entry = &g_array_index(linker->file_list, BiosLinkerFileEntry, i);
134 g_free(entry->name);
135 }
136 g_array_free(linker->file_list, true);
137 g_free(linker);
138}
139
140static const BiosLinkerFileEntry *
141bios_linker_find_file(const BIOSLinker *linker, const char *name)
142{
143 int i;
144 BiosLinkerFileEntry *entry;
145
146 for (i = 0; i < linker->file_list->len; i++) {
147 entry = &g_array_index(linker->file_list, BiosLinkerFileEntry, i);
148 if (!strcmp(entry->name, name)) {
149 return entry;
150 }
151 }
152 return NULL;
153}
154
155
156
157
158
159
160
161
162
163
164
165
166void bios_linker_loader_alloc(BIOSLinker *linker,
167 const char *file_name,
168 GArray *file_blob,
169 uint32_t alloc_align,
170 bool alloc_fseg)
171{
172 BiosLinkerLoaderEntry entry;
173 BiosLinkerFileEntry file = { g_strdup(file_name), file_blob};
174
175 assert(!(alloc_align & (alloc_align - 1)));
176
177 assert(!bios_linker_find_file(linker, file_name));
178 g_array_append_val(linker->file_list, file);
179
180 memset(&entry, 0, sizeof entry);
181 strncpy(entry.alloc.file, file_name, sizeof entry.alloc.file - 1);
182 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ALLOCATE);
183 entry.alloc.align = cpu_to_le32(alloc_align);
184 entry.alloc.zone = alloc_fseg ? BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG :
185 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH;
186
187
188 g_array_prepend_vals(linker->cmd_blob, &entry, sizeof entry);
189}
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206void bios_linker_loader_add_checksum(BIOSLinker *linker, const char *file_name,
207 unsigned start_offset, unsigned size,
208 unsigned checksum_offset)
209{
210 BiosLinkerLoaderEntry entry;
211 const BiosLinkerFileEntry *file = bios_linker_find_file(linker, file_name);
212
213 assert(file);
214 assert(start_offset < file->blob->len);
215 assert(start_offset + size <= file->blob->len);
216 assert(checksum_offset >= start_offset);
217 assert(checksum_offset + 1 <= start_offset + size);
218
219 *(file->blob->data + checksum_offset) = 0;
220 memset(&entry, 0, sizeof entry);
221 strncpy(entry.cksum.file, file_name, sizeof entry.cksum.file - 1);
222 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM);
223 entry.cksum.offset = cpu_to_le32(checksum_offset);
224 entry.cksum.start = cpu_to_le32(start_offset);
225 entry.cksum.length = cpu_to_le32(size);
226
227 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry);
228}
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246void bios_linker_loader_add_pointer(BIOSLinker *linker,
247 const char *dest_file,
248 uint32_t dst_patched_offset,
249 uint8_t dst_patched_size,
250 const char *src_file,
251 uint32_t src_offset)
252{
253 uint64_t le_src_offset;
254 BiosLinkerLoaderEntry entry;
255 const BiosLinkerFileEntry *dst_file =
256 bios_linker_find_file(linker, dest_file);
257 const BiosLinkerFileEntry *source_file =
258 bios_linker_find_file(linker, src_file);
259
260 assert(dst_patched_offset < dst_file->blob->len);
261 assert(dst_patched_offset + dst_patched_size <= dst_file->blob->len);
262 assert(src_offset < source_file->blob->len);
263
264 memset(&entry, 0, sizeof entry);
265 strncpy(entry.pointer.dest_file, dest_file,
266 sizeof entry.pointer.dest_file - 1);
267 strncpy(entry.pointer.src_file, src_file,
268 sizeof entry.pointer.src_file - 1);
269 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_POINTER);
270 entry.pointer.offset = cpu_to_le32(dst_patched_offset);
271 entry.pointer.size = dst_patched_size;
272 assert(dst_patched_size == 1 || dst_patched_size == 2 ||
273 dst_patched_size == 4 || dst_patched_size == 8);
274
275 le_src_offset = cpu_to_le64(src_offset);
276 memcpy(dst_file->blob->data + dst_patched_offset,
277 &le_src_offset, dst_patched_size);
278
279 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry);
280}
281