1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 1994-1996, 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29#include <sys/param.h>
30#include <sys/sysmacros.h>
31#include <sys/stat.h>
32#include <sys/bootvfs.h>
33#include <sys/bootsyms.h>
34#include <sys/promif.h>
35#include <sys/salib.h>
36
37static struct boot_fs_ops *dfl_fsw = (struct boot_fs_ops *)NULL;
38static char *fsmsg = "Fstype has not been selected yet!\n";
39static char *msg_noops = "not fs_ops supplied\n";
40
41/*
42 * return fs_ops pointer for a given file system name
43 */
44struct boot_fs_ops *
45get_fs_ops_pointer(char *fsw_name)
46{
47	int	fsw_idx;
48
49	for (fsw_idx = 0; fsw_idx < boot_nfsw; fsw_idx++)
50		if (strcmp(boot_fsw[fsw_idx]->fsw_name, fsw_name) == 0) {
51			return (boot_fsw[fsw_idx]);
52		}
53	return ((struct boot_fs_ops *)NULL);
54}
55
56/*
57 * set default file system type
58 */
59void
60set_default_fs(char *fsw_name)
61{
62	int	fsw_idx;
63
64	for (fsw_idx = 0; fsw_idx < boot_nfsw; fsw_idx++)
65		if (strcmp(boot_fsw[fsw_idx]->fsw_name, fsw_name) == 0) {
66			dfl_fsw = boot_fsw[fsw_idx];
67			return;
68		}
69	printf("Fstype <%s> is not recognized\n", fsw_name);
70	prom_panic("");
71}
72
73/*
74 * clear default file system type
75 */
76void
77clr_default_fs(void)
78{
79	dfl_fsw = NULL;
80}
81
82struct boot_fs_ops *
83get_default_fs(void)
84{
85	return (dfl_fsw);
86}
87
88void
89boot_no_ops_void()
90{
91	prom_panic(msg_noops);
92	/*NOTREACHED*/
93}
94
95int
96boot_no_ops()
97{
98	prom_panic(msg_noops);
99	/*NOTREACHED*/
100	return (0);
101}
102
103int
104close(int fd)
105{
106	if (dfl_fsw != (struct boot_fs_ops *)NULL)
107		return ((*dfl_fsw->fsw_close)(fd));
108	prom_panic(fsmsg);
109	/*NOTREACHED*/
110}
111
112int
113mountroot(char *str)
114{
115	if (dfl_fsw != (struct boot_fs_ops *)NULL)
116		return ((*dfl_fsw->fsw_mountroot)(str));
117	prom_panic(fsmsg);
118	/*NOTREACHED*/
119}
120
121int
122unmountroot(void)
123{
124	if (dfl_fsw != (struct boot_fs_ops *)NULL)
125		return ((*dfl_fsw->fsw_unmountroot)());
126	prom_panic(fsmsg);
127	/*NOTREACHED*/
128}
129
130/*ARGSUSED*/
131int
132open(const char *filename, int flags)
133{
134	if (dfl_fsw != (struct boot_fs_ops *)NULL)
135		return ((*dfl_fsw->fsw_open)((char *)filename, flags));
136	prom_panic(fsmsg);
137	/*NOTREACHED*/
138}
139
140ssize_t
141read(int fd, void *buf, size_t size)
142{
143	if (dfl_fsw != (struct boot_fs_ops *)NULL)
144		return ((*dfl_fsw->fsw_read)(fd, buf, size));
145	prom_panic(fsmsg);
146	/*NOTREACHED*/
147}
148
149void
150closeall(int flag)
151{
152	if (dfl_fsw != (struct boot_fs_ops *)NULL) {
153		(*dfl_fsw->fsw_closeall)(flag);
154		return;
155	}
156	prom_panic(fsmsg);
157	/*NOTREACHED*/
158}
159
160int
161fstat(int fd, struct stat *sb)
162{
163	struct bootstat buf;
164	int ret;
165
166	if (dfl_fsw == NULL)
167		prom_panic(fsmsg);
168
169	ret = (*dfl_fsw->fsw_fstat)(fd, &buf);
170	if (ret == -1)
171		return (-1);
172
173	sb->st_dev		= buf.st_dev;
174	sb->st_ino		= buf.st_ino;
175	sb->st_mode		= buf.st_mode;
176	sb->st_nlink		= buf.st_nlink;
177	sb->st_uid 		= buf.st_uid;
178	sb->st_gid		= buf.st_gid;
179	sb->st_rdev		= buf.st_rdev;
180	sb->st_size		= (off_t)buf.st_size;
181	sb->st_blksize		= buf.st_blksize;
182	sb->st_blocks		= buf.st_blocks;
183	sb->st_atim.tv_sec	= buf.st_atim.tv_sec;
184	sb->st_atim.tv_nsec	= buf.st_atim.tv_nsec;
185	sb->st_mtim.tv_sec 	= buf.st_mtim.tv_sec;
186	sb->st_mtim.tv_nsec	= buf.st_mtim.tv_nsec;
187	sb->st_ctim.tv_sec	= buf.st_ctim.tv_sec;
188	sb->st_ctim.tv_nsec	= buf.st_ctim.tv_nsec;
189
190	(void) memcpy(sb->st_fstype, buf.st_fstype, sizeof (sb->st_fstype));
191	return (0);
192}
193
194int
195stat(const char *filename, struct stat *sb)
196{
197	int fd, ret = -1;
198
199	if ((fd = open(filename, O_RDONLY)) != -1) {
200		ret = fstat(fd, sb);
201		(void) close(fd);
202	}
203
204	return (ret);
205}
206
207off_t
208lseek(int filefd, off_t addr, int whence)
209{
210	if (dfl_fsw != (struct boot_fs_ops *)NULL)
211		return ((*dfl_fsw->fsw_lseek)(filefd, addr, whence));
212	prom_panic(fsmsg);
213	/*NOTREACHED*/
214}
215
216/*
217 * Kernel Interface
218 */
219int
220kern_open(char *str, int flags)
221{
222	if (dfl_fsw != (struct boot_fs_ops *)NULL)
223		return ((*dfl_fsw->fsw_open)(str, flags));
224	prom_panic(fsmsg);
225	/*NOTREACHED*/
226}
227
228/*
229 *  hi and lo refer to the MS end of the off_t word
230 *  and the LS end of the off_t word for when we want
231 *  to support 64-bit offsets.  For now, lseek() just
232 *  supports 32 bits.
233 */
234
235/*ARGSUSED*/
236off_t
237kern_lseek(int filefd, off_t hi, off_t lo)
238{
239	if (dfl_fsw != (struct boot_fs_ops *)NULL)
240		return ((*dfl_fsw->fsw_lseek)(filefd, lo, 0));
241	prom_panic(fsmsg);
242	/*NOTREACHED*/
243}
244
245ssize_t
246kern_read(int fd, caddr_t buf, size_t size)
247{
248	if (dfl_fsw != (struct boot_fs_ops *)NULL)
249		return ((*dfl_fsw->fsw_read)(fd, buf, size));
250	prom_panic(fsmsg);
251	/*NOTREACHED*/
252}
253
254int
255kern_close(int fd)
256{
257	if (dfl_fsw != (struct boot_fs_ops *)NULL)
258		return ((*dfl_fsw->fsw_close)(fd));
259	prom_panic(fsmsg);
260	/*NOTREACHED*/
261}
262
263int
264kern_fstat(int fd, struct bootstat *buf)
265{
266	if (dfl_fsw != (struct boot_fs_ops *)NULL)
267		return ((*dfl_fsw->fsw_fstat)(fd, buf));
268	prom_panic(fsmsg);
269	/*NOTREACHED*/
270}
271
272int
273kern_getdents(int fd, struct dirent *buf, size_t size)
274{
275	if (dfl_fsw != (struct boot_fs_ops *)NULL)
276		return ((*dfl_fsw->fsw_getdents)(fd, buf, size));
277	prom_panic(fsmsg);
278	/*NOTREACHED*/
279}
280
281int
282kern_mountroot(char *path)
283{
284	return (mountroot(path));
285}
286
287int
288kern_unmountroot(void)
289{
290	return (unmountroot());
291}
292