geom_redboot.c revision 189608
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: head/sys/geom/geom_redboot.c 189608 2009-03-09 23:18:36Z sam $");
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"
65189608Ssam
66189608Ssam#define	REDBOOT_MAXSLICE	64
67189608Ssam#define	REDBOOT_MAXOFF \
68189608Ssam	(REDBOOT_MAXSLICE*sizeof(struct fis_image_desc))
69189608Ssam
70189608Ssamstruct g_redboot_softc {
71189608Ssam	uint32_t	entry[REDBOOT_MAXSLICE];
72189608Ssam	uint32_t	dsize[REDBOOT_MAXSLICE];
73189608Ssam};
74189608Ssam
75189608Ssamstatic void
76189608Ssamg_redboot_print(int i, struct fis_image_desc *fd)
77189608Ssam{
78189608Ssam
79189608Ssam	printf("[%2d] \"%-15.15s\" %08x:%08x", i, fd->name,
80189608Ssam	    fd->offset, fd->size);
81189608Ssam	printf(" addr %08x entry %08x\n", fd->addr, fd->entry);
82189608Ssam	printf("     dsize 0x%x dsum 0x%x fsum 0x%x\n", fd->dsize,
83189608Ssam	    fd->dsum, fd->fsum);
84189608Ssam}
85189608Ssam
86189608Ssamstatic int
87189608Ssamg_redboot_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
88189608Ssam{
89189608Ssam	return (ENOIOCTL);
90189608Ssam}
91189608Ssam
92189608Ssamstatic int
93189608Ssamg_redboot_start(struct bio *bp)
94189608Ssam{
95189608Ssam	struct g_provider *pp;
96189608Ssam	struct g_geom *gp;
97189608Ssam	struct g_redboot_softc *sc;
98189608Ssam	struct g_slicer *gsp;
99189608Ssam	int idx;
100189608Ssam
101189608Ssam	pp = bp->bio_to;
102189608Ssam	idx = pp->index;
103189608Ssam	gp = pp->geom;
104189608Ssam	gsp = gp->softc;
105189608Ssam	sc = gsp->softc;
106189608Ssam	if (bp->bio_cmd == BIO_GETATTR) {
107189608Ssam		if (g_handleattr_int(bp, REDBOOT_CLASS_NAME "::entry",
108189608Ssam		    sc->entry[idx]))
109189608Ssam			return (1);
110189608Ssam		if (g_handleattr_int(bp, REDBOOT_CLASS_NAME "::dsize",
111189608Ssam		    sc->dsize[idx]))
112189608Ssam			return (1);
113189608Ssam	}
114189608Ssam
115189608Ssam	return (0);
116189608Ssam}
117189608Ssam
118189608Ssamstatic void
119189608Ssamg_redboot_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
120189608Ssam	struct g_consumer *cp __unused, struct g_provider *pp)
121189608Ssam{
122189608Ssam	struct g_redboot_softc *sc;
123189608Ssam	struct g_slicer *gsp;
124189608Ssam
125189608Ssam	gsp = gp->softc;
126189608Ssam	sc = gsp->softc;
127189608Ssam	g_slice_dumpconf(sb, indent, gp, cp, pp);
128189608Ssam	if (pp != NULL) {
129189608Ssam		if (indent == NULL) {
130189608Ssam			sbuf_printf(sb, " entry %d", sc->entry[pp->index]);
131189608Ssam			sbuf_printf(sb, " dsize %d", sc->dsize[pp->index]);
132189608Ssam		} else {
133189608Ssam			sbuf_printf(sb, "%s<entry>%d</entry>\n", indent,
134189608Ssam			    sc->entry[pp->index]);
135189608Ssam			sbuf_printf(sb, "%s<dsize>%d</dsize>\n", indent,
136189608Ssam			    sc->dsize[pp->index]);
137189608Ssam		}
138189608Ssam	}
139189608Ssam}
140189608Ssam
141189608Ssam#include <sys/ctype.h>
142189608Ssam
143189608Ssamstatic int
144189608Ssamnameok(const char name[16])
145189608Ssam{
146189608Ssam	int i;
147189608Ssam
148189608Ssam	/* descriptor names are null-terminated printable ascii */
149189608Ssam	for (i = 0; i < 15; i++)
150189608Ssam		if (!isprint(name[i]))
151189608Ssam			break;
152189608Ssam	return (name[i] == '\0');
153189608Ssam}
154189608Ssam
155189608Ssamstatic struct fis_image_desc *
156189608Ssamparse_fis_directory(u_char *buf, size_t bufsize, off_t offset, uint32_t offmask)
157189608Ssam{
158189608Ssam#define	match(a,b) \
159189608Ssam	(bcmp(fd->name, FISDIR_NAME, sizeof(FISDIR_NAME)-1) == 0)
160189608Ssam	struct fis_image_desc *fd, *efd;
161189608Ssam	struct fis_image_desc *fisdir, *redbcfg;
162189608Ssam	struct fis_image_desc *head, **tail;
163189608Ssam	int i;
164189608Ssam
165189608Ssam	fd = (struct fis_image_desc *)buf;
166189608Ssam	efd = fd + (bufsize / sizeof(struct fis_image_desc));
167189608Ssam#if 0
168189608Ssam	/*
169189608Ssam	 * Find the start of the FIS table.
170189608Ssam	 */
171189608Ssam	while (fd < efd && fd->name[0] != 0xff)
172189608Ssam		fd++;
173189608Ssam	if (fd == efd)
174189608Ssam		return (NULL);
175189608Ssam	if (bootverbose)
176189608Ssam		printf("RedBoot FIS table starts at 0x%jx\n",
177189608Ssam		    offset + fd - (struct fis_image_desc *) buf);
178189608Ssam#endif
179189608Ssam	/*
180189608Ssam	 * Scan forward collecting entries in a list.
181189608Ssam	 */
182189608Ssam	fisdir = redbcfg = NULL;
183189608Ssam	*(tail = &head) = NULL;
184189608Ssam	for (i = 0; fd < efd && fd->name[0] != 0xff; i++, fd++) {
185189608Ssam		if (match(fd->name, FISDIR_NAME))
186189608Ssam			fisdir = fd;
187189608Ssam		else if (match(fd->name, REDBCFG_NAME))
188189608Ssam			redbcfg = fd;
189189608Ssam		if (nameok(fd->name)) {
190189608Ssam			/*
191189608Ssam			 * NB: flash address includes platform mapping;
192189608Ssam			 *     strip it so we have only a flash offset.
193189608Ssam			 */
194189608Ssam			fd->offset &= offmask;
195189608Ssam			if (bootverbose)
196189608Ssam				g_redboot_print(i, fd);
197189608Ssam			*tail = fd;
198189608Ssam			*(tail = &fd->next) = NULL;
199189608Ssam		}
200189608Ssam	}
201189608Ssam	if (fisdir == NULL) {
202189608Ssam		if (bootverbose)
203189608Ssam			printf("No RedBoot FIS table located at %lu\n",
204189608Ssam			    (long) offset);
205189608Ssam		return (NULL);
206189608Ssam	}
207189608Ssam	if (redbcfg != NULL &&
208189608Ssam	    fisdir->offset + fisdir->size == redbcfg->offset) {
209189608Ssam		/*
210189608Ssam		 * Merged FIS/RedBoot config directory.
211189608Ssam		 */
212189608Ssam		if (bootverbose)
213189608Ssam			printf("FIS/RedBoot merged at 0x%jx (not yet)\n",
214189608Ssam			    offset + fisdir->offset);
215189608Ssam		/* XXX */
216189608Ssam	}
217189608Ssam	return head;
218189608Ssam#undef match
219189608Ssam}
220189608Ssam
221189608Ssamstatic struct g_geom *
222189608Ssamg_redboot_taste(struct g_class *mp, struct g_provider *pp, int insist)
223189608Ssam{
224189608Ssam	struct g_geom *gp;
225189608Ssam	struct g_consumer *cp;
226189608Ssam	struct g_redboot_softc *sc;
227189608Ssam	int error, sectorsize, i;
228189608Ssam	struct fis_image_desc *fd, *head;
229189608Ssam	uint32_t offmask;
230189608Ssam	u_int blksize;		/* NB: flash block size stored as stripesize */
231189608Ssam	u_char *buf;
232189608Ssam	off_t offset;
233189608Ssam
234189608Ssam	g_trace(G_T_TOPOLOGY, "redboot_taste(%s,%s)", mp->name, pp->name);
235189608Ssam	g_topology_assert();
236189608Ssam	if (!strcmp(pp->geom->class->name, REDBOOT_CLASS_NAME))
237189608Ssam		return (NULL);
238189608Ssam	/* XXX only taste flash providers */
239189608Ssam	if (strncmp(pp->name, "cfi", 3))
240189608Ssam		return (NULL);
241189608Ssam	gp = g_slice_new(mp, REDBOOT_MAXSLICE, pp, &cp, &sc, sizeof(*sc),
242189608Ssam	    g_redboot_start);
243189608Ssam	if (gp == NULL)
244189608Ssam		return (NULL);
245189608Ssam	sectorsize = cp->provider->sectorsize;
246189608Ssam	blksize = cp->provider->stripesize;
247189608Ssam	if (powerof2(cp->provider->mediasize))
248189608Ssam		offmask = cp->provider->mediasize-1;
249189608Ssam	else
250189608Ssam		offmask = 0xffffffff;		/* XXX */
251189608Ssam	if (bootverbose)
252189608Ssam		printf("%s: mediasize %ld secsize %d blksize %d offmask 0x%x\n",
253189608Ssam		    __func__, (long) cp->provider->mediasize, sectorsize,
254189608Ssam		    blksize, offmask);
255189608Ssam	if (sectorsize < sizeof(struct fis_image_desc) ||
256189608Ssam	    (sectorsize % sizeof(struct fis_image_desc)))
257189608Ssam		return (NULL);
258189608Ssam	g_topology_unlock();
259189608Ssam	head = NULL;
260189608Ssam	offset = cp->provider->mediasize - blksize;
261189608Ssamagain:
262189608Ssam	buf = g_read_data(cp, offset, blksize, NULL);
263189608Ssam	if (buf != NULL)
264189608Ssam		head = parse_fis_directory(buf, blksize, offset, offmask);
265189608Ssam	if (head == NULL && offset != 0) {
266189608Ssam		if (buf != NULL)
267189608Ssam			g_free(buf);
268189608Ssam		offset = 0;			/* check the front */
269189608Ssam		goto again;
270189608Ssam	}
271189608Ssam	g_topology_lock();
272189608Ssam	if (head == NULL) {
273189608Ssam		if (buf != NULL)
274189608Ssam			g_free(buf);
275189608Ssam		return NULL;
276189608Ssam	}
277189608Ssam	/*
278189608Ssam	 * Craft a slice for each entry.
279189608Ssam	 */
280189608Ssam	for (fd = head, i = 0; fd != NULL; fd = fd->next) {
281189608Ssam		if (fd->name[0] == '\0')
282189608Ssam			continue;
283189608Ssam		error = g_slice_config(gp, i, G_SLICE_CONFIG_SET,
284189608Ssam		    fd->offset, fd->size, sectorsize, "redboot/%s", fd->name);
285189608Ssam		if (error)
286189608Ssam			printf("%s: g_slice_config returns %d for \"%s\"\n",
287189608Ssam			    __func__, error, fd->name);
288189608Ssam		sc->entry[i] = fd->entry;
289189608Ssam		sc->dsize[i] = fd->dsize;
290189608Ssam		i++;
291189608Ssam	}
292189608Ssam	g_free(buf);
293189608Ssam	g_access(cp, -1, 0, 0);
294189608Ssam	if (LIST_EMPTY(&gp->provider)) {
295189608Ssam		g_slice_spoiled(cp);
296189608Ssam		return (NULL);
297189608Ssam	}
298189608Ssam	return (gp);
299189608Ssam}
300189608Ssam
301189608Ssamstatic void
302189608Ssamg_redboot_config(struct gctl_req *req, struct g_class *mp, const char *verb)
303189608Ssam{
304189608Ssam	struct g_geom *gp;
305189608Ssam
306189608Ssam	g_topology_assert();
307189608Ssam	gp = gctl_get_geom(req, mp, "geom");
308189608Ssam	if (gp == NULL)
309189608Ssam		return;
310189608Ssam	gctl_error(req, "Unknown verb");
311189608Ssam}
312189608Ssam
313189608Ssamstatic struct g_class g_redboot_class	= {
314189608Ssam	.name		= REDBOOT_CLASS_NAME,
315189608Ssam	.version	= G_VERSION,
316189608Ssam	.taste		= g_redboot_taste,
317189608Ssam	.dumpconf	= g_redboot_dumpconf,
318189608Ssam	.ctlreq		= g_redboot_config,
319189608Ssam	.ioctl		= g_redboot_ioctl,
320189608Ssam};
321189608SsamDECLARE_GEOM_CLASS(g_redboot_class, g_redboot);
322