2 * Copyright (c) 2007 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>
32 #include <sys/disklabel.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/queue.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <geom/geom.h>
45 #include <geom/part/g_part.h>
47 #include "g_part_if.h"
49 #define BOOT1_SIZE 512
50 #define LABEL_SIZE 512
51 #define BOOT2_OFF (BOOT1_SIZE + LABEL_SIZE)
52 #define BOOT2_SIZE (BBSIZE - BOOT2_OFF)
54 FEATURE(geom_part_bsd, "GEOM partitioning class for BSD disklabels");
56 struct g_part_bsd_table {
57 struct g_part_table base;
62 struct g_part_bsd_entry {
63 struct g_part_entry base;
64 struct partition part;
67 static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *,
68 struct g_part_parms *);
69 static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *);
70 static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *);
71 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
72 static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
73 struct sbuf *, const char *);
74 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
75 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,
76 struct g_part_parms *);
77 static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
79 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
80 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
81 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
83 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
84 static int g_part_bsd_resize(struct g_part_table *, struct g_part_entry *,
85 struct g_part_parms *);
87 static kobj_method_t g_part_bsd_methods[] = {
88 KOBJMETHOD(g_part_add, g_part_bsd_add),
89 KOBJMETHOD(g_part_bootcode, g_part_bsd_bootcode),
90 KOBJMETHOD(g_part_create, g_part_bsd_create),
91 KOBJMETHOD(g_part_destroy, g_part_bsd_destroy),
92 KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf),
93 KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto),
94 KOBJMETHOD(g_part_modify, g_part_bsd_modify),
95 KOBJMETHOD(g_part_resize, g_part_bsd_resize),
96 KOBJMETHOD(g_part_name, g_part_bsd_name),
97 KOBJMETHOD(g_part_probe, g_part_bsd_probe),
98 KOBJMETHOD(g_part_read, g_part_bsd_read),
99 KOBJMETHOD(g_part_type, g_part_bsd_type),
100 KOBJMETHOD(g_part_write, g_part_bsd_write),
104 static struct g_part_scheme g_part_bsd_scheme = {
107 sizeof(struct g_part_bsd_table),
108 .gps_entrysz = sizeof(struct g_part_bsd_entry),
110 .gps_maxent = 20, /* Only 22 entries fit in 512 byte sectors */
111 .gps_bootcodesz = BBSIZE,
113 G_PART_SCHEME_DECLARE(g_part_bsd);
115 static struct g_part_bsd_alias {
118 } bsd_alias_match[] = {
119 { FS_BSDFFS, G_PART_ALIAS_FREEBSD_UFS },
120 { FS_SWAP, G_PART_ALIAS_FREEBSD_SWAP },
121 { FS_ZFS, G_PART_ALIAS_FREEBSD_ZFS },
122 { FS_VINUM, G_PART_ALIAS_FREEBSD_VINUM },
123 { FS_NANDFS, G_PART_ALIAS_FREEBSD_NANDFS },
124 { FS_HAMMER, G_PART_ALIAS_DFBSD_HAMMER },
125 { FS_HAMMER2, G_PART_ALIAS_DFBSD_HAMMER2 },
129 bsd_parse_type(const char *type, uint8_t *fstype)
136 if (type[0] == '!') {
137 lt = strtol(type + 1, &endp, 0);
138 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
143 for (i = 0; i < nitems(bsd_alias_match); i++) {
144 alias = g_part_alias_name(bsd_alias_match[i].alias);
145 if (strcasecmp(type, alias) == 0) {
146 *fstype = bsd_alias_match[i].type;
154 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
155 struct g_part_parms *gpp)
157 struct g_part_bsd_entry *entry;
158 struct g_part_bsd_table *table;
160 if (gpp->gpp_parms & G_PART_PARM_LABEL)
163 entry = (struct g_part_bsd_entry *)baseentry;
164 table = (struct g_part_bsd_table *)basetable;
166 entry->part.p_size = gpp->gpp_size;
167 entry->part.p_offset = gpp->gpp_start + table->offset;
168 entry->part.p_fsize = 0;
169 entry->part.p_frag = 0;
170 entry->part.p_cpg = 0;
171 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
175 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
177 struct g_part_bsd_table *table;
178 const u_char *codeptr;
180 if (gpp->gpp_codesize != BOOT1_SIZE && gpp->gpp_codesize != BBSIZE)
183 table = (struct g_part_bsd_table *)basetable;
184 codeptr = gpp->gpp_codeptr;
185 bcopy(codeptr, table->bbarea, BOOT1_SIZE);
186 if (gpp->gpp_codesize == BBSIZE)
187 bcopy(codeptr + BOOT2_OFF, table->bbarea + BOOT2_OFF,
193 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
195 struct g_provider *pp;
196 struct g_part_entry *baseentry;
197 struct g_part_bsd_entry *entry;
198 struct g_part_bsd_table *table;
200 uint32_t msize, ncyls, secpercyl;
202 pp = gpp->gpp_provider;
204 if (pp->sectorsize < sizeof(struct disklabel))
206 if (BBSIZE % pp->sectorsize)
209 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
210 secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
211 ncyls = msize / secpercyl;
213 table = (struct g_part_bsd_table *)basetable;
214 table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
215 ptr = table->bbarea + pp->sectorsize;
217 le32enc(ptr + 0, DISKMAGIC); /* d_magic */
218 le32enc(ptr + 40, pp->sectorsize); /* d_secsize */
219 le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */
220 le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */
221 le32enc(ptr + 52, ncyls); /* d_ncylinders */
222 le32enc(ptr + 56, secpercyl); /* d_secpercyl */
223 le32enc(ptr + 60, msize); /* d_secperunit */
224 le16enc(ptr + 72, 3600); /* d_rpm */
225 le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */
226 le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */
227 le32enc(ptr + 140, BBSIZE); /* d_bbsize */
229 basetable->gpt_first = 0;
230 basetable->gpt_last = msize - 1;
231 basetable->gpt_isleaf = 1;
233 baseentry = g_part_new_entry(basetable, RAW_PART + 1,
234 basetable->gpt_first, basetable->gpt_last);
235 baseentry->gpe_internal = 1;
236 entry = (struct g_part_bsd_entry *)baseentry;
237 entry->part.p_size = basetable->gpt_last + 1;
238 entry->part.p_offset = table->offset;
244 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
246 struct g_part_bsd_table *table;
248 table = (struct g_part_bsd_table *)basetable;
249 if (table->bbarea != NULL)
250 g_free(table->bbarea);
251 table->bbarea = NULL;
253 /* Wipe the second sector to clear the partitioning. */
254 basetable->gpt_smhead |= 2;
259 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
260 struct sbuf *sb, const char *indent)
262 struct g_part_bsd_entry *entry;
264 entry = (struct g_part_bsd_entry *)baseentry;
265 if (indent == NULL) {
266 /* conftxt: libdisk compatibility */
267 sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
268 } else if (entry != NULL) {
269 /* confxml: partition entry information */
270 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
271 entry->part.p_fstype);
273 /* confxml: scheme information */
278 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
280 struct g_part_bsd_entry *entry;
282 /* Allow dumping to a swap partition or an unused partition. */
283 entry = (struct g_part_bsd_entry *)baseentry;
284 return ((entry->part.p_fstype == FS_UNUSED ||
285 entry->part.p_fstype == FS_SWAP) ? 1 : 0);
289 g_part_bsd_modify(struct g_part_table *basetable,
290 struct g_part_entry *baseentry, struct g_part_parms *gpp)
292 struct g_part_bsd_entry *entry;
294 if (gpp->gpp_parms & G_PART_PARM_LABEL)
297 entry = (struct g_part_bsd_entry *)baseentry;
298 if (gpp->gpp_parms & G_PART_PARM_TYPE)
299 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
304 bsd_set_rawsize(struct g_part_table *basetable, struct g_provider *pp)
306 struct g_part_bsd_table *table;
307 struct g_part_bsd_entry *entry;
308 struct g_part_entry *baseentry;
311 table = (struct g_part_bsd_table *)basetable;
312 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
313 le32enc(table->bbarea + pp->sectorsize + 60, msize); /* d_secperunit */
314 basetable->gpt_last = msize - 1;
315 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
316 if (baseentry->gpe_index != RAW_PART + 1)
318 baseentry->gpe_end = basetable->gpt_last;
319 entry = (struct g_part_bsd_entry *)baseentry;
320 entry->part.p_size = msize;
326 g_part_bsd_resize(struct g_part_table *basetable,
327 struct g_part_entry *baseentry, struct g_part_parms *gpp)
329 struct g_part_bsd_entry *entry;
330 struct g_provider *pp;
332 if (baseentry == NULL) {
333 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
334 bsd_set_rawsize(basetable, pp);
337 entry = (struct g_part_bsd_entry *)baseentry;
338 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
339 entry->part.p_size = gpp->gpp_size;
345 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
346 char *buf, size_t bufsz)
349 snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
354 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
356 struct g_provider *pp;
358 uint32_t magic1, magic2;
363 /* Sanity-check the provider. */
364 if (pp->sectorsize < sizeof(struct disklabel) ||
365 pp->mediasize < BBSIZE)
367 if (BBSIZE % pp->sectorsize)
370 /* Check that there's a disklabel. */
371 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
374 magic1 = le32dec(buf + 0);
375 magic2 = le32dec(buf + 132);
377 return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
378 ? G_PART_PROBE_PRI_HIGH : ENXIO);
382 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
384 struct g_provider *pp;
385 struct g_part_bsd_table *table;
386 struct g_part_entry *baseentry;
387 struct g_part_bsd_entry *entry;
388 struct partition part;
391 u_int sectors, heads;
395 table = (struct g_part_bsd_table *)basetable;
396 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
398 table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
399 if (table->bbarea == NULL)
402 buf = table->bbarea + pp->sectorsize;
404 if (le32dec(buf + 40) != pp->sectorsize)
406 sectors = le32dec(buf + 44);
407 if (sectors < 1 || sectors > 255)
409 if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
410 g_part_geometry_heads(msize, sectors, &chs, &heads);
412 basetable->gpt_sectors = sectors;
413 basetable->gpt_heads = heads;
416 heads = le32dec(buf + 48);
417 if (heads < 1 || heads > 255)
419 if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
420 basetable->gpt_heads = heads;
422 chs = le32dec(buf + 60);
425 /* Fix-up a sysinstall bug. */
428 le32enc(buf + 60, msize);
431 basetable->gpt_first = 0;
432 basetable->gpt_last = msize - 1;
433 basetable->gpt_isleaf = 1;
435 basetable->gpt_entries = le16dec(buf + 138);
436 if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
437 basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
440 table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
441 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
442 p = buf + 148 + index * 16;
443 part.p_size = le32dec(p + 0);
444 part.p_offset = le32dec(p + 4);
445 part.p_fsize = le32dec(p + 8);
446 part.p_fstype = p[12];
448 part.p_cpg = le16dec(p + 14);
449 if (part.p_size == 0)
451 if (part.p_offset < table->offset)
453 if (part.p_offset - table->offset > basetable->gpt_last)
455 baseentry = g_part_new_entry(basetable, index + 1,
456 part.p_offset - table->offset,
457 part.p_offset - table->offset + part.p_size - 1);
458 entry = (struct g_part_bsd_entry *)baseentry;
460 if (index == RAW_PART)
461 baseentry->gpe_internal = 1;
467 printf("GEOM: %s: invalid disklabel.\n", pp->name);
468 g_free(table->bbarea);
469 table->bbarea = NULL;
474 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
475 char *buf, size_t bufsz)
477 struct g_part_bsd_entry *entry;
480 entry = (struct g_part_bsd_entry *)baseentry;
481 type = entry->part.p_fstype;
482 if (type == FS_NANDFS)
483 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
485 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
486 if (type == FS_BSDFFS)
487 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
488 if (type == FS_VINUM)
489 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
491 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
492 snprintf(buf, bufsz, "!%d", type);
497 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
499 struct g_provider *pp;
500 struct g_part_entry *baseentry;
501 struct g_part_bsd_entry *entry;
502 struct g_part_bsd_table *table;
504 u_char *label, *p, *pe;
508 table = (struct g_part_bsd_table *)basetable;
509 baseentry = LIST_FIRST(&basetable->gpt_entry);
510 label = table->bbarea + pp->sectorsize;
511 for (index = 1; index <= basetable->gpt_entries; index++) {
512 p = label + 148 + (index - 1) * 16;
513 entry = (baseentry != NULL && index == baseentry->gpe_index)
514 ? (struct g_part_bsd_entry *)baseentry : NULL;
515 if (entry != NULL && !baseentry->gpe_deleted) {
516 le32enc(p + 0, entry->part.p_size);
517 le32enc(p + 4, entry->part.p_offset);
518 le32enc(p + 8, entry->part.p_fsize);
519 p[12] = entry->part.p_fstype;
520 p[13] = entry->part.p_frag;
521 le16enc(p + 14, entry->part.p_cpg);
526 baseentry = LIST_NEXT(baseentry, gpe_entry);
529 /* Calculate checksum. */
530 le16enc(label + 136, 0);
531 pe = label + 148 + basetable->gpt_entries * 16;
533 for (p = label; p < pe; p += 2)
535 le16enc(label + 136, sum);
537 error = g_write_data(cp, 0, table->bbarea, BBSIZE);