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