xref: /illumos-gate/usr/src/lib/libfru/libfru/Ancestor.cc (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 (c) 2000-2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
28*7c478bd9Sstevel@tonic-gate #include <string.h>
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include "Ancestor.h"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /* ========================================================================= */
33*7c478bd9Sstevel@tonic-gate /* Ancestor object definitions. */
34*7c478bd9Sstevel@tonic-gate 
Ancestor(Str field,fru_tag_t t,const fru_regdef_t * d)35*7c478bd9Sstevel@tonic-gate Ancestor::Ancestor(Str field, fru_tag_t t, const fru_regdef_t *d)
36*7c478bd9Sstevel@tonic-gate 	: field_name(field),
37*7c478bd9Sstevel@tonic-gate     tag(t),
38*7c478bd9Sstevel@tonic-gate     def(d),
39*7c478bd9Sstevel@tonic-gate     numInstances(0),
40*7c478bd9Sstevel@tonic-gate     numBufs(1),
41*7c478bd9Sstevel@tonic-gate     next(NULL)
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 	offsets = (uint32_t *)malloc(sizeof (uint32_t)
44*7c478bd9Sstevel@tonic-gate 					* ANCESTOR_INST_BUF_SIZE);
45*7c478bd9Sstevel@tonic-gate 	paths = (char **)malloc(sizeof (char *)
46*7c478bd9Sstevel@tonic-gate 					* ANCESTOR_INST_BUF_SIZE);
47*7c478bd9Sstevel@tonic-gate }
48*7c478bd9Sstevel@tonic-gate 
~Ancestor()49*7c478bd9Sstevel@tonic-gate Ancestor::~Ancestor()
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	free(offsets);
52*7c478bd9Sstevel@tonic-gate 	if (paths != NULL) {
53*7c478bd9Sstevel@tonic-gate 		for (int i = 0; i < numInstances; i++) {
54*7c478bd9Sstevel@tonic-gate 			free(paths[i]);
55*7c478bd9Sstevel@tonic-gate 		}
56*7c478bd9Sstevel@tonic-gate 	}
57*7c478bd9Sstevel@tonic-gate 	free(paths);
58*7c478bd9Sstevel@tonic-gate 	delete next;
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate void
63*7c478bd9Sstevel@tonic-gate Ancestor::print(void)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Ancestor Information\n");
66*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Tag Name: %s\n", def->name);
67*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Tag Type: %s\n",
68*7c478bd9Sstevel@tonic-gate 			get_tagtype_str(get_tag_type(&tag)));
69*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Num instances: %d\n", numInstances);
70*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "   offsets:\n");
71*7c478bd9Sstevel@tonic-gate 	for (int i = 0; i < numInstances; i++) {
72*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "   %d\n", offsets[i]);
73*7c478bd9Sstevel@tonic-gate 	}
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	if (next != NULL) {
76*7c478bd9Sstevel@tonic-gate 		next->print();
77*7c478bd9Sstevel@tonic-gate 	}
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate */
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate void
addInstance(const char * path,uint32_t offset)82*7c478bd9Sstevel@tonic-gate Ancestor::addInstance(const char *path, uint32_t offset)
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	if (numInstances >= ANCESTOR_INST_BUF_SIZE) {
85*7c478bd9Sstevel@tonic-gate 		numBufs++;
86*7c478bd9Sstevel@tonic-gate 		offsets = (uint32_t *)realloc(offsets,
87*7c478bd9Sstevel@tonic-gate 			(sizeof (uint32_t) *
88*7c478bd9Sstevel@tonic-gate 				(ANCESTOR_INST_BUF_SIZE * numBufs)));
89*7c478bd9Sstevel@tonic-gate 		paths = (char **)realloc(offsets,
90*7c478bd9Sstevel@tonic-gate 			(sizeof (char *) *
91*7c478bd9Sstevel@tonic-gate 				(ANCESTOR_INST_BUF_SIZE * numBufs)));
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 	offsets[numInstances] = offset;
94*7c478bd9Sstevel@tonic-gate 	paths[numInstances++] = strdup(path);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate Str
getFieldName(void)98*7c478bd9Sstevel@tonic-gate Ancestor::getFieldName(void)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	return (field_name);
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate fru_tag_t
getTag(void)104*7c478bd9Sstevel@tonic-gate Ancestor::getTag(void)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	return (tag);
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate const fru_regdef_t *
getDef(void)110*7c478bd9Sstevel@tonic-gate Ancestor::getDef(void)
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	return (def);
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate int
getNumInstances(void)116*7c478bd9Sstevel@tonic-gate Ancestor::getNumInstances(void)
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	return (numInstances);
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate uint32_t
getInstOffset(int num)122*7c478bd9Sstevel@tonic-gate Ancestor::getInstOffset(int num)
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate 	if (num < numInstances)
125*7c478bd9Sstevel@tonic-gate 		return (offsets[num]);
126*7c478bd9Sstevel@tonic-gate 	else
127*7c478bd9Sstevel@tonic-gate 		return (offsets[numInstances]);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate const char *
getPath(int num)131*7c478bd9Sstevel@tonic-gate Ancestor::getPath(int num)
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	if (num < numInstances)
134*7c478bd9Sstevel@tonic-gate 		return (paths[num]);
135*7c478bd9Sstevel@tonic-gate 	else
136*7c478bd9Sstevel@tonic-gate 		return (paths[numInstances]);
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate Ancestor *
listTaggedAncestors(char * element)141*7c478bd9Sstevel@tonic-gate Ancestor::listTaggedAncestors(char *element)
142*7c478bd9Sstevel@tonic-gate {
143*7c478bd9Sstevel@tonic-gate 	Ancestor *rc = NULL;
144*7c478bd9Sstevel@tonic-gate 	fru_regdef_t *def = NULL;
145*7c478bd9Sstevel@tonic-gate 	int i = 0;
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	unsigned int number = 0;
148*7c478bd9Sstevel@tonic-gate 	char **data_elems = fru_reg_list_entries(&number);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	if (data_elems == NULL) {
151*7c478bd9Sstevel@tonic-gate 		return (NULL);
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	// look through all the elements.
155*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < number; i++) {
156*7c478bd9Sstevel@tonic-gate 		def = (fru_regdef_t *)
157*7c478bd9Sstevel@tonic-gate 			fru_reg_lookup_def_by_name(data_elems[i]);
158*7c478bd9Sstevel@tonic-gate 		Ancestor *ant = createTaggedAncestor(def, element);
159*7c478bd9Sstevel@tonic-gate 		if (ant != NULL) {
160*7c478bd9Sstevel@tonic-gate 			if (rc == NULL) {
161*7c478bd9Sstevel@tonic-gate 				rc = ant;
162*7c478bd9Sstevel@tonic-gate 			} else {
163*7c478bd9Sstevel@tonic-gate 				Ancestor *tmp = rc;
164*7c478bd9Sstevel@tonic-gate 				while (tmp->next != NULL) {
165*7c478bd9Sstevel@tonic-gate 					tmp = tmp->next;
166*7c478bd9Sstevel@tonic-gate 				}
167*7c478bd9Sstevel@tonic-gate 				tmp->next = ant;
168*7c478bd9Sstevel@tonic-gate 			}
169*7c478bd9Sstevel@tonic-gate 		}
170*7c478bd9Sstevel@tonic-gate 	}
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < number; i++) {
173*7c478bd9Sstevel@tonic-gate 		free(data_elems[i]);
174*7c478bd9Sstevel@tonic-gate 	}
175*7c478bd9Sstevel@tonic-gate 	free(data_elems);
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	return (rc);
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate Ancestor *
createTaggedAncestor(const fru_regdef_t * def,Str element)181*7c478bd9Sstevel@tonic-gate Ancestor::createTaggedAncestor(const fru_regdef_t *def, Str element)
182*7c478bd9Sstevel@tonic-gate {
183*7c478bd9Sstevel@tonic-gate 	// ancestors have to be tagged.
184*7c478bd9Sstevel@tonic-gate 	if (def->tagType == FRU_X)
185*7c478bd9Sstevel@tonic-gate 		return (NULL);
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	fru_tag_t tag;
188*7c478bd9Sstevel@tonic-gate 	mk_tag(def->tagType, def->tagDense, def->payloadLen, &tag);
189*7c478bd9Sstevel@tonic-gate 	Ancestor *rc = new Ancestor(element, tag, def);
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	if (element.compare(def->name) == 0) {
192*7c478bd9Sstevel@tonic-gate 		rc->addInstance("", 0);
193*7c478bd9Sstevel@tonic-gate 		return (rc);
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	int found = 0;
197*7c478bd9Sstevel@tonic-gate 	if (def->dataType == FDTYPE_Record) {
198*7c478bd9Sstevel@tonic-gate 		uint32_t offset = 0;
199*7c478bd9Sstevel@tonic-gate 		for (int i = 0; i < def->enumCount; i++) {
200*7c478bd9Sstevel@tonic-gate 			const fru_regdef_t *tmp
201*7c478bd9Sstevel@tonic-gate 				= fru_reg_lookup_def_by_name
202*7c478bd9Sstevel@tonic-gate 					((char *)def->enumTable[i].text);
203*7c478bd9Sstevel@tonic-gate 			Str path = "/";
204*7c478bd9Sstevel@tonic-gate 			path << def->name;
205*7c478bd9Sstevel@tonic-gate 			int f = definitionContains(tmp, def, element,
206*7c478bd9Sstevel@tonic-gate 							offset, rc, path);
207*7c478bd9Sstevel@tonic-gate 			if (f == 1) found = 1; // found needs to latch at one.
208*7c478bd9Sstevel@tonic-gate 				offset += tmp->payloadLen;
209*7c478bd9Sstevel@tonic-gate 		}
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	if (!found) {
213*7c478bd9Sstevel@tonic-gate 		delete rc;
214*7c478bd9Sstevel@tonic-gate 		return (NULL);
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	return (rc);
218*7c478bd9Sstevel@tonic-gate }
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate int
definitionContains(const fru_regdef_t * def,const fru_regdef_t * parent_def,Str element,uint32_t offset,Ancestor * ant,Str path)221*7c478bd9Sstevel@tonic-gate Ancestor::definitionContains(const fru_regdef_t *def,
222*7c478bd9Sstevel@tonic-gate 				const fru_regdef_t *parent_def,
223*7c478bd9Sstevel@tonic-gate 				Str element,
224*7c478bd9Sstevel@tonic-gate 				uint32_t offset,
225*7c478bd9Sstevel@tonic-gate 				Ancestor *ant,
226*7c478bd9Sstevel@tonic-gate 				Str path)
227*7c478bd9Sstevel@tonic-gate {
228*7c478bd9Sstevel@tonic-gate 	if (element.compare(def->name) == 0) {
229*7c478bd9Sstevel@tonic-gate 		if (parent_def->iterationType != FRU_NOT_ITERATED) {
230*7c478bd9Sstevel@tonic-gate 			offset += 4;
231*7c478bd9Sstevel@tonic-gate 			for (int i = 0; i < parent_def->iterationCount; i++) {
232*7c478bd9Sstevel@tonic-gate 				Str tmp = path;
233*7c478bd9Sstevel@tonic-gate 				tmp << "[" << i << "]/";
234*7c478bd9Sstevel@tonic-gate 				ant->addInstance(tmp.peak(), offset);
235*7c478bd9Sstevel@tonic-gate 				offset += (parent_def->payloadLen - 4) /
236*7c478bd9Sstevel@tonic-gate 						parent_def->iterationCount;
237*7c478bd9Sstevel@tonic-gate 			}
238*7c478bd9Sstevel@tonic-gate 		} else {
239*7c478bd9Sstevel@tonic-gate 			path << "/";
240*7c478bd9Sstevel@tonic-gate 			ant->addInstance(path.peak(), offset);
241*7c478bd9Sstevel@tonic-gate 		}
242*7c478bd9Sstevel@tonic-gate 		return (1);
243*7c478bd9Sstevel@tonic-gate 	}
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	int found = 0;
246*7c478bd9Sstevel@tonic-gate 	if (def->dataType == FDTYPE_Record) {
247*7c478bd9Sstevel@tonic-gate 		for (int i = 0; i < def->enumCount; i++) {
248*7c478bd9Sstevel@tonic-gate 			const fru_regdef_t *tmp
249*7c478bd9Sstevel@tonic-gate 				= fru_reg_lookup_def_by_name
250*7c478bd9Sstevel@tonic-gate 					((char *)def->enumTable[i].text);
251*7c478bd9Sstevel@tonic-gate 			Str newPath = path;
252*7c478bd9Sstevel@tonic-gate 			newPath << "/" << def->name;
253*7c478bd9Sstevel@tonic-gate 			int f = definitionContains(tmp, def, element,
254*7c478bd9Sstevel@tonic-gate 							offset, ant, newPath);
255*7c478bd9Sstevel@tonic-gate 			if (f == 1) found = 1; // found needs to latch at one.
256*7c478bd9Sstevel@tonic-gate 				offset += tmp->payloadLen;
257*7c478bd9Sstevel@tonic-gate 		}
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	return (found);
261*7c478bd9Sstevel@tonic-gate }
262