FreeBSD/Linux Kernel Cross Reference
sys/geom/geom_apple.c
1 /*-
2 * Copyright (c) 2002 Peter Grehan.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 /*
28 * GEOM module for Apple Partition Maps
29 * As described in 'Inside Macintosh Vol 3: About the SCSI Manager -
30 * The Structure of Block Devices"
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: releng/5.2/sys/geom/geom_apple.c 116196 2003-06-11 06:49:16Z obrien $");
35
36 #include <sys/param.h>
37 #include <sys/endian.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/bio.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44
45 #include <sys/sbuf.h>
46 #include <geom/geom.h>
47 #include <geom/geom_slice.h>
48
49 #define APPLE_CLASS_NAME "APPLE"
50
51 #define NAPMPART 16 /* Max partitions */
52
53 struct apm_partition {
54 char am_sig[2];
55 u_int32_t am_mapcnt;
56 u_int32_t am_start;
57 u_int32_t am_partcnt;
58 char am_name[32];
59 char am_type[32];
60 };
61
62 struct g_apple_softc {
63 u_int16_t dd_bsiz;
64 u_int32_t dd_blkcnt;
65 u_int16_t dd_drvrcnt;
66 u_int32_t am_mapcnt0;
67 struct apm_partition apmpart[NAPMPART];
68 };
69
70 static void
71 g_dec_drvrdesc(u_char *ptr, struct g_apple_softc *sc)
72 {
73 sc->dd_bsiz = be16dec(ptr + 2);
74 sc->dd_blkcnt = be32dec(ptr + 4);
75 sc->dd_drvrcnt = be32dec(ptr + 16);
76 }
77
78 static void
79 g_dec_apple_partition(u_char *ptr, struct apm_partition *d)
80 {
81 d->am_sig[0] = ptr[0];
82 d->am_sig[1] = ptr[1];
83 d->am_mapcnt = be32dec(ptr + 4);
84 d->am_start = be32dec(ptr + 8);
85 d->am_partcnt = be32dec(ptr + 12);
86 memcpy(d->am_name, ptr + 16, 32);
87 memcpy(d->am_type, ptr + 48, 32);
88 }
89
90 static int
91 g_apple_start(struct bio *bp)
92 {
93 struct g_provider *pp;
94 struct g_geom *gp;
95 struct g_slicer *gsp;
96
97 pp = bp->bio_to;
98 gp = pp->geom;
99 gsp = gp->softc;
100 if (bp->bio_cmd == BIO_GETATTR) {
101 if (g_handleattr_off_t(bp, "APM::offset",
102 gsp->slices[pp->index].offset))
103 return (1);
104 }
105 return (0);
106 }
107
108 static void
109 g_apple_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
110 struct g_consumer *cp __unused, struct g_provider *pp)
111 {
112 struct g_apple_softc *mp;
113 struct g_slicer *gsp;
114
115 gsp = gp->softc;
116 mp = gsp->softc;
117 g_slice_dumpconf(sb, indent, gp, cp, pp);
118 if (pp != NULL) {
119 if (indent == NULL)
120 sbuf_printf(sb, " n %s ty %s",
121 mp->apmpart[pp->index].am_name,
122 mp->apmpart[pp->index].am_type);
123 else {
124 sbuf_printf(sb, "%s<name>%s</name>\n", indent,
125 mp->apmpart[pp->index].am_name);
126 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
127 mp->apmpart[pp->index].am_type);
128 }
129 }
130 }
131
132 #if 0
133 static void
134 g_apple_print()
135 {
136
137 /* XXX */
138 }
139 #endif
140
141 static struct g_geom *
142 g_apple_taste(struct g_class *mp, struct g_provider *pp, int insist)
143 {
144 struct g_geom *gp;
145 struct g_consumer *cp;
146 int error, i;
147 struct g_apple_softc *ms;
148 struct apm_partition *apm;
149 u_int sectorsize;
150 u_char *buf;
151
152 g_trace(G_T_TOPOLOGY, "apple_taste(%s,%s)", mp->name, pp->name);
153 g_topology_assert();
154 gp = g_slice_new(mp, NAPMPART, pp, &cp, &ms, sizeof *ms, g_apple_start);
155 if (gp == NULL)
156 return (NULL);
157 g_topology_unlock();
158 gp->dumpconf = g_apple_dumpconf;
159 do {
160 if (gp->rank != 2 && insist == 0)
161 break;
162
163 sectorsize = cp->provider->sectorsize;
164 if (sectorsize != 512)
165 break;
166
167 buf = g_read_data(cp, 0, sectorsize, &error);
168 if (buf == NULL || error != 0)
169 break;
170
171 /*
172 * Test for the sector 0 driver record signature, and
173 * validate sector and disk size
174 */
175 if (buf[0] != 'E' && buf[1] != 'R') {
176 g_free(buf);
177 break;
178 }
179 g_dec_drvrdesc(buf, ms);
180 g_free(buf);
181
182 if (ms->dd_bsiz != 512) {
183 break;
184 }
185
186 /*
187 * Read in the first partition map
188 */
189 buf = g_read_data(cp, sectorsize, sectorsize, &error);
190 if (buf == NULL || error != 0)
191 break;
192
193 /*
194 * Decode the first partition: it's another indication of
195 * validity, as well as giving the size of the partition
196 * map
197 */
198 apm = &ms->apmpart[0];
199 g_dec_apple_partition(buf, apm);
200 g_free(buf);
201
202 if (apm->am_sig[0] != 'P' || apm->am_sig[1] != 'M')
203 break;
204 ms->am_mapcnt0 = apm->am_mapcnt;
205
206 buf = g_read_data(cp, 2 * sectorsize,
207 (NAPMPART - 1) * sectorsize, &error);
208 if (buf == NULL || error != 0)
209 break;
210
211 for (i = 1; i < NAPMPART; i++) {
212 g_dec_apple_partition(buf + ((i - 1) * sectorsize),
213 &ms->apmpart[i]);
214 }
215
216 for (i = 0; i < NAPMPART; i++) {
217 apm = &ms->apmpart[i];
218
219 /*
220 * Validate partition sig and global mapcount
221 */
222 if (apm->am_sig[0] != 'P' ||
223 apm->am_sig[1] != 'M')
224 continue;
225 if (apm->am_mapcnt != ms->am_mapcnt0)
226 continue;
227
228 if (bootverbose) {
229 printf("APM Slice %d (%s/%s) on %s:\n",
230 i + 1, apm->am_name, apm->am_type,
231 gp->name);
232 /* g_apple_print(i, dp + i); */
233 }
234 g_topology_lock();
235 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
236 (off_t)apm->am_start << 9ULL,
237 (off_t)apm->am_partcnt << 9ULL,
238 sectorsize,
239 "%ss%d", gp->name, i + 1);
240 g_topology_unlock();
241 }
242 g_free(buf);
243 break;
244 } while(0);
245 g_topology_lock();
246 g_access_rel(cp, -1, 0, 0);
247 if (LIST_EMPTY(&gp->provider)) {
248 g_slice_spoiled(cp);
249 return (NULL);
250 }
251 return (gp);
252 }
253
254
255 static struct g_class g_apple_class = {
256 .name = APPLE_CLASS_NAME,
257 .taste = g_apple_taste,
258 };
259
260 DECLARE_GEOM_CLASS(g_apple_class, g_apple);
Cache object: e5565e0b0d776e1dc21225992fccc286
|