Home | History | Annotate | Line # | Download | only in common
disk.c revision 1.3
      1 /*	$NetBSD: disk.c,v 1.3 2003/08/07 16:29:27 agc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)disk.c	8.1 (Berkeley) 6/10/93
     35  */
     36 
     37 #include <lib/libsa/stand.h>
     38 #include <machine/stdarg.h>
     39 
     40 #include <sys/param.h>
     41 #include <sys/disklabel.h>
     42 
     43 #include <dev/arcbios/arcbios.h>
     44 
     45 #include "common.h"
     46 #include "disk.h"
     47 
     48 #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
     49 
     50 extern const struct arcbios_fv *ARCBIOS;
     51 
     52 struct	disk_softc {
     53 	int	sc_fd;			/* PROM file id */
     54 	int	sc_part;		/* disk partition number */
     55 	struct	disklabel sc_label;	/* disk label for this disk */
     56 };
     57 
     58 int
     59 diskstrategy(devdata, rw, bn, reqcnt, addr, cnt)
     60 	void *devdata;
     61 	int rw;
     62 	daddr_t bn;
     63 	size_t reqcnt;
     64 	void *addr;
     65 	size_t *cnt;	/* out: number of bytes transfered */
     66 {
     67 	struct disk_softc *sc = (struct disk_softc *)devdata;
     68 	int part = sc->sc_part;
     69 	struct partition *pp = &sc->sc_label.d_partitions[part];
     70 	int s;
     71 	int64_t offset;
     72 	int count;
     73 
     74 	offset = bn;
     75 
     76 	/*
     77 	 * Partial-block transfers not handled.
     78 	 */
     79 	if (reqcnt & (DEV_BSIZE - 1)) {
     80 		*cnt = 0;
     81 		return (EINVAL);
     82 	}
     83 
     84 	offset += pp->p_offset;
     85 
     86 	if (pp->p_fstype == FS_RAID)
     87 		offset += RF_PROTECTED_SECTORS;
     88 
     89 	/*
     90 	 * Convert from blocks to bytes.
     91 	 */
     92 	offset *= DEV_BSIZE;
     93 
     94 	s = ARCBIOS->Seek(sc->sc_fd, &offset, 0);
     95 	s = ARCBIOS->Read(sc->sc_fd, addr, reqcnt, &count);
     96 
     97 	if (s < 0)
     98 		return (EIO);
     99 
    100 	*cnt = count;
    101 	return (0);
    102 }
    103 
    104 int
    105 diskopen(struct open_file *f, ...)
    106 {
    107 	int ctlr, unit, part;
    108 
    109 	struct disk_softc *sc;
    110 	struct disklabel *lp;
    111 #ifdef arc
    112 	char *msg, buf[DEV_BSIZE];
    113 #endif
    114 	int i, cnt;
    115 	char *device;
    116 	va_list ap;
    117 
    118 	va_start(ap, f);
    119 
    120 	device = va_arg(ap, char *);
    121 
    122 	/*
    123 	 * For NetBSD/sgimips, since we use the SGI partition map directly,
    124 	 * we fake an in-core NetBSD disklabel with offset of 0.
    125 	 *
    126 	 * For NetBSD/arc, there is a MBR partition map on the disk, which we
    127 	 * then expect to find a NetBSD disklabel within the MBR partition.
    128 	 * We require that the kernel be located in first partition in the
    129 	 * NetBSD disklabel, because we have not other way to represent the
    130 	 * root partition.
    131 	 */
    132 	part = 0;
    133 
    134 	if (part >= 16)
    135 		return (ENXIO);
    136 
    137 	if (ARCBIOS->Open(device, 0, &i)) {
    138 		printf("open failed\n");
    139 		return (ENXIO);
    140 	}
    141 
    142 	sc = alloc(sizeof(struct disk_softc));
    143 	memset(sc, 0, sizeof(struct disk_softc));
    144 	f->f_devdata = (void *)sc;
    145 
    146 	sc->sc_fd = i;
    147 	sc->sc_part = part;
    148 
    149 	/* try to read disk label and partition table information */
    150 	lp = &sc->sc_label;
    151 	lp->d_secsize = DEV_BSIZE;
    152 	lp->d_secpercyl = 1;
    153 	lp->d_npartitions = MAXPARTITIONS;
    154 	lp->d_partitions[part].p_offset = 0;
    155 	lp->d_partitions[part].p_size = 0x7fffffff;
    156 
    157 #ifdef arc
    158 	i = diskstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE,
    159 	    buf, &cnt);
    160 	if (i || cnt != DEV_BSIZE) {
    161 		printf("%s: error reading disk label\n", device);
    162 		free(sc, sizeof(struct disk_softc));
    163 		return (ENXIO);
    164 	}
    165 	msg = getdisklabel(buf, lp);
    166 	if (msg) {
    167 		/* If no label, just assume 0 and return */
    168 		return (0);
    169 	}
    170 #endif
    171 
    172 	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
    173 		free(sc, sizeof(struct disk_softc));
    174 		return (ENXIO);
    175 	}
    176 	return (0);
    177 }
    178 
    179 #ifndef LIBSA_NO_DEV_CLOSE
    180 int
    181 diskclose(f)
    182 	struct open_file *f;
    183 {
    184 	ARCBIOS->Close(((struct disk_softc *)(f->f_devdata))->sc_fd);
    185 	free(f->f_devdata, sizeof(struct disk_softc));
    186 	f->f_devdata = (void *)0;
    187 	return (0);
    188 }
    189 #endif
    190