2 * Copyright (c) 2011 Google, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
49 char *host_base = NULL;
50 struct termios term, oldterm;
58 void test_exit(void *arg, int v);
65 test_putc(void *arg, int ch)
77 if (read(0, &c, 1) == 1)
87 if (ioctl(0, FIONREAD, &n) >= 0)
107 test_open(void *arg, const char *filename, void **h_return)
110 struct test_file *tf;
116 strlcpy(path, host_base, PATH_MAX);
117 if (path[strlen(path) - 1] == '/')
118 path[strlen(path) - 1] = 0;
119 strlcat(path, filename, PATH_MAX);
120 tf = malloc(sizeof(struct test_file));
121 if (stat(path, &tf->tf_stat) < 0) {
126 tf->tf_size = st.st_size;
127 if (S_ISDIR(tf->tf_stat.st_mode)) {
129 tf->tf_u.dir = opendir(path);
135 if (S_ISREG(tf->tf_stat.st_mode)) {
137 tf->tf_u.fd = open(path, O_RDONLY);
150 test_close(void *arg, void *h)
152 struct test_file *tf = h;
155 closedir(tf->tf_u.dir);
164 test_isdir(void *arg, void *h)
166 struct test_file *tf = h;
168 return (tf->tf_isdir);
172 test_read(void *arg, void *h, void *dst, size_t size, size_t *resid_return)
174 struct test_file *tf = h;
179 sz = read(tf->tf_u.fd, dst, size);
182 *resid_return = size - sz;
187 test_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
188 size_t *namelen_return, char *name)
190 struct test_file *tf = h;
196 dp = readdir(tf->tf_u.dir);
201 * Note: d_namlen is in the range 0..255 and therefore less
202 * than PATH_MAX so we don't need to test before copying.
204 *fileno_return = dp->d_fileno;
205 *type_return = dp->d_type;
206 *namelen_return = dp->d_namlen;
207 memcpy(name, dp->d_name, dp->d_namlen);
208 name[dp->d_namlen] = 0;
214 test_seek(void *arg, void *h, uint64_t offset, int whence)
216 struct test_file *tf = h;
220 if (lseek(tf->tf_u.fd, offset, whence) < 0)
226 test_stat(void *arg, void *h, int *mode_return, int *uid_return, int *gid_return,
227 uint64_t *size_return)
229 struct test_file *tf = h;
231 *mode_return = tf->tf_stat.st_mode;
232 *uid_return = tf->tf_stat.st_uid;
233 *gid_return = tf->tf_stat.st_gid;
234 *size_return = tf->tf_stat.st_size;
243 test_diskread(void *arg, int unit, uint64_t offset, void *dst, size_t size,
244 size_t *resid_return)
248 if (unit != 0 || disk_fd == -1)
250 n = pread(disk_fd, dst, size, offset);
253 *resid_return = size - n;
258 test_diskioctl(void *arg, int unit, u_long cmd, void *data)
262 if (unit != 0 || disk_fd == -1)
265 case DIOCGSECTORSIZE:
266 *(u_int *)data = 512;
269 if (fstat(disk_fd, &sb) == 0)
270 *(off_t *)data = sb.st_size;
281 * Guest virtual machine i/o
283 * Note: guest addresses are kernel virtual
287 test_copyin(void *arg, const void *from, uint64_t to, size_t size)
293 if (to + size > image_size)
294 size = image_size - to;
295 memcpy(&image[to], from, size);
300 test_copyout(void *arg, uint64_t from, void *to, size_t size)
304 if (from > image_size)
306 if (from + size > image_size)
307 size = image_size - from;
308 memcpy(to, &image[from], size);
313 test_setreg(void *arg, int r, uint64_t v)
316 if (r < 0 || r >= 16)
322 test_setmsr(void *arg, int r, uint64_t v)
327 test_setcr(void *arg, int r, uint64_t v)
332 test_setgdt(void *arg, uint64_t v, size_t sz)
337 test_exec(void *arg, uint64_t pc)
339 printf("Execute at 0x%"PRIu64"\n", pc);
348 test_delay(void *arg, int usec)
355 test_exit(void *arg, int v)
358 tcsetattr(0, TCSAFLUSH, &oldterm);
363 test_getmem(void *arg, uint64_t *lowmem, uint64_t *highmem)
366 *lowmem = 128*1024*1024;
371 test_getenv(void *arg, int idx)
373 static char *vars[] = {
382 struct loader_callbacks cb = {
391 .readdir = test_readdir,
395 .diskread = test_diskread,
396 .diskioctl = test_diskioctl,
398 .copyin = test_copyin,
399 .copyout = test_copyout,
400 .setreg = test_setreg,
401 .setmsr = test_setmsr,
403 .setgdt = test_setgdt,
408 .getmem = test_getmem,
410 .getenv = test_getenv,
417 printf("usage: [-b <userboot shared object>] [-d <disk image path>] [-h <host filesystem path>\n");
422 main(int argc, char** argv)
425 void (*func)(struct loader_callbacks *, void *, int, int) __dead2;
427 char *disk_image = NULL;
428 const char *userboot_obj = "/boot/userboot.so";
430 while ((opt = getopt(argc, argv, "b:d:h:")) != -1) {
433 userboot_obj = optarg;
449 h = dlopen(userboot_obj, RTLD_LOCAL);
451 printf("%s\n", dlerror());
454 func = dlsym(h, "loader_main");
456 printf("%s\n", dlerror());
460 image_size = 128*1024*1024;
461 image = malloc(image_size);
463 disk_fd = open(disk_image, O_RDONLY);
465 err(1, "Can't open disk image '%s'", disk_image);
470 term.c_iflag &= ~(ICRNL);
471 term.c_lflag &= ~(ICANON|ECHO);
472 tcsetattr(0, TCSAFLUSH, &term);
474 func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0);