Home | History | Annotate | Line # | Download | only in boot
devopen.c revision 1.1.2.1
      1  1.1.2.1  bouyer /*	$NetBSD: devopen.c,v 1.1.2.1 2000/11/20 20:17:33 bouyer 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.1.2.1  bouyer #include <netinet/in.h>
     33  1.1.2.1  bouyer #include <lib/libsa/nfs.h>
     34      1.1  tsubai 
     35  1.1.2.1  bouyer #include <machine/apcall.h>
     36      1.1  tsubai #include <machine/romcall.h>
     37  1.1.2.1  bouyer #include <promdev.h>
     38      1.1  tsubai 
     39      1.1  tsubai #ifdef BOOT_DEBUG
     40      1.1  tsubai # define DPRINTF printf
     41      1.1  tsubai #else
     42      1.1  tsubai # define DPRINTF while (0) printf
     43      1.1  tsubai #endif
     44      1.1  tsubai 
     45      1.1  tsubai int dkopen __P((struct open_file *, ...));
     46      1.1  tsubai int dkclose __P((struct open_file *));
     47      1.1  tsubai int dkstrategy __P((void *, int, daddr_t, size_t, void *, size_t *));
     48      1.1  tsubai 
     49      1.1  tsubai struct devsw devsw[] = {
     50      1.1  tsubai 	{ "dk", dkstrategy, dkopen, dkclose, noioctl }
     51      1.1  tsubai };
     52      1.1  tsubai int ndevs = sizeof(devsw) / sizeof(devsw[0]);
     53      1.1  tsubai 
     54  1.1.2.1  bouyer struct fs_ops file_system_ufs[] = {
     55      1.1  tsubai 	{ ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat }
     56      1.1  tsubai };
     57  1.1.2.1  bouyer struct fs_ops file_system_nfs[] = {
     58  1.1.2.1  bouyer 	{ nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat },
     59  1.1.2.1  bouyer };
     60  1.1.2.1  bouyer struct fs_ops file_system[1];
     61      1.1  tsubai int nfsys = sizeof(file_system) / sizeof(file_system[0]);
     62      1.1  tsubai 
     63  1.1.2.1  bouyer struct romdev romdev;
     64  1.1.2.1  bouyer 
     65  1.1.2.1  bouyer extern int apbus;
     66      1.1  tsubai 
     67      1.1  tsubai int
     68      1.1  tsubai devopen(f, fname, file)
     69      1.1  tsubai 	struct open_file *f;
     70      1.1  tsubai 	const char *fname;
     71      1.1  tsubai 	char **file;	/* out */
     72      1.1  tsubai {
     73      1.1  tsubai 	int fd;
     74      1.1  tsubai 	char devname[32];
     75      1.1  tsubai 	char *cp;
     76  1.1.2.1  bouyer 	int error = 0;
     77      1.1  tsubai 
     78      1.1  tsubai 	DPRINTF("devopen: %s\n", fname);
     79      1.1  tsubai 
     80      1.1  tsubai 	strcpy(devname, fname);
     81      1.1  tsubai 	cp = strchr(devname, ')') + 1;
     82      1.1  tsubai 	*cp = 0;
     83  1.1.2.1  bouyer 	if (apbus)
     84  1.1.2.1  bouyer 		fd = apcall_open(devname, 2);
     85  1.1.2.1  bouyer 	else
     86  1.1.2.1  bouyer 		fd = rom_open(devname, 2);
     87      1.1  tsubai 
     88      1.1  tsubai 	DPRINTF("devname = %s, fd = %d\n", devname, fd);
     89      1.1  tsubai 	if (fd == -1)
     90      1.1  tsubai 		return -1;
     91      1.1  tsubai 
     92      1.1  tsubai 	romdev.fd = fd;
     93  1.1.2.1  bouyer 	if (strncmp(devname, "sonic", 5) == 0)
     94  1.1.2.1  bouyer 		romdev.devtype = DT_NET;
     95  1.1.2.1  bouyer 	else
     96  1.1.2.1  bouyer 		romdev.devtype = DT_BLOCK;
     97      1.1  tsubai 
     98      1.1  tsubai 	f->f_dev = devsw;
     99      1.1  tsubai 	f->f_devdata = &romdev;
    100      1.1  tsubai 	*file = strchr(fname, ')') + 1;
    101      1.1  tsubai 
    102  1.1.2.1  bouyer 	if (romdev.devtype == DT_BLOCK)
    103  1.1.2.1  bouyer 		bcopy(file_system_ufs, file_system, sizeof(file_system));
    104  1.1.2.1  bouyer 	else {	/* DT_NET */
    105  1.1.2.1  bouyer 		bcopy(file_system_nfs, file_system, sizeof(file_system));
    106  1.1.2.1  bouyer 
    107  1.1.2.1  bouyer 		if ((error = net_open(&romdev)) != 0) {
    108  1.1.2.1  bouyer 			printf("Can't open NFS network connection on `%s'\n",
    109  1.1.2.1  bouyer 			       devname);
    110  1.1.2.1  bouyer 			return error;
    111  1.1.2.1  bouyer 		}
    112  1.1.2.1  bouyer 	}
    113  1.1.2.1  bouyer 
    114      1.1  tsubai 	return 0;
    115      1.1  tsubai }
    116      1.1  tsubai 
    117      1.1  tsubai int
    118      1.1  tsubai dkopen(struct open_file *f, ...)
    119      1.1  tsubai {
    120      1.1  tsubai 	DPRINTF("dkopen\n");
    121      1.1  tsubai 	return 0;
    122      1.1  tsubai }
    123      1.1  tsubai 
    124      1.1  tsubai int
    125      1.1  tsubai dkclose(f)
    126      1.1  tsubai 	struct open_file *f;
    127      1.1  tsubai {
    128      1.1  tsubai 	struct romdev *dev = f->f_devdata;
    129      1.1  tsubai 
    130      1.1  tsubai 	DPRINTF("dkclose\n");
    131  1.1.2.1  bouyer 	if (apbus)
    132  1.1.2.1  bouyer 		apcall_close(dev->fd);
    133  1.1.2.1  bouyer 	else
    134  1.1.2.1  bouyer 		rom_close(dev->fd);
    135  1.1.2.1  bouyer 
    136      1.1  tsubai 	return 0;
    137      1.1  tsubai }
    138      1.1  tsubai 
    139      1.1  tsubai int
    140      1.1  tsubai dkstrategy(devdata, rw, blk, size, buf, rsize)
    141      1.1  tsubai 	void *devdata;
    142      1.1  tsubai 	int rw;
    143      1.1  tsubai 	daddr_t blk;
    144      1.1  tsubai 	size_t size;
    145      1.1  tsubai 	void *buf;
    146      1.1  tsubai 	size_t *rsize;	/* out: number of bytes transfered */
    147      1.1  tsubai {
    148      1.1  tsubai 	struct romdev *dev = devdata;
    149      1.1  tsubai 
    150      1.1  tsubai 	/* XXX should use partition offset */
    151      1.1  tsubai 
    152  1.1.2.1  bouyer 	if (apbus) {
    153  1.1.2.1  bouyer 		apcall_lseek(dev->fd, blk * 512, 0);
    154  1.1.2.1  bouyer 		apcall_read(dev->fd, buf, size);
    155  1.1.2.1  bouyer 	} else {
    156  1.1.2.1  bouyer 		rom_lseek(dev->fd, blk * 512, 0);
    157  1.1.2.1  bouyer 		rom_read(dev->fd, buf, size);
    158  1.1.2.1  bouyer 	}
    159  1.1.2.1  bouyer 	*rsize = size;		/* XXX */
    160      1.1  tsubai 	return 0;
    161      1.1  tsubai }
    162