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