xref: /freebsd/sys/geom/part/g_part_mbr.c (revision b7c60aadbbd5c846a250c05791fe7406d6d78bf4)
1 /*-
2  * Copyright (c) 2007, 2008 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/diskmbr.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_mbr, "GEOM partitioning class for MBR support");
50 
51 #define	MBRSIZE		512
52 
53 struct g_part_mbr_table {
54 	struct g_part_table	base;
55 	u_char		mbr[MBRSIZE];
56 };
57 
58 struct g_part_mbr_entry {
59 	struct g_part_entry	base;
60 	struct dos_partition ent;
61 };
62 
63 static int g_part_mbr_add(struct g_part_table *, struct g_part_entry *,
64     struct g_part_parms *);
65 static int g_part_mbr_bootcode(struct g_part_table *, struct g_part_parms *);
66 static int g_part_mbr_create(struct g_part_table *, struct g_part_parms *);
67 static int g_part_mbr_destroy(struct g_part_table *, struct g_part_parms *);
68 static void g_part_mbr_dumpconf(struct g_part_table *, struct g_part_entry *,
69     struct sbuf *, const char *);
70 static int g_part_mbr_dumpto(struct g_part_table *, struct g_part_entry *);
71 static int g_part_mbr_modify(struct g_part_table *, struct g_part_entry *,
72     struct g_part_parms *);
73 static const char *g_part_mbr_name(struct g_part_table *, struct g_part_entry *,
74     char *, size_t);
75 static int g_part_mbr_probe(struct g_part_table *, struct g_consumer *);
76 static int g_part_mbr_read(struct g_part_table *, struct g_consumer *);
77 static int g_part_mbr_setunset(struct g_part_table *, struct g_part_entry *,
78     const char *, unsigned int);
79 static const char *g_part_mbr_type(struct g_part_table *, struct g_part_entry *,
80     char *, size_t);
81 static int g_part_mbr_write(struct g_part_table *, struct g_consumer *);
82 static int g_part_mbr_resize(struct g_part_table *, struct g_part_entry *,
83     struct g_part_parms *);
84 
85 static kobj_method_t g_part_mbr_methods[] = {
86 	KOBJMETHOD(g_part_add,		g_part_mbr_add),
87 	KOBJMETHOD(g_part_bootcode,	g_part_mbr_bootcode),
88 	KOBJMETHOD(g_part_create,	g_part_mbr_create),
89 	KOBJMETHOD(g_part_destroy,	g_part_mbr_destroy),
90 	KOBJMETHOD(g_part_dumpconf,	g_part_mbr_dumpconf),
91 	KOBJMETHOD(g_part_dumpto,	g_part_mbr_dumpto),
92 	KOBJMETHOD(g_part_modify,	g_part_mbr_modify),
93 	KOBJMETHOD(g_part_resize,	g_part_mbr_resize),
94 	KOBJMETHOD(g_part_name,		g_part_mbr_name),
95 	KOBJMETHOD(g_part_probe,	g_part_mbr_probe),
96 	KOBJMETHOD(g_part_read,		g_part_mbr_read),
97 	KOBJMETHOD(g_part_setunset,	g_part_mbr_setunset),
98 	KOBJMETHOD(g_part_type,		g_part_mbr_type),
99 	KOBJMETHOD(g_part_write,	g_part_mbr_write),
100 	{ 0, 0 }
101 };
102 
103 static struct g_part_scheme g_part_mbr_scheme = {
104 	"MBR",
105 	g_part_mbr_methods,
106 	sizeof(struct g_part_mbr_table),
107 	.gps_entrysz = sizeof(struct g_part_mbr_entry),
108 	.gps_minent = NDOSPART,
109 	.gps_maxent = NDOSPART,
110 	.gps_bootcodesz = MBRSIZE,
111 };
112 G_PART_SCHEME_DECLARE(g_part_mbr);
113 
114 static struct g_part_mbr_alias {
115 	u_char		typ;
116 	int		alias;
117 } mbr_alias_match[] = {
118 	{ DOSPTYP_386BSD,	G_PART_ALIAS_FREEBSD },
119 	{ DOSPTYP_EXT,		G_PART_ALIAS_EBR },
120 	{ DOSPTYP_NTFS,		G_PART_ALIAS_MS_NTFS },
121 	{ DOSPTYP_FAT32,	G_PART_ALIAS_MS_FAT32 },
122 	{ DOSPTYP_LINSWP,	G_PART_ALIAS_LINUX_SWAP },
123 	{ DOSPTYP_LINUX,	G_PART_ALIAS_LINUX_DATA },
124 	{ DOSPTYP_LINLVM,	G_PART_ALIAS_LINUX_LVM },
125 	{ DOSPTYP_LINRAID,	G_PART_ALIAS_LINUX_RAID },
126 	{ DOSPTYP_PPCBOOT,	G_PART_ALIAS_FREEBSD_BOOT },
127 };
128 
129 static int
130 mbr_parse_type(const char *type, u_char *dp_typ)
131 {
132 	const char *alias;
133 	char *endp;
134 	long lt;
135 	int i;
136 
137 	if (type[0] == '!') {
138 		lt = strtol(type + 1, &endp, 0);
139 		if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
140 			return (EINVAL);
141 		*dp_typ = (u_char)lt;
142 		return (0);
143 	}
144 	for (i = 0;
145 	    i < sizeof(mbr_alias_match) / sizeof(mbr_alias_match[0]); i++) {
146 		alias = g_part_alias_name(mbr_alias_match[i].alias);
147 		if (strcasecmp(type, alias) == 0) {
148 			*dp_typ = mbr_alias_match[i].typ;
149 			return (0);
150 		}
151 	}
152 	return (EINVAL);
153 }
154 
155 static int
156 mbr_probe_bpb(u_char *bpb)
157 {
158 	uint16_t secsz;
159 	uint8_t clstsz;
160 
161 #define PO2(x)	((x & (x - 1)) == 0)
162 	secsz = le16dec(bpb);
163 	if (secsz < 512 || secsz > 4096 || !PO2(secsz))
164 		return (0);
165 	clstsz = bpb[2];
166 	if (clstsz < 1 || clstsz > 128 || !PO2(clstsz))
167 		return (0);
168 #undef PO2
169 
170 	return (1);
171 }
172 
173 static void
174 mbr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
175     u_char *secp)
176 {
177 	uint32_t cyl, hd, sec;
178 
179 	sec = lba % table->gpt_sectors + 1;
180 	lba /= table->gpt_sectors;
181 	hd = lba % table->gpt_heads;
182 	lba /= table->gpt_heads;
183 	cyl = lba;
184 	if (cyl > 1023)
185 		sec = hd = cyl = ~0;
186 
187 	*cylp = cyl & 0xff;
188 	*hdp = hd & 0xff;
189 	*secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
190 }
191 
192 static int
193 g_part_mbr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
194     struct g_part_parms *gpp)
195 {
196 	struct g_part_mbr_entry *entry;
197 	struct g_part_mbr_table *table;
198 	uint32_t start, size, sectors;
199 
200 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
201 		return (EINVAL);
202 
203 	sectors = basetable->gpt_sectors;
204 
205 	entry = (struct g_part_mbr_entry *)baseentry;
206 	table = (struct g_part_mbr_table *)basetable;
207 
208 	start = gpp->gpp_start;
209 	size = gpp->gpp_size;
210 	if (size < sectors)
211 		return (EINVAL);
212 	if (start % sectors) {
213 		size = size - sectors + (start % sectors);
214 		start = start - (start % sectors) + sectors;
215 	}
216 	if (size % sectors)
217 		size = size - (size % sectors);
218 	if (size < sectors)
219 		return (EINVAL);
220 
221 	if (baseentry->gpe_deleted)
222 		bzero(&entry->ent, sizeof(entry->ent));
223 
224 	KASSERT(baseentry->gpe_start <= start, ("%s", __func__));
225 	KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__));
226 	baseentry->gpe_start = start;
227 	baseentry->gpe_end = start + size - 1;
228 	entry->ent.dp_start = start;
229 	entry->ent.dp_size = size;
230 	mbr_set_chs(basetable, baseentry->gpe_start, &entry->ent.dp_scyl,
231 	    &entry->ent.dp_shd, &entry->ent.dp_ssect);
232 	mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
233 	    &entry->ent.dp_ehd, &entry->ent.dp_esect);
234 	return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
235 }
236 
237 static int
238 g_part_mbr_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
239 {
240 	struct g_part_mbr_table *table;
241 	uint32_t dsn;
242 
243 	if (gpp->gpp_codesize != MBRSIZE)
244 		return (ENODEV);
245 
246 	table = (struct g_part_mbr_table *)basetable;
247 	dsn = *(uint32_t *)(table->mbr + DOSDSNOFF);
248 	bcopy(gpp->gpp_codeptr, table->mbr, DOSPARTOFF);
249 	if (dsn != 0)
250 		*(uint32_t *)(table->mbr + DOSDSNOFF) = dsn;
251 	return (0);
252 }
253 
254 static int
255 g_part_mbr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
256 {
257 	struct g_provider *pp;
258 	struct g_part_mbr_table *table;
259 
260 	pp = gpp->gpp_provider;
261 	if (pp->sectorsize < MBRSIZE)
262 		return (ENOSPC);
263 
264 	basetable->gpt_first = basetable->gpt_sectors;
265 	basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
266 	    UINT32_MAX) - 1;
267 
268 	table = (struct g_part_mbr_table *)basetable;
269 	le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
270 	return (0);
271 }
272 
273 static int
274 g_part_mbr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
275 {
276 
277 	/* Wipe the first sector to clear the partitioning. */
278 	basetable->gpt_smhead |= 1;
279 	return (0);
280 }
281 
282 static void
283 g_part_mbr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
284     struct sbuf *sb, const char *indent)
285 {
286 	struct g_part_mbr_entry *entry;
287 
288 	entry = (struct g_part_mbr_entry *)baseentry;
289 	if (indent == NULL) {
290 		/* conftxt: libdisk compatibility */
291 		sbuf_printf(sb, " xs MBR xt %u", entry->ent.dp_typ);
292 	} else if (entry != NULL) {
293 		/* confxml: partition entry information */
294 		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
295 		    entry->ent.dp_typ);
296 		if (entry->ent.dp_flag & 0x80)
297 			sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
298 	} else {
299 		/* confxml: scheme information */
300 	}
301 }
302 
303 static int
304 g_part_mbr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
305 {
306 	struct g_part_mbr_entry *entry;
307 
308 	/* Allow dumping to a FreeBSD partition or Linux swap partition only. */
309 	entry = (struct g_part_mbr_entry *)baseentry;
310 	return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
311 	    entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
312 }
313 
314 static int
315 g_part_mbr_modify(struct g_part_table *basetable,
316     struct g_part_entry *baseentry, struct g_part_parms *gpp)
317 {
318 	struct g_part_mbr_entry *entry;
319 
320 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
321 		return (EINVAL);
322 
323 	entry = (struct g_part_mbr_entry *)baseentry;
324 	if (gpp->gpp_parms & G_PART_PARM_TYPE)
325 		return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
326 	return (0);
327 }
328 
329 static int
330 g_part_mbr_resize(struct g_part_table *basetable,
331     struct g_part_entry *baseentry, struct g_part_parms *gpp)
332 {
333 	struct g_part_mbr_entry *entry;
334 	uint32_t size, sectors;
335 
336 	sectors = basetable->gpt_sectors;
337 	size = gpp->gpp_size;
338 
339 	if (size < sectors)
340 		return (EINVAL);
341 	if (size % sectors)
342 		size = size - (size % sectors);
343 	if (size < sectors)
344 		return (EINVAL);
345 
346 	entry = (struct g_part_mbr_entry *)baseentry;
347 	baseentry->gpe_end = baseentry->gpe_start + size - 1;
348 	entry->ent.dp_size = size;
349 	mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
350 	    &entry->ent.dp_ehd, &entry->ent.dp_esect);
351 	return (0);
352 }
353 
354 static const char *
355 g_part_mbr_name(struct g_part_table *table, struct g_part_entry *baseentry,
356     char *buf, size_t bufsz)
357 {
358 
359 	snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
360 	return (buf);
361 }
362 
363 static int
364 g_part_mbr_probe(struct g_part_table *table, struct g_consumer *cp)
365 {
366 	char psn[8];
367 	struct g_provider *pp;
368 	u_char *buf, *p;
369 	int error, index, res, sum;
370 	uint16_t magic;
371 
372 	pp = cp->provider;
373 
374 	/* Sanity-check the provider. */
375 	if (pp->sectorsize < MBRSIZE || pp->mediasize < pp->sectorsize)
376 		return (ENOSPC);
377 	if (pp->sectorsize > 4096)
378 		return (ENXIO);
379 
380 	/* We don't nest under an MBR (see EBR instead). */
381 	error = g_getattr("PART::scheme", cp, &psn);
382 	if (error == 0 && strcmp(psn, g_part_mbr_scheme.name) == 0)
383 		return (ELOOP);
384 
385 	/* Check that there's a MBR. */
386 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
387 	if (buf == NULL)
388 		return (error);
389 
390 	/* We goto out on mismatch. */
391 	res = ENXIO;
392 
393 	magic = le16dec(buf + DOSMAGICOFFSET);
394 	if (magic != DOSMAGIC)
395 		goto out;
396 
397 	for (index = 0; index < NDOSPART; index++) {
398 		p = buf + DOSPARTOFF + index * DOSPARTSIZE;
399 		if (p[0] != 0 && p[0] != 0x80)
400 			goto out;
401 	}
402 
403 	/*
404 	 * If the partition table does not consist of all zeroes,
405 	 * assume we have a MBR. If it's all zeroes, we could have
406 	 * a boot sector. For example, a boot sector that doesn't
407 	 * have boot code -- common on non-i386 hardware. In that
408 	 * case we check if we have a possible BPB. If so, then we
409 	 * assume we have a boot sector instead.
410 	 */
411 	sum = 0;
412 	for (index = 0; index < NDOSPART * DOSPARTSIZE; index++)
413 		sum += buf[DOSPARTOFF + index];
414 	if (sum != 0 || !mbr_probe_bpb(buf + 0x0b))
415 		res = G_PART_PROBE_PRI_NORM;
416 
417  out:
418 	g_free(buf);
419 	return (res);
420 }
421 
422 static int
423 g_part_mbr_read(struct g_part_table *basetable, struct g_consumer *cp)
424 {
425 	struct dos_partition ent;
426 	struct g_provider *pp;
427 	struct g_part_mbr_table *table;
428 	struct g_part_mbr_entry *entry;
429 	u_char *buf, *p;
430 	off_t chs, msize, first;
431 	u_int sectors, heads;
432 	int error, index;
433 
434 	pp = cp->provider;
435 	table = (struct g_part_mbr_table *)basetable;
436 	first = basetable->gpt_sectors;
437 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
438 
439 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
440 	if (buf == NULL)
441 		return (error);
442 
443 	bcopy(buf, table->mbr, sizeof(table->mbr));
444 	for (index = NDOSPART - 1; index >= 0; index--) {
445 		p = buf + DOSPARTOFF + index * DOSPARTSIZE;
446 		ent.dp_flag = p[0];
447 		ent.dp_shd = p[1];
448 		ent.dp_ssect = p[2];
449 		ent.dp_scyl = p[3];
450 		ent.dp_typ = p[4];
451 		ent.dp_ehd = p[5];
452 		ent.dp_esect = p[6];
453 		ent.dp_ecyl = p[7];
454 		ent.dp_start = le32dec(p + 8);
455 		ent.dp_size = le32dec(p + 12);
456 		if (ent.dp_typ == 0 || ent.dp_typ == DOSPTYP_PMBR)
457 			continue;
458 		if (ent.dp_start == 0 || ent.dp_size == 0)
459 			continue;
460 		sectors = ent.dp_esect & 0x3f;
461 		if (sectors > basetable->gpt_sectors &&
462 		    !basetable->gpt_fixgeom) {
463 			g_part_geometry_heads(msize, sectors, &chs, &heads);
464 			if (chs != 0) {
465 				basetable->gpt_sectors = sectors;
466 				basetable->gpt_heads = heads;
467 			}
468 		}
469 		if (ent.dp_start < first)
470 			first = ent.dp_start;
471 		entry = (struct g_part_mbr_entry *)g_part_new_entry(basetable,
472 		    index + 1, ent.dp_start, ent.dp_start + ent.dp_size - 1);
473 		entry->ent = ent;
474 	}
475 
476 	basetable->gpt_entries = NDOSPART;
477 	basetable->gpt_first = basetable->gpt_sectors;
478 	basetable->gpt_last = msize - 1;
479 
480 	if (first < basetable->gpt_first)
481 		basetable->gpt_first = 1;
482 
483 	g_free(buf);
484 	return (0);
485 }
486 
487 static int
488 g_part_mbr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
489     const char *attrib, unsigned int set)
490 {
491 	struct g_part_entry *iter;
492 	struct g_part_mbr_entry *entry;
493 	int changed;
494 
495 	if (strcasecmp(attrib, "active") != 0)
496 		return (EINVAL);
497 
498 	/* Only one entry can have the active attribute. */
499 	LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
500 		if (iter->gpe_deleted)
501 			continue;
502 		changed = 0;
503 		entry = (struct g_part_mbr_entry *)iter;
504 		if (iter == baseentry) {
505 			if (set && (entry->ent.dp_flag & 0x80) == 0) {
506 				entry->ent.dp_flag |= 0x80;
507 				changed = 1;
508 			} else if (!set && (entry->ent.dp_flag & 0x80)) {
509 				entry->ent.dp_flag &= ~0x80;
510 				changed = 1;
511 			}
512 		} else {
513 			if (set && (entry->ent.dp_flag & 0x80)) {
514 				entry->ent.dp_flag &= ~0x80;
515 				changed = 1;
516 			}
517 		}
518 		if (changed && !iter->gpe_created)
519 			iter->gpe_modified = 1;
520 	}
521 	return (0);
522 }
523 
524 static const char *
525 g_part_mbr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
526     char *buf, size_t bufsz)
527 {
528 	struct g_part_mbr_entry *entry;
529 	int i;
530 
531 	entry = (struct g_part_mbr_entry *)baseentry;
532 	for (i = 0;
533 	    i < sizeof(mbr_alias_match) / sizeof(mbr_alias_match[0]); i++) {
534 		if (mbr_alias_match[i].typ == entry->ent.dp_typ)
535 			return (g_part_alias_name(mbr_alias_match[i].alias));
536 	}
537 	snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
538 	return (buf);
539 }
540 
541 static int
542 g_part_mbr_write(struct g_part_table *basetable, struct g_consumer *cp)
543 {
544 	struct g_part_entry *baseentry;
545 	struct g_part_mbr_entry *entry;
546 	struct g_part_mbr_table *table;
547 	u_char *p;
548 	int error, index;
549 
550 	table = (struct g_part_mbr_table *)basetable;
551 	baseentry = LIST_FIRST(&basetable->gpt_entry);
552 	for (index = 1; index <= basetable->gpt_entries; index++) {
553 		p = table->mbr + DOSPARTOFF + (index - 1) * DOSPARTSIZE;
554 		entry = (baseentry != NULL && index == baseentry->gpe_index)
555 		    ? (struct g_part_mbr_entry *)baseentry : NULL;
556 		if (entry != NULL && !baseentry->gpe_deleted) {
557 			p[0] = entry->ent.dp_flag;
558 			p[1] = entry->ent.dp_shd;
559 			p[2] = entry->ent.dp_ssect;
560 			p[3] = entry->ent.dp_scyl;
561 			p[4] = entry->ent.dp_typ;
562 			p[5] = entry->ent.dp_ehd;
563 			p[6] = entry->ent.dp_esect;
564 			p[7] = entry->ent.dp_ecyl;
565 			le32enc(p + 8, entry->ent.dp_start);
566 			le32enc(p + 12, entry->ent.dp_size);
567 		} else
568 			bzero(p, DOSPARTSIZE);
569 
570 		if (entry != NULL)
571 			baseentry = LIST_NEXT(baseentry, gpe_entry);
572 	}
573 
574 	error = g_write_data(cp, 0, table->mbr, cp->provider->sectorsize);
575 	return (error);
576 }
577