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