xref: /illumos-gate/usr/src/uts/i86pc/os/smb_dev.c (revision ac2250cb76bb32944fd2c8a3ba2cd3f79747748d)
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  * Copyright (c) 2018, Joyent, Inc.
27  * Copyright 2026 Edgecast Cloud LLC.
28  */
29 
30 /*
31  * Platform-Specific SMBIOS Subroutines
32  *
33  * The routines in this file form part of <sys/smbios_impl.h> and combine with
34  * the usr/src/common/smbios code to form an in-kernel SMBIOS decoding service.
35  * The SMBIOS entry point is locating by scanning a range of physical memory
36  * assigned to BIOS as described in Section 2 of the DMTF SMBIOS specification.
37  */
38 
39 #include <sys/smbios_impl.h>
40 #include <sys/sysmacros.h>
41 #include <sys/errno.h>
42 #include <sys/psm.h>
43 #include <sys/smp_impldefs.h>
44 
45 smbios_hdl_t *ksmbios;
46 int ksmbios_flags;
47 
48 smbios_hdl_t *
49 smb_open_error(smbios_hdl_t *shp, int *errp, int err)
50 {
51 	if (shp != NULL)
52 		smbios_close(shp);
53 
54 	if (errp != NULL)
55 		*errp = err;
56 
57 	if (ksmbios == NULL)
58 		cmn_err(CE_CONT, "?SMBIOS not loaded (%s)", smbios_errmsg(err));
59 
60 	return (NULL);
61 }
62 
63 smbios_hdl_t *
64 smbios_open(const char *file, int version, int flags, int *errp)
65 {
66 	smbios_hdl_t *shp = NULL;
67 	smbios_entry_t *ep;
68 	caddr_t stbuf, bios, p, q;
69 	caddr_t smb2, smb3;
70 	uint64_t startaddr, startoff = 0;
71 	size_t bioslen;
72 	uint_t smbe_stlen;
73 	smbios_entry_point_t ep_type;
74 	uint8_t smbe_major, smbe_minor;
75 	int err;
76 
77 	if (file != NULL || (flags & ~SMB_O_MASK))
78 		return (smb_open_error(shp, errp, ESMB_INVAL));
79 
80 	if ((startaddr = ddi_prop_get_int64(DDI_DEV_T_ANY, ddi_root_node(),
81 	    DDI_PROP_DONTPASS, "smbios-address", 0)) == 0) {
82 		startaddr = SMB_RANGE_START;
83 		bioslen = SMB_RANGE_LIMIT - SMB_RANGE_START + 1;
84 	} else {
85 		/*
86 		 * We have smbios address from boot loader, map a page or two.
87 		 */
88 		bioslen = MMU_PAGESIZE;
89 		startoff = startaddr & MMU_PAGEOFFSET;
90 		startaddr &= MMU_PAGEMASK;
91 		if (bioslen - startoff <= startoff)
92 			bioslen += MMU_PAGESIZE;
93 	}
94 
95 	bios = psm_map_phys(startaddr, bioslen, PSM_PROT_READ);
96 
97 	if (bios == NULL)
98 		return (smb_open_error(shp, errp, ESMB_MAPDEV));
99 
100 	/*
101 	 * In case we did map one page, make sure we will not cross
102 	 * the end of the page.
103 	 */
104 	p = bios + startoff;
105 	q = bios + bioslen - startoff;
106 	smb2 = smb3 = NULL;
107 	while (p < q) {
108 		if (smb2 != NULL && smb3 != NULL)
109 			break;
110 
111 		if (smb3 == NULL && strncmp(p, SMB3_ENTRY_EANCHOR,
112 		    SMB3_ENTRY_EANCHORLEN) == 0) {
113 			smb3 = p;
114 		} else if (smb2 == NULL && strncmp(p, SMB_ENTRY_EANCHOR,
115 		    SMB_ENTRY_EANCHORLEN) == 0) {
116 			smb2 = p;
117 		}
118 
119 		p += SMB_SCAN_STEP;
120 	}
121 
122 	if (smb2 == NULL && smb3 == NULL) {
123 		psm_unmap_phys(bios, bioslen);
124 		return (smb_open_error(shp, errp, ESMB_NOTFOUND));
125 	}
126 
127 	/*
128 	 * While they're not supposed to (as per the SMBIOS 3.2 spec), some
129 	 * vendors end up having a newer version in one of the two entry points
130 	 * than the other. If we found multiple tables then we will prefer the
131 	 * one with the newer version. If they're equivalent, we prefer the
132 	 * 32-bit version. If only one is present, then we use that.
133 	 */
134 	ep = smb_alloc(SMB_ENTRY_MAXLEN);
135 	if (smb2 != NULL && smb3 != NULL) {
136 		uint8_t smb2maj, smb2min, smb3maj, smb3min;
137 
138 		bcopy(smb2, ep, sizeof (smbios_entry_t));
139 		smb2maj = ep->ep21.smbe_major;
140 		smb2min = ep->ep21.smbe_minor;
141 		bcopy(smb3, ep, sizeof (smbios_entry_t));
142 		smb3maj = ep->ep30.smbe_major;
143 		smb3min = ep->ep30.smbe_minor;
144 
145 		if (smb3maj > smb2maj ||
146 		    (smb3maj == smb2maj && smb3min > smb2min)) {
147 			ep_type = SMBIOS_ENTRY_POINT_30;
148 			p = smb3;
149 		} else {
150 			ep_type = SMBIOS_ENTRY_POINT_21;
151 			p = smb2;
152 		}
153 	} else if (smb3 != NULL) {
154 		ep_type = SMBIOS_ENTRY_POINT_30;
155 		p = smb3;
156 	} else {
157 		ep_type = SMBIOS_ENTRY_POINT_21;
158 		p = smb2;
159 	}
160 	bcopy(p, ep, sizeof (smbios_entry_t));
161 	if (ep_type == SMBIOS_ENTRY_POINT_21) {
162 		ep->ep21.smbe_elen = MIN(ep->ep21.smbe_elen, SMB_ENTRY_MAXLEN);
163 		bcopy(p, ep, ep->ep21.smbe_elen);
164 	} else if (ep_type == SMBIOS_ENTRY_POINT_30) {
165 		ep->ep30.smbe_elen = MIN(ep->ep30.smbe_elen, SMB_ENTRY_MAXLEN);
166 		bcopy(p, ep, ep->ep30.smbe_elen);
167 	}
168 
169 	psm_unmap_phys(bios, bioslen);
170 	switch (ep_type) {
171 	case SMBIOS_ENTRY_POINT_21:
172 		smbe_major = ep->ep21.smbe_major;
173 		smbe_minor = ep->ep21.smbe_minor;
174 		smbe_stlen = ep->ep21.smbe_stlen;
175 		bios = psm_map_phys(ep->ep21.smbe_staddr, smbe_stlen,
176 		    PSM_PROT_READ);
177 		break;
178 	case SMBIOS_ENTRY_POINT_30:
179 		smbe_major = ep->ep30.smbe_major;
180 		smbe_minor = ep->ep30.smbe_minor;
181 		smbe_stlen = ep->ep30.smbe_stlen;
182 		bios = psm_map_phys_new(ep->ep30.smbe_staddr, smbe_stlen,
183 		    PSM_PROT_READ);
184 		break;
185 	default:
186 		smb_free(ep, SMB_ENTRY_MAXLEN);
187 		return (smb_open_error(shp, errp, ESMB_VERSION));
188 	}
189 
190 	if (bios == NULL) {
191 		smb_free(ep, SMB_ENTRY_MAXLEN);
192 		return (smb_open_error(shp, errp, ESMB_MAPDEV));
193 	}
194 
195 	stbuf = smb_alloc(smbe_stlen);
196 	bcopy(bios, stbuf, smbe_stlen);
197 	psm_unmap_phys(bios, smbe_stlen);
198 	shp = smbios_bufopen(ep, stbuf, smbe_stlen, version, flags, &err);
199 
200 	if (shp == NULL) {
201 		smb_free(stbuf, smbe_stlen);
202 		smb_free(ep, SMB_ENTRY_MAXLEN);
203 		return (smb_open_error(shp, errp, err));
204 	}
205 
206 	if (ksmbios == NULL) {
207 		cmn_err(CE_CONT, "?SMBIOS v%u.%u loaded (%u bytes)",
208 		    smbe_major, smbe_minor, smbe_stlen);
209 		if (shp->sh_flags & SMB_FL_TRUNC)
210 			cmn_err(CE_CONT, "?SMBIOS table is truncated");
211 	}
212 
213 	shp->sh_flags |= SMB_FL_BUFALLOC;
214 	smb_free(ep, SMB_ENTRY_MAXLEN);
215 
216 	return (shp);
217 }
218 
219 /*ARGSUSED*/
220 smbios_hdl_t *
221 smbios_fdopen(int fd, int version, int flags, int *errp)
222 {
223 	return (smb_open_error(NULL, errp, ENOTSUP));
224 }
225 
226 /*ARGSUSED*/
227 int
228 smbios_write(smbios_hdl_t *shp, int fd)
229 {
230 	return (smb_set_errno(shp, ENOTSUP));
231 }
232