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