17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 1994-1996, 2002-2003 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate#include <sys/param.h>
307c478bdstevel@tonic-gate#include <sys/sysmacros.h>
317c478bdstevel@tonic-gate#include <sys/stat.h>
327c478bdstevel@tonic-gate#include <sys/bootvfs.h>
337c478bdstevel@tonic-gate#include <sys/bootsyms.h>
347c478bdstevel@tonic-gate#include <sys/promif.h>
357c478bdstevel@tonic-gate#include <sys/salib.h>
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gatestatic struct boot_fs_ops *dfl_fsw = (struct boot_fs_ops *)NULL;
387c478bdstevel@tonic-gatestatic char *fsmsg = "Fstype has not been selected yet!\n";
397c478bdstevel@tonic-gatestatic char *msg_noops = "not fs_ops supplied\n";
407c478bdstevel@tonic-gate
417c478bdstevel@tonic-gate/*
427c478bdstevel@tonic-gate * return fs_ops pointer for a given file system name
437c478bdstevel@tonic-gate */
447c478bdstevel@tonic-gatestruct boot_fs_ops *
457c478bdstevel@tonic-gateget_fs_ops_pointer(char *fsw_name)
467c478bdstevel@tonic-gate{
477c478bdstevel@tonic-gate	int	fsw_idx;
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate	for (fsw_idx = 0; fsw_idx < boot_nfsw; fsw_idx++)
507c478bdstevel@tonic-gate		if (strcmp(boot_fsw[fsw_idx]->fsw_name, fsw_name) == 0) {
517c478bdstevel@tonic-gate			return (boot_fsw[fsw_idx]);
527c478bdstevel@tonic-gate		}
537c478bdstevel@tonic-gate	return ((struct boot_fs_ops *)NULL);
547c478bdstevel@tonic-gate}
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate/*
577c478bdstevel@tonic-gate * set default file system type
587c478bdstevel@tonic-gate */
597c478bdstevel@tonic-gatevoid
607c478bdstevel@tonic-gateset_default_fs(char *fsw_name)
617c478bdstevel@tonic-gate{
627c478bdstevel@tonic-gate	int	fsw_idx;
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gate	for (fsw_idx = 0; fsw_idx < boot_nfsw; fsw_idx++)
657c478bdstevel@tonic-gate		if (strcmp(boot_fsw[fsw_idx]->fsw_name, fsw_name) == 0) {
667c478bdstevel@tonic-gate			dfl_fsw = boot_fsw[fsw_idx];
677c478bdstevel@tonic-gate			return;
687c478bdstevel@tonic-gate		}
697c478bdstevel@tonic-gate	printf("Fstype <%s> is not recognized\n", fsw_name);
707c478bdstevel@tonic-gate	prom_panic("");
717c478bdstevel@tonic-gate}
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gate/*
747c478bdstevel@tonic-gate * clear default file system type
757c478bdstevel@tonic-gate */
767c478bdstevel@tonic-gatevoid
777c478bdstevel@tonic-gateclr_default_fs(void)
787c478bdstevel@tonic-gate{
797c478bdstevel@tonic-gate	dfl_fsw = NULL;
807c478bdstevel@tonic-gate}
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gatestruct boot_fs_ops *
837c478bdstevel@tonic-gateget_default_fs(void)
847c478bdstevel@tonic-gate{
857c478bdstevel@tonic-gate	return (dfl_fsw);
867c478bdstevel@tonic-gate}
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gatevoid
897c478bdstevel@tonic-gateboot_no_ops_void()
907c478bdstevel@tonic-gate{
917c478bdstevel@tonic-gate	prom_panic(msg_noops);
927c478bdstevel@tonic-gate	/*NOTREACHED*/
937c478bdstevel@tonic-gate}
947c478bdstevel@tonic-gate
957c478bdstevel@tonic-gateint
967c478bdstevel@tonic-gateboot_no_ops()
977c478bdstevel@tonic-gate{
987c478bdstevel@tonic-gate	prom_panic(msg_noops);
997c478bdstevel@tonic-gate	/*NOTREACHED*/
1007c478bdstevel@tonic-gate	return (0);
1017c478bdstevel@tonic-gate}
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gateint
1047c478bdstevel@tonic-gateclose(int fd)
1057c478bdstevel@tonic-gate{
1067c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
1077c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_close)(fd));
1087c478bdstevel@tonic-gate	prom_panic(fsmsg);
1097c478bdstevel@tonic-gate	/*NOTREACHED*/
1107c478bdstevel@tonic-gate}
1117c478bdstevel@tonic-gate
1127c478bdstevel@tonic-gateint
1137c478bdstevel@tonic-gatemountroot(char *str)
1147c478bdstevel@tonic-gate{
1157c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
1167c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_mountroot)(str));
1177c478bdstevel@tonic-gate	prom_panic(fsmsg);
1187c478bdstevel@tonic-gate	/*NOTREACHED*/
1197c478bdstevel@tonic-gate}
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gateint
1227c478bdstevel@tonic-gateunmountroot(void)
1237c478bdstevel@tonic-gate{
1247c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
1257c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_unmountroot)());
1267c478bdstevel@tonic-gate	prom_panic(fsmsg);
1277c478bdstevel@tonic-gate	/*NOTREACHED*/
1287c478bdstevel@tonic-gate}
1297c478bdstevel@tonic-gate
1307c478bdstevel@tonic-gate/*ARGSUSED*/
1317c478bdstevel@tonic-gateint
1327c478bdstevel@tonic-gateopen(const char *filename, int flags)
1337c478bdstevel@tonic-gate{
1347c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
1357c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_open)((char *)filename, flags));
1367c478bdstevel@tonic-gate	prom_panic(fsmsg);
1377c478bdstevel@tonic-gate	/*NOTREACHED*/
1387c478bdstevel@tonic-gate}
1397c478bdstevel@tonic-gate
1407c478bdstevel@tonic-gatessize_t
1417c478bdstevel@tonic-gateread(int fd, void *buf, size_t size)
1427c478bdstevel@tonic-gate{
1437c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
1447c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_read)(fd, buf, size));
1457c478bdstevel@tonic-gate	prom_panic(fsmsg);
1467c478bdstevel@tonic-gate	/*NOTREACHED*/
1477c478bdstevel@tonic-gate}
1487c478bdstevel@tonic-gate
1497c478bdstevel@tonic-gatevoid
1507c478bdstevel@tonic-gatecloseall(int flag)
1517c478bdstevel@tonic-gate{
1527c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL) {
1537c478bdstevel@tonic-gate		(*dfl_fsw->fsw_closeall)(flag);
1547c478bdstevel@tonic-gate		return;
1557c478bdstevel@tonic-gate	}
1567c478bdstevel@tonic-gate	prom_panic(fsmsg);
1577c478bdstevel@tonic-gate	/*NOTREACHED*/
1587c478bdstevel@tonic-gate}
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gateint
1617c478bdstevel@tonic-gatefstat(int fd, struct stat *sb)
1627c478bdstevel@tonic-gate{
1637c478bdstevel@tonic-gate	struct bootstat buf;
1647c478bdstevel@tonic-gate	int ret;
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gate	if (dfl_fsw == NULL)
1677c478bdstevel@tonic-gate		prom_panic(fsmsg);
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gate	ret = (*dfl_fsw->fsw_fstat)(fd, &buf);
1707c478bdstevel@tonic-gate	if (ret == -1)
1717c478bdstevel@tonic-gate		return (-1);
1727c478bdstevel@tonic-gate
1737c478bdstevel@tonic-gate	sb->st_dev		= buf.st_dev;
1747c478bdstevel@tonic-gate	sb->st_ino		= buf.st_ino;
1757c478bdstevel@tonic-gate	sb->st_mode		= buf.st_mode;
1767c478bdstevel@tonic-gate	sb->st_nlink		= buf.st_nlink;
1777c478bdstevel@tonic-gate	sb->st_uid 		= buf.st_uid;
1787c478bdstevel@tonic-gate	sb->st_gid		= buf.st_gid;
1797c478bdstevel@tonic-gate	sb->st_rdev		= buf.st_rdev;
1807c478bdstevel@tonic-gate	sb->st_size		= (off_t)buf.st_size;
1817c478bdstevel@tonic-gate	sb->st_blksize		= buf.st_blksize;
1827c478bdstevel@tonic-gate	sb->st_blocks		= buf.st_blocks;
1837c478bdstevel@tonic-gate	sb->st_atim.tv_sec	= buf.st_atim.tv_sec;
1847c478bdstevel@tonic-gate	sb->st_atim.tv_nsec	= buf.st_atim.tv_nsec;
1857c478bdstevel@tonic-gate	sb->st_mtim.tv_sec 	= buf.st_mtim.tv_sec;
1867c478bdstevel@tonic-gate	sb->st_mtim.tv_nsec	= buf.st_mtim.tv_nsec;
1877c478bdstevel@tonic-gate	sb->st_ctim.tv_sec	= buf.st_ctim.tv_sec;
1887c478bdstevel@tonic-gate	sb->st_ctim.tv_nsec	= buf.st_ctim.tv_nsec;
1897c478bdstevel@tonic-gate
1907c478bdstevel@tonic-gate	(void) memcpy(sb->st_fstype, buf.st_fstype, sizeof (sb->st_fstype));
1917c478bdstevel@tonic-gate	return (0);
1927c478bdstevel@tonic-gate}
1937c478bdstevel@tonic-gate
1947c478bdstevel@tonic-gateint
1957c478bdstevel@tonic-gatestat(const char *filename, struct stat *sb)
1967c478bdstevel@tonic-gate{
1977c478bdstevel@tonic-gate	int fd, ret = -1;
1987c478bdstevel@tonic-gate
1997c478bdstevel@tonic-gate	if ((fd = open(filename, O_RDONLY)) != -1) {
2007c478bdstevel@tonic-gate		ret = fstat(fd, sb);
2017c478bdstevel@tonic-gate		(void) close(fd);
2027c478bdstevel@tonic-gate	}
2037c478bdstevel@tonic-gate
2047c478bdstevel@tonic-gate	return (ret);
2057c478bdstevel@tonic-gate}
2067c478bdstevel@tonic-gate
2077c478bdstevel@tonic-gateoff_t
2087c478bdstevel@tonic-gatelseek(int filefd, off_t addr, int whence)
2097c478bdstevel@tonic-gate{
2107c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
2117c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_lseek)(filefd, addr, whence));
2127c478bdstevel@tonic-gate	prom_panic(fsmsg);
2137c478bdstevel@tonic-gate	/*NOTREACHED*/
2147c478bdstevel@tonic-gate}
2157c478bdstevel@tonic-gate
2167c478bdstevel@tonic-gate/*
2177c478bdstevel@tonic-gate * Kernel Interface
2187c478bdstevel@tonic-gate */
2197c478bdstevel@tonic-gateint
2207c478bdstevel@tonic-gatekern_open(char *str, int flags)
2217c478bdstevel@tonic-gate{
2227c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
2237c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_open)(str, flags));
2247c478bdstevel@tonic-gate	prom_panic(fsmsg);
2257c478bdstevel@tonic-gate	/*NOTREACHED*/
2267c478bdstevel@tonic-gate}
2277c478bdstevel@tonic-gate
2287c478bdstevel@tonic-gate/*
2297c478bdstevel@tonic-gate *  hi and lo refer to the MS end of the off_t word
2307c478bdstevel@tonic-gate *  and the LS end of the off_t word for when we want
2317c478bdstevel@tonic-gate *  to support 64-bit offsets.  For now, lseek() just
2327c478bdstevel@tonic-gate *  supports 32 bits.
2337c478bdstevel@tonic-gate */
2347c478bdstevel@tonic-gate
2357c478bdstevel@tonic-gate/*ARGSUSED*/
2367c478bdstevel@tonic-gateoff_t
2377c478bdstevel@tonic-gatekern_lseek(int filefd, off_t hi, off_t lo)
2387c478bdstevel@tonic-gate{
2397c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
2407c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_lseek)(filefd, lo, 0));
2417c478bdstevel@tonic-gate	prom_panic(fsmsg);
2427c478bdstevel@tonic-gate	/*NOTREACHED*/
2437c478bdstevel@tonic-gate}
2447c478bdstevel@tonic-gate
2457c478bdstevel@tonic-gatessize_t
2467c478bdstevel@tonic-gatekern_read(int fd, caddr_t buf, size_t size)
2477c478bdstevel@tonic-gate{
2487c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
2497c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_read)(fd, buf, size));
2507c478bdstevel@tonic-gate	prom_panic(fsmsg);
2517c478bdstevel@tonic-gate	/*NOTREACHED*/
2527c478bdstevel@tonic-gate}
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gateint
2557c478bdstevel@tonic-gatekern_close(int fd)
2567c478bdstevel@tonic-gate{
2577c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
2587c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_close)(fd));
2597c478bdstevel@tonic-gate	prom_panic(fsmsg);
2607c478bdstevel@tonic-gate	/*NOTREACHED*/
2617c478bdstevel@tonic-gate}
2627c478bdstevel@tonic-gate
2637c478bdstevel@tonic-gateint
2647c478bdstevel@tonic-gatekern_fstat(int fd, struct bootstat *buf)
2657c478bdstevel@tonic-gate{
2667c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
2677c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_fstat)(fd, buf));
2687c478bdstevel@tonic-gate	prom_panic(fsmsg);
2697c478bdstevel@tonic-gate	/*NOTREACHED*/
2707c478bdstevel@tonic-gate}
2717c478bdstevel@tonic-gate
2727c478bdstevel@tonic-gateint
2737c478bdstevel@tonic-gatekern_getdents(int fd, struct dirent *buf, size_t size)
2747c478bdstevel@tonic-gate{
2757c478bdstevel@tonic-gate	if (dfl_fsw != (struct boot_fs_ops *)NULL)
2767c478bdstevel@tonic-gate		return ((*dfl_fsw->fsw_getdents)(fd, buf, size));
2777c478bdstevel@tonic-gate	prom_panic(fsmsg);
2787c478bdstevel@tonic-gate	/*NOTREACHED*/
2797c478bdstevel@tonic-gate}
2807c478bdstevel@tonic-gate
2817c478bdstevel@tonic-gateint
2827c478bdstevel@tonic-gatekern_mountroot(char *path)
2837c478bdstevel@tonic-gate{
2847c478bdstevel@tonic-gate	return (mountroot(path));
2857c478bdstevel@tonic-gate}
2867c478bdstevel@tonic-gate
2877c478bdstevel@tonic-gateint
2887c478bdstevel@tonic-gatekern_unmountroot(void)
2897c478bdstevel@tonic-gate{
2907c478bdstevel@tonic-gate	return (unmountroot());
2917c478bdstevel@tonic-gate}
292