1 /* $NetBSD: dkwedge_mbr.c,v 1.10 2017/01/19 00:44:40 maya 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.10 2017/01/19 00:44:40 maya 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 #include <sys/disklabel.h> 49 50 typedef struct mbr_args { 51 struct disk *pdk; 52 struct vnode *vp; 53 void *buf; 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->buf, a->secsize); 92 if (error) { 93 aprint_error("%s: unable to read MBR @ %u/%u, " 94 "error = %d\n", a->pdk->dk_name, off, a->secsize, a->error); 95 a->error = error; 96 return; 97 } 98 99 mbr = a->buf; 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 if ((ptype = mbr_ptype_to_str(dp[i].mbrp_type)) == NULL) { 118 /* 119 * XXX Should probably just add these... 120 * XXX maybe just have an empty ptype? 121 */ 122 aprint_verbose("%s: skipping partition %d, " 123 "type 0x%02x\n", a->pdk->dk_name, i, 124 dp[i].mbrp_type); 125 continue; 126 } 127 strlcpy(dkw.dkw_ptype, ptype, sizeof(dkw.dkw_ptype)); 128 129 strlcpy(dkw.dkw_parent, a->pdk->dk_name, sizeof(dkw.dkw_parent)); 130 dkw.dkw_offset = le32toh(dp[i].mbrp_start); 131 dkw.dkw_size = le32toh(dp[i].mbrp_size); 132 133 /* 134 * These get historical disk naming style 135 * wedge names. We start at 'e', and reserve 136 * 4 slots for each MBR we parse. 137 * 138 * XXX For FAT, we should extract the FAT volume 139 * XXX name. 140 */ 141 snprintf(dkw.dkw_wname, sizeof(dkw.dkw_wname), 142 "%s%c", a->pdk->dk_name, 143 'e' + (a->mbr_count * MBR_PART_COUNT) + i); 144 145 error = dkwedge_add(&dkw); 146 if (error == EEXIST) 147 aprint_error("%s: wedge named '%s' already " 148 "exists, manual intervention required\n", 149 a->pdk->dk_name, dkw.dkw_wname); 150 else if (error) 151 aprint_error("%s: error %d adding partition " 152 "%d type 0x%02x\n", a->pdk->dk_name, error, 153 (a->mbr_count * MBR_PART_COUNT) + i, 154 dp[i].mbrp_type); 155 } 156 157 /* We've parsed one MBR. */ 158 a->mbr_count++; 159 160 /* Recursively scan extended partitions. */ 161 for (i = 0; i < MBR_PART_COUNT; i++) { 162 uint32_t poff; 163 164 if (MBR_IS_EXTENDED(dp[i].mbrp_type)) { 165 poff = le32toh(dp[i].mbrp_start) + extoff; 166 getparts(a, poff, extoff ? extoff : poff); 167 } 168 } 169 } 170 171 static int 172 dkwedge_discover_mbr(struct disk *pdk, struct vnode *vp) 173 { 174 mbr_args_t a; 175 176 a.pdk = pdk; 177 a.secsize = DEV_BSIZE << pdk->dk_blkshift; 178 a.vp = vp; 179 a.buf = malloc(a.secsize, M_DEVBUF, M_WAITOK); 180 a.error = 0; 181 a.mbr_count = 0; 182 183 getparts(&a, MBR_BBSECTOR, 0); 184 if (a.mbr_count != 0) 185 a.error = 0; /* found it, wedges installed */ 186 else if (a.error == 0) 187 a.error = ESRCH; /* no MBRs found */ 188 189 free(a.buf, M_DEVBUF); 190 return (a.error); 191 } 192 193 DKWEDGE_DISCOVERY_METHOD_DECL(MBR, 10, dkwedge_discover_mbr); 194