xref: /freebsd/sys/geom/part/g_part_bsd.c (revision 3ef51c5fb9163f2aafb1c14729e06a8bf0c4d113)
1 /*-
2  * Copyright (c) 2007 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
14  *
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.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/disklabel.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
35 #include <sys/kobj.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/queue.h>
41 #include <sys/sbuf.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <geom/geom.h>
45 #include <geom/part/g_part.h>
46 
47 #include "g_part_if.h"
48 
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)
53 
54 FEATURE(geom_part_bsd, "GEOM partitioning class for BSD disklabels");
55 
56 struct g_part_bsd_table {
57 	struct g_part_table	base;
58 	u_char			*bbarea;
59 	uint32_t		offset;
60 };
61 
62 struct g_part_bsd_entry {
63 	struct g_part_entry	base;
64 	struct partition	part;
65 };
66 
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 *,
78     char *, size_t);
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 *,
82     char *, size_t);
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 *);
86 
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),
101 	{ 0, 0 }
102 };
103 
104 static struct g_part_scheme g_part_bsd_scheme = {
105 	"BSD",
106 	g_part_bsd_methods,
107 	sizeof(struct g_part_bsd_table),
108 	.gps_entrysz = sizeof(struct g_part_bsd_entry),
109 	.gps_minent = 8,
110 	.gps_maxent = 20,	/* Only 22 entries fit in 512 byte sectors */
111 	.gps_bootcodesz = BBSIZE,
112 };
113 G_PART_SCHEME_DECLARE(g_part_bsd);
114 
115 static int
116 bsd_parse_type(const char *type, uint8_t *fstype)
117 {
118 	const char *alias;
119 	char *endp;
120 	long lt;
121 
122 	if (type[0] == '!') {
123 		lt = strtol(type + 1, &endp, 0);
124 		if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
125 			return (EINVAL);
126 		*fstype = (u_int)lt;
127 		return (0);
128 	}
129 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
130 	if (!strcasecmp(type, alias)) {
131 		*fstype = FS_SWAP;
132 		return (0);
133 	}
134 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
135 	if (!strcasecmp(type, alias)) {
136 		*fstype = FS_BSDFFS;
137 		return (0);
138 	}
139 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
140 	if (!strcasecmp(type, alias)) {
141 		*fstype = FS_VINUM;
142 		return (0);
143 	}
144 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
145 	if (!strcasecmp(type, alias)) {
146 		*fstype = FS_ZFS;
147 		return (0);
148 	}
149 	return (EINVAL);
150 }
151 
152 static int
153 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
154     struct g_part_parms *gpp)
155 {
156 	struct g_part_bsd_entry *entry;
157 	struct g_part_bsd_table *table;
158 
159 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
160 		return (EINVAL);
161 
162 	entry = (struct g_part_bsd_entry *)baseentry;
163 	table = (struct g_part_bsd_table *)basetable;
164 
165 	entry->part.p_size = gpp->gpp_size;
166 	entry->part.p_offset = gpp->gpp_start + table->offset;
167 	entry->part.p_fsize = 0;
168 	entry->part.p_frag = 0;
169 	entry->part.p_cpg = 0;
170 	return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
171 }
172 
173 static int
174 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
175 {
176 	struct g_part_bsd_table *table;
177 	const u_char *codeptr;
178 
179 	if (gpp->gpp_codesize != BOOT1_SIZE && gpp->gpp_codesize != BBSIZE)
180 		return (ENODEV);
181 
182 	table = (struct g_part_bsd_table *)basetable;
183 	codeptr = gpp->gpp_codeptr;
184 	bcopy(codeptr, table->bbarea, BOOT1_SIZE);
185 	if (gpp->gpp_codesize == BBSIZE)
186 		bcopy(codeptr + BOOT2_OFF, table->bbarea + BOOT2_OFF,
187 		    BOOT2_SIZE);
188 	return (0);
189 }
190 
191 static int
192 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
193 {
194 	struct g_provider *pp;
195 	struct g_part_entry *baseentry;
196 	struct g_part_bsd_entry *entry;
197 	struct g_part_bsd_table *table;
198 	u_char *ptr;
199 	uint32_t msize, ncyls, secpercyl;
200 
201 	pp = gpp->gpp_provider;
202 
203 	if (pp->sectorsize < sizeof(struct disklabel))
204 		return (ENOSPC);
205 	if (BBSIZE % pp->sectorsize)
206 		return (ENOTBLK);
207 
208 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
209 	secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
210 	ncyls = msize / secpercyl;
211 
212 	table = (struct g_part_bsd_table *)basetable;
213 	table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
214 	ptr = table->bbarea + pp->sectorsize;
215 
216 	le32enc(ptr + 0, DISKMAGIC);			/* d_magic */
217 	le32enc(ptr + 40, pp->sectorsize);		/* d_secsize */
218 	le32enc(ptr + 44, basetable->gpt_sectors);	/* d_nsectors */
219 	le32enc(ptr + 48, basetable->gpt_heads);	/* d_ntracks */
220 	le32enc(ptr + 52, ncyls);			/* d_ncylinders */
221 	le32enc(ptr + 56, secpercyl);			/* d_secpercyl */
222 	le32enc(ptr + 60, msize);			/* d_secperunit */
223 	le16enc(ptr + 72, 3600);			/* d_rpm */
224 	le32enc(ptr + 132, DISKMAGIC);			/* d_magic2 */
225 	le16enc(ptr + 138, basetable->gpt_entries);	/* d_npartitions */
226 	le32enc(ptr + 140, BBSIZE);			/* d_bbsize */
227 
228 	basetable->gpt_first = 0;
229 	basetable->gpt_last = msize - 1;
230 	basetable->gpt_isleaf = 1;
231 
232 	baseentry = g_part_new_entry(basetable, RAW_PART + 1,
233 	    basetable->gpt_first, basetable->gpt_last);
234 	baseentry->gpe_internal = 1;
235 	entry = (struct g_part_bsd_entry *)baseentry;
236 	entry->part.p_size = basetable->gpt_last + 1;
237 	entry->part.p_offset = table->offset;
238 
239 	return (0);
240 }
241 
242 static int
243 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
244 {
245 	struct g_part_bsd_table *table;
246 
247 	table = (struct g_part_bsd_table *)basetable;
248 	if (table->bbarea != NULL)
249 		g_free(table->bbarea);
250 	table->bbarea = NULL;
251 
252 	/* Wipe the second sector to clear the partitioning. */
253 	basetable->gpt_smhead |= 2;
254 	return (0);
255 }
256 
257 static void
258 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
259     struct sbuf *sb, const char *indent)
260 {
261 	struct g_part_bsd_entry *entry;
262 
263 	entry = (struct g_part_bsd_entry *)baseentry;
264 	if (indent == NULL) {
265 		/* conftxt: libdisk compatibility */
266 		sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
267 	} else if (entry != NULL) {
268 		/* confxml: partition entry information */
269 		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
270 		    entry->part.p_fstype);
271 	} else {
272 		/* confxml: scheme information */
273 	}
274 }
275 
276 static int
277 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
278 {
279 	struct g_part_bsd_entry *entry;
280 
281 	/* Allow dumping to a swap partition or an unused partition. */
282 	entry = (struct g_part_bsd_entry *)baseentry;
283 	return ((entry->part.p_fstype == FS_UNUSED ||
284 	    entry->part.p_fstype == FS_SWAP) ? 1 : 0);
285 }
286 
287 static int
288 g_part_bsd_modify(struct g_part_table *basetable,
289     struct g_part_entry *baseentry, struct g_part_parms *gpp)
290 {
291 	struct g_part_bsd_entry *entry;
292 
293 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
294 		return (EINVAL);
295 
296 	entry = (struct g_part_bsd_entry *)baseentry;
297 	if (gpp->gpp_parms & G_PART_PARM_TYPE)
298 		return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
299 	return (0);
300 }
301 
302 static int
303 g_part_bsd_resize(struct g_part_table *basetable,
304     struct g_part_entry *baseentry, struct g_part_parms *gpp)
305 {
306 	struct g_part_bsd_entry *entry;
307 
308 	entry = (struct g_part_bsd_entry *)baseentry;
309 	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
310 	entry->part.p_size = gpp->gpp_size;
311 
312 	return (0);
313 }
314 
315 static const char *
316 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
317     char *buf, size_t bufsz)
318 {
319 
320 	snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
321 	return (buf);
322 }
323 
324 static int
325 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
326 {
327 	struct g_provider *pp;
328 	u_char *buf;
329 	uint32_t magic1, magic2;
330 	int error;
331 
332 	pp = cp->provider;
333 
334 	/* Sanity-check the provider. */
335 	if (pp->sectorsize < sizeof(struct disklabel) ||
336 	    pp->mediasize < BBSIZE)
337 		return (ENOSPC);
338 	if (BBSIZE % pp->sectorsize)
339 		return (ENOTBLK);
340 
341 	/* Check that there's a disklabel. */
342 	buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
343 	if (buf == NULL)
344 		return (error);
345 	magic1 = le32dec(buf + 0);
346 	magic2 = le32dec(buf + 132);
347 	g_free(buf);
348 	return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
349 	    ? G_PART_PROBE_PRI_HIGH : ENXIO);
350 }
351 
352 static int
353 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
354 {
355 	struct g_provider *pp;
356 	struct g_part_bsd_table *table;
357 	struct g_part_entry *baseentry;
358 	struct g_part_bsd_entry *entry;
359 	struct partition part;
360 	u_char *buf, *p;
361 	off_t chs, msize;
362 	u_int sectors, heads;
363 	int error, index;
364 
365 	pp = cp->provider;
366 	table = (struct g_part_bsd_table *)basetable;
367 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
368 
369 	table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
370 	if (table->bbarea == NULL)
371 		return (error);
372 
373 	buf = table->bbarea + pp->sectorsize;
374 
375 	if (le32dec(buf + 40) != pp->sectorsize)
376 		goto invalid_label;
377 	sectors = le32dec(buf + 44);
378 	if (sectors < 1 || sectors > 255)
379 		goto invalid_label;
380 	if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
381 		g_part_geometry_heads(msize, sectors, &chs, &heads);
382 		if (chs != 0) {
383 			basetable->gpt_sectors = sectors;
384 			basetable->gpt_heads = heads;
385 		}
386 	}
387 	heads = le32dec(buf + 48);
388 	if (heads < 1 || heads > 255)
389 		goto invalid_label;
390 	if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
391 		basetable->gpt_heads = heads;
392 
393 	chs = le32dec(buf + 60);
394 	if (chs < 1)
395 		goto invalid_label;
396 	/* Fix-up a sysinstall bug. */
397 	if (chs > msize) {
398 		chs = msize;
399 		le32enc(buf + 60, msize);
400 	}
401 
402 	basetable->gpt_first = 0;
403 	basetable->gpt_last = msize - 1;
404 	basetable->gpt_isleaf = 1;
405 
406 	basetable->gpt_entries = le16dec(buf + 138);
407 	if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
408 	    basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
409 		goto invalid_label;
410 
411 	table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
412 	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
413 		p = buf + 148 + index * 16;
414 		part.p_size = le32dec(p + 0);
415 		part.p_offset = le32dec(p + 4);
416 		part.p_fsize = le32dec(p + 8);
417 		part.p_fstype = p[12];
418 		part.p_frag = p[13];
419 		part.p_cpg = le16dec(p + 14);
420 		if (part.p_size == 0)
421 			continue;
422 		if (part.p_offset < table->offset)
423 			continue;
424 		if (part.p_offset - table->offset > basetable->gpt_last)
425 			goto invalid_label;
426 		baseentry = g_part_new_entry(basetable, index + 1,
427 		    part.p_offset - table->offset,
428 		    part.p_offset - table->offset + part.p_size - 1);
429 		entry = (struct g_part_bsd_entry *)baseentry;
430 		entry->part = part;
431 		if (index == RAW_PART)
432 			baseentry->gpe_internal = 1;
433 	}
434 
435 	return (0);
436 
437  invalid_label:
438 	printf("GEOM: %s: invalid disklabel.\n", pp->name);
439 	g_free(table->bbarea);
440 	table->bbarea = NULL;
441 	return (EINVAL);
442 }
443 
444 static const char *
445 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
446     char *buf, size_t bufsz)
447 {
448 	struct g_part_bsd_entry *entry;
449 	int type;
450 
451 	entry = (struct g_part_bsd_entry *)baseentry;
452 	type = entry->part.p_fstype;
453 	if (type == FS_SWAP)
454 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
455 	if (type == FS_BSDFFS)
456 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
457 	if (type == FS_VINUM)
458 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
459 	if (type == FS_ZFS)
460 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
461 	snprintf(buf, bufsz, "!%d", type);
462 	return (buf);
463 }
464 
465 static int
466 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
467 {
468 	struct g_provider *pp;
469 	struct g_part_entry *baseentry;
470 	struct g_part_bsd_entry *entry;
471 	struct g_part_bsd_table *table;
472 	uint16_t sum;
473 	u_char *label, *p, *pe;
474 	int error, index;
475 
476 	pp = cp->provider;
477 	table = (struct g_part_bsd_table *)basetable;
478 	baseentry = LIST_FIRST(&basetable->gpt_entry);
479 	label = table->bbarea + pp->sectorsize;
480 	for (index = 1; index <= basetable->gpt_entries; index++) {
481 		p = label + 148 + (index - 1) * 16;
482 		entry = (baseentry != NULL && index == baseentry->gpe_index)
483 		    ? (struct g_part_bsd_entry *)baseentry : NULL;
484 		if (entry != NULL && !baseentry->gpe_deleted) {
485 			le32enc(p + 0, entry->part.p_size);
486 			le32enc(p + 4, entry->part.p_offset);
487 			le32enc(p + 8, entry->part.p_fsize);
488 			p[12] = entry->part.p_fstype;
489 			p[13] = entry->part.p_frag;
490 			le16enc(p + 14, entry->part.p_cpg);
491 		} else
492 			bzero(p, 16);
493 
494 		if (entry != NULL)
495 			baseentry = LIST_NEXT(baseentry, gpe_entry);
496 	}
497 
498 	/* Calculate checksum. */
499 	le16enc(label + 136, 0);
500 	pe = label + 148 + basetable->gpt_entries * 16;
501 	sum = 0;
502 	for (p = label; p < pe; p += 2)
503 		sum ^= le16dec(p);
504 	le16enc(label + 136, sum);
505 
506 	error = g_write_data(cp, 0, table->bbarea, BBSIZE);
507 	return (error);
508 }
509