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. 24*38d76b18SRobert Mustacchi * Copyright 2016 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: 92*38d76b18SRobert Mustacchi case SMB_VERSION_31: 9384ab085aSmws break; 9484ab085aSmws default: 9584ab085aSmws return (smb_open_error(shp, errp, ESMB_VERSION)); 9684ab085aSmws } 9784ab085aSmws 9884ab085aSmws if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK)) 9984ab085aSmws return (smb_open_error(shp, errp, ESMB_INVAL)); 10084ab085aSmws 10184ab085aSmws if (shp == NULL) 10284ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM)); 10384ab085aSmws 10484ab085aSmws if (_smb_debug) 10584ab085aSmws shp->sh_flags |= SMB_FL_DEBUG; 10684ab085aSmws 10784ab085aSmws if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN)) 10884ab085aSmws return (smb_open_error(shp, errp, ESMB_HEADER)); 10984ab085aSmws 11084ab085aSmws if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN)) 11184ab085aSmws return (smb_open_error(shp, errp, ESMB_HEADER)); 11284ab085aSmws 11384ab085aSmws smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n", 11484ab085aSmws ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev); 11584ab085aSmws 11684ab085aSmws if (!(flags & SMB_O_NOVERS)) { 11784ab085aSmws if (ep->smbe_major > SMB_MAJOR(SMB_VERSION)) 11884ab085aSmws return (smb_open_error(shp, errp, ESMB_NEW)); 11984ab085aSmws 12084ab085aSmws if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || ( 12184ab085aSmws ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) && 12284ab085aSmws ep->smbe_minor < SMB_MINOR(SMB_VERSION_23))) 12384ab085aSmws return (smb_open_error(shp, errp, ESMB_OLD)); 12484ab085aSmws } 12584ab085aSmws 12684ab085aSmws if (len < sizeof (smb_header_t) || 12784ab085aSmws ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen) 12884ab085aSmws return (smb_open_error(shp, errp, ESMB_SHORT)); 12984ab085aSmws 13084ab085aSmws if (!(flags & SMB_O_NOCKSUM)) { 13184ab085aSmws uint8_t esum = 0, isum = 0; 13284ab085aSmws q = (uchar_t *)ep; 13384ab085aSmws 13484ab085aSmws for (p = q; p < q + ep->smbe_elen; p++) 13584ab085aSmws esum += *p; 13684ab085aSmws 13784ab085aSmws for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++) 13884ab085aSmws isum += *p; 13984ab085aSmws 14084ab085aSmws if (esum != 0 || isum != 0) { 14184ab085aSmws smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum); 14284ab085aSmws return (smb_open_error(shp, errp, ESMB_CKSUM)); 14384ab085aSmws } 14484ab085aSmws } 14584ab085aSmws 146e4586ebfSmws /* 147e4586ebfSmws * Copy the entry point into our handle. The underlying entry point 148e4586ebfSmws * may be larger than our structure definition, so reset smbe_elen 149e4586ebfSmws * to our internal size and recompute good checksums for our copy. 150e4586ebfSmws */ 15184ab085aSmws bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t)); 152e4586ebfSmws shp->sh_ent.smbe_elen = sizeof (smbios_entry_t); 153e4586ebfSmws smbios_checksum(shp, &shp->sh_ent); 154e4586ebfSmws 15584ab085aSmws shp->sh_buf = buf; 15684ab085aSmws shp->sh_buflen = len; 15784ab085aSmws shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * ep->smbe_stnum); 15884ab085aSmws shp->sh_nstructs = 0; 15984ab085aSmws shp->sh_hashlen = _smb_hashlen; 16084ab085aSmws shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen); 16184ab085aSmws shp->sh_libvers = version; 16284ab085aSmws shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor); 16384ab085aSmws 16484ab085aSmws if (shp->sh_structs == NULL || shp->sh_hash == NULL) 16584ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM)); 16684ab085aSmws 16784ab085aSmws hp = shp->sh_buf; 16884ab085aSmws q = (const uchar_t *)buf + MIN(ep->smbe_stlen, len); 16984ab085aSmws 17084ab085aSmws for (i = 0; i < ep->smbe_stnum; i++, hp = nhp) { 17184ab085aSmws smb_struct_t *stp = &shp->sh_structs[i]; 17284ab085aSmws uint_t n = 0; 17384ab085aSmws 174b60ae21dSJonathan Matthew if ((const uchar_t *)hp + sizeof (smb_header_t) > q) { 175b60ae21dSJonathan Matthew shp->sh_flags |= SMB_FL_TRUNC; 176b60ae21dSJonathan Matthew break; 177b60ae21dSJonathan Matthew } 17884ab085aSmws 17984ab085aSmws smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n", 18084ab085aSmws i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp); 18184ab085aSmws 18284ab085aSmws if (hp->smbh_type == SMB_TYPE_EOT) 18384ab085aSmws break; /* ignore any entries beyond end-of-table */ 18484ab085aSmws 185b60ae21dSJonathan Matthew if ((const uchar_t *)hp + hp->smbh_len > q - 2) { 186b60ae21dSJonathan Matthew shp->sh_flags |= SMB_FL_TRUNC; 187b60ae21dSJonathan Matthew break; 188b60ae21dSJonathan Matthew } 189e4586ebfSmws 19084ab085aSmws h = hp->smbh_hdl & (shp->sh_hashlen - 1); 19184ab085aSmws p = s = (const uchar_t *)hp + hp->smbh_len; 19284ab085aSmws 19384ab085aSmws while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) { 19484ab085aSmws if (*p++ == '\0') 19584ab085aSmws n++; /* count strings until \0\0 delimiter */ 19684ab085aSmws } 19784ab085aSmws 198b60ae21dSJonathan Matthew if (p > q - 2) { 199b60ae21dSJonathan Matthew shp->sh_flags |= SMB_FL_TRUNC; 200b60ae21dSJonathan Matthew break; 201b60ae21dSJonathan Matthew } 20284ab085aSmws 20384ab085aSmws if (p > s) 20484ab085aSmws n++; /* add one for final string in string table */ 20584ab085aSmws 20684ab085aSmws stp->smbst_hdr = hp; 20784ab085aSmws stp->smbst_str = s; 20884ab085aSmws stp->smbst_end = p; 20984ab085aSmws stp->smbst_next = shp->sh_hash[h]; 21084ab085aSmws stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n); 21184ab085aSmws stp->smbst_strtablen = n; 21284ab085aSmws 21384ab085aSmws if (n != 0 && stp->smbst_strtab == NULL) 21484ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM)); 21584ab085aSmws 21684ab085aSmws shp->sh_hash[h] = stp; 21784ab085aSmws nhp = (void *)(p + 2); 21884ab085aSmws shp->sh_nstructs++; 21984ab085aSmws 22084ab085aSmws for (n = 0, p = s; n < stp->smbst_strtablen; p++) { 22184ab085aSmws if (*p == '\0') { 22284ab085aSmws stp->smbst_strtab[n++] = 22384ab085aSmws (uint16_t)(s - stp->smbst_str); 22484ab085aSmws s = p + 1; 22584ab085aSmws } 22684ab085aSmws } 22784ab085aSmws } 22884ab085aSmws 229b60ae21dSJonathan Matthew /* error out if we couldn't find any complete entries in the table */ 230b60ae21dSJonathan Matthew if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0) 231b60ae21dSJonathan Matthew return (smb_open_error(shp, errp, ESMB_CORRUPT)); 232b60ae21dSJonathan Matthew 23384ab085aSmws if (flags & SMB_O_ZIDS) 23484ab085aSmws smb_strip(shp); 23584ab085aSmws 23684ab085aSmws return (shp); 23784ab085aSmws } 23884ab085aSmws 23984ab085aSmws void 24084ab085aSmws smbios_close(smbios_hdl_t *shp) 24184ab085aSmws { 24284ab085aSmws const smbios_entry_t *ep = &shp->sh_ent; 24384ab085aSmws uint_t i; 24484ab085aSmws 24584ab085aSmws for (i = 0; i < shp->sh_nstructs; i++) { 24684ab085aSmws smb_free(shp->sh_structs[i].smbst_strtab, 24784ab085aSmws sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen); 24884ab085aSmws } 24984ab085aSmws 25084ab085aSmws smb_free(shp->sh_structs, sizeof (smb_struct_t) * ep->smbe_stnum); 25184ab085aSmws smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen); 25284ab085aSmws 25384ab085aSmws if (shp->sh_flags & SMB_FL_BUFALLOC) 25484ab085aSmws smb_free((void *)shp->sh_buf, shp->sh_buflen); 25584ab085aSmws 25684ab085aSmws smb_free(shp, sizeof (smbios_hdl_t)); 25784ab085aSmws } 25884ab085aSmws 25984ab085aSmws /* 26084ab085aSmws * Recompute the values of the entry point checksums based upon the content 26184ab085aSmws * of the specified SMBIOS entry point. We don't need 'shp' but require it 26284ab085aSmws * anyway in case future versioning requires variations in the algorithm. 26384ab085aSmws */ 26484ab085aSmws /*ARGSUSED*/ 26584ab085aSmws void 26684ab085aSmws smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep) 26784ab085aSmws { 26884ab085aSmws uchar_t *p, *q = (uchar_t *)ep; 26984ab085aSmws uint8_t esum = 0, isum = 0; 27084ab085aSmws 27184ab085aSmws ep->smbe_ecksum = ep->smbe_icksum = 0; 27284ab085aSmws 27384ab085aSmws for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++) 27484ab085aSmws isum += *p; 27584ab085aSmws 27684ab085aSmws ep->smbe_icksum = -isum; 27784ab085aSmws 27884ab085aSmws for (p = q; p < q + ep->smbe_elen; p++) 27984ab085aSmws esum += *p; 28084ab085aSmws 28184ab085aSmws ep->smbe_ecksum = -esum; 28284ab085aSmws } 28384ab085aSmws 28484ab085aSmws const void * 28584ab085aSmws smbios_buf(smbios_hdl_t *shp) 28684ab085aSmws { 28784ab085aSmws return (shp->sh_buf); 28884ab085aSmws } 28984ab085aSmws 29084ab085aSmws size_t 29184ab085aSmws smbios_buflen(smbios_hdl_t *shp) 29284ab085aSmws { 29384ab085aSmws return (shp->sh_buflen); 29484ab085aSmws } 29584ab085aSmws 29684ab085aSmws static smbios_struct_t * 29784ab085aSmws smb_export(const smb_struct_t *stp, smbios_struct_t *sp) 29884ab085aSmws { 29984ab085aSmws const smb_header_t *hdr = stp->smbst_hdr; 30084ab085aSmws 30184ab085aSmws sp->smbstr_id = hdr->smbh_hdl; 30284ab085aSmws sp->smbstr_type = hdr->smbh_type; 30384ab085aSmws sp->smbstr_data = hdr; 30484ab085aSmws sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr); 30584ab085aSmws 30684ab085aSmws return (sp); 30784ab085aSmws } 30884ab085aSmws 30984ab085aSmws int 31084ab085aSmws smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp) 31184ab085aSmws { 31284ab085aSmws const smb_struct_t *stp = smb_lookup_id(shp, id); 31384ab085aSmws 31484ab085aSmws if (stp == NULL) 31584ab085aSmws return (-1); /* errno is set for us */ 31684ab085aSmws 31784ab085aSmws if (sp != NULL) 31884ab085aSmws (void) smb_export(stp, sp); 31984ab085aSmws 32084ab085aSmws return (0); 32184ab085aSmws } 32284ab085aSmws 32384ab085aSmws int 324074bb90dSTom Pothier smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp) 325074bb90dSTom Pothier { 326074bb90dSTom Pothier const smb_struct_t *stp = smb_lookup_type(shp, type); 327074bb90dSTom Pothier 328074bb90dSTom Pothier if (stp == NULL) 329074bb90dSTom Pothier return (-1); /* errno is set for us */ 330074bb90dSTom Pothier 331074bb90dSTom Pothier if (sp != NULL) 332074bb90dSTom Pothier (void) smb_export(stp, sp); 333074bb90dSTom Pothier 334074bb90dSTom Pothier return (0); 335074bb90dSTom Pothier } 336074bb90dSTom Pothier 337074bb90dSTom Pothier int 33884ab085aSmws smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data) 33984ab085aSmws { 34084ab085aSmws const smb_struct_t *sp = shp->sh_structs; 34184ab085aSmws smbios_struct_t s; 34284ab085aSmws int i, rv = 0; 34384ab085aSmws 34484ab085aSmws for (i = 0; i < shp->sh_nstructs; i++, sp++) { 34584ab085aSmws if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE && 34684ab085aSmws (rv = func(shp, smb_export(sp, &s), data)) != 0) 34784ab085aSmws break; 34884ab085aSmws } 34984ab085aSmws 35084ab085aSmws return (rv); 35184ab085aSmws } 35284ab085aSmws 35384ab085aSmws const smb_struct_t * 35484ab085aSmws smb_lookup_type(smbios_hdl_t *shp, uint_t type) 35584ab085aSmws { 35684ab085aSmws uint_t i; 35784ab085aSmws 35884ab085aSmws for (i = 0; i < shp->sh_nstructs; i++) { 35984ab085aSmws if (shp->sh_structs[i].smbst_hdr->smbh_type == type) 36084ab085aSmws return (&shp->sh_structs[i]); 36184ab085aSmws } 36284ab085aSmws 36384ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT); 36484ab085aSmws return (NULL); 36584ab085aSmws } 36684ab085aSmws 36784ab085aSmws const smb_struct_t * 36884ab085aSmws smb_lookup_id(smbios_hdl_t *shp, uint_t id) 36984ab085aSmws { 37084ab085aSmws const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)]; 37184ab085aSmws 37284ab085aSmws switch (id) { 37384ab085aSmws case SMB_ID_NOTSUP: 37484ab085aSmws (void) smb_set_errno(shp, ESMB_NOTSUP); 37584ab085aSmws return (NULL); 37684ab085aSmws case SMB_ID_NONE: 37784ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT); 37884ab085aSmws return (NULL); 37984ab085aSmws } 38084ab085aSmws 38184ab085aSmws for (; stp != NULL; stp = stp->smbst_next) { 38284ab085aSmws if (stp->smbst_hdr->smbh_hdl == id) 38384ab085aSmws break; 38484ab085aSmws } 38584ab085aSmws 38684ab085aSmws if (stp == NULL) 38784ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT); 38884ab085aSmws 38984ab085aSmws return (stp); 39084ab085aSmws } 39184ab085aSmws 39284ab085aSmws const char * 39384ab085aSmws smb_strptr(const smb_struct_t *stp, uint_t i) 39484ab085aSmws { 39584ab085aSmws if (i == 0 || i > stp->smbst_strtablen) 39684ab085aSmws return (_smb_emptystr); 39784ab085aSmws else 39884ab085aSmws return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]); 39984ab085aSmws } 40084ab085aSmws 40184ab085aSmws int 402*38d76b18SRobert Mustacchi smb_libgteq(smbios_hdl_t *shp, int version) 403*38d76b18SRobert Mustacchi { 404*38d76b18SRobert Mustacchi return (SMB_MAJOR(shp->sh_libvers) > SMB_MAJOR(version) || ( 405*38d76b18SRobert Mustacchi SMB_MAJOR(shp->sh_libvers) == SMB_MAJOR(version) && 406*38d76b18SRobert Mustacchi SMB_MINOR(shp->sh_libvers) >= SMB_MINOR(version))); 407*38d76b18SRobert Mustacchi } 408*38d76b18SRobert Mustacchi 409*38d76b18SRobert Mustacchi int 41084ab085aSmws smb_gteq(smbios_hdl_t *shp, int version) 41184ab085aSmws { 41284ab085aSmws return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || ( 41384ab085aSmws SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) && 41484ab085aSmws SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version))); 41584ab085aSmws } 416b60ae21dSJonathan Matthew 417b60ae21dSJonathan Matthew boolean_t 418b60ae21dSJonathan Matthew smbios_truncated(smbios_hdl_t *shp) 419b60ae21dSJonathan Matthew { 420b60ae21dSJonathan Matthew return ((shp->sh_flags & SMB_FL_TRUNC) != 0); 421b60ae21dSJonathan Matthew } 422