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