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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/*-
* Copyright (c) 2011 Google, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <dlfcn.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <boot/userboot/userboot.h>
char *host_base = NULL;
struct termios term, oldterm;
char *image;
size_t image_size;
int disk_fd = -1;
uint64_t regs[16];
uint64_t pc;
void test_exit(void *arg, int v);
/*
* Console i/o
*/
void
test_putc(void *arg, int ch)
{
char c = ch;
write(1, &c, 1);
}
int
test_getc(void *arg)
{
char c;
if (read(0, &c, 1) == 1)
return c;
return -1;
}
int
test_poll(void *arg)
{
int n;
if (ioctl(0, FIONREAD, &n) >= 0)
return (n > 0);
return (0);
}
/*
* Host filesystem i/o
*/
struct test_file {
int tf_isdir;
size_t tf_size;
struct stat tf_stat;
union {
int fd;
DIR *dir;
} tf_u;
};
int
test_open(void *arg, const char *filename, void **h_return)
{
struct stat st;
struct test_file *tf;
char path[PATH_MAX];
if (!host_base)
return (ENOENT);
strlcpy(path, host_base, PATH_MAX);
if (path[strlen(path) - 1] == '/')
path[strlen(path) - 1] = 0;
strlcat(path, filename, PATH_MAX);
tf = malloc(sizeof(struct test_file));
if (stat(path, &tf->tf_stat) < 0) {
free(tf);
return (errno);
}
tf->tf_size = st.st_size;
if (S_ISDIR(tf->tf_stat.st_mode)) {
tf->tf_isdir = 1;
tf->tf_u.dir = opendir(path);
if (!tf->tf_u.dir)
goto out;
*h_return = tf;
return (0);
}
if (S_ISREG(tf->tf_stat.st_mode)) {
tf->tf_isdir = 0;
tf->tf_u.fd = open(path, O_RDONLY);
if (tf->tf_u.fd < 0)
goto out;
*h_return = tf;
return (0);
}
out:
free(tf);
return (EINVAL);
}
int
test_close(void *arg, void *h)
{
struct test_file *tf = h;
if (tf->tf_isdir)
closedir(tf->tf_u.dir);
else
close(tf->tf_u.fd);
free(tf);
return (0);
}
int
test_isdir(void *arg, void *h)
{
struct test_file *tf = h;
return (tf->tf_isdir);
}
int
test_read(void *arg, void *h, void *dst, size_t size, size_t *resid_return)
{
struct test_file *tf = h;
ssize_t sz;
if (tf->tf_isdir)
return (EINVAL);
sz = read(tf->tf_u.fd, dst, size);
if (sz < 0)
return (EINVAL);
*resid_return = size - sz;
return (0);
}
int
test_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
size_t *namelen_return, char *name)
{
struct test_file *tf = h;
struct dirent *dp;
if (!tf->tf_isdir)
return (EINVAL);
dp = readdir(tf->tf_u.dir);
if (!dp)
return (ENOENT);
/*
* Note: d_namlen is in the range 0..255 and therefore less
* than PATH_MAX so we don't need to test before copying.
*/
*fileno_return = dp->d_fileno;
*type_return = dp->d_type;
*namelen_return = dp->d_namlen;
memcpy(name, dp->d_name, dp->d_namlen);
name[dp->d_namlen] = 0;
return (0);
}
int
test_seek(void *arg, void *h, uint64_t offset, int whence)
{
struct test_file *tf = h;
if (tf->tf_isdir)
return (EINVAL);
if (lseek(tf->tf_u.fd, offset, whence) < 0)
return (errno);
return (0);
}
int
test_stat(void *arg, void *h, int *mode_return, int *uid_return, int *gid_return,
uint64_t *size_return)
{
struct test_file *tf = h;
*mode_return = tf->tf_stat.st_mode;
*uid_return = tf->tf_stat.st_uid;
*gid_return = tf->tf_stat.st_gid;
*size_return = tf->tf_stat.st_size;
return (0);
}
/*
* Disk image i/o
*/
int
test_diskread(void *arg, int unit, uint64_t offset, void *dst, size_t size,
size_t *resid_return)
{
ssize_t n;
if (unit != 0 || disk_fd == -1)
return (EIO);
n = pread(disk_fd, dst, size, offset);
if (n < 0)
return (errno);
*resid_return = size - n;
return (0);
}
/*
* Guest virtual machine i/o
*
* Note: guest addresses are kernel virtual
*/
int
test_copyin(void *arg, const void *from, uint64_t to, size_t size)
{
to &= 0x7fffffff;
if (to > image_size)
return (EFAULT);
if (to + size > image_size)
size = image_size - to;
memcpy(&image[to], from, size);
}
int
test_copyout(void *arg, uint64_t from, void *to, size_t size)
{
from &= 0x7fffffff;
if (from > image_size)
return (EFAULT);
if (from + size > image_size)
size = image_size - from;
memcpy(to, &image[from], size);
}
void
test_setreg(void *arg, int r, uint64_t v)
{
if (r < 0 || r >= 16)
return;
regs[r] = v;
}
void
test_setmsr(void *arg, int r, uint64_t v)
{
}
void
test_setcr(void *arg, int r, uint64_t v)
{
}
void
test_setgdt(void *arg, uint64_t v, size_t sz)
{
}
void
test_exec(void *arg, uint64_t pc)
{
printf("Execute at 0x%llx\n", pc);
test_exit(arg, 0);
}
/*
* Misc
*/
void
test_delay(void *arg, int usec)
{
usleep(usec);
}
void
test_exit(void *arg, int v)
{
tcsetattr(0, TCSAFLUSH, &oldterm);
exit(v);
}
void
test_getmem(void *arg, uint64_t *lowmem, uint64_t *highmem)
{
*lowmem = 128*1024*1024;
*highmem = 0;
}
struct loader_callbacks_v1 cb = {
.putc = test_putc,
.getc = test_getc,
.poll = test_poll,
.open = test_open,
.close = test_close,
.isdir = test_isdir,
.read = test_read,
.readdir = test_readdir,
.seek = test_seek,
.stat = test_stat,
.diskread = test_diskread,
.copyin = test_copyin,
.copyout = test_copyout,
.setreg = test_setreg,
.setmsr = test_setmsr,
.setcr = test_setcr,
.setgdt = test_setgdt,
.exec = test_exec,
.delay = test_delay,
.exit = test_exit,
.getmem = test_getmem,
};
void
usage()
{
printf("usage: %s [-d <disk image path>] [-h <host filesystem path>\n");
exit(1);
}
int
main(int argc, char** argv)
{
void *h;
void (*func)(struct loader_callbacks_v1 *, void *, int, int);
int opt;
char *disk_image = NULL;
while ((opt = getopt(argc, argv, "d:h:")) != -1) {
switch (opt) {
case 'd':
disk_image = optarg;
break;
case 'h':
host_base = optarg;
break;
case '?':
usage();
}
}
h = dlopen("/boot/userboot.so",
RTLD_LOCAL);
if (!h) {
printf("%s\n", dlerror());
return (1);
}
func = dlsym(h, "loader_main");
if (!func) {
printf("%s\n", dlerror());
return (1);
}
image_size = 128*1024*1024;
image = malloc(image_size);
if (disk_image) {
disk_fd = open(disk_image, O_RDONLY);
if (disk_fd < 0)
err(1, "Can't open disk image '%s'", disk_image);
}
tcgetattr(0, &term);
oldterm = term;
term.c_iflag &= ~(ICRNL);
term.c_lflag &= ~(ICANON|ECHO);
tcsetattr(0, TCSAFLUSH, &term);
func(&cb, NULL, USERBOOT_VERSION_1, disk_fd >= 0);
}
|