xref: /freebsd/sys/geom/part/g_part_bsd.c (revision 4ed925457ab06e83238a5db33e89ccc94b99a713)
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/disklabel.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 struct g_part_bsd_table {
49 	struct g_part_table	base;
50 	u_char			*bbarea;
51 	uint32_t		offset;
52 };
53 
54 struct g_part_bsd_entry {
55 	struct g_part_entry	base;
56 	struct partition	part;
57 };
58 
59 static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *,
60     struct g_part_parms *);
61 static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *);
62 static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *);
63 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
64 static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
65     struct sbuf *, const char *);
66 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
67 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,
68     struct g_part_parms *);
69 static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
70     char *, size_t);
71 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
72 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
73 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
74     char *, size_t);
75 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
76 
77 static kobj_method_t g_part_bsd_methods[] = {
78 	KOBJMETHOD(g_part_add,		g_part_bsd_add),
79 	KOBJMETHOD(g_part_bootcode,	g_part_bsd_bootcode),
80 	KOBJMETHOD(g_part_create,	g_part_bsd_create),
81 	KOBJMETHOD(g_part_destroy,	g_part_bsd_destroy),
82 	KOBJMETHOD(g_part_dumpconf,	g_part_bsd_dumpconf),
83 	KOBJMETHOD(g_part_dumpto,	g_part_bsd_dumpto),
84 	KOBJMETHOD(g_part_modify,	g_part_bsd_modify),
85 	KOBJMETHOD(g_part_name,		g_part_bsd_name),
86 	KOBJMETHOD(g_part_probe,	g_part_bsd_probe),
87 	KOBJMETHOD(g_part_read,		g_part_bsd_read),
88 	KOBJMETHOD(g_part_type,		g_part_bsd_type),
89 	KOBJMETHOD(g_part_write,	g_part_bsd_write),
90 	{ 0, 0 }
91 };
92 
93 static struct g_part_scheme g_part_bsd_scheme = {
94 	"BSD",
95 	g_part_bsd_methods,
96 	sizeof(struct g_part_bsd_table),
97 	.gps_entrysz = sizeof(struct g_part_bsd_entry),
98 	.gps_minent = 8,
99 	.gps_maxent = 20,
100 	.gps_bootcodesz = BBSIZE,
101 };
102 G_PART_SCHEME_DECLARE(g_part_bsd);
103 
104 static int
105 bsd_parse_type(const char *type, uint8_t *fstype)
106 {
107 	const char *alias;
108 	char *endp;
109 	long lt;
110 
111 	if (type[0] == '!') {
112 		lt = strtol(type + 1, &endp, 0);
113 		if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
114 			return (EINVAL);
115 		*fstype = (u_int)lt;
116 		return (0);
117 	}
118 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
119 	if (!strcasecmp(type, alias)) {
120 		*fstype = FS_SWAP;
121 		return (0);
122 	}
123 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
124 	if (!strcasecmp(type, alias)) {
125 		*fstype = FS_BSDFFS;
126 		return (0);
127 	}
128 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
129 	if (!strcasecmp(type, alias)) {
130 		*fstype = FS_VINUM;
131 		return (0);
132 	}
133 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
134 	if (!strcasecmp(type, alias)) {
135 		*fstype = FS_ZFS;
136 		return (0);
137 	}
138 	return (EINVAL);
139 }
140 
141 static int
142 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
143     struct g_part_parms *gpp)
144 {
145 	struct g_part_bsd_entry *entry;
146 	struct g_part_bsd_table *table;
147 
148 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
149 		return (EINVAL);
150 
151 	entry = (struct g_part_bsd_entry *)baseentry;
152 	table = (struct g_part_bsd_table *)basetable;
153 
154 	entry->part.p_size = gpp->gpp_size;
155 	entry->part.p_offset = gpp->gpp_start + table->offset;
156 	entry->part.p_fsize = 0;
157 	entry->part.p_frag = 0;
158 	entry->part.p_cpg = 0;
159 	return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
160 }
161 
162 static int
163 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
164 {
165 	struct g_part_bsd_table *table;
166 	const u_char *codeptr;
167 	size_t hdsz, tlsz;
168 	size_t codesz, tlofs;
169 
170 	hdsz = 512;
171 	tlofs = hdsz + 148 + basetable->gpt_entries * 16;
172 	tlsz = BBSIZE - tlofs;
173 	table = (struct g_part_bsd_table *)basetable;
174 	bzero(table->bbarea, hdsz);
175 	bzero(table->bbarea + tlofs, tlsz);
176 	codeptr = gpp->gpp_codeptr;
177 	codesz = MIN(hdsz, gpp->gpp_codesize);
178 	if (codesz > 0)
179 		bcopy(codeptr, table->bbarea, codesz);
180 	codesz = MIN(tlsz, gpp->gpp_codesize - tlofs);
181 	if (codesz > 0)
182 		bcopy(codeptr + tlofs, table->bbarea + tlofs, codesz);
183 	return (0);
184 }
185 
186 static int
187 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
188 {
189 	struct g_provider *pp;
190 	struct g_part_entry *baseentry;
191 	struct g_part_bsd_entry *entry;
192 	struct g_part_bsd_table *table;
193 	u_char *ptr;
194 	uint32_t msize, ncyls, secpercyl;
195 
196 	pp = gpp->gpp_provider;
197 
198 	if (pp->sectorsize < sizeof(struct disklabel))
199 		return (ENOSPC);
200 	if (BBSIZE % pp->sectorsize)
201 		return (ENOTBLK);
202 
203 	msize = MIN(pp->mediasize / pp->sectorsize, 0xffffffff);
204 	secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
205 	ncyls = msize / secpercyl;
206 
207 	table = (struct g_part_bsd_table *)basetable;
208 	table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
209 	ptr = table->bbarea + pp->sectorsize;
210 
211 	le32enc(ptr + 0, DISKMAGIC);			/* d_magic */
212 	le32enc(ptr + 40, pp->sectorsize);		/* d_secsize */
213 	le32enc(ptr + 44, basetable->gpt_sectors);	/* d_nsectors */
214 	le32enc(ptr + 48, basetable->gpt_heads);	/* d_ntracks */
215 	le32enc(ptr + 52, ncyls);			/* d_ncylinders */
216 	le32enc(ptr + 56, secpercyl);			/* d_secpercyl */
217 	le32enc(ptr + 60, msize);			/* d_secperunit */
218 	le16enc(ptr + 72, 3600);			/* d_rpm */
219 	le32enc(ptr + 132, DISKMAGIC);			/* d_magic2 */
220 	le16enc(ptr + 138, basetable->gpt_entries);	/* d_npartitions */
221 	le32enc(ptr + 140, BBSIZE);			/* d_bbsize */
222 
223 	basetable->gpt_first = 0;
224 	basetable->gpt_last = msize - 1;
225 	basetable->gpt_isleaf = 1;
226 
227 	baseentry = g_part_new_entry(basetable, RAW_PART + 1,
228 	    basetable->gpt_first, basetable->gpt_last);
229 	baseentry->gpe_internal = 1;
230 	entry = (struct g_part_bsd_entry *)baseentry;
231 	entry->part.p_size = basetable->gpt_last + 1;
232 	entry->part.p_offset = table->offset;
233 
234 	return (0);
235 }
236 
237 static int
238 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
239 {
240 
241 	/* Wipe the second sector to clear the partitioning. */
242 	basetable->gpt_smhead |= 2;
243 	return (0);
244 }
245 
246 static void
247 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
248     struct sbuf *sb, const char *indent)
249 {
250 	struct g_part_bsd_entry *entry;
251 
252 	entry = (struct g_part_bsd_entry *)baseentry;
253 	if (indent == NULL) {
254 		/* conftxt: libdisk compatibility */
255 		sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
256 	} else if (entry != NULL) {
257 		/* confxml: partition entry information */
258 		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
259 		    entry->part.p_fstype);
260 	} else {
261 		/* confxml: scheme information */
262 	}
263 }
264 
265 static int
266 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
267 {
268 	struct g_part_bsd_entry *entry;
269 
270 	/* Allow dumping to a swap partition or an unused partition. */
271 	entry = (struct g_part_bsd_entry *)baseentry;
272 	return ((entry->part.p_fstype == FS_UNUSED ||
273 	    entry->part.p_fstype == FS_SWAP) ? 1 : 0);
274 }
275 
276 static int
277 g_part_bsd_modify(struct g_part_table *basetable,
278     struct g_part_entry *baseentry, struct g_part_parms *gpp)
279 {
280 	struct g_part_bsd_entry *entry;
281 
282 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
283 		return (EINVAL);
284 
285 	entry = (struct g_part_bsd_entry *)baseentry;
286 	if (gpp->gpp_parms & G_PART_PARM_TYPE)
287 		return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
288 	return (0);
289 }
290 
291 static const char *
292 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
293     char *buf, size_t bufsz)
294 {
295 
296 	snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
297 	return (buf);
298 }
299 
300 static int
301 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
302 {
303 	struct g_provider *pp;
304 	u_char *buf;
305 	uint32_t magic1, magic2;
306 	int error;
307 
308 	pp = cp->provider;
309 
310 	/* Sanity-check the provider. */
311 	if (pp->sectorsize < sizeof(struct disklabel) ||
312 	    pp->mediasize < BBSIZE)
313 		return (ENOSPC);
314 	if (BBSIZE % pp->sectorsize)
315 		return (ENOTBLK);
316 
317 	/* Check that there's a disklabel. */
318 	buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
319 	if (buf == NULL)
320 		return (error);
321 	magic1 = le32dec(buf + 0);
322 	magic2 = le32dec(buf + 132);
323 	g_free(buf);
324 	return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
325 	    ? G_PART_PROBE_PRI_HIGH : ENXIO);
326 }
327 
328 static int
329 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
330 {
331 	struct g_provider *pp;
332 	struct g_part_bsd_table *table;
333 	struct g_part_entry *baseentry;
334 	struct g_part_bsd_entry *entry;
335 	struct partition part;
336 	u_char *buf, *p;
337 	off_t chs, msize;
338 	u_int sectors, heads;
339 	int error, index;
340 
341 	pp = cp->provider;
342 	table = (struct g_part_bsd_table *)basetable;
343 	msize = pp->mediasize / pp->sectorsize;
344 
345 	table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
346 	if (table->bbarea == NULL)
347 		return (error);
348 
349 	buf = table->bbarea + pp->sectorsize;
350 
351 	if (le32dec(buf + 40) != pp->sectorsize)
352 		goto invalid_label;
353 	sectors = le32dec(buf + 44);
354 	if (sectors < 1 || sectors > 255)
355 		goto invalid_label;
356 	if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
357 		g_part_geometry_heads(msize, sectors, &chs, &heads);
358 		if (chs != 0) {
359 			basetable->gpt_sectors = sectors;
360 			basetable->gpt_heads = heads;
361 		}
362 	}
363 	heads = le32dec(buf + 48);
364 	if (heads < 1 || heads > 255)
365 		goto invalid_label;
366 	if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
367 		basetable->gpt_heads = heads;
368 	if (sectors != basetable->gpt_sectors || heads != basetable->gpt_heads)
369 		printf("GEOM: %s: geometry does not match label"
370 		    " (%uh,%us != %uh,%us).\n", pp->name, heads, sectors,
371 		    basetable->gpt_heads, basetable->gpt_sectors);
372 
373 	chs = le32dec(buf + 60);
374 	if (chs < 1)
375 		goto invalid_label;
376 	/* Fix-up a sysinstall bug. */
377 	if (chs > msize) {
378 		chs = msize;
379 		le32enc(buf + 60, msize);
380 	}
381 	if (chs != msize)
382 		printf("GEOM: %s: media size does not match label.\n",
383 		    pp->name);
384 
385 	basetable->gpt_first = 0;
386 	basetable->gpt_last = msize - 1;
387 	basetable->gpt_isleaf = 1;
388 
389 	basetable->gpt_entries = le16dec(buf + 138);
390 	if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
391 	    basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
392 		goto invalid_label;
393 
394 	table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
395 	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
396 		p = buf + 148 + index * 16;
397 		part.p_size = le32dec(p + 0);
398 		part.p_offset = le32dec(p + 4);
399 		part.p_fsize = le32dec(p + 8);
400 		part.p_fstype = p[12];
401 		part.p_frag = p[13];
402 		part.p_cpg = le16dec(p + 14);
403 		if (part.p_size == 0)
404 			continue;
405 		if (part.p_offset < table->offset)
406 			continue;
407 		baseentry = g_part_new_entry(basetable, index + 1,
408 		    part.p_offset - table->offset,
409 		    part.p_offset - table->offset + part.p_size - 1);
410 		entry = (struct g_part_bsd_entry *)baseentry;
411 		entry->part = part;
412 		if (index == RAW_PART)
413 			baseentry->gpe_internal = 1;
414 	}
415 
416 	return (0);
417 
418  invalid_label:
419 	printf("GEOM: %s: invalid disklabel.\n", pp->name);
420 	g_free(table->bbarea);
421 	return (EINVAL);
422 }
423 
424 static const char *
425 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
426     char *buf, size_t bufsz)
427 {
428 	struct g_part_bsd_entry *entry;
429 	int type;
430 
431 	entry = (struct g_part_bsd_entry *)baseentry;
432 	type = entry->part.p_fstype;
433 	if (type == FS_SWAP)
434 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
435 	if (type == FS_BSDFFS)
436 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
437 	if (type == FS_VINUM)
438 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
439 	if (type == FS_ZFS)
440 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
441 	snprintf(buf, bufsz, "!%d", type);
442 	return (buf);
443 }
444 
445 static int
446 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
447 {
448 	struct g_provider *pp;
449 	struct g_part_entry *baseentry;
450 	struct g_part_bsd_entry *entry;
451 	struct g_part_bsd_table *table;
452 	uint16_t sum;
453 	u_char *label, *p, *pe;
454 	int error, index;
455 
456 	pp = cp->provider;
457 	table = (struct g_part_bsd_table *)basetable;
458 	baseentry = LIST_FIRST(&basetable->gpt_entry);
459 	label = table->bbarea + pp->sectorsize;
460 	for (index = 1; index <= basetable->gpt_entries; index++) {
461 		p = label + 148 + (index - 1) * 16;
462 		entry = (baseentry != NULL && index == baseentry->gpe_index)
463 		    ? (struct g_part_bsd_entry *)baseentry : NULL;
464 		if (entry != NULL && !baseentry->gpe_deleted) {
465 			le32enc(p + 0, entry->part.p_size);
466 			le32enc(p + 4, entry->part.p_offset);
467 			le32enc(p + 8, entry->part.p_fsize);
468 			p[12] = entry->part.p_fstype;
469 			p[13] = entry->part.p_frag;
470 			le16enc(p + 14, entry->part.p_cpg);
471 		} else
472 			bzero(p, 16);
473 
474 		if (entry != NULL)
475 			baseentry = LIST_NEXT(baseentry, gpe_entry);
476 	}
477 
478 	/* Calculate checksum. */
479 	le16enc(label + 136, 0);
480 	pe = label + 148 + basetable->gpt_entries * 16;
481 	sum = 0;
482 	for (p = label; p < pe; p += 2)
483 		sum ^= le16dec(p);
484 	le16enc(label + 136, sum);
485 
486 	error = g_write_data(cp, 0, table->bbarea, BBSIZE);
487 	return (error);
488 }
489