2 * Copyright (c) 2006-2008 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
33 #include <sys/diskmbr.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
37 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.h>
43 #include <sys/systm.h>
44 #include <sys/sysctl.h>
45 #include <geom/geom.h>
46 #include <geom/geom_int.h>
47 #include <geom/part/g_part.h>
49 #include "g_part_if.h"
51 FEATURE(geom_part_apm, "GEOM partitioning class for Apple-style partitions");
53 struct g_part_apm_table {
54 struct g_part_table base;
60 struct g_part_apm_entry {
61 struct g_part_entry base;
65 static int g_part_apm_add(struct g_part_table *, struct g_part_entry *,
66 struct g_part_parms *);
67 static int g_part_apm_create(struct g_part_table *, struct g_part_parms *);
68 static int g_part_apm_destroy(struct g_part_table *, struct g_part_parms *);
69 static void g_part_apm_dumpconf(struct g_part_table *, struct g_part_entry *,
70 struct sbuf *, const char *);
71 static int g_part_apm_dumpto(struct g_part_table *, struct g_part_entry *);
72 static int g_part_apm_modify(struct g_part_table *, struct g_part_entry *,
73 struct g_part_parms *);
74 static const char *g_part_apm_name(struct g_part_table *, struct g_part_entry *,
76 static int g_part_apm_probe(struct g_part_table *, struct g_consumer *);
77 static int g_part_apm_read(struct g_part_table *, struct g_consumer *);
78 static const char *g_part_apm_type(struct g_part_table *, struct g_part_entry *,
80 static int g_part_apm_write(struct g_part_table *, struct g_consumer *);
81 static int g_part_apm_resize(struct g_part_table *, struct g_part_entry *,
82 struct g_part_parms *);
84 static kobj_method_t g_part_apm_methods[] = {
85 KOBJMETHOD(g_part_add, g_part_apm_add),
86 KOBJMETHOD(g_part_create, g_part_apm_create),
87 KOBJMETHOD(g_part_destroy, g_part_apm_destroy),
88 KOBJMETHOD(g_part_dumpconf, g_part_apm_dumpconf),
89 KOBJMETHOD(g_part_dumpto, g_part_apm_dumpto),
90 KOBJMETHOD(g_part_modify, g_part_apm_modify),
91 KOBJMETHOD(g_part_resize, g_part_apm_resize),
92 KOBJMETHOD(g_part_name, g_part_apm_name),
93 KOBJMETHOD(g_part_probe, g_part_apm_probe),
94 KOBJMETHOD(g_part_read, g_part_apm_read),
95 KOBJMETHOD(g_part_type, g_part_apm_type),
96 KOBJMETHOD(g_part_write, g_part_apm_write),
100 static struct g_part_scheme g_part_apm_scheme = {
103 sizeof(struct g_part_apm_table),
104 .gps_entrysz = sizeof(struct g_part_apm_entry),
108 G_PART_SCHEME_DECLARE(g_part_apm);
111 swab(char *buf, size_t bufsz)
116 for (i = 0; i < bufsz; i += 2) {
124 apm_parse_type(const char *type, char *buf, size_t bufsz)
128 if (type[0] == '!') {
130 if (strlen(type) > bufsz)
132 if (!strcmp(type, APM_ENT_TYPE_SELF) ||
133 !strcmp(type, APM_ENT_TYPE_UNUSED))
135 strncpy(buf, type, bufsz);
138 alias = g_part_alias_name(G_PART_ALIAS_APPLE_BOOT);
139 if (!strcasecmp(type, alias)) {
140 strcpy(buf, APM_ENT_TYPE_APPLE_BOOT);
143 alias = g_part_alias_name(G_PART_ALIAS_APPLE_HFS);
144 if (!strcasecmp(type, alias)) {
145 strcpy(buf, APM_ENT_TYPE_APPLE_HFS);
148 alias = g_part_alias_name(G_PART_ALIAS_APPLE_UFS);
149 if (!strcasecmp(type, alias)) {
150 strcpy(buf, APM_ENT_TYPE_APPLE_UFS);
153 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_BOOT);
154 if (!strcasecmp(type, alias)) {
155 strcpy(buf, APM_ENT_TYPE_APPLE_BOOT);
158 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
159 if (!strcasecmp(type, alias)) {
160 strcpy(buf, APM_ENT_TYPE_FREEBSD);
163 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS);
164 if (!strcasecmp(type, alias)) {
165 strcpy(buf, APM_ENT_TYPE_FREEBSD_NANDFS);
168 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
169 if (!strcasecmp(type, alias)) {
170 strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP);
173 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
174 if (!strcasecmp(type, alias)) {
175 strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS);
178 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
179 if (!strcasecmp(type, alias)) {
180 strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM);
183 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
184 if (!strcasecmp(type, alias)) {
185 strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS);
192 apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent,
195 struct g_provider *pp;
200 buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error);
204 swab(buf, pp->sectorsize);
205 ent->ent_sig = be16dec(buf);
206 ent->ent_pmblkcnt = be32dec(buf + 4);
207 ent->ent_start = be32dec(buf + 8);
208 ent->ent_size = be32dec(buf + 12);
209 bcopy(buf + 16, ent->ent_name, sizeof(ent->ent_name));
210 bcopy(buf + 48, ent->ent_type, sizeof(ent->ent_type));
216 g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
217 struct g_part_parms *gpp)
219 struct g_part_apm_entry *entry;
220 struct g_part_apm_table *table;
223 entry = (struct g_part_apm_entry *)baseentry;
224 table = (struct g_part_apm_table *)basetable;
225 entry->ent.ent_sig = APM_ENT_SIG;
226 entry->ent.ent_pmblkcnt = table->self.ent_pmblkcnt;
227 entry->ent.ent_start = gpp->gpp_start;
228 entry->ent.ent_size = gpp->gpp_size;
229 if (baseentry->gpe_deleted) {
230 bzero(entry->ent.ent_type, sizeof(entry->ent.ent_type));
231 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
233 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
234 sizeof(entry->ent.ent_type));
237 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
238 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
240 strncpy(entry->ent.ent_name, gpp->gpp_label,
241 sizeof(entry->ent.ent_name));
243 if (baseentry->gpe_index >= table->self.ent_pmblkcnt)
244 table->self.ent_pmblkcnt = baseentry->gpe_index + 1;
245 KASSERT(table->self.ent_size >= table->self.ent_pmblkcnt,
247 KASSERT(table->self.ent_size > baseentry->gpe_index,
253 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
255 struct g_provider *pp;
256 struct g_part_apm_table *table;
259 /* We don't nest, which means that our depth should be 0. */
260 if (basetable->gpt_depth != 0)
263 table = (struct g_part_apm_table *)basetable;
264 pp = gpp->gpp_provider;
265 if (pp->sectorsize != 512 ||
266 pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize)
269 /* APM uses 32-bit LBAs. */
270 last = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX) - 1;
272 basetable->gpt_first = 2 + basetable->gpt_entries;
273 basetable->gpt_last = last;
275 table->ddr.ddr_sig = APM_DDR_SIG;
276 table->ddr.ddr_blksize = pp->sectorsize;
277 table->ddr.ddr_blkcount = last + 1;
279 table->self.ent_sig = APM_ENT_SIG;
280 table->self.ent_pmblkcnt = basetable->gpt_entries + 1;
281 table->self.ent_start = 1;
282 table->self.ent_size = table->self.ent_pmblkcnt;
283 strcpy(table->self.ent_name, "Apple");
284 strcpy(table->self.ent_type, APM_ENT_TYPE_SELF);
289 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
292 /* Wipe the first 2 sectors to clear the partitioning. */
293 basetable->gpt_smhead |= 3;
298 g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
299 struct sbuf *sb, const char *indent)
302 char name[APM_ENT_NAMELEN + 1];
303 char type[APM_ENT_TYPELEN + 1];
305 struct g_part_apm_entry *entry;
307 entry = (struct g_part_apm_entry *)baseentry;
308 if (indent == NULL) {
309 /* conftxt: libdisk compatibility */
310 sbuf_printf(sb, " xs APPLE xt %s", entry->ent.ent_type);
311 } else if (entry != NULL) {
312 /* confxml: partition entry information */
313 strncpy(u.name, entry->ent.ent_name, APM_ENT_NAMELEN);
314 u.name[APM_ENT_NAMELEN] = '\0';
315 sbuf_printf(sb, "%s<label>", indent);
316 g_conf_printf_escaped(sb, "%s", u.name);
317 sbuf_printf(sb, "</label>\n");
318 strncpy(u.type, entry->ent.ent_type, APM_ENT_TYPELEN);
319 u.type[APM_ENT_TYPELEN] = '\0';
320 sbuf_printf(sb, "%s<rawtype>", indent);
321 g_conf_printf_escaped(sb, "%s", u.type);
322 sbuf_printf(sb, "</rawtype>\n");
324 /* confxml: scheme information */
329 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
331 struct g_part_apm_entry *entry;
333 entry = (struct g_part_apm_entry *)baseentry;
334 return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP))
339 g_part_apm_modify(struct g_part_table *basetable,
340 struct g_part_entry *baseentry, struct g_part_parms *gpp)
342 struct g_part_apm_entry *entry;
345 entry = (struct g_part_apm_entry *)baseentry;
346 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
347 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
350 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
351 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
352 sizeof(entry->ent.ent_type));
356 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
357 strncpy(entry->ent.ent_name, gpp->gpp_label,
358 sizeof(entry->ent.ent_name));
364 g_part_apm_resize(struct g_part_table *basetable,
365 struct g_part_entry *baseentry, struct g_part_parms *gpp)
367 struct g_part_apm_entry *entry;
369 entry = (struct g_part_apm_entry *)baseentry;
370 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
371 entry->ent.ent_size = gpp->gpp_size;
377 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
378 char *buf, size_t bufsz)
381 snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
386 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
388 struct g_provider *pp;
389 struct g_part_apm_table *table;
393 /* We don't nest, which means that our depth should be 0. */
394 if (basetable->gpt_depth != 0)
397 table = (struct g_part_apm_table *)basetable;
398 table->tivo_series1 = 0;
401 /* Sanity-check the provider. */
402 if (pp->mediasize < 4 * pp->sectorsize)
405 /* Check that there's a Driver Descriptor Record (DDR). */
406 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
409 if (be16dec(buf) == APM_DDR_SIG) {
410 /* Normal Apple DDR */
411 table->ddr.ddr_sig = be16dec(buf);
412 table->ddr.ddr_blksize = be16dec(buf + 2);
413 table->ddr.ddr_blkcount = be32dec(buf + 4);
415 if (table->ddr.ddr_blksize != pp->sectorsize)
417 if (table->ddr.ddr_blkcount > pp->mediasize / pp->sectorsize)
421 * Check for Tivo drives, which have no DDR and a different
422 * signature. Those whose first two bytes are 14 92 are
423 * Series 2 drives, and aren't supported. Those that start
424 * with 92 14 are series 1 drives and are supported.
426 if (be16dec(buf) != 0x9214) {
427 /* If this is 0x1492 it could be a series 2 drive */
431 table->ddr.ddr_sig = APM_DDR_SIG; /* XXX */
432 table->ddr.ddr_blksize = pp->sectorsize; /* XXX */
433 table->ddr.ddr_blkcount =
434 MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
435 table->tivo_series1 = 1;
439 /* Check that there's a Partition Map. */
440 error = apm_read_ent(cp, 1, &table->self, table->tivo_series1);
443 if (table->self.ent_sig != APM_ENT_SIG)
445 if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
447 if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
449 return (G_PART_PROBE_PRI_NORM);
453 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
456 struct g_part_apm_entry *entry;
457 struct g_part_apm_table *table;
460 table = (struct g_part_apm_table *)basetable;
462 basetable->gpt_first = table->self.ent_size + 1;
463 basetable->gpt_last = table->ddr.ddr_blkcount - 1;
464 basetable->gpt_entries = table->self.ent_size - 1;
466 for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
467 error = apm_read_ent(cp, index + 1, &ent, table->tivo_series1);
470 if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
472 entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
473 index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
481 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
482 char *buf, size_t bufsz)
484 struct g_part_apm_entry *entry;
488 entry = (struct g_part_apm_entry *)baseentry;
489 type = entry->ent.ent_type;
490 if (!strcmp(type, APM_ENT_TYPE_APPLE_BOOT))
491 return (g_part_alias_name(G_PART_ALIAS_APPLE_BOOT));
492 if (!strcmp(type, APM_ENT_TYPE_APPLE_HFS))
493 return (g_part_alias_name(G_PART_ALIAS_APPLE_HFS));
494 if (!strcmp(type, APM_ENT_TYPE_APPLE_UFS))
495 return (g_part_alias_name(G_PART_ALIAS_APPLE_UFS));
496 if (!strcmp(type, APM_ENT_TYPE_FREEBSD))
497 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
498 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_NANDFS))
499 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
500 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP))
501 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
502 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS))
503 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
504 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM))
505 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
506 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS))
507 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
509 len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
510 bcopy(type, buf + 1, len);
516 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
518 struct g_provider *pp;
519 struct g_part_entry *baseentry;
520 struct g_part_apm_entry *entry;
521 struct g_part_apm_table *table;
528 table = (struct g_part_apm_table *)basetable;
530 * Tivo Series 1 disk partitions are currently read-only.
532 if (table->tivo_series1)
535 /* Write the DDR only when we're newly created. */
536 if (basetable->gpt_created) {
537 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
538 be16enc(buf, table->ddr.ddr_sig);
539 be16enc(buf + 2, table->ddr.ddr_blksize);
540 be32enc(buf + 4, table->ddr.ddr_blkcount);
541 error = g_write_data(cp, 0, buf, pp->sectorsize);
547 /* Allocate the buffer for all entries */
548 tblsz = table->self.ent_pmblkcnt;
549 buf = g_malloc(tblsz * pp->sectorsize, M_WAITOK | M_ZERO);
551 /* Fill the self entry */
552 be16enc(buf, APM_ENT_SIG);
553 be32enc(buf + 4, table->self.ent_pmblkcnt);
554 be32enc(buf + 8, table->self.ent_start);
555 be32enc(buf + 12, table->self.ent_size);
556 bcopy(table->self.ent_name, buf + 16, sizeof(table->self.ent_name));
557 bcopy(table->self.ent_type, buf + 48, sizeof(table->self.ent_type));
559 baseentry = LIST_FIRST(&basetable->gpt_entry);
560 for (index = 1; index < tblsz; index++) {
561 entry = (baseentry != NULL && index == baseentry->gpe_index)
562 ? (struct g_part_apm_entry *)baseentry : NULL;
563 ptr = buf + index * pp->sectorsize;
564 be16enc(ptr, APM_ENT_SIG);
565 be32enc(ptr + 4, table->self.ent_pmblkcnt);
566 if (entry != NULL && !baseentry->gpe_deleted) {
567 be32enc(ptr + 8, entry->ent.ent_start);
568 be32enc(ptr + 12, entry->ent.ent_size);
569 bcopy(entry->ent.ent_name, ptr + 16,
570 sizeof(entry->ent.ent_name));
571 bcopy(entry->ent.ent_type, ptr + 48,
572 sizeof(entry->ent.ent_type));
574 strcpy(ptr + 48, APM_ENT_TYPE_UNUSED);
577 baseentry = LIST_NEXT(baseentry, gpe_entry);
580 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) {
581 error = g_write_data(cp, (1 + index) * pp->sectorsize,
582 buf + index * pp->sectorsize,
583 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS:
584 (tblsz - index) * pp->sectorsize);