xref: /illumos-gate/usr/src/cmd/fm/schemes/mem/mem_read.c (revision 2a8bcb4e)
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 /*
28*7c478bd9Sstevel@tonic-gate  * Retrieval of the DIMM serial number from data encoded in the SPD and
29*7c478bd9Sstevel@tonic-gate  * SEEPROM formats.
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <mem_spd.h>
33*7c478bd9Sstevel@tonic-gate #include <mem_seeprom.h>
34*7c478bd9Sstevel@tonic-gate #include <mem.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <fm/fmd_fmri.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include <stdio.h>
39*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
40*7c478bd9Sstevel@tonic-gate #include <errno.h>
41*7c478bd9Sstevel@tonic-gate #include <unistd.h>
42*7c478bd9Sstevel@tonic-gate #include <strings.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/byteorder.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #define	BUFSIZ_SPD	256
48*7c478bd9Sstevel@tonic-gate #define	BUFSIZ_SEEPROM	8192
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate  * SEEPROMs are composed of self-describing containers.  The first container,
52*7c478bd9Sstevel@tonic-gate  * starting at offset 0, is r/w.  The second container, starting at 0x1800, is
53*7c478bd9Sstevel@tonic-gate  * r/o, and contains identification information supplied by the manufacturer.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate #define	SEEPROM_OFFSET_RO	0x1800
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static int
mem_get_spd_serid(const char * buf,size_t bufsz,char * serid,size_t seridsz)58*7c478bd9Sstevel@tonic-gate mem_get_spd_serid(const char *buf, size_t bufsz, char *serid, size_t seridsz)
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	static const char hex_digits[] = "0123456789ABCDEF";
61*7c478bd9Sstevel@tonic-gate 	spd_data_t *spd = (spd_data_t *)buf;
62*7c478bd9Sstevel@tonic-gate 	char *c;
63*7c478bd9Sstevel@tonic-gate 	int i;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	if (bufsz < sizeof (spd_data_t))
66*7c478bd9Sstevel@tonic-gate 		return (fmd_fmri_set_errno(EINVAL));
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	if (seridsz < sizeof (spd->asmb_serial_no) * 2 + 1)
69*7c478bd9Sstevel@tonic-gate 		return (fmd_fmri_set_errno(EINVAL));
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	for (c = serid, i = 0; i < sizeof (spd->asmb_serial_no); i++) {
72*7c478bd9Sstevel@tonic-gate 		*c++ = hex_digits[spd->asmb_serial_no[i] >> 4];
73*7c478bd9Sstevel@tonic-gate 		*c++ = hex_digits[spd->asmb_serial_no[i] & 0xf];
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 	*c = '\0';
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	return (0);
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate static void *
seeprom_seg_lookup(const char * buf,size_t bufsz,char * segname,size_t * segszp)81*7c478bd9Sstevel@tonic-gate seeprom_seg_lookup(const char *buf, size_t bufsz, char *segname, size_t *segszp)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	seeprom_container_t *sc;
84*7c478bd9Sstevel@tonic-gate 	seeprom_seg_t *segp, seg;
85*7c478bd9Sstevel@tonic-gate 	int sidx;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	if (strlen(segname) != sizeof (seg.sees_name))
88*7c478bd9Sstevel@tonic-gate 		return (NULL);
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	sc = (seeprom_container_t *)(buf + SEEPROM_OFFSET_RO);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	/* Validate sc size then dereference it */
93*7c478bd9Sstevel@tonic-gate 	if (bufsz < SEEPROM_OFFSET_RO + sizeof (seeprom_container_t) ||
94*7c478bd9Sstevel@tonic-gate 	    bufsz < SEEPROM_OFFSET_RO + sizeof (seeprom_container_t) +
95*7c478bd9Sstevel@tonic-gate 	    sc->seec_contsz)
96*7c478bd9Sstevel@tonic-gate 		return (NULL);
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	if (sc->seec_tag == 0 || sc->seec_contsz == 0 ||
99*7c478bd9Sstevel@tonic-gate 	    sc->seec_nsegs == 0)
100*7c478bd9Sstevel@tonic-gate 		return (NULL);
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	for (sidx = 0; sidx < sc->seec_nsegs; sidx++) {
103*7c478bd9Sstevel@tonic-gate 		/* LINTED - pointer alignment */
104*7c478bd9Sstevel@tonic-gate 		segp = ((seeprom_seg_t *)(sc + 1)) + sidx;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 		bcopy(segp, &seg, sizeof (seeprom_seg_t));
107*7c478bd9Sstevel@tonic-gate 		seg.sees_segoff = ntohs(seg.sees_segoff);
108*7c478bd9Sstevel@tonic-gate 		seg.sees_seglen = ntohs(seg.sees_seglen);
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 		if (bufsz < seg.sees_segoff + seg.sees_seglen)
111*7c478bd9Sstevel@tonic-gate 			return (NULL);
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 		if (strncmp(segname, seg.sees_name,
114*7c478bd9Sstevel@tonic-gate 		    sizeof (seg.sees_name)) == 0) {
115*7c478bd9Sstevel@tonic-gate 			*segszp = seg.sees_seglen;
116*7c478bd9Sstevel@tonic-gate 			return ((void *)(buf + seg.sees_segoff));
117*7c478bd9Sstevel@tonic-gate 		}
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	return (NULL);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate static int
mem_get_seeprom_serid(const char * buf,size_t bufsz,char * serid,size_t seridsz)125*7c478bd9Sstevel@tonic-gate mem_get_seeprom_serid(const char *buf, size_t bufsz, char *serid,
126*7c478bd9Sstevel@tonic-gate     size_t seridsz)
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	seeprom_seg_sd_t *sd;
129*7c478bd9Sstevel@tonic-gate 	size_t segsz;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	if (seridsz < sizeof (sd->seesd_sun_sno) + 1)
132*7c478bd9Sstevel@tonic-gate 		return (fmd_fmri_set_errno(EINVAL));
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if ((sd = seeprom_seg_lookup(buf, bufsz, "SD", &segsz)) == NULL)
135*7c478bd9Sstevel@tonic-gate 		return (fmd_fmri_set_errno(EINVAL));
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if (segsz < sizeof (seeprom_seg_sd_t))
138*7c478bd9Sstevel@tonic-gate 		return (fmd_fmri_set_errno(EINVAL));
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	bcopy(sd->seesd_sun_sno, serid, sizeof (sd->seesd_sun_sno));
141*7c478bd9Sstevel@tonic-gate 	serid[sizeof (sd->seesd_sun_sno)] = '\0';
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	return (0);
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate int
mem_get_serid(const char * device,char * serid,size_t seridsz)147*7c478bd9Sstevel@tonic-gate mem_get_serid(const char *device, char *serid, size_t seridsz)
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	char buf[8192];
150*7c478bd9Sstevel@tonic-gate 	int fd;
151*7c478bd9Sstevel@tonic-gate 	ssize_t sz;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	if ((fd = open(device, O_RDONLY)) < 0)
154*7c478bd9Sstevel@tonic-gate 		return (-1); /* errno is set for us */
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	if ((sz = read(fd, buf, sizeof (buf))) < 0) {
157*7c478bd9Sstevel@tonic-gate 		int err = errno;
158*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
159*7c478bd9Sstevel@tonic-gate 		return (fmd_fmri_set_errno(err));
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	switch (sz) {
165*7c478bd9Sstevel@tonic-gate 	case BUFSIZ_SPD:
166*7c478bd9Sstevel@tonic-gate 		return (mem_get_spd_serid(buf, BUFSIZ_SPD, serid, seridsz));
167*7c478bd9Sstevel@tonic-gate 	case BUFSIZ_SEEPROM:
168*7c478bd9Sstevel@tonic-gate 		return (mem_get_seeprom_serid(buf, BUFSIZ_SEEPROM, serid,
169*7c478bd9Sstevel@tonic-gate 		    seridsz));
170*7c478bd9Sstevel@tonic-gate 	default:
171*7c478bd9Sstevel@tonic-gate 		return (fmd_fmri_set_errno(EINVAL));
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate }
174