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 <geom/geom.h>
45 #include <geom/part/g_part.h>
47 #include "g_part_if.h"
49 struct g_part_apm_table {
50 struct g_part_table base;
56 struct g_part_apm_entry {
57 struct g_part_entry base;
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 *,
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 *,
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 *);
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),
96 static struct g_part_scheme g_part_apm_scheme = {
99 sizeof(struct g_part_apm_table),
100 .gps_entrysz = sizeof(struct g_part_apm_entry),
104 G_PART_SCHEME_DECLARE(g_part_apm);
107 swab(char *buf, size_t bufsz)
112 for (i = 0; i < bufsz; i += 2) {
120 apm_parse_type(const char *type, char *buf, size_t bufsz)
124 if (type[0] == '!') {
126 if (strlen(type) > bufsz)
128 if (!strcmp(type, APM_ENT_TYPE_SELF) ||
129 !strcmp(type, APM_ENT_TYPE_UNUSED))
131 strncpy(buf, type, bufsz);
134 alias = g_part_alias_name(G_PART_ALIAS_APPLE_BOOT);
135 if (!strcasecmp(type, alias)) {
136 strcpy(buf, APM_ENT_TYPE_APPLE_BOOT);
139 alias = g_part_alias_name(G_PART_ALIAS_APPLE_HFS);
140 if (!strcasecmp(type, alias)) {
141 strcpy(buf, APM_ENT_TYPE_APPLE_HFS);
144 alias = g_part_alias_name(G_PART_ALIAS_APPLE_UFS);
145 if (!strcasecmp(type, alias)) {
146 strcpy(buf, APM_ENT_TYPE_APPLE_UFS);
149 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_BOOT);
150 if (!strcasecmp(type, alias)) {
151 strcpy(buf, APM_ENT_TYPE_APPLE_BOOT);
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_SWAP);
160 if (!strcasecmp(type, alias)) {
161 strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP);
164 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
165 if (!strcasecmp(type, alias)) {
166 strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS);
169 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
170 if (!strcasecmp(type, alias)) {
171 strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM);
174 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
175 if (!strcasecmp(type, alias)) {
176 strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS);
183 apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent,
186 struct g_provider *pp;
191 buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error);
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));
207 g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
208 struct g_part_parms *gpp)
210 struct g_part_apm_entry *entry;
211 struct g_part_apm_table *table;
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));
224 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
225 sizeof(entry->ent.ent_type));
228 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
229 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
231 strncpy(entry->ent.ent_name, gpp->gpp_label,
232 sizeof(entry->ent.ent_name));
234 if (baseentry->gpe_index >= table->self.ent_pmblkcnt)
235 table->self.ent_pmblkcnt = baseentry->gpe_index + 1;
236 KASSERT(table->self.ent_size >= table->self.ent_pmblkcnt,
238 KASSERT(table->self.ent_size > baseentry->gpe_index,
244 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
246 struct g_provider *pp;
247 struct g_part_apm_table *table;
250 /* We don't nest, which means that our depth should be 0. */
251 if (basetable->gpt_depth != 0)
254 table = (struct g_part_apm_table *)basetable;
255 pp = gpp->gpp_provider;
256 if (pp->sectorsize != 512 ||
257 pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize)
260 /* APM uses 32-bit LBAs. */
261 last = MIN(pp->mediasize / pp->sectorsize, 0xffffffff) - 1;
263 basetable->gpt_first = 2 + basetable->gpt_entries;
264 basetable->gpt_last = last;
266 table->ddr.ddr_sig = APM_DDR_SIG;
267 table->ddr.ddr_blksize = pp->sectorsize;
268 table->ddr.ddr_blkcount = last + 1;
270 table->self.ent_sig = APM_ENT_SIG;
271 table->self.ent_pmblkcnt = basetable->gpt_entries + 1;
272 table->self.ent_start = 1;
273 table->self.ent_size = table->self.ent_pmblkcnt;
274 strcpy(table->self.ent_name, "Apple");
275 strcpy(table->self.ent_type, APM_ENT_TYPE_SELF);
280 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
283 /* Wipe the first 2 sectors to clear the partitioning. */
284 basetable->gpt_smhead |= 3;
289 g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
290 struct sbuf *sb, const char *indent)
293 char name[APM_ENT_NAMELEN + 1];
294 char type[APM_ENT_TYPELEN + 1];
296 struct g_part_apm_entry *entry;
298 entry = (struct g_part_apm_entry *)baseentry;
299 if (indent == NULL) {
300 /* conftxt: libdisk compatibility */
301 sbuf_printf(sb, " xs APPLE xt %s", entry->ent.ent_type);
302 } else if (entry != NULL) {
303 /* confxml: partition entry information */
304 strncpy(u.name, entry->ent.ent_name, APM_ENT_NAMELEN);
305 u.name[APM_ENT_NAMELEN] = '\0';
306 sbuf_printf(sb, "%s<label>%s</label>\n", indent, u.name);
307 strncpy(u.type, entry->ent.ent_type, APM_ENT_TYPELEN);
308 u.type[APM_ENT_TYPELEN] = '\0';
309 sbuf_printf(sb, "%s<rawtype>%s</rawtype>\n", indent, u.type);
311 /* confxml: scheme information */
316 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
318 struct g_part_apm_entry *entry;
320 entry = (struct g_part_apm_entry *)baseentry;
321 return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP))
326 g_part_apm_modify(struct g_part_table *basetable,
327 struct g_part_entry *baseentry, struct g_part_parms *gpp)
329 struct g_part_apm_entry *entry;
332 entry = (struct g_part_apm_entry *)baseentry;
333 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
334 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
337 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
338 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
339 sizeof(entry->ent.ent_type));
343 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
344 strncpy(entry->ent.ent_name, gpp->gpp_label,
345 sizeof(entry->ent.ent_name));
351 g_part_apm_resize(struct g_part_table *basetable,
352 struct g_part_entry *baseentry, struct g_part_parms *gpp)
354 struct g_part_apm_entry *entry;
356 entry = (struct g_part_apm_entry *)baseentry;
357 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
358 entry->ent.ent_size = gpp->gpp_size;
364 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
365 char *buf, size_t bufsz)
368 snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
373 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
375 struct g_provider *pp;
376 struct g_part_apm_table *table;
380 /* We don't nest, which means that our depth should be 0. */
381 if (basetable->gpt_depth != 0)
384 table = (struct g_part_apm_table *)basetable;
385 table->tivo_series1 = 0;
388 /* Sanity-check the provider. */
389 if (pp->mediasize < 4 * pp->sectorsize)
392 /* Check that there's a Driver Descriptor Record (DDR). */
393 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
396 if (be16dec(buf) == APM_DDR_SIG) {
397 /* Normal Apple DDR */
398 table->ddr.ddr_sig = be16dec(buf);
399 table->ddr.ddr_blksize = be16dec(buf + 2);
400 table->ddr.ddr_blkcount = be32dec(buf + 4);
402 if (table->ddr.ddr_blksize != pp->sectorsize)
406 * Check for Tivo drives, which have no DDR and a different
407 * signature. Those whose first two bytes are 14 92 are
408 * Series 2 drives, and aren't supported. Those that start
409 * with 92 14 are series 1 drives and are supported.
411 if (be16dec(buf) != 0x9214) {
412 /* If this is 0x1492 it could be a series 2 drive */
416 table->ddr.ddr_sig = APM_DDR_SIG; /* XXX */
417 table->ddr.ddr_blksize = pp->sectorsize; /* XXX */
418 table->ddr.ddr_blkcount = pp->mediasize / pp->sectorsize;/* XXX */
419 table->tivo_series1 = 1;
423 /* Check that there's a Partition Map. */
424 error = apm_read_ent(cp, 1, &table->self, table->tivo_series1);
427 if (table->self.ent_sig != APM_ENT_SIG)
429 if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
431 if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
433 return (G_PART_PROBE_PRI_NORM);
437 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
440 struct g_part_apm_entry *entry;
441 struct g_part_apm_table *table;
444 table = (struct g_part_apm_table *)basetable;
446 basetable->gpt_first = table->self.ent_size + 1;
447 basetable->gpt_last = table->ddr.ddr_blkcount - 1;
448 basetable->gpt_entries = table->self.ent_size - 1;
450 for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
451 error = apm_read_ent(cp, index + 1, &ent, table->tivo_series1);
454 if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
456 entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
457 index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
465 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
466 char *buf, size_t bufsz)
468 struct g_part_apm_entry *entry;
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));
491 len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
492 bcopy(type, buf + 1, len);
498 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
500 struct g_provider *pp;
501 struct g_part_entry *baseentry;
502 struct g_part_apm_entry *entry;
503 struct g_part_apm_table *table;
510 table = (struct g_part_apm_table *)basetable;
512 * Tivo Series 1 disk partitions are currently read-only.
514 if (table->tivo_series1)
517 /* Write the DDR only when we're newly created. */
518 if (basetable->gpt_created) {
519 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
520 be16enc(buf, table->ddr.ddr_sig);
521 be16enc(buf + 2, table->ddr.ddr_blksize);
522 be32enc(buf + 4, table->ddr.ddr_blkcount);
523 error = g_write_data(cp, 0, buf, pp->sectorsize);
529 /* Allocate the buffer for all entries */
530 tblsz = table->self.ent_pmblkcnt;
531 buf = g_malloc(tblsz * pp->sectorsize, M_WAITOK | M_ZERO);
533 /* Fill the self entry */
534 be16enc(buf, APM_ENT_SIG);
535 be32enc(buf + 4, table->self.ent_pmblkcnt);
536 be32enc(buf + 8, table->self.ent_start);
537 be32enc(buf + 12, table->self.ent_size);
538 bcopy(table->self.ent_name, buf + 16, sizeof(table->self.ent_name));
539 bcopy(table->self.ent_type, buf + 48, sizeof(table->self.ent_type));
541 baseentry = LIST_FIRST(&basetable->gpt_entry);
542 for (index = 1; index < tblsz; index++) {
543 entry = (baseentry != NULL && index == baseentry->gpe_index)
544 ? (struct g_part_apm_entry *)baseentry : NULL;
545 ptr = buf + index * pp->sectorsize;
546 be16enc(ptr, APM_ENT_SIG);
547 be32enc(ptr + 4, table->self.ent_pmblkcnt);
548 if (entry != NULL && !baseentry->gpe_deleted) {
549 be32enc(ptr + 8, entry->ent.ent_start);
550 be32enc(ptr + 12, entry->ent.ent_size);
551 bcopy(entry->ent.ent_name, ptr + 16,
552 sizeof(entry->ent.ent_name));
553 bcopy(entry->ent.ent_type, ptr + 48,
554 sizeof(entry->ent.ent_type));
556 strcpy(ptr + 48, APM_ENT_TYPE_UNUSED);
559 baseentry = LIST_NEXT(baseentry, gpe_entry);
562 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) {
563 error = g_write_data(cp, (1 + index) * pp->sectorsize,
564 buf + index * pp->sectorsize,
565 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS:
566 (tblsz - index) * pp->sectorsize);