1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG
* Author: Corvin Köhne <c.koehne@beckhoff.com>
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/endian.h>
#include <sys/queue.h>
#include <machine/vmm.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vmmapi.h>
#include "qemu_fwcfg.h"
#include "qemu_loader.h"
#ifndef __FreeBSD__
/*
* This is better than the number of ifdef blocks that would be otherwise
* required throughout this code. Hopefully upstream will clear up the
* char* /uint8_t * confusion.
*/
#define strlen(x) strlen((char *)(x))
#define strncpy(p, q, n) strncpy((char *)(p), (char *)(q), (n))
#endif
struct qemu_loader_entry {
uint32_t cmd_le;
union {
struct {
uint8_t name[QEMU_FWCFG_MAX_NAME];
uint32_t alignment_le;
uint8_t zone;
} alloc;
struct {
uint8_t dest_name[QEMU_FWCFG_MAX_NAME];
uint8_t src_name[QEMU_FWCFG_MAX_NAME];
uint32_t off_le;
uint8_t size;
} add_pointer;
struct {
uint8_t name[QEMU_FWCFG_MAX_NAME];
uint32_t off_le;
uint32_t start_le;
uint32_t len_le;
} add_checksum;
struct {
uint8_t dest_name[QEMU_FWCFG_MAX_NAME];
uint8_t src_name[QEMU_FWCFG_MAX_NAME];
uint32_t dest_off_le;
uint32_t src_off_le;
uint8_t size;
} write_pointer;
/* padding */
uint8_t pad[124];
};
} __packed;
enum qemu_loader_command {
QEMU_LOADER_CMD_ALLOC = 0x1,
QEMU_LOADER_CMD_ADD_POINTER = 0x2,
QEMU_LOADER_CMD_ADD_CHECKSUM = 0x3,
QEMU_LOADER_CMD_WRITE_POINTER = 0x4,
};
struct qemu_loader_element {
STAILQ_ENTRY(qemu_loader_element) chain;
struct qemu_loader_entry entry;
};
struct qemu_loader {
uint8_t fwcfg_name[QEMU_FWCFG_MAX_NAME];
STAILQ_HEAD(qemu_loader_list, qemu_loader_element) list;
};
int
qemu_loader_alloc(struct qemu_loader *const loader, const uint8_t *name,
const uint32_t alignment, const enum qemu_loader_zone zone)
{
struct qemu_loader_element *element;
if (strlen(name) >= QEMU_FWCFG_MAX_NAME)
return (EINVAL);
element = calloc(1, sizeof(struct qemu_loader_element));
if (element == NULL) {
warnx("%s: failed to allocate command", __func__);
return (ENOMEM);
}
element->entry.cmd_le = htole32(QEMU_LOADER_CMD_ALLOC);
strncpy(element->entry.alloc.name, name, QEMU_FWCFG_MAX_NAME);
element->entry.alloc.alignment_le = htole32(alignment);
element->entry.alloc.zone = zone;
/*
* The guest always works on copies of the fwcfg item, which where
* loaded into guest memory. Loading a fwcfg item is caused by ALLOC.
* For that reason, ALLOC should be scheduled in front of any other
* commands.
*/
STAILQ_INSERT_HEAD(&loader->list, element, chain);
return (0);
}
int
qemu_loader_add_checksum(struct qemu_loader *const loader, const uint8_t *name,
const uint32_t off, const uint32_t start, const uint32_t len)
{
struct qemu_loader_element *element;
if (strlen(name) >= QEMU_FWCFG_MAX_NAME)
return (EINVAL);
element = calloc(1, sizeof(struct qemu_loader_element));
if (element == NULL) {
warnx("%s: failed to allocate command", __func__);
return (ENOMEM);
}
element->entry.cmd_le = htole32(QEMU_LOADER_CMD_ADD_CHECKSUM);
strncpy(element->entry.add_checksum.name, name, QEMU_FWCFG_MAX_NAME);
element->entry.add_checksum.off_le = htole32(off);
element->entry.add_checksum.start_le = htole32(start);
element->entry.add_checksum.len_le = htole32(len);
STAILQ_INSERT_TAIL(&loader->list, element, chain);
return (0);
}
int
qemu_loader_add_pointer(struct qemu_loader *const loader,
const uint8_t *dest_name, const uint8_t *src_name, const uint32_t off,
const uint8_t size)
{
struct qemu_loader_element *element;
if (strlen(dest_name) >= QEMU_FWCFG_MAX_NAME ||
strlen(src_name) >= QEMU_FWCFG_MAX_NAME)
return (EINVAL);
element = calloc(1, sizeof(struct qemu_loader_element));
if (element == NULL) {
warnx("%s: failed to allocate command", __func__);
return (ENOMEM);
}
element->entry.cmd_le = htole32(QEMU_LOADER_CMD_ADD_POINTER);
strncpy(element->entry.add_pointer.dest_name, dest_name,
QEMU_FWCFG_MAX_NAME);
strncpy(element->entry.add_pointer.src_name, src_name,
QEMU_FWCFG_MAX_NAME);
element->entry.add_pointer.off_le = htole32(off);
element->entry.add_pointer.size = size;
STAILQ_INSERT_TAIL(&loader->list, element, chain);
return (0);
}
int
qemu_loader_create(struct qemu_loader **const new_loader,
const uint8_t *fwcfg_name)
{
struct qemu_loader *loader;
if (new_loader == NULL || strlen(fwcfg_name) >= QEMU_FWCFG_MAX_NAME) {
return (EINVAL);
}
loader = calloc(1, sizeof(struct qemu_loader));
if (loader == NULL) {
warnx("%s: failed to allocate loader", __func__);
return (ENOMEM);
}
strncpy(loader->fwcfg_name, fwcfg_name, QEMU_FWCFG_MAX_NAME);
STAILQ_INIT(&loader->list);
*new_loader = loader;
return (0);
}
static const uint8_t *
qemu_loader_get_zone_name(const enum qemu_loader_zone zone)
{
#ifdef __FreeBSD__
switch (zone) {
case QEMU_LOADER_ALLOC_HIGH:
return ("HIGH");
case QEMU_LOADER_ALLOC_FSEG:
return ("FSEG");
default:
return ("Unknown");
}
#else
switch (zone) {
case QEMU_LOADER_ALLOC_HIGH:
return ((uint8_t *)"HIGH");
case QEMU_LOADER_ALLOC_FSEG:
return ((uint8_t *)"FSEG");
default:
return ((uint8_t *)"Unknown");
}
#endif
}
static void __unused
qemu_loader_dump_entry(const struct qemu_loader_entry *const entry)
{
switch (le32toh(entry->cmd_le)) {
case QEMU_LOADER_CMD_ALLOC:
printf("CMD_ALLOC\n\r");
printf(" name : %s\n\r", entry->alloc.name);
printf(" alignment: %8x\n\r",
le32toh(entry->alloc.alignment_le));
printf(" zone : %s\n\r",
qemu_loader_get_zone_name(entry->alloc.zone));
break;
case QEMU_LOADER_CMD_ADD_POINTER:
printf("CMD_ADD_POINTER\n\r");
printf(" dest_name: %s\n\r", entry->add_pointer.dest_name);
printf(" src_name : %s\n\r", entry->add_pointer.src_name);
printf(" off : %8x\n\r",
le32toh(entry->add_pointer.off_le));
printf(" size : %8x\n\r", entry->add_pointer.size);
break;
case QEMU_LOADER_CMD_ADD_CHECKSUM:
printf("CMD_ADD_CHECKSUM\n\r");
printf(" name : %s\n\r", entry->add_checksum.name);
printf(" off : %8x\n\r",
le32toh(entry->add_checksum.off_le));
printf(" start : %8x\n\r",
le32toh(entry->add_checksum.start_le));
printf(" length : %8x\n\r",
le32toh(entry->add_checksum.len_le));
break;
case QEMU_LOADER_CMD_WRITE_POINTER:
printf("CMD_WRITE_POINTER\n\r");
printf(" dest_name: %s\n\r", entry->write_pointer.dest_name);
printf(" src_name : %s\n\r", entry->write_pointer.src_name);
printf(" dest_off : %8x\n\r",
le32toh(entry->write_pointer.dest_off_le));
printf(" src_off : %8x\n\r",
le32toh(entry->write_pointer.src_off_le));
printf(" size : %8x\n\r", entry->write_pointer.size);
break;
default:
printf("UNKNOWN\n\r");
break;
}
}
int
qemu_loader_finish(struct qemu_loader *const loader)
{
struct qemu_loader_element *element;
struct qemu_loader_entry *data;
size_t len = 0;
STAILQ_FOREACH(element, &loader->list, chain) {
len += sizeof(struct qemu_loader_entry);
}
if (len == 0) {
warnx("%s: bios loader empty", __func__);
return (EFAULT);
}
data = calloc(1, len);
if (data == NULL) {
warnx("%s: failed to allocate fwcfg data", __func__);
return (ENOMEM);
}
int i = 0;
STAILQ_FOREACH(element, &loader->list, chain) {
memcpy(&data[i], &element->entry,
sizeof(struct qemu_loader_entry));
++i;
}
#ifdef __FreeBSD__
return (qemu_fwcfg_add_file(loader->fwcfg_name, len, data));
#else
return (qemu_fwcfg_add_file((const char *)loader->fwcfg_name,
len, data));
#endif
}
|