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 *);
78 static kobj_method_t g_part_apm_methods[] = {
79 KOBJMETHOD(g_part_add, g_part_apm_add),
80 KOBJMETHOD(g_part_create, g_part_apm_create),
81 KOBJMETHOD(g_part_destroy, g_part_apm_destroy),
82 KOBJMETHOD(g_part_dumpconf, g_part_apm_dumpconf),
83 KOBJMETHOD(g_part_dumpto, g_part_apm_dumpto),
84 KOBJMETHOD(g_part_modify, g_part_apm_modify),
85 KOBJMETHOD(g_part_name, g_part_apm_name),
86 KOBJMETHOD(g_part_probe, g_part_apm_probe),
87 KOBJMETHOD(g_part_read, g_part_apm_read),
88 KOBJMETHOD(g_part_type, g_part_apm_type),
89 KOBJMETHOD(g_part_write, g_part_apm_write),
93 static struct g_part_scheme g_part_apm_scheme = {
96 sizeof(struct g_part_apm_table),
97 .gps_entrysz = sizeof(struct g_part_apm_entry),
99 .gps_maxent = INT_MAX,
101 G_PART_SCHEME_DECLARE(g_part_apm);
104 swab(char *buf, size_t bufsz)
109 for (i = 0; i < bufsz; i += 2) {
117 apm_parse_type(const char *type, char *buf, size_t bufsz)
121 if (type[0] == '!') {
123 if (strlen(type) > bufsz)
125 if (!strcmp(type, APM_ENT_TYPE_SELF) ||
126 !strcmp(type, APM_ENT_TYPE_UNUSED))
128 strncpy(buf, type, bufsz);
131 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
132 if (!strcasecmp(type, alias)) {
133 strcpy(buf, APM_ENT_TYPE_FREEBSD);
136 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
137 if (!strcasecmp(type, alias)) {
138 strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP);
141 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
142 if (!strcasecmp(type, alias)) {
143 strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS);
146 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
147 if (!strcasecmp(type, alias)) {
148 strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM);
151 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
152 if (!strcasecmp(type, alias)) {
153 strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS);
160 apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent,
163 struct g_provider *pp;
168 buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error);
172 swab(buf, pp->sectorsize);
173 ent->ent_sig = be16dec(buf);
174 ent->ent_pmblkcnt = be32dec(buf + 4);
175 ent->ent_start = be32dec(buf + 8);
176 ent->ent_size = be32dec(buf + 12);
177 bcopy(buf + 16, ent->ent_name, sizeof(ent->ent_name));
178 bcopy(buf + 48, ent->ent_type, sizeof(ent->ent_type));
184 g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
185 struct g_part_parms *gpp)
187 struct g_part_apm_entry *entry;
188 struct g_part_apm_table *table;
191 entry = (struct g_part_apm_entry *)baseentry;
192 table = (struct g_part_apm_table *)basetable;
193 entry->ent.ent_sig = APM_ENT_SIG;
194 entry->ent.ent_pmblkcnt = table->self.ent_pmblkcnt;
195 entry->ent.ent_start = gpp->gpp_start;
196 entry->ent.ent_size = gpp->gpp_size;
197 if (baseentry->gpe_deleted) {
198 bzero(entry->ent.ent_type, sizeof(entry->ent.ent_type));
199 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
201 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
202 sizeof(entry->ent.ent_type));
205 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
206 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
208 strncpy(entry->ent.ent_name, gpp->gpp_label,
209 sizeof(entry->ent.ent_name));
215 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
217 struct g_provider *pp;
218 struct g_part_apm_table *table;
220 table = (struct g_part_apm_table *)basetable;
221 pp = gpp->gpp_provider;
222 if (pp->sectorsize != 512 ||
223 pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize)
226 basetable->gpt_first = 2 + basetable->gpt_entries;
227 basetable->gpt_last = (pp->mediasize / pp->sectorsize) - 1;
229 table->ddr.ddr_sig = APM_DDR_SIG;
230 table->ddr.ddr_blksize = pp->sectorsize;
231 table->ddr.ddr_blkcount = basetable->gpt_last + 1;
233 table->self.ent_sig = APM_ENT_SIG;
234 table->self.ent_pmblkcnt = basetable->gpt_entries + 1;
235 table->self.ent_start = 1;
236 table->self.ent_size = table->self.ent_pmblkcnt;
237 strcpy(table->self.ent_name, "Apple");
238 strcpy(table->self.ent_type, APM_ENT_TYPE_SELF);
243 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
246 /* Wipe the first 2 sectors to clear the partitioning. */
247 basetable->gpt_smhead |= 3;
252 g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
253 struct sbuf *sb, const char *indent)
256 char name[APM_ENT_NAMELEN + 1];
257 char type[APM_ENT_TYPELEN + 1];
259 struct g_part_apm_entry *entry;
261 entry = (struct g_part_apm_entry *)baseentry;
262 if (indent == NULL) {
263 /* conftxt: libdisk compatibility */
264 sbuf_printf(sb, " xs APPLE xt %s", entry->ent.ent_type);
265 } else if (entry != NULL) {
266 /* confxml: partition entry information */
267 strncpy(u.name, entry->ent.ent_name, APM_ENT_NAMELEN);
268 u.name[APM_ENT_NAMELEN] = '\0';
269 sbuf_printf(sb, "%s<label>%s</label>\n", indent, u.name);
270 strncpy(u.type, entry->ent.ent_type, APM_ENT_TYPELEN);
271 u.type[APM_ENT_TYPELEN] = '\0';
272 sbuf_printf(sb, "%s<rawtype>%s</rawtype>\n", indent, u.type);
274 /* confxml: scheme information */
279 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
281 struct g_part_apm_entry *entry;
283 entry = (struct g_part_apm_entry *)baseentry;
284 return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP))
289 g_part_apm_modify(struct g_part_table *basetable,
290 struct g_part_entry *baseentry, struct g_part_parms *gpp)
292 struct g_part_apm_entry *entry;
295 entry = (struct g_part_apm_entry *)baseentry;
296 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
297 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
300 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
301 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
302 sizeof(entry->ent.ent_type));
306 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
307 strncpy(entry->ent.ent_name, gpp->gpp_label,
308 sizeof(entry->ent.ent_name));
314 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
315 char *buf, size_t bufsz)
318 snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
323 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
325 struct g_provider *pp;
326 struct g_part_apm_table *table;
330 /* We don't nest, which means that our depth should be 0. */
331 if (basetable->gpt_depth != 0)
334 table = (struct g_part_apm_table *)basetable;
335 table->tivo_series1 = 0;
338 /* Sanity-check the provider. */
339 if (pp->mediasize < 4 * pp->sectorsize)
342 /* Check that there's a Driver Descriptor Record (DDR). */
343 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
346 if (be16dec(buf) == be16toh(APM_DDR_SIG)) {
347 /* Normal Apple DDR */
348 table->ddr.ddr_sig = be16dec(buf);
349 table->ddr.ddr_blksize = be16dec(buf + 2);
350 table->ddr.ddr_blkcount = be32dec(buf + 4);
352 if (table->ddr.ddr_blksize != pp->sectorsize)
356 * Check for Tivo drives, which have no DDR and a different
357 * signature. Those whose first two bytes are 14 92 are
358 * Series 2 drives, and aren't supported. Those that start
359 * with 92 14 are series 1 drives and are supported.
361 if (be16dec(buf) != 0x9214) {
362 /* If this is 0x1492 it could be a series 2 drive */
366 table->ddr.ddr_sig = APM_DDR_SIG; /* XXX */
367 table->ddr.ddr_blksize = pp->sectorsize; /* XXX */
368 table->ddr.ddr_blkcount = pp->mediasize / pp->sectorsize;/* XXX */
369 table->tivo_series1 = 1;
373 /* Check that there's a Partition Map. */
374 error = apm_read_ent(cp, 1, &table->self, table->tivo_series1);
377 if (table->self.ent_sig != APM_ENT_SIG)
379 if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
381 if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
383 return (G_PART_PROBE_PRI_NORM);
387 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
390 struct g_part_apm_entry *entry;
391 struct g_part_apm_table *table;
394 table = (struct g_part_apm_table *)basetable;
396 basetable->gpt_first = table->self.ent_pmblkcnt + 1;
397 basetable->gpt_last = table->ddr.ddr_blkcount - 1;
398 basetable->gpt_entries = table->self.ent_pmblkcnt - 1;
400 for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
401 error = apm_read_ent(cp, index + 1, &ent, table->tivo_series1);
404 if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
406 entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
407 index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
415 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
416 char *buf, size_t bufsz)
418 struct g_part_apm_entry *entry;
422 entry = (struct g_part_apm_entry *)baseentry;
423 type = entry->ent.ent_type;
424 if (!strcmp(type, APM_ENT_TYPE_FREEBSD))
425 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
426 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP))
427 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
428 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS))
429 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
430 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM))
431 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
432 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS))
433 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
435 len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
436 bcopy(type, buf + 1, len);
442 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
445 struct g_part_entry *baseentry;
446 struct g_part_apm_entry *entry;
447 struct g_part_apm_table *table;
450 table = (struct g_part_apm_table *)basetable;
452 * Tivo Series 1 disk partitions are currently read-only.
454 if (table->tivo_series1)
456 bzero(buf, sizeof(buf));
458 /* Write the DDR and 'self' entry only when we're newly created. */
459 if (basetable->gpt_created) {
460 be16enc(buf, table->ddr.ddr_sig);
461 be16enc(buf + 2, table->ddr.ddr_blksize);
462 be32enc(buf + 4, table->ddr.ddr_blkcount);
463 error = g_write_data(cp, 0, buf, sizeof(buf));
468 be16enc(buf, table->self.ent_sig);
470 be32enc(buf + 4, table->self.ent_pmblkcnt);
472 if (basetable->gpt_created) {
473 be32enc(buf + 8, table->self.ent_start);
474 be32enc(buf + 12, table->self.ent_size);
475 bcopy(table->self.ent_name, buf + 16,
476 sizeof(table->self.ent_name));
477 bcopy(table->self.ent_type, buf + 48,
478 sizeof(table->self.ent_type));
479 error = g_write_data(cp, 512, buf, sizeof(buf));
484 baseentry = LIST_FIRST(&basetable->gpt_entry);
485 for (index = 1; index <= basetable->gpt_entries; index++) {
486 entry = (baseentry != NULL && index == baseentry->gpe_index)
487 ? (struct g_part_apm_entry *)baseentry : NULL;
488 if (entry != NULL && !baseentry->gpe_deleted) {
489 be32enc(buf + 8, entry->ent.ent_start);
490 be32enc(buf + 12, entry->ent.ent_size);
491 bcopy(entry->ent.ent_name, buf + 16,
492 sizeof(entry->ent.ent_name));
493 bcopy(entry->ent.ent_type, buf + 48,
494 sizeof(entry->ent.ent_type));
496 bzero(buf + 8, 4 + 4 + 32 + 32);
497 strcpy(buf + 48, APM_ENT_TYPE_UNUSED);
499 error = g_write_data(cp, (index + 1) * 512, buf, sizeof(buf));
503 baseentry = LIST_NEXT(baseentry, gpe_entry);