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