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