2 * Copyright (c) 2002 Peter Grehan.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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"
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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>
43 #include <sys/mutex.h>
46 #include <geom/geom.h>
47 #include <geom/geom_slice.h>
49 #define APPLE_CLASS_NAME "APPLE"
51 #define NAPMPART 16 /* Max partitions */
53 struct apm_partition {
62 struct g_apple_softc {
67 struct apm_partition apmpart[NAPMPART];
71 g_dec_drvrdesc(u_char *ptr, struct g_apple_softc *sc)
73 sc->dd_bsiz = be16dec(ptr + 2);
74 sc->dd_blkcnt = be32dec(ptr + 4);
75 sc->dd_drvrcnt = be32dec(ptr + 16);
79 g_dec_apple_partition(u_char *ptr, struct apm_partition *d)
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);
91 g_apple_start(struct bio *bp)
93 struct g_provider *pp;
100 if (bp->bio_cmd == BIO_GETATTR) {
101 if (g_handleattr_off_t(bp, "APM::offset",
102 gsp->slices[pp->index].offset))
109 g_apple_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
110 struct g_consumer *cp __unused, struct g_provider *pp)
112 struct g_apple_softc *mp;
113 struct g_slicer *gsp;
117 g_slice_dumpconf(sb, indent, gp, cp, pp);
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);
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);
143 static struct g_geom *
144 g_apple_taste(struct g_class *mp, struct g_provider *pp, int insist)
147 struct g_consumer *cp;
149 struct g_apple_softc *ms;
150 struct apm_partition *apm;
154 g_trace(G_T_TOPOLOGY, "apple_taste(%s,%s)", mp->name, pp->name);
156 gp = g_slice_new(mp, NAPMPART, pp, &cp, &ms, sizeof *ms, g_apple_start);
160 gp->dumpconf = g_apple_dumpconf;
162 if (gp->rank != 2 && insist == 0)
165 sectorsize = cp->provider->sectorsize;
166 if (sectorsize != 512)
169 buf = g_read_data(cp, 0, sectorsize, &error);
170 if (buf == NULL || error != 0)
174 * Test for the sector 0 driver record signature, and
175 * validate sector and disk size
177 if (buf[0] != 'E' && buf[1] != 'R') {
181 g_dec_drvrdesc(buf, ms);
184 if (ms->dd_bsiz != 512) {
189 * Read in the first partition map
191 buf = g_read_data(cp, sectorsize, sectorsize, &error);
192 if (buf == NULL || error != 0)
196 * Decode the first partition: it's another indication of
197 * validity, as well as giving the size of the partition
200 apm = &ms->apmpart[0];
201 g_dec_apple_partition(buf, apm);
204 if (apm->am_sig[0] != 'P' || apm->am_sig[1] != 'M')
206 ms->am_mapcnt0 = apm->am_mapcnt;
208 buf = g_read_data(cp, 2 * sectorsize,
209 (NAPMPART - 1) * sectorsize, &error);
210 if (buf == NULL || error != 0)
213 for (i = 1; i < NAPMPART; i++) {
214 g_dec_apple_partition(buf + ((i - 1) * sectorsize),
218 for (i = 0; i < NAPMPART; i++) {
219 apm = &ms->apmpart[i];
222 * Validate partition sig and global mapcount
224 if (apm->am_sig[0] != 'P' ||
225 apm->am_sig[1] != 'M')
227 if (apm->am_mapcnt != ms->am_mapcnt0)
231 printf("APM Slice %d (%s/%s) on %s:\n",
232 i + 1, apm->am_name, apm->am_type,
234 /* g_apple_print(i, dp + i); */
237 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
238 (off_t)apm->am_start << 9ULL,
239 (off_t)apm->am_partcnt << 9ULL,
241 "%ss%d", gp->name, i + 1);
248 g_access(cp, -1, 0, 0);
249 if (LIST_EMPTY(&gp->provider)) {
257 static struct g_class g_apple_class = {
258 .name = APPLE_CLASS_NAME,
259 .taste = g_apple_taste,
262 DECLARE_GEOM_CLASS(g_apple_class, g_apple);