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