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 2025 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 case SMB_VERSION_36: 238 case SMB_VERSION_37: 239 case SMB_VERSION_38: 240 case SMB_VERSION_39: 241 break; 242 default: 243 return (smb_open_error(shp, errp, ESMB_VERSION)); 244 } 245 246 if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK)) 247 return (smb_open_error(shp, errp, ESMB_INVAL)); 248 249 if (shp == NULL) 250 return (smb_open_error(shp, errp, ESMB_NOMEM)); 251 252 if (_smb_debug) 253 shp->sh_flags |= SMB_FL_DEBUG; 254 255 err = smbios_bufopen_21(shp, &ep->ep21, len, flags); 256 if (err != 0) { 257 err = smbios_bufopen_30(shp, &ep->ep30, len, flags); 258 if (err != 0) 259 return (smb_open_error(shp, errp, err)); 260 shp->sh_ent_stnum = 261 smbios_table_nentries(buf, ep->ep30.smbe_stlen); 262 } 263 264 shp->sh_buf = buf; 265 shp->sh_buflen = len; 266 shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * shp->sh_ent_stnum); 267 shp->sh_nstructs = 0; 268 shp->sh_hashlen = _smb_hashlen; 269 shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen); 270 shp->sh_libvers = version; 271 272 if (shp->sh_structs == NULL || shp->sh_hash == NULL) 273 return (smb_open_error(shp, errp, ESMB_NOMEM)); 274 275 hp = shp->sh_buf; 276 switch (shp->sh_ent_type) { 277 case SMBIOS_ENTRY_POINT_21: 278 q = (const uchar_t *)buf + MIN(ep->ep21.smbe_stlen, len); 279 break; 280 case SMBIOS_ENTRY_POINT_30: 281 q = (const uchar_t *)buf + MIN(ep->ep30.smbe_stlen, len); 282 break; 283 default: 284 return (smb_open_error(shp, errp, ESMB_VERSION)); 285 } 286 287 for (i = 0; i < shp->sh_ent_stnum; i++, hp = nhp) { 288 smb_struct_t *stp = &shp->sh_structs[i]; 289 uint_t n = 0; 290 291 if ((const uchar_t *)hp + sizeof (smb_header_t) > q) { 292 shp->sh_flags |= SMB_FL_TRUNC; 293 break; 294 } 295 296 smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n", 297 i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp); 298 299 if (hp->smbh_type == SMB_TYPE_EOT) 300 break; /* ignore any entries beyond end-of-table */ 301 302 if ((const uchar_t *)hp + hp->smbh_len > q - 2) { 303 shp->sh_flags |= SMB_FL_TRUNC; 304 break; 305 } 306 307 h = hp->smbh_hdl & (shp->sh_hashlen - 1); 308 p = s = (const uchar_t *)hp + hp->smbh_len; 309 310 while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) { 311 if (*p++ == '\0') 312 n++; /* count strings until \0\0 delimiter */ 313 } 314 315 if (p > q - 2) { 316 shp->sh_flags |= SMB_FL_TRUNC; 317 break; 318 } 319 320 if (p > s) 321 n++; /* add one for final string in string table */ 322 323 stp->smbst_hdr = hp; 324 stp->smbst_str = s; 325 stp->smbst_end = p; 326 stp->smbst_next = shp->sh_hash[h]; 327 stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n); 328 stp->smbst_strtablen = n; 329 330 if (n != 0 && stp->smbst_strtab == NULL) 331 return (smb_open_error(shp, errp, ESMB_NOMEM)); 332 333 shp->sh_hash[h] = stp; 334 nhp = (void *)(p + 2); 335 shp->sh_nstructs++; 336 337 for (n = 0, p = s; n < stp->smbst_strtablen; p++) { 338 if (*p == '\0') { 339 stp->smbst_strtab[n++] = 340 (uint16_t)(s - stp->smbst_str); 341 s = p + 1; 342 } 343 } 344 } 345 346 /* error out if we couldn't find any complete entries in the table */ 347 if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0) 348 return (smb_open_error(shp, errp, ESMB_CORRUPT)); 349 350 if (flags & SMB_O_ZIDS) 351 smb_strip(shp); 352 353 return (shp); 354 } 355 356 void 357 smbios_close(smbios_hdl_t *shp) 358 { 359 uint_t i; 360 361 for (i = 0; i < shp->sh_nstructs; i++) { 362 smb_free(shp->sh_structs[i].smbst_strtab, 363 sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen); 364 } 365 366 smb_free(shp->sh_structs, sizeof (smb_struct_t) * shp->sh_ent_stnum); 367 smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen); 368 369 if (shp->sh_flags & SMB_FL_BUFALLOC) 370 smb_free((void *)shp->sh_buf, shp->sh_buflen); 371 372 smb_free(shp, sizeof (smbios_hdl_t)); 373 } 374 375 /* 376 * Recompute the values of the entry point checksums based upon the content 377 * of the specified SMBIOS entry point. We don't need 'shp' but require it 378 * anyway in case future versioning requires variations in the algorithm. 379 */ 380 /*ARGSUSED*/ 381 void 382 smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep) 383 { 384 uchar_t *p, *q = (uchar_t *)ep; 385 uint8_t esum = 0, isum = 0; 386 387 switch (shp->sh_ent_type) { 388 case SMBIOS_ENTRY_POINT_21: 389 ep->ep21.smbe_ecksum = ep->ep21.smbe_icksum = 0; 390 391 for (p = (uchar_t *)ep->ep21.smbe_ianchor; 392 p < q + sizeof (*ep); p++) { 393 isum += *p; 394 } 395 396 ep->ep21.smbe_icksum = -isum; 397 398 for (p = q; p < q + ep->ep21.smbe_elen; p++) 399 esum += *p; 400 401 ep->ep21.smbe_ecksum = -esum; 402 break; 403 case SMBIOS_ENTRY_POINT_30: 404 ep->ep30.smbe_ecksum = 0; 405 for (p = q; p < q + ep->ep30.smbe_elen; p++) 406 esum += *p; 407 408 ep->ep30.smbe_ecksum = -esum; 409 break; 410 default: 411 break; 412 } 413 } 414 415 const void * 416 smbios_buf(smbios_hdl_t *shp) 417 { 418 return (shp->sh_buf); 419 } 420 421 size_t 422 smbios_buflen(smbios_hdl_t *shp) 423 { 424 return (shp->sh_buflen); 425 } 426 427 static smbios_struct_t * 428 smb_export(const smb_struct_t *stp, smbios_struct_t *sp) 429 { 430 const smb_header_t *hdr = stp->smbst_hdr; 431 432 sp->smbstr_id = hdr->smbh_hdl; 433 sp->smbstr_type = hdr->smbh_type; 434 sp->smbstr_data = hdr; 435 sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr); 436 437 return (sp); 438 } 439 440 int 441 smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp) 442 { 443 const smb_struct_t *stp = smb_lookup_id(shp, id); 444 445 if (stp == NULL) 446 return (-1); /* errno is set for us */ 447 448 if (sp != NULL) 449 (void) smb_export(stp, sp); 450 451 return (0); 452 } 453 454 int 455 smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp) 456 { 457 const smb_struct_t *stp = smb_lookup_type(shp, type); 458 459 if (stp == NULL) 460 return (-1); /* errno is set for us */ 461 462 if (sp != NULL) 463 (void) smb_export(stp, sp); 464 465 return (0); 466 } 467 468 int 469 smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data) 470 { 471 const smb_struct_t *sp = shp->sh_structs; 472 smbios_struct_t s; 473 int i, rv = 0; 474 475 for (i = 0; i < shp->sh_nstructs; i++, sp++) { 476 if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE && 477 (rv = func(shp, smb_export(sp, &s), data)) != 0) 478 break; 479 } 480 481 return (rv); 482 } 483 484 const smb_struct_t * 485 smb_lookup_type(smbios_hdl_t *shp, uint_t type) 486 { 487 uint_t i; 488 489 for (i = 0; i < shp->sh_nstructs; i++) { 490 if (shp->sh_structs[i].smbst_hdr->smbh_type == type) 491 return (&shp->sh_structs[i]); 492 } 493 494 (void) smb_set_errno(shp, ESMB_NOENT); 495 return (NULL); 496 } 497 498 const smb_struct_t * 499 smb_lookup_id(smbios_hdl_t *shp, uint_t id) 500 { 501 const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)]; 502 503 switch (id) { 504 case SMB_ID_NOTSUP: 505 (void) smb_set_errno(shp, ESMB_NOTSUP); 506 return (NULL); 507 case SMB_ID_NONE: 508 (void) smb_set_errno(shp, ESMB_NOENT); 509 return (NULL); 510 } 511 512 for (; stp != NULL; stp = stp->smbst_next) { 513 if (stp->smbst_hdr->smbh_hdl == id) 514 break; 515 } 516 517 if (stp == NULL) 518 (void) smb_set_errno(shp, ESMB_NOENT); 519 520 return (stp); 521 } 522 523 const char * 524 smb_strptr(const smb_struct_t *stp, uint_t i) 525 { 526 if (i == 0 || i > stp->smbst_strtablen) 527 return (_smb_emptystr); 528 else 529 return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]); 530 } 531 532 boolean_t 533 smb_libgteq(smbios_hdl_t *shp, int version) 534 { 535 return (SMB_MAJOR(shp->sh_libvers) > SMB_MAJOR(version) || ( 536 SMB_MAJOR(shp->sh_libvers) == SMB_MAJOR(version) && 537 SMB_MINOR(shp->sh_libvers) >= SMB_MINOR(version))); 538 } 539 540 boolean_t 541 smb_gteq(smbios_hdl_t *shp, int version) 542 { 543 return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || ( 544 SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) && 545 SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version))); 546 } 547 548 boolean_t 549 smbios_truncated(smbios_hdl_t *shp) 550 { 551 return ((shp->sh_flags & SMB_FL_TRUNC) != 0); 552 } 553