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 *);
77 static kobj_method_t g_part_bsd_methods[] = {
78 KOBJMETHOD(g_part_add, g_part_bsd_add),
79 KOBJMETHOD(g_part_bootcode, g_part_bsd_bootcode),
80 KOBJMETHOD(g_part_create, g_part_bsd_create),
81 KOBJMETHOD(g_part_destroy, g_part_bsd_destroy),
82 KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf),
83 KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto),
84 KOBJMETHOD(g_part_modify, g_part_bsd_modify),
85 KOBJMETHOD(g_part_name, g_part_bsd_name),
86 KOBJMETHOD(g_part_probe, g_part_bsd_probe),
87 KOBJMETHOD(g_part_read, g_part_bsd_read),
88 KOBJMETHOD(g_part_type, g_part_bsd_type),
89 KOBJMETHOD(g_part_write, g_part_bsd_write),
93 static struct g_part_scheme g_part_bsd_scheme = {
96 sizeof(struct g_part_bsd_table),
97 .gps_entrysz = sizeof(struct g_part_bsd_entry),
100 .gps_bootcodesz = BBSIZE,
102 G_PART_SCHEME_DECLARE(g_part_bsd);
105 bsd_parse_type(const char *type, uint8_t *fstype)
111 if (type[0] == '!') {
112 lt = strtol(type + 1, &endp, 0);
113 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
118 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
119 if (!strcasecmp(type, alias)) {
123 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
124 if (!strcasecmp(type, alias)) {
128 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
129 if (!strcasecmp(type, alias)) {
133 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
134 if (!strcasecmp(type, alias)) {
142 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
143 struct g_part_parms *gpp)
145 struct g_part_bsd_entry *entry;
146 struct g_part_bsd_table *table;
148 if (gpp->gpp_parms & G_PART_PARM_LABEL)
151 entry = (struct g_part_bsd_entry *)baseentry;
152 table = (struct g_part_bsd_table *)basetable;
154 entry->part.p_size = gpp->gpp_size;
155 entry->part.p_offset = gpp->gpp_start + table->offset;
156 entry->part.p_fsize = 0;
157 entry->part.p_frag = 0;
158 entry->part.p_cpg = 0;
159 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
163 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
165 struct g_part_bsd_table *table;
166 const u_char *codeptr;
168 size_t codesz, tlofs;
171 tlofs = hdsz + 148 + basetable->gpt_entries * 16;
172 tlsz = BBSIZE - tlofs;
173 table = (struct g_part_bsd_table *)basetable;
174 bzero(table->bbarea, hdsz);
175 bzero(table->bbarea + tlofs, tlsz);
176 codeptr = gpp->gpp_codeptr;
177 codesz = MIN(hdsz, gpp->gpp_codesize);
179 bcopy(codeptr, table->bbarea, codesz);
180 codesz = MIN(tlsz, gpp->gpp_codesize - tlofs);
182 bcopy(codeptr + tlofs, table->bbarea + tlofs, codesz);
187 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
189 struct g_consumer *cp;
190 struct g_provider *pp;
191 struct g_part_entry *baseentry;
192 struct g_part_bsd_entry *entry;
193 struct g_part_bsd_table *table;
195 uint32_t msize, ncyls, secpercyl;
197 pp = gpp->gpp_provider;
198 cp = LIST_FIRST(&pp->consumers);
200 if (pp->sectorsize < sizeof(struct disklabel))
202 if (BBSIZE % pp->sectorsize)
205 msize = MIN(pp->mediasize / pp->sectorsize, 0xffffffff);
206 secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
207 ncyls = msize / secpercyl;
209 table = (struct g_part_bsd_table *)basetable;
210 table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
211 ptr = table->bbarea + pp->sectorsize;
213 le32enc(ptr + 0, DISKMAGIC); /* d_magic */
214 le32enc(ptr + 40, pp->sectorsize); /* d_secsize */
215 le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */
216 le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */
217 le32enc(ptr + 52, ncyls); /* d_ncylinders */
218 le32enc(ptr + 56, secpercyl); /* d_secpercyl */
219 le32enc(ptr + 60, msize); /* d_secperunit */
220 le16enc(ptr + 72, 3600); /* d_rpm */
221 le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */
222 le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */
223 le32enc(ptr + 140, BBSIZE); /* d_bbsize */
225 basetable->gpt_first = 0;
226 basetable->gpt_last = msize - 1;
227 basetable->gpt_isleaf = 1;
229 baseentry = g_part_new_entry(basetable, RAW_PART + 1,
230 basetable->gpt_first, basetable->gpt_last);
231 baseentry->gpe_internal = 1;
232 entry = (struct g_part_bsd_entry *)baseentry;
233 entry->part.p_size = basetable->gpt_last + 1;
234 entry->part.p_offset = table->offset;
240 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
242 struct g_part_bsd_table *table;
244 table = (struct g_part_bsd_table *)basetable;
245 if (table->bbarea != NULL)
246 g_free(table->bbarea);
247 table->bbarea = NULL;
249 /* Wipe the second sector to clear the partitioning. */
250 basetable->gpt_smhead |= 2;
255 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
256 struct sbuf *sb, const char *indent)
258 struct g_part_bsd_entry *entry;
260 entry = (struct g_part_bsd_entry *)baseentry;
261 if (indent == NULL) {
262 /* conftxt: libdisk compatibility */
263 sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
264 } else if (entry != NULL) {
265 /* confxml: partition entry information */
266 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
267 entry->part.p_fstype);
269 /* confxml: scheme information */
274 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
276 struct g_part_bsd_entry *entry;
278 /* Allow dumping to a swap partition or an unused partition. */
279 entry = (struct g_part_bsd_entry *)baseentry;
280 return ((entry->part.p_fstype == FS_UNUSED ||
281 entry->part.p_fstype == FS_SWAP) ? 1 : 0);
285 g_part_bsd_modify(struct g_part_table *basetable,
286 struct g_part_entry *baseentry, struct g_part_parms *gpp)
288 struct g_part_bsd_entry *entry;
290 if (gpp->gpp_parms & G_PART_PARM_LABEL)
293 entry = (struct g_part_bsd_entry *)baseentry;
294 if (gpp->gpp_parms & G_PART_PARM_TYPE)
295 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
300 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
301 char *buf, size_t bufsz)
304 snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
309 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
311 struct g_provider *pp;
313 uint32_t magic1, magic2;
318 /* Sanity-check the provider. */
319 if (pp->sectorsize < sizeof(struct disklabel) ||
320 pp->mediasize < BBSIZE)
322 if (BBSIZE % pp->sectorsize)
325 /* Check that there's a disklabel. */
326 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
329 magic1 = le32dec(buf + 0);
330 magic2 = le32dec(buf + 132);
332 return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
333 ? G_PART_PROBE_PRI_HIGH : ENXIO);
337 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
339 struct g_provider *pp;
340 struct g_part_bsd_table *table;
341 struct g_part_entry *baseentry;
342 struct g_part_bsd_entry *entry;
343 struct partition part;
346 u_int sectors, heads;
350 table = (struct g_part_bsd_table *)basetable;
351 msize = pp->mediasize / pp->sectorsize;
353 table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
354 if (table->bbarea == NULL)
357 buf = table->bbarea + pp->sectorsize;
359 if (le32dec(buf + 40) != pp->sectorsize)
361 sectors = le32dec(buf + 44);
362 if (sectors < 1 || sectors > 255)
364 if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
365 g_part_geometry_heads(msize, sectors, &chs, &heads);
367 basetable->gpt_sectors = sectors;
368 basetable->gpt_heads = heads;
371 heads = le32dec(buf + 48);
372 if (heads < 1 || heads > 255)
374 if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
375 basetable->gpt_heads = heads;
376 if (sectors != basetable->gpt_sectors || heads != basetable->gpt_heads)
377 printf("GEOM: %s: geometry does not match label"
378 " (%uh,%us != %uh,%us).\n", pp->name, heads, sectors,
379 basetable->gpt_heads, basetable->gpt_sectors);
381 chs = le32dec(buf + 60);
384 /* Fix-up a sysinstall bug. */
387 le32enc(buf + 60, msize);
390 printf("GEOM: %s: media size does not match label.\n",
393 basetable->gpt_first = 0;
394 basetable->gpt_last = msize - 1;
395 basetable->gpt_isleaf = 1;
397 basetable->gpt_entries = le16dec(buf + 138);
398 if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
399 basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
402 table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
403 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
404 p = buf + 148 + index * 16;
405 part.p_size = le32dec(p + 0);
406 part.p_offset = le32dec(p + 4);
407 part.p_fsize = le32dec(p + 8);
408 part.p_fstype = p[12];
410 part.p_cpg = le16dec(p + 14);
411 if (part.p_size == 0)
413 if (part.p_offset < table->offset)
415 baseentry = g_part_new_entry(basetable, index + 1,
416 part.p_offset - table->offset,
417 part.p_offset - table->offset + part.p_size - 1);
418 entry = (struct g_part_bsd_entry *)baseentry;
420 if (index == RAW_PART)
421 baseentry->gpe_internal = 1;
427 printf("GEOM: %s: invalid disklabel.\n", pp->name);
428 g_free(table->bbarea);
433 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
434 char *buf, size_t bufsz)
436 struct g_part_bsd_entry *entry;
439 entry = (struct g_part_bsd_entry *)baseentry;
440 type = entry->part.p_fstype;
442 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
443 if (type == FS_BSDFFS)
444 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
445 if (type == FS_VINUM)
446 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
448 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
449 snprintf(buf, bufsz, "!%d", type);
454 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
456 struct g_provider *pp;
457 struct g_part_entry *baseentry;
458 struct g_part_bsd_entry *entry;
459 struct g_part_bsd_table *table;
461 u_char *label, *p, *pe;
465 table = (struct g_part_bsd_table *)basetable;
466 baseentry = LIST_FIRST(&basetable->gpt_entry);
467 label = table->bbarea + pp->sectorsize;
468 for (index = 1; index <= basetable->gpt_entries; index++) {
469 p = label + 148 + (index - 1) * 16;
470 entry = (baseentry != NULL && index == baseentry->gpe_index)
471 ? (struct g_part_bsd_entry *)baseentry : NULL;
472 if (entry != NULL && !baseentry->gpe_deleted) {
473 le32enc(p + 0, entry->part.p_size);
474 le32enc(p + 4, entry->part.p_offset);
475 le32enc(p + 8, entry->part.p_fsize);
476 p[12] = entry->part.p_fstype;
477 p[13] = entry->part.p_frag;
478 le16enc(p + 14, entry->part.p_cpg);
483 baseentry = LIST_NEXT(baseentry, gpe_entry);
486 /* Calculate checksum. */
487 le16enc(label + 136, 0);
488 pe = label + 148 + basetable->gpt_entries * 16;
490 for (p = label; p < pe; p += 2)
492 le16enc(label + 136, sum);
494 error = g_write_data(cp, 0, table->bbarea, BBSIZE);