1 /*- 2 * Copyright (c) 2006-2008 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/apm.h> 32 #include <sys/bio.h> 33 #include <sys/diskmbr.h> 34 #include <sys/endian.h> 35 #include <sys/kernel.h> 36 #include <sys/kobj.h> 37 #include <sys/limits.h> 38 #include <sys/lock.h> 39 #include <sys/malloc.h> 40 #include <sys/mutex.h> 41 #include <sys/queue.h> 42 #include <sys/sbuf.h> 43 #include <sys/systm.h> 44 #include <sys/sysctl.h> 45 #include <geom/geom.h> 46 #include <geom/part/g_part.h> 47 48 #include "g_part_if.h" 49 50 FEATURE(geom_part_apm, "GEOM partitioning class for Apple-style partitions"); 51 52 struct g_part_apm_table { 53 struct g_part_table base; 54 struct apm_ddr ddr; 55 struct apm_ent self; 56 int tivo_series1; 57 }; 58 59 struct g_part_apm_entry { 60 struct g_part_entry base; 61 struct apm_ent ent; 62 }; 63 64 static int g_part_apm_add(struct g_part_table *, struct g_part_entry *, 65 struct g_part_parms *); 66 static int g_part_apm_create(struct g_part_table *, struct g_part_parms *); 67 static int g_part_apm_destroy(struct g_part_table *, struct g_part_parms *); 68 static void g_part_apm_dumpconf(struct g_part_table *, struct g_part_entry *, 69 struct sbuf *, const char *); 70 static int g_part_apm_dumpto(struct g_part_table *, struct g_part_entry *); 71 static int g_part_apm_modify(struct g_part_table *, struct g_part_entry *, 72 struct g_part_parms *); 73 static const char *g_part_apm_name(struct g_part_table *, struct g_part_entry *, 74 char *, size_t); 75 static int g_part_apm_probe(struct g_part_table *, struct g_consumer *); 76 static int g_part_apm_read(struct g_part_table *, struct g_consumer *); 77 static const char *g_part_apm_type(struct g_part_table *, struct g_part_entry *, 78 char *, size_t); 79 static int g_part_apm_write(struct g_part_table *, struct g_consumer *); 80 static int g_part_apm_resize(struct g_part_table *, struct g_part_entry *, 81 struct g_part_parms *); 82 83 static kobj_method_t g_part_apm_methods[] = { 84 KOBJMETHOD(g_part_add, g_part_apm_add), 85 KOBJMETHOD(g_part_create, g_part_apm_create), 86 KOBJMETHOD(g_part_destroy, g_part_apm_destroy), 87 KOBJMETHOD(g_part_dumpconf, g_part_apm_dumpconf), 88 KOBJMETHOD(g_part_dumpto, g_part_apm_dumpto), 89 KOBJMETHOD(g_part_modify, g_part_apm_modify), 90 KOBJMETHOD(g_part_resize, g_part_apm_resize), 91 KOBJMETHOD(g_part_name, g_part_apm_name), 92 KOBJMETHOD(g_part_probe, g_part_apm_probe), 93 KOBJMETHOD(g_part_read, g_part_apm_read), 94 KOBJMETHOD(g_part_type, g_part_apm_type), 95 KOBJMETHOD(g_part_write, g_part_apm_write), 96 { 0, 0 } 97 }; 98 99 static struct g_part_scheme g_part_apm_scheme = { 100 "APM", 101 g_part_apm_methods, 102 sizeof(struct g_part_apm_table), 103 .gps_entrysz = sizeof(struct g_part_apm_entry), 104 .gps_minent = 16, 105 .gps_maxent = INT_MAX, 106 }; 107 G_PART_SCHEME_DECLARE(g_part_apm); 108 109 static void 110 swab(char *buf, size_t bufsz) 111 { 112 int i; 113 char ch; 114 115 for (i = 0; i < bufsz; i += 2) { 116 ch = buf[i]; 117 buf[i] = buf[i + 1]; 118 buf[i + 1] = ch; 119 } 120 } 121 122 static int 123 apm_parse_type(const char *type, char *buf, size_t bufsz) 124 { 125 const char *alias; 126 127 if (type[0] == '!') { 128 type++; 129 if (strlen(type) > bufsz) 130 return (EINVAL); 131 if (!strcmp(type, APM_ENT_TYPE_SELF) || 132 !strcmp(type, APM_ENT_TYPE_UNUSED)) 133 return (EINVAL); 134 strncpy(buf, type, bufsz); 135 return (0); 136 } 137 alias = g_part_alias_name(G_PART_ALIAS_APPLE_BOOT); 138 if (!strcasecmp(type, alias)) { 139 strcpy(buf, APM_ENT_TYPE_APPLE_BOOT); 140 return (0); 141 } 142 alias = g_part_alias_name(G_PART_ALIAS_APPLE_HFS); 143 if (!strcasecmp(type, alias)) { 144 strcpy(buf, APM_ENT_TYPE_APPLE_HFS); 145 return (0); 146 } 147 alias = g_part_alias_name(G_PART_ALIAS_APPLE_UFS); 148 if (!strcasecmp(type, alias)) { 149 strcpy(buf, APM_ENT_TYPE_APPLE_UFS); 150 return (0); 151 } 152 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_BOOT); 153 if (!strcasecmp(type, alias)) { 154 strcpy(buf, APM_ENT_TYPE_APPLE_BOOT); 155 return (0); 156 } 157 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD); 158 if (!strcasecmp(type, alias)) { 159 strcpy(buf, APM_ENT_TYPE_FREEBSD); 160 return (0); 161 } 162 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP); 163 if (!strcasecmp(type, alias)) { 164 strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP); 165 return (0); 166 } 167 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS); 168 if (!strcasecmp(type, alias)) { 169 strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS); 170 return (0); 171 } 172 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM); 173 if (!strcasecmp(type, alias)) { 174 strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM); 175 return (0); 176 } 177 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS); 178 if (!strcasecmp(type, alias)) { 179 strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS); 180 return (0); 181 } 182 return (EINVAL); 183 } 184 185 static int 186 apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent, 187 int tivo_series1) 188 { 189 struct g_provider *pp; 190 char *buf; 191 int error; 192 193 pp = cp->provider; 194 buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error); 195 if (buf == NULL) 196 return (error); 197 if (tivo_series1) 198 swab(buf, pp->sectorsize); 199 ent->ent_sig = be16dec(buf); 200 ent->ent_pmblkcnt = be32dec(buf + 4); 201 ent->ent_start = be32dec(buf + 8); 202 ent->ent_size = be32dec(buf + 12); 203 bcopy(buf + 16, ent->ent_name, sizeof(ent->ent_name)); 204 bcopy(buf + 48, ent->ent_type, sizeof(ent->ent_type)); 205 g_free(buf); 206 return (0); 207 } 208 209 static int 210 g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 211 struct g_part_parms *gpp) 212 { 213 struct g_part_apm_entry *entry; 214 struct g_part_apm_table *table; 215 int error; 216 217 entry = (struct g_part_apm_entry *)baseentry; 218 table = (struct g_part_apm_table *)basetable; 219 entry->ent.ent_sig = APM_ENT_SIG; 220 entry->ent.ent_pmblkcnt = table->self.ent_pmblkcnt; 221 entry->ent.ent_start = gpp->gpp_start; 222 entry->ent.ent_size = gpp->gpp_size; 223 if (baseentry->gpe_deleted) { 224 bzero(entry->ent.ent_type, sizeof(entry->ent.ent_type)); 225 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name)); 226 } 227 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type, 228 sizeof(entry->ent.ent_type)); 229 if (error) 230 return (error); 231 if (gpp->gpp_parms & G_PART_PARM_LABEL) { 232 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name)) 233 return (EINVAL); 234 strncpy(entry->ent.ent_name, gpp->gpp_label, 235 sizeof(entry->ent.ent_name)); 236 } 237 return (0); 238 } 239 240 static int 241 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp) 242 { 243 struct g_provider *pp; 244 struct g_part_apm_table *table; 245 uint32_t last; 246 247 /* We don't nest, which means that our depth should be 0. */ 248 if (basetable->gpt_depth != 0) 249 return (ENXIO); 250 251 table = (struct g_part_apm_table *)basetable; 252 pp = gpp->gpp_provider; 253 if (pp->sectorsize != 512 || 254 pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize) 255 return (ENOSPC); 256 257 /* APM uses 32-bit LBAs. */ 258 last = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX) - 1; 259 260 basetable->gpt_first = 2 + basetable->gpt_entries; 261 basetable->gpt_last = last; 262 263 table->ddr.ddr_sig = APM_DDR_SIG; 264 table->ddr.ddr_blksize = pp->sectorsize; 265 table->ddr.ddr_blkcount = last + 1; 266 267 table->self.ent_sig = APM_ENT_SIG; 268 table->self.ent_pmblkcnt = basetable->gpt_entries + 1; 269 table->self.ent_start = 1; 270 table->self.ent_size = table->self.ent_pmblkcnt; 271 strcpy(table->self.ent_name, "Apple"); 272 strcpy(table->self.ent_type, APM_ENT_TYPE_SELF); 273 return (0); 274 } 275 276 static int 277 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 278 { 279 280 /* Wipe the first 2 sectors to clear the partitioning. */ 281 basetable->gpt_smhead |= 3; 282 return (0); 283 } 284 285 static void 286 g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 287 struct sbuf *sb, const char *indent) 288 { 289 union { 290 char name[APM_ENT_NAMELEN + 1]; 291 char type[APM_ENT_TYPELEN + 1]; 292 } u; 293 struct g_part_apm_entry *entry; 294 295 entry = (struct g_part_apm_entry *)baseentry; 296 if (indent == NULL) { 297 /* conftxt: libdisk compatibility */ 298 sbuf_printf(sb, " xs APPLE xt %s", entry->ent.ent_type); 299 } else if (entry != NULL) { 300 /* confxml: partition entry information */ 301 strncpy(u.name, entry->ent.ent_name, APM_ENT_NAMELEN); 302 u.name[APM_ENT_NAMELEN] = '\0'; 303 sbuf_printf(sb, "%s<label>%s</label>\n", indent, u.name); 304 strncpy(u.type, entry->ent.ent_type, APM_ENT_TYPELEN); 305 u.type[APM_ENT_TYPELEN] = '\0'; 306 sbuf_printf(sb, "%s<rawtype>%s</rawtype>\n", indent, u.type); 307 } else { 308 /* confxml: scheme information */ 309 } 310 } 311 312 static int 313 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 314 { 315 struct g_part_apm_entry *entry; 316 317 entry = (struct g_part_apm_entry *)baseentry; 318 return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP)) 319 ? 1 : 0); 320 } 321 322 static int 323 g_part_apm_modify(struct g_part_table *basetable, 324 struct g_part_entry *baseentry, struct g_part_parms *gpp) 325 { 326 struct g_part_apm_entry *entry; 327 int error; 328 329 entry = (struct g_part_apm_entry *)baseentry; 330 if (gpp->gpp_parms & G_PART_PARM_LABEL) { 331 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name)) 332 return (EINVAL); 333 } 334 if (gpp->gpp_parms & G_PART_PARM_TYPE) { 335 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type, 336 sizeof(entry->ent.ent_type)); 337 if (error) 338 return (error); 339 } 340 if (gpp->gpp_parms & G_PART_PARM_LABEL) { 341 strncpy(entry->ent.ent_name, gpp->gpp_label, 342 sizeof(entry->ent.ent_name)); 343 } 344 return (0); 345 } 346 347 static int 348 g_part_apm_resize(struct g_part_table *basetable, 349 struct g_part_entry *baseentry, struct g_part_parms *gpp) 350 { 351 struct g_part_apm_entry *entry; 352 353 entry = (struct g_part_apm_entry *)baseentry; 354 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1; 355 entry->ent.ent_size = gpp->gpp_size; 356 357 return (0); 358 } 359 360 static const char * 361 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry, 362 char *buf, size_t bufsz) 363 { 364 365 snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1); 366 return (buf); 367 } 368 369 static int 370 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp) 371 { 372 struct g_provider *pp; 373 struct g_part_apm_table *table; 374 char *buf; 375 int error; 376 377 /* We don't nest, which means that our depth should be 0. */ 378 if (basetable->gpt_depth != 0) 379 return (ENXIO); 380 381 table = (struct g_part_apm_table *)basetable; 382 table->tivo_series1 = 0; 383 pp = cp->provider; 384 385 /* Sanity-check the provider. */ 386 if (pp->mediasize < 4 * pp->sectorsize) 387 return (ENOSPC); 388 389 /* Check that there's a Driver Descriptor Record (DDR). */ 390 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 391 if (buf == NULL) 392 return (error); 393 if (be16dec(buf) == APM_DDR_SIG) { 394 /* Normal Apple DDR */ 395 table->ddr.ddr_sig = be16dec(buf); 396 table->ddr.ddr_blksize = be16dec(buf + 2); 397 table->ddr.ddr_blkcount = be32dec(buf + 4); 398 g_free(buf); 399 if (table->ddr.ddr_blksize != pp->sectorsize) 400 return (ENXIO); 401 if (table->ddr.ddr_blkcount > pp->mediasize / pp->sectorsize) 402 return (ENXIO); 403 } else { 404 /* 405 * Check for Tivo drives, which have no DDR and a different 406 * signature. Those whose first two bytes are 14 92 are 407 * Series 2 drives, and aren't supported. Those that start 408 * with 92 14 are series 1 drives and are supported. 409 */ 410 if (be16dec(buf) != 0x9214) { 411 /* If this is 0x1492 it could be a series 2 drive */ 412 g_free(buf); 413 return (ENXIO); 414 } 415 table->ddr.ddr_sig = APM_DDR_SIG; /* XXX */ 416 table->ddr.ddr_blksize = pp->sectorsize; /* XXX */ 417 table->ddr.ddr_blkcount = 418 MIN(pp->mediasize / pp->sectorsize, UINT32_MAX); 419 table->tivo_series1 = 1; 420 g_free(buf); 421 } 422 423 /* Check that there's a Partition Map. */ 424 error = apm_read_ent(cp, 1, &table->self, table->tivo_series1); 425 if (error) 426 return (error); 427 if (table->self.ent_sig != APM_ENT_SIG) 428 return (ENXIO); 429 if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF)) 430 return (ENXIO); 431 if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount) 432 return (ENXIO); 433 return (G_PART_PROBE_PRI_NORM); 434 } 435 436 static int 437 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp) 438 { 439 struct apm_ent ent; 440 struct g_part_apm_entry *entry; 441 struct g_part_apm_table *table; 442 int error, index; 443 444 table = (struct g_part_apm_table *)basetable; 445 446 basetable->gpt_first = table->self.ent_pmblkcnt + 1; 447 basetable->gpt_last = table->ddr.ddr_blkcount - 1; 448 basetable->gpt_entries = table->self.ent_pmblkcnt - 1; 449 450 for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) { 451 error = apm_read_ent(cp, index + 1, &ent, table->tivo_series1); 452 if (error) 453 continue; 454 if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED)) 455 continue; 456 entry = (struct g_part_apm_entry *)g_part_new_entry(basetable, 457 index, ent.ent_start, ent.ent_start + ent.ent_size - 1); 458 entry->ent = ent; 459 } 460 461 return (0); 462 } 463 464 static const char * 465 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 466 char *buf, size_t bufsz) 467 { 468 struct g_part_apm_entry *entry; 469 const char *type; 470 size_t len; 471 472 entry = (struct g_part_apm_entry *)baseentry; 473 type = entry->ent.ent_type; 474 if (!strcmp(type, APM_ENT_TYPE_APPLE_BOOT)) 475 return (g_part_alias_name(G_PART_ALIAS_APPLE_BOOT)); 476 if (!strcmp(type, APM_ENT_TYPE_APPLE_HFS)) 477 return (g_part_alias_name(G_PART_ALIAS_APPLE_HFS)); 478 if (!strcmp(type, APM_ENT_TYPE_APPLE_UFS)) 479 return (g_part_alias_name(G_PART_ALIAS_APPLE_UFS)); 480 if (!strcmp(type, APM_ENT_TYPE_FREEBSD)) 481 return (g_part_alias_name(G_PART_ALIAS_FREEBSD)); 482 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP)) 483 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP)); 484 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS)) 485 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS)); 486 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM)) 487 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM)); 488 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS)) 489 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS)); 490 buf[0] = '!'; 491 len = MIN(sizeof(entry->ent.ent_type), bufsz - 2); 492 bcopy(type, buf + 1, len); 493 buf[len + 1] = '\0'; 494 return (buf); 495 } 496 497 static int 498 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp) 499 { 500 char buf[512]; 501 struct g_part_entry *baseentry; 502 struct g_part_apm_entry *entry; 503 struct g_part_apm_table *table; 504 int error, index; 505 506 table = (struct g_part_apm_table *)basetable; 507 /* 508 * Tivo Series 1 disk partitions are currently read-only. 509 */ 510 if (table->tivo_series1) 511 return (EOPNOTSUPP); 512 bzero(buf, sizeof(buf)); 513 514 /* Write the DDR and 'self' entry only when we're newly created. */ 515 if (basetable->gpt_created) { 516 be16enc(buf, table->ddr.ddr_sig); 517 be16enc(buf + 2, table->ddr.ddr_blksize); 518 be32enc(buf + 4, table->ddr.ddr_blkcount); 519 error = g_write_data(cp, 0, buf, sizeof(buf)); 520 if (error) 521 return (error); 522 } 523 524 be16enc(buf, table->self.ent_sig); 525 be16enc(buf + 2, 0); 526 be32enc(buf + 4, table->self.ent_pmblkcnt); 527 528 if (basetable->gpt_created) { 529 be32enc(buf + 8, table->self.ent_start); 530 be32enc(buf + 12, table->self.ent_size); 531 bcopy(table->self.ent_name, buf + 16, 532 sizeof(table->self.ent_name)); 533 bcopy(table->self.ent_type, buf + 48, 534 sizeof(table->self.ent_type)); 535 error = g_write_data(cp, 512, buf, sizeof(buf)); 536 if (error) 537 return (error); 538 } 539 540 baseentry = LIST_FIRST(&basetable->gpt_entry); 541 for (index = 1; index <= basetable->gpt_entries; index++) { 542 entry = (baseentry != NULL && index == baseentry->gpe_index) 543 ? (struct g_part_apm_entry *)baseentry : NULL; 544 if (entry != NULL && !baseentry->gpe_deleted) { 545 be32enc(buf + 8, entry->ent.ent_start); 546 be32enc(buf + 12, entry->ent.ent_size); 547 bcopy(entry->ent.ent_name, buf + 16, 548 sizeof(entry->ent.ent_name)); 549 bcopy(entry->ent.ent_type, buf + 48, 550 sizeof(entry->ent.ent_type)); 551 } else { 552 bzero(buf + 8, 4 + 4 + 32 + 32); 553 strcpy(buf + 48, APM_ENT_TYPE_UNUSED); 554 } 555 error = g_write_data(cp, (index + 1) * 512, buf, sizeof(buf)); 556 if (error) 557 return (error); 558 if (entry != NULL) 559 baseentry = LIST_NEXT(baseentry, gpe_entry); 560 } 561 562 return (0); 563 } 564