2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * 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/disklabel.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 <sys/sysctl.h>
46 #include <geom/geom.h>
47 #include <geom/part/g_part.h>
49 #include "g_part_if.h"
51 #define BOOT1_SIZE 512
52 #define LABEL_SIZE 512
53 #define BOOT2_OFF (BOOT1_SIZE + LABEL_SIZE)
54 #define BOOT2_SIZE (BBSIZE - BOOT2_OFF)
56 FEATURE(geom_part_bsd, "GEOM partitioning class for BSD disklabels");
58 struct g_part_bsd_table {
59 struct g_part_table base;
64 struct g_part_bsd_entry {
65 struct g_part_entry base;
66 struct partition part;
69 static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *,
70 struct g_part_parms *);
71 static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *);
72 static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *);
73 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
74 static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
75 struct sbuf *, const char *);
76 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
77 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,
78 struct g_part_parms *);
79 static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
81 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
82 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
83 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
85 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
86 static int g_part_bsd_resize(struct g_part_table *, struct g_part_entry *,
87 struct g_part_parms *);
89 static kobj_method_t g_part_bsd_methods[] = {
90 KOBJMETHOD(g_part_add, g_part_bsd_add),
91 KOBJMETHOD(g_part_bootcode, g_part_bsd_bootcode),
92 KOBJMETHOD(g_part_create, g_part_bsd_create),
93 KOBJMETHOD(g_part_destroy, g_part_bsd_destroy),
94 KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf),
95 KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto),
96 KOBJMETHOD(g_part_modify, g_part_bsd_modify),
97 KOBJMETHOD(g_part_resize, g_part_bsd_resize),
98 KOBJMETHOD(g_part_name, g_part_bsd_name),
99 KOBJMETHOD(g_part_probe, g_part_bsd_probe),
100 KOBJMETHOD(g_part_read, g_part_bsd_read),
101 KOBJMETHOD(g_part_type, g_part_bsd_type),
102 KOBJMETHOD(g_part_write, g_part_bsd_write),
106 static struct g_part_scheme g_part_bsd_scheme = {
109 sizeof(struct g_part_bsd_table),
110 .gps_entrysz = sizeof(struct g_part_bsd_entry),
112 .gps_maxent = 20, /* Only 22 entries fit in 512 byte sectors */
113 .gps_bootcodesz = BBSIZE,
115 G_PART_SCHEME_DECLARE(g_part_bsd);
116 MODULE_VERSION(geom_part_bsd, 0);
118 static struct g_part_bsd_alias {
121 } bsd_alias_match[] = {
122 { FS_BSDFFS, G_PART_ALIAS_FREEBSD_UFS },
123 { FS_SWAP, G_PART_ALIAS_FREEBSD_SWAP },
124 { FS_ZFS, G_PART_ALIAS_FREEBSD_ZFS },
125 { FS_VINUM, G_PART_ALIAS_FREEBSD_VINUM },
126 { FS_NANDFS, G_PART_ALIAS_FREEBSD_NANDFS },
127 { FS_HAMMER, G_PART_ALIAS_DFBSD_HAMMER },
128 { FS_HAMMER2, G_PART_ALIAS_DFBSD_HAMMER2 },
132 bsd_parse_type(const char *type, uint8_t *fstype)
139 if (type[0] == '!') {
140 lt = strtol(type + 1, &endp, 0);
141 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
146 for (i = 0; i < nitems(bsd_alias_match); i++) {
147 alias = g_part_alias_name(bsd_alias_match[i].alias);
148 if (strcasecmp(type, alias) == 0) {
149 *fstype = bsd_alias_match[i].type;
157 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
158 struct g_part_parms *gpp)
160 struct g_part_bsd_entry *entry;
161 struct g_part_bsd_table *table;
163 if (gpp->gpp_parms & G_PART_PARM_LABEL)
166 entry = (struct g_part_bsd_entry *)baseentry;
167 table = (struct g_part_bsd_table *)basetable;
169 entry->part.p_size = gpp->gpp_size;
170 entry->part.p_offset = gpp->gpp_start + table->offset;
171 entry->part.p_fsize = 0;
172 entry->part.p_frag = 0;
173 entry->part.p_cpg = 0;
174 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
178 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
180 struct g_part_bsd_table *table;
181 const u_char *codeptr;
183 if (gpp->gpp_codesize != BOOT1_SIZE && gpp->gpp_codesize != BBSIZE)
186 table = (struct g_part_bsd_table *)basetable;
187 codeptr = gpp->gpp_codeptr;
188 bcopy(codeptr, table->bbarea, BOOT1_SIZE);
189 if (gpp->gpp_codesize == BBSIZE)
190 bcopy(codeptr + BOOT2_OFF, table->bbarea + BOOT2_OFF,
196 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
198 struct g_provider *pp;
199 struct g_part_entry *baseentry;
200 struct g_part_bsd_entry *entry;
201 struct g_part_bsd_table *table;
203 uint32_t msize, ncyls, secpercyl;
205 pp = gpp->gpp_provider;
207 if (pp->sectorsize < sizeof(struct disklabel))
209 if (BBSIZE % pp->sectorsize)
212 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
213 secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
214 ncyls = msize / secpercyl;
216 table = (struct g_part_bsd_table *)basetable;
217 table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
218 ptr = table->bbarea + pp->sectorsize;
220 le32enc(ptr + 0, DISKMAGIC); /* d_magic */
221 le32enc(ptr + 40, pp->sectorsize); /* d_secsize */
222 le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */
223 le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */
224 le32enc(ptr + 52, ncyls); /* d_ncylinders */
225 le32enc(ptr + 56, secpercyl); /* d_secpercyl */
226 le32enc(ptr + 60, msize); /* d_secperunit */
227 le16enc(ptr + 72, 3600); /* d_rpm */
228 le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */
229 le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */
230 le32enc(ptr + 140, BBSIZE); /* d_bbsize */
232 basetable->gpt_first = 0;
233 basetable->gpt_last = msize - 1;
234 basetable->gpt_isleaf = 1;
236 baseentry = g_part_new_entry(basetable, RAW_PART + 1,
237 basetable->gpt_first, basetable->gpt_last);
238 baseentry->gpe_internal = 1;
239 entry = (struct g_part_bsd_entry *)baseentry;
240 entry->part.p_size = basetable->gpt_last + 1;
241 entry->part.p_offset = table->offset;
247 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
249 struct g_part_bsd_table *table;
251 table = (struct g_part_bsd_table *)basetable;
252 if (table->bbarea != NULL)
253 g_free(table->bbarea);
254 table->bbarea = NULL;
256 /* Wipe the second sector to clear the partitioning. */
257 basetable->gpt_smhead |= 2;
262 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
263 struct sbuf *sb, const char *indent)
265 struct g_part_bsd_entry *entry;
267 entry = (struct g_part_bsd_entry *)baseentry;
268 if (indent == NULL) {
269 /* conftxt: libdisk compatibility */
270 sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
271 } else if (entry != NULL) {
272 /* confxml: partition entry information */
273 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
274 entry->part.p_fstype);
276 /* confxml: scheme information */
281 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
283 struct g_part_bsd_entry *entry;
285 /* Allow dumping to a swap partition or an unused partition. */
286 entry = (struct g_part_bsd_entry *)baseentry;
287 return ((entry->part.p_fstype == FS_UNUSED ||
288 entry->part.p_fstype == FS_SWAP) ? 1 : 0);
292 g_part_bsd_modify(struct g_part_table *basetable,
293 struct g_part_entry *baseentry, struct g_part_parms *gpp)
295 struct g_part_bsd_entry *entry;
297 if (gpp->gpp_parms & G_PART_PARM_LABEL)
300 entry = (struct g_part_bsd_entry *)baseentry;
301 if (gpp->gpp_parms & G_PART_PARM_TYPE)
302 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
307 bsd_set_rawsize(struct g_part_table *basetable, struct g_provider *pp)
309 struct g_part_bsd_table *table;
310 struct g_part_bsd_entry *entry;
311 struct g_part_entry *baseentry;
314 table = (struct g_part_bsd_table *)basetable;
315 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
316 le32enc(table->bbarea + pp->sectorsize + 60, msize); /* d_secperunit */
317 basetable->gpt_last = msize - 1;
318 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
319 if (baseentry->gpe_index != RAW_PART + 1)
321 baseentry->gpe_end = basetable->gpt_last;
322 entry = (struct g_part_bsd_entry *)baseentry;
323 entry->part.p_size = msize;
329 g_part_bsd_resize(struct g_part_table *basetable,
330 struct g_part_entry *baseentry, struct g_part_parms *gpp)
332 struct g_part_bsd_entry *entry;
333 struct g_provider *pp;
335 if (baseentry == NULL) {
336 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
337 bsd_set_rawsize(basetable, pp);
340 entry = (struct g_part_bsd_entry *)baseentry;
341 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
342 entry->part.p_size = gpp->gpp_size;
348 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
349 char *buf, size_t bufsz)
352 snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
357 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
359 struct g_provider *pp;
361 uint32_t magic1, magic2;
366 /* Sanity-check the provider. */
367 if (pp->sectorsize < sizeof(struct disklabel) ||
368 pp->mediasize < BBSIZE)
370 if (BBSIZE % pp->sectorsize)
373 /* Check that there's a disklabel. */
374 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
377 magic1 = le32dec(buf + 0);
378 magic2 = le32dec(buf + 132);
380 return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
381 ? G_PART_PROBE_PRI_HIGH : ENXIO);
385 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
387 struct g_provider *pp;
388 struct g_part_bsd_table *table;
389 struct g_part_entry *baseentry;
390 struct g_part_bsd_entry *entry;
391 struct partition part;
394 u_int sectors, heads;
398 table = (struct g_part_bsd_table *)basetable;
399 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
401 table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
402 if (table->bbarea == NULL)
405 buf = table->bbarea + pp->sectorsize;
407 if (le32dec(buf + 40) != pp->sectorsize)
409 sectors = le32dec(buf + 44);
410 if (sectors < 1 || sectors > 255)
412 if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
413 g_part_geometry_heads(msize, sectors, &chs, &heads);
415 basetable->gpt_sectors = sectors;
416 basetable->gpt_heads = heads;
419 heads = le32dec(buf + 48);
420 if (heads < 1 || heads > 255)
422 if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
423 basetable->gpt_heads = heads;
425 chs = le32dec(buf + 60);
428 /* Fix-up a sysinstall bug. */
431 le32enc(buf + 60, msize);
434 basetable->gpt_first = 0;
435 basetable->gpt_last = msize - 1;
436 basetable->gpt_isleaf = 1;
438 basetable->gpt_entries = le16dec(buf + 138);
439 if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
440 basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
443 table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
444 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
445 p = buf + 148 + index * 16;
446 part.p_size = le32dec(p + 0);
447 part.p_offset = le32dec(p + 4);
448 part.p_fsize = le32dec(p + 8);
449 part.p_fstype = p[12];
451 part.p_cpg = le16dec(p + 14);
452 if (part.p_size == 0)
454 if (part.p_offset < table->offset)
456 if (part.p_offset - table->offset > basetable->gpt_last)
458 baseentry = g_part_new_entry(basetable, index + 1,
459 part.p_offset - table->offset,
460 part.p_offset - table->offset + part.p_size - 1);
461 entry = (struct g_part_bsd_entry *)baseentry;
463 if (index == RAW_PART)
464 baseentry->gpe_internal = 1;
470 printf("GEOM: %s: invalid disklabel.\n", pp->name);
471 g_free(table->bbarea);
472 table->bbarea = NULL;
477 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
478 char *buf, size_t bufsz)
480 struct g_part_bsd_entry *entry;
483 entry = (struct g_part_bsd_entry *)baseentry;
484 type = entry->part.p_fstype;
485 if (type == FS_NANDFS)
486 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
488 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
489 if (type == FS_BSDFFS)
490 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
491 if (type == FS_VINUM)
492 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
494 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
495 snprintf(buf, bufsz, "!%d", type);
500 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
502 struct g_provider *pp;
503 struct g_part_entry *baseentry;
504 struct g_part_bsd_entry *entry;
505 struct g_part_bsd_table *table;
507 u_char *label, *p, *pe;
511 table = (struct g_part_bsd_table *)basetable;
512 baseentry = LIST_FIRST(&basetable->gpt_entry);
513 label = table->bbarea + pp->sectorsize;
514 for (index = 1; index <= basetable->gpt_entries; index++) {
515 p = label + 148 + (index - 1) * 16;
516 entry = (baseentry != NULL && index == baseentry->gpe_index)
517 ? (struct g_part_bsd_entry *)baseentry : NULL;
518 if (entry != NULL && !baseentry->gpe_deleted) {
519 le32enc(p + 0, entry->part.p_size);
520 le32enc(p + 4, entry->part.p_offset);
521 le32enc(p + 8, entry->part.p_fsize);
522 p[12] = entry->part.p_fstype;
523 p[13] = entry->part.p_frag;
524 le16enc(p + 14, entry->part.p_cpg);
529 baseentry = LIST_NEXT(baseentry, gpe_entry);
532 /* Calculate checksum. */
533 le16enc(label + 136, 0);
534 pe = label + 148 + basetable->gpt_entries * 16;
536 for (p = label; p < pe; p += 2)
538 le16enc(label + 136, sum);
540 error = g_write_data(cp, 0, table->bbarea, BBSIZE);