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;
55 struct g_part_apm_entry {
56 struct g_part_entry base;
60 static int g_part_apm_add(struct g_part_table *, struct g_part_entry *,
61 struct g_part_parms *);
62 static int g_part_apm_create(struct g_part_table *, struct g_part_parms *);
63 static int g_part_apm_destroy(struct g_part_table *, struct g_part_parms *);
64 static int g_part_apm_dumpconf(struct g_part_table *, struct g_part_entry *,
65 struct sbuf *, const char *);
66 static int g_part_apm_dumpto(struct g_part_table *, struct g_part_entry *);
67 static int g_part_apm_modify(struct g_part_table *, struct g_part_entry *,
68 struct g_part_parms *);
69 static char *g_part_apm_name(struct g_part_table *, struct g_part_entry *,
71 static int g_part_apm_probe(struct g_part_table *, struct g_consumer *);
72 static int g_part_apm_read(struct g_part_table *, struct g_consumer *);
73 static const char *g_part_apm_type(struct g_part_table *, struct g_part_entry *,
75 static int g_part_apm_write(struct g_part_table *, struct g_consumer *);
77 static kobj_method_t g_part_apm_methods[] = {
78 KOBJMETHOD(g_part_add, g_part_apm_add),
79 KOBJMETHOD(g_part_create, g_part_apm_create),
80 KOBJMETHOD(g_part_destroy, g_part_apm_destroy),
81 KOBJMETHOD(g_part_dumpconf, g_part_apm_dumpconf),
82 KOBJMETHOD(g_part_dumpto, g_part_apm_dumpto),
83 KOBJMETHOD(g_part_modify, g_part_apm_modify),
84 KOBJMETHOD(g_part_name, g_part_apm_name),
85 KOBJMETHOD(g_part_probe, g_part_apm_probe),
86 KOBJMETHOD(g_part_read, g_part_apm_read),
87 KOBJMETHOD(g_part_type, g_part_apm_type),
88 KOBJMETHOD(g_part_write, g_part_apm_write),
92 static struct g_part_scheme g_part_apm_scheme = {
95 sizeof(struct g_part_apm_table),
96 .gps_entrysz = sizeof(struct g_part_apm_entry),
98 .gps_maxent = INT_MAX,
100 G_PART_SCHEME_DECLARE(g_part_apm);
103 apm_parse_type(const char *type, char *buf, size_t bufsz)
107 if (type[0] == '!') {
109 if (strlen(type) > bufsz)
111 if (!strcmp(type, APM_ENT_TYPE_SELF) ||
112 !strcmp(type, APM_ENT_TYPE_UNUSED))
114 strncpy(buf, type, bufsz);
117 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
118 if (!strcasecmp(type, alias)) {
119 strcpy(buf, APM_ENT_TYPE_FREEBSD);
122 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
123 if (!strcasecmp(type, alias)) {
124 strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP);
127 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
128 if (!strcasecmp(type, alias)) {
129 strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS);
132 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
133 if (!strcasecmp(type, alias)) {
134 strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM);
137 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
138 if (!strcasecmp(type, alias)) {
139 strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS);
146 apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent)
148 struct g_provider *pp;
153 buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error);
156 ent->ent_sig = be16dec(buf);
157 ent->ent_pmblkcnt = be32dec(buf + 4);
158 ent->ent_start = be32dec(buf + 8);
159 ent->ent_size = be32dec(buf + 12);
160 bcopy(buf + 16, ent->ent_name, sizeof(ent->ent_name));
161 bcopy(buf + 48, ent->ent_type, sizeof(ent->ent_type));
167 g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
168 struct g_part_parms *gpp)
170 struct g_part_apm_entry *entry;
171 struct g_part_apm_table *table;
174 entry = (struct g_part_apm_entry *)baseentry;
175 table = (struct g_part_apm_table *)basetable;
176 entry->ent.ent_sig = APM_ENT_SIG;
177 entry->ent.ent_pmblkcnt = table->self.ent_pmblkcnt;
178 entry->ent.ent_start = gpp->gpp_start;
179 entry->ent.ent_size = gpp->gpp_size;
180 if (baseentry->gpe_deleted) {
181 bzero(entry->ent.ent_type, sizeof(entry->ent.ent_type));
182 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
184 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
185 sizeof(entry->ent.ent_type));
188 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
189 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
191 strncpy(entry->ent.ent_name, gpp->gpp_label,
192 sizeof(entry->ent.ent_name));
198 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
200 struct g_provider *pp;
201 struct g_part_apm_table *table;
203 table = (struct g_part_apm_table *)basetable;
204 pp = gpp->gpp_provider;
205 if (pp->sectorsize != 512 ||
206 pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize)
209 basetable->gpt_first = 2 + basetable->gpt_entries;
210 basetable->gpt_last = (pp->mediasize / pp->sectorsize) - 1;
212 table->ddr.ddr_sig = APM_DDR_SIG;
213 table->ddr.ddr_blksize = pp->sectorsize;
214 table->ddr.ddr_blkcount = basetable->gpt_last + 1;
216 table->self.ent_sig = APM_ENT_SIG;
217 table->self.ent_pmblkcnt = basetable->gpt_entries + 1;
218 table->self.ent_start = 1;
219 table->self.ent_size = table->self.ent_pmblkcnt;
220 strcpy(table->self.ent_name, "Apple");
221 strcpy(table->self.ent_type, APM_ENT_TYPE_SELF);
226 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
229 /* Wipe the first 2 sectors to clear the partitioning. */
230 basetable->gpt_smhead |= 3;
235 g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
236 struct sbuf *sb, const char *indent)
239 char name[APM_ENT_NAMELEN + 1];
240 char type[APM_ENT_TYPELEN + 1];
242 struct g_part_apm_entry *entry;
244 entry = (struct g_part_apm_entry *)baseentry;
245 if (indent == NULL) {
246 /* conftxt: libdisk compatibility */
247 sbuf_printf(sb, " xs APPLE xt %s", entry->ent.ent_type);
248 } else if (entry != NULL) {
249 /* confxml: partition entry information */
250 strncpy(u.name, entry->ent.ent_name, APM_ENT_NAMELEN);
251 u.name[APM_ENT_NAMELEN] = '\0';
252 sbuf_printf(sb, "%s<label>%s</label>\n", indent, u.name);
253 strncpy(u.type, entry->ent.ent_type, APM_ENT_TYPELEN);
254 u.type[APM_ENT_TYPELEN] = '\0';
255 sbuf_printf(sb, "%s<rawtype>%s</rawtype>\n", indent, u.type);
257 /* confxml: scheme information */
263 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
265 struct g_part_apm_entry *entry;
267 entry = (struct g_part_apm_entry *)baseentry;
268 return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP))
273 g_part_apm_modify(struct g_part_table *basetable,
274 struct g_part_entry *baseentry, struct g_part_parms *gpp)
276 struct g_part_apm_entry *entry;
279 entry = (struct g_part_apm_entry *)baseentry;
280 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
281 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
284 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
285 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
286 sizeof(entry->ent.ent_type));
290 if (gpp->gpp_parms & G_PART_PARM_LABEL) {
291 strncpy(entry->ent.ent_name, gpp->gpp_label,
292 sizeof(entry->ent.ent_name));
298 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
299 char *buf, size_t bufsz)
302 snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
307 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
309 struct g_provider *pp;
310 struct g_part_apm_table *table;
314 /* We don't nest, which means that our depth should be 0. */
315 if (basetable->gpt_depth != 0)
318 table = (struct g_part_apm_table *)basetable;
321 /* Sanity-check the provider. */
322 if (pp->mediasize < 4 * pp->sectorsize)
325 /* Check that there's a Driver Descriptor Record (DDR). */
326 /* XXX Tivo APM drives do not have a DDR */
327 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
330 table->ddr.ddr_sig = be16dec(buf);
331 table->ddr.ddr_blksize = be16dec(buf + 2);
332 table->ddr.ddr_blkcount = be32dec(buf + 4);
334 if (table->ddr.ddr_sig != APM_DDR_SIG)
336 if (table->ddr.ddr_blksize != pp->sectorsize)
339 /* Check that there's a Partition Map. */
340 error = apm_read_ent(cp, 1, &table->self);
343 if (table->self.ent_sig != APM_ENT_SIG)
345 if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
347 if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
349 return (G_PART_PROBE_PRI_NORM);
353 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
356 struct g_part_apm_entry *entry;
357 struct g_part_apm_table *table;
360 table = (struct g_part_apm_table *)basetable;
362 basetable->gpt_first = table->self.ent_pmblkcnt + 1;
363 basetable->gpt_last = table->ddr.ddr_blkcount - 1;
364 basetable->gpt_entries = table->self.ent_pmblkcnt - 1;
366 for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
367 error = apm_read_ent(cp, index + 1, &ent);
370 if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
372 entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
373 index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
381 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
382 char *buf, size_t bufsz)
384 struct g_part_apm_entry *entry;
388 entry = (struct g_part_apm_entry *)baseentry;
389 type = entry->ent.ent_type;
390 if (!strcmp(type, APM_ENT_TYPE_FREEBSD))
391 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
392 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP))
393 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
394 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS))
395 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
396 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM))
397 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
398 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS))
399 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
401 len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
402 bcopy(type, buf + 1, len);
408 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
411 struct g_part_entry *baseentry;
412 struct g_part_apm_entry *entry;
413 struct g_part_apm_table *table;
416 table = (struct g_part_apm_table *)basetable;
417 bzero(buf, sizeof(buf));
419 /* Write the DDR and 'self' entry only when we're newly created. */
420 if (basetable->gpt_created) {
421 be16enc(buf, table->ddr.ddr_sig);
422 be16enc(buf + 2, table->ddr.ddr_blksize);
423 be32enc(buf + 4, table->ddr.ddr_blkcount);
424 error = g_write_data(cp, 0, buf, sizeof(buf));
429 be16enc(buf, table->self.ent_sig);
431 be32enc(buf + 4, table->self.ent_pmblkcnt);
433 if (basetable->gpt_created) {
434 be32enc(buf + 8, table->self.ent_start);
435 be32enc(buf + 12, table->self.ent_size);
436 bcopy(table->self.ent_name, buf + 16,
437 sizeof(table->self.ent_name));
438 bcopy(table->self.ent_type, buf + 48,
439 sizeof(table->self.ent_type));
440 error = g_write_data(cp, 512, buf, sizeof(buf));
445 baseentry = LIST_FIRST(&basetable->gpt_entry);
446 for (index = 1; index <= basetable->gpt_entries; index++) {
447 entry = (baseentry != NULL && index == baseentry->gpe_index)
448 ? (struct g_part_apm_entry *)baseentry : NULL;
449 if (entry != NULL && !baseentry->gpe_deleted) {
450 be32enc(buf + 8, entry->ent.ent_start);
451 be32enc(buf + 12, entry->ent.ent_size);
452 bcopy(entry->ent.ent_name, buf + 16,
453 sizeof(entry->ent.ent_name));
454 bcopy(entry->ent.ent_type, buf + 48,
455 sizeof(entry->ent.ent_type));
457 bzero(buf + 8, 4 + 4 + 32 + 32);
458 strcpy(buf + 48, APM_ENT_TYPE_UNUSED);
460 error = g_write_data(cp, (index + 1) * 512, buf, sizeof(buf));
464 baseentry = LIST_NEXT(baseentry, gpe_entry);