1223695Sdfr/*-
2223695Sdfr * Copyright (c) 2011 Google, Inc.
3223695Sdfr * All rights reserved.
4223695Sdfr *
5223695Sdfr * Redistribution and use in source and binary forms, with or without
6223695Sdfr * modification, are permitted provided that the following conditions
7223695Sdfr * are met:
8223695Sdfr * 1. Redistributions of source code must retain the above copyright
9223695Sdfr *    notice, this list of conditions and the following disclaimer.
10223695Sdfr * 2. Redistributions in binary form must reproduce the above copyright
11223695Sdfr *    notice, this list of conditions and the following disclaimer in the
12223695Sdfr *    documentation and/or other materials provided with the distribution.
13223695Sdfr *
14223695Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15223695Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16223695Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17223695Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18223695Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19223695Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20223695Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21223695Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22223695Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23223695Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24223695Sdfr * SUCH DAMAGE.
25223695Sdfr */
26223695Sdfr
27223695Sdfr#include <sys/cdefs.h>
28223695Sdfr__FBSDID("$FreeBSD$");
29223695Sdfr
30223695Sdfr/*
31223695Sdfr * Read from the host filesystem
32223695Sdfr */
33223695Sdfr
34223695Sdfr#include <sys/param.h>
35223695Sdfr#include <sys/time.h>
36223695Sdfr#include <stddef.h>
37223695Sdfr#include <stdarg.h>
38223695Sdfr#include <string.h>
39223695Sdfr#include <stand.h>
40223695Sdfr#include <bootstrap.h>
41223695Sdfr
42223695Sdfr#include "libuserboot.h"
43223695Sdfr
44223695Sdfr/*
45223695Sdfr * Open a file.
46223695Sdfr */
47223695Sdfrstatic int
48223695Sdfrhost_open(const char *upath, struct open_file *f)
49223695Sdfr{
50223695Sdfr
51223695Sdfr	if (f->f_dev != &host_dev)
52223695Sdfr		return (EINVAL);
53223695Sdfr
54223695Sdfr	return (CALLBACK(open, upath, &f->f_fsdata));
55223695Sdfr}
56223695Sdfr
57223695Sdfrstatic int
58223695Sdfrhost_close(struct open_file *f)
59223695Sdfr{
60223695Sdfr
61223695Sdfr        CALLBACK(close, f->f_fsdata);
62223695Sdfr	f->f_fsdata = (void *)0;
63223695Sdfr
64223695Sdfr	return (0);
65223695Sdfr}
66223695Sdfr
67223695Sdfr/*
68223695Sdfr * Copy a portion of a file into memory.
69223695Sdfr */
70223695Sdfrstatic int
71223695Sdfrhost_read(struct open_file *f, void *start, size_t size, size_t *resid)
72223695Sdfr{
73223695Sdfr
74223695Sdfr	return (CALLBACK(read, f->f_fsdata, start, size, resid));
75223695Sdfr}
76223695Sdfr
77223695Sdfr/*
78223695Sdfr * Don't be silly - the bootstrap has no business writing anything.
79223695Sdfr */
80223695Sdfrstatic int
81223695Sdfrhost_write(struct open_file *f, void *start, size_t size, size_t *resid)
82223695Sdfr{
83223695Sdfr
84223695Sdfr	return (EROFS);
85223695Sdfr}
86223695Sdfr
87223695Sdfrstatic off_t
88223695Sdfrhost_seek(struct open_file *f, off_t offset, int where)
89223695Sdfr{
90223695Sdfr
91223695Sdfr	return (CALLBACK(seek, f->f_fsdata, offset, where));
92223695Sdfr}
93223695Sdfr
94223695Sdfrstatic int
95223695Sdfrhost_stat(struct open_file *f, struct stat *sb)
96223695Sdfr{
97223695Sdfr	int mode;
98223695Sdfr	int uid;
99223695Sdfr	int gid;
100223695Sdfr	uint64_t size;
101223695Sdfr
102223695Sdfr	CALLBACK(stat, f->f_fsdata, &mode, &uid, &gid, &size);
103223695Sdfr	sb->st_mode = mode;
104223695Sdfr	sb->st_uid = uid;
105223695Sdfr	sb->st_gid = gid;
106223695Sdfr	sb->st_size = size;
107223695Sdfr	return (0);
108223695Sdfr}
109223695Sdfr
110223695Sdfrstatic int
111223695Sdfrhost_readdir(struct open_file *f, struct dirent *d)
112223695Sdfr{
113223695Sdfr	uint32_t fileno;
114223695Sdfr	uint8_t type;
115223695Sdfr	size_t namelen;
116223695Sdfr	int rc;
117223695Sdfr
118223695Sdfr	rc = CALLBACK(readdir, f->f_fsdata, &fileno, &type, &namelen,
119223695Sdfr            d->d_name);
120223695Sdfr	if (rc)
121223695Sdfr		return (rc);
122223695Sdfr
123223695Sdfr	d->d_fileno = fileno;
124223695Sdfr	d->d_type = type;
125223695Sdfr	d->d_namlen = namelen;
126223695Sdfr
127223695Sdfr	return (0);
128223695Sdfr}
129223695Sdfr
130223695Sdfrstatic int
131223695Sdfrhost_dev_init(void)
132223695Sdfr{
133223695Sdfr
134223695Sdfr	return (0);
135223695Sdfr}
136223695Sdfr
137223695Sdfrstatic void
138223695Sdfrhost_dev_print(int verbose)
139223695Sdfr{
140223695Sdfr	char line[80];
141223695Sdfr
142223695Sdfr	sprintf(line, "    host%d:   Host filesystem\n", 0);
143223695Sdfr	pager_output(line);
144223695Sdfr}
145223695Sdfr
146223695Sdfr/*
147223695Sdfr * 'Open' the host device.
148223695Sdfr */
149223695Sdfrstatic int
150223695Sdfrhost_dev_open(struct open_file *f, ...)
151223695Sdfr{
152223695Sdfr	va_list		args;
153223695Sdfr	struct devdesc	*dev;
154223695Sdfr
155223695Sdfr	va_start(args, f);
156223695Sdfr	dev = va_arg(args, struct devdesc*);
157223695Sdfr	va_end(args);
158223695Sdfr
159223695Sdfr	return (0);
160223695Sdfr}
161223695Sdfr
162223695Sdfrstatic int
163223695Sdfrhost_dev_close(struct open_file *f)
164223695Sdfr{
165223695Sdfr
166223695Sdfr	return (0);
167223695Sdfr}
168223695Sdfr
169223695Sdfrstatic int
170223695Sdfrhost_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
171223695Sdfr    char *buf, size_t *rsize)
172223695Sdfr{
173223695Sdfr
174223695Sdfr	return (ENOSYS);
175223695Sdfr}
176223695Sdfr
177223695Sdfrstruct fs_ops host_fsops = {
178223695Sdfr	"host",
179223695Sdfr	host_open,
180223695Sdfr	host_close,
181223695Sdfr	host_read,
182223695Sdfr	host_write,
183223695Sdfr	host_seek,
184223695Sdfr	host_stat,
185223695Sdfr	host_readdir
186223695Sdfr};
187223695Sdfr
188223695Sdfrstruct devsw host_dev = {
189223695Sdfr	.dv_name = "host",
190223695Sdfr	.dv_type = DEVT_NET,
191223695Sdfr	.dv_init = host_dev_init,
192223695Sdfr	.dv_strategy = host_dev_strategy,
193223695Sdfr	.dv_open = host_dev_open,
194223695Sdfr	.dv_close = host_dev_close,
195223695Sdfr	.dv_ioctl = noioctl,
196223695Sdfr	.dv_print = host_dev_print,
197223695Sdfr	.dv_cleanup = NULL
198223695Sdfr};
199