geom_redboot.c revision 189660
1/*-
2 * Copyright (c) 2009 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 *    redistribution must be conditioned upon including a substantially
14 *    similar Disclaimer requirement for further binary redistribution.
15 *
16 * NO WARRANTY
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/geom/geom_redboot.c 189660 2009-03-11 01:12:52Z sam $");
32
33#include <sys/param.h>
34#include <sys/errno.h>
35#include <sys/endian.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/fcntl.h>
39#include <sys/malloc.h>
40#include <sys/bio.h>
41#include <sys/lock.h>
42#include <sys/mutex.h>
43
44#include <sys/sbuf.h>
45#include <geom/geom.h>
46#include <geom/geom_slice.h>
47
48#define REDBOOT_CLASS_NAME "REDBOOT"
49
50struct fis_image_desc {
51	uint8_t		name[16];	/* null-terminated name */
52	uint32_t	offset;		/* offset in flash */
53	uint32_t	addr;		/* address in memory */
54	uint32_t	size;		/* image size in bytes */
55	uint32_t	entry;		/* offset in image for entry point */
56	uint32_t	dsize;		/* data size in bytes */
57	uint8_t		pad[256-(16+7*sizeof(uint32_t)+sizeof(void*))];
58	struct fis_image_desc *next;	/* linked list (in memory) */
59	uint32_t	dsum;		/* descriptor checksum */
60	uint32_t	fsum;		/* checksum over image data */
61};
62
63#define	FISDIR_NAME	"FIS directory"
64#define	REDBCFG_NAME	"RedBoot config"
65#define	REDBOOT_NAME	"RedBoot"
66
67#define	REDBOOT_MAXSLICE	64
68#define	REDBOOT_MAXOFF \
69	(REDBOOT_MAXSLICE*sizeof(struct fis_image_desc))
70
71struct g_redboot_softc {
72	uint32_t	entry[REDBOOT_MAXSLICE];
73	uint32_t	dsize[REDBOOT_MAXSLICE];
74	uint8_t		readonly[REDBOOT_MAXSLICE];
75	g_access_t	*parent_access;
76};
77
78static void
79g_redboot_print(int i, struct fis_image_desc *fd)
80{
81
82	printf("[%2d] \"%-15.15s\" %08x:%08x", i, fd->name,
83	    fd->offset, fd->size);
84	printf(" addr %08x entry %08x\n", fd->addr, fd->entry);
85	printf("     dsize 0x%x dsum 0x%x fsum 0x%x\n", fd->dsize,
86	    fd->dsum, fd->fsum);
87}
88
89static int
90g_redboot_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
91{
92	return (ENOIOCTL);
93}
94
95static int
96g_redboot_access(struct g_provider *pp, int dread, int dwrite, int dexcl)
97{
98	struct g_geom *gp = pp->geom;
99	struct g_slicer *gsp = gp->softc;
100	struct g_redboot_softc *sc = gsp->softc;
101
102	if (dwrite > 0 && sc->readonly[pp->index])
103		return (EPERM);
104	return (sc->parent_access(pp, dread, dwrite, dexcl));
105}
106
107static int
108g_redboot_start(struct bio *bp)
109{
110	struct g_provider *pp;
111	struct g_geom *gp;
112	struct g_redboot_softc *sc;
113	struct g_slicer *gsp;
114	int idx;
115
116	pp = bp->bio_to;
117	idx = pp->index;
118	gp = pp->geom;
119	gsp = gp->softc;
120	sc = gsp->softc;
121	if (bp->bio_cmd == BIO_GETATTR) {
122		if (g_handleattr_int(bp, REDBOOT_CLASS_NAME "::entry",
123		    sc->entry[idx]))
124			return (1);
125		if (g_handleattr_int(bp, REDBOOT_CLASS_NAME "::dsize",
126		    sc->dsize[idx]))
127			return (1);
128	}
129
130	return (0);
131}
132
133static void
134g_redboot_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
135	struct g_consumer *cp __unused, struct g_provider *pp)
136{
137	struct g_redboot_softc *sc;
138	struct g_slicer *gsp;
139
140	gsp = gp->softc;
141	sc = gsp->softc;
142	g_slice_dumpconf(sb, indent, gp, cp, pp);
143	if (pp != NULL) {
144		if (indent == NULL) {
145			sbuf_printf(sb, " entry %d", sc->entry[pp->index]);
146			sbuf_printf(sb, " dsize %d", sc->dsize[pp->index]);
147		} else {
148			sbuf_printf(sb, "%s<entry>%d</entry>\n", indent,
149			    sc->entry[pp->index]);
150			sbuf_printf(sb, "%s<dsize>%d</dsize>\n", indent,
151			    sc->dsize[pp->index]);
152		}
153	}
154}
155
156#include <sys/ctype.h>
157
158static int
159nameok(const char name[16])
160{
161	int i;
162
163	/* descriptor names are null-terminated printable ascii */
164	for (i = 0; i < 15; i++)
165		if (!isprint(name[i]))
166			break;
167	return (name[i] == '\0');
168}
169
170static struct fis_image_desc *
171parse_fis_directory(u_char *buf, size_t bufsize, off_t offset, uint32_t offmask)
172{
173#define	match(a,b)	(bcmp(a, b, sizeof(b)-1) == 0)
174	struct fis_image_desc *fd, *efd;
175	struct fis_image_desc *fisdir, *redbcfg;
176	struct fis_image_desc *head, **tail;
177	int i;
178
179	fd = (struct fis_image_desc *)buf;
180	efd = fd + (bufsize / sizeof(struct fis_image_desc));
181#if 0
182	/*
183	 * Find the start of the FIS table.
184	 */
185	while (fd < efd && fd->name[0] != 0xff)
186		fd++;
187	if (fd == efd)
188		return (NULL);
189	if (bootverbose)
190		printf("RedBoot FIS table starts at 0x%jx\n",
191		    offset + fd - (struct fis_image_desc *) buf);
192#endif
193	/*
194	 * Scan forward collecting entries in a list.
195	 */
196	fisdir = redbcfg = NULL;
197	*(tail = &head) = NULL;
198	for (i = 0; fd < efd && fd->name[0] != 0xff; i++, fd++) {
199		if (match(fd->name, FISDIR_NAME))
200			fisdir = fd;
201		else if (match(fd->name, REDBCFG_NAME))
202			redbcfg = fd;
203		if (nameok(fd->name)) {
204			/*
205			 * NB: flash address includes platform mapping;
206			 *     strip it so we have only a flash offset.
207			 */
208			fd->offset &= offmask;
209			if (bootverbose)
210				g_redboot_print(i, fd);
211			*tail = fd;
212			*(tail = &fd->next) = NULL;
213		}
214	}
215	if (fisdir == NULL) {
216		if (bootverbose)
217			printf("No RedBoot FIS table located at %lu\n",
218			    (long) offset);
219		return (NULL);
220	}
221	if (redbcfg != NULL &&
222	    fisdir->offset + fisdir->size == redbcfg->offset) {
223		/*
224		 * Merged FIS/RedBoot config directory.
225		 */
226		if (bootverbose)
227			printf("FIS/RedBoot merged at 0x%jx (not yet)\n",
228			    offset + fisdir->offset);
229		/* XXX */
230	}
231	return head;
232#undef match
233}
234
235static struct g_geom *
236g_redboot_taste(struct g_class *mp, struct g_provider *pp, int insist)
237{
238	struct g_geom *gp;
239	struct g_consumer *cp;
240	struct g_redboot_softc *sc;
241	int error, sectorsize, i;
242	struct fis_image_desc *fd, *head;
243	uint32_t offmask;
244	u_int blksize;		/* NB: flash block size stored as stripesize */
245	u_char *buf;
246	off_t offset;
247
248	g_trace(G_T_TOPOLOGY, "redboot_taste(%s,%s)", mp->name, pp->name);
249	g_topology_assert();
250	if (!strcmp(pp->geom->class->name, REDBOOT_CLASS_NAME))
251		return (NULL);
252	/* XXX only taste flash providers */
253	if (strncmp(pp->name, "cfi", 3))
254		return (NULL);
255	gp = g_slice_new(mp, REDBOOT_MAXSLICE, pp, &cp, &sc, sizeof(*sc),
256	    g_redboot_start);
257	if (gp == NULL)
258		return (NULL);
259	/* interpose our access method */
260	sc->parent_access = gp->access;
261	gp->access = g_redboot_access;
262
263	sectorsize = cp->provider->sectorsize;
264	blksize = cp->provider->stripesize;
265	if (powerof2(cp->provider->mediasize))
266		offmask = cp->provider->mediasize-1;
267	else
268		offmask = 0xffffffff;		/* XXX */
269	if (bootverbose)
270		printf("%s: mediasize %ld secsize %d blksize %d offmask 0x%x\n",
271		    __func__, (long) cp->provider->mediasize, sectorsize,
272		    blksize, offmask);
273	if (sectorsize < sizeof(struct fis_image_desc) ||
274	    (sectorsize % sizeof(struct fis_image_desc)))
275		return (NULL);
276	g_topology_unlock();
277	head = NULL;
278	offset = cp->provider->mediasize - blksize;
279again:
280	buf = g_read_data(cp, offset, blksize, NULL);
281	if (buf != NULL)
282		head = parse_fis_directory(buf, blksize, offset, offmask);
283	if (head == NULL && offset != 0) {
284		if (buf != NULL)
285			g_free(buf);
286		offset = 0;			/* check the front */
287		goto again;
288	}
289	g_topology_lock();
290	if (head == NULL) {
291		if (buf != NULL)
292			g_free(buf);
293		return NULL;
294	}
295	/*
296	 * Craft a slice for each entry.
297	 */
298	for (fd = head, i = 0; fd != NULL; fd = fd->next) {
299		if (fd->name[0] == '\0')
300			continue;
301		error = g_slice_config(gp, i, G_SLICE_CONFIG_SET,
302		    fd->offset, fd->size, sectorsize, "redboot/%s", fd->name);
303		if (error)
304			printf("%s: g_slice_config returns %d for \"%s\"\n",
305			    __func__, error, fd->name);
306		sc->entry[i] = fd->entry;
307		sc->dsize[i] = fd->dsize;
308		/* disallow writing hard-to-recover entries */
309		sc->readonly[i] = (strcmp(fd->name, FISDIR_NAME) == 0) ||
310				  (strcmp(fd->name, REDBOOT_NAME) == 0);
311		i++;
312	}
313	g_free(buf);
314	g_access(cp, -1, 0, 0);
315	if (LIST_EMPTY(&gp->provider)) {
316		g_slice_spoiled(cp);
317		return (NULL);
318	}
319	return (gp);
320}
321
322static void
323g_redboot_config(struct gctl_req *req, struct g_class *mp, const char *verb)
324{
325	struct g_geom *gp;
326
327	g_topology_assert();
328	gp = gctl_get_geom(req, mp, "geom");
329	if (gp == NULL)
330		return;
331	gctl_error(req, "Unknown verb");
332}
333
334static struct g_class g_redboot_class	= {
335	.name		= REDBOOT_CLASS_NAME,
336	.version	= G_VERSION,
337	.taste		= g_redboot_taste,
338	.dumpconf	= g_redboot_dumpconf,
339	.ctlreq		= g_redboot_config,
340	.ioctl		= g_redboot_ioctl,
341};
342DECLARE_GEOM_CLASS(g_redboot_class, g_redboot);
343