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