1 /*- 2 * Copyright (c) 2007 Marcel Moolenaar 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 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/bio.h> 32 #include <sys/disklabel.h> 33 #include <sys/endian.h> 34 #include <sys/kernel.h> 35 #include <sys/kobj.h> 36 #include <sys/limits.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/mutex.h> 40 #include <sys/queue.h> 41 #include <sys/sbuf.h> 42 #include <sys/systm.h> 43 #include <geom/geom.h> 44 #include <geom/part/g_part.h> 45 46 #include "g_part_if.h" 47 48 struct g_part_bsd_table { 49 struct g_part_table base; 50 u_char *bbarea; 51 uint32_t offset; 52 }; 53 54 struct g_part_bsd_entry { 55 struct g_part_entry base; 56 struct partition part; 57 }; 58 59 static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *, 60 struct g_part_parms *); 61 static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *); 62 static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *); 63 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *); 64 static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *, 65 struct sbuf *, const char *); 66 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *); 67 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *, 68 struct g_part_parms *); 69 static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *, 70 char *, size_t); 71 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *); 72 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *); 73 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *, 74 char *, size_t); 75 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *); 76 static int g_part_bsd_resize(struct g_part_table *, struct g_part_entry *, 77 struct g_part_parms *); 78 79 static kobj_method_t g_part_bsd_methods[] = { 80 KOBJMETHOD(g_part_add, g_part_bsd_add), 81 KOBJMETHOD(g_part_bootcode, g_part_bsd_bootcode), 82 KOBJMETHOD(g_part_create, g_part_bsd_create), 83 KOBJMETHOD(g_part_destroy, g_part_bsd_destroy), 84 KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf), 85 KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto), 86 KOBJMETHOD(g_part_modify, g_part_bsd_modify), 87 KOBJMETHOD(g_part_resize, g_part_bsd_resize), 88 KOBJMETHOD(g_part_name, g_part_bsd_name), 89 KOBJMETHOD(g_part_probe, g_part_bsd_probe), 90 KOBJMETHOD(g_part_read, g_part_bsd_read), 91 KOBJMETHOD(g_part_type, g_part_bsd_type), 92 KOBJMETHOD(g_part_write, g_part_bsd_write), 93 { 0, 0 } 94 }; 95 96 static struct g_part_scheme g_part_bsd_scheme = { 97 "BSD", 98 g_part_bsd_methods, 99 sizeof(struct g_part_bsd_table), 100 .gps_entrysz = sizeof(struct g_part_bsd_entry), 101 .gps_minent = 8, 102 .gps_maxent = 20, /* Only 22 entries fit in 512 byte sectors */ 103 .gps_bootcodesz = BBSIZE, 104 }; 105 G_PART_SCHEME_DECLARE(g_part_bsd); 106 107 static int 108 bsd_parse_type(const char *type, uint8_t *fstype) 109 { 110 const char *alias; 111 char *endp; 112 long lt; 113 114 if (type[0] == '!') { 115 lt = strtol(type + 1, &endp, 0); 116 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256) 117 return (EINVAL); 118 *fstype = (u_int)lt; 119 return (0); 120 } 121 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP); 122 if (!strcasecmp(type, alias)) { 123 *fstype = FS_SWAP; 124 return (0); 125 } 126 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS); 127 if (!strcasecmp(type, alias)) { 128 *fstype = FS_BSDFFS; 129 return (0); 130 } 131 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM); 132 if (!strcasecmp(type, alias)) { 133 *fstype = FS_VINUM; 134 return (0); 135 } 136 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS); 137 if (!strcasecmp(type, alias)) { 138 *fstype = FS_ZFS; 139 return (0); 140 } 141 return (EINVAL); 142 } 143 144 static int 145 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 146 struct g_part_parms *gpp) 147 { 148 struct g_part_bsd_entry *entry; 149 struct g_part_bsd_table *table; 150 151 if (gpp->gpp_parms & G_PART_PARM_LABEL) 152 return (EINVAL); 153 154 entry = (struct g_part_bsd_entry *)baseentry; 155 table = (struct g_part_bsd_table *)basetable; 156 157 entry->part.p_size = gpp->gpp_size; 158 entry->part.p_offset = gpp->gpp_start + table->offset; 159 entry->part.p_fsize = 0; 160 entry->part.p_frag = 0; 161 entry->part.p_cpg = 0; 162 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype)); 163 } 164 165 static int 166 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 167 { 168 struct g_part_bsd_table *table; 169 const u_char *codeptr; 170 size_t hdsz, tlsz; 171 size_t codesz, tlofs; 172 173 hdsz = 512; 174 tlofs = hdsz + 148 + basetable->gpt_entries * 16; 175 tlsz = BBSIZE - tlofs; 176 table = (struct g_part_bsd_table *)basetable; 177 bzero(table->bbarea, hdsz); 178 bzero(table->bbarea + tlofs, tlsz); 179 codeptr = gpp->gpp_codeptr; 180 codesz = MIN(hdsz, gpp->gpp_codesize); 181 if (codesz > 0) 182 bcopy(codeptr, table->bbarea, codesz); 183 codesz = MIN(tlsz, gpp->gpp_codesize - tlofs); 184 if (codesz > 0) 185 bcopy(codeptr + tlofs, table->bbarea + tlofs, codesz); 186 return (0); 187 } 188 189 static int 190 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp) 191 { 192 struct g_provider *pp; 193 struct g_part_entry *baseentry; 194 struct g_part_bsd_entry *entry; 195 struct g_part_bsd_table *table; 196 u_char *ptr; 197 uint32_t msize, ncyls, secpercyl; 198 199 pp = gpp->gpp_provider; 200 201 if (pp->sectorsize < sizeof(struct disklabel)) 202 return (ENOSPC); 203 if (BBSIZE % pp->sectorsize) 204 return (ENOTBLK); 205 206 msize = MIN(pp->mediasize / pp->sectorsize, 0xffffffff); 207 secpercyl = basetable->gpt_sectors * basetable->gpt_heads; 208 ncyls = msize / secpercyl; 209 210 table = (struct g_part_bsd_table *)basetable; 211 table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO); 212 ptr = table->bbarea + pp->sectorsize; 213 214 le32enc(ptr + 0, DISKMAGIC); /* d_magic */ 215 le32enc(ptr + 40, pp->sectorsize); /* d_secsize */ 216 le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */ 217 le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */ 218 le32enc(ptr + 52, ncyls); /* d_ncylinders */ 219 le32enc(ptr + 56, secpercyl); /* d_secpercyl */ 220 le32enc(ptr + 60, msize); /* d_secperunit */ 221 le16enc(ptr + 72, 3600); /* d_rpm */ 222 le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */ 223 le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */ 224 le32enc(ptr + 140, BBSIZE); /* d_bbsize */ 225 226 basetable->gpt_first = 0; 227 basetable->gpt_last = msize - 1; 228 basetable->gpt_isleaf = 1; 229 230 baseentry = g_part_new_entry(basetable, RAW_PART + 1, 231 basetable->gpt_first, basetable->gpt_last); 232 baseentry->gpe_internal = 1; 233 entry = (struct g_part_bsd_entry *)baseentry; 234 entry->part.p_size = basetable->gpt_last + 1; 235 entry->part.p_offset = table->offset; 236 237 return (0); 238 } 239 240 static int 241 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 242 { 243 struct g_part_bsd_table *table; 244 245 table = (struct g_part_bsd_table *)basetable; 246 if (table->bbarea != NULL) 247 g_free(table->bbarea); 248 table->bbarea = NULL; 249 250 /* Wipe the second sector to clear the partitioning. */ 251 basetable->gpt_smhead |= 2; 252 return (0); 253 } 254 255 static void 256 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 257 struct sbuf *sb, const char *indent) 258 { 259 struct g_part_bsd_entry *entry; 260 261 entry = (struct g_part_bsd_entry *)baseentry; 262 if (indent == NULL) { 263 /* conftxt: libdisk compatibility */ 264 sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype); 265 } else if (entry != NULL) { 266 /* confxml: partition entry information */ 267 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent, 268 entry->part.p_fstype); 269 } else { 270 /* confxml: scheme information */ 271 } 272 } 273 274 static int 275 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 276 { 277 struct g_part_bsd_entry *entry; 278 279 /* Allow dumping to a swap partition or an unused partition. */ 280 entry = (struct g_part_bsd_entry *)baseentry; 281 return ((entry->part.p_fstype == FS_UNUSED || 282 entry->part.p_fstype == FS_SWAP) ? 1 : 0); 283 } 284 285 static int 286 g_part_bsd_modify(struct g_part_table *basetable, 287 struct g_part_entry *baseentry, struct g_part_parms *gpp) 288 { 289 struct g_part_bsd_entry *entry; 290 291 if (gpp->gpp_parms & G_PART_PARM_LABEL) 292 return (EINVAL); 293 294 entry = (struct g_part_bsd_entry *)baseentry; 295 if (gpp->gpp_parms & G_PART_PARM_TYPE) 296 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype)); 297 return (0); 298 } 299 300 static int 301 g_part_bsd_resize(struct g_part_table *basetable, 302 struct g_part_entry *baseentry, struct g_part_parms *gpp) 303 { 304 struct g_part_bsd_entry *entry; 305 306 entry = (struct g_part_bsd_entry *)baseentry; 307 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1; 308 entry->part.p_size = gpp->gpp_size; 309 310 return (0); 311 } 312 313 static const char * 314 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry, 315 char *buf, size_t bufsz) 316 { 317 318 snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1); 319 return (buf); 320 } 321 322 static int 323 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp) 324 { 325 struct g_provider *pp; 326 u_char *buf; 327 uint32_t magic1, magic2; 328 int error; 329 330 pp = cp->provider; 331 332 /* Sanity-check the provider. */ 333 if (pp->sectorsize < sizeof(struct disklabel) || 334 pp->mediasize < BBSIZE) 335 return (ENOSPC); 336 if (BBSIZE % pp->sectorsize) 337 return (ENOTBLK); 338 339 /* Check that there's a disklabel. */ 340 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 341 if (buf == NULL) 342 return (error); 343 magic1 = le32dec(buf + 0); 344 magic2 = le32dec(buf + 132); 345 g_free(buf); 346 return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC) 347 ? G_PART_PROBE_PRI_HIGH : ENXIO); 348 } 349 350 static int 351 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp) 352 { 353 struct g_provider *pp; 354 struct g_part_bsd_table *table; 355 struct g_part_entry *baseentry; 356 struct g_part_bsd_entry *entry; 357 struct partition part; 358 u_char *buf, *p; 359 off_t chs, msize; 360 u_int sectors, heads; 361 int error, index; 362 363 pp = cp->provider; 364 table = (struct g_part_bsd_table *)basetable; 365 msize = pp->mediasize / pp->sectorsize; 366 367 table->bbarea = g_read_data(cp, 0, BBSIZE, &error); 368 if (table->bbarea == NULL) 369 return (error); 370 371 buf = table->bbarea + pp->sectorsize; 372 373 if (le32dec(buf + 40) != pp->sectorsize) 374 goto invalid_label; 375 sectors = le32dec(buf + 44); 376 if (sectors < 1 || sectors > 255) 377 goto invalid_label; 378 if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) { 379 g_part_geometry_heads(msize, sectors, &chs, &heads); 380 if (chs != 0) { 381 basetable->gpt_sectors = sectors; 382 basetable->gpt_heads = heads; 383 } 384 } 385 heads = le32dec(buf + 48); 386 if (heads < 1 || heads > 255) 387 goto invalid_label; 388 if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom) 389 basetable->gpt_heads = heads; 390 if (sectors != basetable->gpt_sectors || heads != basetable->gpt_heads) 391 printf("GEOM: %s: geometry does not match label" 392 " (%uh,%us != %uh,%us).\n", pp->name, heads, sectors, 393 basetable->gpt_heads, basetable->gpt_sectors); 394 395 chs = le32dec(buf + 60); 396 if (chs < 1) 397 goto invalid_label; 398 /* Fix-up a sysinstall bug. */ 399 if (chs > msize) { 400 chs = msize; 401 le32enc(buf + 60, msize); 402 } 403 if (chs != msize) 404 printf("GEOM: %s: media size does not match label.\n", 405 pp->name); 406 407 basetable->gpt_first = 0; 408 basetable->gpt_last = msize - 1; 409 basetable->gpt_isleaf = 1; 410 411 basetable->gpt_entries = le16dec(buf + 138); 412 if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent || 413 basetable->gpt_entries > g_part_bsd_scheme.gps_maxent) 414 goto invalid_label; 415 416 table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4); 417 for (index = basetable->gpt_entries - 1; index >= 0; index--) { 418 p = buf + 148 + index * 16; 419 part.p_size = le32dec(p + 0); 420 part.p_offset = le32dec(p + 4); 421 part.p_fsize = le32dec(p + 8); 422 part.p_fstype = p[12]; 423 part.p_frag = p[13]; 424 part.p_cpg = le16dec(p + 14); 425 if (part.p_size == 0) 426 continue; 427 if (part.p_offset < table->offset) 428 continue; 429 baseentry = g_part_new_entry(basetable, index + 1, 430 part.p_offset - table->offset, 431 part.p_offset - table->offset + part.p_size - 1); 432 entry = (struct g_part_bsd_entry *)baseentry; 433 entry->part = part; 434 if (index == RAW_PART) 435 baseentry->gpe_internal = 1; 436 } 437 438 return (0); 439 440 invalid_label: 441 printf("GEOM: %s: invalid disklabel.\n", pp->name); 442 g_free(table->bbarea); 443 return (EINVAL); 444 } 445 446 static const char * 447 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 448 char *buf, size_t bufsz) 449 { 450 struct g_part_bsd_entry *entry; 451 int type; 452 453 entry = (struct g_part_bsd_entry *)baseentry; 454 type = entry->part.p_fstype; 455 if (type == FS_SWAP) 456 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP)); 457 if (type == FS_BSDFFS) 458 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS)); 459 if (type == FS_VINUM) 460 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM)); 461 if (type == FS_ZFS) 462 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS)); 463 snprintf(buf, bufsz, "!%d", type); 464 return (buf); 465 } 466 467 static int 468 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp) 469 { 470 struct g_provider *pp; 471 struct g_part_entry *baseentry; 472 struct g_part_bsd_entry *entry; 473 struct g_part_bsd_table *table; 474 uint16_t sum; 475 u_char *label, *p, *pe; 476 int error, index; 477 478 pp = cp->provider; 479 table = (struct g_part_bsd_table *)basetable; 480 baseentry = LIST_FIRST(&basetable->gpt_entry); 481 label = table->bbarea + pp->sectorsize; 482 for (index = 1; index <= basetable->gpt_entries; index++) { 483 p = label + 148 + (index - 1) * 16; 484 entry = (baseentry != NULL && index == baseentry->gpe_index) 485 ? (struct g_part_bsd_entry *)baseentry : NULL; 486 if (entry != NULL && !baseentry->gpe_deleted) { 487 le32enc(p + 0, entry->part.p_size); 488 le32enc(p + 4, entry->part.p_offset); 489 le32enc(p + 8, entry->part.p_fsize); 490 p[12] = entry->part.p_fstype; 491 p[13] = entry->part.p_frag; 492 le16enc(p + 14, entry->part.p_cpg); 493 } else 494 bzero(p, 16); 495 496 if (entry != NULL) 497 baseentry = LIST_NEXT(baseentry, gpe_entry); 498 } 499 500 /* Calculate checksum. */ 501 le16enc(label + 136, 0); 502 pe = label + 148 + basetable->gpt_entries * 16; 503 sum = 0; 504 for (p = label; p < pe; p += 2) 505 sum ^= le16dec(p); 506 le16enc(label + 136, sum); 507 508 error = g_write_data(cp, 0, table->bbarea, BBSIZE); 509 return (error); 510 } 511