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_create(struct g_part_table *, struct g_part_parms *);
62 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
63 static int g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
64 struct sbuf *, const char *);
65 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
66 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,
67 struct g_part_parms *);
68 static char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
70 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
71 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
72 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
74 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
76 static kobj_method_t g_part_bsd_methods[] = {
77 KOBJMETHOD(g_part_add, g_part_bsd_add),
78 KOBJMETHOD(g_part_create, g_part_bsd_create),
79 KOBJMETHOD(g_part_destroy, g_part_bsd_destroy),
80 KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf),
81 KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto),
82 KOBJMETHOD(g_part_modify, g_part_bsd_modify),
83 KOBJMETHOD(g_part_name, g_part_bsd_name),
84 KOBJMETHOD(g_part_probe, g_part_bsd_probe),
85 KOBJMETHOD(g_part_read, g_part_bsd_read),
86 KOBJMETHOD(g_part_type, g_part_bsd_type),
87 KOBJMETHOD(g_part_write, g_part_bsd_write),
91 static struct g_part_scheme g_part_bsd_scheme = {
94 sizeof(struct g_part_bsd_table),
95 .gps_entrysz = sizeof(struct g_part_bsd_entry),
99 G_PART_SCHEME_DECLARE(g_part_bsd);
102 bsd_parse_type(const char *type, uint8_t *fstype)
108 if (type[0] == '!') {
109 lt = strtol(type + 1, &endp, 0);
110 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
115 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
116 if (!strcasecmp(type, alias)) {
120 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
121 if (!strcasecmp(type, alias)) {
125 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
126 if (!strcasecmp(type, alias)) {
130 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
131 if (!strcasecmp(type, alias)) {
139 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
140 struct g_part_parms *gpp)
142 struct g_part_bsd_entry *entry;
143 struct g_part_bsd_table *table;
145 if (gpp->gpp_parms & G_PART_PARM_LABEL)
148 entry = (struct g_part_bsd_entry *)baseentry;
149 table = (struct g_part_bsd_table *)basetable;
151 entry->part.p_size = gpp->gpp_size;
152 entry->part.p_offset = gpp->gpp_start + table->offset;
153 entry->part.p_fsize = 0;
154 entry->part.p_frag = 0;
155 entry->part.p_cpg = 0;
156 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
160 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
162 struct g_consumer *cp;
163 struct g_provider *pp;
164 struct g_part_entry *baseentry;
165 struct g_part_bsd_entry *entry;
166 struct g_part_bsd_table *table;
169 uint32_t ncyls, secpercyl;
171 pp = gpp->gpp_provider;
172 cp = LIST_FIRST(&pp->consumers);
174 if (pp->sectorsize < sizeof(struct disklabel))
177 msize = pp->mediasize / pp->sectorsize;
178 secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
179 ncyls = msize / secpercyl;
181 table = (struct g_part_bsd_table *)basetable;
182 ptr = table->label = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
184 le32enc(ptr + 0, DISKMAGIC); /* d_magic */
185 le32enc(ptr + 40, pp->sectorsize); /* d_secsize */
186 le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */
187 le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */
188 le32enc(ptr + 52, ncyls); /* d_ncylinders */
189 le32enc(ptr + 56, secpercyl); /* d_secpercyl */
190 le32enc(ptr + 60, msize); /* d_secperunit */
191 le16enc(ptr + 72, 3600); /* d_rpm */
192 le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */
193 le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */
194 le32enc(ptr + 140, BBSIZE); /* d_bbsize */
196 basetable->gpt_first = 0;
197 basetable->gpt_last = msize - 1;
198 basetable->gpt_isleaf = 1;
200 baseentry = g_part_new_entry(basetable, RAW_PART + 1,
201 basetable->gpt_first, basetable->gpt_last);
202 baseentry->gpe_internal = 1;
203 entry = (struct g_part_bsd_entry *)baseentry;
204 entry->part.p_size = basetable->gpt_last + 1;
205 entry->part.p_offset = table->offset;
211 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
214 /* Wipe the second sector to clear the partitioning. */
215 basetable->gpt_smhead |= 2;
220 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
221 struct sbuf *sb, const char *indent)
223 struct g_part_bsd_entry *entry;
225 entry = (struct g_part_bsd_entry *)baseentry;
226 if (indent == NULL) {
227 /* conftxt: libdisk compatibility */
228 sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
229 } else if (entry != NULL) {
230 /* confxml: partition entry information */
231 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
232 entry->part.p_fstype);
234 /* confxml: scheme information */
240 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
242 struct g_part_bsd_entry *entry;
244 /* Allow dumping to a swap partition only. */
245 entry = (struct g_part_bsd_entry *)baseentry;
246 return ((entry->part.p_fstype == FS_SWAP) ? 1 : 0);
250 g_part_bsd_modify(struct g_part_table *basetable,
251 struct g_part_entry *baseentry, struct g_part_parms *gpp)
253 struct g_part_bsd_entry *entry;
255 if (gpp->gpp_parms & G_PART_PARM_LABEL)
258 entry = (struct g_part_bsd_entry *)baseentry;
259 if (gpp->gpp_parms & G_PART_PARM_TYPE)
260 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
265 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
266 char *buf, size_t bufsz)
269 snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
274 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
276 struct g_provider *pp;
278 uint32_t magic1, magic2;
283 /* Sanity-check the provider. */
284 if (pp->sectorsize < sizeof(struct disklabel) ||
285 pp->mediasize < BBSIZE)
288 /* Check that there's a disklabel. */
289 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
292 magic1 = le32dec(buf + 0);
293 magic2 = le32dec(buf + 132);
295 return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
296 ? G_PART_PROBE_PRI_NORM : ENXIO);
300 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
302 struct g_provider *pp;
303 struct g_part_bsd_table *table;
304 struct g_part_entry *baseentry;
305 struct g_part_bsd_entry *entry;
306 struct partition part;
309 u_int sectors, heads;
313 table = (struct g_part_bsd_table *)basetable;
314 msize = pp->mediasize / pp->sectorsize;
316 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
322 if (le32dec(buf + 40) != pp->sectorsize)
324 sectors = le32dec(buf + 44);
325 if (sectors < 1 || sectors > 63)
327 if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
328 g_part_geometry_heads(msize, sectors, &chs, &heads);
330 basetable->gpt_sectors = sectors;
331 basetable->gpt_heads = heads;
334 heads = le32dec(buf + 48);
335 if (heads < 1 || heads > 255)
337 if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
338 basetable->gpt_heads = heads;
339 if (sectors != basetable->gpt_sectors ||
340 heads != basetable->gpt_heads)
341 printf("GEOM: %s: geometry does not match label.\n", pp->name);
343 chs = le32dec(buf + 60);
344 if (chs < 1 || chs > msize)
347 printf("GEOM: %s: media size does not match label.\n",
350 basetable->gpt_first = 0;
351 basetable->gpt_last = msize - 1;
352 basetable->gpt_isleaf = 1;
354 basetable->gpt_entries = le16dec(buf + 138);
355 if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
356 basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
359 table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
360 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
361 p = buf + 148 + index * 16;
362 part.p_size = le32dec(p + 0);
363 part.p_offset = le32dec(p + 4);
364 part.p_fsize = le32dec(p + 8);
365 part.p_fstype = p[12];
367 part.p_cpg = le16dec(p + 14);
368 if (part.p_size == 0)
370 if (part.p_fstype == FS_UNUSED && index != RAW_PART)
372 if (part.p_offset < table->offset)
374 baseentry = g_part_new_entry(basetable, index + 1,
375 part.p_offset - table->offset,
376 part.p_offset - table->offset + part.p_size - 1);
377 entry = (struct g_part_bsd_entry *)baseentry;
379 if (part.p_fstype == FS_UNUSED)
380 baseentry->gpe_internal = 1;
386 printf("GEOM: %s: invalid disklabel.\n", pp->name);
387 g_free(table->label);
392 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
393 char *buf, size_t bufsz)
395 struct g_part_bsd_entry *entry;
398 entry = (struct g_part_bsd_entry *)baseentry;
399 type = entry->part.p_fstype;
401 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
402 if (type == FS_BSDFFS)
403 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
404 if (type == FS_VINUM)
405 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
407 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
408 snprintf(buf, bufsz, "!%d", type);
413 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
415 struct g_provider *pp;
416 struct g_part_entry *baseentry;
417 struct g_part_bsd_entry *entry;
418 struct g_part_bsd_table *table;
424 table = (struct g_part_bsd_table *)basetable;
425 baseentry = LIST_FIRST(&basetable->gpt_entry);
426 for (index = 1; index <= basetable->gpt_entries; index++) {
427 p = table->label + 148 + (index - 1) * 16;
428 entry = (baseentry != NULL && index == baseentry->gpe_index)
429 ? (struct g_part_bsd_entry *)baseentry : NULL;
430 if (entry != NULL && !baseentry->gpe_deleted) {
431 le32enc(p + 0, entry->part.p_size);
432 le32enc(p + 4, entry->part.p_offset);
433 le32enc(p + 8, entry->part.p_fsize);
434 p[12] = entry->part.p_fstype;
435 p[13] = entry->part.p_frag;
436 le16enc(p + 14, entry->part.p_cpg);
441 baseentry = LIST_NEXT(baseentry, gpe_entry);
444 /* Calculate checksum. */
445 le16enc(table->label + 136, 0);
446 pe = table->label + 148 + basetable->gpt_entries * 16;
448 for (p = table->label; p < pe; p += 2)
450 le16enc(table->label + 136, sum);
452 error = g_write_data(cp, pp->sectorsize, table->label, pp->sectorsize);