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