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