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