xref: /freebsd/stand/userboot/test/test.c (revision b3e76948)
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 
27 #include <sys/types.h>
28 #include <sys/disk.h>
29 #include <sys/ioctl.h>
30 #include <sys/stat.h>
31 #include <dirent.h>
32 #include <dlfcn.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <getopt.h>
37 #include <inttypes.h>
38 #include <limits.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <termios.h>
43 #include <unistd.h>
44 
45 #include <userboot.h>
46 
47 char *host_base = NULL;
48 struct termios term, oldterm;
49 char *image;
50 size_t image_size;
51 
52 uint64_t regs[16];
53 uint64_t pc;
54 int *disk_fd;
55 int disk_index = -1;
56 
57 void test_exit(void *arg, int v);
58 
59 /*
60  * Console i/o
61  */
62 
63 void
test_putc(void * arg,int ch)64 test_putc(void *arg, int ch)
65 {
66 	char c = ch;
67 
68 	write(1, &c, 1);
69 }
70 
71 int
test_getc(void * arg)72 test_getc(void *arg)
73 {
74 	char c;
75 
76 	if (read(0, &c, 1) == 1)
77 		return c;
78 	return -1;
79 }
80 
81 int
test_poll(void * arg)82 test_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 
95 struct 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 
105 int
test_open(void * arg,const char * filename,void ** h_return)106 test_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 
143 out:
144 	free(tf);
145 	return (EINVAL);
146 }
147 
148 int
test_close(void * arg,void * h)149 test_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 
162 int
test_isdir(void * arg,void * h)163 test_isdir(void *arg, void *h)
164 {
165 	struct test_file *tf = h;
166 
167 	return (tf->tf_isdir);
168 }
169 
170 int
test_read(void * arg,void * h,void * dst,size_t size,size_t * resid_return)171 test_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 
185 int
test_readdir(void * arg,void * h,uint32_t * fileno_return,uint8_t * type_return,size_t * namelen_return,char * name)186 test_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 
212 int
test_seek(void * arg,void * h,uint64_t offset,int whence)213 test_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 
224 int
test_stat(void * arg,void * h,struct stat * stp)225 test_stat(void *arg, void *h, struct stat *stp)
226 {
227 	struct test_file *tf = h;
228 
229 	if (!stp)
230 		return (-1);
231 	memset(stp, 0, sizeof(struct stat));
232 	stp->st_mode = tf->tf_stat.st_mode;
233 	stp->st_uid = tf->tf_stat.st_uid;
234 	stp->st_gid = tf->tf_stat.st_gid;
235 	stp->st_size = tf->tf_stat.st_size;
236 	stp->st_ino = tf->tf_stat.st_ino;
237 	stp->st_dev = tf->tf_stat.st_dev;
238 	stp->st_mtime = tf->tf_stat.st_mtime;
239 	return (0);
240 }
241 
242 /*
243  * Disk image i/o
244  */
245 
246 int
test_diskread(void * arg,int unit,uint64_t offset,void * dst,size_t size,size_t * resid_return)247 test_diskread(void *arg, int unit, uint64_t offset, void *dst, size_t size,
248     size_t *resid_return)
249 {
250 	ssize_t n;
251 
252 	if (unit > disk_index || disk_fd[unit] == -1)
253 		return (EIO);
254 	n = pread(disk_fd[unit], dst, size, offset);
255 	if (n == 0) {
256 		printf("%s: end of disk (%ju)\n", __func__, (intmax_t)offset);
257 		return (EIO);
258 	}
259 
260 	if (n < 0)
261 		return (errno);
262 	*resid_return = size - n;
263 	return (0);
264 }
265 
266 int
test_diskwrite(void * arg,int unit,uint64_t offset,void * src,size_t size,size_t * resid_return)267 test_diskwrite(void *arg, int unit, uint64_t offset, void *src, size_t size,
268     size_t *resid_return)
269 {
270 	ssize_t n;
271 
272 	if (unit > disk_index || disk_fd[unit] == -1)
273 		return (EIO);
274 	n = pwrite(disk_fd[unit], src, size, offset);
275 	if (n < 0)
276 		return (errno);
277 	*resid_return = size - n;
278 	return (0);
279 }
280 
281 int
test_diskioctl(void * arg,int unit,u_long cmd,void * data)282 test_diskioctl(void *arg, int unit, u_long cmd, void *data)
283 {
284 	struct stat sb;
285 
286 	if (unit > disk_index || disk_fd[unit] == -1)
287 		return (EBADF);
288 	switch (cmd) {
289 	case DIOCGSECTORSIZE:
290 		*(u_int *)data = 512;
291 		break;
292 	case DIOCGMEDIASIZE:
293 		if (fstat(disk_fd[unit], &sb) == 0)
294 			*(off_t *)data = sb.st_size;
295 		else
296 			return (ENOTTY);
297 		break;
298 	default:
299 		return (ENOTTY);
300 	}
301 	return (0);
302 }
303 
304 /*
305  * Guest virtual machine i/o
306  *
307  * Note: guest addresses are kernel virtual
308  */
309 
310 int
test_copyin(void * arg,const void * from,uint64_t to,size_t size)311 test_copyin(void *arg, const void *from, uint64_t to, size_t size)
312 {
313 
314 	to &= 0x7fffffff;
315 	if (to > image_size)
316 		return (EFAULT);
317 	if (to + size > image_size)
318 		size = image_size - to;
319 	memcpy(&image[to], from, size);
320 	return(0);
321 }
322 
323 int
test_copyout(void * arg,uint64_t from,void * to,size_t size)324 test_copyout(void *arg, uint64_t from, void *to, size_t size)
325 {
326 
327 	from &= 0x7fffffff;
328 	if (from > image_size)
329 		return (EFAULT);
330 	if (from + size > image_size)
331 		size = image_size - from;
332 	memcpy(to, &image[from], size);
333 	return(0);
334 }
335 
336 void
test_setreg(void * arg,int r,uint64_t v)337 test_setreg(void *arg, int r, uint64_t v)
338 {
339 
340 	if (r < 0 || r >= 16)
341 		return;
342 	regs[r] = v;
343 }
344 
345 void
test_setmsr(void * arg,int r,uint64_t v)346 test_setmsr(void *arg, int r, uint64_t v)
347 {
348 }
349 
350 void
test_setcr(void * arg,int r,uint64_t v)351 test_setcr(void *arg, int r, uint64_t v)
352 {
353 }
354 
355 void
test_setgdt(void * arg,uint64_t v,size_t sz)356 test_setgdt(void *arg, uint64_t v, size_t sz)
357 {
358 }
359 
360 void
test_exec(void * arg,uint64_t pc)361 test_exec(void *arg, uint64_t pc)
362 {
363 	printf("Execute at 0x%"PRIx64"\n", pc);
364 	test_exit(arg, 0);
365 }
366 
367 /*
368  * Misc
369  */
370 
371 void
test_delay(void * arg,int usec)372 test_delay(void *arg, int usec)
373 {
374 
375 	usleep(usec);
376 }
377 
378 void
test_exit(void * arg,int v)379 test_exit(void *arg, int v)
380 {
381 
382 	tcsetattr(0, TCSAFLUSH, &oldterm);
383 	exit(v);
384 }
385 
386 void
test_getmem(void * arg,uint64_t * lowmem,uint64_t * highmem)387 test_getmem(void *arg, uint64_t *lowmem, uint64_t *highmem)
388 {
389 
390         *lowmem = 128*1024*1024;
391         *highmem = 0;
392 }
393 
394 char *
test_getenv(void * arg,int idx)395 test_getenv(void *arg, int idx)
396 {
397 	static char *vars[] = {
398 		"foo=bar",
399 		"bar=barbar",
400 		NULL
401 	};
402 
403 	return (vars[idx]);
404 }
405 
406 struct loader_callbacks cb = {
407 	.putc = test_putc,
408 	.getc = test_getc,
409 	.poll = test_poll,
410 
411 	.open = test_open,
412 	.close = test_close,
413 	.isdir = test_isdir,
414 	.read = test_read,
415 	.readdir = test_readdir,
416 	.seek = test_seek,
417 	.stat = test_stat,
418 
419 	.diskread = test_diskread,
420 	.diskwrite = test_diskwrite,
421 	.diskioctl = test_diskioctl,
422 
423 	.copyin = test_copyin,
424 	.copyout = test_copyout,
425 	.setreg = test_setreg,
426 	.setmsr = test_setmsr,
427 	.setcr = test_setcr,
428         .setgdt = test_setgdt,
429 	.exec = test_exec,
430 
431 	.delay = test_delay,
432 	.exit = test_exit,
433         .getmem = test_getmem,
434 
435 	.getenv = test_getenv,
436 };
437 
438 void
usage()439 usage()
440 {
441 
442 	printf("usage: [-b <userboot shared object>] [-d <disk image path>] [-h <host filesystem path>\n");
443 	exit(1);
444 }
445 
446 int
main(int argc,char ** argv)447 main(int argc, char** argv)
448 {
449 	void *h;
450 	void (*func)(struct loader_callbacks *, void *, int, int) __dead2;
451 	int opt;
452 	const char *userboot_obj = "/boot/userboot.so";
453 	int oflag = O_RDONLY;
454 
455 	while ((opt = getopt(argc, argv, "wb:d:h:")) != -1) {
456 		switch (opt) {
457 		case 'b':
458 			userboot_obj = optarg;
459 			break;
460 
461 		case 'd':
462 			disk_index++;
463 			disk_fd = reallocarray(disk_fd, disk_index + 1,
464 			    sizeof (int));
465 			disk_fd[disk_index] = open(optarg, oflag);
466 			if (disk_fd[disk_index] < 0)
467 				err(1, "Can't open disk image '%s'", optarg);
468 			break;
469 
470 		case 'h':
471 			host_base = optarg;
472 			break;
473 
474 		case 'w':
475 			oflag = O_RDWR;
476 			break;
477 
478 		case '?':
479 			usage();
480 		}
481 	}
482 
483 	h = dlopen(userboot_obj, RTLD_LOCAL);
484 	if (!h) {
485 		printf("%s\n", dlerror());
486 		return (1);
487 	}
488 	func = dlsym(h, "loader_main");
489 	if (!func) {
490 		printf("%s\n", dlerror());
491 		return (1);
492 	}
493 
494 	image_size = 128*1024*1024;
495 	image = malloc(image_size);
496 
497 	tcgetattr(0, &term);
498 	oldterm = term;
499 	term.c_iflag &= ~(ICRNL);
500 	term.c_lflag &= ~(ICANON|ECHO);
501 	tcsetattr(0, TCSAFLUSH, &term);
502 
503 	func(&cb, NULL, USERBOOT_VERSION_3, disk_index + 1);
504 }
505