xref: /onnv-gate/usr/src/lib/efcode/fcdriver/property.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 2000 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <unistd.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <strings.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <fcode/private.h>
35*0Sstevel@tonic-gate #include <fcode/log.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate static char *prop_buf;
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate static int
getproplen(common_data_t * cdp,fc_phandle_t node,char * propname,int inherit)43*0Sstevel@tonic-gate getproplen(common_data_t *cdp, fc_phandle_t node, char *propname, int inherit)
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate 	fc_cell_t len;
46*0Sstevel@tonic-gate 	char *service;
47*0Sstevel@tonic-gate 	int error;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	service = inherit ? FC_GET_IN_PROPLEN : FC_GET_PKG_PROPLEN;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 	error = fc_run_priv(cdp, service, 2, 1, fc_phandle2cell(node),
52*0Sstevel@tonic-gate 	    fc_ptr2cell(propname), &len);
53*0Sstevel@tonic-gate 	if (error)
54*0Sstevel@tonic-gate 		return (-1);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	return (fc_cell2int(len));
57*0Sstevel@tonic-gate }
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static int
getprop(common_data_t * cdp,fc_phandle_t node,char * propname,char * buf,int inherit)60*0Sstevel@tonic-gate getprop(common_data_t *cdp, fc_phandle_t node, char *propname, char *buf,
61*0Sstevel@tonic-gate     int inherit)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	fc_cell_t len;
64*0Sstevel@tonic-gate 	char *service;
65*0Sstevel@tonic-gate 	int error;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	service = inherit ? FC_GET_IN_PROP : FC_GET_PKG_PROP;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	error = fc_run_priv(cdp, service, 3, 1, fc_phandle2cell(node),
70*0Sstevel@tonic-gate 	    fc_ptr2cell(buf), fc_ptr2cell(propname), &len);
71*0Sstevel@tonic-gate 	if (error)
72*0Sstevel@tonic-gate 		return (-1);
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	return (fc_cell2int(len));
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate int
os_get_prop_common(common_data_t * cdp,fc_phandle_t node,char * name,int inherit,char ** buf,int * len)78*0Sstevel@tonic-gate os_get_prop_common(common_data_t *cdp, fc_phandle_t node, char *name,
79*0Sstevel@tonic-gate     int inherit, char **buf, int *len)
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate 	int i, j;
82*0Sstevel@tonic-gate 	char *bp;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	i = getproplen(cdp, node, name, inherit);
85*0Sstevel@tonic-gate 	if (i <= 0) {
86*0Sstevel@tonic-gate 		/*
87*0Sstevel@tonic-gate 		 * OK for properties to be undefined, suppress error message
88*0Sstevel@tonic-gate 		 * unless some debug is on.
89*0Sstevel@tonic-gate 		 */
90*0Sstevel@tonic-gate 		if (get_interpreter_debug_level())
91*0Sstevel@tonic-gate 			log_message(MSG_ERROR, "os_get_prop_common:"
92*0Sstevel@tonic-gate 			    " getproplen(%s) returned %d\n", name, i);
93*0Sstevel@tonic-gate 		return (-1);
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 	bp = MALLOC(i);
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	j = getprop(cdp, node, name, bp, inherit);
98*0Sstevel@tonic-gate 	if (i != j) {
99*0Sstevel@tonic-gate 		/*
100*0Sstevel@tonic-gate 		 * It's an error if getproplen succeeded but getprop didn't
101*0Sstevel@tonic-gate 		 * return a matching length.
102*0Sstevel@tonic-gate 		 */
103*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "os_get_prop_common: getprop(%s)"
104*0Sstevel@tonic-gate 		    " return %d, getproplen returned %d\n", name, j, i);
105*0Sstevel@tonic-gate 		FREE(bp);
106*0Sstevel@tonic-gate 		return (-2);
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 	memcpy(prop_buf, bp, i);
109*0Sstevel@tonic-gate 	*buf = prop_buf;
110*0Sstevel@tonic-gate 	*len = i;
111*0Sstevel@tonic-gate 	FREE(bp);
112*0Sstevel@tonic-gate 	return (0);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate static void
os_get_prop(fcode_env_t * env,int inherit,device_t * dev)116*0Sstevel@tonic-gate os_get_prop(fcode_env_t *env, int inherit, device_t *dev)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	fc_phandle_t node;
119*0Sstevel@tonic-gate 	char *name;
120*0Sstevel@tonic-gate 	char *prop;
121*0Sstevel@tonic-gate 	int len;
122*0Sstevel@tonic-gate 	private_data_t *pd;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	ASSERT(dev);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	pd = dev->private;
127*0Sstevel@tonic-gate 	ASSERT(pd);
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	name = pop_a_string(env, &len);
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	node = pd->node;
132*0Sstevel@tonic-gate 	if (node == NULL) {
133*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "os_get_prop: NULL node: %s\n",
134*0Sstevel@tonic-gate 		    get_path(env, dev));
135*0Sstevel@tonic-gate 		PUSH(DS, TRUE);
136*0Sstevel@tonic-gate 	} else if (os_get_prop_common(pd->common, node, name, inherit, &prop,
137*0Sstevel@tonic-gate 	    &len)) {
138*0Sstevel@tonic-gate 		PUSH(DS, TRUE);
139*0Sstevel@tonic-gate 	} else {
140*0Sstevel@tonic-gate 		PUSH(DS, (fstack_t)prop);
141*0Sstevel@tonic-gate 		PUSH(DS, len);
142*0Sstevel@tonic-gate 		PUSH(DS, FALSE);
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate static void
os_get_package_prop(fcode_env_t * env)147*0Sstevel@tonic-gate os_get_package_prop(fcode_env_t *env)
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate 	device_t *dev;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	CONVERT_PHANDLE(env, dev, POP(DS));
152*0Sstevel@tonic-gate 	os_get_prop(env, 0, dev);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate static void
os_get_inherited_prop(fcode_env_t * env)156*0Sstevel@tonic-gate os_get_inherited_prop(fcode_env_t *env)
157*0Sstevel@tonic-gate {
158*0Sstevel@tonic-gate 	os_get_prop(env, 1, env->attachment_pt);
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate void
install_property_vectors(fcode_env_t * env,device_t * d)162*0Sstevel@tonic-gate install_property_vectors(fcode_env_t *env, device_t *d)
163*0Sstevel@tonic-gate {
164*0Sstevel@tonic-gate 	d->vectors.get_package_prop = os_get_package_prop;
165*0Sstevel@tonic-gate 	d->vectors.get_inherited_prop = os_get_inherited_prop;
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate #pragma init(_init)
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate static void
_init(void)171*0Sstevel@tonic-gate _init(void)
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	ASSERT(env);
176*0Sstevel@tonic-gate 	NOTICE;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	prop_buf = MALLOC(4096);
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate }
181