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/i386/bios/vpd.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) 2003 Matthew N. Dodd <winter@jurai.net>
    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  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/5.3/sys/i386/bios/vpd.c 130312 2004-06-10 20:31:00Z jhb $");
   29 
   30 /*
   31  * VPD decoder for IBM systems (Thinkpads)
   32  * http://www-1.ibm.com/support/docview.wss?uid=psg1MIGR-45120
   33  */
   34 
   35 #include <sys/param.h>
   36 #include <sys/systm.h>
   37 #include <sys/kernel.h>
   38 #include <sys/socket.h>
   39 #include <sys/sysctl.h>
   40 
   41 #include <sys/module.h>
   42 #include <sys/bus.h>
   43 
   44 #include <machine/bus.h>
   45 #include <machine/resource.h>
   46 #include <sys/rman.h>
   47 
   48 #include <vm/vm.h>
   49 #include <vm/vm_param.h>
   50 #include <vm/pmap.h>
   51 #include <machine/md_var.h>
   52 #include <machine/pc/bios.h>
   53 
   54 /*
   55  * Vital Product Data
   56  */
   57 struct vpd {
   58         u_int16_t       Header;                 /* 0x55AA */
   59         u_int8_t        Signature[3];           /* Always 'VPD' */
   60         u_int8_t        Length;                 /* Sructure Length */
   61 
   62         u_int8_t        Reserved[7];            /* Reserved */
   63 
   64         u_int8_t        BuildID[9];             /* BIOS Build ID */
   65         u_int8_t        BoxSerial[7];           /* Box Serial Number */
   66         u_int8_t        PlanarSerial[11];       /* Motherboard Serial Number */
   67         u_int8_t        MachType[7];            /* Machine Type/Model */
   68         u_int8_t        Checksum;               /* Checksum */
   69 } __packed;
   70 
   71 struct vpd_softc {
   72         device_t                dev;
   73         struct resource *       res;
   74         int                     rid;
   75 
   76         struct vpd *            vpd;
   77 
   78         struct sysctl_ctx_list  ctx;
   79 
   80         char            BuildID[10];
   81         char            BoxSerial[8];
   82         char            PlanarSerial[12];
   83         char            MachineType[5];
   84         char            MachineModel[4];
   85 };
   86 
   87 #define VPD_START       0xf0000
   88 #define VPD_STEP        0x10
   89 #define VPD_OFF         2
   90 #define VPD_LEN         3
   91 #define VPD_SIG         "VPD"
   92 
   93 #define RES2VPD(res)    ((struct vpd *)rman_get_virtual(res))
   94 #define ADDR2VPD(addr)  ((struct vpd *)BIOS_PADDRTOVADDR(addr))
   95 
   96 static devclass_t vpd_devclass;
   97 
   98 static void     vpd_identify    (driver_t *, device_t);
   99 static int      vpd_probe       (device_t);
  100 static int      vpd_attach      (device_t);
  101 static int      vpd_detach      (device_t);
  102 static int      vpd_modevent    (module_t, int, void *);
  103 
  104 static int      vpd_cksum       (struct vpd *);
  105 
  106 SYSCTL_NODE(_hw, OID_AUTO, vpd, CTLFLAG_RD, NULL, NULL);
  107 SYSCTL_NODE(_hw_vpd, OID_AUTO, machine, CTLFLAG_RD, NULL, NULL);
  108 SYSCTL_NODE(_hw_vpd_machine, OID_AUTO, type, CTLFLAG_RD, NULL, NULL);
  109 SYSCTL_NODE(_hw_vpd_machine, OID_AUTO, model, CTLFLAG_RD, NULL, NULL);
  110 SYSCTL_NODE(_hw_vpd, OID_AUTO, build_id, CTLFLAG_RD, NULL, NULL);
  111 SYSCTL_NODE(_hw_vpd, OID_AUTO, serial, CTLFLAG_RD, NULL, NULL);
  112 SYSCTL_NODE(_hw_vpd_serial, OID_AUTO, box, CTLFLAG_RD, NULL, NULL);
  113 SYSCTL_NODE(_hw_vpd_serial, OID_AUTO, planar, CTLFLAG_RD, NULL, NULL);
  114 
  115 static void
  116 vpd_identify (driver_t *driver, device_t parent)
  117 {
  118         device_t child;
  119         u_int32_t addr;
  120         int length;
  121         int rid;
  122 
  123         if (!device_is_alive(parent))
  124                 return;
  125 
  126         addr = bios_sigsearch(VPD_START, VPD_SIG, VPD_LEN, VPD_STEP, VPD_OFF);
  127         if (addr != 0) {
  128                 rid = 0;
  129                 length = ADDR2VPD(addr)->Length;
  130 
  131                 child = BUS_ADD_CHILD(parent, 0, "vpd", -1);
  132                 device_set_driver(child, driver);
  133                 bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length);
  134                 device_set_desc(child, "Vital Product Data Area");
  135         }
  136                 
  137         return;
  138 }
  139 
  140 static int
  141 vpd_probe (device_t dev)
  142 {
  143         struct resource *res;
  144         int rid;
  145         int error;
  146 
  147         error = 0;
  148         rid = 0;
  149         res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
  150         if (res == NULL) {
  151                 device_printf(dev, "Unable to allocate memory resource.\n");
  152                 error = ENOMEM;
  153                 goto bad;
  154         }
  155 
  156         if (vpd_cksum(RES2VPD(res))) {
  157                 device_printf(dev, "VPD checksum failed.\n");
  158                 error = ENXIO;
  159                 goto bad;
  160         }
  161 
  162 bad:
  163         if (res)
  164                 bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
  165         return (error);
  166 }
  167 
  168 static int
  169 vpd_attach (device_t dev)
  170 {
  171         struct vpd_softc *sc;
  172         char unit[4];
  173         int error;
  174 
  175         sc = device_get_softc(dev);
  176         error = 0;
  177 
  178         sc->dev = dev;
  179         sc->rid = 0;
  180         sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
  181                 RF_ACTIVE);
  182         if (sc->res == NULL) {
  183                 device_printf(dev, "Unable to allocate memory resource.\n");
  184                 error = ENOMEM;
  185                 goto bad;
  186         }
  187         sc->vpd = RES2VPD(sc->res);
  188 
  189         snprintf(unit, sizeof(unit), "%d", device_get_unit(sc->dev));
  190         snprintf(sc->MachineType, 5, "%.4s", sc->vpd->MachType);
  191         snprintf(sc->MachineModel, 4, "%.3s", sc->vpd->MachType+4);
  192         snprintf(sc->BuildID, 10, "%.9s", sc->vpd->BuildID);
  193         snprintf(sc->BoxSerial, 8, "%.7s", sc->vpd->BoxSerial);
  194         snprintf(sc->PlanarSerial, 12, "%.11s", sc->vpd->PlanarSerial);
  195 
  196         sysctl_ctx_init(&sc->ctx);
  197         SYSCTL_ADD_STRING(&sc->ctx,
  198                 SYSCTL_STATIC_CHILDREN(_hw_vpd_machine_type), OID_AUTO,
  199                 unit, CTLFLAG_RD|CTLFLAG_DYN, sc->MachineType, 0, NULL);
  200         SYSCTL_ADD_STRING(&sc->ctx,
  201                 SYSCTL_STATIC_CHILDREN(_hw_vpd_machine_model), OID_AUTO,
  202                 unit, CTLFLAG_RD|CTLFLAG_DYN, sc->MachineModel, 0, NULL);
  203         SYSCTL_ADD_STRING(&sc->ctx,
  204                 SYSCTL_STATIC_CHILDREN(_hw_vpd_build_id), OID_AUTO,
  205                 unit, CTLFLAG_RD|CTLFLAG_DYN, sc->BuildID, 0, NULL);
  206         SYSCTL_ADD_STRING(&sc->ctx,
  207                 SYSCTL_STATIC_CHILDREN(_hw_vpd_serial_box), OID_AUTO,
  208                 unit, CTLFLAG_RD|CTLFLAG_DYN, sc->BoxSerial, 0, NULL);
  209         SYSCTL_ADD_STRING(&sc->ctx,
  210                 SYSCTL_STATIC_CHILDREN(_hw_vpd_serial_planar), OID_AUTO,
  211                 unit, CTLFLAG_RD|CTLFLAG_DYN, sc->PlanarSerial, 0, NULL);
  212 
  213         device_printf(dev, "Machine Type: %.4s, Model: %.3s, Build ID: %.9s\n",
  214                 sc->MachineType, sc->MachineModel, sc->BuildID);
  215         device_printf(dev, "Box Serial: %.7s, Planar Serial: %.11s\n",
  216                 sc->BoxSerial, sc->PlanarSerial);
  217                 
  218         return (0);
  219 bad:
  220         if (sc->res)
  221                 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
  222         return (error);
  223 }
  224 
  225 static int
  226 vpd_detach (device_t dev)
  227 {
  228         struct vpd_softc *sc;
  229 
  230         sc = device_get_softc(dev);
  231 
  232         if (sc->res)
  233                 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
  234 
  235         sysctl_ctx_free(&sc->ctx);
  236 
  237         return (0);
  238 }
  239 
  240 static int
  241 vpd_modevent (mod, what, arg)
  242         module_t        mod;
  243         int             what;
  244         void *          arg;
  245 {
  246         device_t *      devs;
  247         int             count;
  248         int             i;
  249 
  250         switch (what) {
  251         case MOD_LOAD:
  252                 break;
  253         case MOD_UNLOAD:
  254                 devclass_get_devices(vpd_devclass, &devs, &count);
  255                 for (i = 0; i < count; i++) {
  256                         device_delete_child(device_get_parent(devs[i]), devs[i]);
  257                 }
  258                 break;
  259         default:
  260                 break;
  261         }
  262 
  263         return (0);
  264 }
  265 
  266 static device_method_t vpd_methods[] = {
  267         /* Device interface */
  268         DEVMETHOD(device_identify,      vpd_identify),
  269         DEVMETHOD(device_probe,         vpd_probe),
  270         DEVMETHOD(device_attach,        vpd_attach),
  271         DEVMETHOD(device_detach,        vpd_detach),
  272         { 0, 0 }
  273 };
  274 
  275 static driver_t vpd_driver = {
  276         "vpd",
  277         vpd_methods,
  278         sizeof(struct vpd_softc),
  279 };
  280 
  281 DRIVER_MODULE(vpd, nexus, vpd_driver, vpd_devclass, vpd_modevent, 0);
  282 MODULE_VERSION(vpd, 1);
  283 
  284 static int
  285 vpd_cksum (struct vpd *v)
  286 {
  287         u_int8_t *ptr;
  288         u_int8_t cksum;
  289         int i;
  290 
  291         ptr = (u_int8_t *)v;
  292         cksum = 0;
  293         for (i = 0; i < v->Length ; i++) {
  294                 cksum += ptr[i];
  295         }
  296 
  297         /* XXX: For some reason this isn't right. */
  298         printf("VPD cksum: 0x%02x\n", cksum);
  299         cksum = 0;
  300 
  301         return (cksum);
  302 }

Cache object: 3da409e0d91ae7500a751b104b09dad3


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