184ab085aSmws /* 284ab085aSmws * CDDL HEADER START 384ab085aSmws * 484ab085aSmws * The contents of this file are subject to the terms of the 5074bb90dSTom Pothier * Common Development and Distribution License (the "License"). 6074bb90dSTom Pothier * You may not use this file except in compliance with the License. 784ab085aSmws * 884ab085aSmws * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 984ab085aSmws * or http://www.opensolaris.org/os/licensing. 1084ab085aSmws * See the License for the specific language governing permissions 1184ab085aSmws * and limitations under the License. 1284ab085aSmws * 1384ab085aSmws * When distributing Covered Code, include this CDDL HEADER in each 1484ab085aSmws * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1584ab085aSmws * If applicable, add the following below this CDDL HEADER, with the 1684ab085aSmws * fields enclosed by brackets "[]" replaced with your own identifying 1784ab085aSmws * information: Portions Copyright [yyyy] [name of copyright owner] 1884ab085aSmws * 1984ab085aSmws * CDDL HEADER END 2084ab085aSmws */ 2184ab085aSmws 2284ab085aSmws /* 234e901881SDale Ghent * Copyright 2015 OmniTI Computer Consulting, Inc. All rights reserved. 246734c4b0SRobert Mustacchi * Copyright 2015 Joyent, Inc. 25074bb90dSTom Pothier * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 2684ab085aSmws * Use is subject to license terms. 2784ab085aSmws */ 2884ab085aSmws 2984ab085aSmws #include <sys/smbios_impl.h> 3084ab085aSmws 3184ab085aSmws static const uint_t _smb_hashlen = 64; /* hash length (must be Pof2) */ 3284ab085aSmws static const char _smb_emptystr[] = ""; /* empty string to return */ 3384ab085aSmws int _smb_debug = 0; /* default debug mode */ 3484ab085aSmws 3584ab085aSmws /* 3684ab085aSmws * Strip out identification information for you privacy weenies. This is quite 3784ab085aSmws * simple using our smbios_info_common() abstraction: we just locate any serial 3884ab085aSmws * numbers and asset tags for each record, and then zero out those strings. 3984ab085aSmws * Then we must handle two special cases: SMB_TYPE_SYSTEM holds a 16-byte UUID 4084ab085aSmws * and SMB_TYPE_BATTERY stores a Smart Battery Data Spec 16-bit serial number. 4184ab085aSmws * We use a literal '0' rather than '\0' for zeroing strings because \0\0 in 4284ab085aSmws * the SMBIOS string table has a special meaning (denotes end-of-record). 4384ab085aSmws */ 4484ab085aSmws static void 4584ab085aSmws smb_strip(smbios_hdl_t *shp) 4684ab085aSmws { 4784ab085aSmws uint_t i; 4884ab085aSmws 4984ab085aSmws for (i = 0; i < shp->sh_nstructs; i++) { 5084ab085aSmws const smb_header_t *hp = shp->sh_structs[i].smbst_hdr; 5184ab085aSmws smbios_info_t info; 5284ab085aSmws char *p; 5384ab085aSmws 5484ab085aSmws if (hp->smbh_type == SMB_TYPE_SYSTEM && 5584ab085aSmws hp->smbh_len >= offsetof(smb_system_t, smbsi_wakeup)) { 5684ab085aSmws smb_system_t *sp = (smb_system_t *)(uintptr_t)hp; 5784ab085aSmws bzero(sp->smbsi_uuid, sizeof (sp->smbsi_uuid)); 5884ab085aSmws } 5984ab085aSmws 6084ab085aSmws if (hp->smbh_type == SMB_TYPE_BATTERY && 6184ab085aSmws hp->smbh_len >= offsetof(smb_battery_t, smbbat_sdate)) { 6284ab085aSmws smb_battery_t *bp = (smb_battery_t *)(uintptr_t)hp; 6384ab085aSmws bp->smbbat_ssn = 0; 6484ab085aSmws } 6584ab085aSmws 6684ab085aSmws if (smbios_info_common(shp, hp->smbh_hdl, &info) != SMB_ERR) { 6784ab085aSmws for (p = (char *)info.smbi_serial; *p != '\0'; p++) 6884ab085aSmws *p = '0'; 6984ab085aSmws for (p = (char *)info.smbi_asset; *p != '\0'; p++) 7084ab085aSmws *p = '0'; 7184ab085aSmws } 7284ab085aSmws } 7384ab085aSmws } 7484ab085aSmws 7584ab085aSmws smbios_hdl_t * 7684ab085aSmws smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len, 7784ab085aSmws int version, int flags, int *errp) 7884ab085aSmws { 7984ab085aSmws smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t)); 8084ab085aSmws const smb_header_t *hp, *nhp; 8184ab085aSmws const uchar_t *p, *q, *s; 8284ab085aSmws uint_t i, h; 8384ab085aSmws 8484ab085aSmws switch (version) { 8584ab085aSmws case SMB_VERSION_23: 8684ab085aSmws case SMB_VERSION_24: 874e901881SDale Ghent case SMB_VERSION_25: 884e901881SDale Ghent case SMB_VERSION_26: 894e901881SDale Ghent case SMB_VERSION_27: 904e901881SDale Ghent case SMB_VERSION_28: 916734c4b0SRobert Mustacchi case SMB_VERSION_30: 9284ab085aSmws break; 9384ab085aSmws default: 9484ab085aSmws return (smb_open_error(shp, errp, ESMB_VERSION)); 9584ab085aSmws } 9684ab085aSmws 9784ab085aSmws if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK)) 9884ab085aSmws return (smb_open_error(shp, errp, ESMB_INVAL)); 9984ab085aSmws 10084ab085aSmws if (shp == NULL) 10184ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM)); 10284ab085aSmws 10384ab085aSmws if (_smb_debug) 10484ab085aSmws shp->sh_flags |= SMB_FL_DEBUG; 10584ab085aSmws 10684ab085aSmws if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN)) 10784ab085aSmws return (smb_open_error(shp, errp, ESMB_HEADER)); 10884ab085aSmws 10984ab085aSmws if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN)) 11084ab085aSmws return (smb_open_error(shp, errp, ESMB_HEADER)); 11184ab085aSmws 11284ab085aSmws smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n", 11384ab085aSmws ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev); 11484ab085aSmws 11584ab085aSmws if (!(flags & SMB_O_NOVERS)) { 11684ab085aSmws if (ep->smbe_major > SMB_MAJOR(SMB_VERSION)) 11784ab085aSmws return (smb_open_error(shp, errp, ESMB_NEW)); 11884ab085aSmws 11984ab085aSmws if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || ( 12084ab085aSmws ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) && 12184ab085aSmws ep->smbe_minor < SMB_MINOR(SMB_VERSION_23))) 12284ab085aSmws return (smb_open_error(shp, errp, ESMB_OLD)); 12384ab085aSmws } 12484ab085aSmws 12584ab085aSmws if (len < sizeof (smb_header_t) || 12684ab085aSmws ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen) 12784ab085aSmws return (smb_open_error(shp, errp, ESMB_SHORT)); 12884ab085aSmws 12984ab085aSmws if (!(flags & SMB_O_NOCKSUM)) { 13084ab085aSmws uint8_t esum = 0, isum = 0; 13184ab085aSmws q = (uchar_t *)ep; 13284ab085aSmws 13384ab085aSmws for (p = q; p < q + ep->smbe_elen; p++) 13484ab085aSmws esum += *p; 13584ab085aSmws 13684ab085aSmws for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++) 13784ab085aSmws isum += *p; 13884ab085aSmws 13984ab085aSmws if (esum != 0 || isum != 0) { 14084ab085aSmws smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum); 14184ab085aSmws return (smb_open_error(shp, errp, ESMB_CKSUM)); 14284ab085aSmws } 14384ab085aSmws } 14484ab085aSmws 145e4586ebfSmws /* 146e4586ebfSmws * Copy the entry point into our handle. The underlying entry point 147e4586ebfSmws * may be larger than our structure definition, so reset smbe_elen 148e4586ebfSmws * to our internal size and recompute good checksums for our copy. 149e4586ebfSmws */ 15084ab085aSmws bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t)); 151e4586ebfSmws shp->sh_ent.smbe_elen = sizeof (smbios_entry_t); 152e4586ebfSmws smbios_checksum(shp, &shp->sh_ent); 153e4586ebfSmws 15484ab085aSmws shp->sh_buf = buf; 15584ab085aSmws shp->sh_buflen = len; 15684ab085aSmws shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * ep->smbe_stnum); 15784ab085aSmws shp->sh_nstructs = 0; 15884ab085aSmws shp->sh_hashlen = _smb_hashlen; 15984ab085aSmws shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen); 16084ab085aSmws shp->sh_libvers = version; 16184ab085aSmws shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor); 16284ab085aSmws 16384ab085aSmws if (shp->sh_structs == NULL || shp->sh_hash == NULL) 16484ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM)); 16584ab085aSmws 16684ab085aSmws hp = shp->sh_buf; 16784ab085aSmws q = (const uchar_t *)buf + MIN(ep->smbe_stlen, len); 16884ab085aSmws 16984ab085aSmws for (i = 0; i < ep->smbe_stnum; i++, hp = nhp) { 17084ab085aSmws smb_struct_t *stp = &shp->sh_structs[i]; 17184ab085aSmws uint_t n = 0; 17284ab085aSmws 173*b60ae21dSJonathan Matthew if ((const uchar_t *)hp + sizeof (smb_header_t) > q) { 174*b60ae21dSJonathan Matthew shp->sh_flags |= SMB_FL_TRUNC; 175*b60ae21dSJonathan Matthew break; 176*b60ae21dSJonathan Matthew } 17784ab085aSmws 17884ab085aSmws smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n", 17984ab085aSmws i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp); 18084ab085aSmws 18184ab085aSmws if (hp->smbh_type == SMB_TYPE_EOT) 18284ab085aSmws break; /* ignore any entries beyond end-of-table */ 18384ab085aSmws 184*b60ae21dSJonathan Matthew if ((const uchar_t *)hp + hp->smbh_len > q - 2) { 185*b60ae21dSJonathan Matthew shp->sh_flags |= SMB_FL_TRUNC; 186*b60ae21dSJonathan Matthew break; 187*b60ae21dSJonathan Matthew } 188e4586ebfSmws 18984ab085aSmws h = hp->smbh_hdl & (shp->sh_hashlen - 1); 19084ab085aSmws p = s = (const uchar_t *)hp + hp->smbh_len; 19184ab085aSmws 19284ab085aSmws while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) { 19384ab085aSmws if (*p++ == '\0') 19484ab085aSmws n++; /* count strings until \0\0 delimiter */ 19584ab085aSmws } 19684ab085aSmws 197*b60ae21dSJonathan Matthew if (p > q - 2) { 198*b60ae21dSJonathan Matthew shp->sh_flags |= SMB_FL_TRUNC; 199*b60ae21dSJonathan Matthew break; 200*b60ae21dSJonathan Matthew } 20184ab085aSmws 20284ab085aSmws if (p > s) 20384ab085aSmws n++; /* add one for final string in string table */ 20484ab085aSmws 20584ab085aSmws stp->smbst_hdr = hp; 20684ab085aSmws stp->smbst_str = s; 20784ab085aSmws stp->smbst_end = p; 20884ab085aSmws stp->smbst_next = shp->sh_hash[h]; 20984ab085aSmws stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n); 21084ab085aSmws stp->smbst_strtablen = n; 21184ab085aSmws 21284ab085aSmws if (n != 0 && stp->smbst_strtab == NULL) 21384ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM)); 21484ab085aSmws 21584ab085aSmws shp->sh_hash[h] = stp; 21684ab085aSmws nhp = (void *)(p + 2); 21784ab085aSmws shp->sh_nstructs++; 21884ab085aSmws 21984ab085aSmws for (n = 0, p = s; n < stp->smbst_strtablen; p++) { 22084ab085aSmws if (*p == '\0') { 22184ab085aSmws stp->smbst_strtab[n++] = 22284ab085aSmws (uint16_t)(s - stp->smbst_str); 22384ab085aSmws s = p + 1; 22484ab085aSmws } 22584ab085aSmws } 22684ab085aSmws } 22784ab085aSmws 228*b60ae21dSJonathan Matthew /* error out if we couldn't find any complete entries in the table */ 229*b60ae21dSJonathan Matthew if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0) 230*b60ae21dSJonathan Matthew return (smb_open_error(shp, errp, ESMB_CORRUPT)); 231*b60ae21dSJonathan Matthew 23284ab085aSmws if (flags & SMB_O_ZIDS) 23384ab085aSmws smb_strip(shp); 23484ab085aSmws 23584ab085aSmws return (shp); 23684ab085aSmws } 23784ab085aSmws 23884ab085aSmws void 23984ab085aSmws smbios_close(smbios_hdl_t *shp) 24084ab085aSmws { 24184ab085aSmws const smbios_entry_t *ep = &shp->sh_ent; 24284ab085aSmws uint_t i; 24384ab085aSmws 24484ab085aSmws for (i = 0; i < shp->sh_nstructs; i++) { 24584ab085aSmws smb_free(shp->sh_structs[i].smbst_strtab, 24684ab085aSmws sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen); 24784ab085aSmws } 24884ab085aSmws 24984ab085aSmws smb_free(shp->sh_structs, sizeof (smb_struct_t) * ep->smbe_stnum); 25084ab085aSmws smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen); 25184ab085aSmws 25284ab085aSmws if (shp->sh_flags & SMB_FL_BUFALLOC) 25384ab085aSmws smb_free((void *)shp->sh_buf, shp->sh_buflen); 25484ab085aSmws 25584ab085aSmws smb_free(shp, sizeof (smbios_hdl_t)); 25684ab085aSmws } 25784ab085aSmws 25884ab085aSmws /* 25984ab085aSmws * Recompute the values of the entry point checksums based upon the content 26084ab085aSmws * of the specified SMBIOS entry point. We don't need 'shp' but require it 26184ab085aSmws * anyway in case future versioning requires variations in the algorithm. 26284ab085aSmws */ 26384ab085aSmws /*ARGSUSED*/ 26484ab085aSmws void 26584ab085aSmws smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep) 26684ab085aSmws { 26784ab085aSmws uchar_t *p, *q = (uchar_t *)ep; 26884ab085aSmws uint8_t esum = 0, isum = 0; 26984ab085aSmws 27084ab085aSmws ep->smbe_ecksum = ep->smbe_icksum = 0; 27184ab085aSmws 27284ab085aSmws for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++) 27384ab085aSmws isum += *p; 27484ab085aSmws 27584ab085aSmws ep->smbe_icksum = -isum; 27684ab085aSmws 27784ab085aSmws for (p = q; p < q + ep->smbe_elen; p++) 27884ab085aSmws esum += *p; 27984ab085aSmws 28084ab085aSmws ep->smbe_ecksum = -esum; 28184ab085aSmws } 28284ab085aSmws 28384ab085aSmws const void * 28484ab085aSmws smbios_buf(smbios_hdl_t *shp) 28584ab085aSmws { 28684ab085aSmws return (shp->sh_buf); 28784ab085aSmws } 28884ab085aSmws 28984ab085aSmws size_t 29084ab085aSmws smbios_buflen(smbios_hdl_t *shp) 29184ab085aSmws { 29284ab085aSmws return (shp->sh_buflen); 29384ab085aSmws } 29484ab085aSmws 29584ab085aSmws static smbios_struct_t * 29684ab085aSmws smb_export(const smb_struct_t *stp, smbios_struct_t *sp) 29784ab085aSmws { 29884ab085aSmws const smb_header_t *hdr = stp->smbst_hdr; 29984ab085aSmws 30084ab085aSmws sp->smbstr_id = hdr->smbh_hdl; 30184ab085aSmws sp->smbstr_type = hdr->smbh_type; 30284ab085aSmws sp->smbstr_data = hdr; 30384ab085aSmws sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr); 30484ab085aSmws 30584ab085aSmws return (sp); 30684ab085aSmws } 30784ab085aSmws 30884ab085aSmws int 30984ab085aSmws smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp) 31084ab085aSmws { 31184ab085aSmws const smb_struct_t *stp = smb_lookup_id(shp, id); 31284ab085aSmws 31384ab085aSmws if (stp == NULL) 31484ab085aSmws return (-1); /* errno is set for us */ 31584ab085aSmws 31684ab085aSmws if (sp != NULL) 31784ab085aSmws (void) smb_export(stp, sp); 31884ab085aSmws 31984ab085aSmws return (0); 32084ab085aSmws } 32184ab085aSmws 32284ab085aSmws int 323074bb90dSTom Pothier smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp) 324074bb90dSTom Pothier { 325074bb90dSTom Pothier const smb_struct_t *stp = smb_lookup_type(shp, type); 326074bb90dSTom Pothier 327074bb90dSTom Pothier if (stp == NULL) 328074bb90dSTom Pothier return (-1); /* errno is set for us */ 329074bb90dSTom Pothier 330074bb90dSTom Pothier if (sp != NULL) 331074bb90dSTom Pothier (void) smb_export(stp, sp); 332074bb90dSTom Pothier 333074bb90dSTom Pothier return (0); 334074bb90dSTom Pothier } 335074bb90dSTom Pothier 336074bb90dSTom Pothier int 33784ab085aSmws smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data) 33884ab085aSmws { 33984ab085aSmws const smb_struct_t *sp = shp->sh_structs; 34084ab085aSmws smbios_struct_t s; 34184ab085aSmws int i, rv = 0; 34284ab085aSmws 34384ab085aSmws for (i = 0; i < shp->sh_nstructs; i++, sp++) { 34484ab085aSmws if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE && 34584ab085aSmws (rv = func(shp, smb_export(sp, &s), data)) != 0) 34684ab085aSmws break; 34784ab085aSmws } 34884ab085aSmws 34984ab085aSmws return (rv); 35084ab085aSmws } 35184ab085aSmws 35284ab085aSmws const smb_struct_t * 35384ab085aSmws smb_lookup_type(smbios_hdl_t *shp, uint_t type) 35484ab085aSmws { 35584ab085aSmws uint_t i; 35684ab085aSmws 35784ab085aSmws for (i = 0; i < shp->sh_nstructs; i++) { 35884ab085aSmws if (shp->sh_structs[i].smbst_hdr->smbh_type == type) 35984ab085aSmws return (&shp->sh_structs[i]); 36084ab085aSmws } 36184ab085aSmws 36284ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT); 36384ab085aSmws return (NULL); 36484ab085aSmws } 36584ab085aSmws 36684ab085aSmws const smb_struct_t * 36784ab085aSmws smb_lookup_id(smbios_hdl_t *shp, uint_t id) 36884ab085aSmws { 36984ab085aSmws const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)]; 37084ab085aSmws 37184ab085aSmws switch (id) { 37284ab085aSmws case SMB_ID_NOTSUP: 37384ab085aSmws (void) smb_set_errno(shp, ESMB_NOTSUP); 37484ab085aSmws return (NULL); 37584ab085aSmws case SMB_ID_NONE: 37684ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT); 37784ab085aSmws return (NULL); 37884ab085aSmws } 37984ab085aSmws 38084ab085aSmws for (; stp != NULL; stp = stp->smbst_next) { 38184ab085aSmws if (stp->smbst_hdr->smbh_hdl == id) 38284ab085aSmws break; 38384ab085aSmws } 38484ab085aSmws 38584ab085aSmws if (stp == NULL) 38684ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT); 38784ab085aSmws 38884ab085aSmws return (stp); 38984ab085aSmws } 39084ab085aSmws 39184ab085aSmws const char * 39284ab085aSmws smb_strptr(const smb_struct_t *stp, uint_t i) 39384ab085aSmws { 39484ab085aSmws if (i == 0 || i > stp->smbst_strtablen) 39584ab085aSmws return (_smb_emptystr); 39684ab085aSmws else 39784ab085aSmws return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]); 39884ab085aSmws } 39984ab085aSmws 40084ab085aSmws int 40184ab085aSmws smb_gteq(smbios_hdl_t *shp, int version) 40284ab085aSmws { 40384ab085aSmws return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || ( 40484ab085aSmws SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) && 40584ab085aSmws SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version))); 40684ab085aSmws } 407*b60ae21dSJonathan Matthew 408*b60ae21dSJonathan Matthew boolean_t 409*b60ae21dSJonathan Matthew smbios_truncated(smbios_hdl_t *shp) 410*b60ae21dSJonathan Matthew { 411*b60ae21dSJonathan Matthew return ((shp->sh_flags & SMB_FL_TRUNC) != 0); 412*b60ae21dSJonathan Matthew } 413