1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
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.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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
21 * permission.
22 *
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
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: head/sys/geom/geom_bsd.c 93776 2002-04-04 09:54:13Z phk $
36 */
37
38
39#include <sys/param.h>
40#ifndef _KERNEL
41#include <stdio.h>
42#include <string.h>
43#include <stdlib.h>
44#include <signal.h>
45#include <err.h>
46#else
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/conf.h>
50#include <sys/bio.h>
51#include <sys/malloc.h>
52#include <sys/lock.h>
53#include <sys/mutex.h>
54#endif
55#include <sys/errno.h>
56#include <sys/disklabel.h>
57#include <geom/geom.h>
58#include <geom/geom_slice.h>
59
60#define BSD_CLASS_NAME "BSD-class"
61
62struct g_bsd_softc {
63 struct disklabel ondisk;
64 struct disklabel inram;
65};
66
67static void
68g_bsd_ledec_partition(u_char *ptr, struct partition *d)
69{
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];
74 d->p_frag = ptr[13];
75 d->p_cpg = g_dec_le2(ptr + 14);
76}
77
78static void
79g_bsd_ledec_disklabel(u_char *ptr, struct disklabel *d)
80{
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]);
125}
126
127#if 0
128static void
129g_bsd_leenc_partition(u_char *ptr, struct partition *d)
130{
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;
135 ptr[13] = d->p_frag;
136 g_enc_le2(ptr + 14, d->p_cpg);
137}
138
139static void
140g_bsd_leenc_disklabel(u_char *ptr, struct disklabel *d)
141{
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]);
186}
187
188#endif
189
190static void
191ondisk2inram(struct g_bsd_softc *sc)
192{
193 struct partition *ppp;
194 unsigned offset;
195 int i;
196
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;
203 }
204 sc->inram.d_checksum = 0;
205 sc->inram.d_checksum = dkcksum(&sc->inram);
206}
207
208/*
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.
212 */
213static int
214g_bsd_lesum(struct disklabel *dl, u_char *p)
215{
216 u_char *pe;
217 uint16_t sum;
218
219 pe = p + 148 + 16 * dl->d_npartitions;
220 sum = 0;
221 while (p < pe) {
222 sum ^= g_dec_le2(p);
223 p += 2;
224 }
225 return (sum);
226}
227
228static int
229g_bsd_i386(struct g_consumer *cp, int secsize, struct disklabel *dl)
230{
231 int error;
232 u_char *buf;
233
234 buf = g_read_data(cp, secsize * 1, secsize, &error);
235 if (buf == NULL || error != 0)
236 return(ENOENT);
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)
241 error = 0;
242 else
243 error = ENOENT;
244 g_free(buf);
245 return(error);
246}
247
248static int
249g_bsd_alpha(struct g_consumer *cp, int secsize, struct disklabel *dl)
250{
251 int error;
252 u_char *buf;
253
254 buf = g_read_data(cp, 0, secsize, &error);
255 if (buf == NULL || error != 0)
256 return(ENOENT);
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 + 64) == 0)
261 error = 0;
262 else
263 error = ENOENT;
264 g_free(buf);
265 return(error);
266}
267
268static int
269g_bsd_start(struct bio *bp)
270{
271 struct g_geom *gp;
272 struct g_bsd_softc *ms;
273 struct g_slicer *gsp;
274 struct g_ioctl *gio;
275
276 gp = bp->bio_to->geom;
277 gsp = gp->softc;
278 ms = gsp->softc;
279 if (strcmp(bp->bio_attribute, "GEOM::ioctl"))
280 return(0);
281 else if (bp->bio_length != sizeof *gio)
282 return(0);
283 gio = (struct g_ioctl *)bp->bio_data;
284 if (gio->cmd == DIOCGDINFO) {
285 bcopy(&ms->inram, gio->data, sizeof ms->inram);
286 bp->bio_error = 0;
287 g_io_deliver(bp);
288 return (1);
289 }
290 return (0);
291}
292
293static void
294g_bsd_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
295{
296#if 0
297 struct g_mbr_softc *ms;
298 struct g_slicer *gsp;
299
300 gsp = gp->softc;
301 ms = gsp->softc;
302 if (pp != NULL) {
303 sbuf_printf(sb, "%s<type>%d</type>\n",
304 indent, ms->type[pp->index]);
305 }
306#endif
307 g_slice_dumpconf(sb, indent, gp, cp, pp);
308}
309
310static struct g_geom *
311g_bsd_taste(struct g_class *mp, struct g_provider *pp, int flags)
312{
313 struct g_geom *gp;
314 struct g_consumer *cp;
315 struct g_provider *pp2;
316 int error, i, j, npart;
317 struct g_bsd_softc *ms;
318 struct disklabel *dl;
319 u_int secsize;
320 u_int fwsectors, fwheads;
321 off_t mediasize;
322 struct partition *ppp, *ppr;
323
324 g_trace(G_T_TOPOLOGY, "bsd_taste(%s,%s)", mp->name, pp->name);
325 g_topology_assert();
326 if (flags == G_TF_NORMAL &&
327 !strcmp(pp->geom->class->name, BSD_CLASS_NAME))
328 return (NULL);
329 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_bsd_start);
330 if (gp == NULL)
331 return (NULL);
332 g_topology_unlock();
333 gp->dumpconf = g_bsd_dumpconf;
334 npart = 0;
335 while (1) { /* a trick to allow us to use break */
336 j = sizeof i;
337 error = g_io_getattr("MBR::type", cp, &j, &i);
338 if (!error && i != 165 && flags == G_TF_NORMAL)
339 break;
340 j = sizeof secsize;
341 error = g_io_getattr("GEOM::sectorsize", cp, &j, &secsize);
342 if (error) {
343 secsize = 512;
344 printf("g_bsd_taste: error %d Sectors are %d bytes\n",
345 error, secsize);
346 }
347 j = sizeof mediasize;
348 error = g_io_getattr("GEOM::mediasize", cp, &j, &mediasize);
349 if (error) {
350 mediasize = 0;
351 printf("g_error %d Mediasize is %lld bytes\n",
352 error, (long long)mediasize);
353 }
354 error = g_bsd_i386(cp, secsize, &ms->ondisk);
355 if (error)
356 error = g_bsd_alpha(cp, secsize, &ms->ondisk);
357 if (error)
358 break;
359 dl = &ms->ondisk;
360 if (bootverbose)
361 g_hexdump(dl, sizeof(*dl));
362 if (dl->d_secsize < secsize)
363 break;
364 if (dl->d_secsize > secsize)
365 secsize = dl->d_secsize;
366 ppr = &dl->d_partitions[2];
367 for (i = 0; i < 8; i++) {
368 ppp = &dl->d_partitions[i];
369 if (ppp->p_size == 0)
370 continue;
371 npart++;
372 pp2 = g_slice_addslice(gp, i,
373 ((off_t)(ppp->p_offset - ppr->p_offset)) << 9ULL,
374 ((off_t)ppp->p_size) << 9ULL,
375 "%s%c", pp->name, 'a' + i);
376 g_error_provider(pp2, 0);
377 }
378 ondisk2inram(ms);
379 break;
380 }
381 if (npart == 0 && (
382 (flags == G_TF_INSIST && mediasize != 0) ||
383 (flags == G_TF_TRANSPARENT))) {
384 dl = &ms->ondisk;
385 bzero(dl, sizeof *dl);
386 dl->d_magic = DISKMAGIC;
387 dl->d_magic2 = DISKMAGIC;
388 ppp = &dl->d_partitions[RAW_PART];
389 ppp->p_offset = 0;
390 ppp->p_size = mediasize / secsize;
391 dl->d_npartitions = MAXPARTITIONS;
392 dl->d_interleave = 1;
393 dl->d_secsize = secsize;
394 dl->d_rpm = 3600;
395 j = sizeof fwsectors;
396 error = g_io_getattr("GEOM::fwsectors", cp, &j, &fwsectors);
397 if (error)
398 dl->d_nsectors = 32;
399 else
400 dl->d_nsectors = fwsectors;
401 error = g_io_getattr("GEOM::fwheads", cp, &j, &fwheads);
402 if (error)
403 dl->d_ntracks = 64;
404 else
405 dl->d_ntracks = fwheads;
406 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
407 dl->d_ncylinders = ppp->p_size / dl->d_secpercyl;
408 dl->d_secperunit = ppp->p_size;
409 dl->d_checksum = 0;
410 dl->d_checksum = dkcksum(dl);
411 ms->inram = ms->ondisk;
412 pp2 = g_slice_addslice(gp, RAW_PART,
413 0, mediasize, "%s%c", pp->name, 'a' + RAW_PART);
414 g_error_provider(pp2, 0);
415 npart = 1;
416 }
417 g_topology_lock();
418 error = g_access_rel(cp, -1, 0, 0);
419 if (npart > 0)
420 return (gp);
421 g_std_spoiled(cp);
422 return (NULL);
423}
424
425static struct g_class g_bsd_class = {
426 BSD_CLASS_NAME,
427 g_bsd_taste,
428 NULL,
429 G_CLASS_INITSTUFF
430};
431
432DECLARE_GEOM_CLASS(g_bsd_class, g_bsd);
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
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.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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
21 * permission.
22 *
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
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: head/sys/geom/geom_bsd.c 93776 2002-04-04 09:54:13Z phk $
36 */
37
38
39#include <sys/param.h>
40#ifndef _KERNEL
41#include <stdio.h>
42#include <string.h>
43#include <stdlib.h>
44#include <signal.h>
45#include <err.h>
46#else
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/conf.h>
50#include <sys/bio.h>
51#include <sys/malloc.h>
52#include <sys/lock.h>
53#include <sys/mutex.h>
54#endif
55#include <sys/errno.h>
56#include <sys/disklabel.h>
57#include <geom/geom.h>
58#include <geom/geom_slice.h>
59
60#define BSD_CLASS_NAME "BSD-class"
61
62struct g_bsd_softc {
63 struct disklabel ondisk;
64 struct disklabel inram;
65};
66
67static void
68g_bsd_ledec_partition(u_char *ptr, struct partition *d)
69{
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];
74 d->p_frag = ptr[13];
75 d->p_cpg = g_dec_le2(ptr + 14);
76}
77
78static void
79g_bsd_ledec_disklabel(u_char *ptr, struct disklabel *d)
80{
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]);
125}
126
127#if 0
128static void
129g_bsd_leenc_partition(u_char *ptr, struct partition *d)
130{
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;
135 ptr[13] = d->p_frag;
136 g_enc_le2(ptr + 14, d->p_cpg);
137}
138
139static void
140g_bsd_leenc_disklabel(u_char *ptr, struct disklabel *d)
141{
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]);
186}
187
188#endif
189
190static void
191ondisk2inram(struct g_bsd_softc *sc)
192{
193 struct partition *ppp;
194 unsigned offset;
195 int i;
196
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;
203 }
204 sc->inram.d_checksum = 0;
205 sc->inram.d_checksum = dkcksum(&sc->inram);
206}
207
208/*
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.
212 */
213static int
214g_bsd_lesum(struct disklabel *dl, u_char *p)
215{
216 u_char *pe;
217 uint16_t sum;
218
219 pe = p + 148 + 16 * dl->d_npartitions;
220 sum = 0;
221 while (p < pe) {
222 sum ^= g_dec_le2(p);
223 p += 2;
224 }
225 return (sum);
226}
227
228static int
229g_bsd_i386(struct g_consumer *cp, int secsize, struct disklabel *dl)
230{
231 int error;
232 u_char *buf;
233
234 buf = g_read_data(cp, secsize * 1, secsize, &error);
235 if (buf == NULL || error != 0)
236 return(ENOENT);
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)
241 error = 0;
242 else
243 error = ENOENT;
244 g_free(buf);
245 return(error);
246}
247
248static int
249g_bsd_alpha(struct g_consumer *cp, int secsize, struct disklabel *dl)
250{
251 int error;
252 u_char *buf;
253
254 buf = g_read_data(cp, 0, secsize, &error);
255 if (buf == NULL || error != 0)
256 return(ENOENT);
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 + 64) == 0)
261 error = 0;
262 else
263 error = ENOENT;
264 g_free(buf);
265 return(error);
266}
267
268static int
269g_bsd_start(struct bio *bp)
270{
271 struct g_geom *gp;
272 struct g_bsd_softc *ms;
273 struct g_slicer *gsp;
274 struct g_ioctl *gio;
275
276 gp = bp->bio_to->geom;
277 gsp = gp->softc;
278 ms = gsp->softc;
279 if (strcmp(bp->bio_attribute, "GEOM::ioctl"))
280 return(0);
281 else if (bp->bio_length != sizeof *gio)
282 return(0);
283 gio = (struct g_ioctl *)bp->bio_data;
284 if (gio->cmd == DIOCGDINFO) {
285 bcopy(&ms->inram, gio->data, sizeof ms->inram);
286 bp->bio_error = 0;
287 g_io_deliver(bp);
288 return (1);
289 }
290 return (0);
291}
292
293static void
294g_bsd_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
295{
296#if 0
297 struct g_mbr_softc *ms;
298 struct g_slicer *gsp;
299
300 gsp = gp->softc;
301 ms = gsp->softc;
302 if (pp != NULL) {
303 sbuf_printf(sb, "%s<type>%d</type>\n",
304 indent, ms->type[pp->index]);
305 }
306#endif
307 g_slice_dumpconf(sb, indent, gp, cp, pp);
308}
309
310static struct g_geom *
311g_bsd_taste(struct g_class *mp, struct g_provider *pp, int flags)
312{
313 struct g_geom *gp;
314 struct g_consumer *cp;
315 struct g_provider *pp2;
316 int error, i, j, npart;
317 struct g_bsd_softc *ms;
318 struct disklabel *dl;
319 u_int secsize;
320 u_int fwsectors, fwheads;
321 off_t mediasize;
322 struct partition *ppp, *ppr;
323
324 g_trace(G_T_TOPOLOGY, "bsd_taste(%s,%s)", mp->name, pp->name);
325 g_topology_assert();
326 if (flags == G_TF_NORMAL &&
327 !strcmp(pp->geom->class->name, BSD_CLASS_NAME))
328 return (NULL);
329 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_bsd_start);
330 if (gp == NULL)
331 return (NULL);
332 g_topology_unlock();
333 gp->dumpconf = g_bsd_dumpconf;
334 npart = 0;
335 while (1) { /* a trick to allow us to use break */
336 j = sizeof i;
337 error = g_io_getattr("MBR::type", cp, &j, &i);
338 if (!error && i != 165 && flags == G_TF_NORMAL)
339 break;
340 j = sizeof secsize;
341 error = g_io_getattr("GEOM::sectorsize", cp, &j, &secsize);
342 if (error) {
343 secsize = 512;
344 printf("g_bsd_taste: error %d Sectors are %d bytes\n",
345 error, secsize);
346 }
347 j = sizeof mediasize;
348 error = g_io_getattr("GEOM::mediasize", cp, &j, &mediasize);
349 if (error) {
350 mediasize = 0;
351 printf("g_error %d Mediasize is %lld bytes\n",
352 error, (long long)mediasize);
353 }
354 error = g_bsd_i386(cp, secsize, &ms->ondisk);
355 if (error)
356 error = g_bsd_alpha(cp, secsize, &ms->ondisk);
357 if (error)
358 break;
359 dl = &ms->ondisk;
360 if (bootverbose)
361 g_hexdump(dl, sizeof(*dl));
362 if (dl->d_secsize < secsize)
363 break;
364 if (dl->d_secsize > secsize)
365 secsize = dl->d_secsize;
366 ppr = &dl->d_partitions[2];
367 for (i = 0; i < 8; i++) {
368 ppp = &dl->d_partitions[i];
369 if (ppp->p_size == 0)
370 continue;
371 npart++;
372 pp2 = g_slice_addslice(gp, i,
373 ((off_t)(ppp->p_offset - ppr->p_offset)) << 9ULL,
374 ((off_t)ppp->p_size) << 9ULL,
375 "%s%c", pp->name, 'a' + i);
376 g_error_provider(pp2, 0);
377 }
378 ondisk2inram(ms);
379 break;
380 }
381 if (npart == 0 && (
382 (flags == G_TF_INSIST && mediasize != 0) ||
383 (flags == G_TF_TRANSPARENT))) {
384 dl = &ms->ondisk;
385 bzero(dl, sizeof *dl);
386 dl->d_magic = DISKMAGIC;
387 dl->d_magic2 = DISKMAGIC;
388 ppp = &dl->d_partitions[RAW_PART];
389 ppp->p_offset = 0;
390 ppp->p_size = mediasize / secsize;
391 dl->d_npartitions = MAXPARTITIONS;
392 dl->d_interleave = 1;
393 dl->d_secsize = secsize;
394 dl->d_rpm = 3600;
395 j = sizeof fwsectors;
396 error = g_io_getattr("GEOM::fwsectors", cp, &j, &fwsectors);
397 if (error)
398 dl->d_nsectors = 32;
399 else
400 dl->d_nsectors = fwsectors;
401 error = g_io_getattr("GEOM::fwheads", cp, &j, &fwheads);
402 if (error)
403 dl->d_ntracks = 64;
404 else
405 dl->d_ntracks = fwheads;
406 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
407 dl->d_ncylinders = ppp->p_size / dl->d_secpercyl;
408 dl->d_secperunit = ppp->p_size;
409 dl->d_checksum = 0;
410 dl->d_checksum = dkcksum(dl);
411 ms->inram = ms->ondisk;
412 pp2 = g_slice_addslice(gp, RAW_PART,
413 0, mediasize, "%s%c", pp->name, 'a' + RAW_PART);
414 g_error_provider(pp2, 0);
415 npart = 1;
416 }
417 g_topology_lock();
418 error = g_access_rel(cp, -1, 0, 0);
419 if (npart > 0)
420 return (gp);
421 g_std_spoiled(cp);
422 return (NULL);
423}
424
425static struct g_class g_bsd_class = {
426 BSD_CLASS_NAME,
427 g_bsd_taste,
428 NULL,
429 G_CLASS_INITSTUFF
430};
431
432DECLARE_GEOM_CLASS(g_bsd_class, g_bsd);