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