1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2007 Marcel Moolenaar
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/bio.h>
34#include <sys/disklabel.h>
35#include <sys/endian.h>
36#include <sys/kernel.h>
37#include <sys/kobj.h>
38#include <sys/limits.h>
39#include <sys/lock.h>
40#include <sys/malloc.h>
41#include <sys/mutex.h>
42#include <sys/queue.h>
43#include <sys/sbuf.h>
44#include <sys/systm.h>
45#include <sys/sysctl.h>
46#include <geom/geom.h>
47#include <geom/part/g_part.h>
48
49#include "g_part_if.h"
50
51#define	BOOT1_SIZE	512
52#define	LABEL_SIZE	512
53#define	BOOT2_OFF	(BOOT1_SIZE + LABEL_SIZE)
54#define	BOOT2_SIZE	(BBSIZE - BOOT2_OFF)
55
56FEATURE(geom_part_bsd, "GEOM partitioning class for BSD disklabels");
57
58struct g_part_bsd_table {
59	struct g_part_table	base;
60	u_char			*bbarea;
61	uint32_t		offset;
62};
63
64struct g_part_bsd_entry {
65	struct g_part_entry	base;
66	struct partition	part;
67};
68
69static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *,
70    struct g_part_parms *);
71static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *);
72static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *);
73static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
74static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
75    struct sbuf *, const char *);
76static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
77static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,
78    struct g_part_parms *);
79static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
80    char *, size_t);
81static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
82static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
83static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
84    char *, size_t);
85static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
86static int g_part_bsd_resize(struct g_part_table *, struct g_part_entry *,
87    struct g_part_parms *);
88
89static kobj_method_t g_part_bsd_methods[] = {
90	KOBJMETHOD(g_part_add,		g_part_bsd_add),
91	KOBJMETHOD(g_part_bootcode,	g_part_bsd_bootcode),
92	KOBJMETHOD(g_part_create,	g_part_bsd_create),
93	KOBJMETHOD(g_part_destroy,	g_part_bsd_destroy),
94	KOBJMETHOD(g_part_dumpconf,	g_part_bsd_dumpconf),
95	KOBJMETHOD(g_part_dumpto,	g_part_bsd_dumpto),
96	KOBJMETHOD(g_part_modify,	g_part_bsd_modify),
97	KOBJMETHOD(g_part_resize,	g_part_bsd_resize),
98	KOBJMETHOD(g_part_name,		g_part_bsd_name),
99	KOBJMETHOD(g_part_probe,	g_part_bsd_probe),
100	KOBJMETHOD(g_part_read,		g_part_bsd_read),
101	KOBJMETHOD(g_part_type,		g_part_bsd_type),
102	KOBJMETHOD(g_part_write,	g_part_bsd_write),
103	{ 0, 0 }
104};
105
106static struct g_part_scheme g_part_bsd_scheme = {
107	"BSD",
108	g_part_bsd_methods,
109	sizeof(struct g_part_bsd_table),
110	.gps_entrysz = sizeof(struct g_part_bsd_entry),
111	.gps_minent = 8,
112	.gps_maxent = 20,	/* Only 22 entries fit in 512 byte sectors */
113	.gps_bootcodesz = BBSIZE,
114};
115G_PART_SCHEME_DECLARE(g_part_bsd);
116MODULE_VERSION(geom_part_bsd, 0);
117
118static struct g_part_bsd_alias {
119	uint8_t		type;
120	int		alias;
121} bsd_alias_match[] = {
122	{ FS_BSDFFS,	G_PART_ALIAS_FREEBSD_UFS },
123	{ FS_SWAP,	G_PART_ALIAS_FREEBSD_SWAP },
124	{ FS_ZFS,	G_PART_ALIAS_FREEBSD_ZFS },
125	{ FS_VINUM,	G_PART_ALIAS_FREEBSD_VINUM },
126	{ FS_NANDFS,	G_PART_ALIAS_FREEBSD_NANDFS },
127	{ FS_HAMMER,	G_PART_ALIAS_DFBSD_HAMMER },
128	{ FS_HAMMER2,	G_PART_ALIAS_DFBSD_HAMMER2 },
129};
130
131static int
132bsd_parse_type(const char *type, uint8_t *fstype)
133{
134	const char *alias;
135	char *endp;
136	long lt;
137	int i;
138
139	if (type[0] == '!') {
140		lt = strtol(type + 1, &endp, 0);
141		if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
142			return (EINVAL);
143		*fstype = (u_int)lt;
144		return (0);
145	}
146	for (i = 0; i < nitems(bsd_alias_match); i++) {
147		alias = g_part_alias_name(bsd_alias_match[i].alias);
148		if (strcasecmp(type, alias) == 0) {
149			*fstype = bsd_alias_match[i].type;
150			return (0);
151		}
152	}
153	return (EINVAL);
154}
155
156static int
157g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
158    struct g_part_parms *gpp)
159{
160	struct g_part_bsd_entry *entry;
161	struct g_part_bsd_table *table;
162
163	if (gpp->gpp_parms & G_PART_PARM_LABEL)
164		return (EINVAL);
165
166	entry = (struct g_part_bsd_entry *)baseentry;
167	table = (struct g_part_bsd_table *)basetable;
168
169	entry->part.p_size = gpp->gpp_size;
170	entry->part.p_offset = gpp->gpp_start + table->offset;
171	entry->part.p_fsize = 0;
172	entry->part.p_frag = 0;
173	entry->part.p_cpg = 0;
174	return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
175}
176
177static int
178g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
179{
180	struct g_part_bsd_table *table;
181	const u_char *codeptr;
182
183	if (gpp->gpp_codesize != BOOT1_SIZE && gpp->gpp_codesize != BBSIZE)
184		return (ENODEV);
185
186	table = (struct g_part_bsd_table *)basetable;
187	codeptr = gpp->gpp_codeptr;
188	bcopy(codeptr, table->bbarea, BOOT1_SIZE);
189	if (gpp->gpp_codesize == BBSIZE)
190		bcopy(codeptr + BOOT2_OFF, table->bbarea + BOOT2_OFF,
191		    BOOT2_SIZE);
192	return (0);
193}
194
195static int
196g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
197{
198	struct g_provider *pp;
199	struct g_part_entry *baseentry;
200	struct g_part_bsd_entry *entry;
201	struct g_part_bsd_table *table;
202	u_char *ptr;
203	uint32_t msize, ncyls, secpercyl;
204
205	pp = gpp->gpp_provider;
206
207	if (pp->sectorsize < sizeof(struct disklabel))
208		return (ENOSPC);
209	if (BBSIZE % pp->sectorsize)
210		return (ENOTBLK);
211
212	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
213	secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
214	ncyls = msize / secpercyl;
215
216	table = (struct g_part_bsd_table *)basetable;
217	table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
218	ptr = table->bbarea + pp->sectorsize;
219
220	le32enc(ptr + 0, DISKMAGIC);			/* d_magic */
221	le32enc(ptr + 40, pp->sectorsize);		/* d_secsize */
222	le32enc(ptr + 44, basetable->gpt_sectors);	/* d_nsectors */
223	le32enc(ptr + 48, basetable->gpt_heads);	/* d_ntracks */
224	le32enc(ptr + 52, ncyls);			/* d_ncylinders */
225	le32enc(ptr + 56, secpercyl);			/* d_secpercyl */
226	le32enc(ptr + 60, msize);			/* d_secperunit */
227	le16enc(ptr + 72, 3600);			/* d_rpm */
228	le32enc(ptr + 132, DISKMAGIC);			/* d_magic2 */
229	le16enc(ptr + 138, basetable->gpt_entries);	/* d_npartitions */
230	le32enc(ptr + 140, BBSIZE);			/* d_bbsize */
231
232	basetable->gpt_first = 0;
233	basetable->gpt_last = msize - 1;
234	basetable->gpt_isleaf = 1;
235
236	baseentry = g_part_new_entry(basetable, RAW_PART + 1,
237	    basetable->gpt_first, basetable->gpt_last);
238	baseentry->gpe_internal = 1;
239	entry = (struct g_part_bsd_entry *)baseentry;
240	entry->part.p_size = basetable->gpt_last + 1;
241	entry->part.p_offset = table->offset;
242
243	return (0);
244}
245
246static int
247g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
248{
249	struct g_part_bsd_table *table;
250
251	table = (struct g_part_bsd_table *)basetable;
252	if (table->bbarea != NULL)
253		g_free(table->bbarea);
254	table->bbarea = NULL;
255
256	/* Wipe the second sector to clear the partitioning. */
257	basetable->gpt_smhead |= 2;
258	return (0);
259}
260
261static void
262g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
263    struct sbuf *sb, const char *indent)
264{
265	struct g_part_bsd_entry *entry;
266
267	entry = (struct g_part_bsd_entry *)baseentry;
268	if (indent == NULL) {
269		/* conftxt: libdisk compatibility */
270		sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
271	} else if (entry != NULL) {
272		/* confxml: partition entry information */
273		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
274		    entry->part.p_fstype);
275	} else {
276		/* confxml: scheme information */
277	}
278}
279
280static int
281g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
282{
283	struct g_part_bsd_entry *entry;
284
285	/* Allow dumping to a swap partition or an unused partition. */
286	entry = (struct g_part_bsd_entry *)baseentry;
287	return ((entry->part.p_fstype == FS_UNUSED ||
288	    entry->part.p_fstype == FS_SWAP) ? 1 : 0);
289}
290
291static int
292g_part_bsd_modify(struct g_part_table *basetable,
293    struct g_part_entry *baseentry, struct g_part_parms *gpp)
294{
295	struct g_part_bsd_entry *entry;
296
297	if (gpp->gpp_parms & G_PART_PARM_LABEL)
298		return (EINVAL);
299
300	entry = (struct g_part_bsd_entry *)baseentry;
301	if (gpp->gpp_parms & G_PART_PARM_TYPE)
302		return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
303	return (0);
304}
305
306static void
307bsd_set_rawsize(struct g_part_table *basetable, struct g_provider *pp)
308{
309	struct g_part_bsd_table *table;
310	struct g_part_bsd_entry *entry;
311	struct g_part_entry *baseentry;
312	uint32_t msize;
313
314	table = (struct g_part_bsd_table *)basetable;
315	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
316	le32enc(table->bbarea + pp->sectorsize + 60, msize); /* d_secperunit */
317	basetable->gpt_last = msize - 1;
318	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
319		if (baseentry->gpe_index != RAW_PART + 1)
320			continue;
321		baseentry->gpe_end = basetable->gpt_last;
322		entry = (struct g_part_bsd_entry *)baseentry;
323		entry->part.p_size = msize;
324		return;
325	}
326}
327
328static int
329g_part_bsd_resize(struct g_part_table *basetable,
330    struct g_part_entry *baseentry, struct g_part_parms *gpp)
331{
332	struct g_part_bsd_entry *entry;
333	struct g_provider *pp;
334
335	if (baseentry == NULL) {
336		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
337		bsd_set_rawsize(basetable, pp);
338		return (0);
339	}
340	entry = (struct g_part_bsd_entry *)baseentry;
341	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
342	entry->part.p_size = gpp->gpp_size;
343
344	return (0);
345}
346
347static const char *
348g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
349    char *buf, size_t bufsz)
350{
351
352	snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
353	return (buf);
354}
355
356static int
357g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
358{
359	struct g_provider *pp;
360	u_char *buf;
361	uint32_t magic1, magic2;
362	int error;
363
364	pp = cp->provider;
365
366	/* Sanity-check the provider. */
367	if (pp->sectorsize < sizeof(struct disklabel) ||
368	    pp->mediasize < BBSIZE)
369		return (ENOSPC);
370	if (BBSIZE % pp->sectorsize)
371		return (ENOTBLK);
372
373	/* Check that there's a disklabel. */
374	buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
375	if (buf == NULL)
376		return (error);
377	magic1 = le32dec(buf + 0);
378	magic2 = le32dec(buf + 132);
379	g_free(buf);
380	return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
381	    ? G_PART_PROBE_PRI_HIGH : ENXIO);
382}
383
384static int
385g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
386{
387	struct g_provider *pp;
388	struct g_part_bsd_table *table;
389	struct g_part_entry *baseentry;
390	struct g_part_bsd_entry *entry;
391	struct partition part;
392	u_char *buf, *p;
393	off_t chs, msize;
394	u_int sectors, heads;
395	int error, index;
396
397	pp = cp->provider;
398	table = (struct g_part_bsd_table *)basetable;
399	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
400
401	table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
402	if (table->bbarea == NULL)
403		return (error);
404
405	buf = table->bbarea + pp->sectorsize;
406
407	if (le32dec(buf + 40) != pp->sectorsize)
408		goto invalid_label;
409	sectors = le32dec(buf + 44);
410	if (sectors < 1 || sectors > 255)
411		goto invalid_label;
412	if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
413		g_part_geometry_heads(msize, sectors, &chs, &heads);
414		if (chs != 0) {
415			basetable->gpt_sectors = sectors;
416			basetable->gpt_heads = heads;
417		}
418	}
419	heads = le32dec(buf + 48);
420	if (heads < 1 || heads > 255)
421		goto invalid_label;
422	if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
423		basetable->gpt_heads = heads;
424
425	chs = le32dec(buf + 60);
426	if (chs < 1)
427		goto invalid_label;
428	/* Fix-up a sysinstall bug. */
429	if (chs > msize) {
430		chs = msize;
431		le32enc(buf + 60, msize);
432	}
433
434	basetable->gpt_first = 0;
435	basetable->gpt_last = msize - 1;
436	basetable->gpt_isleaf = 1;
437
438	basetable->gpt_entries = le16dec(buf + 138);
439	if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
440	    basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
441		goto invalid_label;
442
443	table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
444	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
445		p = buf + 148 + index * 16;
446		part.p_size = le32dec(p + 0);
447		part.p_offset = le32dec(p + 4);
448		part.p_fsize = le32dec(p + 8);
449		part.p_fstype = p[12];
450		part.p_frag = p[13];
451		part.p_cpg = le16dec(p + 14);
452		if (part.p_size == 0)
453			continue;
454		if (part.p_offset < table->offset)
455			continue;
456		if (part.p_offset - table->offset > basetable->gpt_last)
457			goto invalid_label;
458		baseentry = g_part_new_entry(basetable, index + 1,
459		    part.p_offset - table->offset,
460		    part.p_offset - table->offset + part.p_size - 1);
461		entry = (struct g_part_bsd_entry *)baseentry;
462		entry->part = part;
463		if (index == RAW_PART)
464			baseentry->gpe_internal = 1;
465	}
466
467	return (0);
468
469 invalid_label:
470	printf("GEOM: %s: invalid disklabel.\n", pp->name);
471	g_free(table->bbarea);
472	table->bbarea = NULL;
473	return (EINVAL);
474}
475
476static const char *
477g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
478    char *buf, size_t bufsz)
479{
480	struct g_part_bsd_entry *entry;
481	int type;
482
483	entry = (struct g_part_bsd_entry *)baseentry;
484	type = entry->part.p_fstype;
485	if (type == FS_NANDFS)
486		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
487	if (type == FS_SWAP)
488		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
489	if (type == FS_BSDFFS)
490		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
491	if (type == FS_VINUM)
492		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
493	if (type == FS_ZFS)
494		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
495	snprintf(buf, bufsz, "!%d", type);
496	return (buf);
497}
498
499static int
500g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
501{
502	struct g_provider *pp;
503	struct g_part_entry *baseentry;
504	struct g_part_bsd_entry *entry;
505	struct g_part_bsd_table *table;
506	uint16_t sum;
507	u_char *label, *p, *pe;
508	int error, index;
509
510	pp = cp->provider;
511	table = (struct g_part_bsd_table *)basetable;
512	baseentry = LIST_FIRST(&basetable->gpt_entry);
513	label = table->bbarea + pp->sectorsize;
514	for (index = 1; index <= basetable->gpt_entries; index++) {
515		p = label + 148 + (index - 1) * 16;
516		entry = (baseentry != NULL && index == baseentry->gpe_index)
517		    ? (struct g_part_bsd_entry *)baseentry : NULL;
518		if (entry != NULL && !baseentry->gpe_deleted) {
519			le32enc(p + 0, entry->part.p_size);
520			le32enc(p + 4, entry->part.p_offset);
521			le32enc(p + 8, entry->part.p_fsize);
522			p[12] = entry->part.p_fstype;
523			p[13] = entry->part.p_frag;
524			le16enc(p + 14, entry->part.p_cpg);
525		} else
526			bzero(p, 16);
527
528		if (entry != NULL)
529			baseentry = LIST_NEXT(baseentry, gpe_entry);
530	}
531
532	/* Calculate checksum. */
533	le16enc(label + 136, 0);
534	pe = label + 148 + basetable->gpt_entries * 16;
535	sum = 0;
536	for (p = label; p < pe; p += 2)
537		sum ^= le16dec(p);
538	le16enc(label + 136, sum);
539
540	error = g_write_data(cp, 0, table->bbarea, BBSIZE);
541	return (error);
542}
543