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