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