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