1189608Ssam/*-
2189608Ssam * Copyright (c) 2009 Sam Leffler, Errno Consulting
3189608Ssam * All rights reserved.
4189608Ssam *
5189608Ssam * Redistribution and use in source and binary forms, with or without
6189608Ssam * modification, are permitted provided that the following conditions
7189608Ssam * are met:
8189608Ssam * 1. Redistributions of source code must retain the above copyright
9189608Ssam *    notice, this list of conditions and the following disclaimer,
10189608Ssam *    without modification.
11189608Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12189608Ssam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13189608Ssam *    redistribution must be conditioned upon including a substantially
14189608Ssam *    similar Disclaimer requirement for further binary redistribution.
15189608Ssam *
16189608Ssam * NO WARRANTY
17189608Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18189608Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19189608Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20189608Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21189608Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22189608Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23189608Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24189608Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25189608Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26189608Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27189608Ssam * THE POSSIBILITY OF SUCH DAMAGES.
28189608Ssam */
29189608Ssam
30189608Ssam#include <sys/cdefs.h>
31189608Ssam__FBSDID("$FreeBSD: stable/11/sys/geom/geom_redboot.c 332640 2018-04-17 02:18:04Z kevans $");
32189608Ssam
33189608Ssam#include <sys/param.h>
34189608Ssam#include <sys/errno.h>
35189608Ssam#include <sys/endian.h>
36189608Ssam#include <sys/systm.h>
37189608Ssam#include <sys/kernel.h>
38189608Ssam#include <sys/fcntl.h>
39189608Ssam#include <sys/malloc.h>
40189608Ssam#include <sys/bio.h>
41189608Ssam#include <sys/lock.h>
42189608Ssam#include <sys/mutex.h>
43189608Ssam
44189608Ssam#include <sys/sbuf.h>
45189608Ssam#include <geom/geom.h>
46189608Ssam#include <geom/geom_slice.h>
47189608Ssam
48189608Ssam#define REDBOOT_CLASS_NAME "REDBOOT"
49189608Ssam
50189608Ssamstruct fis_image_desc {
51189608Ssam	uint8_t		name[16];	/* null-terminated name */
52189608Ssam	uint32_t	offset;		/* offset in flash */
53189608Ssam	uint32_t	addr;		/* address in memory */
54189608Ssam	uint32_t	size;		/* image size in bytes */
55189608Ssam	uint32_t	entry;		/* offset in image for entry point */
56189608Ssam	uint32_t	dsize;		/* data size in bytes */
57189608Ssam	uint8_t		pad[256-(16+7*sizeof(uint32_t)+sizeof(void*))];
58189608Ssam	struct fis_image_desc *next;	/* linked list (in memory) */
59189608Ssam	uint32_t	dsum;		/* descriptor checksum */
60189608Ssam	uint32_t	fsum;		/* checksum over image data */
61189608Ssam};
62189608Ssam
63189608Ssam#define	FISDIR_NAME	"FIS directory"
64189608Ssam#define	REDBCFG_NAME	"RedBoot config"
65189660Ssam#define	REDBOOT_NAME	"RedBoot"
66189608Ssam
67189608Ssam#define	REDBOOT_MAXSLICE	64
68189608Ssam#define	REDBOOT_MAXOFF \
69189608Ssam	(REDBOOT_MAXSLICE*sizeof(struct fis_image_desc))
70189608Ssam
71189608Ssamstruct g_redboot_softc {
72189608Ssam	uint32_t	entry[REDBOOT_MAXSLICE];
73189608Ssam	uint32_t	dsize[REDBOOT_MAXSLICE];
74189660Ssam	uint8_t		readonly[REDBOOT_MAXSLICE];
75189660Ssam	g_access_t	*parent_access;
76189608Ssam};
77189608Ssam
78189608Ssamstatic void
79189608Ssamg_redboot_print(int i, struct fis_image_desc *fd)
80189608Ssam{
81189608Ssam
82189608Ssam	printf("[%2d] \"%-15.15s\" %08x:%08x", i, fd->name,
83189608Ssam	    fd->offset, fd->size);
84189608Ssam	printf(" addr %08x entry %08x\n", fd->addr, fd->entry);
85189608Ssam	printf("     dsize 0x%x dsum 0x%x fsum 0x%x\n", fd->dsize,
86189608Ssam	    fd->dsum, fd->fsum);
87189608Ssam}
88189608Ssam
89189608Ssamstatic int
90189608Ssamg_redboot_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
91189608Ssam{
92189608Ssam	return (ENOIOCTL);
93189608Ssam}
94189608Ssam
95189608Ssamstatic int
96189660Ssamg_redboot_access(struct g_provider *pp, int dread, int dwrite, int dexcl)
97189660Ssam{
98189660Ssam	struct g_geom *gp = pp->geom;
99189660Ssam	struct g_slicer *gsp = gp->softc;
100189660Ssam	struct g_redboot_softc *sc = gsp->softc;
101189660Ssam
102189660Ssam	if (dwrite > 0 && sc->readonly[pp->index])
103189660Ssam		return (EPERM);
104189660Ssam	return (sc->parent_access(pp, dread, dwrite, dexcl));
105189660Ssam}
106189660Ssam
107189660Ssamstatic int
108189608Ssamg_redboot_start(struct bio *bp)
109189608Ssam{
110189608Ssam	struct g_provider *pp;
111189608Ssam	struct g_geom *gp;
112189608Ssam	struct g_redboot_softc *sc;
113189608Ssam	struct g_slicer *gsp;
114189608Ssam	int idx;
115189608Ssam
116189608Ssam	pp = bp->bio_to;
117189608Ssam	idx = pp->index;
118189608Ssam	gp = pp->geom;
119189608Ssam	gsp = gp->softc;
120189608Ssam	sc = gsp->softc;
121189608Ssam	if (bp->bio_cmd == BIO_GETATTR) {
122189608Ssam		if (g_handleattr_int(bp, REDBOOT_CLASS_NAME "::entry",
123189608Ssam		    sc->entry[idx]))
124189608Ssam			return (1);
125189608Ssam		if (g_handleattr_int(bp, REDBOOT_CLASS_NAME "::dsize",
126189608Ssam		    sc->dsize[idx]))
127189608Ssam			return (1);
128189608Ssam	}
129189608Ssam
130189608Ssam	return (0);
131189608Ssam}
132189608Ssam
133189608Ssamstatic void
134189608Ssamg_redboot_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
135189608Ssam	struct g_consumer *cp __unused, struct g_provider *pp)
136189608Ssam{
137189608Ssam	struct g_redboot_softc *sc;
138189608Ssam	struct g_slicer *gsp;
139189608Ssam
140189608Ssam	gsp = gp->softc;
141189608Ssam	sc = gsp->softc;
142189608Ssam	g_slice_dumpconf(sb, indent, gp, cp, pp);
143189608Ssam	if (pp != NULL) {
144189608Ssam		if (indent == NULL) {
145189608Ssam			sbuf_printf(sb, " entry %d", sc->entry[pp->index]);
146189608Ssam			sbuf_printf(sb, " dsize %d", sc->dsize[pp->index]);
147189608Ssam		} else {
148189608Ssam			sbuf_printf(sb, "%s<entry>%d</entry>\n", indent,
149189608Ssam			    sc->entry[pp->index]);
150189608Ssam			sbuf_printf(sb, "%s<dsize>%d</dsize>\n", indent,
151189608Ssam			    sc->dsize[pp->index]);
152189608Ssam		}
153189608Ssam	}
154189608Ssam}
155189608Ssam
156189608Ssam#include <sys/ctype.h>
157189608Ssam
158189608Ssamstatic int
159189608Ssamnameok(const char name[16])
160189608Ssam{
161189608Ssam	int i;
162189608Ssam
163189608Ssam	/* descriptor names are null-terminated printable ascii */
164189608Ssam	for (i = 0; i < 15; i++)
165189608Ssam		if (!isprint(name[i]))
166189608Ssam			break;
167189608Ssam	return (name[i] == '\0');
168189608Ssam}
169189608Ssam
170189608Ssamstatic struct fis_image_desc *
171189608Ssamparse_fis_directory(u_char *buf, size_t bufsize, off_t offset, uint32_t offmask)
172189608Ssam{
173189660Ssam#define	match(a,b)	(bcmp(a, b, sizeof(b)-1) == 0)
174189608Ssam	struct fis_image_desc *fd, *efd;
175189608Ssam	struct fis_image_desc *fisdir, *redbcfg;
176189608Ssam	struct fis_image_desc *head, **tail;
177189608Ssam	int i;
178189608Ssam
179189608Ssam	fd = (struct fis_image_desc *)buf;
180189608Ssam	efd = fd + (bufsize / sizeof(struct fis_image_desc));
181189608Ssam#if 0
182189608Ssam	/*
183189608Ssam	 * Find the start of the FIS table.
184189608Ssam	 */
185189608Ssam	while (fd < efd && fd->name[0] != 0xff)
186189608Ssam		fd++;
187189608Ssam	if (fd == efd)
188189608Ssam		return (NULL);
189189608Ssam	if (bootverbose)
190189608Ssam		printf("RedBoot FIS table starts at 0x%jx\n",
191189608Ssam		    offset + fd - (struct fis_image_desc *) buf);
192189608Ssam#endif
193189608Ssam	/*
194189608Ssam	 * Scan forward collecting entries in a list.
195189608Ssam	 */
196189608Ssam	fisdir = redbcfg = NULL;
197189608Ssam	*(tail = &head) = NULL;
198194811Sjhay	for (i = 0; fd < efd; i++, fd++) {
199194811Sjhay		if (fd->name[0] == 0xff)
200194811Sjhay			continue;
201189608Ssam		if (match(fd->name, FISDIR_NAME))
202189608Ssam			fisdir = fd;
203189608Ssam		else if (match(fd->name, REDBCFG_NAME))
204189608Ssam			redbcfg = fd;
205189608Ssam		if (nameok(fd->name)) {
206189608Ssam			/*
207189608Ssam			 * NB: flash address includes platform mapping;
208189608Ssam			 *     strip it so we have only a flash offset.
209189608Ssam			 */
210189608Ssam			fd->offset &= offmask;
211189608Ssam			if (bootverbose)
212189608Ssam				g_redboot_print(i, fd);
213189608Ssam			*tail = fd;
214189608Ssam			*(tail = &fd->next) = NULL;
215189608Ssam		}
216189608Ssam	}
217189608Ssam	if (fisdir == NULL) {
218189608Ssam		if (bootverbose)
219189608Ssam			printf("No RedBoot FIS table located at %lu\n",
220189608Ssam			    (long) offset);
221189608Ssam		return (NULL);
222189608Ssam	}
223189608Ssam	if (redbcfg != NULL &&
224189608Ssam	    fisdir->offset + fisdir->size == redbcfg->offset) {
225189608Ssam		/*
226189608Ssam		 * Merged FIS/RedBoot config directory.
227189608Ssam		 */
228189608Ssam		if (bootverbose)
229189608Ssam			printf("FIS/RedBoot merged at 0x%jx (not yet)\n",
230189608Ssam			    offset + fisdir->offset);
231189608Ssam		/* XXX */
232189608Ssam	}
233189608Ssam	return head;
234189608Ssam#undef match
235189608Ssam}
236189608Ssam
237189608Ssamstatic struct g_geom *
238189608Ssamg_redboot_taste(struct g_class *mp, struct g_provider *pp, int insist)
239189608Ssam{
240189608Ssam	struct g_geom *gp;
241189608Ssam	struct g_consumer *cp;
242189608Ssam	struct g_redboot_softc *sc;
243189608Ssam	int error, sectorsize, i;
244189608Ssam	struct fis_image_desc *fd, *head;
245189608Ssam	uint32_t offmask;
246189608Ssam	u_int blksize;		/* NB: flash block size stored as stripesize */
247189608Ssam	u_char *buf;
248189608Ssam	off_t offset;
249189608Ssam
250189608Ssam	g_trace(G_T_TOPOLOGY, "redboot_taste(%s,%s)", mp->name, pp->name);
251189608Ssam	g_topology_assert();
252189608Ssam	if (!strcmp(pp->geom->class->name, REDBOOT_CLASS_NAME))
253189608Ssam		return (NULL);
254189608Ssam	/* XXX only taste flash providers */
255203411Sgonzo	if (strncmp(pp->name, "cfi", 3) &&
256203411Sgonzo	    strncmp(pp->name, "flash/spi", 9))
257189608Ssam		return (NULL);
258189608Ssam	gp = g_slice_new(mp, REDBOOT_MAXSLICE, pp, &cp, &sc, sizeof(*sc),
259189608Ssam	    g_redboot_start);
260189608Ssam	if (gp == NULL)
261189608Ssam		return (NULL);
262189660Ssam	/* interpose our access method */
263189660Ssam	sc->parent_access = gp->access;
264189660Ssam	gp->access = g_redboot_access;
265189660Ssam
266189608Ssam	sectorsize = cp->provider->sectorsize;
267189608Ssam	blksize = cp->provider->stripesize;
268189608Ssam	if (powerof2(cp->provider->mediasize))
269189608Ssam		offmask = cp->provider->mediasize-1;
270189608Ssam	else
271189608Ssam		offmask = 0xffffffff;		/* XXX */
272189608Ssam	if (bootverbose)
273189608Ssam		printf("%s: mediasize %ld secsize %d blksize %d offmask 0x%x\n",
274189608Ssam		    __func__, (long) cp->provider->mediasize, sectorsize,
275189608Ssam		    blksize, offmask);
276189608Ssam	if (sectorsize < sizeof(struct fis_image_desc) ||
277189608Ssam	    (sectorsize % sizeof(struct fis_image_desc)))
278189608Ssam		return (NULL);
279189608Ssam	g_topology_unlock();
280189608Ssam	head = NULL;
281189608Ssam	offset = cp->provider->mediasize - blksize;
282189608Ssamagain:
283189608Ssam	buf = g_read_data(cp, offset, blksize, NULL);
284189608Ssam	if (buf != NULL)
285189608Ssam		head = parse_fis_directory(buf, blksize, offset, offmask);
286189608Ssam	if (head == NULL && offset != 0) {
287189608Ssam		if (buf != NULL)
288189608Ssam			g_free(buf);
289189608Ssam		offset = 0;			/* check the front */
290189608Ssam		goto again;
291189608Ssam	}
292189608Ssam	g_topology_lock();
293189608Ssam	if (head == NULL) {
294189608Ssam		if (buf != NULL)
295189608Ssam			g_free(buf);
296189608Ssam		return NULL;
297189608Ssam	}
298189608Ssam	/*
299189608Ssam	 * Craft a slice for each entry.
300189608Ssam	 */
301189608Ssam	for (fd = head, i = 0; fd != NULL; fd = fd->next) {
302189608Ssam		if (fd->name[0] == '\0')
303189608Ssam			continue;
304189608Ssam		error = g_slice_config(gp, i, G_SLICE_CONFIG_SET,
305189608Ssam		    fd->offset, fd->size, sectorsize, "redboot/%s", fd->name);
306189608Ssam		if (error)
307189608Ssam			printf("%s: g_slice_config returns %d for \"%s\"\n",
308189608Ssam			    __func__, error, fd->name);
309189608Ssam		sc->entry[i] = fd->entry;
310189608Ssam		sc->dsize[i] = fd->dsize;
311189660Ssam		/* disallow writing hard-to-recover entries */
312189660Ssam		sc->readonly[i] = (strcmp(fd->name, FISDIR_NAME) == 0) ||
313189660Ssam				  (strcmp(fd->name, REDBOOT_NAME) == 0);
314189608Ssam		i++;
315189608Ssam	}
316189608Ssam	g_free(buf);
317189608Ssam	g_access(cp, -1, 0, 0);
318189608Ssam	if (LIST_EMPTY(&gp->provider)) {
319189608Ssam		g_slice_spoiled(cp);
320189608Ssam		return (NULL);
321189608Ssam	}
322189608Ssam	return (gp);
323189608Ssam}
324189608Ssam
325189608Ssamstatic void
326189608Ssamg_redboot_config(struct gctl_req *req, struct g_class *mp, const char *verb)
327189608Ssam{
328189608Ssam	struct g_geom *gp;
329189608Ssam
330189608Ssam	g_topology_assert();
331189608Ssam	gp = gctl_get_geom(req, mp, "geom");
332189608Ssam	if (gp == NULL)
333189608Ssam		return;
334189608Ssam	gctl_error(req, "Unknown verb");
335189608Ssam}
336189608Ssam
337189608Ssamstatic struct g_class g_redboot_class	= {
338189608Ssam	.name		= REDBOOT_CLASS_NAME,
339189608Ssam	.version	= G_VERSION,
340189608Ssam	.taste		= g_redboot_taste,
341189608Ssam	.dumpconf	= g_redboot_dumpconf,
342189608Ssam	.ctlreq		= g_redboot_config,
343189608Ssam	.ioctl		= g_redboot_ioctl,
344189608Ssam};
345189608SsamDECLARE_GEOM_CLASS(g_redboot_class, g_redboot);
346332640SkevansMODULE_VERSION(geom_redboot, 0);
347