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  * $FreeBSD: releng/5.1/sys/geom/geom_pc98.c 114507 2003-05-02 06:34:51Z phk $
   33  */
   34 
   35 #include <sys/param.h>
   36 #include <sys/endian.h>
   37 #include <sys/systm.h>
   38 #include <sys/kernel.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/diskpc98.h>
   45 #include <geom/geom.h>
   46 #include <geom/geom_slice.h>
   47 
   48 #define PC98_CLASS_NAME "PC98"
   49 
   50 struct g_pc98_softc {
   51         u_int fwsectors, fwheads, sectorsize;
   52         int type[NDOSPART];
   53         u_char sec[8192];
   54 };
   55 
   56 static void
   57 g_pc98_print(int i, struct pc98_partition *dp)
   58 {
   59         char sname[17];
   60 
   61         strncpy(sname, dp->dp_name, 16);
   62         sname[16] = '\0';
   63 
   64         g_hexdump(dp, sizeof(dp[0]));
   65         printf("[%d] mid:%d(0x%x) sid:%d(0x%x)",
   66                i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid);
   67         printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
   68         printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
   69         printf(" sname:%s\n", sname);
   70 }
   71 
   72 static int
   73 g_pc98_modify(struct g_geom *gp, struct g_pc98_softc *ms, u_char *sec)
   74 {
   75         int i, error;
   76         off_t s[NDOSPART], l[NDOSPART];
   77         struct pc98_partition dp[NDOSPART];
   78 
   79         g_topology_assert();
   80         
   81         if (sec[0x1fe] != 0x55 || sec[0x1ff] != 0xaa)
   82                 return (EBUSY);
   83 
   84 #if 0
   85         /*
   86          * XXX: Some sources indicate this is a magic sequence, but appearantly
   87          * XXX: it is not universal. Documentation would be wonderful to have.
   88          */
   89         if (sec[4] != 'I' || sec[5] != 'P' || sec[6] != 'L' || sec[7] != '1')
   90                 return (EBUSY);
   91 #endif
   92 
   93         for (i = 0; i < NDOSPART; i++)
   94                 pc98_partition_dec(
   95                         sec + 512 + i * sizeof(struct pc98_partition), &dp[i]);
   96 
   97         for (i = 0; i < NDOSPART; i++) {
   98                 /* If start and end are identical it's bogus */
   99                 if (dp[i].dp_ssect == dp[i].dp_esect &&
  100                     dp[i].dp_shd == dp[i].dp_ehd &&
  101                     dp[i].dp_scyl == dp[i].dp_ecyl)
  102                         s[i] = l[i] = 0;
  103                 else if (dp[i].dp_ecyl == 0)
  104                         s[i] = l[i] = 0;
  105                 else {
  106                         s[i] = (off_t)dp[i].dp_scyl *
  107                                 ms->fwsectors * ms->fwheads * ms->sectorsize;
  108                         l[i] = (off_t)(dp[i].dp_ecyl - dp[i].dp_scyl + 1) *
  109                                 ms->fwsectors * ms->fwheads * ms->sectorsize;
  110                 }
  111                 if (bootverbose) {
  112                         printf("PC98 Slice %d on %s:\n", i + 1, gp->name);
  113                         g_pc98_print(i, dp + i);
  114                 }
  115                 if (s[i] < 0 || l[i] < 0)
  116                         error = EBUSY;
  117                 else
  118                         error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
  119                                        s[i], l[i], ms->sectorsize,
  120                                        "%ss%d", gp->name, i + 1);
  121                 if (error)
  122                         return (error);
  123         }
  124 
  125         for (i = 0; i < NDOSPART; i++) {
  126                 ms->type[i] = (dp[i].dp_sid << 8) | dp[i].dp_mid;
  127                 g_slice_config(gp, i, G_SLICE_CONFIG_SET, s[i], l[i],
  128                                ms->sectorsize, "%ss%d", gp->name, i + 1);
  129         }
  130 
  131         bcopy(sec, ms->sec, sizeof (ms->sec));
  132 
  133         return (0);
  134 }
  135 
  136 static void
  137 g_pc98_ioctl(void *arg, int flag)
  138 {
  139         struct bio *bp;
  140         struct g_geom *gp;
  141         struct g_slicer *gsp;
  142         struct g_pc98_softc *ms;
  143         struct g_ioctl *gio;
  144         struct g_consumer *cp;
  145         u_char *sec;
  146         int error;
  147 
  148         bp = arg;
  149         if (flag == EV_CANCEL) {
  150                 g_io_deliver(bp, ENXIO);
  151                 return;
  152         }
  153         gp = bp->bio_to->geom;
  154         gsp = gp->softc;
  155         ms = gsp->softc;
  156         gio = (struct g_ioctl *)bp->bio_data;
  157 
  158         /* The disklabel to set is the ioctl argument. */
  159         sec = gio->data;
  160 
  161         error = g_pc98_modify(gp, ms, sec);
  162         if (error) {
  163                 g_io_deliver(bp, error);
  164                 return;
  165         }
  166         cp = LIST_FIRST(&gp->consumer);
  167         error = g_write_data(cp, 0, sec, 8192);
  168         g_io_deliver(bp, error);
  169 }
  170 
  171 static int
  172 g_pc98_start(struct bio *bp)
  173 {
  174         struct g_provider *pp;
  175         struct g_geom *gp;
  176         struct g_pc98_softc *mp;
  177         struct g_slicer *gsp;
  178         struct g_ioctl *gio;
  179         int idx, error;
  180 
  181         pp = bp->bio_to;
  182         idx = pp->index;
  183         gp = pp->geom;
  184         gsp = gp->softc;
  185         mp = gsp->softc;
  186         if (bp->bio_cmd == BIO_GETATTR) {
  187                 if (g_handleattr_int(bp, "PC98::type", mp->type[idx]))
  188                         return (1);
  189                 if (g_handleattr_off_t(bp, "PC98::offset",
  190                                        gsp->slices[idx].offset))
  191                         return (1);
  192         }
  193 
  194         /* We only handle ioctl(2) requests of the right format. */
  195         if (strcmp(bp->bio_attribute, "GEOM::ioctl"))
  196                 return (0);
  197         else if (bp->bio_length != sizeof(*gio))
  198                 return (0);
  199         /* Get hold of the ioctl parameters. */
  200         gio = (struct g_ioctl *)bp->bio_data;
  201 
  202         switch (gio->cmd) {
  203         case DIOCSPC98:
  204                 /*
  205                  * These we cannot do without the topology lock and some
  206                  * some I/O requests.  Ask the event-handler to schedule
  207                  * us in a less restricted environment.
  208                  */
  209                 error = g_post_event(g_pc98_ioctl, bp, M_NOWAIT, gp, NULL);
  210                 if (error)
  211                         g_io_deliver(bp, error);
  212                 /*
  213                  * We must return non-zero to indicate that we will deal
  214                  * with this bio, even though we have not done so yet.
  215                  */
  216                 return (1);
  217         default:
  218                 return (0);
  219         }
  220 
  221         return (0);
  222 }
  223 
  224 static void
  225 g_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
  226                 struct g_consumer *cp __unused, struct g_provider *pp)
  227 {
  228         struct g_pc98_softc *mp;
  229         struct g_slicer *gsp;
  230         struct pc98_partition dp;
  231         char sname[17];
  232 
  233         gsp = gp->softc;
  234         mp = gsp->softc;
  235         g_slice_dumpconf(sb, indent, gp, cp, pp);
  236         if (pp != NULL) {
  237                 pc98_partition_dec(
  238                         mp->sec + 512 +
  239                         pp->index * sizeof(struct pc98_partition), &dp);
  240                 strncpy(sname, dp.dp_name, 16);
  241                 sname[16] = '\0';
  242                 if (indent == NULL) {
  243                         sbuf_printf(sb, " ty %d", mp->type[pp->index]);
  244                         sbuf_printf(sb, " sn %s", sname);
  245                 } else {
  246                         sbuf_printf(sb, "%s<type>%d</type>\n", indent,
  247                                     mp->type[pp->index]);
  248                         sbuf_printf(sb, "%s<sname>%s</sname>\n", indent,
  249                                     sname);
  250                 }
  251         }
  252 }
  253 
  254 static struct g_geom *
  255 g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
  256 {
  257         struct g_geom *gp;
  258         struct g_consumer *cp;
  259         int error;
  260         struct g_pc98_softc *ms;
  261         struct g_slicer *gsp;
  262         u_int fwsectors, fwheads, sectorsize;
  263         u_char *buf;
  264 
  265         g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
  266         g_topology_assert();
  267         if (flags == G_TF_NORMAL &&
  268             !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
  269                 return (NULL);
  270         gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start);
  271         if (gp == NULL)
  272                 return (NULL);
  273         gsp = gp->softc;
  274         g_topology_unlock();
  275         gp->dumpconf = g_pc98_dumpconf;
  276         do {
  277                 if (gp->rank != 2 && flags == G_TF_NORMAL)
  278                         break;
  279                 error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
  280                 if (error || fwsectors == 0) {
  281                         fwsectors = 17;
  282                         if (bootverbose)
  283                                 printf("g_pc98_taste: guessing %d sectors\n",
  284                                     fwsectors);
  285                 }
  286                 error = g_getattr("GEOM::fwheads", cp, &fwheads);
  287                 if (error || fwheads == 0) {
  288                         fwheads = 8;
  289                         if (bootverbose)
  290                                 printf("g_pc98_taste: guessing %d heads\n",
  291                                     fwheads);
  292                 }
  293                 sectorsize = cp->provider->sectorsize;
  294                 if (sectorsize < 512)
  295                         break;
  296                 buf = g_read_data(cp, 0, 8192, &error);
  297                 if (buf == NULL || error != 0)
  298                         break;
  299                 ms->fwsectors = fwsectors;
  300                 ms->fwheads = fwheads;
  301                 ms->sectorsize = sectorsize;
  302                 g_topology_lock();
  303                 g_pc98_modify(gp, ms, buf);
  304                 g_topology_unlock();
  305                 g_free(buf);
  306                 break;
  307         } while (0);
  308         g_topology_lock();
  309         g_access_rel(cp, -1, 0, 0);
  310         if (LIST_EMPTY(&gp->provider)) {
  311                 g_slice_spoiled(cp);
  312                 return (NULL);
  313         }
  314         return (gp);
  315 }
  316 
  317 static struct g_class g_pc98_class = {
  318         .name = PC98_CLASS_NAME,
  319         .taste = g_pc98_taste,
  320         G_CLASS_INITIALIZER
  321 };
  322 
  323 DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);

Cache object: 74b736482dcd847fc0ddbf842a3e2618


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