xref: /freebsd/sys/geom/part/g_part_bsd.c (revision de7b456e596ff18032d2cbfdf244c66f36770da4)
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_NANDFS);
130 	if (!strcasecmp(type, alias)) {
131 		*fstype = FS_NANDFS;
132 		return (0);
133 	}
134 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
135 	if (!strcasecmp(type, alias)) {
136 		*fstype = FS_SWAP;
137 		return (0);
138 	}
139 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
140 	if (!strcasecmp(type, alias)) {
141 		*fstype = FS_BSDFFS;
142 		return (0);
143 	}
144 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
145 	if (!strcasecmp(type, alias)) {
146 		*fstype = FS_VINUM;
147 		return (0);
148 	}
149 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
150 	if (!strcasecmp(type, alias)) {
151 		*fstype = FS_ZFS;
152 		return (0);
153 	}
154 	return (EINVAL);
155 }
156 
157 static int
158 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
159     struct g_part_parms *gpp)
160 {
161 	struct g_part_bsd_entry *entry;
162 	struct g_part_bsd_table *table;
163 
164 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
165 		return (EINVAL);
166 
167 	entry = (struct g_part_bsd_entry *)baseentry;
168 	table = (struct g_part_bsd_table *)basetable;
169 
170 	entry->part.p_size = gpp->gpp_size;
171 	entry->part.p_offset = gpp->gpp_start + table->offset;
172 	entry->part.p_fsize = 0;
173 	entry->part.p_frag = 0;
174 	entry->part.p_cpg = 0;
175 	return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
176 }
177 
178 static int
179 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
180 {
181 	struct g_part_bsd_table *table;
182 	const u_char *codeptr;
183 
184 	if (gpp->gpp_codesize != BOOT1_SIZE && gpp->gpp_codesize != BBSIZE)
185 		return (ENODEV);
186 
187 	table = (struct g_part_bsd_table *)basetable;
188 	codeptr = gpp->gpp_codeptr;
189 	bcopy(codeptr, table->bbarea, BOOT1_SIZE);
190 	if (gpp->gpp_codesize == BBSIZE)
191 		bcopy(codeptr + BOOT2_OFF, table->bbarea + BOOT2_OFF,
192 		    BOOT2_SIZE);
193 	return (0);
194 }
195 
196 static int
197 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
198 {
199 	struct g_provider *pp;
200 	struct g_part_entry *baseentry;
201 	struct g_part_bsd_entry *entry;
202 	struct g_part_bsd_table *table;
203 	u_char *ptr;
204 	uint32_t msize, ncyls, secpercyl;
205 
206 	pp = gpp->gpp_provider;
207 
208 	if (pp->sectorsize < sizeof(struct disklabel))
209 		return (ENOSPC);
210 	if (BBSIZE % pp->sectorsize)
211 		return (ENOTBLK);
212 
213 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
214 	secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
215 	ncyls = msize / secpercyl;
216 
217 	table = (struct g_part_bsd_table *)basetable;
218 	table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
219 	ptr = table->bbarea + pp->sectorsize;
220 
221 	le32enc(ptr + 0, DISKMAGIC);			/* d_magic */
222 	le32enc(ptr + 40, pp->sectorsize);		/* d_secsize */
223 	le32enc(ptr + 44, basetable->gpt_sectors);	/* d_nsectors */
224 	le32enc(ptr + 48, basetable->gpt_heads);	/* d_ntracks */
225 	le32enc(ptr + 52, ncyls);			/* d_ncylinders */
226 	le32enc(ptr + 56, secpercyl);			/* d_secpercyl */
227 	le32enc(ptr + 60, msize);			/* d_secperunit */
228 	le16enc(ptr + 72, 3600);			/* d_rpm */
229 	le32enc(ptr + 132, DISKMAGIC);			/* d_magic2 */
230 	le16enc(ptr + 138, basetable->gpt_entries);	/* d_npartitions */
231 	le32enc(ptr + 140, BBSIZE);			/* d_bbsize */
232 
233 	basetable->gpt_first = 0;
234 	basetable->gpt_last = msize - 1;
235 	basetable->gpt_isleaf = 1;
236 
237 	baseentry = g_part_new_entry(basetable, RAW_PART + 1,
238 	    basetable->gpt_first, basetable->gpt_last);
239 	baseentry->gpe_internal = 1;
240 	entry = (struct g_part_bsd_entry *)baseentry;
241 	entry->part.p_size = basetable->gpt_last + 1;
242 	entry->part.p_offset = table->offset;
243 
244 	return (0);
245 }
246 
247 static int
248 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
249 {
250 	struct g_part_bsd_table *table;
251 
252 	table = (struct g_part_bsd_table *)basetable;
253 	if (table->bbarea != NULL)
254 		g_free(table->bbarea);
255 	table->bbarea = NULL;
256 
257 	/* Wipe the second sector to clear the partitioning. */
258 	basetable->gpt_smhead |= 2;
259 	return (0);
260 }
261 
262 static void
263 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
264     struct sbuf *sb, const char *indent)
265 {
266 	struct g_part_bsd_entry *entry;
267 
268 	entry = (struct g_part_bsd_entry *)baseentry;
269 	if (indent == NULL) {
270 		/* conftxt: libdisk compatibility */
271 		sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
272 	} else if (entry != NULL) {
273 		/* confxml: partition entry information */
274 		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
275 		    entry->part.p_fstype);
276 	} else {
277 		/* confxml: scheme information */
278 	}
279 }
280 
281 static int
282 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
283 {
284 	struct g_part_bsd_entry *entry;
285 
286 	/* Allow dumping to a swap partition or an unused partition. */
287 	entry = (struct g_part_bsd_entry *)baseentry;
288 	return ((entry->part.p_fstype == FS_UNUSED ||
289 	    entry->part.p_fstype == FS_SWAP) ? 1 : 0);
290 }
291 
292 static int
293 g_part_bsd_modify(struct g_part_table *basetable,
294     struct g_part_entry *baseentry, struct g_part_parms *gpp)
295 {
296 	struct g_part_bsd_entry *entry;
297 
298 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
299 		return (EINVAL);
300 
301 	entry = (struct g_part_bsd_entry *)baseentry;
302 	if (gpp->gpp_parms & G_PART_PARM_TYPE)
303 		return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
304 	return (0);
305 }
306 
307 static void
308 bsd_set_rawsize(struct g_part_table *basetable, struct g_provider *pp)
309 {
310 	struct g_part_bsd_table *table;
311 	struct g_part_bsd_entry *entry;
312 	struct g_part_entry *baseentry;
313 	uint32_t msize;
314 
315 	table = (struct g_part_bsd_table *)basetable;
316 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
317 	le32enc(table->bbarea + pp->sectorsize + 60, msize); /* d_secperunit */
318 	basetable->gpt_last = msize - 1;
319 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
320 		if (baseentry->gpe_index != RAW_PART + 1)
321 			continue;
322 		baseentry->gpe_end = basetable->gpt_last;
323 		entry = (struct g_part_bsd_entry *)baseentry;
324 		entry->part.p_size = msize;
325 		return;
326 	}
327 }
328 
329 static int
330 g_part_bsd_resize(struct g_part_table *basetable,
331     struct g_part_entry *baseentry, struct g_part_parms *gpp)
332 {
333 	struct g_part_bsd_entry *entry;
334 	struct g_provider *pp;
335 
336 	if (baseentry == NULL) {
337 		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
338 		bsd_set_rawsize(basetable, pp);
339 		return (0);
340 	}
341 	entry = (struct g_part_bsd_entry *)baseentry;
342 	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
343 	entry->part.p_size = gpp->gpp_size;
344 
345 	return (0);
346 }
347 
348 static const char *
349 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
350     char *buf, size_t bufsz)
351 {
352 
353 	snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
354 	return (buf);
355 }
356 
357 static int
358 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
359 {
360 	struct g_provider *pp;
361 	u_char *buf;
362 	uint32_t magic1, magic2;
363 	int error;
364 
365 	pp = cp->provider;
366 
367 	/* Sanity-check the provider. */
368 	if (pp->sectorsize < sizeof(struct disklabel) ||
369 	    pp->mediasize < BBSIZE)
370 		return (ENOSPC);
371 	if (BBSIZE % pp->sectorsize)
372 		return (ENOTBLK);
373 
374 	/* Check that there's a disklabel. */
375 	buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
376 	if (buf == NULL)
377 		return (error);
378 	magic1 = le32dec(buf + 0);
379 	magic2 = le32dec(buf + 132);
380 	g_free(buf);
381 	return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
382 	    ? G_PART_PROBE_PRI_HIGH : ENXIO);
383 }
384 
385 static int
386 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
387 {
388 	struct g_provider *pp;
389 	struct g_part_bsd_table *table;
390 	struct g_part_entry *baseentry;
391 	struct g_part_bsd_entry *entry;
392 	struct partition part;
393 	u_char *buf, *p;
394 	off_t chs, msize;
395 	u_int sectors, heads;
396 	int error, index;
397 
398 	pp = cp->provider;
399 	table = (struct g_part_bsd_table *)basetable;
400 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
401 
402 	table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
403 	if (table->bbarea == NULL)
404 		return (error);
405 
406 	buf = table->bbarea + pp->sectorsize;
407 
408 	if (le32dec(buf + 40) != pp->sectorsize)
409 		goto invalid_label;
410 	sectors = le32dec(buf + 44);
411 	if (sectors < 1 || sectors > 255)
412 		goto invalid_label;
413 	if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
414 		g_part_geometry_heads(msize, sectors, &chs, &heads);
415 		if (chs != 0) {
416 			basetable->gpt_sectors = sectors;
417 			basetable->gpt_heads = heads;
418 		}
419 	}
420 	heads = le32dec(buf + 48);
421 	if (heads < 1 || heads > 255)
422 		goto invalid_label;
423 	if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
424 		basetable->gpt_heads = heads;
425 
426 	chs = le32dec(buf + 60);
427 	if (chs < 1)
428 		goto invalid_label;
429 	/* Fix-up a sysinstall bug. */
430 	if (chs > msize) {
431 		chs = msize;
432 		le32enc(buf + 60, msize);
433 	}
434 
435 	basetable->gpt_first = 0;
436 	basetable->gpt_last = msize - 1;
437 	basetable->gpt_isleaf = 1;
438 
439 	basetable->gpt_entries = le16dec(buf + 138);
440 	if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
441 	    basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
442 		goto invalid_label;
443 
444 	table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
445 	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
446 		p = buf + 148 + index * 16;
447 		part.p_size = le32dec(p + 0);
448 		part.p_offset = le32dec(p + 4);
449 		part.p_fsize = le32dec(p + 8);
450 		part.p_fstype = p[12];
451 		part.p_frag = p[13];
452 		part.p_cpg = le16dec(p + 14);
453 		if (part.p_size == 0)
454 			continue;
455 		if (part.p_offset < table->offset)
456 			continue;
457 		if (part.p_offset - table->offset > basetable->gpt_last)
458 			goto invalid_label;
459 		baseentry = g_part_new_entry(basetable, index + 1,
460 		    part.p_offset - table->offset,
461 		    part.p_offset - table->offset + part.p_size - 1);
462 		entry = (struct g_part_bsd_entry *)baseentry;
463 		entry->part = part;
464 		if (index == RAW_PART)
465 			baseentry->gpe_internal = 1;
466 	}
467 
468 	return (0);
469 
470  invalid_label:
471 	printf("GEOM: %s: invalid disklabel.\n", pp->name);
472 	g_free(table->bbarea);
473 	table->bbarea = NULL;
474 	return (EINVAL);
475 }
476 
477 static const char *
478 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
479     char *buf, size_t bufsz)
480 {
481 	struct g_part_bsd_entry *entry;
482 	int type;
483 
484 	entry = (struct g_part_bsd_entry *)baseentry;
485 	type = entry->part.p_fstype;
486 	if (type == FS_NANDFS)
487 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
488 	if (type == FS_SWAP)
489 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
490 	if (type == FS_BSDFFS)
491 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
492 	if (type == FS_VINUM)
493 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
494 	if (type == FS_ZFS)
495 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
496 	snprintf(buf, bufsz, "!%d", type);
497 	return (buf);
498 }
499 
500 static int
501 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
502 {
503 	struct g_provider *pp;
504 	struct g_part_entry *baseentry;
505 	struct g_part_bsd_entry *entry;
506 	struct g_part_bsd_table *table;
507 	uint16_t sum;
508 	u_char *label, *p, *pe;
509 	int error, index;
510 
511 	pp = cp->provider;
512 	table = (struct g_part_bsd_table *)basetable;
513 	baseentry = LIST_FIRST(&basetable->gpt_entry);
514 	label = table->bbarea + pp->sectorsize;
515 	for (index = 1; index <= basetable->gpt_entries; index++) {
516 		p = label + 148 + (index - 1) * 16;
517 		entry = (baseentry != NULL && index == baseentry->gpe_index)
518 		    ? (struct g_part_bsd_entry *)baseentry : NULL;
519 		if (entry != NULL && !baseentry->gpe_deleted) {
520 			le32enc(p + 0, entry->part.p_size);
521 			le32enc(p + 4, entry->part.p_offset);
522 			le32enc(p + 8, entry->part.p_fsize);
523 			p[12] = entry->part.p_fstype;
524 			p[13] = entry->part.p_frag;
525 			le16enc(p + 14, entry->part.p_cpg);
526 		} else
527 			bzero(p, 16);
528 
529 		if (entry != NULL)
530 			baseentry = LIST_NEXT(baseentry, gpe_entry);
531 	}
532 
533 	/* Calculate checksum. */
534 	le16enc(label + 136, 0);
535 	pe = label + 148 + basetable->gpt_entries * 16;
536 	sum = 0;
537 	for (p = label; p < pe; p += 2)
538 		sum ^= le16dec(p);
539 	le16enc(label + 136, sum);
540 
541 	error = g_write_data(cp, 0, table->bbarea, BBSIZE);
542 	return (error);
543 }
544