Home | History | Annotate | Line # | Download | only in boot
devopen.c revision 1.4
      1  1.4  tsutsui /*	$NetBSD: devopen.c,v 1.4 2002/04/30 01:14:39 tsutsui Exp $	*/
      2  1.1   tsubai 
      3  1.1   tsubai /*-
      4  1.1   tsubai  * Copyright (C) 1999 Tsubai Masanari.  All rights reserved.
      5  1.1   tsubai  *
      6  1.1   tsubai  * Redistribution and use in source and binary forms, with or without
      7  1.1   tsubai  * modification, are permitted provided that the following conditions
      8  1.1   tsubai  * are met:
      9  1.1   tsubai  * 1. Redistributions of source code must retain the above copyright
     10  1.1   tsubai  *    notice, this list of conditions and the following disclaimer.
     11  1.1   tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   tsubai  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   tsubai  *    documentation and/or other materials provided with the distribution.
     14  1.1   tsubai  * 3. The name of the author may not be used to endorse or promote products
     15  1.1   tsubai  *    derived from this software without specific prior written permission.
     16  1.1   tsubai  *
     17  1.1   tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1   tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1   tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1   tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1   tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1   tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1   tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1   tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1   tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  1.1   tsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1   tsubai  */
     28  1.1   tsubai 
     29  1.1   tsubai #include <lib/libkern/libkern.h>
     30  1.1   tsubai #include <lib/libsa/stand.h>
     31  1.1   tsubai #include <lib/libsa/ufs.h>
     32  1.3  tsutsui #include <lib/libsa/ustarfs.h>
     33  1.2   tsubai #include <netinet/in.h>
     34  1.2   tsubai #include <lib/libsa/nfs.h>
     35  1.1   tsubai 
     36  1.2   tsubai #include <machine/apcall.h>
     37  1.1   tsubai #include <machine/romcall.h>
     38  1.2   tsubai #include <promdev.h>
     39  1.1   tsubai 
     40  1.1   tsubai #ifdef BOOT_DEBUG
     41  1.4  tsutsui # define DPRINTF(x) printf x
     42  1.1   tsubai #else
     43  1.4  tsutsui # define DPRINTF(x)
     44  1.1   tsubai #endif
     45  1.1   tsubai 
     46  1.1   tsubai int dkopen __P((struct open_file *, ...));
     47  1.1   tsubai int dkclose __P((struct open_file *));
     48  1.1   tsubai int dkstrategy __P((void *, int, daddr_t, size_t, void *, size_t *));
     49  1.1   tsubai 
     50  1.1   tsubai struct devsw devsw[] = {
     51  1.1   tsubai 	{ "dk", dkstrategy, dkopen, dkclose, noioctl }
     52  1.1   tsubai };
     53  1.1   tsubai int ndevs = sizeof(devsw) / sizeof(devsw[0]);
     54  1.1   tsubai 
     55  1.3  tsutsui struct fs_ops file_system_ufs = {
     56  1.3  tsutsui 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
     57  1.1   tsubai };
     58  1.3  tsutsui struct fs_ops file_system_nfs = {
     59  1.3  tsutsui 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
     60  1.2   tsubai };
     61  1.3  tsutsui #ifdef SUPPORT_USTARFS
     62  1.3  tsutsui struct fs_ops file_system_ustarfs = {
     63  1.3  tsutsui 	ustarfs_open, ustarfs_close, ustarfs_read, ustarfs_write,
     64  1.3  tsutsui 	    ustarfs_seek, ustarfs_stat
     65  1.3  tsutsui };
     66  1.3  tsutsui struct fs_ops file_system[2];
     67  1.3  tsutsui #else
     68  1.2   tsubai struct fs_ops file_system[1];
     69  1.3  tsutsui #endif
     70  1.3  tsutsui int nfsys;
     71  1.1   tsubai 
     72  1.2   tsubai struct romdev romdev;
     73  1.2   tsubai 
     74  1.2   tsubai extern int apbus;
     75  1.1   tsubai 
     76  1.1   tsubai int
     77  1.1   tsubai devopen(f, fname, file)
     78  1.1   tsubai 	struct open_file *f;
     79  1.1   tsubai 	const char *fname;
     80  1.1   tsubai 	char **file;	/* out */
     81  1.1   tsubai {
     82  1.1   tsubai 	int fd;
     83  1.1   tsubai 	char *cp;
     84  1.2   tsubai 	int error = 0;
     85  1.1   tsubai 
     86  1.4  tsutsui 	DPRINTF(("devopen: %s\n", fname));
     87  1.1   tsubai 
     88  1.3  tsutsui 	strcpy(romdev.devname, fname);
     89  1.3  tsutsui 	cp = strchr(romdev.devname, ')') + 1;
     90  1.1   tsubai 	*cp = 0;
     91  1.2   tsubai 	if (apbus)
     92  1.3  tsutsui 		fd = apcall_open(romdev.devname, 2);
     93  1.2   tsubai 	else
     94  1.3  tsutsui 		fd = rom_open(romdev.devname, 2);
     95  1.1   tsubai 
     96  1.4  tsutsui 	DPRINTF(("devname = %s, fd = %d\n", romdev.devname, fd));
     97  1.1   tsubai 	if (fd == -1)
     98  1.1   tsubai 		return -1;
     99  1.1   tsubai 
    100  1.1   tsubai 	romdev.fd = fd;
    101  1.3  tsutsui 	if (strncmp(romdev.devname, "sonic", 5) == 0)
    102  1.2   tsubai 		romdev.devtype = DT_NET;
    103  1.2   tsubai 	else
    104  1.2   tsubai 		romdev.devtype = DT_BLOCK;
    105  1.1   tsubai 
    106  1.1   tsubai 	f->f_dev = devsw;
    107  1.1   tsubai 	f->f_devdata = &romdev;
    108  1.1   tsubai 	*file = strchr(fname, ')') + 1;
    109  1.1   tsubai 
    110  1.3  tsutsui 	if (romdev.devtype == DT_BLOCK) {
    111  1.3  tsutsui 		file_system[0] = file_system_ufs;
    112  1.3  tsutsui #ifdef SUPPORT_USTARFS
    113  1.3  tsutsui 		file_system[1] = file_system_ustarfs;
    114  1.3  tsutsui 		nfsys = 2;
    115  1.3  tsutsui #else
    116  1.3  tsutsui 		nfsys = 1;
    117  1.3  tsutsui #endif
    118  1.3  tsutsui 	} else {	/* DT_NET */
    119  1.3  tsutsui 		file_system[0] = file_system_nfs;
    120  1.3  tsutsui 		nfsys = 1;
    121  1.2   tsubai 
    122  1.2   tsubai 		if ((error = net_open(&romdev)) != 0) {
    123  1.2   tsubai 			printf("Can't open NFS network connection on `%s'\n",
    124  1.3  tsutsui 			    romdev.devname);
    125  1.2   tsubai 			return error;
    126  1.2   tsubai 		}
    127  1.2   tsubai 	}
    128  1.2   tsubai 
    129  1.1   tsubai 	return 0;
    130  1.1   tsubai }
    131  1.1   tsubai 
    132  1.1   tsubai int
    133  1.1   tsubai dkopen(struct open_file *f, ...)
    134  1.1   tsubai {
    135  1.4  tsutsui 	DPRINTF(("dkopen\n"));
    136  1.1   tsubai 	return 0;
    137  1.1   tsubai }
    138  1.1   tsubai 
    139  1.1   tsubai int
    140  1.1   tsubai dkclose(f)
    141  1.1   tsubai 	struct open_file *f;
    142  1.1   tsubai {
    143  1.1   tsubai 	struct romdev *dev = f->f_devdata;
    144  1.1   tsubai 
    145  1.4  tsutsui 	DPRINTF(("dkclose\n"));
    146  1.2   tsubai 	if (apbus)
    147  1.2   tsubai 		apcall_close(dev->fd);
    148  1.2   tsubai 	else
    149  1.2   tsubai 		rom_close(dev->fd);
    150  1.2   tsubai 
    151  1.1   tsubai 	return 0;
    152  1.1   tsubai }
    153  1.1   tsubai 
    154  1.1   tsubai int
    155  1.1   tsubai dkstrategy(devdata, rw, blk, size, buf, rsize)
    156  1.1   tsubai 	void *devdata;
    157  1.1   tsubai 	int rw;
    158  1.1   tsubai 	daddr_t blk;
    159  1.1   tsubai 	size_t size;
    160  1.1   tsubai 	void *buf;
    161  1.1   tsubai 	size_t *rsize;	/* out: number of bytes transfered */
    162  1.1   tsubai {
    163  1.1   tsubai 	struct romdev *dev = devdata;
    164  1.1   tsubai 
    165  1.1   tsubai 	/* XXX should use partition offset */
    166  1.1   tsubai 
    167  1.2   tsubai 	if (apbus) {
    168  1.2   tsubai 		apcall_lseek(dev->fd, blk * 512, 0);
    169  1.2   tsubai 		apcall_read(dev->fd, buf, size);
    170  1.2   tsubai 	} else {
    171  1.2   tsubai 		rom_lseek(dev->fd, blk * 512, 0);
    172  1.2   tsubai 		rom_read(dev->fd, buf, size);
    173  1.2   tsubai 	}
    174  1.2   tsubai 	*rsize = size;		/* XXX */
    175  1.1   tsubai 	return 0;
    176  1.1   tsubai }
    177  1.3  tsutsui 
    178  1.3  tsutsui #ifdef HAVE_CHANGEDISK_HOOK
    179  1.3  tsutsui void
    180  1.3  tsutsui changedisk_hook(f)
    181  1.3  tsutsui 	struct open_file *f;
    182  1.3  tsutsui {
    183  1.3  tsutsui 	struct romdev *dev = f->f_devdata;
    184  1.3  tsutsui 
    185  1.3  tsutsui 	if (apbus) {
    186  1.3  tsutsui 		apcall_ioctl(dev->fd, APIOCEJECT, NULL);
    187  1.3  tsutsui 		apcall_close(dev->fd);
    188  1.3  tsutsui 		getchar();
    189  1.3  tsutsui 		dev->fd = apcall_open(dev->devname, 2);
    190  1.3  tsutsui 	} else {
    191  1.3  tsutsui 		rom_ioctl(dev->fd, SYSIOCEJECT, NULL);
    192  1.3  tsutsui 		rom_close(dev->fd);
    193  1.3  tsutsui 		getchar();
    194  1.3  tsutsui 		dev->fd = rom_open(dev->devname, 2);
    195  1.3  tsutsui 	}
    196  1.3  tsutsui 
    197  1.3  tsutsui }
    198  1.3  tsutsui #endif
    199