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