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 /* 23*4e901881SDale Ghent * Copyright 2015 OmniTI Computer Consulting, Inc. All rights reserved. 24074bb90dSTom Pothier * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 2584ab085aSmws * Use is subject to license terms. 2684ab085aSmws */ 2784ab085aSmws 2884ab085aSmws #include <sys/smbios_impl.h> 2984ab085aSmws 3084ab085aSmws static const uint_t _smb_hashlen = 64; /* hash length (must be Pof2) */ 3184ab085aSmws static const char _smb_emptystr[] = ""; /* empty string to return */ 3284ab085aSmws int _smb_debug = 0; /* default debug mode */ 3384ab085aSmws 3484ab085aSmws /* 3584ab085aSmws * Strip out identification information for you privacy weenies. This is quite 3684ab085aSmws * simple using our smbios_info_common() abstraction: we just locate any serial 3784ab085aSmws * numbers and asset tags for each record, and then zero out those strings. 3884ab085aSmws * Then we must handle two special cases: SMB_TYPE_SYSTEM holds a 16-byte UUID 3984ab085aSmws * and SMB_TYPE_BATTERY stores a Smart Battery Data Spec 16-bit serial number. 4084ab085aSmws * We use a literal '0' rather than '\0' for zeroing strings because \0\0 in 4184ab085aSmws * the SMBIOS string table has a special meaning (denotes end-of-record). 4284ab085aSmws */ 4384ab085aSmws static void 4484ab085aSmws smb_strip(smbios_hdl_t *shp) 4584ab085aSmws { 4684ab085aSmws uint_t i; 4784ab085aSmws 4884ab085aSmws for (i = 0; i < shp->sh_nstructs; i++) { 4984ab085aSmws const smb_header_t *hp = shp->sh_structs[i].smbst_hdr; 5084ab085aSmws smbios_info_t info; 5184ab085aSmws char *p; 5284ab085aSmws 5384ab085aSmws if (hp->smbh_type == SMB_TYPE_SYSTEM && 5484ab085aSmws hp->smbh_len >= offsetof(smb_system_t, smbsi_wakeup)) { 5584ab085aSmws smb_system_t *sp = (smb_system_t *)(uintptr_t)hp; 5684ab085aSmws bzero(sp->smbsi_uuid, sizeof (sp->smbsi_uuid)); 5784ab085aSmws } 5884ab085aSmws 5984ab085aSmws if (hp->smbh_type == SMB_TYPE_BATTERY && 6084ab085aSmws hp->smbh_len >= offsetof(smb_battery_t, smbbat_sdate)) { 6184ab085aSmws smb_battery_t *bp = (smb_battery_t *)(uintptr_t)hp; 6284ab085aSmws bp->smbbat_ssn = 0; 6384ab085aSmws } 6484ab085aSmws 6584ab085aSmws if (smbios_info_common(shp, hp->smbh_hdl, &info) != SMB_ERR) { 6684ab085aSmws for (p = (char *)info.smbi_serial; *p != '\0'; p++) 6784ab085aSmws *p = '0'; 6884ab085aSmws for (p = (char *)info.smbi_asset; *p != '\0'; p++) 6984ab085aSmws *p = '0'; 7084ab085aSmws } 7184ab085aSmws } 7284ab085aSmws } 7384ab085aSmws 7484ab085aSmws smbios_hdl_t * 7584ab085aSmws smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len, 7684ab085aSmws int version, int flags, int *errp) 7784ab085aSmws { 7884ab085aSmws smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t)); 7984ab085aSmws const smb_header_t *hp, *nhp; 8084ab085aSmws const uchar_t *p, *q, *s; 8184ab085aSmws uint_t i, h; 8284ab085aSmws 8384ab085aSmws switch (version) { 8484ab085aSmws case SMB_VERSION_23: 8584ab085aSmws case SMB_VERSION_24: 86*4e901881SDale Ghent case SMB_VERSION_25: 87*4e901881SDale Ghent case SMB_VERSION_26: 88*4e901881SDale Ghent case SMB_VERSION_27: 89*4e901881SDale Ghent case SMB_VERSION_28: 9084ab085aSmws break; 9184ab085aSmws default: 9284ab085aSmws return (smb_open_error(shp, errp, ESMB_VERSION)); 9384ab085aSmws } 9484ab085aSmws 9584ab085aSmws if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK)) 9684ab085aSmws return (smb_open_error(shp, errp, ESMB_INVAL)); 9784ab085aSmws 9884ab085aSmws if (shp == NULL) 9984ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM)); 10084ab085aSmws 10184ab085aSmws if (_smb_debug) 10284ab085aSmws shp->sh_flags |= SMB_FL_DEBUG; 10384ab085aSmws 10484ab085aSmws if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN)) 10584ab085aSmws return (smb_open_error(shp, errp, ESMB_HEADER)); 10684ab085aSmws 10784ab085aSmws if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN)) 10884ab085aSmws return (smb_open_error(shp, errp, ESMB_HEADER)); 10984ab085aSmws 11084ab085aSmws smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n", 11184ab085aSmws ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev); 11284ab085aSmws 11384ab085aSmws if (!(flags & SMB_O_NOVERS)) { 11484ab085aSmws if (ep->smbe_major > SMB_MAJOR(SMB_VERSION)) 11584ab085aSmws return (smb_open_error(shp, errp, ESMB_NEW)); 11684ab085aSmws 11784ab085aSmws if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || ( 11884ab085aSmws ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) && 11984ab085aSmws ep->smbe_minor < SMB_MINOR(SMB_VERSION_23))) 12084ab085aSmws return (smb_open_error(shp, errp, ESMB_OLD)); 12184ab085aSmws } 12284ab085aSmws 12384ab085aSmws if (len < sizeof (smb_header_t) || 12484ab085aSmws ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen) 12584ab085aSmws return (smb_open_error(shp, errp, ESMB_SHORT)); 12684ab085aSmws 12784ab085aSmws if (!(flags & SMB_O_NOCKSUM)) { 12884ab085aSmws uint8_t esum = 0, isum = 0; 12984ab085aSmws q = (uchar_t *)ep; 13084ab085aSmws 13184ab085aSmws for (p = q; p < q + ep->smbe_elen; p++) 13284ab085aSmws esum += *p; 13384ab085aSmws 13484ab085aSmws for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++) 13584ab085aSmws isum += *p; 13684ab085aSmws 13784ab085aSmws if (esum != 0 || isum != 0) { 13884ab085aSmws smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum); 13984ab085aSmws return (smb_open_error(shp, errp, ESMB_CKSUM)); 14084ab085aSmws } 14184ab085aSmws } 14284ab085aSmws 143e4586ebfSmws /* 144e4586ebfSmws * Copy the entry point into our handle. The underlying entry point 145e4586ebfSmws * may be larger than our structure definition, so reset smbe_elen 146e4586ebfSmws * to our internal size and recompute good checksums for our copy. 147e4586ebfSmws */ 14884ab085aSmws bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t)); 149e4586ebfSmws shp->sh_ent.smbe_elen = sizeof (smbios_entry_t); 150e4586ebfSmws smbios_checksum(shp, &shp->sh_ent); 151e4586ebfSmws 15284ab085aSmws shp->sh_buf = buf; 15384ab085aSmws shp->sh_buflen = len; 15484ab085aSmws shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * ep->smbe_stnum); 15584ab085aSmws shp->sh_nstructs = 0; 15684ab085aSmws shp->sh_hashlen = _smb_hashlen; 15784ab085aSmws shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen); 15884ab085aSmws shp->sh_libvers = version; 15984ab085aSmws shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor); 16084ab085aSmws 16184ab085aSmws if (shp->sh_structs == NULL || shp->sh_hash == NULL) 16284ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM)); 16384ab085aSmws 16484ab085aSmws hp = shp->sh_buf; 16584ab085aSmws q = (const uchar_t *)buf + MIN(ep->smbe_stlen, len); 16684ab085aSmws 16784ab085aSmws for (i = 0; i < ep->smbe_stnum; i++, hp = nhp) { 16884ab085aSmws smb_struct_t *stp = &shp->sh_structs[i]; 16984ab085aSmws uint_t n = 0; 17084ab085aSmws 17184ab085aSmws if ((const uchar_t *)hp + sizeof (smb_header_t) > q) 17284ab085aSmws return (smb_open_error(shp, errp, ESMB_CORRUPT)); 17384ab085aSmws 17484ab085aSmws smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n", 17584ab085aSmws i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp); 17684ab085aSmws 17784ab085aSmws if (hp->smbh_type == SMB_TYPE_EOT) 17884ab085aSmws break; /* ignore any entries beyond end-of-table */ 17984ab085aSmws 180e4586ebfSmws if ((const uchar_t *)hp + hp->smbh_len > q - 2) 181e4586ebfSmws return (smb_open_error(shp, errp, ESMB_CORRUPT)); 182e4586ebfSmws 18384ab085aSmws h = hp->smbh_hdl & (shp->sh_hashlen - 1); 18484ab085aSmws p = s = (const uchar_t *)hp + hp->smbh_len; 18584ab085aSmws 18684ab085aSmws while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) { 18784ab085aSmws if (*p++ == '\0') 18884ab085aSmws n++; /* count strings until \0\0 delimiter */ 18984ab085aSmws } 19084ab085aSmws 19184ab085aSmws if (p > q - 2) 19284ab085aSmws return (smb_open_error(shp, errp, ESMB_CORRUPT)); 19384ab085aSmws 19484ab085aSmws if (p > s) 19584ab085aSmws n++; /* add one for final string in string table */ 19684ab085aSmws 19784ab085aSmws stp->smbst_hdr = hp; 19884ab085aSmws stp->smbst_str = s; 19984ab085aSmws stp->smbst_end = p; 20084ab085aSmws stp->smbst_next = shp->sh_hash[h]; 20184ab085aSmws stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n); 20284ab085aSmws stp->smbst_strtablen = n; 20384ab085aSmws 20484ab085aSmws if (n != 0 && stp->smbst_strtab == NULL) 20584ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM)); 20684ab085aSmws 20784ab085aSmws shp->sh_hash[h] = stp; 20884ab085aSmws nhp = (void *)(p + 2); 20984ab085aSmws shp->sh_nstructs++; 21084ab085aSmws 21184ab085aSmws for (n = 0, p = s; n < stp->smbst_strtablen; p++) { 21284ab085aSmws if (*p == '\0') { 21384ab085aSmws stp->smbst_strtab[n++] = 21484ab085aSmws (uint16_t)(s - stp->smbst_str); 21584ab085aSmws s = p + 1; 21684ab085aSmws } 21784ab085aSmws } 21884ab085aSmws } 21984ab085aSmws 22084ab085aSmws if (flags & SMB_O_ZIDS) 22184ab085aSmws smb_strip(shp); 22284ab085aSmws 22384ab085aSmws return (shp); 22484ab085aSmws } 22584ab085aSmws 22684ab085aSmws void 22784ab085aSmws smbios_close(smbios_hdl_t *shp) 22884ab085aSmws { 22984ab085aSmws const smbios_entry_t *ep = &shp->sh_ent; 23084ab085aSmws uint_t i; 23184ab085aSmws 23284ab085aSmws for (i = 0; i < shp->sh_nstructs; i++) { 23384ab085aSmws smb_free(shp->sh_structs[i].smbst_strtab, 23484ab085aSmws sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen); 23584ab085aSmws } 23684ab085aSmws 23784ab085aSmws smb_free(shp->sh_structs, sizeof (smb_struct_t) * ep->smbe_stnum); 23884ab085aSmws smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen); 23984ab085aSmws 24084ab085aSmws if (shp->sh_flags & SMB_FL_BUFALLOC) 24184ab085aSmws smb_free((void *)shp->sh_buf, shp->sh_buflen); 24284ab085aSmws 24384ab085aSmws smb_free(shp, sizeof (smbios_hdl_t)); 24484ab085aSmws } 24584ab085aSmws 24684ab085aSmws /* 24784ab085aSmws * Recompute the values of the entry point checksums based upon the content 24884ab085aSmws * of the specified SMBIOS entry point. We don't need 'shp' but require it 24984ab085aSmws * anyway in case future versioning requires variations in the algorithm. 25084ab085aSmws */ 25184ab085aSmws /*ARGSUSED*/ 25284ab085aSmws void 25384ab085aSmws smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep) 25484ab085aSmws { 25584ab085aSmws uchar_t *p, *q = (uchar_t *)ep; 25684ab085aSmws uint8_t esum = 0, isum = 0; 25784ab085aSmws 25884ab085aSmws ep->smbe_ecksum = ep->smbe_icksum = 0; 25984ab085aSmws 26084ab085aSmws for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++) 26184ab085aSmws isum += *p; 26284ab085aSmws 26384ab085aSmws ep->smbe_icksum = -isum; 26484ab085aSmws 26584ab085aSmws for (p = q; p < q + ep->smbe_elen; p++) 26684ab085aSmws esum += *p; 26784ab085aSmws 26884ab085aSmws ep->smbe_ecksum = -esum; 26984ab085aSmws } 27084ab085aSmws 27184ab085aSmws const void * 27284ab085aSmws smbios_buf(smbios_hdl_t *shp) 27384ab085aSmws { 27484ab085aSmws return (shp->sh_buf); 27584ab085aSmws } 27684ab085aSmws 27784ab085aSmws size_t 27884ab085aSmws smbios_buflen(smbios_hdl_t *shp) 27984ab085aSmws { 28084ab085aSmws return (shp->sh_buflen); 28184ab085aSmws } 28284ab085aSmws 28384ab085aSmws static smbios_struct_t * 28484ab085aSmws smb_export(const smb_struct_t *stp, smbios_struct_t *sp) 28584ab085aSmws { 28684ab085aSmws const smb_header_t *hdr = stp->smbst_hdr; 28784ab085aSmws 28884ab085aSmws sp->smbstr_id = hdr->smbh_hdl; 28984ab085aSmws sp->smbstr_type = hdr->smbh_type; 29084ab085aSmws sp->smbstr_data = hdr; 29184ab085aSmws sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr); 29284ab085aSmws 29384ab085aSmws return (sp); 29484ab085aSmws } 29584ab085aSmws 29684ab085aSmws int 29784ab085aSmws smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp) 29884ab085aSmws { 29984ab085aSmws const smb_struct_t *stp = smb_lookup_id(shp, id); 30084ab085aSmws 30184ab085aSmws if (stp == NULL) 30284ab085aSmws return (-1); /* errno is set for us */ 30384ab085aSmws 30484ab085aSmws if (sp != NULL) 30584ab085aSmws (void) smb_export(stp, sp); 30684ab085aSmws 30784ab085aSmws return (0); 30884ab085aSmws } 30984ab085aSmws 31084ab085aSmws int 311074bb90dSTom Pothier smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp) 312074bb90dSTom Pothier { 313074bb90dSTom Pothier const smb_struct_t *stp = smb_lookup_type(shp, type); 314074bb90dSTom Pothier 315074bb90dSTom Pothier if (stp == NULL) 316074bb90dSTom Pothier return (-1); /* errno is set for us */ 317074bb90dSTom Pothier 318074bb90dSTom Pothier if (sp != NULL) 319074bb90dSTom Pothier (void) smb_export(stp, sp); 320074bb90dSTom Pothier 321074bb90dSTom Pothier return (0); 322074bb90dSTom Pothier } 323074bb90dSTom Pothier 324074bb90dSTom Pothier int 32584ab085aSmws smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data) 32684ab085aSmws { 32784ab085aSmws const smb_struct_t *sp = shp->sh_structs; 32884ab085aSmws smbios_struct_t s; 32984ab085aSmws int i, rv = 0; 33084ab085aSmws 33184ab085aSmws for (i = 0; i < shp->sh_nstructs; i++, sp++) { 33284ab085aSmws if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE && 33384ab085aSmws (rv = func(shp, smb_export(sp, &s), data)) != 0) 33484ab085aSmws break; 33584ab085aSmws } 33684ab085aSmws 33784ab085aSmws return (rv); 33884ab085aSmws } 33984ab085aSmws 34084ab085aSmws const smb_struct_t * 34184ab085aSmws smb_lookup_type(smbios_hdl_t *shp, uint_t type) 34284ab085aSmws { 34384ab085aSmws uint_t i; 34484ab085aSmws 34584ab085aSmws for (i = 0; i < shp->sh_nstructs; i++) { 34684ab085aSmws if (shp->sh_structs[i].smbst_hdr->smbh_type == type) 34784ab085aSmws return (&shp->sh_structs[i]); 34884ab085aSmws } 34984ab085aSmws 35084ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT); 35184ab085aSmws return (NULL); 35284ab085aSmws } 35384ab085aSmws 35484ab085aSmws const smb_struct_t * 35584ab085aSmws smb_lookup_id(smbios_hdl_t *shp, uint_t id) 35684ab085aSmws { 35784ab085aSmws const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)]; 35884ab085aSmws 35984ab085aSmws switch (id) { 36084ab085aSmws case SMB_ID_NOTSUP: 36184ab085aSmws (void) smb_set_errno(shp, ESMB_NOTSUP); 36284ab085aSmws return (NULL); 36384ab085aSmws case SMB_ID_NONE: 36484ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT); 36584ab085aSmws return (NULL); 36684ab085aSmws } 36784ab085aSmws 36884ab085aSmws for (; stp != NULL; stp = stp->smbst_next) { 36984ab085aSmws if (stp->smbst_hdr->smbh_hdl == id) 37084ab085aSmws break; 37184ab085aSmws } 37284ab085aSmws 37384ab085aSmws if (stp == NULL) 37484ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT); 37584ab085aSmws 37684ab085aSmws return (stp); 37784ab085aSmws } 37884ab085aSmws 37984ab085aSmws const char * 38084ab085aSmws smb_strptr(const smb_struct_t *stp, uint_t i) 38184ab085aSmws { 38284ab085aSmws if (i == 0 || i > stp->smbst_strtablen) 38384ab085aSmws return (_smb_emptystr); 38484ab085aSmws else 38584ab085aSmws return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]); 38684ab085aSmws } 38784ab085aSmws 38884ab085aSmws int 38984ab085aSmws smb_gteq(smbios_hdl_t *shp, int version) 39084ab085aSmws { 39184ab085aSmws return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || ( 39284ab085aSmws SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) && 39384ab085aSmws SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version))); 39484ab085aSmws } 395