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