Home | History | Annotate | Line # | Download | only in dkwedge
dkwedge_mbr.c revision 1.8
      1  1.8   mlelstv /*	$NetBSD: dkwedge_mbr.c,v 1.8 2014/11/04 07:46:26 mlelstv 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.8   mlelstv __KERNEL_RCSID(0, "$NetBSD: dkwedge_mbr.c,v 1.8 2014/11/04 07:46:26 mlelstv 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.7  christos #include <sys/disklabel.h>
     49  1.1   thorpej 
     50  1.1   thorpej typedef struct mbr_args {
     51  1.1   thorpej 	struct disk	*pdk;
     52  1.1   thorpej 	struct vnode	*vp;
     53  1.1   thorpej 	void		*buf;
     54  1.1   thorpej 	int		error;
     55  1.7  christos 	uint32_t	secsize;
     56  1.1   thorpej 	int		mbr_count;
     57  1.1   thorpej } mbr_args_t;
     58  1.1   thorpej 
     59  1.1   thorpej static const char *
     60  1.1   thorpej mbr_ptype_to_str(uint8_t ptype)
     61  1.1   thorpej {
     62  1.1   thorpej 	const char *str;
     63  1.1   thorpej 
     64  1.1   thorpej 	switch (ptype) {
     65  1.1   thorpej 	case MBR_PTYPE_FAT12:	str = DKW_PTYPE_FAT;		break;
     66  1.1   thorpej 	case MBR_PTYPE_FAT16S:	str = DKW_PTYPE_FAT;		break;
     67  1.1   thorpej 	case MBR_PTYPE_FAT16B:	str = DKW_PTYPE_FAT;		break;
     68  1.1   thorpej 	case MBR_PTYPE_NTFS:	str = DKW_PTYPE_NTFS;		break;
     69  1.1   thorpej 	case MBR_PTYPE_FAT32:	str = DKW_PTYPE_FAT;		break;
     70  1.1   thorpej 	case MBR_PTYPE_FAT32L:	str = DKW_PTYPE_FAT;		break;
     71  1.1   thorpej 	case MBR_PTYPE_FAT16L:	str = DKW_PTYPE_FAT;		break;
     72  1.1   thorpej 	case MBR_PTYPE_LNXSWAP:	str = DKW_PTYPE_SWAP;		break;
     73  1.1   thorpej 	case MBR_PTYPE_LNXEXT2:	str = DKW_PTYPE_EXT2FS;		break;
     74  1.2   thorpej 	case MBR_PTYPE_APPLE_UFS:str = DKW_PTYPE_APPLEUFS;	break;
     75  1.1   thorpej 	case MBR_PTYPE_EFI:	str = DKW_PTYPE_FAT;		break;
     76  1.1   thorpej 	default:		str = NULL;			break;
     77  1.1   thorpej 	}
     78  1.1   thorpej 
     79  1.1   thorpej 	return (str);
     80  1.1   thorpej }
     81  1.1   thorpej 
     82  1.1   thorpej static void
     83  1.1   thorpej getparts(mbr_args_t *a, uint32_t off, uint32_t extoff)
     84  1.1   thorpej {
     85  1.1   thorpej 	struct dkwedge_info dkw;
     86  1.1   thorpej 	struct mbr_partition *dp;
     87  1.1   thorpej 	struct mbr_sector *mbr;
     88  1.1   thorpej 	const char *ptype;
     89  1.1   thorpej 	int i, error;
     90  1.1   thorpej 
     91  1.7  christos 	error = dkwedge_read(a->pdk, a->vp, off, a->buf, a->secsize);
     92  1.1   thorpej 	if (error) {
     93  1.7  christos 		aprint_error("%s: unable to read MBR @ %u/%u, "
     94  1.7  christos 		    "error = %d\n", a->pdk->dk_name, off, a->secsize, a->error);
     95  1.1   thorpej 		a->error = error;
     96  1.1   thorpej 		return;
     97  1.1   thorpej 	}
     98  1.1   thorpej 
     99  1.1   thorpej 	mbr = a->buf;
    100  1.1   thorpej 	if (mbr->mbr_magic != htole16(MBR_MAGIC))
    101  1.1   thorpej 		return;
    102  1.1   thorpej 
    103  1.1   thorpej 	dp = mbr->mbr_parts;
    104  1.1   thorpej 
    105  1.1   thorpej 	for (i = 0; i < MBR_PART_COUNT; i++) {
    106  1.1   thorpej 		/* Extended partitions are handled below. */
    107  1.1   thorpej 		if (dp[i].mbrp_type == 0 ||
    108  1.1   thorpej 		    MBR_IS_EXTENDED(dp[i].mbrp_type))
    109  1.1   thorpej 		    	continue;
    110  1.1   thorpej 
    111  1.1   thorpej 		if ((ptype = mbr_ptype_to_str(dp[i].mbrp_type)) == NULL) {
    112  1.1   thorpej 			/*
    113  1.1   thorpej 			 * XXX Should probably just add these...
    114  1.1   thorpej 			 * XXX maybe just have an empty ptype?
    115  1.1   thorpej 			 */
    116  1.1   thorpej 			aprint_verbose("%s: skipping partition %d, "
    117  1.1   thorpej 			    "type 0x%02x\n", a->pdk->dk_name, i,
    118  1.1   thorpej 			    dp[i].mbrp_type);
    119  1.1   thorpej 			continue;
    120  1.1   thorpej 		}
    121  1.1   thorpej 		strcpy(dkw.dkw_ptype, ptype);
    122  1.1   thorpej 
    123  1.1   thorpej 		strcpy(dkw.dkw_parent, a->pdk->dk_name);
    124  1.1   thorpej 		dkw.dkw_offset = le32toh(dp[i].mbrp_start);
    125  1.1   thorpej 		dkw.dkw_size = le32toh(dp[i].mbrp_size);
    126  1.1   thorpej 
    127  1.1   thorpej 		/*
    128  1.1   thorpej 		 * These get historical disk naming style
    129  1.1   thorpej 		 * wedge names.  We start at 'e', and reserve
    130  1.1   thorpej 		 * 4 slots for each MBR we parse.
    131  1.1   thorpej 		 *
    132  1.1   thorpej 		 * XXX For FAT, we should extract the FAT volume
    133  1.1   thorpej 		 * XXX name.
    134  1.1   thorpej 		 */
    135  1.1   thorpej 		snprintf(dkw.dkw_wname, sizeof(dkw.dkw_wname),
    136  1.1   thorpej 		    "%s%c", a->pdk->dk_name,
    137  1.1   thorpej 		    'e' + (a->mbr_count * MBR_PART_COUNT) + i);
    138  1.1   thorpej 
    139  1.1   thorpej 		error = dkwedge_add(&dkw);
    140  1.1   thorpej 		if (error == EEXIST)
    141  1.1   thorpej 			aprint_error("%s: wedge named '%s' already "
    142  1.1   thorpej 			    "exists, manual intervention required\n",
    143  1.1   thorpej 			    a->pdk->dk_name, dkw.dkw_wname);
    144  1.1   thorpej 		else if (error)
    145  1.1   thorpej 			aprint_error("%s: error %d adding partition "
    146  1.1   thorpej 			    "%d type 0x%02x\n", a->pdk->dk_name, error,
    147  1.1   thorpej 			    (a->mbr_count * MBR_PART_COUNT) + i,
    148  1.1   thorpej 			    dp[i].mbrp_type);
    149  1.1   thorpej 	}
    150  1.1   thorpej 
    151  1.1   thorpej 	/* We've parsed one MBR. */
    152  1.1   thorpej 	a->mbr_count++;
    153  1.1   thorpej 
    154  1.1   thorpej 	/* Recursively scan extended partitions. */
    155  1.1   thorpej 	for (i = 0; i < MBR_PART_COUNT; i++) {
    156  1.1   thorpej 		uint32_t poff;
    157  1.1   thorpej 
    158  1.1   thorpej 		if (MBR_IS_EXTENDED(dp[i].mbrp_type)) {
    159  1.1   thorpej 			poff = le32toh(dp[i].mbrp_start) + extoff;
    160  1.1   thorpej 			getparts(a, poff, extoff ? extoff : poff);
    161  1.1   thorpej 		}
    162  1.1   thorpej 	}
    163  1.1   thorpej }
    164  1.1   thorpej 
    165  1.1   thorpej static int
    166  1.1   thorpej dkwedge_discover_mbr(struct disk *pdk, struct vnode *vp)
    167  1.1   thorpej {
    168  1.1   thorpej 	mbr_args_t a;
    169  1.1   thorpej 
    170  1.1   thorpej 	a.pdk = pdk;
    171  1.8   mlelstv 	a.secsize = DEV_BSIZE << pdk->dk_blkshift;
    172  1.1   thorpej 	a.vp = vp;
    173  1.7  christos 	a.buf = malloc(a.secsize, M_DEVBUF, M_WAITOK);
    174  1.1   thorpej 	a.error = 0;
    175  1.1   thorpej 	a.mbr_count = 0;
    176  1.1   thorpej 
    177  1.1   thorpej 	getparts(&a, MBR_BBSECTOR, 0);
    178  1.1   thorpej 	if (a.mbr_count != 0)
    179  1.1   thorpej 		a.error = 0;		/* found it, wedges installed */
    180  1.1   thorpej 	else if (a.error == 0)
    181  1.1   thorpej 		a.error = ESRCH;	/* no MBRs found */
    182  1.1   thorpej 
    183  1.1   thorpej 	free(a.buf, M_DEVBUF);
    184  1.1   thorpej 	return (a.error);
    185  1.1   thorpej }
    186  1.1   thorpej 
    187  1.1   thorpej DKWEDGE_DISCOVERY_METHOD_DECL(MBR, 10, dkwedge_discover_mbr);
    188