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