]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_redboot.c
MFV r323678: file 5.32
[FreeBSD/FreeBSD.git] / sys / geom / geom_redboot.c
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$");
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 #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
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
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
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
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
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
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 *
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 *
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         u_int 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 %d offmask 0x%x\n",
285                     __func__, (long) cp->provider->mediasize, sectorsize,
286                     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                 if (buf != NULL)
300                         g_free(buf);
301                 offset = 0;                     /* check the front */
302                 goto again;
303         }
304         g_topology_lock();
305         if (head == NULL) {
306                 if (buf != NULL)
307                         g_free(buf);
308                 return NULL;
309         }
310         /*
311          * Craft a slice for each entry.
312          */
313         for (fd = head, i = 0; fd != NULL; fd = fd->next) {
314                 if (fd->name[0] == '\0')
315                         continue;
316                 error = g_slice_config(gp, i, G_SLICE_CONFIG_SET,
317                     fd->offset, fd->size, sectorsize, "redboot/%s", fd->name);
318                 if (error)
319                         printf("%s: g_slice_config returns %d for \"%s\"\n",
320                             __func__, error, fd->name);
321                 sc->entry[i] = fd->entry;
322                 sc->dsize[i] = fd->dsize;
323                 /* disallow writing hard-to-recover entries */
324                 sc->readonly[i] = (strcmp(fd->name, FISDIR_NAME) == 0) ||
325                                   (strcmp(fd->name, REDBOOT_NAME) == 0);
326                 i++;
327         }
328         g_free(buf);
329         g_access(cp, -1, 0, 0);
330         if (LIST_EMPTY(&gp->provider)) {
331                 g_slice_spoiled(cp);
332                 return (NULL);
333         }
334         return (gp);
335 }
336
337 static void
338 g_redboot_config(struct gctl_req *req, struct g_class *mp, const char *verb)
339 {
340         struct g_geom *gp;
341
342         g_topology_assert();
343         gp = gctl_get_geom(req, mp, "geom");
344         if (gp == NULL)
345                 return;
346         gctl_error(req, "Unknown verb");
347 }
348
349 static struct g_class g_redboot_class   = {
350         .name           = REDBOOT_CLASS_NAME,
351         .version        = G_VERSION,
352         .taste          = g_redboot_taste,
353         .dumpconf       = g_redboot_dumpconf,
354         .ctlreq         = g_redboot_config,
355         .ioctl          = g_redboot_ioctl,
356 };
357 DECLARE_GEOM_CLASS(g_redboot_class, g_redboot);