Home | History | Annotate | Line # | Download | only in boot
devopen.c revision 1.8
      1  1.8  junyoung /*	$NetBSD: devopen.c,v 1.8 2005/06/23 19:44:01 junyoung 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.6   tsutsui # define DPRINTF printf
     42  1.1    tsubai #else
     43  1.6   tsutsui # define DPRINTF while (0) printf
     44  1.1    tsubai #endif
     45  1.1    tsubai 
     46  1.7   tsutsui int dkopen(struct open_file *, ...);
     47  1.7   tsutsui int dkclose(struct open_file *);
     48  1.7   tsutsui int dkstrategy(void *, int, daddr_t, size_t, void *, size_t *);
     49  1.5   tsutsui #ifdef HAVE_CHANGEDISK_HOOK
     50  1.7   tsutsui void changedisk_hook(struct open_file *);
     51  1.5   tsutsui #endif
     52  1.1    tsubai 
     53  1.1    tsubai struct devsw devsw[] = {
     54  1.1    tsubai 	{ "dk", dkstrategy, dkopen, dkclose, noioctl }
     55  1.1    tsubai };
     56  1.1    tsubai int ndevs = sizeof(devsw) / sizeof(devsw[0]);
     57  1.1    tsubai 
     58  1.8  junyoung struct fs_ops file_system_ufs = FS_OPS(ufs);
     59  1.8  junyoung struct fs_ops file_system_nfs = FS_OPS(nfs);
     60  1.3   tsutsui #ifdef SUPPORT_USTARFS
     61  1.8  junyoung struct fs_ops file_system_ustarfs = FS_OPS(ustarfs);
     62  1.3   tsutsui struct fs_ops file_system[2];
     63  1.3   tsutsui #else
     64  1.2    tsubai struct fs_ops file_system[1];
     65  1.3   tsutsui #endif
     66  1.3   tsutsui int nfsys;
     67  1.1    tsubai 
     68  1.2    tsubai struct romdev romdev;
     69  1.2    tsubai 
     70  1.2    tsubai extern int apbus;
     71  1.1    tsubai 
     72  1.1    tsubai int
     73  1.7   tsutsui devopen(struct open_file *f, const char *fname, char **file)
     74  1.1    tsubai {
     75  1.1    tsubai 	int fd;
     76  1.1    tsubai 	char *cp;
     77  1.2    tsubai 	int error = 0;
     78  1.1    tsubai 
     79  1.6   tsutsui 	DPRINTF("devopen: %s\n", fname);
     80  1.1    tsubai 
     81  1.3   tsutsui 	strcpy(romdev.devname, fname);
     82  1.3   tsutsui 	cp = strchr(romdev.devname, ')') + 1;
     83  1.1    tsubai 	*cp = 0;
     84  1.2    tsubai 	if (apbus)
     85  1.3   tsutsui 		fd = apcall_open(romdev.devname, 2);
     86  1.2    tsubai 	else
     87  1.3   tsutsui 		fd = rom_open(romdev.devname, 2);
     88  1.1    tsubai 
     89  1.6   tsutsui 	DPRINTF("devname = %s, fd = %d\n", romdev.devname, fd);
     90  1.1    tsubai 	if (fd == -1)
     91  1.1    tsubai 		return -1;
     92  1.1    tsubai 
     93  1.1    tsubai 	romdev.fd = fd;
     94  1.3   tsutsui 	if (strncmp(romdev.devname, "sonic", 5) == 0)
     95  1.2    tsubai 		romdev.devtype = DT_NET;
     96  1.2    tsubai 	else
     97  1.2    tsubai 		romdev.devtype = DT_BLOCK;
     98  1.1    tsubai 
     99  1.1    tsubai 	f->f_dev = devsw;
    100  1.1    tsubai 	f->f_devdata = &romdev;
    101  1.1    tsubai 	*file = strchr(fname, ')') + 1;
    102  1.1    tsubai 
    103  1.3   tsutsui 	if (romdev.devtype == DT_BLOCK) {
    104  1.3   tsutsui 		file_system[0] = file_system_ufs;
    105  1.3   tsutsui #ifdef SUPPORT_USTARFS
    106  1.3   tsutsui 		file_system[1] = file_system_ustarfs;
    107  1.3   tsutsui 		nfsys = 2;
    108  1.3   tsutsui #else
    109  1.3   tsutsui 		nfsys = 1;
    110  1.3   tsutsui #endif
    111  1.3   tsutsui 	} else {	/* DT_NET */
    112  1.3   tsutsui 		file_system[0] = file_system_nfs;
    113  1.3   tsutsui 		nfsys = 1;
    114  1.2    tsubai 
    115  1.2    tsubai 		if ((error = net_open(&romdev)) != 0) {
    116  1.2    tsubai 			printf("Can't open NFS network connection on `%s'\n",
    117  1.3   tsutsui 			    romdev.devname);
    118  1.2    tsubai 			return error;
    119  1.2    tsubai 		}
    120  1.2    tsubai 	}
    121  1.2    tsubai 
    122  1.1    tsubai 	return 0;
    123  1.1    tsubai }
    124  1.1    tsubai 
    125  1.1    tsubai int
    126  1.1    tsubai dkopen(struct open_file *f, ...)
    127  1.1    tsubai {
    128  1.7   tsutsui 
    129  1.6   tsutsui 	DPRINTF("dkopen\n");
    130  1.1    tsubai 	return 0;
    131  1.1    tsubai }
    132  1.1    tsubai 
    133  1.1    tsubai int
    134  1.7   tsutsui dkclose(struct open_file *f)
    135  1.1    tsubai {
    136  1.1    tsubai 	struct romdev *dev = f->f_devdata;
    137  1.1    tsubai 
    138  1.6   tsutsui 	DPRINTF("dkclose\n");
    139  1.2    tsubai 	if (apbus)
    140  1.2    tsubai 		apcall_close(dev->fd);
    141  1.2    tsubai 	else
    142  1.2    tsubai 		rom_close(dev->fd);
    143  1.2    tsubai 
    144  1.1    tsubai 	return 0;
    145  1.1    tsubai }
    146  1.1    tsubai 
    147  1.1    tsubai int
    148  1.7   tsutsui dkstrategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
    149  1.7   tsutsui     size_t *rsize)
    150  1.1    tsubai {
    151  1.1    tsubai 	struct romdev *dev = devdata;
    152  1.1    tsubai 
    153  1.1    tsubai 	/* XXX should use partition offset */
    154  1.1    tsubai 
    155  1.2    tsubai 	if (apbus) {
    156  1.2    tsubai 		apcall_lseek(dev->fd, blk * 512, 0);
    157  1.2    tsubai 		apcall_read(dev->fd, buf, size);
    158  1.2    tsubai 	} else {
    159  1.2    tsubai 		rom_lseek(dev->fd, blk * 512, 0);
    160  1.2    tsubai 		rom_read(dev->fd, buf, size);
    161  1.2    tsubai 	}
    162  1.2    tsubai 	*rsize = size;		/* XXX */
    163  1.1    tsubai 	return 0;
    164  1.1    tsubai }
    165  1.3   tsutsui 
    166  1.3   tsutsui #ifdef HAVE_CHANGEDISK_HOOK
    167  1.3   tsutsui void
    168  1.7   tsutsui changedisk_hook(struct open_file *f)
    169  1.3   tsutsui {
    170  1.3   tsutsui 	struct romdev *dev = f->f_devdata;
    171  1.3   tsutsui 
    172  1.3   tsutsui 	if (apbus) {
    173  1.3   tsutsui 		apcall_ioctl(dev->fd, APIOCEJECT, NULL);
    174  1.3   tsutsui 		apcall_close(dev->fd);
    175  1.3   tsutsui 		getchar();
    176  1.3   tsutsui 		dev->fd = apcall_open(dev->devname, 2);
    177  1.3   tsutsui 	} else {
    178  1.3   tsutsui 		rom_ioctl(dev->fd, SYSIOCEJECT, NULL);
    179  1.3   tsutsui 		rom_close(dev->fd);
    180  1.3   tsutsui 		getchar();
    181  1.3   tsutsui 		dev->fd = rom_open(dev->devname, 2);
    182  1.3   tsutsui 	}
    183  1.3   tsutsui }
    184  1.3   tsutsui #endif
    185