2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006-2008 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
38 #include <sys/limits.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/queue.h>
44 #include <sys/systm.h>
45 #include <sys/sysctl.h>
46 #include <geom/geom.h>
47 #include <geom/geom_int.h>
48 #include <geom/part/g_part.h>
50 #include "g_part_if.h"
52 FEATURE(geom_part_apm, "GEOM partitioning class for Apple-style partitions");
54 struct g_part_apm_table {
55 struct g_part_table base;
61 struct g_part_apm_entry {
62 struct g_part_entry base;
66 static int g_part_apm_add(struct g_part_table *, struct g_part_entry *,
67 struct g_part_parms *);
68 static int g_part_apm_create(struct g_part_table *, struct g_part_parms *);
69 static int g_part_apm_destroy(struct g_part_table *, struct g_part_parms *);
70 static void g_part_apm_dumpconf(struct g_part_table *, struct g_part_entry *,
71 struct sbuf *, const char *);
72 static int g_part_apm_dumpto(struct g_part_table *, struct g_part_entry *);
73 static int g_part_apm_modify(struct g_part_table *, struct g_part_entry *,
74 struct g_part_parms *);
75 static const char *g_part_apm_name(struct g_part_table *, struct g_part_entry *,
77 static int g_part_apm_probe(struct g_part_table *, struct g_consumer *);
78 static int g_part_apm_read(struct g_part_table *, struct g_consumer *);
79 static const char *g_part_apm_type(struct g_part_table *, struct g_part_entry *,
81 static int g_part_apm_write(struct g_part_table *, struct g_consumer *);
82 static int g_part_apm_resize(struct g_part_table *, struct g_part_entry *,
83 struct g_part_parms *);
85 static kobj_method_t g_part_apm_methods[] = {
86 KOBJMETHOD(g_part_add, g_part_apm_add),
87 KOBJMETHOD(g_part_create, g_part_apm_create),
88 KOBJMETHOD(g_part_destroy, g_part_apm_destroy),
89 KOBJMETHOD(g_part_dumpconf, g_part_apm_dumpconf),
90 KOBJMETHOD(g_part_dumpto, g_part_apm_dumpto),
91 KOBJMETHOD(g_part_modify, g_part_apm_modify),
92 KOBJMETHOD(g_part_resize, g_part_apm_resize),
93 KOBJMETHOD(g_part_name, g_part_apm_name),
94 KOBJMETHOD(g_part_probe, g_part_apm_probe),
95 KOBJMETHOD(g_part_read, g_part_apm_read),
96 KOBJMETHOD(g_part_type, g_part_apm_type),
97 KOBJMETHOD(g_part_write, g_part_apm_write),
101 static struct g_part_scheme g_part_apm_scheme = {
104 sizeof(struct g_part_apm_table),
105 .gps_entrysz = sizeof(struct g_part_apm_entry),
109 G_PART_SCHEME_DECLARE(g_part_apm);
110 MODULE_VERSION(geom_part_apm, 0);
113 swab(char *buf, size_t bufsz)
118 for (i = 0; i < bufsz; i += 2) {
126 apm_parse_type(const char *type, char *buf, size_t bufsz)
130 if (type[0] == '!') {
132 if (strlen(type) > bufsz)
134 if (!strcmp(type, APM_ENT_TYPE_SELF) ||
135 !strcmp(type, APM_ENT_TYPE_UNUSED))
137 strncpy(buf, type, bufsz);
140 alias = g_part_alias_name(G_PART_ALIAS_APPLE_BOOT);
141 if (!strcasecmp(type, alias)) {
142 strcpy(buf, APM_ENT_TYPE_APPLE_BOOT);
145 alias = g_part_alias_name(G_PART_ALIAS_APPLE_HFS);
146 if (!strcasecmp(type, alias)) {
147 strcpy(buf, APM_ENT_TYPE_APPLE_HFS);
150 alias = g_part_alias_name(G_PART_ALIAS_APPLE_UFS);
151 if (!strcasecmp(type, alias)) {
152 strcpy(buf, APM_ENT_TYPE_APPLE_UFS);
155 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
156 if (!strcasecmp(type, alias)) {
157 strcpy(buf, APM_ENT_TYPE_FREEBSD);
160 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS);
161 if (!strcasecmp(type, alias)) {
162 strcpy(buf, APM_ENT_TYPE_FREEBSD_NANDFS);
165 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
166 if (!strcasecmp(type, alias)) {
167 strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP);
170 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
171 if (!strcasecmp(type, alias)) {
172 strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS);
175 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
176 if (!strcasecmp(type, alias)) {
177 strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM);
180 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
181 if (!strcasecmp(type, alias)) {
182 strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS);
189 apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent,
192 struct g_provider *pp;
197 buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error);
201 swab(buf, pp->sectorsize);
202 ent->ent_sig = be16dec(buf);
203 ent->ent_pmblkcnt = be32dec(buf + 4);
204 ent->ent_start = be32dec(buf + 8);
205 ent->ent_size = be32dec(buf + 12);
206 bcopy(buf + 16, ent->ent_name, sizeof(ent->ent_name));
207 bcopy(buf + 48, ent->ent_type, sizeof(ent->ent_type));
213 g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
214 struct g_part_parms *gpp)
216 struct g_part_apm_entry *entry;
217 struct g_part_apm_table *table;
220 entry = (struct g_part_apm_entry *)baseentry;
221 table = (struct g_part_apm_table *)basetable;
222 entry->ent.ent_sig = APM_ENT_SIG;
223 entry->ent.ent_pmblkcnt = table->self.ent_pmblkcnt;
224 entry->ent.ent_start = gpp->gpp_start;
225 entry->ent.ent_size = gpp->gpp_size;
226 if (baseentry->gpe_deleted) {
227 bzero(entry->ent.ent_type, sizeof(entry->ent.ent_type));
228 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
230 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
231 sizeof(entry->ent.ent_type));
234 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
235 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
237 strncpy(entry->ent.ent_name, gpp->gpp_label,
238 sizeof(entry->ent.ent_name));
240 if (baseentry->gpe_index >= table->self.ent_pmblkcnt)
241 table->self.ent_pmblkcnt = baseentry->gpe_index + 1;
242 KASSERT(table->self.ent_size >= table->self.ent_pmblkcnt,
244 KASSERT(table->self.ent_size > baseentry->gpe_index,
250 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
252 struct g_provider *pp;
253 struct g_part_apm_table *table;
256 /* We don't nest, which means that our depth should be 0. */
257 if (basetable->gpt_depth != 0)
260 table = (struct g_part_apm_table *)basetable;
261 pp = gpp->gpp_provider;
262 if (pp->sectorsize != 512 ||
263 pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize)
266 /* APM uses 32-bit LBAs. */
267 last = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX) - 1;
269 basetable->gpt_first = 2 + basetable->gpt_entries;
270 basetable->gpt_last = last;
272 table->ddr.ddr_sig = APM_DDR_SIG;
273 table->ddr.ddr_blksize = pp->sectorsize;
274 table->ddr.ddr_blkcount = last + 1;
276 table->self.ent_sig = APM_ENT_SIG;
277 table->self.ent_pmblkcnt = basetable->gpt_entries + 1;
278 table->self.ent_start = 1;
279 table->self.ent_size = table->self.ent_pmblkcnt;
280 strcpy(table->self.ent_name, "Apple");
281 strcpy(table->self.ent_type, APM_ENT_TYPE_SELF);
286 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
289 /* Wipe the first 2 sectors to clear the partitioning. */
290 basetable->gpt_smhead |= 3;
295 g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
296 struct sbuf *sb, const char *indent)
299 char name[APM_ENT_NAMELEN + 1];
300 char type[APM_ENT_TYPELEN + 1];
302 struct g_part_apm_entry *entry;
304 entry = (struct g_part_apm_entry *)baseentry;
305 if (indent == NULL) {
306 /* conftxt: libdisk compatibility */
307 sbuf_printf(sb, " xs APPLE xt %s", entry->ent.ent_type);
308 } else if (entry != NULL) {
309 /* confxml: partition entry information */
310 strncpy(u.name, entry->ent.ent_name, APM_ENT_NAMELEN);
311 u.name[APM_ENT_NAMELEN] = '\0';
312 sbuf_printf(sb, "%s<label>", indent);
313 g_conf_cat_escaped(sb, u.name);
314 sbuf_cat(sb, "</label>\n");
315 strncpy(u.type, entry->ent.ent_type, APM_ENT_TYPELEN);
316 u.type[APM_ENT_TYPELEN] = '\0';
317 sbuf_printf(sb, "%s<rawtype>", indent);
318 g_conf_cat_escaped(sb, u.type);
319 sbuf_cat(sb, "</rawtype>\n");
321 /* confxml: scheme information */
326 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
328 struct g_part_apm_entry *entry;
330 entry = (struct g_part_apm_entry *)baseentry;
331 return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP))
336 g_part_apm_modify(struct g_part_table *basetable,
337 struct g_part_entry *baseentry, struct g_part_parms *gpp)
339 struct g_part_apm_entry *entry;
342 entry = (struct g_part_apm_entry *)baseentry;
343 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
344 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
347 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
348 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
349 sizeof(entry->ent.ent_type));
353 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
354 strncpy(entry->ent.ent_name, gpp->gpp_label,
355 sizeof(entry->ent.ent_name));
361 g_part_apm_resize(struct g_part_table *basetable,
362 struct g_part_entry *baseentry, struct g_part_parms *gpp)
364 struct g_part_apm_entry *entry;
365 struct g_provider *pp;
367 if (baseentry == NULL) {
368 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
369 basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
374 entry = (struct g_part_apm_entry *)baseentry;
375 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
376 entry->ent.ent_size = gpp->gpp_size;
382 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
383 char *buf, size_t bufsz)
386 snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
391 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
393 struct g_provider *pp;
394 struct g_part_apm_table *table;
398 /* We don't nest, which means that our depth should be 0. */
399 if (basetable->gpt_depth != 0)
402 table = (struct g_part_apm_table *)basetable;
403 table->tivo_series1 = 0;
406 /* Sanity-check the provider. */
407 if (pp->mediasize < 4 * pp->sectorsize)
410 /* Check that there's a Driver Descriptor Record (DDR). */
411 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
414 if (be16dec(buf) == APM_DDR_SIG) {
415 /* Normal Apple DDR */
416 table->ddr.ddr_sig = be16dec(buf);
417 table->ddr.ddr_blksize = be16dec(buf + 2);
418 table->ddr.ddr_blkcount = be32dec(buf + 4);
420 if (table->ddr.ddr_blksize != pp->sectorsize)
422 if (table->ddr.ddr_blkcount > pp->mediasize / pp->sectorsize)
426 * Check for Tivo drives, which have no DDR and a different
427 * signature. Those whose first two bytes are 14 92 are
428 * Series 2 drives, and aren't supported. Those that start
429 * with 92 14 are series 1 drives and are supported.
431 if (be16dec(buf) != 0x9214) {
432 /* If this is 0x1492 it could be a series 2 drive */
436 table->ddr.ddr_sig = APM_DDR_SIG; /* XXX */
437 table->ddr.ddr_blksize = pp->sectorsize; /* XXX */
438 table->ddr.ddr_blkcount =
439 MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
440 table->tivo_series1 = 1;
444 /* Check that there's a Partition Map. */
445 error = apm_read_ent(cp, 1, &table->self, table->tivo_series1);
448 if (table->self.ent_sig != APM_ENT_SIG)
450 if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
452 if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
454 return (G_PART_PROBE_PRI_NORM);
458 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
461 struct g_part_apm_entry *entry;
462 struct g_part_apm_table *table;
465 table = (struct g_part_apm_table *)basetable;
467 basetable->gpt_first = table->self.ent_size + 1;
468 basetable->gpt_last = table->ddr.ddr_blkcount - 1;
469 basetable->gpt_entries = table->self.ent_size - 1;
471 for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
472 error = apm_read_ent(cp, index + 1, &ent, table->tivo_series1);
475 if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
477 entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
478 index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
486 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
487 char *buf, size_t bufsz)
489 struct g_part_apm_entry *entry;
493 entry = (struct g_part_apm_entry *)baseentry;
494 type = entry->ent.ent_type;
495 if (!strcmp(type, APM_ENT_TYPE_APPLE_BOOT))
496 return (g_part_alias_name(G_PART_ALIAS_APPLE_BOOT));
497 if (!strcmp(type, APM_ENT_TYPE_APPLE_HFS))
498 return (g_part_alias_name(G_PART_ALIAS_APPLE_HFS));
499 if (!strcmp(type, APM_ENT_TYPE_APPLE_UFS))
500 return (g_part_alias_name(G_PART_ALIAS_APPLE_UFS));
501 if (!strcmp(type, APM_ENT_TYPE_FREEBSD))
502 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
503 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_NANDFS))
504 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
505 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP))
506 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
507 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS))
508 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
509 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM))
510 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
511 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS))
512 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
514 len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
515 bcopy(type, buf + 1, len);
521 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
523 struct g_provider *pp;
524 struct g_part_entry *baseentry;
525 struct g_part_apm_entry *entry;
526 struct g_part_apm_table *table;
533 table = (struct g_part_apm_table *)basetable;
535 * Tivo Series 1 disk partitions are currently read-only.
537 if (table->tivo_series1)
540 /* Write the DDR only when we're newly created. */
541 if (basetable->gpt_created) {
542 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
543 be16enc(buf, table->ddr.ddr_sig);
544 be16enc(buf + 2, table->ddr.ddr_blksize);
545 be32enc(buf + 4, table->ddr.ddr_blkcount);
546 error = g_write_data(cp, 0, buf, pp->sectorsize);
552 /* Allocate the buffer for all entries */
553 tblsz = table->self.ent_pmblkcnt;
554 buf = g_malloc(tblsz * pp->sectorsize, M_WAITOK | M_ZERO);
556 /* Fill the self entry */
557 be16enc(buf, APM_ENT_SIG);
558 be32enc(buf + 4, table->self.ent_pmblkcnt);
559 be32enc(buf + 8, table->self.ent_start);
560 be32enc(buf + 12, table->self.ent_size);
561 bcopy(table->self.ent_name, buf + 16, sizeof(table->self.ent_name));
562 bcopy(table->self.ent_type, buf + 48, sizeof(table->self.ent_type));
564 baseentry = LIST_FIRST(&basetable->gpt_entry);
565 for (index = 1; index < tblsz; index++) {
566 entry = (baseentry != NULL && index == baseentry->gpe_index)
567 ? (struct g_part_apm_entry *)baseentry : NULL;
568 ptr = buf + index * pp->sectorsize;
569 be16enc(ptr, APM_ENT_SIG);
570 be32enc(ptr + 4, table->self.ent_pmblkcnt);
571 if (entry != NULL && !baseentry->gpe_deleted) {
572 be32enc(ptr + 8, entry->ent.ent_start);
573 be32enc(ptr + 12, entry->ent.ent_size);
574 bcopy(entry->ent.ent_name, ptr + 16,
575 sizeof(entry->ent.ent_name));
576 bcopy(entry->ent.ent_type, ptr + 48,
577 sizeof(entry->ent.ent_type));
579 strcpy(ptr + 48, APM_ENT_TYPE_UNUSED);
582 baseentry = LIST_NEXT(baseentry, gpe_entry);
585 for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
586 error = g_write_data(cp, (1 + index) * pp->sectorsize,
587 buf + index * pp->sectorsize,
588 (tblsz - index > maxphys / pp->sectorsize) ? maxphys:
589 (tblsz - index) * pp->sectorsize);