xref: /illumos-gate/usr/src/uts/i86pc/os/smb_dev.c (revision 584b574a)
184ab085aSmws /*
284ab085aSmws  * CDDL HEADER START
384ab085aSmws  *
484ab085aSmws  * The contents of this file are subject to the terms of the
584ab085aSmws  * Common Development and Distribution License, Version 1.0 only
684ab085aSmws  * (the "License").  You may not use this file except in compliance
784ab085aSmws  * with the License.
884ab085aSmws  *
984ab085aSmws  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1084ab085aSmws  * or http://www.opensolaris.org/os/licensing.
1184ab085aSmws  * See the License for the specific language governing permissions
1284ab085aSmws  * and limitations under the License.
1384ab085aSmws  *
1484ab085aSmws  * When distributing Covered Code, include this CDDL HEADER in each
1584ab085aSmws  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1684ab085aSmws  * If applicable, add the following below this CDDL HEADER, with the
1784ab085aSmws  * fields enclosed by brackets "[]" replaced with your own identifying
1884ab085aSmws  * information: Portions Copyright [yyyy] [name of copyright owner]
1984ab085aSmws  *
2084ab085aSmws  * CDDL HEADER END
2184ab085aSmws  */
2284ab085aSmws 
2384ab085aSmws /*
24e4586ebfSmws  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2584ab085aSmws  * Use is subject to license terms.
261566bc34SRobert Mustacchi  * Copyright (c) 2018, Joyent, Inc.
2784ab085aSmws  */
2884ab085aSmws 
2984ab085aSmws /*
3084ab085aSmws  * Platform-Specific SMBIOS Subroutines
3184ab085aSmws  *
3284ab085aSmws  * The routines in this file form part of <sys/smbios_impl.h> and combine with
3384ab085aSmws  * the usr/src/common/smbios code to form an in-kernel SMBIOS decoding service.
3484ab085aSmws  * The SMBIOS entry point is locating by scanning a range of physical memory
3584ab085aSmws  * assigned to BIOS as described in Section 2 of the DMTF SMBIOS specification.
3684ab085aSmws  */
3784ab085aSmws 
3884ab085aSmws #include <sys/smbios_impl.h>
3984ab085aSmws #include <sys/sysmacros.h>
4084ab085aSmws #include <sys/errno.h>
4184ab085aSmws #include <sys/psm.h>
4284ab085aSmws #include <sys/smp_impldefs.h>
4384ab085aSmws 
4484ab085aSmws smbios_hdl_t *ksmbios;
4584ab085aSmws int ksmbios_flags;
4684ab085aSmws 
4784ab085aSmws smbios_hdl_t *
smb_open_error(smbios_hdl_t * shp,int * errp,int err)4884ab085aSmws smb_open_error(smbios_hdl_t *shp, int *errp, int err)
4984ab085aSmws {
5084ab085aSmws 	if (shp != NULL)
5184ab085aSmws 		smbios_close(shp);
5284ab085aSmws 
5384ab085aSmws 	if (errp != NULL)
5484ab085aSmws 		*errp = err;
5584ab085aSmws 
5684ab085aSmws 	if (ksmbios == NULL)
5784ab085aSmws 		cmn_err(CE_CONT, "?SMBIOS not loaded (%s)", smbios_errmsg(err));
5884ab085aSmws 
5984ab085aSmws 	return (NULL);
6084ab085aSmws }
6184ab085aSmws 
6284ab085aSmws smbios_hdl_t *
smbios_open(const char * file,int version,int flags,int * errp)6384ab085aSmws smbios_open(const char *file, int version, int flags, int *errp)
6484ab085aSmws {
6584ab085aSmws 	smbios_hdl_t *shp = NULL;
66e4586ebfSmws 	smbios_entry_t *ep;
6784ab085aSmws 	caddr_t stbuf, bios, p, q;
681566bc34SRobert Mustacchi 	caddr_t smb2, smb3;
6996e7037fSToomas Soome 	uint64_t startaddr, startoff = 0;
7084ab085aSmws 	size_t bioslen;
711951a933SToomas Soome 	uint_t smbe_stlen;
721566bc34SRobert Mustacchi 	smbios_entry_point_t ep_type;
731951a933SToomas Soome 	uint8_t smbe_major, smbe_minor;
7484ab085aSmws 	int err;
7584ab085aSmws 
7684ab085aSmws 	if (file != NULL || (flags & ~SMB_O_MASK))
7784ab085aSmws 		return (smb_open_error(shp, errp, ESMB_INVAL));
7884ab085aSmws 
7996e7037fSToomas Soome 	if ((startaddr = ddi_prop_get_int64(DDI_DEV_T_ANY, ddi_root_node(),
8096e7037fSToomas Soome 	    DDI_PROP_DONTPASS, "smbios-address", 0)) == 0) {
8196e7037fSToomas Soome 		startaddr = SMB_RANGE_START;
8296e7037fSToomas Soome 		bioslen = SMB_RANGE_LIMIT - SMB_RANGE_START + 1;
8396e7037fSToomas Soome 	} else {
8496e7037fSToomas Soome 		/* We have smbios address from boot loader, map one page. */
8596e7037fSToomas Soome 		bioslen = MMU_PAGESIZE;
8696e7037fSToomas Soome 		startoff = startaddr & MMU_PAGEOFFSET;
8796e7037fSToomas Soome 		startaddr &= MMU_PAGEMASK;
8896e7037fSToomas Soome 	}
8996e7037fSToomas Soome 
9096e7037fSToomas Soome 	bios = psm_map_phys(startaddr, bioslen, PSM_PROT_READ);
9184ab085aSmws 
9284ab085aSmws 	if (bios == NULL)
9384ab085aSmws 		return (smb_open_error(shp, errp, ESMB_MAPDEV));
9484ab085aSmws 
9596e7037fSToomas Soome 	/*
9696e7037fSToomas Soome 	 * In case we did map one page, make sure we will not cross
9796e7037fSToomas Soome 	 * the end of the page.
9896e7037fSToomas Soome 	 */
9996e7037fSToomas Soome 	p = bios + startoff;
10096e7037fSToomas Soome 	q = bios + bioslen - startoff;
1011566bc34SRobert Mustacchi 	smb2 = smb3 = NULL;
10296e7037fSToomas Soome 	while (p < q) {
1031566bc34SRobert Mustacchi 		if (smb2 != NULL && smb3 != NULL)
1041951a933SToomas Soome 			break;
1051566bc34SRobert Mustacchi 
1061566bc34SRobert Mustacchi 		if (smb3 == NULL && strncmp(p, SMB3_ENTRY_EANCHOR,
1071566bc34SRobert Mustacchi 		    SMB3_ENTRY_EANCHORLEN) == 0) {
1081566bc34SRobert Mustacchi 			smb3 = p;
1091566bc34SRobert Mustacchi 		} else if (smb2 == NULL && strncmp(p, SMB_ENTRY_EANCHOR,
1101566bc34SRobert Mustacchi 		    SMB_ENTRY_EANCHORLEN) == 0) {
1111566bc34SRobert Mustacchi 			smb2 = p;
1121951a933SToomas Soome 		}
1137205bbdbSToomas Soome 
11496e7037fSToomas Soome 		p += SMB_SCAN_STEP;
11584ab085aSmws 	}
11684ab085aSmws 
1171566bc34SRobert Mustacchi 	if (smb2 == NULL && smb3 == NULL) {
11884ab085aSmws 		psm_unmap_phys(bios, bioslen);
11984ab085aSmws 		return (smb_open_error(shp, errp, ESMB_NOTFOUND));
12084ab085aSmws 	}
12184ab085aSmws 
1221566bc34SRobert Mustacchi 	/*
1231566bc34SRobert Mustacchi 	 * While they're not supposed to (as per the SMBIOS 3.2 spec), some
1241566bc34SRobert Mustacchi 	 * vendors end up having a newer version in one of the two entry points
1251566bc34SRobert Mustacchi 	 * than the other. If we found multiple tables then we will prefer the
1261566bc34SRobert Mustacchi 	 * one with the newer version. If they're equivalent, we prefer the
1271566bc34SRobert Mustacchi 	 * 32-bit version. If only one is present, then we use that.
1281566bc34SRobert Mustacchi 	 */
129e4586ebfSmws 	ep = smb_alloc(SMB_ENTRY_MAXLEN);
1301566bc34SRobert Mustacchi 	if (smb2 != NULL && smb3 != NULL) {
1311566bc34SRobert Mustacchi 		uint8_t smb2maj, smb2min, smb3maj, smb3min;
1321566bc34SRobert Mustacchi 
1331566bc34SRobert Mustacchi 		bcopy(smb2, ep, sizeof (smbios_entry_t));
1341566bc34SRobert Mustacchi 		smb2maj = ep->ep21.smbe_major;
1351566bc34SRobert Mustacchi 		smb2min = ep->ep21.smbe_minor;
1361566bc34SRobert Mustacchi 		bcopy(smb3, ep, sizeof (smbios_entry_t));
1371566bc34SRobert Mustacchi 		smb3maj = ep->ep30.smbe_major;
1381566bc34SRobert Mustacchi 		smb3min = ep->ep30.smbe_minor;
1391566bc34SRobert Mustacchi 
1401566bc34SRobert Mustacchi 		if (smb3maj > smb2maj ||
1411566bc34SRobert Mustacchi 		    (smb3maj == smb2maj && smb3min > smb2min)) {
1421566bc34SRobert Mustacchi 			ep_type = SMBIOS_ENTRY_POINT_30;
1431566bc34SRobert Mustacchi 			p = smb3;
1441566bc34SRobert Mustacchi 		} else {
1451566bc34SRobert Mustacchi 			ep_type = SMBIOS_ENTRY_POINT_21;
1461566bc34SRobert Mustacchi 			p = smb2;
1471566bc34SRobert Mustacchi 		}
1481566bc34SRobert Mustacchi 	} else if (smb3 != NULL) {
1491566bc34SRobert Mustacchi 		ep_type = SMBIOS_ENTRY_POINT_30;
1501566bc34SRobert Mustacchi 		p = smb3;
151*584b574aSToomas Soome 	} else {
1521566bc34SRobert Mustacchi 		ep_type = SMBIOS_ENTRY_POINT_21;
1531566bc34SRobert Mustacchi 		p = smb2;
1541566bc34SRobert Mustacchi 	}
155e4586ebfSmws 	bcopy(p, ep, sizeof (smbios_entry_t));
1561951a933SToomas Soome 	if (ep_type == SMBIOS_ENTRY_POINT_21) {
1571951a933SToomas Soome 		ep->ep21.smbe_elen = MIN(ep->ep21.smbe_elen, SMB_ENTRY_MAXLEN);
1581951a933SToomas Soome 		bcopy(p, ep, ep->ep21.smbe_elen);
1591951a933SToomas Soome 	} else if (ep_type == SMBIOS_ENTRY_POINT_30) {
1601951a933SToomas Soome 		ep->ep30.smbe_elen = MIN(ep->ep30.smbe_elen, SMB_ENTRY_MAXLEN);
1611951a933SToomas Soome 		bcopy(p, ep, ep->ep30.smbe_elen);
1621951a933SToomas Soome 	}
163e4586ebfSmws 
16484ab085aSmws 	psm_unmap_phys(bios, bioslen);
1651951a933SToomas Soome 	switch (ep_type) {
1661951a933SToomas Soome 	case SMBIOS_ENTRY_POINT_21:
1671951a933SToomas Soome 		smbe_major = ep->ep21.smbe_major;
1681951a933SToomas Soome 		smbe_minor = ep->ep21.smbe_minor;
1691951a933SToomas Soome 		smbe_stlen = ep->ep21.smbe_stlen;
1701951a933SToomas Soome 		bios = psm_map_phys(ep->ep21.smbe_staddr, smbe_stlen,
1711951a933SToomas Soome 		    PSM_PROT_READ);
1721951a933SToomas Soome 		break;
1731951a933SToomas Soome 	case SMBIOS_ENTRY_POINT_30:
1741951a933SToomas Soome 		smbe_major = ep->ep30.smbe_major;
1751951a933SToomas Soome 		smbe_minor = ep->ep30.smbe_minor;
1761951a933SToomas Soome 		smbe_stlen = ep->ep30.smbe_stlen;
1771951a933SToomas Soome 		bios = psm_map_phys_new(ep->ep30.smbe_staddr, smbe_stlen,
1781951a933SToomas Soome 		    PSM_PROT_READ);
1791951a933SToomas Soome 		break;
1801951a933SToomas Soome 	default:
1811951a933SToomas Soome 		smb_free(ep, SMB_ENTRY_MAXLEN);
1821951a933SToomas Soome 		return (smb_open_error(shp, errp, ESMB_VERSION));
1831951a933SToomas Soome 	}
18484ab085aSmws 
185e4586ebfSmws 	if (bios == NULL) {
186e4586ebfSmws 		smb_free(ep, SMB_ENTRY_MAXLEN);
18784ab085aSmws 		return (smb_open_error(shp, errp, ESMB_MAPDEV));
188e4586ebfSmws 	}
18984ab085aSmws 
1901951a933SToomas Soome 	stbuf = smb_alloc(smbe_stlen);
1911951a933SToomas Soome 	bcopy(bios, stbuf, smbe_stlen);
1921951a933SToomas Soome 	psm_unmap_phys(bios, smbe_stlen);
1931951a933SToomas Soome 	shp = smbios_bufopen(ep, stbuf, smbe_stlen, version, flags, &err);
19484ab085aSmws 
19584ab085aSmws 	if (shp == NULL) {
1961951a933SToomas Soome 		smb_free(stbuf, smbe_stlen);
197e4586ebfSmws 		smb_free(ep, SMB_ENTRY_MAXLEN);
19884ab085aSmws 		return (smb_open_error(shp, errp, err));
19984ab085aSmws 	}
20084ab085aSmws 
20184ab085aSmws 	if (ksmbios == NULL) {
20284ab085aSmws 		cmn_err(CE_CONT, "?SMBIOS v%u.%u loaded (%u bytes)",
2031951a933SToomas Soome 		    smbe_major, smbe_minor, smbe_stlen);
204516627f3SJonathan Matthew 		if (shp->sh_flags & SMB_FL_TRUNC)
205516627f3SJonathan Matthew 			cmn_err(CE_CONT, "?SMBIOS table is truncated");
20684ab085aSmws 	}
20784ab085aSmws 
20884ab085aSmws 	shp->sh_flags |= SMB_FL_BUFALLOC;
209e4586ebfSmws 	smb_free(ep, SMB_ENTRY_MAXLEN);
210e4586ebfSmws 
21184ab085aSmws 	return (shp);
21284ab085aSmws }
21384ab085aSmws 
21484ab085aSmws /*ARGSUSED*/
21584ab085aSmws smbios_hdl_t *
smbios_fdopen(int fd,int version,int flags,int * errp)21684ab085aSmws smbios_fdopen(int fd, int version, int flags, int *errp)
21784ab085aSmws {
21884ab085aSmws 	return (smb_open_error(NULL, errp, ENOTSUP));
21984ab085aSmws }
22084ab085aSmws 
22184ab085aSmws /*ARGSUSED*/
22284ab085aSmws int
smbios_write(smbios_hdl_t * shp,int fd)22384ab085aSmws smbios_write(smbios_hdl_t *shp, int fd)
22484ab085aSmws {
22584ab085aSmws 	return (smb_set_errno(shp, ENOTSUP));
22684ab085aSmws }
227