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