1 /*- 2 * Copyright (c) 2011 Google, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/types.h> 30 #include <sys/disk.h> 31 #include <sys/ioctl.h> 32 #include <sys/stat.h> 33 #include <dirent.h> 34 #include <dlfcn.h> 35 #include <err.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <getopt.h> 39 #include <inttypes.h> 40 #include <limits.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <termios.h> 45 #include <unistd.h> 46 47 #include <userboot.h> 48 49 char *host_base = NULL; 50 struct termios term, oldterm; 51 char *image; 52 size_t image_size; 53 int disk_fd = -1; 54 55 uint64_t regs[16]; 56 uint64_t pc; 57 58 void test_exit(void *arg, int v); 59 60 /* 61 * Console i/o 62 */ 63 64 void 65 test_putc(void *arg, int ch) 66 { 67 char c = ch; 68 69 write(1, &c, 1); 70 } 71 72 int 73 test_getc(void *arg) 74 { 75 char c; 76 77 if (read(0, &c, 1) == 1) 78 return c; 79 return -1; 80 } 81 82 int 83 test_poll(void *arg) 84 { 85 int n; 86 87 if (ioctl(0, FIONREAD, &n) >= 0) 88 return (n > 0); 89 return (0); 90 } 91 92 /* 93 * Host filesystem i/o 94 */ 95 96 struct test_file { 97 int tf_isdir; 98 size_t tf_size; 99 struct stat tf_stat; 100 union { 101 int fd; 102 DIR *dir; 103 } tf_u; 104 }; 105 106 int 107 test_open(void *arg, const char *filename, void **h_return) 108 { 109 struct stat st; 110 struct test_file *tf; 111 char path[PATH_MAX]; 112 113 if (!host_base) 114 return (ENOENT); 115 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) { 122 free(tf); 123 return (errno); 124 } 125 126 tf->tf_size = st.st_size; 127 if (S_ISDIR(tf->tf_stat.st_mode)) { 128 tf->tf_isdir = 1; 129 tf->tf_u.dir = opendir(path); 130 if (!tf->tf_u.dir) 131 goto out; 132 *h_return = tf; 133 return (0); 134 } 135 if (S_ISREG(tf->tf_stat.st_mode)) { 136 tf->tf_isdir = 0; 137 tf->tf_u.fd = open(path, O_RDONLY); 138 if (tf->tf_u.fd < 0) 139 goto out; 140 *h_return = tf; 141 return (0); 142 } 143 144 out: 145 free(tf); 146 return (EINVAL); 147 } 148 149 int 150 test_close(void *arg, void *h) 151 { 152 struct test_file *tf = h; 153 154 if (tf->tf_isdir) 155 closedir(tf->tf_u.dir); 156 else 157 close(tf->tf_u.fd); 158 free(tf); 159 160 return (0); 161 } 162 163 int 164 test_isdir(void *arg, void *h) 165 { 166 struct test_file *tf = h; 167 168 return (tf->tf_isdir); 169 } 170 171 int 172 test_read(void *arg, void *h, void *dst, size_t size, size_t *resid_return) 173 { 174 struct test_file *tf = h; 175 ssize_t sz; 176 177 if (tf->tf_isdir) 178 return (EINVAL); 179 sz = read(tf->tf_u.fd, dst, size); 180 if (sz < 0) 181 return (EINVAL); 182 *resid_return = size - sz; 183 return (0); 184 } 185 186 int 187 test_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return, 188 size_t *namelen_return, char *name) 189 { 190 struct test_file *tf = h; 191 struct dirent *dp; 192 193 if (!tf->tf_isdir) 194 return (EINVAL); 195 196 dp = readdir(tf->tf_u.dir); 197 if (!dp) 198 return (ENOENT); 199 200 /* 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. 203 */ 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; 209 210 return (0); 211 } 212 213 int 214 test_seek(void *arg, void *h, uint64_t offset, int whence) 215 { 216 struct test_file *tf = h; 217 218 if (tf->tf_isdir) 219 return (EINVAL); 220 if (lseek(tf->tf_u.fd, offset, whence) < 0) 221 return (errno); 222 return (0); 223 } 224 225 int 226 test_stat(void *arg, void *h, int *mode_return, int *uid_return, int *gid_return, 227 uint64_t *size_return) 228 { 229 struct test_file *tf = h; 230 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; 235 return (0); 236 } 237 238 /* 239 * Disk image i/o 240 */ 241 242 int 243 test_diskread(void *arg, int unit, uint64_t offset, void *dst, size_t size, 244 size_t *resid_return) 245 { 246 ssize_t n; 247 248 if (unit != 0 || disk_fd == -1) 249 return (EIO); 250 n = pread(disk_fd, dst, size, offset); 251 if (n < 0) 252 return (errno); 253 *resid_return = size - n; 254 return (0); 255 } 256 257 int 258 test_diskioctl(void *arg, int unit, u_long cmd, void *data) 259 { 260 struct stat sb; 261 262 if (unit != 0 || disk_fd == -1) 263 return (EBADF); 264 switch (cmd) { 265 case DIOCGSECTORSIZE: 266 *(u_int *)data = 512; 267 break; 268 case DIOCGMEDIASIZE: 269 if (fstat(disk_fd, &sb) == 0) 270 *(off_t *)data = sb.st_size; 271 else 272 return (ENOTTY); 273 break; 274 default: 275 return (ENOTTY); 276 } 277 return (0); 278 } 279 280 /* 281 * Guest virtual machine i/o 282 * 283 * Note: guest addresses are kernel virtual 284 */ 285 286 int 287 test_copyin(void *arg, const void *from, uint64_t to, size_t size) 288 { 289 290 to &= 0x7fffffff; 291 if (to > image_size) 292 return (EFAULT); 293 if (to + size > image_size) 294 size = image_size - to; 295 memcpy(&image[to], from, size); 296 return(0); 297 } 298 299 int 300 test_copyout(void *arg, uint64_t from, void *to, size_t size) 301 { 302 303 from &= 0x7fffffff; 304 if (from > image_size) 305 return (EFAULT); 306 if (from + size > image_size) 307 size = image_size - from; 308 memcpy(to, &image[from], size); 309 return(0); 310 } 311 312 void 313 test_setreg(void *arg, int r, uint64_t v) 314 { 315 316 if (r < 0 || r >= 16) 317 return; 318 regs[r] = v; 319 } 320 321 void 322 test_setmsr(void *arg, int r, uint64_t v) 323 { 324 } 325 326 void 327 test_setcr(void *arg, int r, uint64_t v) 328 { 329 } 330 331 void 332 test_setgdt(void *arg, uint64_t v, size_t sz) 333 { 334 } 335 336 void 337 test_exec(void *arg, uint64_t pc) 338 { 339 printf("Execute at 0x%"PRIu64"\n", pc); 340 test_exit(arg, 0); 341 } 342 343 /* 344 * Misc 345 */ 346 347 void 348 test_delay(void *arg, int usec) 349 { 350 351 usleep(usec); 352 } 353 354 void 355 test_exit(void *arg, int v) 356 { 357 358 tcsetattr(0, TCSAFLUSH, &oldterm); 359 exit(v); 360 } 361 362 void 363 test_getmem(void *arg, uint64_t *lowmem, uint64_t *highmem) 364 { 365 366 *lowmem = 128*1024*1024; 367 *highmem = 0; 368 } 369 370 char * 371 test_getenv(void *arg, int idx) 372 { 373 static char *vars[] = { 374 "foo=bar", 375 "bar=barbar", 376 NULL 377 }; 378 379 return (vars[idx]); 380 } 381 382 struct loader_callbacks cb = { 383 .putc = test_putc, 384 .getc = test_getc, 385 .poll = test_poll, 386 387 .open = test_open, 388 .close = test_close, 389 .isdir = test_isdir, 390 .read = test_read, 391 .readdir = test_readdir, 392 .seek = test_seek, 393 .stat = test_stat, 394 395 .diskread = test_diskread, 396 .diskioctl = test_diskioctl, 397 398 .copyin = test_copyin, 399 .copyout = test_copyout, 400 .setreg = test_setreg, 401 .setmsr = test_setmsr, 402 .setcr = test_setcr, 403 .setgdt = test_setgdt, 404 .exec = test_exec, 405 406 .delay = test_delay, 407 .exit = test_exit, 408 .getmem = test_getmem, 409 410 .getenv = test_getenv, 411 }; 412 413 void 414 usage() 415 { 416 417 printf("usage: [-b <userboot shared object>] [-d <disk image path>] [-h <host filesystem path>\n"); 418 exit(1); 419 } 420 421 int 422 main(int argc, char** argv) 423 { 424 void *h; 425 void (*func)(struct loader_callbacks *, void *, int, int) __dead2; 426 int opt; 427 char *disk_image = NULL; 428 const char *userboot_obj = "/boot/userboot.so"; 429 430 while ((opt = getopt(argc, argv, "b:d:h:")) != -1) { 431 switch (opt) { 432 case 'b': 433 userboot_obj = optarg; 434 break; 435 436 case 'd': 437 disk_image = optarg; 438 break; 439 440 case 'h': 441 host_base = optarg; 442 break; 443 444 case '?': 445 usage(); 446 } 447 } 448 449 h = dlopen(userboot_obj, RTLD_LOCAL); 450 if (!h) { 451 printf("%s\n", dlerror()); 452 return (1); 453 } 454 func = dlsym(h, "loader_main"); 455 if (!func) { 456 printf("%s\n", dlerror()); 457 return (1); 458 } 459 460 image_size = 128*1024*1024; 461 image = malloc(image_size); 462 if (disk_image) { 463 disk_fd = open(disk_image, O_RDONLY); 464 if (disk_fd < 0) 465 err(1, "Can't open disk image '%s'", disk_image); 466 } 467 468 tcgetattr(0, &term); 469 oldterm = term; 470 term.c_iflag &= ~(ICRNL); 471 term.c_lflag &= ~(ICANON|ECHO); 472 tcsetattr(0, TCSAFLUSH, &term); 473 474 func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0); 475 } 476