diff --git a/sys/geom/geom_redboot.c b/sys/geom/geom_redboot.c index 8ab89203d228..0314fe7c31aa 100644 --- a/sys/geom/geom_redboot.c +++ b/sys/geom/geom_redboot.c @@ -62,6 +62,7 @@ struct fis_image_desc { #define FISDIR_NAME "FIS directory" #define REDBCFG_NAME "RedBoot config" +#define REDBOOT_NAME "RedBoot" #define REDBOOT_MAXSLICE 64 #define REDBOOT_MAXOFF \ @@ -70,6 +71,8 @@ struct fis_image_desc { struct g_redboot_softc { uint32_t entry[REDBOOT_MAXSLICE]; uint32_t dsize[REDBOOT_MAXSLICE]; + uint8_t readonly[REDBOOT_MAXSLICE]; + g_access_t *parent_access; }; static void @@ -89,6 +92,18 @@ g_redboot_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct return (ENOIOCTL); } +static int +g_redboot_access(struct g_provider *pp, int dread, int dwrite, int dexcl) +{ + struct g_geom *gp = pp->geom; + struct g_slicer *gsp = gp->softc; + struct g_redboot_softc *sc = gsp->softc; + + if (dwrite > 0 && sc->readonly[pp->index]) + return (EPERM); + return (sc->parent_access(pp, dread, dwrite, dexcl)); +} + static int g_redboot_start(struct bio *bp) { @@ -155,8 +170,7 @@ nameok(const char name[16]) static struct fis_image_desc * parse_fis_directory(u_char *buf, size_t bufsize, off_t offset, uint32_t offmask) { -#define match(a,b) \ - (bcmp(fd->name, FISDIR_NAME, sizeof(FISDIR_NAME)-1) == 0) +#define match(a,b) (bcmp(a, b, sizeof(b)-1) == 0) struct fis_image_desc *fd, *efd; struct fis_image_desc *fisdir, *redbcfg; struct fis_image_desc *head, **tail; @@ -242,6 +256,10 @@ g_redboot_taste(struct g_class *mp, struct g_provider *pp, int insist) g_redboot_start); if (gp == NULL) return (NULL); + /* interpose our access method */ + sc->parent_access = gp->access; + gp->access = g_redboot_access; + sectorsize = cp->provider->sectorsize; blksize = cp->provider->stripesize; if (powerof2(cp->provider->mediasize)) @@ -287,6 +305,9 @@ g_redboot_taste(struct g_class *mp, struct g_provider *pp, int insist) __func__, error, fd->name); sc->entry[i] = fd->entry; sc->dsize[i] = fd->dsize; + /* disallow writing hard-to-recover entries */ + sc->readonly[i] = (strcmp(fd->name, FISDIR_NAME) == 0) || + (strcmp(fd->name, REDBOOT_NAME) == 0); i++; } g_free(buf);