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);
112 swab(char *buf, size_t bufsz)
117 for (i = 0; i < bufsz; i += 2) {
125 apm_parse_type(const char *type, char *buf, size_t bufsz)
129 if (type[0] == '!') {
131 if (strlen(type) > bufsz)
133 if (!strcmp(type, APM_ENT_TYPE_SELF) ||
134 !strcmp(type, APM_ENT_TYPE_UNUSED))
136 strncpy(buf, type, bufsz);
139 alias = g_part_alias_name(G_PART_ALIAS_APPLE_BOOT);
140 if (!strcasecmp(type, alias)) {
141 strcpy(buf, APM_ENT_TYPE_APPLE_BOOT);
144 alias = g_part_alias_name(G_PART_ALIAS_APPLE_HFS);
145 if (!strcasecmp(type, alias)) {
146 strcpy(buf, APM_ENT_TYPE_APPLE_HFS);
149 alias = g_part_alias_name(G_PART_ALIAS_APPLE_UFS);
150 if (!strcasecmp(type, alias)) {
151 strcpy(buf, APM_ENT_TYPE_APPLE_UFS);
154 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
155 if (!strcasecmp(type, alias)) {
156 strcpy(buf, APM_ENT_TYPE_FREEBSD);
159 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS);
160 if (!strcasecmp(type, alias)) {
161 strcpy(buf, APM_ENT_TYPE_FREEBSD_NANDFS);
164 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
165 if (!strcasecmp(type, alias)) {
166 strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP);
169 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
170 if (!strcasecmp(type, alias)) {
171 strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS);
174 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
175 if (!strcasecmp(type, alias)) {
176 strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM);
179 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
180 if (!strcasecmp(type, alias)) {
181 strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS);
188 apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent,
191 struct g_provider *pp;
196 buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error);
200 swab(buf, pp->sectorsize);
201 ent->ent_sig = be16dec(buf);
202 ent->ent_pmblkcnt = be32dec(buf + 4);
203 ent->ent_start = be32dec(buf + 8);
204 ent->ent_size = be32dec(buf + 12);
205 bcopy(buf + 16, ent->ent_name, sizeof(ent->ent_name));
206 bcopy(buf + 48, ent->ent_type, sizeof(ent->ent_type));
212 g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
213 struct g_part_parms *gpp)
215 struct g_part_apm_entry *entry;
216 struct g_part_apm_table *table;
219 entry = (struct g_part_apm_entry *)baseentry;
220 table = (struct g_part_apm_table *)basetable;
221 entry->ent.ent_sig = APM_ENT_SIG;
222 entry->ent.ent_pmblkcnt = table->self.ent_pmblkcnt;
223 entry->ent.ent_start = gpp->gpp_start;
224 entry->ent.ent_size = gpp->gpp_size;
225 if (baseentry->gpe_deleted) {
226 bzero(entry->ent.ent_type, sizeof(entry->ent.ent_type));
227 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
229 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
230 sizeof(entry->ent.ent_type));
233 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
234 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
236 strncpy(entry->ent.ent_name, gpp->gpp_label,
237 sizeof(entry->ent.ent_name));
239 if (baseentry->gpe_index >= table->self.ent_pmblkcnt)
240 table->self.ent_pmblkcnt = baseentry->gpe_index + 1;
241 KASSERT(table->self.ent_size >= table->self.ent_pmblkcnt,
243 KASSERT(table->self.ent_size > baseentry->gpe_index,
249 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
251 struct g_provider *pp;
252 struct g_part_apm_table *table;
255 /* We don't nest, which means that our depth should be 0. */
256 if (basetable->gpt_depth != 0)
259 table = (struct g_part_apm_table *)basetable;
260 pp = gpp->gpp_provider;
261 if (pp->sectorsize != 512 ||
262 pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize)
265 /* APM uses 32-bit LBAs. */
266 last = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX) - 1;
268 basetable->gpt_first = 2 + basetable->gpt_entries;
269 basetable->gpt_last = last;
271 table->ddr.ddr_sig = APM_DDR_SIG;
272 table->ddr.ddr_blksize = pp->sectorsize;
273 table->ddr.ddr_blkcount = last + 1;
275 table->self.ent_sig = APM_ENT_SIG;
276 table->self.ent_pmblkcnt = basetable->gpt_entries + 1;
277 table->self.ent_start = 1;
278 table->self.ent_size = table->self.ent_pmblkcnt;
279 strcpy(table->self.ent_name, "Apple");
280 strcpy(table->self.ent_type, APM_ENT_TYPE_SELF);
285 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
288 /* Wipe the first 2 sectors to clear the partitioning. */
289 basetable->gpt_smhead |= 3;
294 g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
295 struct sbuf *sb, const char *indent)
298 char name[APM_ENT_NAMELEN + 1];
299 char type[APM_ENT_TYPELEN + 1];
301 struct g_part_apm_entry *entry;
303 entry = (struct g_part_apm_entry *)baseentry;
304 if (indent == NULL) {
305 /* conftxt: libdisk compatibility */
306 sbuf_printf(sb, " xs APPLE xt %s", entry->ent.ent_type);
307 } else if (entry != NULL) {
308 /* confxml: partition entry information */
309 strncpy(u.name, entry->ent.ent_name, APM_ENT_NAMELEN);
310 u.name[APM_ENT_NAMELEN] = '\0';
311 sbuf_printf(sb, "%s<label>", indent);
312 g_conf_printf_escaped(sb, "%s", u.name);
313 sbuf_printf(sb, "</label>\n");
314 strncpy(u.type, entry->ent.ent_type, APM_ENT_TYPELEN);
315 u.type[APM_ENT_TYPELEN] = '\0';
316 sbuf_printf(sb, "%s<rawtype>", indent);
317 g_conf_printf_escaped(sb, "%s", u.type);
318 sbuf_printf(sb, "</rawtype>\n");
320 /* confxml: scheme information */
325 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
327 struct g_part_apm_entry *entry;
329 entry = (struct g_part_apm_entry *)baseentry;
330 return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP))
335 g_part_apm_modify(struct g_part_table *basetable,
336 struct g_part_entry *baseentry, struct g_part_parms *gpp)
338 struct g_part_apm_entry *entry;
341 entry = (struct g_part_apm_entry *)baseentry;
342 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
343 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
346 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
347 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
348 sizeof(entry->ent.ent_type));
352 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
353 strncpy(entry->ent.ent_name, gpp->gpp_label,
354 sizeof(entry->ent.ent_name));
360 g_part_apm_resize(struct g_part_table *basetable,
361 struct g_part_entry *baseentry, struct g_part_parms *gpp)
363 struct g_part_apm_entry *entry;
364 struct g_provider *pp;
366 if (baseentry == NULL) {
367 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
368 basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
373 entry = (struct g_part_apm_entry *)baseentry;
374 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
375 entry->ent.ent_size = gpp->gpp_size;
381 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
382 char *buf, size_t bufsz)
385 snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
390 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
392 struct g_provider *pp;
393 struct g_part_apm_table *table;
397 /* We don't nest, which means that our depth should be 0. */
398 if (basetable->gpt_depth != 0)
401 table = (struct g_part_apm_table *)basetable;
402 table->tivo_series1 = 0;
405 /* Sanity-check the provider. */
406 if (pp->mediasize < 4 * pp->sectorsize)
409 /* Check that there's a Driver Descriptor Record (DDR). */
410 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
413 if (be16dec(buf) == APM_DDR_SIG) {
414 /* Normal Apple DDR */
415 table->ddr.ddr_sig = be16dec(buf);
416 table->ddr.ddr_blksize = be16dec(buf + 2);
417 table->ddr.ddr_blkcount = be32dec(buf + 4);
419 if (table->ddr.ddr_blksize != pp->sectorsize)
421 if (table->ddr.ddr_blkcount > pp->mediasize / pp->sectorsize)
425 * Check for Tivo drives, which have no DDR and a different
426 * signature. Those whose first two bytes are 14 92 are
427 * Series 2 drives, and aren't supported. Those that start
428 * with 92 14 are series 1 drives and are supported.
430 if (be16dec(buf) != 0x9214) {
431 /* If this is 0x1492 it could be a series 2 drive */
435 table->ddr.ddr_sig = APM_DDR_SIG; /* XXX */
436 table->ddr.ddr_blksize = pp->sectorsize; /* XXX */
437 table->ddr.ddr_blkcount =
438 MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
439 table->tivo_series1 = 1;
443 /* Check that there's a Partition Map. */
444 error = apm_read_ent(cp, 1, &table->self, table->tivo_series1);
447 if (table->self.ent_sig != APM_ENT_SIG)
449 if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
451 if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
453 return (G_PART_PROBE_PRI_NORM);
457 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
460 struct g_part_apm_entry *entry;
461 struct g_part_apm_table *table;
464 table = (struct g_part_apm_table *)basetable;
466 basetable->gpt_first = table->self.ent_size + 1;
467 basetable->gpt_last = table->ddr.ddr_blkcount - 1;
468 basetable->gpt_entries = table->self.ent_size - 1;
470 for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
471 error = apm_read_ent(cp, index + 1, &ent, table->tivo_series1);
474 if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
476 entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
477 index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
485 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
486 char *buf, size_t bufsz)
488 struct g_part_apm_entry *entry;
492 entry = (struct g_part_apm_entry *)baseentry;
493 type = entry->ent.ent_type;
494 if (!strcmp(type, APM_ENT_TYPE_APPLE_BOOT))
495 return (g_part_alias_name(G_PART_ALIAS_APPLE_BOOT));
496 if (!strcmp(type, APM_ENT_TYPE_APPLE_HFS))
497 return (g_part_alias_name(G_PART_ALIAS_APPLE_HFS));
498 if (!strcmp(type, APM_ENT_TYPE_APPLE_UFS))
499 return (g_part_alias_name(G_PART_ALIAS_APPLE_UFS));
500 if (!strcmp(type, APM_ENT_TYPE_FREEBSD))
501 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
502 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_NANDFS))
503 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
504 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP))
505 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
506 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS))
507 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
508 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM))
509 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
510 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS))
511 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
513 len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
514 bcopy(type, buf + 1, len);
520 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
522 struct g_provider *pp;
523 struct g_part_entry *baseentry;
524 struct g_part_apm_entry *entry;
525 struct g_part_apm_table *table;
532 table = (struct g_part_apm_table *)basetable;
534 * Tivo Series 1 disk partitions are currently read-only.
536 if (table->tivo_series1)
539 /* Write the DDR only when we're newly created. */
540 if (basetable->gpt_created) {
541 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
542 be16enc(buf, table->ddr.ddr_sig);
543 be16enc(buf + 2, table->ddr.ddr_blksize);
544 be32enc(buf + 4, table->ddr.ddr_blkcount);
545 error = g_write_data(cp, 0, buf, pp->sectorsize);
551 /* Allocate the buffer for all entries */
552 tblsz = table->self.ent_pmblkcnt;
553 buf = g_malloc(tblsz * pp->sectorsize, M_WAITOK | M_ZERO);
555 /* Fill the self entry */
556 be16enc(buf, APM_ENT_SIG);
557 be32enc(buf + 4, table->self.ent_pmblkcnt);
558 be32enc(buf + 8, table->self.ent_start);
559 be32enc(buf + 12, table->self.ent_size);
560 bcopy(table->self.ent_name, buf + 16, sizeof(table->self.ent_name));
561 bcopy(table->self.ent_type, buf + 48, sizeof(table->self.ent_type));
563 baseentry = LIST_FIRST(&basetable->gpt_entry);
564 for (index = 1; index < tblsz; index++) {
565 entry = (baseentry != NULL && index == baseentry->gpe_index)
566 ? (struct g_part_apm_entry *)baseentry : NULL;
567 ptr = buf + index * pp->sectorsize;
568 be16enc(ptr, APM_ENT_SIG);
569 be32enc(ptr + 4, table->self.ent_pmblkcnt);
570 if (entry != NULL && !baseentry->gpe_deleted) {
571 be32enc(ptr + 8, entry->ent.ent_start);
572 be32enc(ptr + 12, entry->ent.ent_size);
573 bcopy(entry->ent.ent_name, ptr + 16,
574 sizeof(entry->ent.ent_name));
575 bcopy(entry->ent.ent_type, ptr + 48,
576 sizeof(entry->ent.ent_type));
578 strcpy(ptr + 48, APM_ENT_TYPE_UNUSED);
581 baseentry = LIST_NEXT(baseentry, gpe_entry);
584 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) {
585 error = g_write_data(cp, (1 + index) * pp->sectorsize,
586 buf + index * pp->sectorsize,
587 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS:
588 (tblsz - index) * pp->sectorsize);