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