2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
51 #include <sys/malloc.h>
53 #include <sys/mutex.h>
55 #include <sys/errno.h>
56 #include <sys/disklabel.h>
57 #include <geom/geom.h>
58 #include <geom/geom_slice.h>
60 #define BSD_CLASS_NAME "BSD-class"
63 struct disklabel ondisk;
64 struct disklabel inram;
68 g_bsd_ledec_partition(u_char *ptr, struct partition *d)
70 d->p_size = g_dec_le4(ptr + 0);
71 d->p_offset = g_dec_le4(ptr + 4);
72 d->p_fsize = g_dec_le4(ptr + 8);
73 d->p_fstype = ptr[12];
75 d->p_cpg = g_dec_le2(ptr + 14);
79 g_bsd_ledec_disklabel(u_char *ptr, struct disklabel *d)
81 d->d_magic = g_dec_le4(ptr + 0);
82 d->d_type = g_dec_le2(ptr + 4);
83 d->d_subtype = g_dec_le2(ptr + 6);
84 bcopy(ptr + 8, d->d_typename, 16);
85 bcopy(d->d_packname, ptr + 24, 16);
86 d->d_secsize = g_dec_le4(ptr + 40);
87 d->d_nsectors = g_dec_le4(ptr + 44);
88 d->d_ntracks = g_dec_le4(ptr + 48);
89 d->d_ncylinders = g_dec_le4(ptr + 52);
90 d->d_secpercyl = g_dec_le4(ptr + 56);
91 d->d_secperunit = g_dec_le4(ptr + 60);
92 d->d_sparespertrack = g_dec_le2(ptr + 64);
93 d->d_sparespercyl = g_dec_le2(ptr + 66);
94 d->d_acylinders = g_dec_le4(ptr + 68);
95 d->d_rpm = g_dec_le2(ptr + 72);
96 d->d_interleave = g_dec_le2(ptr + 74);
97 d->d_trackskew = g_dec_le2(ptr + 76);
98 d->d_cylskew = g_dec_le2(ptr + 78);
99 d->d_headswitch = g_dec_le4(ptr + 80);
100 d->d_trkseek = g_dec_le4(ptr + 84);
101 d->d_flags = g_dec_le4(ptr + 88);
102 d->d_drivedata[0] = g_dec_le4(ptr + 92);
103 d->d_drivedata[1] = g_dec_le4(ptr + 96);
104 d->d_drivedata[2] = g_dec_le4(ptr + 100);
105 d->d_drivedata[3] = g_dec_le4(ptr + 104);
106 d->d_drivedata[4] = g_dec_le4(ptr + 108);
107 d->d_spare[0] = g_dec_le4(ptr + 112);
108 d->d_spare[1] = g_dec_le4(ptr + 116);
109 d->d_spare[2] = g_dec_le4(ptr + 120);
110 d->d_spare[3] = g_dec_le4(ptr + 124);
111 d->d_spare[4] = g_dec_le4(ptr + 128);
112 d->d_magic2 = g_dec_le4(ptr + 132);
113 d->d_checksum = g_dec_le2(ptr + 136);
114 d->d_npartitions = g_dec_le2(ptr + 138);
115 d->d_bbsize = g_dec_le4(ptr + 140);
116 d->d_sbsize = g_dec_le4(ptr + 144);
117 g_bsd_ledec_partition(ptr + 148, &d->d_partitions[0]);
118 g_bsd_ledec_partition(ptr + 164, &d->d_partitions[1]);
119 g_bsd_ledec_partition(ptr + 180, &d->d_partitions[2]);
120 g_bsd_ledec_partition(ptr + 196, &d->d_partitions[3]);
121 g_bsd_ledec_partition(ptr + 212, &d->d_partitions[4]);
122 g_bsd_ledec_partition(ptr + 228, &d->d_partitions[5]);
123 g_bsd_ledec_partition(ptr + 244, &d->d_partitions[6]);
124 g_bsd_ledec_partition(ptr + 260, &d->d_partitions[7]);
129 g_bsd_leenc_partition(u_char *ptr, struct partition *d)
131 g_enc_le4(ptr + 0, d->p_size);
132 g_enc_le4(ptr + 4, d->p_offset);
133 g_enc_le4(ptr + 8, d->p_fsize);
134 ptr[12] = d->p_fstype;
136 g_enc_le2(ptr + 14, d->p_cpg);
140 g_bsd_leenc_disklabel(u_char *ptr, struct disklabel *d)
142 g_enc_le4(ptr + 0, d->d_magic);
143 g_enc_le2(ptr + 4, d->d_type);
144 g_enc_le2(ptr + 6, d->d_subtype);
145 bcopy(d->d_typename, ptr + 8, 16);
146 bcopy(d->d_packname, ptr + 24, 16);
147 g_enc_le4(ptr + 40, d->d_secsize);
148 g_enc_le4(ptr + 44, d->d_nsectors);
149 g_enc_le4(ptr + 48, d->d_ntracks);
150 g_enc_le4(ptr + 52, d->d_ncylinders);
151 g_enc_le4(ptr + 56, d->d_secpercyl);
152 g_enc_le4(ptr + 60, d->d_secperunit);
153 g_enc_le2(ptr + 64, d->d_sparespertrack);
154 g_enc_le2(ptr + 66, d->d_sparespercyl);
155 g_enc_le4(ptr + 68, d->d_acylinders);
156 g_enc_le2(ptr + 72, d->d_rpm);
157 g_enc_le2(ptr + 74, d->d_interleave);
158 g_enc_le2(ptr + 76, d->d_trackskew);
159 g_enc_le2(ptr + 78, d->d_cylskew);
160 g_enc_le4(ptr + 80, d->d_headswitch);
161 g_enc_le4(ptr + 84, d->d_trkseek);
162 g_enc_le4(ptr + 88, d->d_flags);
163 g_enc_le4(ptr + 92, d->d_drivedata[0]);
164 g_enc_le4(ptr + 96, d->d_drivedata[1]);
165 g_enc_le4(ptr + 100, d->d_drivedata[2]);
166 g_enc_le4(ptr + 104, d->d_drivedata[3]);
167 g_enc_le4(ptr + 108, d->d_drivedata[4]);
168 g_enc_le4(ptr + 112, d->d_spare[0]);
169 g_enc_le4(ptr + 116, d->d_spare[1]);
170 g_enc_le4(ptr + 120, d->d_spare[2]);
171 g_enc_le4(ptr + 124, d->d_spare[3]);
172 g_enc_le4(ptr + 128, d->d_spare[4]);
173 g_enc_le4(ptr + 132, d->d_magic2);
174 g_enc_le2(ptr + 136, d->d_checksum);
175 g_enc_le2(ptr + 138, d->d_npartitions);
176 g_enc_le4(ptr + 140, d->d_bbsize);
177 g_enc_le4(ptr + 144, d->d_sbsize);
178 g_bsd_leenc_partition(ptr + 148, &d->d_partitions[0]);
179 g_bsd_leenc_partition(ptr + 164, &d->d_partitions[1]);
180 g_bsd_leenc_partition(ptr + 180, &d->d_partitions[2]);
181 g_bsd_leenc_partition(ptr + 196, &d->d_partitions[3]);
182 g_bsd_leenc_partition(ptr + 212, &d->d_partitions[4]);
183 g_bsd_leenc_partition(ptr + 228, &d->d_partitions[5]);
184 g_bsd_leenc_partition(ptr + 244, &d->d_partitions[6]);
185 g_bsd_leenc_partition(ptr + 260, &d->d_partitions[7]);
191 ondisk2inram(struct g_bsd_softc *sc)
193 struct partition *ppp;
197 sc->inram = sc->ondisk;
198 offset = sc->inram.d_partitions[RAW_PART].p_offset;
199 for (i = 0; i < 8; i++) {
200 ppp = &sc->inram.d_partitions[i];
201 if (ppp->p_offset >= offset)
202 ppp->p_offset -= offset;
204 sc->inram.d_checksum = 0;
205 sc->inram.d_checksum = dkcksum(&sc->inram);
209 * It is rather fortunate that this checksum only covers up to the
210 * actual end of actual data, otherwise the pointer-screwup in
211 * alpha architectures would have been much harder to handle.
214 g_bsd_lesum(struct disklabel *dl, u_char *p)
219 pe = p + 148 + 16 * dl->d_npartitions;
229 g_bsd_i386(struct g_consumer *cp, int secsize, struct disklabel *dl)
234 buf = g_read_data(cp, secsize * 1, secsize, &error);
235 if (buf == NULL || error != 0)
237 g_bsd_ledec_disklabel(buf, dl);
238 if (dl->d_magic == DISKMAGIC &&
239 dl->d_magic2 == DISKMAGIC &&
240 g_bsd_lesum(dl, buf) == 0)
249 g_bsd_alpha(struct g_consumer *cp, int secsize, struct disklabel *dl)
254 buf = g_read_data(cp, 0, secsize, &error);
255 if (buf == NULL || error != 0)
257 g_bsd_ledec_disklabel(buf + 64, dl);
258 if (dl->d_magic == DISKMAGIC &&
259 dl->d_magic2 == DISKMAGIC &&
260 g_bsd_lesum(dl, buf) == 0)
269 g_bsd_start(struct bio *bp)
272 struct g_bsd_softc *ms;
273 struct g_slicer *gsp;
276 gp = bp->bio_to->geom;
279 if (strcmp(bp->bio_attribute, "GEOM::ioctl"))
281 else if (bp->bio_length != sizeof *gio)
283 gio = (struct g_ioctl *)bp->bio_data;
284 if (gio->cmd == DIOCGDINFO) {
285 bcopy(&ms->inram, gio->data, sizeof ms->inram);
291 if (gio->cmd == DIOCGPART) {
293 pi.disklab = &ms->inram;
294 pi.part = &ms->inram.d_partitions[bp->bio_to->index];
295 bcopy(&pi, gio->data, sizeof pi);
305 g_bsd_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
308 struct g_mbr_softc *ms;
309 struct g_slicer *gsp;
314 sbuf_printf(sb, "%s<type>%d</type>\n",
315 indent, ms->type[pp->index]);
318 g_slice_dumpconf(sb, indent, gp, cp, pp);
321 static struct g_geom *
322 g_bsd_taste(struct g_class *mp, struct g_provider *pp, int flags)
325 struct g_consumer *cp;
326 struct g_provider *pp2;
327 int error, i, j, npart;
328 struct g_bsd_softc *ms;
329 struct disklabel *dl;
331 u_int fwsectors, fwheads;
333 struct partition *ppp, *ppr;
335 g_trace(G_T_TOPOLOGY, "bsd_taste(%s,%s)", mp->name, pp->name);
337 if (flags == G_TF_NORMAL &&
338 !strcmp(pp->geom->class->name, BSD_CLASS_NAME))
340 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_bsd_start);
344 gp->dumpconf = g_bsd_dumpconf;
346 while (1) { /* a trick to allow us to use break */
348 error = g_io_getattr("MBR::type", cp, &j, &i);
349 if (!error && i != 165 && flags == G_TF_NORMAL)
352 error = g_io_getattr("GEOM::sectorsize", cp, &j, &secsize);
355 printf("g_bsd_taste: error %d Sectors are %d bytes\n",
358 j = sizeof mediasize;
359 error = g_io_getattr("GEOM::mediasize", cp, &j, &mediasize);
362 printf("g_error %d Mediasize is %lld bytes\n",
365 error = g_bsd_i386(cp, secsize, &ms->ondisk);
367 error = g_bsd_alpha(cp, secsize, &ms->ondisk);
372 g_hexdump(dl, sizeof(*dl));
373 if (dl->d_secsize < secsize)
375 if (dl->d_secsize > secsize)
376 secsize = dl->d_secsize;
377 ppr = &dl->d_partitions[2];
378 for (i = 0; i < 8; i++) {
379 ppp = &dl->d_partitions[i];
380 if (ppp->p_size == 0)
383 pp2 = g_slice_addslice(gp, i,
384 ((off_t)(ppp->p_offset - ppr->p_offset)) << 9ULL,
385 ((off_t)ppp->p_size) << 9ULL,
386 "%s%c", pp->name, 'a' + i);
387 g_error_provider(pp2, 0);
393 (flags == G_TF_INSIST && mediasize != 0) ||
394 (flags == G_TF_TRANSPARENT))) {
396 bzero(dl, sizeof *dl);
397 dl->d_magic = DISKMAGIC;
398 dl->d_magic2 = DISKMAGIC;
399 ppp = &dl->d_partitions[RAW_PART];
401 ppp->p_size = mediasize / secsize;
402 dl->d_npartitions = MAXPARTITIONS;
403 dl->d_interleave = 1;
404 dl->d_secsize = secsize;
406 j = sizeof fwsectors;
407 error = g_io_getattr("GEOM::fwsectors", cp, &j, &fwsectors);
411 dl->d_nsectors = fwsectors;
412 error = g_io_getattr("GEOM::fwheads", cp, &j, &fwheads);
416 dl->d_ntracks = fwheads;
417 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
418 dl->d_ncylinders = ppp->p_size / dl->d_secpercyl;
419 dl->d_secperunit = ppp->p_size;
421 dl->d_checksum = dkcksum(dl);
422 ms->inram = ms->ondisk;
423 pp2 = g_slice_addslice(gp, RAW_PART,
424 0, mediasize, "%s%c", pp->name, 'a' + RAW_PART);
425 g_error_provider(pp2, 0);
429 error = g_access_rel(cp, -1, 0, 0);
436 static struct g_class g_bsd_class = {
445 DECLARE_GEOM_CLASS(g_bsd_class, g_bsd);