Home | History | Annotate | Line # | Download | only in boot
devopen.c revision 1.5.6.1
      1  1.5.6.1    skrll /*	$NetBSD: devopen.c,v 1.5.6.1 2004/08/03 10:38:35 skrll 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.5.6.1    skrll # define DPRINTF printf
     42      1.1   tsubai #else
     43  1.5.6.1    skrll # define DPRINTF while (0) printf
     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.5  tsutsui #ifdef HAVE_CHANGEDISK_HOOK
     50      1.5  tsutsui void changedisk_hook __P((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.3  tsutsui struct fs_ops file_system_ufs = {
     59      1.3  tsutsui 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
     60      1.1   tsubai };
     61      1.3  tsutsui struct fs_ops file_system_nfs = {
     62      1.3  tsutsui 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
     63      1.2   tsubai };
     64      1.3  tsutsui #ifdef SUPPORT_USTARFS
     65      1.3  tsutsui struct fs_ops file_system_ustarfs = {
     66      1.3  tsutsui 	ustarfs_open, ustarfs_close, ustarfs_read, ustarfs_write,
     67      1.3  tsutsui 	    ustarfs_seek, ustarfs_stat
     68      1.3  tsutsui };
     69      1.3  tsutsui struct fs_ops file_system[2];
     70      1.3  tsutsui #else
     71      1.2   tsubai struct fs_ops file_system[1];
     72      1.3  tsutsui #endif
     73      1.3  tsutsui int nfsys;
     74      1.1   tsubai 
     75      1.2   tsubai struct romdev romdev;
     76      1.2   tsubai 
     77      1.2   tsubai extern int apbus;
     78      1.1   tsubai 
     79      1.1   tsubai int
     80      1.1   tsubai devopen(f, fname, file)
     81      1.1   tsubai 	struct open_file *f;
     82      1.1   tsubai 	const char *fname;
     83      1.1   tsubai 	char **file;	/* out */
     84      1.1   tsubai {
     85      1.1   tsubai 	int fd;
     86      1.1   tsubai 	char *cp;
     87      1.2   tsubai 	int error = 0;
     88      1.1   tsubai 
     89  1.5.6.1    skrll 	DPRINTF("devopen: %s\n", fname);
     90      1.1   tsubai 
     91      1.3  tsutsui 	strcpy(romdev.devname, fname);
     92      1.3  tsutsui 	cp = strchr(romdev.devname, ')') + 1;
     93      1.1   tsubai 	*cp = 0;
     94      1.2   tsubai 	if (apbus)
     95      1.3  tsutsui 		fd = apcall_open(romdev.devname, 2);
     96      1.2   tsubai 	else
     97      1.3  tsutsui 		fd = rom_open(romdev.devname, 2);
     98      1.1   tsubai 
     99  1.5.6.1    skrll 	DPRINTF("devname = %s, fd = %d\n", romdev.devname, fd);
    100      1.1   tsubai 	if (fd == -1)
    101      1.1   tsubai 		return -1;
    102      1.1   tsubai 
    103      1.1   tsubai 	romdev.fd = fd;
    104      1.3  tsutsui 	if (strncmp(romdev.devname, "sonic", 5) == 0)
    105      1.2   tsubai 		romdev.devtype = DT_NET;
    106      1.2   tsubai 	else
    107      1.2   tsubai 		romdev.devtype = DT_BLOCK;
    108      1.1   tsubai 
    109      1.1   tsubai 	f->f_dev = devsw;
    110      1.1   tsubai 	f->f_devdata = &romdev;
    111      1.1   tsubai 	*file = strchr(fname, ')') + 1;
    112      1.1   tsubai 
    113      1.3  tsutsui 	if (romdev.devtype == DT_BLOCK) {
    114      1.3  tsutsui 		file_system[0] = file_system_ufs;
    115      1.3  tsutsui #ifdef SUPPORT_USTARFS
    116      1.3  tsutsui 		file_system[1] = file_system_ustarfs;
    117      1.3  tsutsui 		nfsys = 2;
    118      1.3  tsutsui #else
    119      1.3  tsutsui 		nfsys = 1;
    120      1.3  tsutsui #endif
    121      1.3  tsutsui 	} else {	/* DT_NET */
    122      1.3  tsutsui 		file_system[0] = file_system_nfs;
    123      1.3  tsutsui 		nfsys = 1;
    124      1.2   tsubai 
    125      1.2   tsubai 		if ((error = net_open(&romdev)) != 0) {
    126      1.2   tsubai 			printf("Can't open NFS network connection on `%s'\n",
    127      1.3  tsutsui 			    romdev.devname);
    128      1.2   tsubai 			return error;
    129      1.2   tsubai 		}
    130      1.2   tsubai 	}
    131      1.2   tsubai 
    132      1.1   tsubai 	return 0;
    133      1.1   tsubai }
    134      1.1   tsubai 
    135      1.1   tsubai int
    136      1.1   tsubai dkopen(struct open_file *f, ...)
    137      1.1   tsubai {
    138  1.5.6.1    skrll 	DPRINTF("dkopen\n");
    139      1.1   tsubai 	return 0;
    140      1.1   tsubai }
    141      1.1   tsubai 
    142      1.1   tsubai int
    143      1.1   tsubai dkclose(f)
    144      1.1   tsubai 	struct open_file *f;
    145      1.1   tsubai {
    146      1.1   tsubai 	struct romdev *dev = f->f_devdata;
    147      1.1   tsubai 
    148  1.5.6.1    skrll 	DPRINTF("dkclose\n");
    149      1.2   tsubai 	if (apbus)
    150      1.2   tsubai 		apcall_close(dev->fd);
    151      1.2   tsubai 	else
    152      1.2   tsubai 		rom_close(dev->fd);
    153      1.2   tsubai 
    154      1.1   tsubai 	return 0;
    155      1.1   tsubai }
    156      1.1   tsubai 
    157      1.1   tsubai int
    158      1.1   tsubai dkstrategy(devdata, rw, blk, size, buf, rsize)
    159      1.1   tsubai 	void *devdata;
    160      1.1   tsubai 	int rw;
    161      1.1   tsubai 	daddr_t blk;
    162      1.1   tsubai 	size_t size;
    163      1.1   tsubai 	void *buf;
    164      1.1   tsubai 	size_t *rsize;	/* out: number of bytes transfered */
    165      1.1   tsubai {
    166      1.1   tsubai 	struct romdev *dev = devdata;
    167      1.1   tsubai 
    168      1.1   tsubai 	/* XXX should use partition offset */
    169      1.1   tsubai 
    170      1.2   tsubai 	if (apbus) {
    171      1.2   tsubai 		apcall_lseek(dev->fd, blk * 512, 0);
    172      1.2   tsubai 		apcall_read(dev->fd, buf, size);
    173      1.2   tsubai 	} else {
    174      1.2   tsubai 		rom_lseek(dev->fd, blk * 512, 0);
    175      1.2   tsubai 		rom_read(dev->fd, buf, size);
    176      1.2   tsubai 	}
    177      1.2   tsubai 	*rsize = size;		/* XXX */
    178      1.1   tsubai 	return 0;
    179      1.1   tsubai }
    180      1.3  tsutsui 
    181      1.3  tsutsui #ifdef HAVE_CHANGEDISK_HOOK
    182      1.3  tsutsui void
    183      1.3  tsutsui changedisk_hook(f)
    184      1.3  tsutsui 	struct open_file *f;
    185      1.3  tsutsui {
    186      1.3  tsutsui 	struct romdev *dev = f->f_devdata;
    187      1.3  tsutsui 
    188      1.3  tsutsui 	if (apbus) {
    189      1.3  tsutsui 		apcall_ioctl(dev->fd, APIOCEJECT, NULL);
    190      1.3  tsutsui 		apcall_close(dev->fd);
    191      1.3  tsutsui 		getchar();
    192      1.3  tsutsui 		dev->fd = apcall_open(dev->devname, 2);
    193      1.3  tsutsui 	} else {
    194      1.3  tsutsui 		rom_ioctl(dev->fd, SYSIOCEJECT, NULL);
    195      1.3  tsutsui 		rom_close(dev->fd);
    196      1.3  tsutsui 		getchar();
    197      1.3  tsutsui 		dev->fd = rom_open(dev->devname, 2);
    198      1.3  tsutsui 	}
    199      1.3  tsutsui 
    200      1.3  tsutsui }
    201      1.3  tsutsui #endif
    202