Home | History | Annotate | Line # | Download | only in dkwedge
dkwedge_mbr.c revision 1.6
      1  1.6   martin /*	$NetBSD: dkwedge_mbr.c,v 1.6 2008/04/28 20:23:48 martin Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe.
      9  1.1  thorpej  *
     10  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1  thorpej  * modification, are permitted provided that the following conditions
     12  1.1  thorpej  * are met:
     13  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1  thorpej  *
     19  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  thorpej  */
     31  1.1  thorpej 
     32  1.1  thorpej /*
     33  1.1  thorpej  * Master Boot Record partition table support for disk wedges
     34  1.1  thorpej  */
     35  1.1  thorpej 
     36  1.1  thorpej #include <sys/cdefs.h>
     37  1.6   martin __KERNEL_RCSID(0, "$NetBSD: dkwedge_mbr.c,v 1.6 2008/04/28 20:23:48 martin Exp $");
     38  1.4   martin 
     39  1.1  thorpej #include <sys/param.h>
     40  1.1  thorpej #include <sys/systm.h>
     41  1.1  thorpej #include <sys/proc.h>
     42  1.1  thorpej #include <sys/errno.h>
     43  1.1  thorpej #include <sys/disk.h>
     44  1.1  thorpej #include <sys/vnode.h>
     45  1.1  thorpej #include <sys/malloc.h>
     46  1.1  thorpej 
     47  1.1  thorpej #include <sys/bootblock.h>
     48  1.1  thorpej 
     49  1.1  thorpej typedef struct mbr_args {
     50  1.1  thorpej 	struct disk	*pdk;
     51  1.1  thorpej 	struct vnode	*vp;
     52  1.1  thorpej 	void		*buf;
     53  1.1  thorpej 	int		error;
     54  1.1  thorpej 	int		mbr_count;
     55  1.1  thorpej } mbr_args_t;
     56  1.1  thorpej 
     57  1.1  thorpej static const char *
     58  1.1  thorpej mbr_ptype_to_str(uint8_t ptype)
     59  1.1  thorpej {
     60  1.1  thorpej 	const char *str;
     61  1.1  thorpej 
     62  1.1  thorpej 	switch (ptype) {
     63  1.1  thorpej 	case MBR_PTYPE_FAT12:	str = DKW_PTYPE_FAT;		break;
     64  1.1  thorpej 	case MBR_PTYPE_FAT16S:	str = DKW_PTYPE_FAT;		break;
     65  1.1  thorpej 	case MBR_PTYPE_FAT16B:	str = DKW_PTYPE_FAT;		break;
     66  1.1  thorpej 	case MBR_PTYPE_NTFS:	str = DKW_PTYPE_NTFS;		break;
     67  1.1  thorpej 	case MBR_PTYPE_FAT32:	str = DKW_PTYPE_FAT;		break;
     68  1.1  thorpej 	case MBR_PTYPE_FAT32L:	str = DKW_PTYPE_FAT;		break;
     69  1.1  thorpej 	case MBR_PTYPE_FAT16L:	str = DKW_PTYPE_FAT;		break;
     70  1.1  thorpej 	case MBR_PTYPE_LNXSWAP:	str = DKW_PTYPE_SWAP;		break;
     71  1.1  thorpej 	case MBR_PTYPE_LNXEXT2:	str = DKW_PTYPE_EXT2FS;		break;
     72  1.2  thorpej 	case MBR_PTYPE_APPLE_UFS:str = DKW_PTYPE_APPLEUFS;	break;
     73  1.1  thorpej 	case MBR_PTYPE_EFI:	str = DKW_PTYPE_FAT;		break;
     74  1.1  thorpej 	default:		str = NULL;			break;
     75  1.1  thorpej 	}
     76  1.1  thorpej 
     77  1.1  thorpej 	return (str);
     78  1.1  thorpej }
     79  1.1  thorpej 
     80  1.1  thorpej static void
     81  1.1  thorpej getparts(mbr_args_t *a, uint32_t off, uint32_t extoff)
     82  1.1  thorpej {
     83  1.1  thorpej 	struct dkwedge_info dkw;
     84  1.1  thorpej 	struct mbr_partition *dp;
     85  1.1  thorpej 	struct mbr_sector *mbr;
     86  1.1  thorpej 	const char *ptype;
     87  1.1  thorpej 	int i, error;
     88  1.1  thorpej 
     89  1.1  thorpej 	error = dkwedge_read(a->pdk, a->vp, off, a->buf, DEV_BSIZE);
     90  1.1  thorpej 	if (error) {
     91  1.1  thorpej 		aprint_error("%s: unable to read MBR @ %u, "
     92  1.1  thorpej 		    "error = %d\n", a->pdk->dk_name, off, a->error);
     93  1.1  thorpej 		a->error = error;
     94  1.1  thorpej 		return;
     95  1.1  thorpej 	}
     96  1.1  thorpej 
     97  1.1  thorpej 	mbr = a->buf;
     98  1.1  thorpej 	if (mbr->mbr_magic != htole16(MBR_MAGIC))
     99  1.1  thorpej 		return;
    100  1.1  thorpej 
    101  1.1  thorpej 	dp = mbr->mbr_parts;
    102  1.1  thorpej 
    103  1.1  thorpej 	for (i = 0; i < MBR_PART_COUNT; i++) {
    104  1.1  thorpej 		/* Extended partitions are handled below. */
    105  1.1  thorpej 		if (dp[i].mbrp_type == 0 ||
    106  1.1  thorpej 		    MBR_IS_EXTENDED(dp[i].mbrp_type))
    107  1.1  thorpej 		    	continue;
    108  1.1  thorpej 
    109  1.1  thorpej 		if ((ptype = mbr_ptype_to_str(dp[i].mbrp_type)) == NULL) {
    110  1.1  thorpej 			/*
    111  1.1  thorpej 			 * XXX Should probably just add these...
    112  1.1  thorpej 			 * XXX maybe just have an empty ptype?
    113  1.1  thorpej 			 */
    114  1.1  thorpej 			aprint_verbose("%s: skipping partition %d, "
    115  1.1  thorpej 			    "type 0x%02x\n", a->pdk->dk_name, i,
    116  1.1  thorpej 			    dp[i].mbrp_type);
    117  1.1  thorpej 			continue;
    118  1.1  thorpej 		}
    119  1.1  thorpej 		strcpy(dkw.dkw_ptype, ptype);
    120  1.1  thorpej 
    121  1.1  thorpej 		strcpy(dkw.dkw_parent, a->pdk->dk_name);
    122  1.1  thorpej 		dkw.dkw_offset = le32toh(dp[i].mbrp_start);
    123  1.1  thorpej 		dkw.dkw_size = le32toh(dp[i].mbrp_size);
    124  1.1  thorpej 
    125  1.1  thorpej 		/*
    126  1.1  thorpej 		 * These get historical disk naming style
    127  1.1  thorpej 		 * wedge names.  We start at 'e', and reserve
    128  1.1  thorpej 		 * 4 slots for each MBR we parse.
    129  1.1  thorpej 		 *
    130  1.1  thorpej 		 * XXX For FAT, we should extract the FAT volume
    131  1.1  thorpej 		 * XXX name.
    132  1.1  thorpej 		 */
    133  1.1  thorpej 		snprintf(dkw.dkw_wname, sizeof(dkw.dkw_wname),
    134  1.1  thorpej 		    "%s%c", a->pdk->dk_name,
    135  1.1  thorpej 		    'e' + (a->mbr_count * MBR_PART_COUNT) + i);
    136  1.1  thorpej 
    137  1.1  thorpej 		error = dkwedge_add(&dkw);
    138  1.1  thorpej 		if (error == EEXIST)
    139  1.1  thorpej 			aprint_error("%s: wedge named '%s' already "
    140  1.1  thorpej 			    "exists, manual intervention required\n",
    141  1.1  thorpej 			    a->pdk->dk_name, dkw.dkw_wname);
    142  1.1  thorpej 		else if (error)
    143  1.1  thorpej 			aprint_error("%s: error %d adding partition "
    144  1.1  thorpej 			    "%d type 0x%02x\n", a->pdk->dk_name, error,
    145  1.1  thorpej 			    (a->mbr_count * MBR_PART_COUNT) + i,
    146  1.1  thorpej 			    dp[i].mbrp_type);
    147  1.1  thorpej 	}
    148  1.1  thorpej 
    149  1.1  thorpej 	/* We've parsed one MBR. */
    150  1.1  thorpej 	a->mbr_count++;
    151  1.1  thorpej 
    152  1.1  thorpej 	/* Recursively scan extended partitions. */
    153  1.1  thorpej 	for (i = 0; i < MBR_PART_COUNT; i++) {
    154  1.1  thorpej 		uint32_t poff;
    155  1.1  thorpej 
    156  1.1  thorpej 		if (MBR_IS_EXTENDED(dp[i].mbrp_type)) {
    157  1.1  thorpej 			poff = le32toh(dp[i].mbrp_start) + extoff;
    158  1.1  thorpej 			getparts(a, poff, extoff ? extoff : poff);
    159  1.1  thorpej 		}
    160  1.1  thorpej 	}
    161  1.1  thorpej }
    162  1.1  thorpej 
    163  1.1  thorpej static int
    164  1.1  thorpej dkwedge_discover_mbr(struct disk *pdk, struct vnode *vp)
    165  1.1  thorpej {
    166  1.1  thorpej 	mbr_args_t a;
    167  1.1  thorpej 
    168  1.1  thorpej 	a.pdk = pdk;
    169  1.1  thorpej 	a.vp = vp;
    170  1.1  thorpej 	a.buf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK);
    171  1.1  thorpej 	a.error = 0;
    172  1.1  thorpej 	a.mbr_count = 0;
    173  1.1  thorpej 
    174  1.1  thorpej 	getparts(&a, MBR_BBSECTOR, 0);
    175  1.1  thorpej 	if (a.mbr_count != 0)
    176  1.1  thorpej 		a.error = 0;		/* found it, wedges installed */
    177  1.1  thorpej 	else if (a.error == 0)
    178  1.1  thorpej 		a.error = ESRCH;	/* no MBRs found */
    179  1.1  thorpej 
    180  1.1  thorpej 	free(a.buf, M_DEVBUF);
    181  1.1  thorpej 	return (a.error);
    182  1.1  thorpej }
    183  1.1  thorpej 
    184  1.1  thorpej DKWEDGE_DISCOVERY_METHOD_DECL(MBR, 10, dkwedge_discover_mbr);
    185