Print this page
    
5094 Update libsmbios with recent items
Reviewed by: Dan McDonald <danmcd@omniti.com>
Reviewed by: Josef 'Jeff' Sipek<jeffpc@josefsipek.net>
Reviewed by: Garrett D'Amore <garrett@damore.org>
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/common/smbios/smb_open.c
          +++ new/usr/src/common/smbios/smb_open.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  
    | 
      ↓ open down ↓ | 
    12 lines elided | 
    
      ↑ open up ↑ | 
  
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
       23 + * Copyright 2015 OmniTI Computer Consulting, Inc.  All rights reserved.
  23   24   * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24   25   * Use is subject to license terms.
  25   26   */
  26   27  
  27   28  #include <sys/smbios_impl.h>
  28   29  
  29   30  static const uint_t _smb_hashlen = 64;          /* hash length (must be Pof2) */
  30   31  static const char _smb_emptystr[] = "";         /* empty string to return */
  31   32  int _smb_debug = 0;                             /* default debug mode */
  32   33  
  33   34  /*
  34   35   * Strip out identification information for you privacy weenies.  This is quite
  35   36   * simple using our smbios_info_common() abstraction: we just locate any serial
  36   37   * numbers and asset tags for each record, and then zero out those strings.
  37   38   * Then we must handle two special cases: SMB_TYPE_SYSTEM holds a 16-byte UUID
  38   39   * and SMB_TYPE_BATTERY stores a Smart Battery Data Spec 16-bit serial number.
  39   40   * We use a literal '0' rather than '\0' for zeroing strings because \0\0 in
  40   41   * the SMBIOS string table has a special meaning (denotes end-of-record).
  41   42   */
  42   43  static void
  43   44  smb_strip(smbios_hdl_t *shp)
  44   45  {
  45   46          uint_t i;
  46   47  
  47   48          for (i = 0; i < shp->sh_nstructs; i++) {
  48   49                  const smb_header_t *hp = shp->sh_structs[i].smbst_hdr;
  49   50                  smbios_info_t info;
  50   51                  char *p;
  51   52  
  52   53                  if (hp->smbh_type == SMB_TYPE_SYSTEM &&
  53   54                      hp->smbh_len >= offsetof(smb_system_t, smbsi_wakeup)) {
  54   55                          smb_system_t *sp = (smb_system_t *)(uintptr_t)hp;
  55   56                          bzero(sp->smbsi_uuid, sizeof (sp->smbsi_uuid));
  56   57                  }
  57   58  
  58   59                  if (hp->smbh_type == SMB_TYPE_BATTERY &&
  59   60                      hp->smbh_len >= offsetof(smb_battery_t, smbbat_sdate)) {
  60   61                          smb_battery_t *bp = (smb_battery_t *)(uintptr_t)hp;
  61   62                          bp->smbbat_ssn = 0;
  62   63                  }
  63   64  
  64   65                  if (smbios_info_common(shp, hp->smbh_hdl, &info) != SMB_ERR) {
  65   66                          for (p = (char *)info.smbi_serial; *p != '\0'; p++)
  66   67                                  *p = '0';
  67   68                          for (p = (char *)info.smbi_asset; *p != '\0'; p++)
  68   69                                  *p = '0';
  69   70                  }
  70   71          }
  71   72  }
  72   73  
  73   74  smbios_hdl_t *
  74   75  smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len,
  
    | 
      ↓ open down ↓ | 
    42 lines elided | 
    
      ↑ open up ↑ | 
  
  75   76      int version, int flags, int *errp)
  76   77  {
  77   78          smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t));
  78   79          const smb_header_t *hp, *nhp;
  79   80          const uchar_t *p, *q, *s;
  80   81          uint_t i, h;
  81   82  
  82   83          switch (version) {
  83   84          case SMB_VERSION_23:
  84   85          case SMB_VERSION_24:
       86 +        case SMB_VERSION_25:
       87 +        case SMB_VERSION_26:
       88 +        case SMB_VERSION_27:
       89 +        case SMB_VERSION_28:
  85   90                  break;
  86   91          default:
  87   92                  return (smb_open_error(shp, errp, ESMB_VERSION));
  88   93          }
  89   94  
  90   95          if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
  91   96                  return (smb_open_error(shp, errp, ESMB_INVAL));
  92   97  
  93   98          if (shp == NULL)
  94   99                  return (smb_open_error(shp, errp, ESMB_NOMEM));
  95  100  
  96  101          if (_smb_debug)
  97  102                  shp->sh_flags |= SMB_FL_DEBUG;
  98  103  
  99  104          if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN))
 100  105                  return (smb_open_error(shp, errp, ESMB_HEADER));
 101  106  
 102  107          if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN))
 103  108                  return (smb_open_error(shp, errp, ESMB_HEADER));
 104  109  
 105  110          smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n",
 106  111              ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev);
 107  112  
 108  113          if (!(flags & SMB_O_NOVERS)) {
 109  114                  if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
 110  115                          return (smb_open_error(shp, errp, ESMB_NEW));
 111  116  
 112  117                  if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
 113  118                      ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
 114  119                      ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
 115  120                          return (smb_open_error(shp, errp, ESMB_OLD));
 116  121          }
 117  122  
 118  123          if (len < sizeof (smb_header_t) ||
 119  124              ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
 120  125                  return (smb_open_error(shp, errp, ESMB_SHORT));
 121  126  
 122  127          if (!(flags & SMB_O_NOCKSUM)) {
 123  128                  uint8_t esum = 0, isum = 0;
 124  129                  q = (uchar_t *)ep;
 125  130  
 126  131                  for (p = q; p < q + ep->smbe_elen; p++)
 127  132                          esum += *p;
 128  133  
 129  134                  for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
 130  135                          isum += *p;
 131  136  
 132  137                  if (esum != 0 || isum != 0) {
 133  138                          smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum);
 134  139                          return (smb_open_error(shp, errp, ESMB_CKSUM));
 135  140                  }
 136  141          }
 137  142  
 138  143          /*
 139  144           * Copy the entry point into our handle.  The underlying entry point
 140  145           * may be larger than our structure definition, so reset smbe_elen
 141  146           * to our internal size and recompute good checksums for our copy.
 142  147           */
 143  148          bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
 144  149          shp->sh_ent.smbe_elen = sizeof (smbios_entry_t);
 145  150          smbios_checksum(shp, &shp->sh_ent);
 146  151  
 147  152          shp->sh_buf = buf;
 148  153          shp->sh_buflen = len;
 149  154          shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * ep->smbe_stnum);
 150  155          shp->sh_nstructs = 0;
 151  156          shp->sh_hashlen = _smb_hashlen;
 152  157          shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
 153  158          shp->sh_libvers = version;
 154  159          shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
 155  160  
 156  161          if (shp->sh_structs == NULL || shp->sh_hash == NULL)
 157  162                  return (smb_open_error(shp, errp, ESMB_NOMEM));
 158  163  
 159  164          hp = shp->sh_buf;
 160  165          q = (const uchar_t *)buf + MIN(ep->smbe_stlen, len);
 161  166  
 162  167          for (i = 0; i < ep->smbe_stnum; i++, hp = nhp) {
 163  168                  smb_struct_t *stp = &shp->sh_structs[i];
 164  169                  uint_t n = 0;
 165  170  
 166  171                  if ((const uchar_t *)hp + sizeof (smb_header_t) > q)
 167  172                          return (smb_open_error(shp, errp, ESMB_CORRUPT));
 168  173  
 169  174                  smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
 170  175                      i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
 171  176  
 172  177                  if (hp->smbh_type == SMB_TYPE_EOT)
 173  178                          break; /* ignore any entries beyond end-of-table */
 174  179  
 175  180                  if ((const uchar_t *)hp + hp->smbh_len > q - 2)
 176  181                          return (smb_open_error(shp, errp, ESMB_CORRUPT));
 177  182  
 178  183                  h = hp->smbh_hdl & (shp->sh_hashlen - 1);
 179  184                  p = s = (const uchar_t *)hp + hp->smbh_len;
 180  185  
 181  186                  while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
 182  187                          if (*p++ == '\0')
 183  188                                  n++; /* count strings until \0\0 delimiter */
 184  189                  }
 185  190  
 186  191                  if (p > q - 2)
 187  192                          return (smb_open_error(shp, errp, ESMB_CORRUPT));
 188  193  
 189  194                  if (p > s)
 190  195                          n++; /* add one for final string in string table */
 191  196  
 192  197                  stp->smbst_hdr = hp;
 193  198                  stp->smbst_str = s;
 194  199                  stp->smbst_end = p;
 195  200                  stp->smbst_next = shp->sh_hash[h];
 196  201                  stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
 197  202                  stp->smbst_strtablen = n;
 198  203  
 199  204                  if (n != 0 && stp->smbst_strtab == NULL)
 200  205                          return (smb_open_error(shp, errp, ESMB_NOMEM));
 201  206  
 202  207                  shp->sh_hash[h] = stp;
 203  208                  nhp = (void *)(p + 2);
 204  209                  shp->sh_nstructs++;
 205  210  
 206  211                  for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
 207  212                          if (*p == '\0') {
 208  213                                  stp->smbst_strtab[n++] =
 209  214                                      (uint16_t)(s - stp->smbst_str);
 210  215                                  s = p + 1;
 211  216                          }
 212  217                  }
 213  218          }
 214  219  
 215  220          if (flags & SMB_O_ZIDS)
 216  221                  smb_strip(shp);
 217  222  
 218  223          return (shp);
 219  224  }
 220  225  
 221  226  void
 222  227  smbios_close(smbios_hdl_t *shp)
 223  228  {
 224  229          const smbios_entry_t *ep = &shp->sh_ent;
 225  230          uint_t i;
 226  231  
 227  232          for (i = 0; i < shp->sh_nstructs; i++) {
 228  233                  smb_free(shp->sh_structs[i].smbst_strtab,
 229  234                      sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
 230  235          }
 231  236  
 232  237          smb_free(shp->sh_structs, sizeof (smb_struct_t) * ep->smbe_stnum);
 233  238          smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
 234  239  
 235  240          if (shp->sh_flags & SMB_FL_BUFALLOC)
 236  241                  smb_free((void *)shp->sh_buf, shp->sh_buflen);
 237  242  
 238  243          smb_free(shp, sizeof (smbios_hdl_t));
 239  244  }
 240  245  
 241  246  /*
 242  247   * Recompute the values of the entry point checksums based upon the content
 243  248   * of the specified SMBIOS entry point.  We don't need 'shp' but require it
 244  249   * anyway in case future versioning requires variations in the algorithm.
 245  250   */
 246  251  /*ARGSUSED*/
 247  252  void
 248  253  smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
 249  254  {
 250  255          uchar_t *p, *q = (uchar_t *)ep;
 251  256          uint8_t esum = 0, isum = 0;
 252  257  
 253  258          ep->smbe_ecksum = ep->smbe_icksum = 0;
 254  259  
 255  260          for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
 256  261                  isum += *p;
 257  262  
 258  263          ep->smbe_icksum = -isum;
 259  264  
 260  265          for (p = q; p < q + ep->smbe_elen; p++)
 261  266                  esum += *p;
 262  267  
 263  268          ep->smbe_ecksum = -esum;
 264  269  }
 265  270  
 266  271  const void *
 267  272  smbios_buf(smbios_hdl_t *shp)
 268  273  {
 269  274          return (shp->sh_buf);
 270  275  }
 271  276  
 272  277  size_t
 273  278  smbios_buflen(smbios_hdl_t *shp)
 274  279  {
 275  280          return (shp->sh_buflen);
 276  281  }
 277  282  
 278  283  static smbios_struct_t *
 279  284  smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
 280  285  {
 281  286          const smb_header_t *hdr = stp->smbst_hdr;
 282  287  
 283  288          sp->smbstr_id = hdr->smbh_hdl;
 284  289          sp->smbstr_type = hdr->smbh_type;
 285  290          sp->smbstr_data = hdr;
 286  291          sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
 287  292  
 288  293          return (sp);
 289  294  }
 290  295  
 291  296  int
 292  297  smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
 293  298  {
 294  299          const smb_struct_t *stp = smb_lookup_id(shp, id);
 295  300  
 296  301          if (stp == NULL)
 297  302                  return (-1); /* errno is set for us */
 298  303  
 299  304          if (sp != NULL)
 300  305                  (void) smb_export(stp, sp);
 301  306  
 302  307          return (0);
 303  308  }
 304  309  
 305  310  int
 306  311  smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp)
 307  312  {
 308  313          const smb_struct_t *stp = smb_lookup_type(shp, type);
 309  314  
 310  315          if (stp == NULL)
 311  316                  return (-1); /* errno is set for us */
 312  317  
 313  318          if (sp != NULL)
 314  319                  (void) smb_export(stp, sp);
 315  320  
 316  321          return (0);
 317  322  }
 318  323  
 319  324  int
 320  325  smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
 321  326  {
 322  327          const smb_struct_t *sp = shp->sh_structs;
 323  328          smbios_struct_t s;
 324  329          int i, rv = 0;
 325  330  
 326  331          for (i = 0; i < shp->sh_nstructs; i++, sp++) {
 327  332                  if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
 328  333                      (rv = func(shp, smb_export(sp, &s), data)) != 0)
 329  334                          break;
 330  335          }
 331  336  
 332  337          return (rv);
 333  338  }
 334  339  
 335  340  const smb_struct_t *
 336  341  smb_lookup_type(smbios_hdl_t *shp, uint_t type)
 337  342  {
 338  343          uint_t i;
 339  344  
 340  345          for (i = 0; i < shp->sh_nstructs; i++) {
 341  346                  if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
 342  347                          return (&shp->sh_structs[i]);
 343  348          }
 344  349  
 345  350          (void) smb_set_errno(shp, ESMB_NOENT);
 346  351          return (NULL);
 347  352  }
 348  353  
 349  354  const smb_struct_t *
 350  355  smb_lookup_id(smbios_hdl_t *shp, uint_t id)
 351  356  {
 352  357          const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
 353  358  
 354  359          switch (id) {
 355  360          case SMB_ID_NOTSUP:
 356  361                  (void) smb_set_errno(shp, ESMB_NOTSUP);
 357  362                  return (NULL);
 358  363          case SMB_ID_NONE:
 359  364                  (void) smb_set_errno(shp, ESMB_NOENT);
 360  365                  return (NULL);
 361  366          }
 362  367  
 363  368          for (; stp != NULL; stp = stp->smbst_next) {
 364  369                  if (stp->smbst_hdr->smbh_hdl == id)
 365  370                          break;
 366  371          }
 367  372  
 368  373          if (stp == NULL)
 369  374                  (void) smb_set_errno(shp, ESMB_NOENT);
 370  375  
 371  376          return (stp);
 372  377  }
 373  378  
 374  379  const char *
 375  380  smb_strptr(const smb_struct_t *stp, uint_t i)
 376  381  {
 377  382          if (i == 0 || i > stp->smbst_strtablen)
 378  383                  return (_smb_emptystr);
 379  384          else
 380  385                  return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
 381  386  }
 382  387  
 383  388  int
 384  389  smb_gteq(smbios_hdl_t *shp, int version)
 385  390  {
 386  391          return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
 387  392              SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
 388  393              SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
 389  394  }
  
    | 
      ↓ open down ↓ | 
    295 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX