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 <geom/geom.h>
44 #include <geom/part/g_part.h>
46 #include "g_part_if.h"
48 struct g_part_bsd_table {
49 struct g_part_table base;
54 struct g_part_bsd_entry {
55 struct g_part_entry base;
56 struct partition part;
59 static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *,
60 struct g_part_parms *);
61 static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *);
62 static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *);
63 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
64 static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
65 struct sbuf *, const char *);
66 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
67 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,
68 struct g_part_parms *);
69 static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
71 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
72 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
73 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
75 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
76 static int g_part_bsd_resize(struct g_part_table *, struct g_part_entry *,
77 struct g_part_parms *);
79 static kobj_method_t g_part_bsd_methods[] = {
80 KOBJMETHOD(g_part_add, g_part_bsd_add),
81 KOBJMETHOD(g_part_bootcode, g_part_bsd_bootcode),
82 KOBJMETHOD(g_part_create, g_part_bsd_create),
83 KOBJMETHOD(g_part_destroy, g_part_bsd_destroy),
84 KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf),
85 KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto),
86 KOBJMETHOD(g_part_modify, g_part_bsd_modify),
87 KOBJMETHOD(g_part_resize, g_part_bsd_resize),
88 KOBJMETHOD(g_part_name, g_part_bsd_name),
89 KOBJMETHOD(g_part_probe, g_part_bsd_probe),
90 KOBJMETHOD(g_part_read, g_part_bsd_read),
91 KOBJMETHOD(g_part_type, g_part_bsd_type),
92 KOBJMETHOD(g_part_write, g_part_bsd_write),
96 static struct g_part_scheme g_part_bsd_scheme = {
99 sizeof(struct g_part_bsd_table),
100 .gps_entrysz = sizeof(struct g_part_bsd_entry),
103 .gps_bootcodesz = BBSIZE,
105 G_PART_SCHEME_DECLARE(g_part_bsd);
108 bsd_parse_type(const char *type, uint8_t *fstype)
114 if (type[0] == '!') {
115 lt = strtol(type + 1, &endp, 0);
116 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
121 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
122 if (!strcasecmp(type, alias)) {
126 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
127 if (!strcasecmp(type, alias)) {
131 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
132 if (!strcasecmp(type, alias)) {
136 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
137 if (!strcasecmp(type, alias)) {
145 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
146 struct g_part_parms *gpp)
148 struct g_part_bsd_entry *entry;
149 struct g_part_bsd_table *table;
151 if (gpp->gpp_parms & G_PART_PARM_LABEL)
154 entry = (struct g_part_bsd_entry *)baseentry;
155 table = (struct g_part_bsd_table *)basetable;
157 entry->part.p_size = gpp->gpp_size;
158 entry->part.p_offset = gpp->gpp_start + table->offset;
159 entry->part.p_fsize = 0;
160 entry->part.p_frag = 0;
161 entry->part.p_cpg = 0;
162 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
166 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
168 struct g_part_bsd_table *table;
169 const u_char *codeptr;
171 size_t codesz, tlofs;
174 tlofs = hdsz + 148 + basetable->gpt_entries * 16;
175 tlsz = BBSIZE - tlofs;
176 table = (struct g_part_bsd_table *)basetable;
177 bzero(table->bbarea, hdsz);
178 bzero(table->bbarea + tlofs, tlsz);
179 codeptr = gpp->gpp_codeptr;
180 codesz = MIN(hdsz, gpp->gpp_codesize);
182 bcopy(codeptr, table->bbarea, codesz);
183 codesz = MIN(tlsz, gpp->gpp_codesize - tlofs);
185 bcopy(codeptr + tlofs, table->bbarea + tlofs, codesz);
190 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
192 struct g_provider *pp;
193 struct g_part_entry *baseentry;
194 struct g_part_bsd_entry *entry;
195 struct g_part_bsd_table *table;
197 uint32_t msize, ncyls, secpercyl;
199 pp = gpp->gpp_provider;
201 if (pp->sectorsize < sizeof(struct disklabel))
203 if (BBSIZE % pp->sectorsize)
206 msize = MIN(pp->mediasize / pp->sectorsize, 0xffffffff);
207 secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
208 ncyls = msize / secpercyl;
210 table = (struct g_part_bsd_table *)basetable;
211 table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
212 ptr = table->bbarea + pp->sectorsize;
214 le32enc(ptr + 0, DISKMAGIC); /* d_magic */
215 le32enc(ptr + 40, pp->sectorsize); /* d_secsize */
216 le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */
217 le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */
218 le32enc(ptr + 52, ncyls); /* d_ncylinders */
219 le32enc(ptr + 56, secpercyl); /* d_secpercyl */
220 le32enc(ptr + 60, msize); /* d_secperunit */
221 le16enc(ptr + 72, 3600); /* d_rpm */
222 le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */
223 le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */
224 le32enc(ptr + 140, BBSIZE); /* d_bbsize */
226 basetable->gpt_first = 0;
227 basetable->gpt_last = msize - 1;
228 basetable->gpt_isleaf = 1;
230 baseentry = g_part_new_entry(basetable, RAW_PART + 1,
231 basetable->gpt_first, basetable->gpt_last);
232 baseentry->gpe_internal = 1;
233 entry = (struct g_part_bsd_entry *)baseentry;
234 entry->part.p_size = basetable->gpt_last + 1;
235 entry->part.p_offset = table->offset;
241 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
243 struct g_part_bsd_table *table;
245 table = (struct g_part_bsd_table *)basetable;
246 if (table->bbarea != NULL)
247 g_free(table->bbarea);
248 table->bbarea = NULL;
250 /* Wipe the second sector to clear the partitioning. */
251 basetable->gpt_smhead |= 2;
256 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
257 struct sbuf *sb, const char *indent)
259 struct g_part_bsd_entry *entry;
261 entry = (struct g_part_bsd_entry *)baseentry;
262 if (indent == NULL) {
263 /* conftxt: libdisk compatibility */
264 sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
265 } else if (entry != NULL) {
266 /* confxml: partition entry information */
267 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
268 entry->part.p_fstype);
270 /* confxml: scheme information */
275 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
277 struct g_part_bsd_entry *entry;
279 /* Allow dumping to a swap partition or an unused partition. */
280 entry = (struct g_part_bsd_entry *)baseentry;
281 return ((entry->part.p_fstype == FS_UNUSED ||
282 entry->part.p_fstype == FS_SWAP) ? 1 : 0);
286 g_part_bsd_modify(struct g_part_table *basetable,
287 struct g_part_entry *baseentry, struct g_part_parms *gpp)
289 struct g_part_bsd_entry *entry;
291 if (gpp->gpp_parms & G_PART_PARM_LABEL)
294 entry = (struct g_part_bsd_entry *)baseentry;
295 if (gpp->gpp_parms & G_PART_PARM_TYPE)
296 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
301 g_part_bsd_resize(struct g_part_table *basetable,
302 struct g_part_entry *baseentry, struct g_part_parms *gpp)
304 struct g_part_bsd_entry *entry;
306 entry = (struct g_part_bsd_entry *)baseentry;
307 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
308 entry->part.p_size = gpp->gpp_size;
314 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
315 char *buf, size_t bufsz)
318 snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
323 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
325 struct g_provider *pp;
327 uint32_t magic1, magic2;
332 /* Sanity-check the provider. */
333 if (pp->sectorsize < sizeof(struct disklabel) ||
334 pp->mediasize < BBSIZE)
336 if (BBSIZE % pp->sectorsize)
339 /* Check that there's a disklabel. */
340 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
343 magic1 = le32dec(buf + 0);
344 magic2 = le32dec(buf + 132);
346 return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
347 ? G_PART_PROBE_PRI_HIGH : ENXIO);
351 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
353 struct g_provider *pp;
354 struct g_part_bsd_table *table;
355 struct g_part_entry *baseentry;
356 struct g_part_bsd_entry *entry;
357 struct partition part;
360 u_int sectors, heads;
364 table = (struct g_part_bsd_table *)basetable;
365 msize = pp->mediasize / pp->sectorsize;
367 table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
368 if (table->bbarea == NULL)
371 buf = table->bbarea + pp->sectorsize;
373 if (le32dec(buf + 40) != pp->sectorsize)
375 sectors = le32dec(buf + 44);
376 if (sectors < 1 || sectors > 255)
378 if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
379 g_part_geometry_heads(msize, sectors, &chs, &heads);
381 basetable->gpt_sectors = sectors;
382 basetable->gpt_heads = heads;
385 heads = le32dec(buf + 48);
386 if (heads < 1 || heads > 255)
388 if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
389 basetable->gpt_heads = heads;
390 if (sectors != basetable->gpt_sectors || heads != basetable->gpt_heads)
391 printf("GEOM: %s: geometry does not match label"
392 " (%uh,%us != %uh,%us).\n", pp->name, heads, sectors,
393 basetable->gpt_heads, basetable->gpt_sectors);
395 chs = le32dec(buf + 60);
398 /* Fix-up a sysinstall bug. */
401 le32enc(buf + 60, msize);
404 printf("GEOM: %s: media size does not match label.\n",
407 basetable->gpt_first = 0;
408 basetable->gpt_last = msize - 1;
409 basetable->gpt_isleaf = 1;
411 basetable->gpt_entries = le16dec(buf + 138);
412 if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
413 basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
416 table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
417 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
418 p = buf + 148 + index * 16;
419 part.p_size = le32dec(p + 0);
420 part.p_offset = le32dec(p + 4);
421 part.p_fsize = le32dec(p + 8);
422 part.p_fstype = p[12];
424 part.p_cpg = le16dec(p + 14);
425 if (part.p_size == 0)
427 if (part.p_offset < table->offset)
429 baseentry = g_part_new_entry(basetable, index + 1,
430 part.p_offset - table->offset,
431 part.p_offset - table->offset + part.p_size - 1);
432 entry = (struct g_part_bsd_entry *)baseentry;
434 if (index == RAW_PART)
435 baseentry->gpe_internal = 1;
441 printf("GEOM: %s: invalid disklabel.\n", pp->name);
442 g_free(table->bbarea);
447 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
448 char *buf, size_t bufsz)
450 struct g_part_bsd_entry *entry;
453 entry = (struct g_part_bsd_entry *)baseentry;
454 type = entry->part.p_fstype;
456 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
457 if (type == FS_BSDFFS)
458 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
459 if (type == FS_VINUM)
460 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
462 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
463 snprintf(buf, bufsz, "!%d", type);
468 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
470 struct g_provider *pp;
471 struct g_part_entry *baseentry;
472 struct g_part_bsd_entry *entry;
473 struct g_part_bsd_table *table;
475 u_char *label, *p, *pe;
479 table = (struct g_part_bsd_table *)basetable;
480 baseentry = LIST_FIRST(&basetable->gpt_entry);
481 label = table->bbarea + pp->sectorsize;
482 for (index = 1; index <= basetable->gpt_entries; index++) {
483 p = label + 148 + (index - 1) * 16;
484 entry = (baseentry != NULL && index == baseentry->gpe_index)
485 ? (struct g_part_bsd_entry *)baseentry : NULL;
486 if (entry != NULL && !baseentry->gpe_deleted) {
487 le32enc(p + 0, entry->part.p_size);
488 le32enc(p + 4, entry->part.p_offset);
489 le32enc(p + 8, entry->part.p_fsize);
490 p[12] = entry->part.p_fstype;
491 p[13] = entry->part.p_frag;
492 le16enc(p + 14, entry->part.p_cpg);
497 baseentry = LIST_NEXT(baseentry, gpe_entry);
500 /* Calculate checksum. */
501 le16enc(label + 136, 0);
502 pe = label + 148 + basetable->gpt_entries * 16;
504 for (p = label; p < pe; p += 2)
506 le16enc(label + 136, sum);
508 error = g_write_data(cp, 0, table->bbarea, BBSIZE);