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),
102 .gps_maxent = INT_MAX,
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));
238 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
240 struct g_provider *pp;
241 struct g_part_apm_table *table;
244 /* We don't nest, which means that our depth should be 0. */
245 if (basetable->gpt_depth != 0)
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)
254 /* APM uses 32-bit LBAs. */
255 last = MIN(pp->mediasize / pp->sectorsize, 0xffffffff) - 1;
257 basetable->gpt_first = 2 + basetable->gpt_entries;
258 basetable->gpt_last = last;
260 table->ddr.ddr_sig = APM_DDR_SIG;
261 table->ddr.ddr_blksize = pp->sectorsize;
262 table->ddr.ddr_blkcount = last + 1;
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);
274 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
277 /* Wipe the first 2 sectors to clear the partitioning. */
278 basetable->gpt_smhead |= 3;
283 g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
284 struct sbuf *sb, const char *indent)
287 char name[APM_ENT_NAMELEN + 1];
288 char type[APM_ENT_TYPELEN + 1];
290 struct g_part_apm_entry *entry;
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);
305 /* confxml: scheme information */
310 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
312 struct g_part_apm_entry *entry;
314 entry = (struct g_part_apm_entry *)baseentry;
315 return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP))
320 g_part_apm_modify(struct g_part_table *basetable,
321 struct g_part_entry *baseentry, struct g_part_parms *gpp)
323 struct g_part_apm_entry *entry;
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))
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));
337 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
338 strncpy(entry->ent.ent_name, gpp->gpp_label,
339 sizeof(entry->ent.ent_name));
345 g_part_apm_resize(struct g_part_table *basetable,
346 struct g_part_entry *baseentry, struct g_part_parms *gpp)
348 struct g_part_apm_entry *entry;
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;
358 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
359 char *buf, size_t bufsz)
362 snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
367 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
369 struct g_provider *pp;
370 struct g_part_apm_table *table;
374 /* We don't nest, which means that our depth should be 0. */
375 if (basetable->gpt_depth != 0)
378 table = (struct g_part_apm_table *)basetable;
379 table->tivo_series1 = 0;
382 /* Sanity-check the provider. */
383 if (pp->mediasize < 4 * pp->sectorsize)
386 /* Check that there's a Driver Descriptor Record (DDR). */
387 buf = g_read_data(cp, 0L, pp->sectorsize, &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);
396 if (table->ddr.ddr_blksize != pp->sectorsize)
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.
405 if (be16dec(buf) != 0x9214) {
406 /* If this is 0x1492 it could be a series 2 drive */
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;
417 /* Check that there's a Partition Map. */
418 error = apm_read_ent(cp, 1, &table->self, table->tivo_series1);
421 if (table->self.ent_sig != APM_ENT_SIG)
423 if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
425 if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
427 return (G_PART_PROBE_PRI_NORM);
431 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
434 struct g_part_apm_entry *entry;
435 struct g_part_apm_table *table;
438 table = (struct g_part_apm_table *)basetable;
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;
444 for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
445 error = apm_read_ent(cp, index + 1, &ent, table->tivo_series1);
448 if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
450 entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
451 index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
459 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
460 char *buf, size_t bufsz)
462 struct g_part_apm_entry *entry;
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));
485 len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
486 bcopy(type, buf + 1, len);
492 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
495 struct g_part_entry *baseentry;
496 struct g_part_apm_entry *entry;
497 struct g_part_apm_table *table;
500 table = (struct g_part_apm_table *)basetable;
502 * Tivo Series 1 disk partitions are currently read-only.
504 if (table->tivo_series1)
506 bzero(buf, sizeof(buf));
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));
518 be16enc(buf, table->self.ent_sig);
520 be32enc(buf + 4, table->self.ent_pmblkcnt);
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));
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));
546 bzero(buf + 8, 4 + 4 + 32 + 32);
547 strcpy(buf + 48, APM_ENT_TYPE_UNUSED);
549 error = g_write_data(cp, (index + 1) * 512, buf, sizeof(buf));
553 baseentry = LIST_NEXT(baseentry, gpe_entry);