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 $
|
35 * $FreeBSD: head/sys/geom/geom_bsd.c 94287 2002-04-09 15:43:32Z 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)
|
229g_bsd_i386(struct g_slicer *gsp, 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 gsp->frontstuff = 16 * secsize; |
246 return(error); 247} 248 249static int
|
249g_bsd_alpha(struct g_consumer *cp, int secsize, struct disklabel *dl)
|
250g_bsd_alpha(struct g_slicer *gsp, struct g_consumer *cp, int secsize, struct disklabel *dl) |
251{ 252 int error; 253 u_char *buf; 254 255 buf = g_read_data(cp, 0, secsize, &error); 256 if (buf == NULL || error != 0) 257 return(ENOENT); 258 g_bsd_ledec_disklabel(buf + 64, dl); 259 if (dl->d_magic == DISKMAGIC && 260 dl->d_magic2 == DISKMAGIC && 261 g_bsd_lesum(dl, buf + 64) == 0) 262 error = 0; 263 else 264 error = ENOENT; 265 g_free(buf);
|
266 gsp->frontstuff = 16 * secsize; |
267 return(error); 268} 269 270static int 271g_bsd_start(struct bio *bp) 272{ 273 struct g_geom *gp; 274 struct g_bsd_softc *ms; 275 struct g_slicer *gsp; 276 struct g_ioctl *gio; 277 278 gp = bp->bio_to->geom; 279 gsp = gp->softc; 280 ms = gsp->softc; 281 if (strcmp(bp->bio_attribute, "GEOM::ioctl")) 282 return(0); 283 else if (bp->bio_length != sizeof *gio) 284 return(0); 285 gio = (struct g_ioctl *)bp->bio_data; 286 if (gio->cmd == DIOCGDINFO) { 287 bcopy(&ms->inram, gio->data, sizeof ms->inram); 288 bp->bio_error = 0; 289 g_io_deliver(bp); 290 return (1); 291 } 292 return (0); 293} 294 295static void 296g_bsd_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp) 297{ 298#if 0 299 struct g_mbr_softc *ms; 300 struct g_slicer *gsp; 301 302 gsp = gp->softc; 303 ms = gsp->softc; 304 if (pp != NULL) { 305 sbuf_printf(sb, "%s<type>%d</type>\n", 306 indent, ms->type[pp->index]); 307 } 308#endif 309 g_slice_dumpconf(sb, indent, gp, cp, pp); 310} 311 312static struct g_geom * 313g_bsd_taste(struct g_class *mp, struct g_provider *pp, int flags) 314{ 315 struct g_geom *gp; 316 struct g_consumer *cp; 317 struct g_provider *pp2; 318 int error, i, j, npart; 319 struct g_bsd_softc *ms; 320 struct disklabel *dl; 321 u_int secsize; 322 u_int fwsectors, fwheads; 323 off_t mediasize; 324 struct partition *ppp, *ppr;
|
325 struct g_slicer *gsp; |
326 327 g_trace(G_T_TOPOLOGY, "bsd_taste(%s,%s)", mp->name, pp->name); 328 g_topology_assert(); 329 if (flags == G_TF_NORMAL && 330 !strcmp(pp->geom->class->name, BSD_CLASS_NAME)) 331 return (NULL); 332 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_bsd_start); 333 if (gp == NULL) 334 return (NULL);
|
335 gsp = gp->softc; |
336 g_topology_unlock(); 337 gp->dumpconf = g_bsd_dumpconf; 338 npart = 0; 339 while (1) { /* a trick to allow us to use break */
|
336 j = sizeof i;
337 error = g_io_getattr("MBR::type", cp, &j, &i);
|
340 error = g_getattr("MBR::type", cp, &i); |
341 if (!error && i != 165 && flags == G_TF_NORMAL) 342 break;
|
340 j = sizeof secsize;
341 error = g_io_getattr("GEOM::sectorsize", cp, &j, &secsize);
|
343 error = g_getattr("GEOM::sectorsize", cp, &secsize); |
344 if (error) { 345 secsize = 512; 346 printf("g_bsd_taste: error %d Sectors are %d bytes\n", 347 error, secsize); 348 }
|
347 j = sizeof mediasize;
348 error = g_io_getattr("GEOM::mediasize", cp, &j, &mediasize);
|
349 error = g_getattr("GEOM::mediasize", cp, &mediasize); |
350 if (error) { 351 mediasize = 0; 352 printf("g_error %d Mediasize is %lld bytes\n", 353 error, (long long)mediasize); 354 }
|
354 error = g_bsd_i386(cp, secsize, &ms->ondisk);
|
355 error = g_bsd_i386(gsp, cp, secsize, &ms->ondisk); |
356 if (error)
|
356 error = g_bsd_alpha(cp, secsize, &ms->ondisk);
|
357 error = g_bsd_alpha(gsp, cp, secsize, &ms->ondisk); |
358 if (error) 359 break; 360 dl = &ms->ondisk; 361 if (bootverbose) 362 g_hexdump(dl, sizeof(*dl)); 363 if (dl->d_secsize < secsize) 364 break; 365 if (dl->d_secsize > secsize) 366 secsize = dl->d_secsize; 367 ppr = &dl->d_partitions[2]; 368 for (i = 0; i < 8; i++) { 369 ppp = &dl->d_partitions[i]; 370 if (ppp->p_size == 0) 371 continue; 372 npart++; 373 pp2 = g_slice_addslice(gp, i, 374 ((off_t)(ppp->p_offset - ppr->p_offset)) << 9ULL, 375 ((off_t)ppp->p_size) << 9ULL, 376 "%s%c", pp->name, 'a' + i); 377 g_error_provider(pp2, 0); 378 } 379 ondisk2inram(ms); 380 break; 381 } 382 if (npart == 0 && ( 383 (flags == G_TF_INSIST && mediasize != 0) || 384 (flags == G_TF_TRANSPARENT))) { 385 dl = &ms->ondisk; 386 bzero(dl, sizeof *dl); 387 dl->d_magic = DISKMAGIC; 388 dl->d_magic2 = DISKMAGIC; 389 ppp = &dl->d_partitions[RAW_PART]; 390 ppp->p_offset = 0; 391 ppp->p_size = mediasize / secsize; 392 dl->d_npartitions = MAXPARTITIONS; 393 dl->d_interleave = 1; 394 dl->d_secsize = secsize; 395 dl->d_rpm = 3600; 396 j = sizeof fwsectors; 397 error = g_io_getattr("GEOM::fwsectors", cp, &j, &fwsectors); 398 if (error) 399 dl->d_nsectors = 32; 400 else 401 dl->d_nsectors = fwsectors; 402 error = g_io_getattr("GEOM::fwheads", cp, &j, &fwheads); 403 if (error) 404 dl->d_ntracks = 64; 405 else 406 dl->d_ntracks = fwheads; 407 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks; 408 dl->d_ncylinders = ppp->p_size / dl->d_secpercyl; 409 dl->d_secperunit = ppp->p_size; 410 dl->d_checksum = 0; 411 dl->d_checksum = dkcksum(dl); 412 ms->inram = ms->ondisk; 413 pp2 = g_slice_addslice(gp, RAW_PART, 414 0, mediasize, "%s%c", pp->name, 'a' + RAW_PART); 415 g_error_provider(pp2, 0); 416 npart = 1; 417 } 418 g_topology_lock(); 419 error = g_access_rel(cp, -1, 0, 0); 420 if (npart > 0) 421 return (gp); 422 g_std_spoiled(cp); 423 return (NULL); 424} 425 426static struct g_class g_bsd_class = { 427 BSD_CLASS_NAME, 428 g_bsd_taste, 429 NULL, 430 G_CLASS_INITSTUFF 431}; 432 433DECLARE_GEOM_CLASS(g_bsd_class, g_bsd);
|