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