xref: /illumos-gate/usr/src/uts/i86pc/os/smb_dev.c (revision 55a13001fbd9772352bc050632ef966a249dc73b)
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 	size_t bioslen;
68 	int err;
69 
70 	if (file != NULL || (flags & ~SMB_O_MASK))
71 		return (smb_open_error(shp, errp, ESMB_INVAL));
72 
73 	bioslen = SMB_RANGE_LIMIT - SMB_RANGE_START + 1;
74 	bios = psm_map_phys(SMB_RANGE_START, bioslen, PSM_PROT_READ);
75 
76 	if (bios == NULL)
77 		return (smb_open_error(shp, errp, ESMB_MAPDEV));
78 
79 	for (p = bios, q = bios + bioslen; p < q; p += 16) {
80 		if (strncmp(p, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN) == 0)
81 			break;
82 	}
83 
84 	if (p >= q) {
85 		psm_unmap_phys(bios, bioslen);
86 		return (smb_open_error(shp, errp, ESMB_NOTFOUND));
87 	}
88 
89 	ep = smb_alloc(SMB_ENTRY_MAXLEN);
90 	bcopy(p, ep, sizeof (smbios_entry_t));
91 	ep->smbe_elen = MIN(ep->smbe_elen, SMB_ENTRY_MAXLEN);
92 	bcopy(p, ep, ep->smbe_elen);
93 
94 	psm_unmap_phys(bios, bioslen);
95 	bios = psm_map_phys(ep->smbe_staddr, ep->smbe_stlen, PSM_PROT_READ);
96 
97 	if (bios == NULL) {
98 		smb_free(ep, SMB_ENTRY_MAXLEN);
99 		return (smb_open_error(shp, errp, ESMB_MAPDEV));
100 	}
101 
102 	stbuf = smb_alloc(ep->smbe_stlen);
103 	bcopy(bios, stbuf, ep->smbe_stlen);
104 	psm_unmap_phys(bios, ep->smbe_stlen);
105 	shp = smbios_bufopen(ep, stbuf, ep->smbe_stlen, version, flags, &err);
106 
107 	if (shp == NULL) {
108 		smb_free(stbuf, ep->smbe_stlen);
109 		smb_free(ep, SMB_ENTRY_MAXLEN);
110 		return (smb_open_error(shp, errp, err));
111 	}
112 
113 	if (ksmbios == NULL) {
114 		cmn_err(CE_CONT, "?SMBIOS v%u.%u loaded (%u bytes)",
115 		    ep->smbe_major, ep->smbe_minor, ep->smbe_stlen);
116 		if (shp->sh_flags & SMB_FL_TRUNC)
117 			cmn_err(CE_CONT, "?SMBIOS table is truncated");
118 	}
119 
120 	shp->sh_flags |= SMB_FL_BUFALLOC;
121 	smb_free(ep, SMB_ENTRY_MAXLEN);
122 
123 	return (shp);
124 }
125 
126 /*ARGSUSED*/
127 smbios_hdl_t *
128 smbios_fdopen(int fd, int version, int flags, int *errp)
129 {
130 	return (smb_open_error(NULL, errp, ENOTSUP));
131 }
132 
133 /*ARGSUSED*/
134 int
135 smbios_write(smbios_hdl_t *shp, int fd)
136 {
137 	return (smb_set_errno(shp, ENOTSUP));
138 }
139