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