xref: /freebsd/stand/userboot/userboot/host.c (revision 61e21613)
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/cdefs.h>
28 /*
29  * Read from the host filesystem
30  */
31 
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <stddef.h>
35 #include <stdarg.h>
36 #include <string.h>
37 #include <stand.h>
38 #include <bootstrap.h>
39 
40 #include "libuserboot.h"
41 
42 /*
43  * Open a file.
44  */
45 static int
46 host_open(const char *upath, struct open_file *f)
47 {
48 
49 	if (f->f_dev != &host_dev)
50 		return (EINVAL);
51 
52 	return (CALLBACK(open, upath, &f->f_fsdata));
53 }
54 
55 static int
56 host_close(struct open_file *f)
57 {
58 
59         CALLBACK(close, f->f_fsdata);
60 	f->f_fsdata = (void *)0;
61 
62 	return (0);
63 }
64 
65 /*
66  * Copy a portion of a file into memory.
67  */
68 static int
69 host_read(struct open_file *f, void *start, size_t size, size_t *resid)
70 {
71 
72 	return (CALLBACK(read, f->f_fsdata, start, size, resid));
73 }
74 
75 static off_t
76 host_seek(struct open_file *f, off_t offset, int where)
77 {
78 
79 	return (CALLBACK(seek, f->f_fsdata, offset, where));
80 }
81 
82 static int
83 host_stat(struct open_file *f, struct stat *sb)
84 {
85 
86 	CALLBACK(stat, f->f_fsdata, sb);
87 	return (0);
88 }
89 
90 static int
91 host_readdir(struct open_file *f, struct dirent *d)
92 {
93 	uint32_t fileno;
94 	uint8_t type;
95 	size_t namelen;
96 	int rc;
97 
98 	rc = CALLBACK(readdir, f->f_fsdata, &fileno, &type, &namelen,
99             d->d_name);
100 	if (rc)
101 		return (rc);
102 
103 	d->d_fileno = fileno;
104 	d->d_type = type;
105 	d->d_namlen = namelen;
106 
107 	return (0);
108 }
109 
110 static int
111 host_dev_init(void)
112 {
113 
114 	return (0);
115 }
116 
117 static int
118 host_dev_print(int verbose)
119 {
120 	char line[80];
121 
122 	printf("%s devices:", host_dev.dv_name);
123 	if (pager_output("\n") != 0)
124 		return (1);
125 
126 	snprintf(line, sizeof(line), "    host%d:   Host filesystem\n", 0);
127 	return (pager_output(line));
128 }
129 
130 /*
131  * 'Open' the host device.
132  */
133 static int
134 host_dev_open(struct open_file *f, ...)
135 {
136 
137 	return (0);
138 }
139 
140 static int
141 host_dev_close(struct open_file *f)
142 {
143 
144 	return (0);
145 }
146 
147 static int
148 host_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
149     char *buf, size_t *rsize)
150 {
151 
152 	return (ENOSYS);
153 }
154 
155 struct fs_ops host_fsops = {
156 	.fs_name = "host",
157 	.fo_open = host_open,
158 	.fo_close = host_close,
159 	.fo_read = host_read,
160 	.fo_write = null_write,
161 	.fo_seek = host_seek,
162 	.fo_stat = host_stat,
163 	.fo_readdir = host_readdir,
164 };
165 
166 struct devsw host_dev = {
167 	.dv_name = "host",
168 	.dv_type = DEVT_NET,
169 	.dv_init = host_dev_init,
170 	.dv_strategy = host_dev_strategy,
171 	.dv_open = host_dev_open,
172 	.dv_close = host_dev_close,
173 	.dv_ioctl = noioctl,
174 	.dv_print = host_dev_print,
175 	.dv_cleanup = nullsys,
176 };
177