2 * Copyright (c) 2007-2009 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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/diskmbr.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
38 #include <sys/limits.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/queue.h>
44 #include <sys/systm.h>
45 #include <geom/geom.h>
46 #include <geom/part/g_part.h>
48 #include "g_part_if.h"
52 struct g_part_ebr_table {
53 struct g_part_table base;
56 struct g_part_ebr_entry {
57 struct g_part_entry base;
58 struct dos_partition ent;
61 static int g_part_ebr_add(struct g_part_table *, struct g_part_entry *,
62 struct g_part_parms *);
63 static int g_part_ebr_create(struct g_part_table *, struct g_part_parms *);
64 static int g_part_ebr_destroy(struct g_part_table *, struct g_part_parms *);
65 static void g_part_ebr_dumpconf(struct g_part_table *, struct g_part_entry *,
66 struct sbuf *, const char *);
67 static int g_part_ebr_dumpto(struct g_part_table *, struct g_part_entry *);
68 #if defined(GEOM_PART_EBR_COMPAT)
69 static void g_part_ebr_fullname(struct g_part_table *, struct g_part_entry *,
70 struct sbuf *, const char *);
72 static int g_part_ebr_modify(struct g_part_table *, struct g_part_entry *,
73 struct g_part_parms *);
74 static const char *g_part_ebr_name(struct g_part_table *, struct g_part_entry *,
76 static int g_part_ebr_precheck(struct g_part_table *, enum g_part_ctl,
77 struct g_part_parms *);
78 static int g_part_ebr_probe(struct g_part_table *, struct g_consumer *);
79 static int g_part_ebr_read(struct g_part_table *, struct g_consumer *);
80 static int g_part_ebr_setunset(struct g_part_table *, struct g_part_entry *,
81 const char *, unsigned int);
82 static const char *g_part_ebr_type(struct g_part_table *, struct g_part_entry *,
84 static int g_part_ebr_write(struct g_part_table *, struct g_consumer *);
86 static kobj_method_t g_part_ebr_methods[] = {
87 KOBJMETHOD(g_part_add, g_part_ebr_add),
88 KOBJMETHOD(g_part_create, g_part_ebr_create),
89 KOBJMETHOD(g_part_destroy, g_part_ebr_destroy),
90 KOBJMETHOD(g_part_dumpconf, g_part_ebr_dumpconf),
91 KOBJMETHOD(g_part_dumpto, g_part_ebr_dumpto),
92 #if defined(GEOM_PART_EBR_COMPAT)
93 KOBJMETHOD(g_part_fullname, g_part_ebr_fullname),
95 KOBJMETHOD(g_part_modify, g_part_ebr_modify),
96 KOBJMETHOD(g_part_name, g_part_ebr_name),
97 KOBJMETHOD(g_part_precheck, g_part_ebr_precheck),
98 KOBJMETHOD(g_part_probe, g_part_ebr_probe),
99 KOBJMETHOD(g_part_read, g_part_ebr_read),
100 KOBJMETHOD(g_part_setunset, g_part_ebr_setunset),
101 KOBJMETHOD(g_part_type, g_part_ebr_type),
102 KOBJMETHOD(g_part_write, g_part_ebr_write),
106 static struct g_part_scheme g_part_ebr_scheme = {
109 sizeof(struct g_part_ebr_table),
110 .gps_entrysz = sizeof(struct g_part_ebr_entry),
112 .gps_maxent = INT_MAX,
114 G_PART_SCHEME_DECLARE(g_part_ebr);
116 static void ebr_set_chs(struct g_part_table *, uint32_t, u_char *, u_char *,
120 ebr_entry_decode(const char *p, struct dos_partition *ent)
124 ent->dp_ssect = p[2];
128 ent->dp_esect = p[6];
130 ent->dp_start = le32dec(p + 8);
131 ent->dp_size = le32dec(p + 12);
135 ebr_entry_link(struct g_part_table *table, uint32_t start, uint32_t end,
139 buf[0] = 0 /* dp_flag */;
140 ebr_set_chs(table, start, &buf[3] /* dp_scyl */, &buf[1] /* dp_shd */,
141 &buf[2] /* dp_ssect */);
142 buf[4] = 5 /* dp_typ */;
143 ebr_set_chs(table, end, &buf[7] /* dp_ecyl */, &buf[5] /* dp_ehd */,
144 &buf[6] /* dp_esect */);
145 le32enc(buf + 8, start);
146 le32enc(buf + 12, end - start + 1);
150 ebr_parse_type(const char *type, u_char *dp_typ)
156 if (type[0] == '!') {
157 lt = strtol(type + 1, &endp, 0);
158 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
160 *dp_typ = (u_char)lt;
163 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
164 if (!strcasecmp(type, alias)) {
165 *dp_typ = DOSPTYP_386BSD;
172 ebr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
175 uint32_t cyl, hd, sec;
177 sec = lba % table->gpt_sectors + 1;
178 lba /= table->gpt_sectors;
179 hd = lba % table->gpt_heads;
180 lba /= table->gpt_heads;
187 *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
191 g_part_ebr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
192 struct g_part_parms *gpp)
195 struct g_provider *pp;
196 struct g_part_ebr_entry *entry;
197 uint32_t start, size, sectors;
199 if (gpp->gpp_parms & G_PART_PARM_LABEL)
202 gp = basetable->gpt_gp;
203 pp = LIST_FIRST(&gp->consumer)->provider;
204 sectors = basetable->gpt_sectors;
206 entry = (struct g_part_ebr_entry *)baseentry;
208 start = gpp->gpp_start;
209 size = gpp->gpp_size;
210 if (size < 2 * sectors)
212 if (start % sectors) {
213 size = size - sectors + (start % sectors);
214 start = start - (start % sectors) + sectors;
217 size = size - (size % sectors);
218 if (size < 2 * sectors)
221 if (baseentry->gpe_deleted)
222 bzero(&entry->ent, sizeof(entry->ent));
224 KASSERT(baseentry->gpe_start <= start, (__func__));
225 KASSERT(baseentry->gpe_end >= start + size - 1, (__func__));
226 baseentry->gpe_index = (start / sectors) + 1;
227 baseentry->gpe_offset = (off_t)(start + sectors) * pp->sectorsize;
228 baseentry->gpe_start = start;
229 baseentry->gpe_end = start + size - 1;
230 entry->ent.dp_start = sectors;
231 entry->ent.dp_size = size - sectors;
232 ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl,
233 &entry->ent.dp_shd, &entry->ent.dp_ssect);
234 ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
235 &entry->ent.dp_ehd, &entry->ent.dp_esect);
236 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
240 g_part_ebr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
243 struct g_consumer *cp;
244 struct g_provider *pp;
248 pp = gpp->gpp_provider;
250 if (pp->sectorsize < EBRSIZE)
252 if (pp->sectorsize > 4096)
255 /* Check that we have a parent and that it's a MBR. */
256 if (basetable->gpt_depth == 0)
258 cp = LIST_FIRST(&pp->consumers);
259 error = g_getattr("PART::scheme", cp, &psn);
262 if (strcmp(psn, "MBR"))
265 msize = MIN(pp->mediasize / pp->sectorsize, 0xffffffff);
266 msize -= msize % basetable->gpt_sectors;
267 basetable->gpt_first = 0;
268 basetable->gpt_last = msize - 1;
269 basetable->gpt_entries = msize / basetable->gpt_sectors;
274 g_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
277 /* Wipe the first sector to clear the partitioning. */
278 basetable->gpt_smhead |= 1;
283 g_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
284 struct sbuf *sb, const char *indent)
286 struct g_part_ebr_entry *entry;
288 entry = (struct g_part_ebr_entry *)baseentry;
289 if (indent == NULL) {
290 /* conftxt: libdisk compatibility */
291 sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ);
292 } else if (entry != NULL) {
293 /* confxml: partition entry information */
294 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
296 if (entry->ent.dp_flag & 0x80)
297 sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
299 /* confxml: scheme information */
304 g_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
306 struct g_part_ebr_entry *entry;
308 /* Allow dumping to a FreeBSD partition only. */
309 entry = (struct g_part_ebr_entry *)baseentry;
310 return ((entry->ent.dp_typ == DOSPTYP_386BSD) ? 1 : 0);
313 #if defined(GEOM_PART_EBR_COMPAT)
315 g_part_ebr_fullname(struct g_part_table *table, struct g_part_entry *entry,
316 struct sbuf *sb, const char *pfx)
318 struct g_part_entry *iter;
322 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
327 sbuf_printf(sb, "%.*s%u", (int)strlen(pfx) - 1, pfx, idx);
332 g_part_ebr_modify(struct g_part_table *basetable,
333 struct g_part_entry *baseentry, struct g_part_parms *gpp)
335 struct g_part_ebr_entry *entry;
337 if (gpp->gpp_parms & G_PART_PARM_LABEL)
340 entry = (struct g_part_ebr_entry *)baseentry;
341 if (gpp->gpp_parms & G_PART_PARM_TYPE)
342 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
347 g_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry,
348 char *buf, size_t bufsz)
351 snprintf(buf, bufsz, "+%08u", entry->gpe_index);
356 g_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req,
357 struct g_part_parms *gpp)
359 #if defined(GEOM_PART_EBR_COMPAT)
363 * The index is a function of the start of the partition.
364 * This is not something the user can override, nor is it
365 * something the common code will do right. We can set the
366 * index now so that we get what we need.
368 if (req == G_PART_CTL_ADD)
369 gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1;
375 g_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp)
378 struct g_provider *pp;
380 int error, index, res, sum;
385 /* Sanity-check the provider. */
386 if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize)
388 if (pp->sectorsize > 4096)
391 /* Check that we have a parent and that it's a MBR. */
392 if (table->gpt_depth == 0)
394 error = g_getattr("PART::scheme", cp, &psn);
397 if (strcmp(psn, "MBR"))
400 /* Check that there's a EBR. */
401 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
405 /* We goto out on mismatch. */
408 magic = le16dec(buf + DOSMAGICOFFSET);
409 if (magic != DOSMAGIC)
413 * The sector is all zeroes, except for the partition entries,
414 * pseudo boot code and some signatures or disk serial number.
415 * The latter can be found in the 9 bytes immediately in front
416 * of the partition table.
419 for (index = 96; index < DOSPARTOFF - 9; index++)
424 for (index = 0; index < NDOSPART; index++) {
425 p = buf + DOSPARTOFF + index * DOSPARTSIZE;
426 if (p[0] != 0 && p[0] != 0x80)
430 /* The 3rd & 4th entries are always zero. */
431 if ((le64dec(p+0) + le64dec(p+8)) != 0)
435 res = G_PART_PROBE_PRI_NORM;
443 g_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp)
445 struct dos_partition ent[2];
446 struct g_provider *pp;
447 struct g_part_entry *baseentry;
448 struct g_part_ebr_table *table;
449 struct g_part_ebr_entry *entry;
456 table = (struct g_part_ebr_table *)basetable;
457 msize = pp->mediasize / pp->sectorsize;
461 ofs = (off_t)lba * pp->sectorsize;
462 buf = g_read_data(cp, ofs, pp->sectorsize, &error);
466 ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0);
467 ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1);
470 if (ent[0].dp_typ == 0)
473 if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) {
474 lba = ent[0].dp_start;
478 index = (lba / basetable->gpt_sectors) + 1;
479 baseentry = (struct g_part_entry *)g_part_new_entry(basetable,
480 index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1);
481 baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) *
483 entry = (struct g_part_ebr_entry *)baseentry;
486 if (ent[1].dp_typ == 0)
489 lba = ent[1].dp_start;
492 basetable->gpt_entries = msize / basetable->gpt_sectors;
493 basetable->gpt_first = 0;
494 basetable->gpt_last = msize - (msize % basetable->gpt_sectors) - 1;
499 g_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
500 const char *attrib, unsigned int set)
502 struct g_part_entry *iter;
503 struct g_part_ebr_entry *entry;
506 if (strcasecmp(attrib, "active") != 0)
509 /* Only one entry can have the active attribute. */
510 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
511 if (iter->gpe_deleted)
514 entry = (struct g_part_ebr_entry *)iter;
515 if (iter == baseentry) {
516 if (set && (entry->ent.dp_flag & 0x80) == 0) {
517 entry->ent.dp_flag |= 0x80;
519 } else if (!set && (entry->ent.dp_flag & 0x80)) {
520 entry->ent.dp_flag &= ~0x80;
524 if (set && (entry->ent.dp_flag & 0x80)) {
525 entry->ent.dp_flag &= ~0x80;
529 if (changed && !iter->gpe_created)
530 iter->gpe_modified = 1;
536 g_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
537 char *buf, size_t bufsz)
539 struct g_part_ebr_entry *entry;
542 entry = (struct g_part_ebr_entry *)baseentry;
543 type = entry->ent.dp_typ;
544 if (type == DOSPTYP_386BSD)
545 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
546 snprintf(buf, bufsz, "!%d", type);
551 g_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp)
553 struct g_provider *pp;
554 struct g_part_entry *baseentry, *next;
555 struct g_part_ebr_entry *entry;
561 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
562 le16enc(buf + DOSMAGICOFFSET, DOSMAGIC);
564 baseentry = LIST_FIRST(&basetable->gpt_entry);
565 while (baseentry != NULL && baseentry->gpe_deleted)
566 baseentry = LIST_NEXT(baseentry, gpe_entry);
568 /* Wipe-out the the first EBR when there are no slices. */
569 if (baseentry == NULL) {
570 error = g_write_data(cp, 0, buf, pp->sectorsize);
575 * If the first partition is not in LBA 0, we need to
576 * put a "link" EBR in LBA 0.
578 if (baseentry->gpe_start != 0) {
579 ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start,
580 (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF);
581 error = g_write_data(cp, 0, buf, pp->sectorsize);
587 entry = (struct g_part_ebr_entry *)baseentry;
589 p = buf + DOSPARTOFF;
590 p[0] = entry->ent.dp_flag;
591 p[1] = entry->ent.dp_shd;
592 p[2] = entry->ent.dp_ssect;
593 p[3] = entry->ent.dp_scyl;
594 p[4] = entry->ent.dp_typ;
595 p[5] = entry->ent.dp_ehd;
596 p[6] = entry->ent.dp_esect;
597 p[7] = entry->ent.dp_ecyl;
598 le32enc(p + 8, entry->ent.dp_start);
599 le32enc(p + 12, entry->ent.dp_size);
601 next = LIST_NEXT(baseentry, gpe_entry);
602 while (next != NULL && next->gpe_deleted)
603 next = LIST_NEXT(next, gpe_entry);
607 ebr_entry_link(basetable, (uint32_t)next->gpe_start,
608 (uint32_t)next->gpe_end, p);
610 bzero(p, DOSPARTSIZE);
612 error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize,
613 buf, pp->sectorsize);
616 } while (!error && baseentry != NULL);