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 2018 Joyent, Inc. 25 * Copyright 2021 Oxide Computer Company 26 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 30 #include <sys/smbios_impl.h> 31 32 static const uint_t _smb_hashlen = 64; /* hash length (must be Pof2) */ 33 static const char _smb_emptystr[] = ""; /* empty string to return */ 34 int _smb_debug = 0; /* default debug mode */ 35 36 /* 37 * Strip out identification information for you privacy weenies. This is quite 38 * simple using our smbios_info_common() abstraction: we just locate any serial 39 * numbers and asset tags for each record, and then zero out those strings. 40 * Then we must handle two special cases: SMB_TYPE_SYSTEM holds a 16-byte UUID 41 * and SMB_TYPE_BATTERY stores a Smart Battery Data Spec 16-bit serial number. 42 * We use a literal '0' rather than '\0' for zeroing strings because \0\0 in 43 * the SMBIOS string table has a special meaning (denotes end-of-record). 44 */ 45 static void 46 smb_strip(smbios_hdl_t *shp) 47 { 48 uint_t i; 49 50 for (i = 0; i < shp->sh_nstructs; i++) { 51 const smb_header_t *hp = shp->sh_structs[i].smbst_hdr; 52 smbios_info_t info; 53 char *p; 54 55 if (hp->smbh_type == SMB_TYPE_SYSTEM && 56 hp->smbh_len >= offsetof(smb_system_t, smbsi_wakeup)) { 57 smb_system_t *sp = (smb_system_t *)(uintptr_t)hp; 58 bzero(sp->smbsi_uuid, sizeof (sp->smbsi_uuid)); 59 } 60 61 if (hp->smbh_type == SMB_TYPE_BATTERY && 62 hp->smbh_len >= offsetof(smb_battery_t, smbbat_sdate)) { 63 smb_battery_t *bp = (smb_battery_t *)(uintptr_t)hp; 64 bp->smbbat_ssn = 0; 65 } 66 67 if (smbios_info_common(shp, hp->smbh_hdl, &info) != SMB_ERR) { 68 for (p = (char *)info.smbi_serial; *p != '\0'; p++) 69 *p = '0'; 70 for (p = (char *)info.smbi_asset; *p != '\0'; p++) 71 *p = '0'; 72 } 73 } 74 } 75 76 static int 77 smbios_bufopen_21(smbios_hdl_t *shp, const smbios_21_entry_t *ep, size_t len, 78 int flags) 79 { 80 if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN)) 81 return (ESMB_HEADER); 82 83 if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN)) 84 return (ESMB_HEADER); 85 86 smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n", 87 ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev); 88 89 if (!(flags & SMB_O_NOVERS)) { 90 if (ep->smbe_major > SMB_MAJOR(SMB_VERSION)) 91 return (ESMB_NEW); 92 93 if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || ( 94 ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) && 95 ep->smbe_minor < SMB_MINOR(SMB_VERSION_23))) 96 return (ESMB_OLD); 97 } 98 99 if (len < sizeof (smb_header_t) || 100 ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen) 101 return (ESMB_SHORT); 102 103 if (!(flags & SMB_O_NOCKSUM)) { 104 uint8_t esum = 0, isum = 0; 105 const uchar_t *p, *q; 106 q = (uchar_t *)ep; 107 108 for (p = q; p < q + ep->smbe_elen; p++) 109 esum += *p; 110 111 for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++) 112 isum += *p; 113 114 if (esum != 0 || isum != 0) { 115 smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum); 116 return (ESMB_CKSUM); 117 } 118 } 119 120 /* 121 * Copy the entry point into our handle. The underlying entry point 122 * may be larger than our structure definition, so reset smbe_elen 123 * to our internal size and recompute good checksums for our copy. 124 */ 125 shp->sh_ent_type = SMBIOS_ENTRY_POINT_21; 126 bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t)); 127 shp->sh_ent.ep21.smbe_elen = sizeof (smbios_entry_t); 128 smbios_checksum(shp, &shp->sh_ent); 129 130 shp->sh_ent_stnum = ep->smbe_stnum; 131 shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor); 132 return (0); 133 } 134 135 static int 136 smbios_bufopen_30(smbios_hdl_t *shp, const smbios_30_entry_t *ep, size_t len, 137 int flags) 138 { 139 if (strncmp(ep->smbe_eanchor, SMB3_ENTRY_EANCHOR, 140 SMB3_ENTRY_EANCHORLEN)) 141 return (ESMB_HEADER); 142 143 smb_dprintf(shp, "opening SMBIOS version %u.%u\n", 144 ep->smbe_major, ep->smbe_minor); 145 146 if (!(flags & SMB_O_NOVERS)) { 147 if (ep->smbe_major > SMB_MAJOR(SMB_VERSION)) 148 return (ESMB_NEW); 149 150 if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || ( 151 ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) && 152 ep->smbe_minor < SMB_MINOR(SMB_VERSION_23))) 153 return (ESMB_OLD); 154 } 155 156 if (len < sizeof (smb_header_t) || 157 ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen) 158 return (ESMB_SHORT); 159 160 if (!(flags & SMB_O_NOCKSUM)) { 161 uint8_t esum = 0; 162 const uchar_t *p, *q; 163 q = (uchar_t *)ep; 164 165 for (p = q; p < q + ep->smbe_elen; p++) 166 esum += *p; 167 168 if (esum != 0) { 169 smb_dprintf(shp, "bad cksum: e=%x\n", esum); 170 return (ESMB_CKSUM); 171 } 172 } 173 174 /* 175 * Copy the entry point into our handle. The underlying entry point 176 * may be larger than our structure definition, so reset smbe_elen 177 * to our internal size and recompute good checksums for our copy. 178 */ 179 shp->sh_ent_type = SMBIOS_ENTRY_POINT_30; 180 bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t)); 181 shp->sh_ent.ep30.smbe_elen = sizeof (smbios_entry_t); 182 smbios_checksum(shp, &shp->sh_ent); 183 184 shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor); 185 186 return (0); 187 } 188 189 static uint_t 190 smbios_table_nentries(const char *smbe_staddr, uint32_t smbe_stlen) 191 { 192 uint_t i = 0; 193 char *dmi; 194 smb_header_t *hdr; 195 196 if (smbe_staddr == NULL) 197 return (i); 198 199 for (dmi = (char *)smbe_staddr; dmi < smbe_staddr + smbe_stlen; i++) { 200 hdr = (smb_header_t *)dmi; 201 dmi += hdr->smbh_len; 202 /* 203 * Search for the end of the string area. 204 */ 205 while (dmi + 1 < smbe_staddr + smbe_stlen && 206 dmi[0] != '\0' && dmi[1] != '\0') { 207 dmi++; 208 } 209 dmi += 2; 210 } 211 return (i); 212 } 213 214 smbios_hdl_t * 215 smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len, 216 int version, int flags, int *errp) 217 { 218 smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t)); 219 const smb_header_t *hp, *nhp; 220 const uchar_t *p, *q, *s; 221 uint_t i, h; 222 int err; 223 224 switch (version) { 225 case SMB_VERSION_23: 226 case SMB_VERSION_24: 227 case SMB_VERSION_25: 228 case SMB_VERSION_26: 229 case SMB_VERSION_27: 230 case SMB_VERSION_28: 231 case SMB_VERSION_30: 232 case SMB_VERSION_31: 233 case SMB_VERSION_32: 234 case SMB_VERSION_33: 235 case SMB_VERSION_34: 236 case SMB_VERSION_35: 237 break; 238 default: 239 return (smb_open_error(shp, errp, ESMB_VERSION)); 240 } 241 242 if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK)) 243 return (smb_open_error(shp, errp, ESMB_INVAL)); 244 245 if (shp == NULL) 246 return (smb_open_error(shp, errp, ESMB_NOMEM)); 247 248 if (_smb_debug) 249 shp->sh_flags |= SMB_FL_DEBUG; 250 251 err = smbios_bufopen_21(shp, &ep->ep21, len, flags); 252 if (err != 0) { 253 err = smbios_bufopen_30(shp, &ep->ep30, len, flags); 254 if (err != 0) 255 return (smb_open_error(shp, errp, err)); 256 shp->sh_ent_stnum = 257 smbios_table_nentries(buf, ep->ep30.smbe_stlen); 258 } 259 260 shp->sh_buf = buf; 261 shp->sh_buflen = len; 262 shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * shp->sh_ent_stnum); 263 shp->sh_nstructs = 0; 264 shp->sh_hashlen = _smb_hashlen; 265 shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen); 266 shp->sh_libvers = version; 267 268 if (shp->sh_structs == NULL || shp->sh_hash == NULL) 269 return (smb_open_error(shp, errp, ESMB_NOMEM)); 270 271 hp = shp->sh_buf; 272 switch (shp->sh_ent_type) { 273 case SMBIOS_ENTRY_POINT_21: 274 q = (const uchar_t *)buf + MIN(ep->ep21.smbe_stlen, len); 275 break; 276 case SMBIOS_ENTRY_POINT_30: 277 q = (const uchar_t *)buf + MIN(ep->ep30.smbe_stlen, len); 278 break; 279 default: 280 return (smb_open_error(shp, errp, ESMB_VERSION)); 281 } 282 283 for (i = 0; i < shp->sh_ent_stnum; i++, hp = nhp) { 284 smb_struct_t *stp = &shp->sh_structs[i]; 285 uint_t n = 0; 286 287 if ((const uchar_t *)hp + sizeof (smb_header_t) > q) { 288 shp->sh_flags |= SMB_FL_TRUNC; 289 break; 290 } 291 292 smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n", 293 i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp); 294 295 if (hp->smbh_type == SMB_TYPE_EOT) 296 break; /* ignore any entries beyond end-of-table */ 297 298 if ((const uchar_t *)hp + hp->smbh_len > q - 2) { 299 shp->sh_flags |= SMB_FL_TRUNC; 300 break; 301 } 302 303 h = hp->smbh_hdl & (shp->sh_hashlen - 1); 304 p = s = (const uchar_t *)hp + hp->smbh_len; 305 306 while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) { 307 if (*p++ == '\0') 308 n++; /* count strings until \0\0 delimiter */ 309 } 310 311 if (p > q - 2) { 312 shp->sh_flags |= SMB_FL_TRUNC; 313 break; 314 } 315 316 if (p > s) 317 n++; /* add one for final string in string table */ 318 319 stp->smbst_hdr = hp; 320 stp->smbst_str = s; 321 stp->smbst_end = p; 322 stp->smbst_next = shp->sh_hash[h]; 323 stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n); 324 stp->smbst_strtablen = n; 325 326 if (n != 0 && stp->smbst_strtab == NULL) 327 return (smb_open_error(shp, errp, ESMB_NOMEM)); 328 329 shp->sh_hash[h] = stp; 330 nhp = (void *)(p + 2); 331 shp->sh_nstructs++; 332 333 for (n = 0, p = s; n < stp->smbst_strtablen; p++) { 334 if (*p == '\0') { 335 stp->smbst_strtab[n++] = 336 (uint16_t)(s - stp->smbst_str); 337 s = p + 1; 338 } 339 } 340 } 341 342 /* error out if we couldn't find any complete entries in the table */ 343 if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0) 344 return (smb_open_error(shp, errp, ESMB_CORRUPT)); 345 346 if (flags & SMB_O_ZIDS) 347 smb_strip(shp); 348 349 return (shp); 350 } 351 352 void 353 smbios_close(smbios_hdl_t *shp) 354 { 355 uint_t i; 356 357 for (i = 0; i < shp->sh_nstructs; i++) { 358 smb_free(shp->sh_structs[i].smbst_strtab, 359 sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen); 360 } 361 362 smb_free(shp->sh_structs, sizeof (smb_struct_t) * shp->sh_ent_stnum); 363 smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen); 364 365 if (shp->sh_flags & SMB_FL_BUFALLOC) 366 smb_free((void *)shp->sh_buf, shp->sh_buflen); 367 368 smb_free(shp, sizeof (smbios_hdl_t)); 369 } 370 371 /* 372 * Recompute the values of the entry point checksums based upon the content 373 * of the specified SMBIOS entry point. We don't need 'shp' but require it 374 * anyway in case future versioning requires variations in the algorithm. 375 */ 376 /*ARGSUSED*/ 377 void 378 smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep) 379 { 380 uchar_t *p, *q = (uchar_t *)ep; 381 uint8_t esum = 0, isum = 0; 382 383 switch (shp->sh_ent_type) { 384 case SMBIOS_ENTRY_POINT_21: 385 ep->ep21.smbe_ecksum = ep->ep21.smbe_icksum = 0; 386 387 for (p = (uchar_t *)ep->ep21.smbe_ianchor; 388 p < q + sizeof (*ep); p++) { 389 isum += *p; 390 } 391 392 ep->ep21.smbe_icksum = -isum; 393 394 for (p = q; p < q + ep->ep21.smbe_elen; p++) 395 esum += *p; 396 397 ep->ep21.smbe_ecksum = -esum; 398 break; 399 case SMBIOS_ENTRY_POINT_30: 400 ep->ep30.smbe_ecksum = 0; 401 for (p = q; p < q + ep->ep30.smbe_elen; p++) 402 esum += *p; 403 404 ep->ep30.smbe_ecksum = -esum; 405 break; 406 default: 407 break; 408 } 409 } 410 411 const void * 412 smbios_buf(smbios_hdl_t *shp) 413 { 414 return (shp->sh_buf); 415 } 416 417 size_t 418 smbios_buflen(smbios_hdl_t *shp) 419 { 420 return (shp->sh_buflen); 421 } 422 423 static smbios_struct_t * 424 smb_export(const smb_struct_t *stp, smbios_struct_t *sp) 425 { 426 const smb_header_t *hdr = stp->smbst_hdr; 427 428 sp->smbstr_id = hdr->smbh_hdl; 429 sp->smbstr_type = hdr->smbh_type; 430 sp->smbstr_data = hdr; 431 sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr); 432 433 return (sp); 434 } 435 436 int 437 smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp) 438 { 439 const smb_struct_t *stp = smb_lookup_id(shp, id); 440 441 if (stp == NULL) 442 return (-1); /* errno is set for us */ 443 444 if (sp != NULL) 445 (void) smb_export(stp, sp); 446 447 return (0); 448 } 449 450 int 451 smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp) 452 { 453 const smb_struct_t *stp = smb_lookup_type(shp, type); 454 455 if (stp == NULL) 456 return (-1); /* errno is set for us */ 457 458 if (sp != NULL) 459 (void) smb_export(stp, sp); 460 461 return (0); 462 } 463 464 int 465 smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data) 466 { 467 const smb_struct_t *sp = shp->sh_structs; 468 smbios_struct_t s; 469 int i, rv = 0; 470 471 for (i = 0; i < shp->sh_nstructs; i++, sp++) { 472 if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE && 473 (rv = func(shp, smb_export(sp, &s), data)) != 0) 474 break; 475 } 476 477 return (rv); 478 } 479 480 const smb_struct_t * 481 smb_lookup_type(smbios_hdl_t *shp, uint_t type) 482 { 483 uint_t i; 484 485 for (i = 0; i < shp->sh_nstructs; i++) { 486 if (shp->sh_structs[i].smbst_hdr->smbh_type == type) 487 return (&shp->sh_structs[i]); 488 } 489 490 (void) smb_set_errno(shp, ESMB_NOENT); 491 return (NULL); 492 } 493 494 const smb_struct_t * 495 smb_lookup_id(smbios_hdl_t *shp, uint_t id) 496 { 497 const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)]; 498 499 switch (id) { 500 case SMB_ID_NOTSUP: 501 (void) smb_set_errno(shp, ESMB_NOTSUP); 502 return (NULL); 503 case SMB_ID_NONE: 504 (void) smb_set_errno(shp, ESMB_NOENT); 505 return (NULL); 506 } 507 508 for (; stp != NULL; stp = stp->smbst_next) { 509 if (stp->smbst_hdr->smbh_hdl == id) 510 break; 511 } 512 513 if (stp == NULL) 514 (void) smb_set_errno(shp, ESMB_NOENT); 515 516 return (stp); 517 } 518 519 const char * 520 smb_strptr(const smb_struct_t *stp, uint_t i) 521 { 522 if (i == 0 || i > stp->smbst_strtablen) 523 return (_smb_emptystr); 524 else 525 return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]); 526 } 527 528 int 529 smb_libgteq(smbios_hdl_t *shp, int version) 530 { 531 return (SMB_MAJOR(shp->sh_libvers) > SMB_MAJOR(version) || ( 532 SMB_MAJOR(shp->sh_libvers) == SMB_MAJOR(version) && 533 SMB_MINOR(shp->sh_libvers) >= SMB_MINOR(version))); 534 } 535 536 int 537 smb_gteq(smbios_hdl_t *shp, int version) 538 { 539 return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || ( 540 SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) && 541 SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version))); 542 } 543 544 boolean_t 545 smbios_truncated(smbios_hdl_t *shp) 546 { 547 return ((shp->sh_flags & SMB_FL_TRUNC) != 0); 548 } 549