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