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;
59 void test_exit(void *arg, int v);
66 test_putc(void *arg, int ch)
78 if (read(0, &c, 1) == 1)
88 if (ioctl(0, FIONREAD, &n) >= 0)
108 test_open(void *arg, const char *filename, void **h_return)
111 struct test_file *tf;
117 strlcpy(path, host_base, PATH_MAX);
118 if (path[strlen(path) - 1] == '/')
119 path[strlen(path) - 1] = 0;
120 strlcat(path, filename, PATH_MAX);
121 tf = malloc(sizeof(struct test_file));
122 if (stat(path, &tf->tf_stat) < 0) {
127 tf->tf_size = st.st_size;
128 if (S_ISDIR(tf->tf_stat.st_mode)) {
130 tf->tf_u.dir = opendir(path);
136 if (S_ISREG(tf->tf_stat.st_mode)) {
138 tf->tf_u.fd = open(path, O_RDONLY);
151 test_close(void *arg, void *h)
153 struct test_file *tf = h;
156 closedir(tf->tf_u.dir);
165 test_isdir(void *arg, void *h)
167 struct test_file *tf = h;
169 return (tf->tf_isdir);
173 test_read(void *arg, void *h, void *dst, size_t size, size_t *resid_return)
175 struct test_file *tf = h;
180 sz = read(tf->tf_u.fd, dst, size);
183 *resid_return = size - sz;
188 test_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
189 size_t *namelen_return, char *name)
191 struct test_file *tf = h;
197 dp = readdir(tf->tf_u.dir);
202 * Note: d_namlen is in the range 0..255 and therefore less
203 * than PATH_MAX so we don't need to test before copying.
205 *fileno_return = dp->d_fileno;
206 *type_return = dp->d_type;
207 *namelen_return = dp->d_namlen;
208 memcpy(name, dp->d_name, dp->d_namlen);
209 name[dp->d_namlen] = 0;
215 test_seek(void *arg, void *h, uint64_t offset, int whence)
217 struct test_file *tf = h;
221 if (lseek(tf->tf_u.fd, offset, whence) < 0)
227 test_stat(void *arg, void *h, struct stat *stp)
229 struct test_file *tf = h;
233 memset(stp, 0, sizeof(struct stat));
234 stp->st_mode = tf->tf_stat.st_mode;
235 stp->st_uid = tf->tf_stat.st_uid;
236 stp->st_gid = tf->tf_stat.st_gid;
237 stp->st_size = tf->tf_stat.st_size;
238 stp->st_ino = tf->tf_stat.st_ino;
239 stp->st_dev = tf->tf_stat.st_dev;
240 stp->st_mtime = tf->tf_stat.st_mtime;
249 test_diskread(void *arg, int unit, uint64_t offset, void *dst, size_t size,
250 size_t *resid_return)
254 if (unit > disk_index || disk_fd[unit] == -1)
256 n = pread(disk_fd[unit], dst, size, offset);
259 *resid_return = size - n;
264 test_diskioctl(void *arg, int unit, u_long cmd, void *data)
268 if (unit > disk_index || disk_fd[unit] == -1)
271 case DIOCGSECTORSIZE:
272 *(u_int *)data = 512;
275 if (fstat(disk_fd[unit], &sb) == 0)
276 *(off_t *)data = sb.st_size;
287 * Guest virtual machine i/o
289 * Note: guest addresses are kernel virtual
293 test_copyin(void *arg, const void *from, uint64_t to, size_t size)
299 if (to + size > image_size)
300 size = image_size - to;
301 memcpy(&image[to], from, size);
306 test_copyout(void *arg, uint64_t from, void *to, size_t size)
310 if (from > image_size)
312 if (from + size > image_size)
313 size = image_size - from;
314 memcpy(to, &image[from], size);
319 test_setreg(void *arg, int r, uint64_t v)
322 if (r < 0 || r >= 16)
328 test_setmsr(void *arg, int r, uint64_t v)
333 test_setcr(void *arg, int r, uint64_t v)
338 test_setgdt(void *arg, uint64_t v, size_t sz)
343 test_exec(void *arg, uint64_t pc)
345 printf("Execute at 0x%"PRIx64"\n", pc);
354 test_delay(void *arg, int usec)
361 test_exit(void *arg, int v)
364 tcsetattr(0, TCSAFLUSH, &oldterm);
369 test_getmem(void *arg, uint64_t *lowmem, uint64_t *highmem)
372 *lowmem = 128*1024*1024;
377 test_getenv(void *arg, int idx)
379 static char *vars[] = {
388 struct loader_callbacks cb = {
397 .readdir = test_readdir,
401 .diskread = test_diskread,
402 .diskioctl = test_diskioctl,
404 .copyin = test_copyin,
405 .copyout = test_copyout,
406 .setreg = test_setreg,
407 .setmsr = test_setmsr,
409 .setgdt = test_setgdt,
414 .getmem = test_getmem,
416 .getenv = test_getenv,
423 printf("usage: [-b <userboot shared object>] [-d <disk image path>] [-h <host filesystem path>\n");
428 main(int argc, char** argv)
431 void (*func)(struct loader_callbacks *, void *, int, int) __dead2;
433 const char *userboot_obj = "/boot/userboot.so";
435 while ((opt = getopt(argc, argv, "b:d:h:")) != -1) {
438 userboot_obj = optarg;
443 disk_fd = reallocarray(disk_fd, disk_index + 1,
445 disk_fd[disk_index] = open(optarg, O_RDONLY);
446 if (disk_fd[disk_index] < 0)
447 err(1, "Can't open disk image '%s'", optarg);
459 h = dlopen(userboot_obj, RTLD_LOCAL);
461 printf("%s\n", dlerror());
464 func = dlsym(h, "loader_main");
466 printf("%s\n", dlerror());
470 image_size = 128*1024*1024;
471 image = malloc(image_size);
475 term.c_iflag &= ~(ICRNL);
476 term.c_lflag &= ~(ICANON|ECHO);
477 tcsetattr(0, TCSAFLUSH, &term);
479 func(&cb, NULL, USERBOOT_VERSION_3, disk_index + 1);