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