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