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/6.4/sys/geom/geom_apple.c 153827 2005-12-29 05:59:51Z sobomax $");
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, " ty %s",
121 mp->apmpart[pp->index].am_type);
122 if (*mp->apmpart[pp->index].am_name)
123 sbuf_printf(sb, " sn %s",
124 mp->apmpart[pp->index].am_name);
125 } else {
126 sbuf_printf(sb, "%s<name>%s</name>\n", indent,
127 mp->apmpart[pp->index].am_name);
128 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
129 mp->apmpart[pp->index].am_type);
130 }
131 }
132 }
133
134 #if 0
135 static void
136 g_apple_print()
137 {
138
139 /* XXX */
140 }
141 #endif
142
143 static struct g_geom *
144 g_apple_taste(struct g_class *mp, struct g_provider *pp, int insist)
145 {
146 struct g_geom *gp;
147 struct g_consumer *cp;
148 int i;
149 struct g_apple_softc *ms;
150 struct apm_partition *apm;
151 u_int sectorsize;
152 u_char *buf;
153
154 g_trace(G_T_TOPOLOGY, "apple_taste(%s,%s)", mp->name, pp->name);
155 g_topology_assert();
156 gp = g_slice_new(mp, NAPMPART, pp, &cp, &ms, sizeof *ms, g_apple_start);
157 if (gp == NULL)
158 return (NULL);
159 g_topology_unlock();
160 do {
161 if (gp->rank != 2 && insist == 0)
162 break;
163
164 sectorsize = cp->provider->sectorsize;
165 if (sectorsize != 512)
166 break;
167
168 buf = g_read_data(cp, 0, sectorsize, NULL);
169 if (buf == NULL)
170 break;
171
172 /*
173 * Test for the sector 0 driver record signature, and
174 * validate sector and disk size
175 */
176 if (buf[0] != 'E' && buf[1] != 'R') {
177 g_free(buf);
178 break;
179 }
180 g_dec_drvrdesc(buf, ms);
181 g_free(buf);
182
183 if (ms->dd_bsiz != 512) {
184 break;
185 }
186
187 /*
188 * Read in the first partition map
189 */
190 buf = g_read_data(cp, sectorsize, sectorsize, NULL);
191 if (buf == NULL)
192 break;
193
194 /*
195 * Decode the first partition: it's another indication of
196 * validity, as well as giving the size of the partition
197 * map
198 */
199 apm = &ms->apmpart[0];
200 g_dec_apple_partition(buf, apm);
201 g_free(buf);
202
203 if (apm->am_sig[0] != 'P' || apm->am_sig[1] != 'M')
204 break;
205 ms->am_mapcnt0 = apm->am_mapcnt;
206
207 buf = g_read_data(cp, 2 * sectorsize,
208 (NAPMPART - 1) * sectorsize, NULL);
209 if (buf == NULL)
210 break;
211
212 for (i = 1; i < NAPMPART; i++) {
213 g_dec_apple_partition(buf + ((i - 1) * sectorsize),
214 &ms->apmpart[i]);
215 }
216
217 for (i = 0; i < NAPMPART; i++) {
218 apm = &ms->apmpart[i];
219
220 /*
221 * Validate partition sig and global mapcount
222 */
223 if (apm->am_sig[0] != 'P' ||
224 apm->am_sig[1] != 'M')
225 continue;
226 if (apm->am_mapcnt != ms->am_mapcnt0)
227 continue;
228
229 if (bootverbose) {
230 printf("APM Slice %d (%s/%s) on %s:\n",
231 i + 1, apm->am_name, apm->am_type,
232 gp->name);
233 /* g_apple_print(i, dp + i); */
234 }
235 g_topology_lock();
236 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
237 (off_t)apm->am_start << 9ULL,
238 (off_t)apm->am_partcnt << 9ULL,
239 sectorsize,
240 "%ss%d", gp->name, i + 1);
241 g_topology_unlock();
242 }
243 g_free(buf);
244 break;
245 } while(0);
246 g_topology_lock();
247 g_access(cp, -1, 0, 0);
248 if (LIST_EMPTY(&gp->provider)) {
249 g_slice_spoiled(cp);
250 return (NULL);
251 }
252 return (gp);
253 }
254
255
256 static struct g_class g_apple_class = {
257 .name = APPLE_CLASS_NAME,
258 .version = G_VERSION,
259 .taste = g_apple_taste,
260 .dumpconf = g_apple_dumpconf,
261 };
262
263 DECLARE_GEOM_CLASS(g_apple_class, g_apple);
Cache object: 5fbe1f0f9662453d4e7cf6ac1bb2cf86
|