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/ioctl.h>
44 #include <boot/userboot/userboot.h>
46 char *host_base = NULL;
47 struct termios term, oldterm;
55 void test_exit(void *arg, int v);
62 test_putc(void *arg, int ch)
74 if (read(0, &c, 1) == 1)
84 if (ioctl(0, FIONREAD, &n) >= 0)
104 test_open(void *arg, const char *filename, void **h_return)
107 struct test_file *tf;
113 strlcpy(path, host_base, PATH_MAX);
114 if (path[strlen(path) - 1] == '/')
115 path[strlen(path) - 1] = 0;
116 strlcat(path, filename, PATH_MAX);
117 tf = malloc(sizeof(struct test_file));
118 if (stat(path, &tf->tf_stat) < 0) {
123 tf->tf_size = st.st_size;
124 if (S_ISDIR(tf->tf_stat.st_mode)) {
126 tf->tf_u.dir = opendir(path);
132 if (S_ISREG(tf->tf_stat.st_mode)) {
134 tf->tf_u.fd = open(path, O_RDONLY);
147 test_close(void *arg, void *h)
149 struct test_file *tf = h;
152 closedir(tf->tf_u.dir);
161 test_isdir(void *arg, void *h)
163 struct test_file *tf = h;
165 return (tf->tf_isdir);
169 test_read(void *arg, void *h, void *dst, size_t size, size_t *resid_return)
171 struct test_file *tf = h;
176 sz = read(tf->tf_u.fd, dst, size);
179 *resid_return = size - sz;
184 test_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
185 size_t *namelen_return, char *name)
187 struct test_file *tf = h;
193 dp = readdir(tf->tf_u.dir);
198 * Note: d_namlen is in the range 0..255 and therefore less
199 * than PATH_MAX so we don't need to test before copying.
201 *fileno_return = dp->d_fileno;
202 *type_return = dp->d_type;
203 *namelen_return = dp->d_namlen;
204 memcpy(name, dp->d_name, dp->d_namlen);
205 name[dp->d_namlen] = 0;
211 test_seek(void *arg, void *h, uint64_t offset, int whence)
213 struct test_file *tf = h;
217 if (lseek(tf->tf_u.fd, offset, whence) < 0)
223 test_stat(void *arg, void *h, int *mode_return, int *uid_return, int *gid_return,
224 uint64_t *size_return)
226 struct test_file *tf = h;
228 *mode_return = tf->tf_stat.st_mode;
229 *uid_return = tf->tf_stat.st_uid;
230 *gid_return = tf->tf_stat.st_gid;
231 *size_return = tf->tf_stat.st_size;
240 test_diskread(void *arg, int unit, uint64_t offset, void *dst, size_t size,
241 size_t *resid_return)
245 if (unit != 0 || disk_fd == -1)
247 n = pread(disk_fd, dst, size, offset);
250 *resid_return = size - n;
255 * Guest virtual machine i/o
257 * Note: guest addresses are kernel virtual
261 test_copyin(void *arg, const void *from, uint64_t to, size_t size)
267 if (to + size > image_size)
268 size = image_size - to;
269 memcpy(&image[to], from, size);
273 test_copyout(void *arg, uint64_t from, void *to, size_t size)
277 if (from > image_size)
279 if (from + size > image_size)
280 size = image_size - from;
281 memcpy(to, &image[from], size);
285 test_setreg(void *arg, int r, uint64_t v)
288 if (r < 0 || r >= 16)
294 test_setmsr(void *arg, int r, uint64_t v)
299 test_setcr(void *arg, int r, uint64_t v)
304 test_setgdt(void *arg, uint64_t v, size_t sz)
309 test_exec(void *arg, uint64_t pc)
311 printf("Execute at 0x%llx\n", pc);
320 test_delay(void *arg, int usec)
327 test_exit(void *arg, int v)
330 tcsetattr(0, TCSAFLUSH, &oldterm);
335 test_getmem(void *arg, uint64_t *lowmem, uint64_t *highmem)
338 *lowmem = 128*1024*1024;
342 struct loader_callbacks_v1 cb = {
351 .readdir = test_readdir,
355 .diskread = test_diskread,
357 .copyin = test_copyin,
358 .copyout = test_copyout,
359 .setreg = test_setreg,
360 .setmsr = test_setmsr,
362 .setgdt = test_setgdt,
367 .getmem = test_getmem,
374 printf("usage: %s [-d <disk image path>] [-h <host filesystem path>\n");
379 main(int argc, char** argv)
382 void (*func)(struct loader_callbacks_v1 *, void *, int, int);
384 char *disk_image = NULL;
386 while ((opt = getopt(argc, argv, "d:h:")) != -1) {
401 h = dlopen("/boot/userboot.so",
404 printf("%s\n", dlerror());
407 func = dlsym(h, "loader_main");
409 printf("%s\n", dlerror());
413 image_size = 128*1024*1024;
414 image = malloc(image_size);
416 disk_fd = open(disk_image, O_RDONLY);
418 err(1, "Can't open disk image '%s'", disk_image);
423 term.c_iflag &= ~(ICRNL);
424 term.c_lflag &= ~(ICANON|ECHO);
425 tcsetattr(0, TCSAFLUSH, &term);
427 func(&cb, NULL, USERBOOT_VERSION_1, disk_fd >= 0);