xref: /titanic_44/usr/src/lib/libfru/libfruraw/fru_access_impl.h (revision 1e4c938b57d1656808e4112127ff1dce3eba5314)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_FRU_ACCESS_IMPL_H
28 #define	_FRU_ACCESS_IMPL_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <dial.h>
41 #include <strings.h>
42 #include <libdevinfo.h>
43 #include <sys/systeminfo.h>
44 #include <sys/byteorder.h>
45 #include <syslog.h>
46 #include <errno.h>
47 #include <libfru.h>
48 #include <limits.h>
49 #include <fru_tag.h>
50 #include <fru_access.h>
51 
52 
53 /* object types */
54 typedef	enum {CONTAINER_TYPE, SECTION_TYPE, SEGMENT_TYPE, PACKET_TYPE} object_t;
55 
56 #define	TABLE_SIZE		64	/* hash table size */
57 
58 
59 /* section header */
60 #define	SECTION_HDR_TAG		0x08
61 #define	SECTION_HDR_VER		0x0001
62 #define	SECTION_HDR_LENGTH	0x06
63 #define	SECTION_HDR_CRC8	0x00
64 #define	SECTION_HDR_VER_BIT0	0x00
65 #define	SECTION_HDR_VER_BIT1	0x01
66 
67 #define	READ_ONLY_SECTION	1	/* section is read-only */
68 
69 #define	GET_SEGMENT_DESCRIPTOR	\
70 		(BE_16(seg_layout->descriptor[1])| \
71 		BE_16(seg_layout->descriptor[0] << 16))
72 
73 #define	GET_SECTION_HDR_VERSION	\
74 		(sec_hdr.headerversion[1]|sec_hdr.headerversion[0] << 8)
75 
76 /* Segment Trailer Tag */
77 #define	SEG_TRAILER_TAG 0x0C
78 
79 /* defines fixed segment */
80 #define	SEGMENT_FIXED		1
81 
82 typedef union {
83 	uint32_t all_bits;
84 	struct {
85 		unsigned read_only : 1;
86 		unsigned unused : 8;
87 		unsigned : 8;
88 		unsigned : 8;
89 		unsigned : 7;
90 	} field;
91 } sectdescbit_t;
92 
93 typedef enum {
94 	ENC_STANDARD = 0,	/* proper fruid data */
95 	ENC_SPD			/* serial presence detect data */
96 } sectencoding_t;
97 
98 typedef struct {
99 	sectdescbit_t	description;
100 	uint32_t	address; /* for SEEPROMS this is the offset */
101 	uint32_t	size;
102 	sectencoding_t	encoding;
103 } sectioninfo_t;
104 
105 typedef uint16_t headerrev_t;
106 
107 #define	MAX_NUMOF_SECTION	2
108 
109 typedef struct {
110 	headerrev_t header_ver;
111 	int num_sections;
112 	sectioninfo_t section_info[MAX_NUMOF_SECTION];
113 } container_info_t;
114 
115 
116 /* section header layout */
117 typedef struct {
118 	uint8_t	headertag; /* section header tag */
119 	uint8_t	headerversion[2]; /* header version (msb) */
120 	uint8_t	headerlength; /* header length */
121 	uint8_t	headercrc8; /* crc8 */
122 	uint8_t	segmentcount; /* total number of segment */
123 } section_layout_t;
124 
125 /* segment header layout */
126 typedef struct  {
127 	uint16_t	name; 	/* segment name */
128 	uint16_t	descriptor[2]; /* descriptor (msb) */
129 	uint16_t	offset; /* segment data offset */
130 	uint16_t	length; /* segment length */
131 } segment_layout_t;
132 
133 /* segment information used in finding new offset for a new segment */
134 typedef struct {
135 	int segnum;	/* segment number */
136 	int offset;	/* segment offset */
137 	int length;	/* segment length */
138 	int fixed;	/* fixed or non-fixed segment */
139 } seg_info_t;
140 
141 typedef	uint64_t	handle_t;
142 
143 struct	hash_obj;
144 
145 /* packet hash object */
146 typedef	struct {
147 	handle_t	segment_hdl;	/* segment handle */
148 	fru_tag_t	tag;
149 	int		tag_size;
150 	uint8_t		*payload;
151 	uint32_t	paylen;
152 	uint32_t	payload_offset;
153 	struct hash_obj *next;
154 } packet_obj_t;
155 
156 /* segment hash object */
157 typedef struct {
158 	handle_t	section_hdl;	/* section handle */
159 	int		num_of_packets;	/* in a segment */
160 	int		trailer_offset;
161 	segment_t	segment;
162 	struct hash_obj	*pkt_obj_list;	/* packet object list */
163 	struct hash_obj	*next;
164 } segment_obj_t;
165 
166 /* section hash object */
167 typedef	struct {
168 	handle_t	cont_hdl;	/* container handle */
169 	section_t	section;
170 	sectencoding_t	encoding;	/* standard or needing interpretation */
171 	int	num_of_segment;		/* in a section */
172 	struct hash_obj	*seg_obj_list;	/* points to segment objects list */
173 	struct hash_obj	*next;
174 } section_obj_t;
175 
176 /* container hash object */
177 typedef	struct {
178 	char	device_pathname[PATH_MAX]; /* device name */
179 	int	num_of_section;	/* num of section in container */
180 	struct hash_obj	*sec_obj_list; /* points to section objects list */
181 } container_obj_t;
182 
183 /* hash object */
184 typedef	struct hash_obj {
185 	int	object_type;
186 	handle_t obj_hdl;
187 	union {
188 		container_obj_t		*cont_obj;
189 		section_obj_t		*sec_obj;
190 		segment_obj_t		*seg_obj;
191 		packet_obj_t		*pkt_obj;
192 	} u;
193 	struct hash_obj 	*next;
194 	struct hash_obj 	*prev;
195 } hash_obj_t;
196 
197 unsigned char compute_crc8(unsigned char *bytes, int length);
198 long compute_crc32(unsigned char *bytes, int length);
199 long compute_checksum32(unsigned char *bytes, int length);
200 
201 #ifdef	__cplusplus
202 }
203 #endif
204 
205 #endif /* _FRU_ACCESS_IMPL_H */
206