1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Retrieval of the DIMM serial number from data encoded in the SPD and
* SEEPROM formats.
*/
#include <mem_spd.h>
#include <mem_seeprom.h>
#include <mem.h>
#include <fm/fmd_fmri.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <strings.h>
#include <sys/byteorder.h>
#include <sys/stat.h>
#include <sys/types.h>
#define BUFSIZ_SPD 256
#define BUFSIZ_SEEPROM 8192
/*
* SEEPROMs are composed of self-describing containers. The first container,
* starting at offset 0, is r/w. The second container, starting at 0x1800, is
* r/o, and contains identification information supplied by the manufacturer.
*/
#define SEEPROM_OFFSET_RO 0x1800
static int
mem_get_spd_serid(const char *buf, size_t bufsz, char *serid, size_t seridsz)
{
static const char hex_digits[] = "0123456789ABCDEF";
spd_data_t *spd = (spd_data_t *)buf;
char *c;
int i;
if (bufsz < sizeof (spd_data_t))
return (fmd_fmri_set_errno(EINVAL));
if (seridsz < sizeof (spd->asmb_serial_no) * 2 + 1)
return (fmd_fmri_set_errno(EINVAL));
for (c = serid, i = 0; i < sizeof (spd->asmb_serial_no); i++) {
*c++ = hex_digits[spd->asmb_serial_no[i] >> 4];
*c++ = hex_digits[spd->asmb_serial_no[i] & 0xf];
}
*c = '\0';
return (0);
}
static void *
seeprom_seg_lookup(const char *buf, size_t bufsz, char *segname, size_t *segszp)
{
seeprom_container_t *sc;
seeprom_seg_t *segp, seg;
int sidx;
if (strlen(segname) != sizeof (seg.sees_name))
return (NULL);
sc = (seeprom_container_t *)(buf + SEEPROM_OFFSET_RO);
/* Validate sc size then dereference it */
if (bufsz < SEEPROM_OFFSET_RO + sizeof (seeprom_container_t) ||
bufsz < SEEPROM_OFFSET_RO + sizeof (seeprom_container_t) +
sc->seec_contsz)
return (NULL);
if (sc->seec_tag == 0 || sc->seec_contsz == 0 ||
sc->seec_nsegs == 0)
return (NULL);
for (sidx = 0; sidx < sc->seec_nsegs; sidx++) {
/* LINTED - pointer alignment */
segp = ((seeprom_seg_t *)(sc + 1)) + sidx;
bcopy(segp, &seg, sizeof (seeprom_seg_t));
seg.sees_segoff = ntohs(seg.sees_segoff);
seg.sees_seglen = ntohs(seg.sees_seglen);
if (bufsz < seg.sees_segoff + seg.sees_seglen)
return (NULL);
if (strncmp(segname, seg.sees_name,
sizeof (seg.sees_name)) == 0) {
*segszp = seg.sees_seglen;
return ((void *)(buf + seg.sees_segoff));
}
}
return (NULL);
}
static int
mem_get_seeprom_serid(const char *buf, size_t bufsz, char *serid,
size_t seridsz)
{
seeprom_seg_sd_t *sd;
size_t segsz;
if (seridsz < sizeof (sd->seesd_sun_sno) + 1)
return (fmd_fmri_set_errno(EINVAL));
if ((sd = seeprom_seg_lookup(buf, bufsz, "SD", &segsz)) == NULL)
return (fmd_fmri_set_errno(EINVAL));
if (segsz < sizeof (seeprom_seg_sd_t))
return (fmd_fmri_set_errno(EINVAL));
bcopy(sd->seesd_sun_sno, serid, sizeof (sd->seesd_sun_sno));
serid[sizeof (sd->seesd_sun_sno)] = '\0';
return (0);
}
int
mem_get_serid(const char *device, char *serid, size_t seridsz)
{
char buf[8192];
int fd;
ssize_t sz;
if ((fd = open(device, O_RDONLY)) < 0)
return (-1); /* errno is set for us */
if ((sz = read(fd, buf, sizeof (buf))) < 0) {
int err = errno;
(void) close(fd);
return (fmd_fmri_set_errno(err));
}
(void) close(fd);
switch (sz) {
case BUFSIZ_SPD:
return (mem_get_spd_serid(buf, BUFSIZ_SPD, serid, seridsz));
case BUFSIZ_SEEPROM:
return (mem_get_seeprom_serid(buf, BUFSIZ_SEEPROM, serid,
seridsz));
default:
return (fmd_fmri_set_errno(EINVAL));
}
}
|