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>
46 #include <boot/userboot/userboot.h>
48 char *host_base = NULL;
49 struct termios term, oldterm;
57 void test_exit(void *arg, int v);
64 test_putc(void *arg, int ch)
76 if (read(0, &c, 1) == 1)
86 if (ioctl(0, FIONREAD, &n) >= 0)
106 test_open(void *arg, const char *filename, void **h_return)
109 struct test_file *tf;
115 strlcpy(path, host_base, PATH_MAX);
116 if (path[strlen(path) - 1] == '/')
117 path[strlen(path) - 1] = 0;
118 strlcat(path, filename, PATH_MAX);
119 tf = malloc(sizeof(struct test_file));
120 if (stat(path, &tf->tf_stat) < 0) {
125 tf->tf_size = st.st_size;
126 if (S_ISDIR(tf->tf_stat.st_mode)) {
128 tf->tf_u.dir = opendir(path);
134 if (S_ISREG(tf->tf_stat.st_mode)) {
136 tf->tf_u.fd = open(path, O_RDONLY);
149 test_close(void *arg, void *h)
151 struct test_file *tf = h;
154 closedir(tf->tf_u.dir);
163 test_isdir(void *arg, void *h)
165 struct test_file *tf = h;
167 return (tf->tf_isdir);
171 test_read(void *arg, void *h, void *dst, size_t size, size_t *resid_return)
173 struct test_file *tf = h;
178 sz = read(tf->tf_u.fd, dst, size);
181 *resid_return = size - sz;
186 test_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
187 size_t *namelen_return, char *name)
189 struct test_file *tf = h;
195 dp = readdir(tf->tf_u.dir);
200 * Note: d_namlen is in the range 0..255 and therefore less
201 * than PATH_MAX so we don't need to test before copying.
203 *fileno_return = dp->d_fileno;
204 *type_return = dp->d_type;
205 *namelen_return = dp->d_namlen;
206 memcpy(name, dp->d_name, dp->d_namlen);
207 name[dp->d_namlen] = 0;
213 test_seek(void *arg, void *h, uint64_t offset, int whence)
215 struct test_file *tf = h;
219 if (lseek(tf->tf_u.fd, offset, whence) < 0)
225 test_stat(void *arg, void *h, int *mode_return, int *uid_return, int *gid_return,
226 uint64_t *size_return)
228 struct test_file *tf = h;
230 *mode_return = tf->tf_stat.st_mode;
231 *uid_return = tf->tf_stat.st_uid;
232 *gid_return = tf->tf_stat.st_gid;
233 *size_return = tf->tf_stat.st_size;
242 test_diskread(void *arg, int unit, uint64_t offset, void *dst, size_t size,
243 size_t *resid_return)
247 if (unit != 0 || disk_fd == -1)
249 n = pread(disk_fd, dst, size, offset);
252 *resid_return = size - n;
257 test_diskioctl(void *arg, int unit, u_long cmd, void *data)
261 if (unit != 0 || disk_fd == -1)
264 case DIOCGSECTORSIZE:
265 *(u_int *)data = 512;
268 if (fstat(disk_fd, &sb) == 0)
269 *(off_t *)data = sb.st_size;
280 * Guest virtual machine i/o
282 * Note: guest addresses are kernel virtual
286 test_copyin(void *arg, const void *from, uint64_t to, size_t size)
292 if (to + size > image_size)
293 size = image_size - to;
294 memcpy(&image[to], from, size);
298 test_copyout(void *arg, uint64_t from, void *to, size_t size)
302 if (from > image_size)
304 if (from + size > image_size)
305 size = image_size - from;
306 memcpy(to, &image[from], size);
310 test_setreg(void *arg, int r, uint64_t v)
313 if (r < 0 || r >= 16)
319 test_setmsr(void *arg, int r, uint64_t v)
324 test_setcr(void *arg, int r, uint64_t v)
329 test_setgdt(void *arg, uint64_t v, size_t sz)
334 test_exec(void *arg, uint64_t pc)
336 printf("Execute at 0x%llx\n", pc);
345 test_delay(void *arg, int usec)
352 test_exit(void *arg, int v)
355 tcsetattr(0, TCSAFLUSH, &oldterm);
360 test_getmem(void *arg, uint64_t *lowmem, uint64_t *highmem)
363 *lowmem = 128*1024*1024;
367 struct loader_callbacks cb = {
376 .readdir = test_readdir,
380 .diskread = test_diskread,
381 .diskioctl = test_diskioctl,
383 .copyin = test_copyin,
384 .copyout = test_copyout,
385 .setreg = test_setreg,
386 .setmsr = test_setmsr,
388 .setgdt = test_setgdt,
393 .getmem = test_getmem,
400 printf("usage: %s [-d <disk image path>] [-h <host filesystem path>\n");
405 main(int argc, char** argv)
408 void (*func)(struct loader_callbacks *, void *, int, int);
410 char *disk_image = NULL;
412 while ((opt = getopt(argc, argv, "d:h:")) != -1) {
427 h = dlopen("/boot/userboot.so",
430 printf("%s\n", dlerror());
433 func = dlsym(h, "loader_main");
435 printf("%s\n", dlerror());
439 image_size = 128*1024*1024;
440 image = malloc(image_size);
442 disk_fd = open(disk_image, O_RDONLY);
444 err(1, "Can't open disk image '%s'", disk_image);
449 term.c_iflag &= ~(ICRNL);
450 term.c_lflag &= ~(ICANON|ECHO);
451 tcsetattr(0, TCSAFLUSH, &term);
453 func(&cb, NULL, USERBOOT_VERSION_2, disk_fd >= 0);