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