111891Speter/*- 211891Speter * Copyright (c) 2009 Sam Leffler, Errno Consulting 311891Speter * All rights reserved. 411891Speter * 550472Speter * Redistribution and use in source and binary forms, with or without 611891Speter * modification, are permitted provided that the following conditions 711891Speter * are met: 811891Speter * 1. Redistributions of source code must retain the above copyright 911891Speter * notice, this list of conditions and the following disclaimer, 1011891Speter * without modification. 1111891Speter * 2. Redistributions in binary form must reproduce at minimum a disclaimer 1211891Speter * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 1311891Speter * redistribution must be conditioned upon including a substantially 1411891Speter * similar Disclaimer requirement for further binary redistribution. 1511891Speter * 1611891Speter * NO WARRANTY 1711891Speter * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1811891Speter * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1911891Speter * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 2011891Speter * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 2111891Speter * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 2211891Speter * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2311891Speter * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2411891Speter * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 2511891Speter * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2611891Speter * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 2711891Speter * THE POSSIBILITY OF SUCH DAMAGES. 2811891Speter */ 2911891Speter 3011891Speter#include <sys/cdefs.h> 3111891Speter__FBSDID("$FreeBSD: releng/10.3/sys/geom/geom_redboot.c 203411 2010-02-03 01:12:19Z gonzo $"); 3211891Speter 3311891Speter#include <sys/param.h> 3411891Speter#include <sys/errno.h> 3511891Speter#include <sys/endian.h> 3611891Speter#include <sys/systm.h> 3711891Speter#include <sys/kernel.h> 3811891Speter#include <sys/fcntl.h> 3911891Speter#include <sys/malloc.h> 4011891Speter#include <sys/bio.h> 4111891Speter#include <sys/lock.h> 4211891Speter#include <sys/mutex.h> 4311891Speter 4411891Speter#include <sys/sbuf.h> 4511891Speter#include <geom/geom.h> 4611891Speter#include <geom/geom_slice.h> 4711891Speter 4811891Speter#define REDBOOT_CLASS_NAME "REDBOOT" 4911891Speter 5011891Speterstruct fis_image_desc { 5111891Speter uint8_t name[16]; /* null-terminated name */ 5211891Speter uint32_t offset; /* offset in flash */ 5311891Speter uint32_t addr; /* address in memory */ 5411891Speter uint32_t size; /* image size in bytes */ 5511891Speter uint32_t entry; /* offset in image for entry point */ 5611891Speter uint32_t dsize; /* data size in bytes */ 5711891Speter uint8_t pad[256-(16+7*sizeof(uint32_t)+sizeof(void*))]; 5811891Speter struct fis_image_desc *next; /* linked list (in memory) */ 5911891Speter uint32_t dsum; /* descriptor checksum */ 6011891Speter uint32_t fsum; /* checksum over image data */ 6111891Speter}; 6211891Speter 6311891Speter#define FISDIR_NAME "FIS directory" 6411891Speter#define REDBCFG_NAME "RedBoot config" 6511891Speter#define REDBOOT_NAME "RedBoot" 6611891Speter 6711891Speter#define REDBOOT_MAXSLICE 64 6811891Speter#define REDBOOT_MAXOFF \ 6911891Speter (REDBOOT_MAXSLICE*sizeof(struct fis_image_desc)) 7011891Speter 7111891Speterstruct g_redboot_softc { 7211891Speter uint32_t entry[REDBOOT_MAXSLICE]; 7311891Speter uint32_t dsize[REDBOOT_MAXSLICE]; 7411891Speter uint8_t readonly[REDBOOT_MAXSLICE]; 7511891Speter g_access_t *parent_access; 7611891Speter}; 7711891Speter 7811891Speterstatic void 7911891Speterg_redboot_print(int i, struct fis_image_desc *fd) 8011891Speter{ 8111891Speter 8211891Speter printf("[%2d] \"%-15.15s\" %08x:%08x", i, fd->name, 8311891Speter fd->offset, fd->size); 8411891Speter printf(" addr %08x entry %08x\n", fd->addr, fd->entry); 8511891Speter printf(" dsize 0x%x dsum 0x%x fsum 0x%x\n", fd->dsize, 8611891Speter fd->dsum, fd->fsum); 8711891Speter} 8811891Speter 8911891Speterstatic int 9011891Speterg_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; i++, fd++) { 199 if (fd->name[0] == 0xff) 200 continue; 201 if (match(fd->name, FISDIR_NAME)) 202 fisdir = fd; 203 else if (match(fd->name, REDBCFG_NAME)) 204 redbcfg = fd; 205 if (nameok(fd->name)) { 206 /* 207 * NB: flash address includes platform mapping; 208 * strip it so we have only a flash offset. 209 */ 210 fd->offset &= offmask; 211 if (bootverbose) 212 g_redboot_print(i, fd); 213 *tail = fd; 214 *(tail = &fd->next) = NULL; 215 } 216 } 217 if (fisdir == NULL) { 218 if (bootverbose) 219 printf("No RedBoot FIS table located at %lu\n", 220 (long) offset); 221 return (NULL); 222 } 223 if (redbcfg != NULL && 224 fisdir->offset + fisdir->size == redbcfg->offset) { 225 /* 226 * Merged FIS/RedBoot config directory. 227 */ 228 if (bootverbose) 229 printf("FIS/RedBoot merged at 0x%jx (not yet)\n", 230 offset + fisdir->offset); 231 /* XXX */ 232 } 233 return head; 234#undef match 235} 236 237static struct g_geom * 238g_redboot_taste(struct g_class *mp, struct g_provider *pp, int insist) 239{ 240 struct g_geom *gp; 241 struct g_consumer *cp; 242 struct g_redboot_softc *sc; 243 int error, sectorsize, i; 244 struct fis_image_desc *fd, *head; 245 uint32_t offmask; 246 u_int blksize; /* NB: flash block size stored as stripesize */ 247 u_char *buf; 248 off_t offset; 249 250 g_trace(G_T_TOPOLOGY, "redboot_taste(%s,%s)", mp->name, pp->name); 251 g_topology_assert(); 252 if (!strcmp(pp->geom->class->name, REDBOOT_CLASS_NAME)) 253 return (NULL); 254 /* XXX only taste flash providers */ 255 if (strncmp(pp->name, "cfi", 3) && 256 strncmp(pp->name, "flash/spi", 9)) 257 return (NULL); 258 gp = g_slice_new(mp, REDBOOT_MAXSLICE, pp, &cp, &sc, sizeof(*sc), 259 g_redboot_start); 260 if (gp == NULL) 261 return (NULL); 262 /* interpose our access method */ 263 sc->parent_access = gp->access; 264 gp->access = g_redboot_access; 265 266 sectorsize = cp->provider->sectorsize; 267 blksize = cp->provider->stripesize; 268 if (powerof2(cp->provider->mediasize)) 269 offmask = cp->provider->mediasize-1; 270 else 271 offmask = 0xffffffff; /* XXX */ 272 if (bootverbose) 273 printf("%s: mediasize %ld secsize %d blksize %d offmask 0x%x\n", 274 __func__, (long) cp->provider->mediasize, sectorsize, 275 blksize, offmask); 276 if (sectorsize < sizeof(struct fis_image_desc) || 277 (sectorsize % sizeof(struct fis_image_desc))) 278 return (NULL); 279 g_topology_unlock(); 280 head = NULL; 281 offset = cp->provider->mediasize - blksize; 282again: 283 buf = g_read_data(cp, offset, blksize, NULL); 284 if (buf != NULL) 285 head = parse_fis_directory(buf, blksize, offset, offmask); 286 if (head == NULL && offset != 0) { 287 if (buf != NULL) 288 g_free(buf); 289 offset = 0; /* check the front */ 290 goto again; 291 } 292 g_topology_lock(); 293 if (head == NULL) { 294 if (buf != NULL) 295 g_free(buf); 296 return NULL; 297 } 298 /* 299 * Craft a slice for each entry. 300 */ 301 for (fd = head, i = 0; fd != NULL; fd = fd->next) { 302 if (fd->name[0] == '\0') 303 continue; 304 error = g_slice_config(gp, i, G_SLICE_CONFIG_SET, 305 fd->offset, fd->size, sectorsize, "redboot/%s", fd->name); 306 if (error) 307 printf("%s: g_slice_config returns %d for \"%s\"\n", 308 __func__, error, fd->name); 309 sc->entry[i] = fd->entry; 310 sc->dsize[i] = fd->dsize; 311 /* disallow writing hard-to-recover entries */ 312 sc->readonly[i] = (strcmp(fd->name, FISDIR_NAME) == 0) || 313 (strcmp(fd->name, REDBOOT_NAME) == 0); 314 i++; 315 } 316 g_free(buf); 317 g_access(cp, -1, 0, 0); 318 if (LIST_EMPTY(&gp->provider)) { 319 g_slice_spoiled(cp); 320 return (NULL); 321 } 322 return (gp); 323} 324 325static void 326g_redboot_config(struct gctl_req *req, struct g_class *mp, const char *verb) 327{ 328 struct g_geom *gp; 329 330 g_topology_assert(); 331 gp = gctl_get_geom(req, mp, "geom"); 332 if (gp == NULL) 333 return; 334 gctl_error(req, "Unknown verb"); 335} 336 337static struct g_class g_redboot_class = { 338 .name = REDBOOT_CLASS_NAME, 339 .version = G_VERSION, 340 .taste = g_redboot_taste, 341 .dumpconf = g_redboot_dumpconf, 342 .ctlreq = g_redboot_config, 343 .ioctl = g_redboot_ioctl, 344}; 345DECLARE_GEOM_CLASS(g_redboot_class, g_redboot); 346