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 (c) 2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <strings.h>
31 
32 #include <fcode/private.h>
33 #include <fcode/log.h>
34 
35 #include <fcdriver/fcdriver.h>
36 
37 
38 static char *prop_buf;
39 
40 static int
getproplen(common_data_t * cdp,fc_phandle_t node,char * propname,int inherit)41 getproplen(common_data_t *cdp, fc_phandle_t node, char *propname, int inherit)
42 {
43 	fc_cell_t len;
44 	char *service;
45 	int error;
46 
47 	service = inherit ? FC_GET_IN_PROPLEN : FC_GET_PKG_PROPLEN;
48 
49 	error = fc_run_priv(cdp, service, 2, 1, fc_phandle2cell(node),
50 	    fc_ptr2cell(propname), &len);
51 	if (error)
52 		return (-1);
53 
54 	return (fc_cell2int(len));
55 }
56 
57 static int
getprop(common_data_t * cdp,fc_phandle_t node,char * propname,char * buf,int inherit)58 getprop(common_data_t *cdp, fc_phandle_t node, char *propname, char *buf,
59     int inherit)
60 {
61 	fc_cell_t len;
62 	char *service;
63 	int error;
64 
65 	service = inherit ? FC_GET_IN_PROP : FC_GET_PKG_PROP;
66 
67 	error = fc_run_priv(cdp, service, 3, 1, fc_phandle2cell(node),
68 	    fc_ptr2cell(buf), fc_ptr2cell(propname), &len);
69 	if (error)
70 		return (-1);
71 
72 	return (fc_cell2int(len));
73 }
74 
75 int
os_get_prop_common(common_data_t * cdp,fc_phandle_t node,char * name,int inherit,char ** buf,int * len)76 os_get_prop_common(common_data_t *cdp, fc_phandle_t node, char *name,
77     int inherit, char **buf, int *len)
78 {
79 	int i, j;
80 	char *bp;
81 
82 	i = getproplen(cdp, node, name, inherit);
83 	if (i <= 0) {
84 		/*
85 		 * OK for properties to be undefined, suppress error message
86 		 * unless some debug is on.
87 		 */
88 		if (get_interpreter_debug_level())
89 			log_message(MSG_ERROR, "os_get_prop_common:"
90 			    " getproplen(%s) returned %d\n", name, i);
91 		return (-1);
92 	}
93 	bp = MALLOC(i);
94 
95 	j = getprop(cdp, node, name, bp, inherit);
96 	if (i != j) {
97 		/*
98 		 * It's an error if getproplen succeeded but getprop didn't
99 		 * return a matching length.
100 		 */
101 		log_message(MSG_ERROR, "os_get_prop_common: getprop(%s)"
102 		    " return %d, getproplen returned %d\n", name, j, i);
103 		FREE(bp);
104 		return (-2);
105 	}
106 	memcpy(prop_buf, bp, i);
107 	*buf = prop_buf;
108 	*len = i;
109 	FREE(bp);
110 	return (0);
111 }
112 
113 static void
os_get_prop(fcode_env_t * env,int inherit,device_t * dev)114 os_get_prop(fcode_env_t *env, int inherit, device_t *dev)
115 {
116 	fc_phandle_t node;
117 	char *name;
118 	char *prop;
119 	int len;
120 	private_data_t *pd;
121 
122 	ASSERT(dev);
123 
124 	pd = dev->private;
125 	ASSERT(pd);
126 
127 	name = pop_a_string(env, &len);
128 
129 	node = pd->node;
130 	if (node == 0) {
131 		log_message(MSG_ERROR, "os_get_prop: NULL node: %s\n",
132 		    get_path(env, dev));
133 		PUSH(DS, TRUE);
134 	} else if (os_get_prop_common(pd->common, node, name, inherit, &prop,
135 	    &len)) {
136 		PUSH(DS, TRUE);
137 	} else {
138 		PUSH(DS, (fstack_t)prop);
139 		PUSH(DS, len);
140 		PUSH(DS, FALSE);
141 	}
142 }
143 
144 static void
os_get_package_prop(fcode_env_t * env)145 os_get_package_prop(fcode_env_t *env)
146 {
147 	device_t *dev;
148 
149 	CONVERT_PHANDLE(env, dev, POP(DS));
150 	os_get_prop(env, 0, dev);
151 }
152 
153 static void
os_get_inherited_prop(fcode_env_t * env)154 os_get_inherited_prop(fcode_env_t *env)
155 {
156 	os_get_prop(env, 1, env->attachment_pt);
157 }
158 
159 void
install_property_vectors(fcode_env_t * env,device_t * d)160 install_property_vectors(fcode_env_t *env, device_t *d)
161 {
162 	d->vectors.get_package_prop = os_get_package_prop;
163 	d->vectors.get_inherited_prop = os_get_inherited_prop;
164 }
165 
166 #pragma init(_init)
167 
168 static void
_init(void)169 _init(void)
170 {
171 	fcode_env_t *env = initial_env;
172 
173 	ASSERT(env);
174 	NOTICE;
175 
176 	prop_buf = MALLOC(4096);
177 
178 }
179