dkwedge_mbr.c revision 1.8 1 /* $NetBSD: dkwedge_mbr.c,v 1.8 2014/11/04 07:46:26 mlelstv 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.8 2014/11/04 07:46:26 mlelstv 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 /* Extended partitions are handled below. */
107 if (dp[i].mbrp_type == 0 ||
108 MBR_IS_EXTENDED(dp[i].mbrp_type))
109 continue;
110
111 if ((ptype = mbr_ptype_to_str(dp[i].mbrp_type)) == NULL) {
112 /*
113 * XXX Should probably just add these...
114 * XXX maybe just have an empty ptype?
115 */
116 aprint_verbose("%s: skipping partition %d, "
117 "type 0x%02x\n", a->pdk->dk_name, i,
118 dp[i].mbrp_type);
119 continue;
120 }
121 strcpy(dkw.dkw_ptype, ptype);
122
123 strcpy(dkw.dkw_parent, a->pdk->dk_name);
124 dkw.dkw_offset = le32toh(dp[i].mbrp_start);
125 dkw.dkw_size = le32toh(dp[i].mbrp_size);
126
127 /*
128 * These get historical disk naming style
129 * wedge names. We start at 'e', and reserve
130 * 4 slots for each MBR we parse.
131 *
132 * XXX For FAT, we should extract the FAT volume
133 * XXX name.
134 */
135 snprintf(dkw.dkw_wname, sizeof(dkw.dkw_wname),
136 "%s%c", a->pdk->dk_name,
137 'e' + (a->mbr_count * MBR_PART_COUNT) + i);
138
139 error = dkwedge_add(&dkw);
140 if (error == EEXIST)
141 aprint_error("%s: wedge named '%s' already "
142 "exists, manual intervention required\n",
143 a->pdk->dk_name, dkw.dkw_wname);
144 else if (error)
145 aprint_error("%s: error %d adding partition "
146 "%d type 0x%02x\n", a->pdk->dk_name, error,
147 (a->mbr_count * MBR_PART_COUNT) + i,
148 dp[i].mbrp_type);
149 }
150
151 /* We've parsed one MBR. */
152 a->mbr_count++;
153
154 /* Recursively scan extended partitions. */
155 for (i = 0; i < MBR_PART_COUNT; i++) {
156 uint32_t poff;
157
158 if (MBR_IS_EXTENDED(dp[i].mbrp_type)) {
159 poff = le32toh(dp[i].mbrp_start) + extoff;
160 getparts(a, poff, extoff ? extoff : poff);
161 }
162 }
163 }
164
165 static int
166 dkwedge_discover_mbr(struct disk *pdk, struct vnode *vp)
167 {
168 mbr_args_t a;
169
170 a.pdk = pdk;
171 a.secsize = DEV_BSIZE << pdk->dk_blkshift;
172 a.vp = vp;
173 a.buf = malloc(a.secsize, M_DEVBUF, M_WAITOK);
174 a.error = 0;
175 a.mbr_count = 0;
176
177 getparts(&a, MBR_BBSECTOR, 0);
178 if (a.mbr_count != 0)
179 a.error = 0; /* found it, wedges installed */
180 else if (a.error == 0)
181 a.error = ESRCH; /* no MBRs found */
182
183 free(a.buf, M_DEVBUF);
184 return (a.error);
185 }
186
187 DKWEDGE_DISCOVERY_METHOD_DECL(MBR, 10, dkwedge_discover_mbr);
188