xref: /titanic_51/usr/src/uts/i86pc/os/smb_dev.c (revision b10a562e3f5e730a20b76a2716d85674fe62a226)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*
29  * Platform-Specific SMBIOS Subroutines
30  *
31  * The routines in this file form part of <sys/smbios_impl.h> and combine with
32  * the usr/src/common/smbios code to form an in-kernel SMBIOS decoding service.
33  * The SMBIOS entry point is locating by scanning a range of physical memory
34  * assigned to BIOS as described in Section 2 of the DMTF SMBIOS specification.
35  */
36 
37 #include <sys/smbios_impl.h>
38 #include <sys/sysmacros.h>
39 #include <sys/errno.h>
40 #include <sys/psm.h>
41 #include <sys/smp_impldefs.h>
42 
43 smbios_hdl_t *ksmbios;
44 int ksmbios_flags;
45 
46 smbios_hdl_t *
47 smb_open_error(smbios_hdl_t *shp, int *errp, int err)
48 {
49 	if (shp != NULL)
50 		smbios_close(shp);
51 
52 	if (errp != NULL)
53 		*errp = err;
54 
55 	if (ksmbios == NULL)
56 		cmn_err(CE_CONT, "?SMBIOS not loaded (%s)", smbios_errmsg(err));
57 
58 	return (NULL);
59 }
60 
61 smbios_hdl_t *
62 smbios_open(const char *file, int version, int flags, int *errp)
63 {
64 	smbios_hdl_t *shp = NULL;
65 	smbios_entry_t *ep;
66 	caddr_t stbuf, bios, p, q;
67 	uint64_t startaddr, startoff = 0;
68 	size_t bioslen;
69 	int err;
70 
71 	if (file != NULL || (flags & ~SMB_O_MASK))
72 		return (smb_open_error(shp, errp, ESMB_INVAL));
73 
74 	if ((startaddr = ddi_prop_get_int64(DDI_DEV_T_ANY, ddi_root_node(),
75 	    DDI_PROP_DONTPASS, "smbios-address", 0)) == 0) {
76 		startaddr = SMB_RANGE_START;
77 		bioslen = SMB_RANGE_LIMIT - SMB_RANGE_START + 1;
78 	} else {
79 		/* We have smbios address from boot loader, map one page. */
80 		bioslen = MMU_PAGESIZE;
81 		startoff = startaddr & MMU_PAGEOFFSET;
82 		startaddr &= MMU_PAGEMASK;
83 #if defined(__i386)
84 		/* If the address is not good, use memory scan instead. */
85 		if (startaddr > UINT32_MAX) {
86 			startaddr = SMB_RANGE_START;
87 			bioslen = SMB_RANGE_LIMIT - SMB_RANGE_START + 1;
88 			startoff = 0;
89 		}
90 #endif
91 	}
92 
93 	bios = psm_map_phys(startaddr, bioslen, PSM_PROT_READ);
94 
95 	if (bios == NULL)
96 		return (smb_open_error(shp, errp, ESMB_MAPDEV));
97 
98 	/*
99 	 * In case we did map one page, make sure we will not cross
100 	 * the end of the page.
101 	 */
102 	p = bios + startoff;
103 	q = bios + bioslen - startoff;
104 	while (p < q) {
105 		if (strncmp(p, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN) == 0)
106 			break;
107 		p += SMB_SCAN_STEP;
108 	}
109 
110 	if (p >= q) {
111 		psm_unmap_phys(bios, bioslen);
112 		return (smb_open_error(shp, errp, ESMB_NOTFOUND));
113 	}
114 
115 	ep = smb_alloc(SMB_ENTRY_MAXLEN);
116 	bcopy(p, ep, sizeof (smbios_entry_t));
117 	ep->smbe_elen = MIN(ep->smbe_elen, SMB_ENTRY_MAXLEN);
118 	bcopy(p, ep, ep->smbe_elen);
119 
120 	psm_unmap_phys(bios, bioslen);
121 	bios = psm_map_phys(ep->smbe_staddr, ep->smbe_stlen, PSM_PROT_READ);
122 
123 	if (bios == NULL) {
124 		smb_free(ep, SMB_ENTRY_MAXLEN);
125 		return (smb_open_error(shp, errp, ESMB_MAPDEV));
126 	}
127 
128 	stbuf = smb_alloc(ep->smbe_stlen);
129 	bcopy(bios, stbuf, ep->smbe_stlen);
130 	psm_unmap_phys(bios, ep->smbe_stlen);
131 	shp = smbios_bufopen(ep, stbuf, ep->smbe_stlen, version, flags, &err);
132 
133 	if (shp == NULL) {
134 		smb_free(stbuf, ep->smbe_stlen);
135 		smb_free(ep, SMB_ENTRY_MAXLEN);
136 		return (smb_open_error(shp, errp, err));
137 	}
138 
139 	if (ksmbios == NULL) {
140 		cmn_err(CE_CONT, "?SMBIOS v%u.%u loaded (%u bytes)",
141 		    ep->smbe_major, ep->smbe_minor, ep->smbe_stlen);
142 		if (shp->sh_flags & SMB_FL_TRUNC)
143 			cmn_err(CE_CONT, "?SMBIOS table is truncated");
144 	}
145 
146 	shp->sh_flags |= SMB_FL_BUFALLOC;
147 	smb_free(ep, SMB_ENTRY_MAXLEN);
148 
149 	return (shp);
150 }
151 
152 /*ARGSUSED*/
153 smbios_hdl_t *
154 smbios_fdopen(int fd, int version, int flags, int *errp)
155 {
156 	return (smb_open_error(NULL, errp, ENOTSUP));
157 }
158 
159 /*ARGSUSED*/
160 int
161 smbios_write(smbios_hdl_t *shp, int fd)
162 {
163 	return (smb_set_errno(shp, ENOTSUP));
164 }
165