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*a60349c8SRobert Mustacchi * Copyright 2018 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
smb_strip(smbios_hdl_t * shp)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
75af349cd6SToomas Soome static int
smbios_bufopen_21(smbios_hdl_t * shp,const smbios_21_entry_t * ep,size_t len,int flags)76af349cd6SToomas Soome smbios_bufopen_21(smbios_hdl_t *shp, const smbios_21_entry_t *ep, size_t len,
77af349cd6SToomas Soome int flags)
78af349cd6SToomas Soome {
79af349cd6SToomas Soome if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN))
80af349cd6SToomas Soome return (ESMB_HEADER);
81af349cd6SToomas Soome
82af349cd6SToomas Soome if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN))
83af349cd6SToomas Soome return (ESMB_HEADER);
84af349cd6SToomas Soome
85af349cd6SToomas Soome smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n",
86af349cd6SToomas Soome ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev);
87af349cd6SToomas Soome
88af349cd6SToomas Soome if (!(flags & SMB_O_NOVERS)) {
89af349cd6SToomas Soome if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
90af349cd6SToomas Soome return (ESMB_NEW);
91af349cd6SToomas Soome
92af349cd6SToomas Soome if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
93af349cd6SToomas Soome ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
94af349cd6SToomas Soome ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
95af349cd6SToomas Soome return (ESMB_OLD);
96af349cd6SToomas Soome }
97af349cd6SToomas Soome
98af349cd6SToomas Soome if (len < sizeof (smb_header_t) ||
99af349cd6SToomas Soome ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
100af349cd6SToomas Soome return (ESMB_SHORT);
101af349cd6SToomas Soome
102af349cd6SToomas Soome if (!(flags & SMB_O_NOCKSUM)) {
103af349cd6SToomas Soome uint8_t esum = 0, isum = 0;
104af349cd6SToomas Soome const uchar_t *p, *q;
105af349cd6SToomas Soome q = (uchar_t *)ep;
106af349cd6SToomas Soome
107af349cd6SToomas Soome for (p = q; p < q + ep->smbe_elen; p++)
108af349cd6SToomas Soome esum += *p;
109af349cd6SToomas Soome
110af349cd6SToomas Soome for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
111af349cd6SToomas Soome isum += *p;
112af349cd6SToomas Soome
113af349cd6SToomas Soome if (esum != 0 || isum != 0) {
114af349cd6SToomas Soome smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum);
115af349cd6SToomas Soome return (ESMB_CKSUM);
116af349cd6SToomas Soome }
117af349cd6SToomas Soome }
118af349cd6SToomas Soome
119af349cd6SToomas Soome /*
120af349cd6SToomas Soome * Copy the entry point into our handle. The underlying entry point
121af349cd6SToomas Soome * may be larger than our structure definition, so reset smbe_elen
122af349cd6SToomas Soome * to our internal size and recompute good checksums for our copy.
123af349cd6SToomas Soome */
124af349cd6SToomas Soome shp->sh_ent_type = SMBIOS_ENTRY_POINT_21;
125af349cd6SToomas Soome bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
126af349cd6SToomas Soome shp->sh_ent.ep21.smbe_elen = sizeof (smbios_entry_t);
127af349cd6SToomas Soome smbios_checksum(shp, &shp->sh_ent);
128af349cd6SToomas Soome
129af349cd6SToomas Soome shp->sh_ent_stnum = ep->smbe_stnum;
130af349cd6SToomas Soome shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
131af349cd6SToomas Soome return (0);
132af349cd6SToomas Soome }
133af349cd6SToomas Soome
134af349cd6SToomas Soome static int
smbios_bufopen_30(smbios_hdl_t * shp,const smbios_30_entry_t * ep,size_t len,int flags)135af349cd6SToomas Soome smbios_bufopen_30(smbios_hdl_t *shp, const smbios_30_entry_t *ep, size_t len,
136af349cd6SToomas Soome int flags)
137af349cd6SToomas Soome {
138af349cd6SToomas Soome if (strncmp(ep->smbe_eanchor, SMB3_ENTRY_EANCHOR,
139af349cd6SToomas Soome SMB3_ENTRY_EANCHORLEN))
140af349cd6SToomas Soome return (ESMB_HEADER);
141af349cd6SToomas Soome
142af349cd6SToomas Soome smb_dprintf(shp, "opening SMBIOS version %u.%u\n",
143af349cd6SToomas Soome ep->smbe_major, ep->smbe_minor);
144af349cd6SToomas Soome
145af349cd6SToomas Soome if (!(flags & SMB_O_NOVERS)) {
146af349cd6SToomas Soome if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
147af349cd6SToomas Soome return (ESMB_NEW);
148af349cd6SToomas Soome
149af349cd6SToomas Soome if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
150af349cd6SToomas Soome ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
151af349cd6SToomas Soome ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
152af349cd6SToomas Soome return (ESMB_OLD);
153af349cd6SToomas Soome }
154af349cd6SToomas Soome
155af349cd6SToomas Soome if (len < sizeof (smb_header_t) ||
156af349cd6SToomas Soome ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
157af349cd6SToomas Soome return (ESMB_SHORT);
158af349cd6SToomas Soome
159af349cd6SToomas Soome if (!(flags & SMB_O_NOCKSUM)) {
160af349cd6SToomas Soome uint8_t esum = 0;
161af349cd6SToomas Soome const uchar_t *p, *q;
162af349cd6SToomas Soome q = (uchar_t *)ep;
163af349cd6SToomas Soome
164af349cd6SToomas Soome for (p = q; p < q + ep->smbe_elen; p++)
165af349cd6SToomas Soome esum += *p;
166af349cd6SToomas Soome
167af349cd6SToomas Soome if (esum != 0) {
168af349cd6SToomas Soome smb_dprintf(shp, "bad cksum: e=%x\n", esum);
169af349cd6SToomas Soome return (ESMB_CKSUM);
170af349cd6SToomas Soome }
171af349cd6SToomas Soome }
172af349cd6SToomas Soome
173af349cd6SToomas Soome /*
174af349cd6SToomas Soome * Copy the entry point into our handle. The underlying entry point
175af349cd6SToomas Soome * may be larger than our structure definition, so reset smbe_elen
176af349cd6SToomas Soome * to our internal size and recompute good checksums for our copy.
177af349cd6SToomas Soome */
178af349cd6SToomas Soome shp->sh_ent_type = SMBIOS_ENTRY_POINT_30;
179af349cd6SToomas Soome bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
180af349cd6SToomas Soome shp->sh_ent.ep30.smbe_elen = sizeof (smbios_entry_t);
181af349cd6SToomas Soome smbios_checksum(shp, &shp->sh_ent);
182af349cd6SToomas Soome
183af349cd6SToomas Soome shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
184af349cd6SToomas Soome
185af349cd6SToomas Soome return (0);
186af349cd6SToomas Soome }
187af349cd6SToomas Soome
188af349cd6SToomas Soome static uint_t
smbios_table_nentries(const char * smbe_staddr,uint32_t smbe_stlen)189af349cd6SToomas Soome smbios_table_nentries(const char *smbe_staddr, uint32_t smbe_stlen)
190af349cd6SToomas Soome {
191af349cd6SToomas Soome uint_t i = 0;
192af349cd6SToomas Soome char *dmi;
193af349cd6SToomas Soome smb_header_t *hdr;
194af349cd6SToomas Soome
195af349cd6SToomas Soome if (smbe_staddr == NULL)
196af349cd6SToomas Soome return (i);
197af349cd6SToomas Soome
198af349cd6SToomas Soome for (dmi = (char *)smbe_staddr; dmi < smbe_staddr + smbe_stlen; i++) {
199af349cd6SToomas Soome hdr = (smb_header_t *)dmi;
200af349cd6SToomas Soome dmi += hdr->smbh_len;
201af349cd6SToomas Soome /*
202af349cd6SToomas Soome * Search for the end of the string area.
203af349cd6SToomas Soome */
204af349cd6SToomas Soome while (dmi + 1 < smbe_staddr + smbe_stlen &&
205af349cd6SToomas Soome dmi[0] != '\0' && dmi[1] != '\0') {
206af349cd6SToomas Soome dmi++;
207af349cd6SToomas Soome }
208af349cd6SToomas Soome dmi += 2;
209af349cd6SToomas Soome }
210af349cd6SToomas Soome return (i);
211af349cd6SToomas Soome }
212af349cd6SToomas Soome
21384ab085aSmws smbios_hdl_t *
smbios_bufopen(const smbios_entry_t * ep,const void * buf,size_t len,int version,int flags,int * errp)21484ab085aSmws smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len,
21584ab085aSmws int version, int flags, int *errp)
21684ab085aSmws {
21784ab085aSmws smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t));
21884ab085aSmws const smb_header_t *hp, *nhp;
21984ab085aSmws const uchar_t *p, *q, *s;
22084ab085aSmws uint_t i, h;
221af349cd6SToomas Soome int err;
22284ab085aSmws
22384ab085aSmws switch (version) {
22484ab085aSmws case SMB_VERSION_23:
22584ab085aSmws case SMB_VERSION_24:
2264e901881SDale Ghent case SMB_VERSION_25:
2274e901881SDale Ghent case SMB_VERSION_26:
2284e901881SDale Ghent case SMB_VERSION_27:
2294e901881SDale Ghent case SMB_VERSION_28:
23004cdb0e3SRobert Mustacchi case SMB_VERSION_30:
2311eb9e3eaSRobert Mustacchi case SMB_VERSION_31:
232*a60349c8SRobert Mustacchi case SMB_VERSION_32:
23384ab085aSmws break;
23484ab085aSmws default:
23584ab085aSmws return (smb_open_error(shp, errp, ESMB_VERSION));
23684ab085aSmws }
23784ab085aSmws
23884ab085aSmws if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
23984ab085aSmws return (smb_open_error(shp, errp, ESMB_INVAL));
24084ab085aSmws
24184ab085aSmws if (shp == NULL)
24284ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM));
24384ab085aSmws
24484ab085aSmws if (_smb_debug)
24584ab085aSmws shp->sh_flags |= SMB_FL_DEBUG;
24684ab085aSmws
247af349cd6SToomas Soome err = smbios_bufopen_21(shp, &ep->ep21, len, flags);
248af349cd6SToomas Soome if (err != 0) {
249af349cd6SToomas Soome err = smbios_bufopen_30(shp, &ep->ep30, len, flags);
250af349cd6SToomas Soome if (err != 0)
251af349cd6SToomas Soome return (smb_open_error(shp, errp, err));
252af349cd6SToomas Soome shp->sh_ent_stnum =
253af349cd6SToomas Soome smbios_table_nentries(buf, ep->ep30.smbe_stlen);
25484ab085aSmws }
25584ab085aSmws
25684ab085aSmws shp->sh_buf = buf;
25784ab085aSmws shp->sh_buflen = len;
258af349cd6SToomas Soome shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * shp->sh_ent_stnum);
25984ab085aSmws shp->sh_nstructs = 0;
26084ab085aSmws shp->sh_hashlen = _smb_hashlen;
26184ab085aSmws shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
26284ab085aSmws shp->sh_libvers = version;
26384ab085aSmws
26484ab085aSmws if (shp->sh_structs == NULL || shp->sh_hash == NULL)
26584ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM));
26684ab085aSmws
26784ab085aSmws hp = shp->sh_buf;
268af349cd6SToomas Soome switch (shp->sh_ent_type) {
269af349cd6SToomas Soome case SMBIOS_ENTRY_POINT_21:
270af349cd6SToomas Soome q = (const uchar_t *)buf + MIN(ep->ep21.smbe_stlen, len);
271af349cd6SToomas Soome break;
272af349cd6SToomas Soome case SMBIOS_ENTRY_POINT_30:
273af349cd6SToomas Soome q = (const uchar_t *)buf + MIN(ep->ep30.smbe_stlen, len);
274af349cd6SToomas Soome break;
275af349cd6SToomas Soome default:
276af349cd6SToomas Soome return (smb_open_error(shp, errp, ESMB_VERSION));
277af349cd6SToomas Soome }
27884ab085aSmws
279af349cd6SToomas Soome for (i = 0; i < shp->sh_ent_stnum; i++, hp = nhp) {
28084ab085aSmws smb_struct_t *stp = &shp->sh_structs[i];
28184ab085aSmws uint_t n = 0;
28284ab085aSmws
283dabec466SJonathan Matthew if ((const uchar_t *)hp + sizeof (smb_header_t) > q) {
284dabec466SJonathan Matthew shp->sh_flags |= SMB_FL_TRUNC;
285dabec466SJonathan Matthew break;
286dabec466SJonathan Matthew }
28784ab085aSmws
28884ab085aSmws smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
28984ab085aSmws i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
29084ab085aSmws
29184ab085aSmws if (hp->smbh_type == SMB_TYPE_EOT)
29284ab085aSmws break; /* ignore any entries beyond end-of-table */
29384ab085aSmws
294dabec466SJonathan Matthew if ((const uchar_t *)hp + hp->smbh_len > q - 2) {
295dabec466SJonathan Matthew shp->sh_flags |= SMB_FL_TRUNC;
296dabec466SJonathan Matthew break;
297dabec466SJonathan Matthew }
298e4586ebfSmws
29984ab085aSmws h = hp->smbh_hdl & (shp->sh_hashlen - 1);
30084ab085aSmws p = s = (const uchar_t *)hp + hp->smbh_len;
30184ab085aSmws
30284ab085aSmws while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
30384ab085aSmws if (*p++ == '\0')
30484ab085aSmws n++; /* count strings until \0\0 delimiter */
30584ab085aSmws }
30684ab085aSmws
307dabec466SJonathan Matthew if (p > q - 2) {
308dabec466SJonathan Matthew shp->sh_flags |= SMB_FL_TRUNC;
309dabec466SJonathan Matthew break;
310dabec466SJonathan Matthew }
31184ab085aSmws
31284ab085aSmws if (p > s)
31384ab085aSmws n++; /* add one for final string in string table */
31484ab085aSmws
31584ab085aSmws stp->smbst_hdr = hp;
31684ab085aSmws stp->smbst_str = s;
31784ab085aSmws stp->smbst_end = p;
31884ab085aSmws stp->smbst_next = shp->sh_hash[h];
31984ab085aSmws stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
32084ab085aSmws stp->smbst_strtablen = n;
32184ab085aSmws
32284ab085aSmws if (n != 0 && stp->smbst_strtab == NULL)
32384ab085aSmws return (smb_open_error(shp, errp, ESMB_NOMEM));
32484ab085aSmws
32584ab085aSmws shp->sh_hash[h] = stp;
32684ab085aSmws nhp = (void *)(p + 2);
32784ab085aSmws shp->sh_nstructs++;
32884ab085aSmws
32984ab085aSmws for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
33084ab085aSmws if (*p == '\0') {
33184ab085aSmws stp->smbst_strtab[n++] =
33284ab085aSmws (uint16_t)(s - stp->smbst_str);
33384ab085aSmws s = p + 1;
33484ab085aSmws }
33584ab085aSmws }
33684ab085aSmws }
33784ab085aSmws
338dabec466SJonathan Matthew /* error out if we couldn't find any complete entries in the table */
339dabec466SJonathan Matthew if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0)
340dabec466SJonathan Matthew return (smb_open_error(shp, errp, ESMB_CORRUPT));
341dabec466SJonathan Matthew
34284ab085aSmws if (flags & SMB_O_ZIDS)
34384ab085aSmws smb_strip(shp);
34484ab085aSmws
34584ab085aSmws return (shp);
34684ab085aSmws }
34784ab085aSmws
34884ab085aSmws void
smbios_close(smbios_hdl_t * shp)34984ab085aSmws smbios_close(smbios_hdl_t *shp)
35084ab085aSmws {
35184ab085aSmws uint_t i;
35284ab085aSmws
35384ab085aSmws for (i = 0; i < shp->sh_nstructs; i++) {
35484ab085aSmws smb_free(shp->sh_structs[i].smbst_strtab,
35584ab085aSmws sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
35684ab085aSmws }
35784ab085aSmws
358af349cd6SToomas Soome smb_free(shp->sh_structs, sizeof (smb_struct_t) * shp->sh_ent_stnum);
35984ab085aSmws smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
36084ab085aSmws
36184ab085aSmws if (shp->sh_flags & SMB_FL_BUFALLOC)
36284ab085aSmws smb_free((void *)shp->sh_buf, shp->sh_buflen);
36384ab085aSmws
36484ab085aSmws smb_free(shp, sizeof (smbios_hdl_t));
36584ab085aSmws }
36684ab085aSmws
36784ab085aSmws /*
36884ab085aSmws * Recompute the values of the entry point checksums based upon the content
36984ab085aSmws * of the specified SMBIOS entry point. We don't need 'shp' but require it
37084ab085aSmws * anyway in case future versioning requires variations in the algorithm.
37184ab085aSmws */
37284ab085aSmws /*ARGSUSED*/
37384ab085aSmws void
smbios_checksum(smbios_hdl_t * shp,smbios_entry_t * ep)37484ab085aSmws smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
37584ab085aSmws {
37684ab085aSmws uchar_t *p, *q = (uchar_t *)ep;
37784ab085aSmws uint8_t esum = 0, isum = 0;
37884ab085aSmws
379af349cd6SToomas Soome switch (shp->sh_ent_type) {
380af349cd6SToomas Soome case SMBIOS_ENTRY_POINT_21:
381af349cd6SToomas Soome ep->ep21.smbe_ecksum = ep->ep21.smbe_icksum = 0;
38284ab085aSmws
383af349cd6SToomas Soome for (p = (uchar_t *)ep->ep21.smbe_ianchor;
384af349cd6SToomas Soome p < q + sizeof (*ep); p++) {
38584ab085aSmws isum += *p;
386af349cd6SToomas Soome }
38784ab085aSmws
388af349cd6SToomas Soome ep->ep21.smbe_icksum = -isum;
38984ab085aSmws
390af349cd6SToomas Soome for (p = q; p < q + ep->ep21.smbe_elen; p++)
39184ab085aSmws esum += *p;
39284ab085aSmws
393af349cd6SToomas Soome ep->ep21.smbe_ecksum = -esum;
394af349cd6SToomas Soome break;
395af349cd6SToomas Soome case SMBIOS_ENTRY_POINT_30:
396af349cd6SToomas Soome ep->ep30.smbe_ecksum = 0;
397af349cd6SToomas Soome for (p = q; p < q + ep->ep30.smbe_elen; p++)
398af349cd6SToomas Soome esum += *p;
399af349cd6SToomas Soome
400af349cd6SToomas Soome ep->ep30.smbe_ecksum = -esum;
401af349cd6SToomas Soome break;
402af349cd6SToomas Soome default:
403af349cd6SToomas Soome break;
404af349cd6SToomas Soome }
40584ab085aSmws }
40684ab085aSmws
40784ab085aSmws const void *
smbios_buf(smbios_hdl_t * shp)40884ab085aSmws smbios_buf(smbios_hdl_t *shp)
40984ab085aSmws {
41084ab085aSmws return (shp->sh_buf);
41184ab085aSmws }
41284ab085aSmws
41384ab085aSmws size_t
smbios_buflen(smbios_hdl_t * shp)41484ab085aSmws smbios_buflen(smbios_hdl_t *shp)
41584ab085aSmws {
41684ab085aSmws return (shp->sh_buflen);
41784ab085aSmws }
41884ab085aSmws
41984ab085aSmws static smbios_struct_t *
smb_export(const smb_struct_t * stp,smbios_struct_t * sp)42084ab085aSmws smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
42184ab085aSmws {
42284ab085aSmws const smb_header_t *hdr = stp->smbst_hdr;
42384ab085aSmws
42484ab085aSmws sp->smbstr_id = hdr->smbh_hdl;
42584ab085aSmws sp->smbstr_type = hdr->smbh_type;
42684ab085aSmws sp->smbstr_data = hdr;
42784ab085aSmws sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
42884ab085aSmws
42984ab085aSmws return (sp);
43084ab085aSmws }
43184ab085aSmws
43284ab085aSmws int
smbios_lookup_id(smbios_hdl_t * shp,id_t id,smbios_struct_t * sp)43384ab085aSmws smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
43484ab085aSmws {
43584ab085aSmws const smb_struct_t *stp = smb_lookup_id(shp, id);
43684ab085aSmws
43784ab085aSmws if (stp == NULL)
43884ab085aSmws return (-1); /* errno is set for us */
43984ab085aSmws
44084ab085aSmws if (sp != NULL)
44184ab085aSmws (void) smb_export(stp, sp);
44284ab085aSmws
44384ab085aSmws return (0);
44484ab085aSmws }
44584ab085aSmws
44684ab085aSmws int
smbios_lookup_type(smbios_hdl_t * shp,uint_t type,smbios_struct_t * sp)447074bb90dSTom Pothier smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp)
448074bb90dSTom Pothier {
449074bb90dSTom Pothier const smb_struct_t *stp = smb_lookup_type(shp, type);
450074bb90dSTom Pothier
451074bb90dSTom Pothier if (stp == NULL)
452074bb90dSTom Pothier return (-1); /* errno is set for us */
453074bb90dSTom Pothier
454074bb90dSTom Pothier if (sp != NULL)
455074bb90dSTom Pothier (void) smb_export(stp, sp);
456074bb90dSTom Pothier
457074bb90dSTom Pothier return (0);
458074bb90dSTom Pothier }
459074bb90dSTom Pothier
460074bb90dSTom Pothier int
smbios_iter(smbios_hdl_t * shp,smbios_struct_f * func,void * data)46184ab085aSmws smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
46284ab085aSmws {
46384ab085aSmws const smb_struct_t *sp = shp->sh_structs;
46484ab085aSmws smbios_struct_t s;
46584ab085aSmws int i, rv = 0;
46684ab085aSmws
46784ab085aSmws for (i = 0; i < shp->sh_nstructs; i++, sp++) {
46884ab085aSmws if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
46984ab085aSmws (rv = func(shp, smb_export(sp, &s), data)) != 0)
47084ab085aSmws break;
47184ab085aSmws }
47284ab085aSmws
47384ab085aSmws return (rv);
47484ab085aSmws }
47584ab085aSmws
47684ab085aSmws const smb_struct_t *
smb_lookup_type(smbios_hdl_t * shp,uint_t type)47784ab085aSmws smb_lookup_type(smbios_hdl_t *shp, uint_t type)
47884ab085aSmws {
47984ab085aSmws uint_t i;
48084ab085aSmws
48184ab085aSmws for (i = 0; i < shp->sh_nstructs; i++) {
48284ab085aSmws if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
48384ab085aSmws return (&shp->sh_structs[i]);
48484ab085aSmws }
48584ab085aSmws
48684ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT);
48784ab085aSmws return (NULL);
48884ab085aSmws }
48984ab085aSmws
49084ab085aSmws const smb_struct_t *
smb_lookup_id(smbios_hdl_t * shp,uint_t id)49184ab085aSmws smb_lookup_id(smbios_hdl_t *shp, uint_t id)
49284ab085aSmws {
49384ab085aSmws const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
49484ab085aSmws
49584ab085aSmws switch (id) {
49684ab085aSmws case SMB_ID_NOTSUP:
49784ab085aSmws (void) smb_set_errno(shp, ESMB_NOTSUP);
49884ab085aSmws return (NULL);
49984ab085aSmws case SMB_ID_NONE:
50084ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT);
50184ab085aSmws return (NULL);
50284ab085aSmws }
50384ab085aSmws
50484ab085aSmws for (; stp != NULL; stp = stp->smbst_next) {
50584ab085aSmws if (stp->smbst_hdr->smbh_hdl == id)
50684ab085aSmws break;
50784ab085aSmws }
50884ab085aSmws
50984ab085aSmws if (stp == NULL)
51084ab085aSmws (void) smb_set_errno(shp, ESMB_NOENT);
51184ab085aSmws
51284ab085aSmws return (stp);
51384ab085aSmws }
51484ab085aSmws
51584ab085aSmws const char *
smb_strptr(const smb_struct_t * stp,uint_t i)51684ab085aSmws smb_strptr(const smb_struct_t *stp, uint_t i)
51784ab085aSmws {
51884ab085aSmws if (i == 0 || i > stp->smbst_strtablen)
51984ab085aSmws return (_smb_emptystr);
52084ab085aSmws else
52184ab085aSmws return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
52284ab085aSmws }
52384ab085aSmws
52484ab085aSmws int
smb_libgteq(smbios_hdl_t * shp,int version)5251eb9e3eaSRobert Mustacchi smb_libgteq(smbios_hdl_t *shp, int version)
5261eb9e3eaSRobert Mustacchi {
5271eb9e3eaSRobert Mustacchi return (SMB_MAJOR(shp->sh_libvers) > SMB_MAJOR(version) || (
5281eb9e3eaSRobert Mustacchi SMB_MAJOR(shp->sh_libvers) == SMB_MAJOR(version) &&
5291eb9e3eaSRobert Mustacchi SMB_MINOR(shp->sh_libvers) >= SMB_MINOR(version)));
5301eb9e3eaSRobert Mustacchi }
5311eb9e3eaSRobert Mustacchi
5321eb9e3eaSRobert Mustacchi int
smb_gteq(smbios_hdl_t * shp,int version)53384ab085aSmws smb_gteq(smbios_hdl_t *shp, int version)
53484ab085aSmws {
53584ab085aSmws return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
53684ab085aSmws SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
53784ab085aSmws SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
53884ab085aSmws }
539dabec466SJonathan Matthew
540dabec466SJonathan Matthew boolean_t
smbios_truncated(smbios_hdl_t * shp)541dabec466SJonathan Matthew smbios_truncated(smbios_hdl_t *shp)
542dabec466SJonathan Matthew {
543dabec466SJonathan Matthew return ((shp->sh_flags & SMB_FL_TRUNC) != 0);
544dabec466SJonathan Matthew }
545