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