Home | History | Annotate | Line # | Download | only in boot
      1  1.4  mrg /*	$NetBSD: biosdisk.c,v 1.4 2021/05/17 19:31:38 mrg Exp $	*/
      2  1.1  uwe 
      3  1.1  uwe /*
      4  1.1  uwe  * Copyright (c) 1996, 1998
      5  1.1  uwe  *	Matthias Drochner.  All rights reserved.
      6  1.1  uwe  *
      7  1.1  uwe  * Redistribution and use in source and binary forms, with or without
      8  1.1  uwe  * modification, are permitted provided that the following conditions
      9  1.1  uwe  * are met:
     10  1.1  uwe  * 1. Redistributions of source code must retain the above copyright
     11  1.1  uwe  *    notice, this list of conditions and the following disclaimer.
     12  1.1  uwe  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  uwe  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  uwe  *    documentation and/or other materials provided with the distribution.
     15  1.1  uwe  *
     16  1.1  uwe  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  uwe  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  uwe  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  uwe  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  uwe  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  uwe  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  uwe  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  uwe  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  uwe  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  uwe  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  uwe  *
     27  1.1  uwe  */
     28  1.1  uwe 
     29  1.1  uwe /*
     30  1.1  uwe  * raw BIOS disk device for libsa.
     31  1.1  uwe  * needs lowlevel parts from bios_disk.S and biosdisk_ll.c
     32  1.1  uwe  * partly from netbsd:sys/arch/i386/boot/disk.c
     33  1.1  uwe  * no bad144 handling!
     34  1.1  uwe  *
     35  1.1  uwe  * A lot of this must match sys/kern/subr_disk_mbr.c
     36  1.1  uwe  */
     37  1.1  uwe 
     38  1.1  uwe /*
     39  1.1  uwe  * Ported to boot 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     40  1.1  uwe  *
     41  1.1  uwe  * Mach Operating System
     42  1.1  uwe  * Copyright (c) 1992, 1991 Carnegie Mellon University
     43  1.1  uwe  * All Rights Reserved.
     44  1.1  uwe  *
     45  1.1  uwe  * Permission to use, copy, modify and distribute this software and its
     46  1.1  uwe  * documentation is hereby granted, provided that both the copyright
     47  1.1  uwe  * notice and this permission notice appear in all copies of the
     48  1.1  uwe  * software, derivative works or modified versions, and any portions
     49  1.1  uwe  * thereof, and that both notices appear in supporting documentation.
     50  1.1  uwe  *
     51  1.1  uwe  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     52  1.1  uwe  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     53  1.1  uwe  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     54  1.1  uwe  *
     55  1.1  uwe  * Carnegie Mellon requests users of this software to return to
     56  1.1  uwe  *
     57  1.1  uwe  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     58  1.1  uwe  *  School of Computer Science
     59  1.1  uwe  *  Carnegie Mellon University
     60  1.1  uwe  *  Pittsburgh PA 15213-3890
     61  1.1  uwe  *
     62  1.1  uwe  * any improvements or extensions that they make and grant Carnegie Mellon
     63  1.1  uwe  * the rights to redistribute these changes.
     64  1.1  uwe  */
     65  1.1  uwe 
     66  1.1  uwe #include <sys/types.h>
     67  1.1  uwe #include <sys/disklabel.h>
     68  1.1  uwe 
     69  1.4  mrg #include <lib/libkern/libkern.h>
     70  1.1  uwe #include <lib/libsa/stand.h>
     71  1.1  uwe #include <lib/libsa/saerrno.h>
     72  1.1  uwe #include <lib/libsa/loadfile.h>
     73  1.1  uwe 
     74  1.1  uwe #include <machine/disklabel.h>
     75  1.1  uwe 
     76  1.1  uwe #include "biosdisk.h"
     77  1.1  uwe #include "biosdisk_ll.h"
     78  1.1  uwe 
     79  1.1  uwe #include "bootinfo.h"
     80  1.1  uwe 
     81  1.1  uwe #define BUFSIZE (1 * BIOSDISK_SECSIZE)
     82  1.1  uwe 
     83  1.1  uwe struct biosdisk {
     84  1.1  uwe 	int	dev;
     85  1.1  uwe 	int	boff;
     86  1.1  uwe 	char	buf[BUFSIZE];
     87  1.1  uwe };
     88  1.1  uwe 
     89  1.1  uwe static struct btinfo_bootdisk bi_disk;
     90  1.1  uwe 
     91  1.1  uwe #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
     92  1.1  uwe 
     93  1.1  uwe static struct biosdisk *
     94  1.1  uwe alloc_biosdisk(int dev)
     95  1.1  uwe {
     96  1.1  uwe 	struct biosdisk *d;
     97  1.1  uwe 
     98  1.1  uwe 	d = (struct biosdisk *)ALLOC(sizeof(*d));
     99  1.1  uwe 	if (d == NULL)
    100  1.1  uwe 		return (NULL);
    101  1.1  uwe 
    102  1.1  uwe 	memset(d, 0, sizeof(*d));
    103  1.1  uwe 	d->dev = dev;
    104  1.1  uwe 
    105  1.1  uwe 	return (d);
    106  1.1  uwe }
    107  1.1  uwe 
    108  1.1  uwe static int
    109  1.1  uwe check_label(struct biosdisk *d, int sector)
    110  1.1  uwe {
    111  1.1  uwe 	struct disklabel *lp;
    112  1.1  uwe 
    113  1.1  uwe 	/* find partition in NetBSD disklabel */
    114  1.1  uwe 	if (readsects(d->dev, sector + LABELSECTOR, d->buf, 1)) {
    115  1.1  uwe #ifdef DISK_DEBUG
    116  1.1  uwe 		printf("Error reading disklabel\n");
    117  1.1  uwe #endif
    118  1.1  uwe 		return (EIO);
    119  1.1  uwe 	}
    120  1.1  uwe 
    121  1.1  uwe 	lp = (struct disklabel *)(d->buf + LABELOFFSET);
    122  1.1  uwe 	if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
    123  1.1  uwe #ifdef DISK_DEBUG
    124  1.1  uwe 		printf("warning: no disklabel\n");
    125  1.1  uwe #endif
    126  1.1  uwe 		return (-1);
    127  1.1  uwe 	}
    128  1.1  uwe 
    129  1.1  uwe 	d->boff = sector;
    130  1.1  uwe 	return (0);
    131  1.1  uwe }
    132  1.1  uwe 
    133  1.1  uwe static int
    134  1.1  uwe read_label(struct biosdisk *d)
    135  1.1  uwe {
    136  1.2  mrg 	struct mbr_sector *mbr_sect;
    137  1.1  uwe 	struct disklabel dflt_lbl;
    138  1.1  uwe 	struct mbr_partition mbr[MBR_PART_COUNT];
    139  1.1  uwe 	struct partition *p;
    140  1.1  uwe 	int sector, i;
    141  1.1  uwe 	int error;
    142  1.1  uwe 	int typ;
    143  1.1  uwe 	int ext_base, this_ext, next_ext;
    144  1.1  uwe #ifdef COMPAT_386BSD_MBRPART
    145  1.1  uwe 	int sector_386bsd = -1;
    146  1.1  uwe #endif
    147  1.1  uwe 
    148  1.1  uwe 	memset(&dflt_lbl, 0, sizeof dflt_lbl);
    149  1.1  uwe 	dflt_lbl.d_npartitions = 8;
    150  1.1  uwe 
    151  1.1  uwe 	d->boff = 0;
    152  1.1  uwe 
    153  1.1  uwe 	/*
    154  1.1  uwe 	 * find NetBSD Partition in DOS partition table
    155  1.1  uwe 	 * XXX check magic???
    156  1.1  uwe 	 */
    157  1.1  uwe 	ext_base = 0;
    158  1.1  uwe 	next_ext = 0;
    159  1.1  uwe 	for (;;) {
    160  1.1  uwe 		this_ext = ext_base + next_ext;
    161  1.1  uwe 		next_ext = 0;
    162  1.2  mrg 		mbr_sect = (struct mbr_sector *)d->buf;
    163  1.2  mrg 		memcpy(&mbr, mbr_sect->mbr_parts, sizeof(mbr));
    164  1.1  uwe 		if (readsects(d->dev, this_ext, d->buf, 1)) {
    165  1.1  uwe #ifdef DISK_DEBUG
    166  1.1  uwe 			printf("error reading MBR sector %d\n", this_ext);
    167  1.1  uwe #endif
    168  1.1  uwe 			return (EIO);
    169  1.1  uwe 		}
    170  1.2  mrg 		memcpy(&mbr, mbr_sect->mbr_parts, sizeof(mbr));
    171  1.1  uwe 		/* Look for NetBSD partition ID */
    172  1.1  uwe 		for (i = 0; i < MBR_PART_COUNT; i++) {
    173  1.1  uwe 			typ = mbr[i].mbrp_type;
    174  1.1  uwe 			if (typ == 0)
    175  1.1  uwe 				continue;
    176  1.1  uwe 			sector = this_ext + mbr[i].mbrp_start;
    177  1.1  uwe 			if (typ == MBR_PTYPE_NETBSD) {
    178  1.1  uwe 				error = check_label(d, sector);
    179  1.1  uwe 				if (error >= 0)
    180  1.1  uwe 					return error;
    181  1.1  uwe 			}
    182  1.1  uwe 			if (MBR_IS_EXTENDED(typ)) {
    183  1.1  uwe 				next_ext = mbr[i].mbrp_start;
    184  1.1  uwe 				continue;
    185  1.1  uwe 			}
    186  1.1  uwe #ifdef COMPAT_386BSD_MBRPART
    187  1.1  uwe 			if (this_ext == 0 && typ == MBR_PTYPE_386BSD)
    188  1.1  uwe 				sector_386bsd = sector;
    189  1.1  uwe #endif
    190  1.1  uwe 			if (this_ext != 0) {
    191  1.1  uwe 				if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
    192  1.1  uwe 					continue;
    193  1.1  uwe 				p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
    194  1.1  uwe 			} else
    195  1.1  uwe 				p = &dflt_lbl.d_partitions[i];
    196  1.1  uwe 			p->p_offset = sector;
    197  1.1  uwe 			p->p_size = mbr[i].mbrp_size;
    198  1.1  uwe 			p->p_fstype = xlat_mbr_fstype(typ);
    199  1.1  uwe 		}
    200  1.1  uwe 		if (next_ext == 0)
    201  1.1  uwe 			break;
    202  1.1  uwe 		if (ext_base == 0) {
    203  1.1  uwe 			ext_base = next_ext;
    204  1.1  uwe 			next_ext = 0;
    205  1.1  uwe 		}
    206  1.1  uwe 	}
    207  1.1  uwe 
    208  1.1  uwe 	sector = 0;
    209  1.1  uwe #ifdef COMPAT_386BSD_MBRPART
    210  1.1  uwe 	if (sector_386bsd != -1) {
    211  1.1  uwe 		printf("old BSD partition ID!\n");
    212  1.1  uwe 		sector = sector_386bsd;
    213  1.1  uwe 	}
    214  1.1  uwe #endif
    215  1.1  uwe 
    216  1.1  uwe 	/*
    217  1.1  uwe 	 * One of two things:
    218  1.1  uwe 	 * 	1. no MBR
    219  1.1  uwe 	 *	2. no NetBSD partition in MBR
    220  1.1  uwe 	 *
    221  1.1  uwe 	 * We simply default to "start of disk" in this case and
    222  1.1  uwe 	 * press on.
    223  1.1  uwe 	 */
    224  1.1  uwe 	error = check_label(d, sector);
    225  1.1  uwe 	if (error >= 0)
    226  1.1  uwe 		return (error);
    227  1.1  uwe 
    228  1.1  uwe 	/*
    229  1.1  uwe 	 * Nothing at start of disk, return info from mbr partitions.
    230  1.1  uwe 	 */
    231  1.1  uwe 	/* XXX fill it to make checksum match kernel one */
    232  1.1  uwe 	dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
    233  1.1  uwe 	memcpy(d->buf, &dflt_lbl, sizeof(dflt_lbl));
    234  1.1  uwe 	return (-1);
    235  1.1  uwe }
    236  1.1  uwe 
    237  1.1  uwe 
    238  1.1  uwe /*
    239  1.1  uwe  * Determine likely partition for possible sector number of dos
    240  1.1  uwe  * partition.
    241  1.1  uwe  */
    242  1.1  uwe u_int
    243  1.1  uwe biosdisk_findptn(int biosdev, u_int sector)
    244  1.1  uwe {
    245  1.1  uwe 	struct biosdisk *d;
    246  1.1  uwe 	u_int partition = 0;
    247  1.1  uwe 	struct disklabel *lp;
    248  1.1  uwe 
    249  1.1  uwe 	/* Look for netbsd partition that is the dos boot one */
    250  1.1  uwe 	d = alloc_biosdisk(biosdev);
    251  1.1  uwe 	if (d == NULL)
    252  1.1  uwe 		return (0);
    253  1.1  uwe 
    254  1.1  uwe 	if (read_label(d) == 0) {
    255  1.1  uwe 		lp = (struct disklabel *)(d->buf + LABELOFFSET);
    256  1.1  uwe 		for (partition = lp->d_npartitions; --partition;){
    257  1.1  uwe 			if (lp->d_partitions[partition].p_fstype == FS_UNUSED)
    258  1.1  uwe 				continue;
    259  1.1  uwe 			if (lp->d_partitions[partition].p_offset == sector)
    260  1.1  uwe 				break;
    261  1.1  uwe 		}
    262  1.1  uwe 	}
    263  1.1  uwe 
    264  1.1  uwe 	DEALLOC(d, sizeof(*d));
    265  1.1  uwe 	return (partition);
    266  1.1  uwe }
    267  1.1  uwe 
    268  1.1  uwe int
    269  1.1  uwe biosdisk_open(struct open_file *f, ...)
    270  1.1  uwe {
    271  1.1  uwe 	va_list ap;
    272  1.1  uwe 	struct biosdisk *d;
    273  1.1  uwe 	struct disklabel *lp;
    274  1.1  uwe 	int dev;
    275  1.1  uwe 	int partition;
    276  1.1  uwe 	int error = 0;
    277  1.1  uwe 
    278  1.1  uwe 	va_start(ap, f);
    279  1.1  uwe 	dev = va_arg(ap, int);
    280  1.1  uwe 	d = alloc_biosdisk(dev);
    281  1.1  uwe 	if (d == NULL) {
    282  1.1  uwe 		error = ENXIO;
    283  1.1  uwe 		goto out;
    284  1.1  uwe 	}
    285  1.1  uwe 
    286  1.1  uwe 	partition = va_arg(ap, int);
    287  1.1  uwe 	bi_disk.biosdev = d->dev;
    288  1.1  uwe 	bi_disk.partition = partition;
    289  1.1  uwe 	bi_disk.labelsector = -1;
    290  1.1  uwe 
    291  1.1  uwe 	if (partition == RAW_PART)
    292  1.1  uwe 		goto nolabel;
    293  1.1  uwe 	error = read_label(d);
    294  1.1  uwe 	if (error == -1) {
    295  1.1  uwe 		error = 0;
    296  1.1  uwe 		goto nolabel;
    297  1.1  uwe 	}
    298  1.1  uwe 	if (error)
    299  1.1  uwe 		goto out;
    300  1.1  uwe 
    301  1.1  uwe 	lp = (struct disklabel *)(d->buf + LABELOFFSET);
    302  1.1  uwe 	if (partition >= lp->d_npartitions ||
    303  1.1  uwe 	   lp->d_partitions[partition].p_fstype == FS_UNUSED) {
    304  1.1  uwe #ifdef DISK_DEBUG
    305  1.1  uwe 		printf("illegal partition\n");
    306  1.1  uwe #endif
    307  1.1  uwe 		error = EPART;
    308  1.1  uwe 		goto out;
    309  1.1  uwe 	}
    310  1.1  uwe 	bi_disk.labelsector = d->boff + LABELSECTOR;
    311  1.1  uwe 	bi_disk.label.type = lp->d_type;
    312  1.1  uwe 	memcpy(bi_disk.label.packname, lp->d_packname, 16);
    313  1.1  uwe 	bi_disk.label.checksum = lp->d_checksum;
    314  1.1  uwe 
    315  1.1  uwe 	d->boff = lp->d_partitions[partition].p_offset;
    316  1.1  uwe 	if (lp->d_partitions[partition].p_fstype == FS_RAID) {
    317  1.1  uwe 		d->boff += RF_PROTECTED_SECTORS;
    318  1.1  uwe 	}
    319  1.1  uwe nolabel:
    320  1.1  uwe 
    321  1.1  uwe #ifdef DISK_DEBUG
    322  1.1  uwe 	printf("partition @%d\n", d->boff);
    323  1.1  uwe #endif
    324  1.1  uwe 
    325  1.1  uwe 	BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
    326  1.1  uwe 
    327  1.1  uwe 	f->f_devdata = d;
    328  1.1  uwe out:
    329  1.1  uwe         va_end(ap);
    330  1.1  uwe 	if (error)
    331  1.1  uwe 		DEALLOC(d, sizeof(struct biosdisk));
    332  1.1  uwe 	return (error);
    333  1.1  uwe }
    334  1.1  uwe 
    335  1.1  uwe int
    336  1.1  uwe biosdisk_close(struct open_file *f)
    337  1.1  uwe {
    338  1.1  uwe 	struct biosdisk *d = f->f_devdata;
    339  1.1  uwe 
    340  1.1  uwe 	DEALLOC(d, sizeof(struct biosdisk));
    341  1.1  uwe 	f->f_devdata = NULL;
    342  1.1  uwe 
    343  1.1  uwe 	return (0);
    344  1.1  uwe }
    345  1.1  uwe 
    346  1.1  uwe int
    347  1.1  uwe biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
    348  1.1  uwe {
    349  1.1  uwe 
    350  1.1  uwe 	return (EIO);
    351  1.1  uwe }
    352  1.1  uwe 
    353  1.1  uwe int
    354  1.1  uwe biosdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
    355  1.1  uwe {
    356  1.1  uwe 	struct biosdisk *d;
    357  1.1  uwe 	int blks;
    358  1.1  uwe 	int frag;
    359  1.1  uwe 
    360  1.1  uwe 	if (flag != F_READ)
    361  1.1  uwe 		return EROFS;
    362  1.1  uwe 
    363  1.1  uwe 	d = (struct biosdisk *)devdata;
    364  1.1  uwe 
    365  1.1  uwe 	dblk += d->boff;
    366  1.1  uwe 
    367  1.1  uwe 	blks = size / BIOSDISK_SECSIZE;
    368  1.1  uwe 	if (blks && readsects(d->dev, dblk, buf, blks)) {
    369  1.1  uwe 		if (rsize)
    370  1.1  uwe 			*rsize = 0;
    371  1.1  uwe 		return (EIO);
    372  1.1  uwe 	}
    373  1.1  uwe 
    374  1.1  uwe 	/* do we really need this? */
    375  1.1  uwe 	frag = size % BIOSDISK_SECSIZE;
    376  1.1  uwe 	if (frag) {
    377  1.1  uwe 		if (readsects(d->dev, dblk + blks, d->buf, 1)) {
    378  1.1  uwe 			if (rsize)
    379  1.1  uwe 				*rsize = blks * BIOSDISK_SECSIZE;
    380  1.1  uwe 			return (EIO);
    381  1.1  uwe 		}
    382  1.1  uwe 		memcpy(buf + blks * BIOSDISK_SECSIZE, d->buf, frag);
    383  1.1  uwe 	}
    384  1.1  uwe 
    385  1.1  uwe 	if (rsize)
    386  1.1  uwe 		*rsize = size;
    387  1.1  uwe 	return (0);
    388  1.1  uwe }
    389