Home | History | Annotate | Line # | Download | only in libsa
diskio.c revision 1.2.56.1
      1  1.2.56.1  skrll /*	$NetBSD: diskio.c,v 1.2.56.1 2005/11/10 13:55:33 skrll Exp $	*/
      2       1.1    leo 
      3       1.1    leo /*
      4       1.1    leo  * Copyright (c) 1995 Waldi Ravens.
      5       1.1    leo  * All rights reserved.
      6       1.1    leo  *
      7       1.1    leo  * Redistribution and use in source and binary forms, with or without
      8       1.1    leo  * modification, are permitted provided that the following conditions
      9       1.1    leo  * are met:
     10       1.1    leo  * 1. Redistributions of source code must retain the above copyright
     11       1.1    leo  *    notice, this list of conditions and the following disclaimer.
     12       1.1    leo  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1    leo  *    notice, this list of conditions and the following disclaimer in the
     14       1.1    leo  *    documentation and/or other materials provided with the distribution.
     15       1.1    leo  * 3. All advertising materials mentioning features or use of this software
     16       1.1    leo  *    must display the following acknowledgement:
     17       1.1    leo  *        This product includes software developed by Waldi Ravens.
     18       1.1    leo  * 4. The name of the author may not be used to endorse or promote products
     19       1.1    leo  *    derived from this software without specific prior written permission
     20       1.1    leo  *
     21       1.1    leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22       1.1    leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23       1.1    leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24       1.1    leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25       1.1    leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26       1.1    leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27       1.1    leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28       1.1    leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29       1.1    leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30       1.1    leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31       1.1    leo  */
     32       1.1    leo 
     33  1.2.56.1  skrll #include <lib/libsa/stand.h>
     34       1.2    leo #include "atari_stand.h"
     35       1.1    leo #include <sys/disklabel.h>
     36       1.1    leo 
     37       1.1    leo typedef int (*rdsec_f)__P((void *buffer, u_int offset, u_int count));
     38       1.1    leo typedef	struct { rdsec_f rds; u_int rst; u_int rend; } bdevd_t;
     39       1.1    leo 
     40       1.1    leo static int rootstrategy __P((void *, int, daddr_t, size_t, void *, size_t *));
     41       1.1    leo static int rootopen __P((struct open_file *, ...));
     42       1.1    leo static int rootclose __P((struct open_file *));
     43       1.1    leo static int rootioctl __P((struct open_file *, u_long, void *));
     44       1.1    leo 
     45       1.1    leo static struct devsw devsw[] = {
     46       1.1    leo 	{ "root", rootstrategy, rootopen, rootclose, rootioctl }
     47       1.1    leo };
     48       1.1    leo static bdevd_t	bootdev;
     49       1.1    leo 
     50       1.1    leo /*
     51       1.1    leo  * Initialise boot device info.
     52       1.1    leo  */
     53       1.1    leo int
     54       1.1    leo init_dskio (func, label, root)
     55       1.1    leo 	void	*func, *label;
     56       1.1    leo 	int	root;
     57       1.1    leo {
     58       1.1    leo 	struct disklabel *dl = label;
     59       1.1    leo 	struct partition *pd = &dl->d_partitions[root];
     60       1.1    leo 
     61       1.1    leo 	if (dl->d_magic != DISKMAGIC || dl->d_magic2 != DISKMAGIC
     62       1.1    leo 	    || dl->d_npartitions > MAXPARTITIONS || dkcksum(dl) != 0) {
     63       1.1    leo 		printf("Invalid disk label.\n");
     64       1.1    leo 		return(-1);
     65       1.1    leo 	}
     66       1.1    leo 	if (root >= 0) {
     67       1.1    leo 		if (root >= dl->d_npartitions
     68       1.1    leo 		    || pd->p_fstype != FS_BSDFFS || pd->p_size == 0) {
     69       1.1    leo 			printf("No suitable root.\n");
     70       1.1    leo 			return(-1);
     71       1.1    leo 		}
     72       1.1    leo 		bootdev.rds  = func;
     73       1.1    leo 		bootdev.rst  = pd->p_offset;
     74       1.1    leo 		bootdev.rend = pd->p_offset + pd->p_size - 1;
     75       1.1    leo 	}
     76       1.1    leo 	return(0);
     77       1.1    leo }
     78       1.1    leo 
     79       1.1    leo /*
     80       1.1    leo  * No choice, the kernel is loaded from the
     81       1.1    leo  * same device as the bootstrap.
     82       1.1    leo  */
     83       1.1    leo int
     84       1.1    leo devopen (f, fname, file)
     85       1.1    leo 	struct open_file *f;
     86       1.1    leo 	const char *fname;
     87       1.1    leo 	char **file;
     88       1.1    leo {
     89       1.1    leo 	f->f_devdata = &bootdev;
     90       1.1    leo 	f->f_dev = &devsw[0];
     91       1.1    leo 	*file = (char *)fname;
     92       1.1    leo 	return(0);
     93       1.1    leo }
     94       1.1    leo 
     95       1.1    leo static int
     96       1.1    leo rootstrategy (devd, flag, dblk, size, buf, rsize)
     97       1.1    leo 	void	*devd;
     98       1.1    leo 	int	flag;
     99       1.1    leo 	daddr_t	dblk;
    100       1.1    leo 	size_t	size;
    101       1.1    leo 	void	*buf;
    102       1.1    leo 	size_t	*rsize;
    103       1.1    leo {
    104       1.1    leo 	bdevd_t	*dd = devd;
    105       1.1    leo 	daddr_t stb = dd->rst + dblk;
    106       1.1    leo 	size_t	nb  = size >> 9;
    107       1.1    leo 
    108       1.1    leo 	if ((flag == F_READ) && !(size & 511) && (stb + nb <= dd->rend)) {
    109       1.1    leo 		if (!dd->rds(buf, stb, nb)) {
    110       1.1    leo 			*rsize = size;
    111       1.1    leo 			return(0);
    112       1.1    leo 		}
    113       1.1    leo 	}
    114       1.1    leo 	*rsize = 0;
    115       1.1    leo 	return(EIO);
    116       1.1    leo }
    117       1.1    leo 
    118       1.1    leo static int
    119       1.1    leo rootopen (f)
    120       1.1    leo 	struct open_file *f;
    121       1.1    leo {
    122       1.1    leo 	return(0);
    123       1.1    leo }
    124       1.1    leo 
    125       1.1    leo static int
    126       1.1    leo rootclose (f)
    127       1.1    leo 	struct open_file *f;
    128       1.1    leo {
    129       1.1    leo 	return(EIO);
    130       1.1    leo }
    131       1.1    leo 
    132       1.1    leo static int
    133       1.1    leo rootioctl (f, cmd, data)
    134       1.1    leo 	struct open_file *f;
    135       1.1    leo 	u_long	cmd;
    136       1.1    leo 	void	*data;
    137       1.1    leo {
    138       1.1    leo 	return(EIO);
    139       1.1    leo }
    140