1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
6 * All rights reserved.
7 *
8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9 * and NAI Labs, the Security Research Division of Network Associates, Inc.
10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11 * DARPA CHATS research program.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. The names of the authors may not be used to endorse or promote
22 *    products derived from this software without specific prior written
23 *    permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38/*
39 * This is the method for dealing with BSD disklabels.  It has been
40 * extensively (by my standards at least) commented, in the vain hope that
41 * it will serve as the source in future copy&paste operations.
42 */
43
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD$");
46
47#include <sys/param.h>
48#include <sys/endian.h>
49#include <sys/systm.h>
50#include <sys/sysctl.h>
51#include <sys/kernel.h>
52#include <sys/fcntl.h>
53#include <sys/conf.h>
54#include <sys/bio.h>
55#include <sys/malloc.h>
56#include <sys/lock.h>
57#include <sys/mutex.h>
58#include <sys/md5.h>
59#include <sys/errno.h>
60#include <sys/disklabel.h>
61#include <sys/gpt.h>
62#include <sys/proc.h>
63#include <sys/sbuf.h>
64#include <sys/uuid.h>
65#include <geom/geom.h>
66#include <geom/geom_slice.h>
67
68FEATURE(geom_bsd, "GEOM BSD disklabels support");
69
70#define	BSD_CLASS_NAME "BSD"
71
72#define ALPHA_LABEL_OFFSET	64
73#define HISTORIC_LABEL_OFFSET	512
74
75#define LABELSIZE (148 + 16 * MAXPARTITIONS)
76
77static int g_bsd_once;
78
79static void g_bsd_hotwrite(void *arg, int flag);
80/*
81 * Our private data about one instance.  All the rest is handled by the
82 * slice code and stored in its softc, so this is just the stuff
83 * specific to BSD disklabels.
84 */
85struct g_bsd_softc {
86	off_t	labeloffset;
87	off_t	mbroffset;
88	off_t	rawoffset;
89	struct disklabel ondisk;
90	u_char	label[LABELSIZE];
91	u_char	labelsum[16];
92};
93
94/*
95 * Modify our slicer to match proposed disklabel, if possible.
96 * This is where we make sure we don't do something stupid.
97 */
98static int
99g_bsd_modify(struct g_geom *gp, u_char *label)
100{
101	int i, error;
102	struct partition *ppp;
103	struct g_slicer *gsp;
104	struct g_consumer *cp;
105	struct g_bsd_softc *ms;
106	u_int secsize, u;
107	off_t rawoffset, o;
108	struct disklabel dl;
109	MD5_CTX md5sum;
110
111	g_topology_assert();
112	gsp = gp->softc;
113	ms = gsp->softc;
114
115	error = bsd_disklabel_le_dec(label, &dl, MAXPARTITIONS);
116	if (error) {
117		return (error);
118	}
119
120	/* Get dimensions of our device. */
121	cp = LIST_FIRST(&gp->consumer);
122	secsize = cp->provider->sectorsize;
123
124	/* ... or a smaller sector size. */
125	if (dl.d_secsize < secsize) {
126		return (EINVAL);
127	}
128
129	/* ... or a non-multiple sector size. */
130	if (dl.d_secsize % secsize != 0) {
131		return (EINVAL);
132	}
133
134	/* Historical braindamage... */
135	rawoffset = (off_t)dl.d_partitions[RAW_PART].p_offset * dl.d_secsize;
136
137	for (i = 0; i < dl.d_npartitions; i++) {
138		ppp = &dl.d_partitions[i];
139		if (ppp->p_size == 0)
140			continue;
141	        o = (off_t)ppp->p_offset * dl.d_secsize;
142
143		if (o < rawoffset)
144			rawoffset = 0;
145	}
146
147	if (rawoffset != 0 && (off_t)rawoffset != ms->mbroffset)
148		printf("WARNING: %s expected rawoffset %jd, found %jd\n",
149		    gp->name,
150		    (intmax_t)ms->mbroffset/dl.d_secsize,
151		    (intmax_t)rawoffset/dl.d_secsize);
152
153	/* Don't munge open partitions. */
154	for (i = 0; i < dl.d_npartitions; i++) {
155		ppp = &dl.d_partitions[i];
156
157	        o = (off_t)ppp->p_offset * dl.d_secsize;
158		if (o == 0)
159			o = rawoffset;
160		error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
161		    o - rawoffset,
162		    (off_t)ppp->p_size * dl.d_secsize,
163		     dl.d_secsize,
164		    "%s%c", gp->name, 'a' + i);
165		if (error)
166			return (error);
167	}
168
169	/* Look good, go for it... */
170	for (u = 0; u < gsp->nslice; u++) {
171		ppp = &dl.d_partitions[u];
172	        o = (off_t)ppp->p_offset * dl.d_secsize;
173		if (o == 0)
174			o = rawoffset;
175		g_slice_config(gp, u, G_SLICE_CONFIG_SET,
176		    o - rawoffset,
177		    (off_t)ppp->p_size * dl.d_secsize,
178		     dl.d_secsize,
179		    "%s%c", gp->name, 'a' + u);
180	}
181
182	/* Update our softc */
183	ms->ondisk = dl;
184	if (label != ms->label)
185		bcopy(label, ms->label, LABELSIZE);
186	ms->rawoffset = rawoffset;
187
188	/*
189	 * In order to avoid recursively attaching to the same
190	 * on-disk label (it's usually visible through the 'c'
191	 * partition) we calculate an MD5 and ask if other BSD's
192	 * below us love that label.  If they do, we don't.
193	 */
194	MD5Init(&md5sum);
195	MD5Update(&md5sum, ms->label, sizeof(ms->label));
196	MD5Final(ms->labelsum, &md5sum);
197
198	return (0);
199}
200
201/*
202 * This is an internal helper function, called multiple times from the taste
203 * function to try to locate a disklabel on the disk.  More civilized formats
204 * will not need this, as there is only one possible place on disk to look
205 * for the magic spot.
206 */
207
208static int
209g_bsd_try(struct g_geom *gp, struct g_slicer *gsp, struct g_consumer *cp, int secsize, struct g_bsd_softc *ms, off_t offset)
210{
211	int error;
212	u_char *buf;
213	struct disklabel *dl;
214	off_t secoff;
215
216	/*
217	 * We need to read entire aligned sectors, and we assume that the
218	 * disklabel does not span sectors, so one sector is enough.
219	 */
220	secoff = offset % secsize;
221	buf = g_read_data(cp, offset - secoff, secsize, NULL);
222	if (buf == NULL)
223		return (ENOENT);
224
225	/* Decode into our native format. */
226	dl = &ms->ondisk;
227	error = bsd_disklabel_le_dec(buf + secoff, dl, MAXPARTITIONS);
228	if (!error)
229		bcopy(buf + secoff, ms->label, LABELSIZE);
230
231	/* Remember to free the buffer g_read_data() gave us. */
232	g_free(buf);
233
234	ms->labeloffset = offset;
235	return (error);
236}
237
238/*
239 * This function writes the current label to disk, possibly updating
240 * the alpha SRM checksum.
241 */
242
243static int
244g_bsd_writelabel(struct g_geom *gp, u_char *bootcode)
245{
246	off_t secoff;
247	u_int secsize;
248	struct g_consumer *cp;
249	struct g_slicer *gsp;
250	struct g_bsd_softc *ms;
251	u_char *buf;
252	uint64_t sum;
253	int error, i;
254
255	gsp = gp->softc;
256	ms = gsp->softc;
257	cp = LIST_FIRST(&gp->consumer);
258	/* Get sector size, we need it to read data. */
259	secsize = cp->provider->sectorsize;
260	secoff = ms->labeloffset % secsize;
261	if (bootcode == NULL) {
262		buf = g_read_data(cp, ms->labeloffset - secoff, secsize, &error);
263		if (buf == NULL)
264			return (error);
265		bcopy(ms->label, buf + secoff, sizeof(ms->label));
266	} else {
267		buf = bootcode;
268		bcopy(ms->label, buf + ms->labeloffset, sizeof(ms->label));
269	}
270	if (ms->labeloffset == ALPHA_LABEL_OFFSET) {
271		sum = 0;
272		for (i = 0; i < 63; i++)
273			sum += le64dec(buf + i * 8);
274		le64enc(buf + 504, sum);
275	}
276	if (bootcode == NULL) {
277		error = g_write_data(cp, ms->labeloffset - secoff, buf, secsize);
278		g_free(buf);
279	} else {
280		error = g_write_data(cp, 0, bootcode, BBSIZE);
281	}
282	return(error);
283}
284
285/*
286 * If the user tries to overwrite our disklabel through an open partition
287 * or via a magicwrite config call, we end up here and try to prevent
288 * footshooting as best we can.
289 */
290static void
291g_bsd_hotwrite(void *arg, int flag)
292{
293	struct bio *bp;
294	struct g_geom *gp;
295	struct g_slicer *gsp;
296	struct g_slice *gsl;
297	struct g_bsd_softc *ms;
298	u_char *p;
299	int error;
300
301	g_topology_assert();
302	/*
303	 * We should never get canceled, because that would amount to a removal
304	 * of the geom while there was outstanding I/O requests.
305	 */
306	KASSERT(flag != EV_CANCEL, ("g_bsd_hotwrite cancelled"));
307	bp = arg;
308	gp = bp->bio_to->geom;
309	gsp = gp->softc;
310	ms = gsp->softc;
311	gsl = &gsp->slices[bp->bio_to->index];
312	p = (u_char*)bp->bio_data + ms->labeloffset -
313	    (bp->bio_offset + gsl->offset);
314	error = g_bsd_modify(gp, p);
315	if (error) {
316		g_io_deliver(bp, EPERM);
317		return;
318	}
319	g_slice_finish_hot(bp);
320}
321
322static int
323g_bsd_start(struct bio *bp)
324{
325	struct g_geom *gp;
326	struct g_bsd_softc *ms;
327	struct g_slicer *gsp;
328
329	gp = bp->bio_to->geom;
330	gsp = gp->softc;
331	ms = gsp->softc;
332	if (bp->bio_cmd == BIO_GETATTR) {
333		if (g_handleattr(bp, "BSD::labelsum", ms->labelsum,
334		    sizeof(ms->labelsum)))
335			return (1);
336	}
337	return (0);
338}
339
340/*
341 * Dump configuration information in XML format.
342 * Notice that the function is called once for the geom and once for each
343 * consumer and provider.  We let g_slice_dumpconf() do most of the work.
344 */
345static void
346g_bsd_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
347{
348	struct g_bsd_softc *ms;
349	struct g_slicer *gsp;
350
351	gsp = gp->softc;
352	ms = gsp->softc;
353	g_slice_dumpconf(sb, indent, gp, cp, pp);
354	if (indent != NULL && pp == NULL && cp == NULL) {
355		sbuf_printf(sb, "%s<labeloffset>%jd</labeloffset>\n",
356		    indent, (intmax_t)ms->labeloffset);
357		sbuf_printf(sb, "%s<rawoffset>%jd</rawoffset>\n",
358		    indent, (intmax_t)ms->rawoffset);
359		sbuf_printf(sb, "%s<mbroffset>%jd</mbroffset>\n",
360		    indent, (intmax_t)ms->mbroffset);
361	} else if (pp != NULL) {
362		if (indent == NULL)
363			sbuf_printf(sb, " ty %d",
364			    ms->ondisk.d_partitions[pp->index].p_fstype);
365		else
366			sbuf_printf(sb, "%s<type>%d</type>\n", indent,
367			    ms->ondisk.d_partitions[pp->index].p_fstype);
368	}
369}
370
371/*
372 * The taste function is called from the event-handler, with the topology
373 * lock already held and a provider to examine.  The flags are unused.
374 *
375 * If flags == G_TF_NORMAL, the idea is to take a bite of the provider and
376 * if we find valid, consistent magic on it, build a geom on it.
377 *
378 * There may be cases where the operator would like to put a BSD-geom on
379 * providers which do not meet all of the requirements.  This can be done
380 * by instead passing the G_TF_INSIST flag, which will override these
381 * checks.
382 *
383 * The final flags value is G_TF_TRANSPARENT, which instructs the method
384 * to put a geom on top of the provider and configure it to be as transparent
385 * as possible.  This is not really relevant to the BSD method and therefore
386 * not implemented here.
387 */
388
389static struct uuid freebsd_slice = GPT_ENT_TYPE_FREEBSD;
390
391static struct g_geom *
392g_bsd_taste(struct g_class *mp, struct g_provider *pp, int flags)
393{
394	struct g_geom *gp;
395	struct g_consumer *cp;
396	int error, i;
397	struct g_bsd_softc *ms;
398	u_int secsize;
399	struct g_slicer *gsp;
400	u_char hash[16];
401	MD5_CTX md5sum;
402	struct uuid uuid;
403
404	g_trace(G_T_TOPOLOGY, "bsd_taste(%s,%s)", mp->name, pp->name);
405	g_topology_assert();
406
407	/* We don't implement transparent inserts. */
408	if (flags == G_TF_TRANSPARENT)
409		return (NULL);
410
411	/*
412	 * BSD labels are a subclass of the general "slicing" topology so
413	 * a lot of the work can be done by the common "slice" code.
414	 * Create a geom with space for MAXPARTITIONS providers, one consumer
415	 * and a softc structure for us.  Specify the provider to attach
416	 * the consumer to and our "start" routine for special requests.
417	 * The provider is opened with mode (1,0,0) so we can do reads
418	 * from it.
419	 */
420	gp = g_slice_new(mp, MAXPARTITIONS, pp, &cp, &ms,
421	     sizeof(*ms), g_bsd_start);
422	if (gp == NULL)
423		return (NULL);
424
425	/* Get the geom_slicer softc from the geom. */
426	gsp = gp->softc;
427
428	/*
429	 * The do...while loop here allows us to have multiple escapes
430	 * using a simple "break".  This improves code clarity without
431	 * ending up in deep nesting and without using goto or come from.
432	 */
433	do {
434		/*
435		 * If the provider is an MBR we will only auto attach
436		 * to type 165 slices in the G_TF_NORMAL case.  We will
437		 * attach to any other type.
438		 */
439		error = g_getattr("MBR::type", cp, &i);
440		if (!error) {
441			if (i != 165 && flags == G_TF_NORMAL)
442				break;
443			error = g_getattr("MBR::offset", cp, &ms->mbroffset);
444			if (error)
445				break;
446		}
447
448		/* Same thing if we are inside a GPT */
449		error = g_getattr("GPT::type", cp, &uuid);
450		if (!error) {
451			if (memcmp(&uuid, &freebsd_slice, sizeof(uuid)) != 0 &&
452			    flags == G_TF_NORMAL)
453				break;
454		}
455
456		/* Get sector size, we need it to read data. */
457		secsize = cp->provider->sectorsize;
458		if (secsize < 512)
459			break;
460
461		/* First look for a label at the start of the second sector. */
462		error = g_bsd_try(gp, gsp, cp, secsize, ms, secsize);
463
464		/*
465		 * If sector size is not 512 the label still can be at
466		 * offset 512, not at the start of the second sector. At least
467		 * it's true for labels created by the FreeBSD's bsdlabel(8).
468		 */
469		if (error && secsize != HISTORIC_LABEL_OFFSET)
470			error = g_bsd_try(gp, gsp, cp, secsize, ms,
471			    HISTORIC_LABEL_OFFSET);
472
473		/* Next, look for alpha labels */
474		if (error)
475			error = g_bsd_try(gp, gsp, cp, secsize, ms,
476			    ALPHA_LABEL_OFFSET);
477
478		/* If we didn't find a label, punt. */
479		if (error)
480			break;
481
482		/*
483		 * In order to avoid recursively attaching to the same
484		 * on-disk label (it's usually visible through the 'c'
485		 * partition) we calculate an MD5 and ask if other BSD's
486		 * below us love that label.  If they do, we don't.
487		 */
488		MD5Init(&md5sum);
489		MD5Update(&md5sum, ms->label, sizeof(ms->label));
490		MD5Final(ms->labelsum, &md5sum);
491
492		error = g_getattr("BSD::labelsum", cp, &hash);
493		if (!error && !bcmp(ms->labelsum, hash, sizeof(hash)))
494			break;
495
496		/*
497		 * Process the found disklabel, and modify our "slice"
498		 * instance to match it, if possible.
499		 */
500		error = g_bsd_modify(gp, ms->label);
501	} while (0);
502
503	/* Success or failure, we can close our provider now. */
504	g_access(cp, -1, 0, 0);
505
506	/* If we have configured any providers, return the new geom. */
507	if (gsp->nprovider > 0) {
508		g_slice_conf_hot(gp, 0, ms->labeloffset, LABELSIZE,
509		    G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
510		gsp->hot = g_bsd_hotwrite;
511		if (!g_bsd_once) {
512			g_bsd_once = 1;
513			printf(
514			    "WARNING: geom_bsd (geom %s) is deprecated, "
515			    "use gpart instead.\n", gp->name);
516		}
517		return (gp);
518	}
519	/*
520	 * ...else push the "self-destruct" button, by spoiling our own
521	 * consumer.  This triggers a call to g_slice_spoiled which will
522	 * dismantle what was setup.
523	 */
524	g_slice_spoiled(cp);
525	return (NULL);
526}
527
528struct h0h0 {
529	struct g_geom *gp;
530	struct g_bsd_softc *ms;
531	u_char *label;
532	int error;
533};
534
535static void
536g_bsd_callconfig(void *arg, int flag)
537{
538	struct h0h0 *hp;
539
540	hp = arg;
541	hp->error = g_bsd_modify(hp->gp, hp->label);
542	if (!hp->error)
543		hp->error = g_bsd_writelabel(hp->gp, NULL);
544}
545
546/*
547 * NB! curthread is user process which GCTL'ed.
548 */
549static void
550g_bsd_config(struct gctl_req *req, struct g_class *mp, char const *verb)
551{
552	u_char *label;
553	int error;
554	struct h0h0 h0h0;
555	struct g_geom *gp;
556	struct g_slicer *gsp;
557	struct g_consumer *cp;
558	struct g_bsd_softc *ms;
559
560	g_topology_assert();
561	gp = gctl_get_geom(req, mp, "geom");
562	if (gp == NULL)
563		return;
564	cp = LIST_FIRST(&gp->consumer);
565	gsp = gp->softc;
566	ms = gsp->softc;
567	if (!strcmp(verb, "read mbroffset")) {
568		gctl_set_param_err(req, "mbroffset", &ms->mbroffset,
569		    sizeof(ms->mbroffset));
570		return;
571	} else if (!strcmp(verb, "write label")) {
572		label = gctl_get_paraml(req, "label", LABELSIZE);
573		if (label == NULL)
574			return;
575		h0h0.gp = gp;
576		h0h0.ms = gsp->softc;
577		h0h0.label = label;
578		h0h0.error = -1;
579		/* XXX: Does this reference register with our selfdestruct code ? */
580		error = g_access(cp, 1, 1, 1);
581		if (error) {
582			gctl_error(req, "could not access consumer");
583			return;
584		}
585		g_bsd_callconfig(&h0h0, 0);
586		error = h0h0.error;
587		g_access(cp, -1, -1, -1);
588	} else if (!strcmp(verb, "write bootcode")) {
589		label = gctl_get_paraml(req, "bootcode", BBSIZE);
590		if (label == NULL)
591			return;
592		/* XXX: Does this reference register with our selfdestruct code ? */
593		error = g_access(cp, 1, 1, 1);
594		if (error) {
595			gctl_error(req, "could not access consumer");
596			return;
597		}
598		error = g_bsd_writelabel(gp, label);
599		g_access(cp, -1, -1, -1);
600	} else {
601		gctl_error(req, "Unknown verb parameter");
602	}
603
604	return;
605}
606
607/* Finally, register with GEOM infrastructure. */
608static struct g_class g_bsd_class = {
609	.name = BSD_CLASS_NAME,
610	.version = G_VERSION,
611	.taste = g_bsd_taste,
612	.ctlreq = g_bsd_config,
613	.dumpconf = g_bsd_dumpconf,
614};
615
616DECLARE_GEOM_CLASS(g_bsd_class, g_bsd);
617MODULE_VERSION(geom_bsd, 0);
618