The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/geom/geom_redboot.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    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: releng/8.3/sys/geom/geom_redboot.c 199583 2009-11-20 15:27:52Z jhb $");
   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 
   50 struct 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 
   71 struct 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 
   78 static void
   79 g_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 
   89 static int
   90 g_redboot_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
   91 {
   92         return (ENOIOCTL);
   93 }
   94 
   95 static int
   96 g_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 
  107 static int
  108 g_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 
  133 static void
  134 g_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 
  158 static int
  159 nameok(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 
  170 static struct fis_image_desc *
  171 parse_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 
  237 static struct g_geom *
  238 g_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                 return (NULL);
  257         gp = g_slice_new(mp, REDBOOT_MAXSLICE, pp, &cp, &sc, sizeof(*sc),
  258             g_redboot_start);
  259         if (gp == NULL)
  260                 return (NULL);
  261         /* interpose our access method */
  262         sc->parent_access = gp->access;
  263         gp->access = g_redboot_access;
  264 
  265         sectorsize = cp->provider->sectorsize;
  266         blksize = cp->provider->stripesize;
  267         if (powerof2(cp->provider->mediasize))
  268                 offmask = cp->provider->mediasize-1;
  269         else
  270                 offmask = 0xffffffff;           /* XXX */
  271         if (bootverbose)
  272                 printf("%s: mediasize %ld secsize %d blksize %d offmask 0x%x\n",
  273                     __func__, (long) cp->provider->mediasize, sectorsize,
  274                     blksize, offmask);
  275         if (sectorsize < sizeof(struct fis_image_desc) ||
  276             (sectorsize % sizeof(struct fis_image_desc)))
  277                 return (NULL);
  278         g_topology_unlock();
  279         head = NULL;
  280         offset = cp->provider->mediasize - blksize;
  281 again:
  282         buf = g_read_data(cp, offset, blksize, NULL);
  283         if (buf != NULL)
  284                 head = parse_fis_directory(buf, blksize, offset, offmask);
  285         if (head == NULL && offset != 0) {
  286                 if (buf != NULL)
  287                         g_free(buf);
  288                 offset = 0;                     /* check the front */
  289                 goto again;
  290         }
  291         g_topology_lock();
  292         if (head == NULL) {
  293                 if (buf != NULL)
  294                         g_free(buf);
  295                 return NULL;
  296         }
  297         /*
  298          * Craft a slice for each entry.
  299          */
  300         for (fd = head, i = 0; fd != NULL; fd = fd->next) {
  301                 if (fd->name[0] == '\0')
  302                         continue;
  303                 error = g_slice_config(gp, i, G_SLICE_CONFIG_SET,
  304                     fd->offset, fd->size, sectorsize, "redboot/%s", fd->name);
  305                 if (error)
  306                         printf("%s: g_slice_config returns %d for \"%s\"\n",
  307                             __func__, error, fd->name);
  308                 sc->entry[i] = fd->entry;
  309                 sc->dsize[i] = fd->dsize;
  310                 /* disallow writing hard-to-recover entries */
  311                 sc->readonly[i] = (strcmp(fd->name, FISDIR_NAME) == 0) ||
  312                                   (strcmp(fd->name, REDBOOT_NAME) == 0);
  313                 i++;
  314         }
  315         g_free(buf);
  316         g_access(cp, -1, 0, 0);
  317         if (LIST_EMPTY(&gp->provider)) {
  318                 g_slice_spoiled(cp);
  319                 return (NULL);
  320         }
  321         return (gp);
  322 }
  323 
  324 static void
  325 g_redboot_config(struct gctl_req *req, struct g_class *mp, const char *verb)
  326 {
  327         struct g_geom *gp;
  328 
  329         g_topology_assert();
  330         gp = gctl_get_geom(req, mp, "geom");
  331         if (gp == NULL)
  332                 return;
  333         gctl_error(req, "Unknown verb");
  334 }
  335 
  336 static struct g_class g_redboot_class   = {
  337         .name           = REDBOOT_CLASS_NAME,
  338         .version        = G_VERSION,
  339         .taste          = g_redboot_taste,
  340         .dumpconf       = g_redboot_dumpconf,
  341         .ctlreq         = g_redboot_config,
  342         .ioctl          = g_redboot_ioctl,
  343 };
  344 DECLARE_GEOM_CLASS(g_redboot_class, g_redboot);

Cache object: 20352a3a812c74505db4ac5d619ea456


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.