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_pc98.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2002 Poul-Henning Kamp
    3  * Copyright (c) 2002 Networks Associates Technology, Inc.
    4  * All rights reserved.
    5  *
    6  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
    7  * and NAI Labs, the Security Research Division of Network Associates, Inc.
    8  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
    9  * DARPA CHATS research program.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD$");
   35 
   36 #include <sys/param.h>
   37 #include <sys/endian.h>
   38 #include <sys/systm.h>
   39 #include <sys/kernel.h>
   40 #include <sys/fcntl.h>
   41 #include <sys/malloc.h>
   42 #include <sys/bio.h>
   43 #include <sys/lock.h>
   44 #include <sys/mutex.h>
   45 #include <sys/proc.h>
   46 
   47 #include <sys/diskpc98.h>
   48 #include <geom/geom.h>
   49 #include <geom/geom_slice.h>
   50 
   51 #define PC98_CLASS_NAME "PC98"
   52 
   53 struct g_pc98_softc {
   54         u_int fwsectors, fwheads, sectorsize;
   55         int type[NDOSPART];
   56         u_char sec[8192];
   57 };
   58 
   59 static void
   60 g_pc98_print(int i, struct pc98_partition *dp)
   61 {
   62         char sname[17];
   63 
   64         strncpy(sname, dp->dp_name, 16);
   65         sname[16] = '\0';
   66 
   67         hexdump(dp, sizeof(dp[0]), NULL, 0);
   68         printf("[%d] mid:%d(0x%x) sid:%d(0x%x)",
   69                i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid);
   70         printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
   71         printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
   72         printf(" sname:%s\n", sname);
   73 }
   74 
   75 /*
   76  * XXX: Add gctl_req arg and give good error msgs.
   77  * XXX: Check that length argument does not bring boot code inside any slice.
   78  */
   79 static int
   80 g_pc98_modify(struct g_geom *gp, struct g_pc98_softc *ms, u_char *sec, int len __unused)
   81 {
   82         int i, error;
   83         off_t s[NDOSPART], l[NDOSPART];
   84         struct pc98_partition dp[NDOSPART];
   85 
   86         g_topology_assert();
   87         
   88         if (sec[0x1fe] != 0x55 || sec[0x1ff] != 0xaa)
   89                 return (EBUSY);
   90 
   91 #if 0
   92         /*
   93          * By convetion, it seems that the ipl program has a jump at location
   94          * 0 to the real start of the boot loader.  By convetion, it appears
   95          * that after this jump, there's a string, terminated by at last one,
   96          * if not more, zeros, followed by the target of the jump.  FreeBSD's
   97          * pc98 boot0 uses 'IPL1' followed by 3 zeros here, likely for
   98          * compatibility with some older boot loader.  Linux98's boot loader
   99          * appears to use 'Linux 98' followed by only two.  GRUB/98 appears to
  100          * use 'GRUB/98 ' followed by none.  These last two appear to be
  101          * ported from the ia32 versions, but appear to show similar
  102          * convention.  Grub/98 has an additional NOP after the jmp, which
  103          * isn't present in others.
  104          *
  105          * The following test was inspired by looking only at partitions
  106          * with FreeBSD's boot0 (or one that it is compatible with).  As
  107          * such, if failed when other IPL programs were used.
  108          */
  109         if (sec[4] != 'I' || sec[5] != 'P' || sec[6] != 'L' || sec[7] != '1')
  110                 return (EBUSY);
  111 #endif
  112 
  113         for (i = 0; i < NDOSPART; i++)
  114                 pc98_partition_dec(
  115                         sec + 512 + i * sizeof(struct pc98_partition), &dp[i]);
  116 
  117         for (i = 0; i < NDOSPART; i++) {
  118                 /* If start and end are identical it's bogus */
  119                 if (dp[i].dp_ssect == dp[i].dp_esect &&
  120                     dp[i].dp_shd == dp[i].dp_ehd &&
  121                     dp[i].dp_scyl == dp[i].dp_ecyl)
  122                         s[i] = l[i] = 0;
  123                 else if (dp[i].dp_ecyl == 0)
  124                         s[i] = l[i] = 0;
  125                 else {
  126                         s[i] = (off_t)dp[i].dp_scyl *
  127                                 ms->fwsectors * ms->fwheads * ms->sectorsize;
  128                         l[i] = (off_t)(dp[i].dp_ecyl - dp[i].dp_scyl + 1) *
  129                                 ms->fwsectors * ms->fwheads * ms->sectorsize;
  130                 }
  131                 if (bootverbose) {
  132                         printf("PC98 Slice %d on %s:\n", i + 1, gp->name);
  133                         g_pc98_print(i, dp + i);
  134                 }
  135                 if (s[i] < 0 || l[i] < 0)
  136                         error = EBUSY;
  137                 else
  138                         error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
  139                                        s[i], l[i], ms->sectorsize,
  140                                        "%ss%d", gp->name, i + 1);
  141                 if (error)
  142                         return (error);
  143         }
  144 
  145         for (i = 0; i < NDOSPART; i++) {
  146                 ms->type[i] = (dp[i].dp_sid << 8) | dp[i].dp_mid;
  147                 g_slice_config(gp, i, G_SLICE_CONFIG_SET, s[i], l[i],
  148                                ms->sectorsize, "%ss%d", gp->name, i + 1);
  149         }
  150 
  151         bcopy(sec, ms->sec, sizeof (ms->sec));
  152 
  153         return (0);
  154 }
  155 
  156 static int
  157 g_pc98_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
  158 {
  159         struct g_geom *gp;
  160         struct g_pc98_softc *ms;
  161         struct g_slicer *gsp;
  162         struct g_consumer *cp;
  163         int error, opened;
  164 
  165         gp = pp->geom;
  166         gsp = gp->softc;
  167         ms = gsp->softc;
  168 
  169         opened = 0;
  170         error = 0;
  171         switch(cmd) {
  172         case DIOCSPC98: {
  173                 if (!(fflag & FWRITE))
  174                         return (EPERM);
  175                 DROP_GIANT();
  176                 g_topology_lock();
  177                 cp = LIST_FIRST(&gp->consumer);
  178                 if (cp->acw == 0) {
  179                         error = g_access(cp, 0, 1, 0);
  180                         if (error == 0)
  181                                 opened = 1;
  182                 }
  183                 if (!error)
  184                         error = g_pc98_modify(gp, ms, data, 8192);
  185                 if (!error)
  186                         error = g_write_data(cp, 0, data, 8192);
  187                 if (opened)
  188                         g_access(cp, 0, -1 , 0);
  189                 g_topology_unlock();
  190                 PICKUP_GIANT();
  191                 return(error);
  192         }
  193         default:
  194                 return (ENOIOCTL);
  195         }
  196 }
  197 
  198 static int
  199 g_pc98_start(struct bio *bp)
  200 {
  201         struct g_provider *pp;
  202         struct g_geom *gp;
  203         struct g_pc98_softc *mp;
  204         struct g_slicer *gsp;
  205         int idx;
  206 
  207         pp = bp->bio_to;
  208         idx = pp->index;
  209         gp = pp->geom;
  210         gsp = gp->softc;
  211         mp = gsp->softc;
  212         if (bp->bio_cmd == BIO_GETATTR) {
  213                 if (g_handleattr_int(bp, "PC98::type", mp->type[idx]))
  214                         return (1);
  215                 if (g_handleattr_off_t(bp, "PC98::offset",
  216                                        gsp->slices[idx].offset))
  217                         return (1);
  218         }
  219 
  220         return (0);
  221 }
  222 
  223 static void
  224 g_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
  225                 struct g_consumer *cp __unused, struct g_provider *pp)
  226 {
  227         struct g_pc98_softc *mp;
  228         struct g_slicer *gsp;
  229         struct pc98_partition dp;
  230         char sname[17];
  231 
  232         gsp = gp->softc;
  233         mp = gsp->softc;
  234         g_slice_dumpconf(sb, indent, gp, cp, pp);
  235         if (pp != NULL) {
  236                 pc98_partition_dec(
  237                         mp->sec + 512 +
  238                         pp->index * sizeof(struct pc98_partition), &dp);
  239                 strncpy(sname, dp.dp_name, 16);
  240                 sname[16] = '\0';
  241                 if (indent == NULL) {
  242                         sbuf_printf(sb, " ty %d", mp->type[pp->index]);
  243                         sbuf_printf(sb, " sn %s", sname);
  244                 } else {
  245                         sbuf_printf(sb, "%s<type>%d</type>\n", indent,
  246                                     mp->type[pp->index]);
  247                         sbuf_printf(sb, "%s<sname>%s</sname>\n", indent,
  248                                     sname);
  249                 }
  250         }
  251 }
  252 
  253 static struct g_geom *
  254 g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
  255 {
  256         struct g_geom *gp;
  257         struct g_consumer *cp;
  258         int error;
  259         struct g_pc98_softc *ms;
  260         u_int fwsectors, fwheads, sectorsize;
  261         u_char *buf;
  262 
  263         g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
  264         g_topology_assert();
  265         if (flags == G_TF_NORMAL &&
  266             !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
  267                 return (NULL);
  268         gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start);
  269         if (gp == NULL)
  270                 return (NULL);
  271         g_topology_unlock();
  272         do {
  273                 if (gp->rank != 2 && flags == G_TF_NORMAL)
  274                         break;
  275                 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
  276                 if (error || fwsectors == 0) {
  277                         fwsectors = 17;
  278                         if (bootverbose)
  279                                 printf("g_pc98_taste: guessing %d sectors\n",
  280                                     fwsectors);
  281                 }
  282                 error = g_getattr("GEOM::fwheads", cp, &fwheads);
  283                 if (error || fwheads == 0) {
  284                         fwheads = 8;
  285                         if (bootverbose)
  286                                 printf("g_pc98_taste: guessing %d heads\n",
  287                                     fwheads);
  288                 }
  289                 sectorsize = cp->provider->sectorsize;
  290                 if (sectorsize % 512 != 0)
  291                         break;
  292                 buf = g_read_data(cp, 0, 8192, NULL);
  293                 if (buf == NULL)
  294                         break;
  295                 ms->fwsectors = fwsectors;
  296                 ms->fwheads = fwheads;
  297                 ms->sectorsize = sectorsize;
  298                 g_topology_lock();
  299                 g_pc98_modify(gp, ms, buf, 8192);
  300                 g_topology_unlock();
  301                 g_free(buf);
  302                 break;
  303         } while (0);
  304         g_topology_lock();
  305         g_access(cp, -1, 0, 0);
  306         if (LIST_EMPTY(&gp->provider)) {
  307                 g_slice_spoiled(cp);
  308                 return (NULL);
  309         }
  310         return (gp);
  311 }
  312 
  313 static void
  314 g_pc98_config(struct gctl_req *req, struct g_class *mp, const char *verb)
  315 {
  316         struct g_geom *gp;
  317         struct g_consumer *cp;
  318         struct g_pc98_softc *ms;
  319         struct g_slicer *gsp;
  320         int opened = 0, error = 0;
  321         void *data;
  322         int len;
  323 
  324         g_topology_assert();
  325         gp = gctl_get_geom(req, mp, "geom");
  326         if (gp == NULL)
  327                 return;
  328         if (strcmp(verb, "write PC98")) {
  329                 gctl_error(req, "Unknown verb");
  330                 return;
  331         }
  332         gsp = gp->softc;
  333         ms = gsp->softc;
  334         data = gctl_get_param(req, "data", &len);
  335         if (data == NULL)
  336                 return;
  337         if (len < 8192 || (len % 512)) {
  338                 gctl_error(req, "Wrong request length");
  339                 return;
  340         }
  341         cp = LIST_FIRST(&gp->consumer);
  342         if (cp->acw == 0) {
  343                 error = g_access(cp, 0, 1, 0);
  344                 if (error == 0)
  345                         opened = 1;
  346         }
  347         if (!error)
  348                 error = g_pc98_modify(gp, ms, data, len);
  349         if (error)
  350                 gctl_error(req, "conflict with open slices");
  351         if (!error)
  352                 error = g_write_data(cp, 0, data, len);
  353         if (error)
  354                 gctl_error(req, "sector zero write failed");
  355         if (opened)
  356                 g_access(cp, 0, -1 , 0);
  357         return;
  358 }
  359 
  360 static struct g_class g_pc98_class = {
  361         .name = PC98_CLASS_NAME,
  362         .version = G_VERSION,
  363         .taste = g_pc98_taste,
  364         .dumpconf = g_pc98_dumpconf,
  365         .ctlreq = g_pc98_config,
  366         .ioctl = g_pc98_ioctl,
  367 };
  368 
  369 DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);

Cache object: 2b8a4a6a9e0fa84071d06ea9e94de3d7


[ 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.