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