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 2003 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 #ifndef	_FRU_ACCESS_IMPL_H
28*7c478bd9Sstevel@tonic-gate #define	_FRU_ACCESS_IMPL_H
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus
33*7c478bd9Sstevel@tonic-gate extern "C" {
34*7c478bd9Sstevel@tonic-gate #endif
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <unistd.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
41*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
42*7c478bd9Sstevel@tonic-gate #include <dial.h>
43*7c478bd9Sstevel@tonic-gate #include <strings.h>
44*7c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
46*7c478bd9Sstevel@tonic-gate #include <picl.h>
47*7c478bd9Sstevel@tonic-gate #include <picltree.h>
48*7c478bd9Sstevel@tonic-gate #include <picldefs.h>
49*7c478bd9Sstevel@tonic-gate #include <syslog.h>
50*7c478bd9Sstevel@tonic-gate #include <errno.h>
51*7c478bd9Sstevel@tonic-gate #include <libfru.h>
52*7c478bd9Sstevel@tonic-gate #include <limits.h>
53*7c478bd9Sstevel@tonic-gate #include <fru_tag.h>
54*7c478bd9Sstevel@tonic-gate #include "fru_access.h"
55*7c478bd9Sstevel@tonic-gate #include "spd_data.h"
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate /* object types */
58*7c478bd9Sstevel@tonic-gate typedef	enum {CONTAINER_TYPE, SECTION_TYPE, SEGMENT_TYPE, PACKET_TYPE} object_t;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate #define	TABLE_SIZE		64	/* hash table size */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /* container configuration file name */
63*7c478bd9Sstevel@tonic-gate #define	FRU_CONTAINER_CONF	"fru_container.conf"
64*7c478bd9Sstevel@tonic-gate #define	CONTAINER_DIR		"/usr/platform/sun4u/lib"
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /* section header */
67*7c478bd9Sstevel@tonic-gate #define	SECTION_HDR_TAG		0x08
68*7c478bd9Sstevel@tonic-gate #define	SECTION_HDR_VER		0x0001
69*7c478bd9Sstevel@tonic-gate #define	SECTION_HDR_LENGTH	0x06
70*7c478bd9Sstevel@tonic-gate #define	SECTION_HDR_CRC8	0x00
71*7c478bd9Sstevel@tonic-gate #define	SECTION_HDR_VER_BIT0	0x00
72*7c478bd9Sstevel@tonic-gate #define	SECTION_HDR_VER_BIT1	0x01
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate #define	READ_ONLY_SECTION	1	/* section is read-only */
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate #define	GET_SEGMENT_DESCRIPTOR	\
77*7c478bd9Sstevel@tonic-gate 		(seg_layout->descriptor[1]|seg_layout->descriptor[0] << 16)
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate #define	GET_SECTION_HDR_VERSION	\
80*7c478bd9Sstevel@tonic-gate 		(sec_hdr.headerversion[1]|sec_hdr.headerversion[0] << 8)
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate /* Segment Trailer Tag */
83*7c478bd9Sstevel@tonic-gate #define	SEG_TRAILER_TAG 0x0C
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate /* defines fixed segment */
86*7c478bd9Sstevel@tonic-gate #define	SEGMENT_FIXED		1
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate typedef union {
89*7c478bd9Sstevel@tonic-gate 	uint32_t all_bits;
90*7c478bd9Sstevel@tonic-gate 	struct {
91*7c478bd9Sstevel@tonic-gate 		unsigned read_only : 1;
92*7c478bd9Sstevel@tonic-gate 		unsigned unused : 8;
93*7c478bd9Sstevel@tonic-gate 		unsigned : 8;
94*7c478bd9Sstevel@tonic-gate 		unsigned : 8;
95*7c478bd9Sstevel@tonic-gate 		unsigned : 7;
96*7c478bd9Sstevel@tonic-gate 	} field;
97*7c478bd9Sstevel@tonic-gate } sectdescbit_t;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate typedef enum {
100*7c478bd9Sstevel@tonic-gate 	ENC_STANDARD = 0,	/* proper fruid data */
101*7c478bd9Sstevel@tonic-gate 	ENC_SPD			/* serial presence detect data */
102*7c478bd9Sstevel@tonic-gate } sectencoding_t;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate typedef struct {
105*7c478bd9Sstevel@tonic-gate 	sectdescbit_t	description;
106*7c478bd9Sstevel@tonic-gate 	uint32_t	address; /* for SEEPROMS this is the offset */
107*7c478bd9Sstevel@tonic-gate 	uint32_t	size;
108*7c478bd9Sstevel@tonic-gate 	sectencoding_t	encoding;
109*7c478bd9Sstevel@tonic-gate } sectioninfo_t;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate typedef uint16_t headerrev_t;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate #define	MAX_NUMOF_SECTION	2
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate typedef struct {
116*7c478bd9Sstevel@tonic-gate 	headerrev_t header_ver;
117*7c478bd9Sstevel@tonic-gate 	int num_sections;
118*7c478bd9Sstevel@tonic-gate 	sectioninfo_t section_info[MAX_NUMOF_SECTION];
119*7c478bd9Sstevel@tonic-gate } container_info_t;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate /* section header layout */
123*7c478bd9Sstevel@tonic-gate typedef struct {
124*7c478bd9Sstevel@tonic-gate 	uint8_t	headertag; /* section header tag */
125*7c478bd9Sstevel@tonic-gate 	uint8_t	headerversion[2]; /* header version (msb) */
126*7c478bd9Sstevel@tonic-gate 	uint8_t	headerlength; /* header length */
127*7c478bd9Sstevel@tonic-gate 	uint8_t	headercrc8; /* crc8 */
128*7c478bd9Sstevel@tonic-gate 	uint8_t	segmentcount; /* total number of segment */
129*7c478bd9Sstevel@tonic-gate } section_layout_t;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate /* segment header layout */
132*7c478bd9Sstevel@tonic-gate typedef struct  {
133*7c478bd9Sstevel@tonic-gate 	uint16_t	name; 	/* segment name */
134*7c478bd9Sstevel@tonic-gate 	uint16_t	descriptor[2]; /* descriptor (msb) */
135*7c478bd9Sstevel@tonic-gate 	uint16_t	offset; /* segment data offset */
136*7c478bd9Sstevel@tonic-gate 	uint16_t	length; /* segment length */
137*7c478bd9Sstevel@tonic-gate } segment_layout_t;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate /* segment information used in finding new offset for a new segment */
140*7c478bd9Sstevel@tonic-gate typedef struct {
141*7c478bd9Sstevel@tonic-gate 	int segnum;	/* segment number */
142*7c478bd9Sstevel@tonic-gate 	int offset;	/* segment offset */
143*7c478bd9Sstevel@tonic-gate 	int length;	/* segment length */
144*7c478bd9Sstevel@tonic-gate 	int fixed;	/* fixed or non-fixed segment */
145*7c478bd9Sstevel@tonic-gate } seg_info_t;
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate typedef	uint64_t	handle_t;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate struct	hash_obj;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate /* packet hash object */
152*7c478bd9Sstevel@tonic-gate typedef	struct {
153*7c478bd9Sstevel@tonic-gate 	handle_t	segment_hdl;	/* segment handle */
154*7c478bd9Sstevel@tonic-gate 	fru_tag_t	tag;
155*7c478bd9Sstevel@tonic-gate 	int		tag_size;
156*7c478bd9Sstevel@tonic-gate 	uint8_t		*payload;
157*7c478bd9Sstevel@tonic-gate 	uint32_t	paylen;
158*7c478bd9Sstevel@tonic-gate 	uint32_t	payload_offset;
159*7c478bd9Sstevel@tonic-gate 	struct hash_obj *next;
160*7c478bd9Sstevel@tonic-gate } packet_obj_t;
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate /* segment hash object */
163*7c478bd9Sstevel@tonic-gate typedef struct {
164*7c478bd9Sstevel@tonic-gate 	handle_t	section_hdl;	/* section handle */
165*7c478bd9Sstevel@tonic-gate 	int		num_of_packets;	/* in a segment */
166*7c478bd9Sstevel@tonic-gate 	int		trailer_offset;
167*7c478bd9Sstevel@tonic-gate 	segment_t	segment;
168*7c478bd9Sstevel@tonic-gate 	struct hash_obj	*pkt_obj_list;	/* packet object list */
169*7c478bd9Sstevel@tonic-gate 	struct hash_obj	*next;
170*7c478bd9Sstevel@tonic-gate } segment_obj_t;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate /* section hash object */
173*7c478bd9Sstevel@tonic-gate typedef	struct {
174*7c478bd9Sstevel@tonic-gate 	handle_t	cont_hdl;	/* container handle */
175*7c478bd9Sstevel@tonic-gate 	section_t	section;
176*7c478bd9Sstevel@tonic-gate 	sectencoding_t	encoding;	/* standard or needing interpretation */
177*7c478bd9Sstevel@tonic-gate 	int	num_of_segment;		/* in a section */
178*7c478bd9Sstevel@tonic-gate 	struct hash_obj	*seg_obj_list;	/* points to segment objects list */
179*7c478bd9Sstevel@tonic-gate 	struct hash_obj	*next;
180*7c478bd9Sstevel@tonic-gate } section_obj_t;
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate /* container hash object */
183*7c478bd9Sstevel@tonic-gate typedef	struct {
184*7c478bd9Sstevel@tonic-gate 	char	device_pathname[PATH_MAX]; /* device name */
185*7c478bd9Sstevel@tonic-gate 	int	num_of_section;	/* num of section in container */
186*7c478bd9Sstevel@tonic-gate 	struct hash_obj	*sec_obj_list; /* points to section objects list */
187*7c478bd9Sstevel@tonic-gate } container_obj_t;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate /* hash object */
190*7c478bd9Sstevel@tonic-gate typedef	struct hash_obj {
191*7c478bd9Sstevel@tonic-gate 	int	object_type;
192*7c478bd9Sstevel@tonic-gate 	handle_t obj_hdl;
193*7c478bd9Sstevel@tonic-gate 	union {
194*7c478bd9Sstevel@tonic-gate 		container_obj_t		*cont_obj;
195*7c478bd9Sstevel@tonic-gate 		section_obj_t		*sec_obj;
196*7c478bd9Sstevel@tonic-gate 		segment_obj_t		*seg_obj;
197*7c478bd9Sstevel@tonic-gate 		packet_obj_t		*pkt_obj;
198*7c478bd9Sstevel@tonic-gate 	} u;
199*7c478bd9Sstevel@tonic-gate 	struct hash_obj 	*next;
200*7c478bd9Sstevel@tonic-gate 	struct hash_obj 	*prev;
201*7c478bd9Sstevel@tonic-gate } hash_obj_t;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate unsigned char compute_crc8(unsigned char *bytes, int length);
204*7c478bd9Sstevel@tonic-gate long compute_crc32(unsigned char *bytes, int length);
205*7c478bd9Sstevel@tonic-gate long compute_checksum32(unsigned char *bytes, int length);
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate #endif
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate #endif /* _FRU_ACCESS_IMPL_H */
212