Home | History | Annotate | Line # | Download | only in boot
wd.c revision 1.9
      1 /*	$NetBSD: wd.c,v 1.9 2008/03/02 06:17:41 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Manuel Bouyer.
      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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/types.h>
     40 #include <sys/stdint.h>
     41 
     42 #include <lib/libsa/stand.h>
     43 #include <lib/libkern/libkern.h>
     44 
     45 #include <machine/param.h>
     46 #include <machine/stdarg.h>
     47 #include <dev/raidframe/raidframevar.h>		/* For RF_PROTECTED_SECTORS */
     48 
     49 #include "boot.h"
     50 #include "wdvar.h"
     51 
     52 static int  wd_get_params(struct wd_softc *wd);
     53 static int  wdgetdisklabel(struct wd_softc *wd);
     54 static void wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp);
     55 
     56 /*
     57  * Get drive parameters through 'device identify' command.
     58  */
     59 int
     60 wd_get_params(struct wd_softc *wd)
     61 {
     62 	int error;
     63 	uint8_t buf[DEV_BSIZE];
     64 
     65 	if ((error = wdc_exec_identify(wd, buf)) != 0)
     66 		return error;
     67 
     68 	wd->sc_params = *(struct ataparams *)buf;
     69 
     70 	/* 48-bit LBA addressing */
     71 	if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0) {
     72 		DPRINTF(("Drive supports LBA48.\n"));
     73 #if defined(_ENABLE_LBA48)
     74 		wd->sc_flags |= WDF_LBA48;
     75 #endif
     76 	}
     77 
     78 	/* Prior to ATA-4, LBA was optional. */
     79 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0) {
     80 		DPRINTF(("Drive supports LBA.\n"));
     81 		wd->sc_flags |= WDF_LBA;
     82 	}
     83 
     84 	return 0;
     85 }
     86 
     87 /*
     88  * Initialize disk label to the default value.
     89  */
     90 void
     91 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
     92 {
     93 
     94 	memset(lp, 0, sizeof(struct disklabel));
     95 
     96 	lp->d_secsize = DEV_BSIZE;
     97 	lp->d_ntracks = wd->sc_params.atap_heads;
     98 	lp->d_nsectors = wd->sc_params.atap_sectors;
     99 	lp->d_ncylinders = wd->sc_params.atap_cylinders;
    100 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    101 
    102 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
    103 		lp->d_type = DTYPE_ST506;
    104 	else
    105 		lp->d_type = DTYPE_ESDI;
    106 
    107 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
    108 	strncpy(lp->d_packname, "fictitious", 16);
    109 	if (wd->sc_capacity > UINT32_MAX)
    110 		lp->d_secperunit = UINT32_MAX;
    111 	else
    112 		lp->d_secperunit = wd->sc_capacity;
    113 	lp->d_rpm = 3600;
    114 	lp->d_interleave = 1;
    115 	lp->d_flags = 0;
    116 
    117 	lp->d_partitions[RAW_PART].p_offset = 0;
    118 	lp->d_partitions[RAW_PART].p_size =
    119 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    120 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    121 	lp->d_npartitions = MAXPARTITIONS;	/* RAW_PART + 1 ??? */
    122 
    123 	lp->d_magic = DISKMAGIC;
    124 	lp->d_magic2 = DISKMAGIC;
    125 	lp->d_checksum = dkcksum(lp);
    126 }
    127 
    128 /*
    129  * Read disk label from the device.
    130  */
    131 int
    132 wdgetdisklabel(struct wd_softc *wd)
    133 {
    134 	char *msg;
    135 	int sector;
    136 	size_t rsize;
    137 	struct disklabel *lp;
    138 	uint8_t buf[DEV_BSIZE];
    139 
    140 	wdgetdefaultlabel(wd, &wd->sc_label);
    141 
    142 	/*
    143 	 * Find NetBSD Partition in DOS partition table.
    144 	 */
    145 	sector = 0;
    146 	if (wdstrategy(wd, F_READ, MBR_BBSECTOR, DEV_BSIZE, buf, &rsize))
    147 		return EOFFSET;
    148 
    149 	if (*(uint16_t *)&buf[MBR_MAGIC_OFFSET] == MBR_MAGIC) {
    150 		int i;
    151 		struct mbr_partition *mp;
    152 
    153 		/*
    154 		 * Lookup NetBSD slice. If there is none, go ahead
    155 		 * and try to read the disklabel off sector #0.
    156 		 */
    157 		mp = (struct mbr_partition *)&buf[MBR_PART_OFFSET];
    158 		for (i = 0; i < MBR_PART_COUNT; i++) {
    159 			if (mp[i].mbrp_type == MBR_PTYPE_NETBSD) {
    160 				sector = mp[i].mbrp_start;
    161 				break;
    162 			}
    163 		}
    164 	}
    165 
    166 	if (wdstrategy(wd, F_READ, sector + LABELSECTOR, DEV_BSIZE,
    167 	    buf, &rsize))
    168 		return EOFFSET;
    169 
    170 	if ((msg = getdisklabel(buf + LABELOFFSET, &wd->sc_label)))
    171 		printf("wd%d: getdisklabel: %s\n", wd->sc_unit, msg);
    172 
    173 	lp = &wd->sc_label;
    174 
    175 	/* check partition */
    176 	if ((wd->sc_part >= lp->d_npartitions) ||
    177 	    (lp->d_partitions[wd->sc_part].p_fstype == FS_UNUSED)) {
    178 		DPRINTF(("illegal partition\n"));
    179 		return EPART;
    180 	}
    181 
    182 	DPRINTF(("label info: d_secsize %d, d_nsectors %d, d_ncylinders %d,"
    183 	    "d_ntracks %d, d_secpercyl %d\n",
    184 	    wd->sc_label.d_secsize,
    185 	    wd->sc_label.d_nsectors,
    186 	    wd->sc_label.d_ncylinders,
    187 	    wd->sc_label.d_ntracks,
    188 	    wd->sc_label.d_secpercyl));
    189 
    190 	return 0;
    191 }
    192 
    193 /*
    194  * Open device (read drive parameters and disklabel)
    195  */
    196 int
    197 wdopen(struct open_file *f, ...)
    198 {
    199 	int error;
    200 	va_list ap;
    201 	u_int unit, part;
    202 	struct wd_softc *wd;
    203 
    204 	va_start(ap, f);
    205 	unit = va_arg(ap, u_int);
    206 	part = va_arg(ap, u_int);
    207 	va_end(ap);
    208 
    209 	DPRINTF(("wdopen: %d:%d\n", unit, part));
    210 
    211 	wd = alloc(sizeof(struct wd_softc));
    212 	if (wd == NULL)
    213 		return ENOMEM;
    214 
    215 	memset(wd, 0, sizeof(struct wd_softc));
    216 
    217 	if (wdc_init(wd, &unit) != 0)
    218 		return (ENXIO);
    219 
    220 	wd->sc_part = part;
    221 	wd->sc_unit = unit;
    222 
    223 	if ((error = wd_get_params(wd)) != 0)
    224 		return error;
    225 
    226 	if ((error = wdgetdisklabel(wd)) != 0)
    227 		return error;
    228 
    229 	f->f_devdata = wd;
    230 	return 0;
    231 }
    232 
    233 /*
    234  * Close device.
    235  */
    236 int
    237 wdclose(struct open_file *f)
    238 {
    239 
    240 	return 0;
    241 }
    242 
    243 /*
    244  * Read some data.
    245  */
    246 int
    247 wdstrategy(void *f, int rw, daddr_t dblk, size_t size, void *p, size_t *rsize)
    248 {
    249 	int i, nsect;
    250 	daddr_t blkno;
    251 	struct wd_softc *wd;
    252 	struct partition *pp;
    253 	uint8_t *buf;
    254 
    255 	if (size == 0)
    256 		return 0;
    257 
    258 	if (rw != F_READ)
    259 		return EOPNOTSUPP;
    260 
    261 	buf = p;
    262 	wd = f;
    263 	pp = &wd->sc_label.d_partitions[wd->sc_part];
    264 
    265 	nsect = howmany(size, wd->sc_label.d_secsize);
    266 	blkno = dblk + pp->p_offset;
    267 	if (pp->p_fstype == FS_RAID)
    268 		blkno += RF_PROTECTED_SECTORS;
    269 
    270 	for (i = 0; i < nsect; i++, blkno++) {
    271 		int error;
    272 
    273 		if ((error = wdc_exec_read(wd, WDCC_READ, blkno, buf)) != 0)
    274 			return error;
    275 
    276 		buf += wd->sc_label.d_secsize;
    277 	}
    278 
    279 	*rsize = size;
    280 	return 0;
    281 }
    282