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