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