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) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <strings.h>
33 #include <fcntl.h>
34 
35 #include <fcode/private.h>
36 #include <fcode/log.h>
37 
38 #include <fcdriver/fcdriver.h>
39 
40 static fc_phandle_t
fc_nodeop(common_data_t * cdp,fc_phandle_t node,char * svc)41 fc_nodeop(common_data_t *cdp, fc_phandle_t node, char *svc)
42 {
43 	fc_cell_t hcell;
44 	int error;
45 
46 	error = fc_run_priv(cdp, svc, 1, 1, fc_phandle2cell(node), &hcell);
47 	if (error)
48 		return (0);
49 	return (fc_cell2phandle(hcell));
50 }
51 
52 void
recurse_tree(fcode_env_t * env,device_t * d,void (* fn)(fcode_env_t *,device_t *))53 recurse_tree(fcode_env_t *env, device_t *d,
54     void (*fn)(fcode_env_t *, device_t *))
55 {
56 	if (d != NULL) {
57 		device_t *p;
58 
59 		fn(env, d);
60 		recurse_tree(env, d->child, fn);
61 		recurse_tree(env, d->peer, fn);
62 	}
63 }
64 
65 static void
get_prom_nodeid(fcode_env_t * env,device_t * d)66 get_prom_nodeid(fcode_env_t *env, device_t *d)
67 {
68 	common_data_t *cdp = env->private;
69 	private_data_t *pd = d->private;
70 	char *name;
71 	int namelen;
72 	char *namebuf;
73 
74 	if ((pd != NULL) && (pd->node)) {
75 		if (os_get_prop_common(cdp, pd->node, "name",
76 		    0, &namebuf, &namelen))
77 			namebuf = "<unknown>";
78 		debug_msg(DEBUG_UPLOAD, "Populated: %s = %p\n", namebuf,
79 		    pd->node);
80 		return;
81 	}
82 
83 	name = get_package_name(env, d);
84 	debug_msg(DEBUG_UPLOAD, "Node %s: %p (%p)\n", name, d, pd);
85 	if (d->parent) {
86 		private_data_t *ppd = (private_data_t *)d->parent->private;
87 		fc_phandle_t thisnode;
88 
89 		if (os_get_prop_common(cdp, ppd->node, "name",
90 		    0, &namebuf, &namelen))
91 			namebuf = "<unknown>";
92 		debug_msg(DEBUG_UPLOAD, "Parent: %p (%p) %s = %p\n", d->parent,
93 		    ppd, namebuf, ppd->node);
94 		for (thisnode = fc_nodeop(cdp, ppd->node, FC_CHILD_FCODE);
95 		    thisnode != 0;
96 		    thisnode = fc_nodeop(cdp, thisnode, FC_PEER_FCODE)) {
97 			int status;
98 
99 			namebuf = "";
100 			namelen = 0;
101 			status = os_get_prop_common(cdp, thisnode, "name",
102 			    0, &namebuf, &namelen);
103 			debug_msg(DEBUG_UPLOAD, "Lookup: %p name '%s'\n"
104 			    " status: %d", thisnode, namebuf, status);
105 			if (status == 0 && strcmp(name, namebuf) == 0)
106 				break;
107 		}
108 		if (thisnode) {
109 			pd = MALLOC(sizeof (private_data_t));
110 			pd->common = cdp;
111 			pd->node = thisnode;
112 			pd->upload = 0;
113 			d->private = pd;
114 			add_my_handle(env, pd->node, d);
115 			install_property_vectors(env, d);
116 			debug_msg(DEBUG_UPLOAD, "Found: %p\n", thisnode);
117 		}
118 	}
119 }
120 
121 static void
update_nodeids(fcode_env_t * env)122 update_nodeids(fcode_env_t *env)
123 {
124 	/*
125 	 * We scan through the tree looking for nodes that don't have
126 	 * one of my structures attached, and for each of those nodes
127 	 * I attempt to match it with a real firmware node
128 	 */
129 	recurse_tree(env, env->root_node, get_prom_nodeid);
130 }
131 
132 static void
build_nodes(fcode_env_t * env,common_data_t * cdp,fc_phandle_t h)133 build_nodes(fcode_env_t *env, common_data_t *cdp, fc_phandle_t h)
134 {
135 	char *name;
136 	int len;
137 	int n, allocd, depth;
138 	fc_phandle_t p;
139 	device_t *current, *attach;
140 	private_data_t *pd;
141 	private_data_t **node_array;
142 
143 	/*
144 	 * This is not nice; new_device calls the allocate_phandle
145 	 * routine without exception, we need to disable the allocation
146 	 * while we are building the tree to the attachment point
147 	 * which is why the init_done variable exists.
148 	 */
149 	cdp->init_done = 0;
150 	node_array = NULL;
151 	depth = 0;
152 	allocd = sizeof (private_data_t *);
153 	do {
154 		node_array = REALLOC(node_array, allocd*(depth+1));
155 		pd = MALLOC(sizeof (private_data_t));
156 		pd->node = h;
157 		node_array[depth] = pd;
158 		name = NULL;
159 		(void) os_get_prop_common(cdp, pd->node, "name", 0, &name,
160 		    &len);
161 		if (name)
162 			debug_msg(DEBUG_UPLOAD, "Node: %p name: '%s'\n", h,
163 			    name);
164 		else
165 			log_message(MSG_ERROR, "Node: %p Unnamed node!!\n", h);
166 		depth++;
167 		h = fc_nodeop(cdp, h, FC_PARENT);
168 	} while (h);
169 
170 	for (n = 0; n < (depth-1); n++) {
171 		new_device(env);
172 	}
173 
174 	env->attachment_pt = current = attach = env->current_device;
175 
176 	for (n = 0; n < depth; n++) {
177 		pd = node_array[n];
178 		pd->common = cdp;
179 		current->private = pd;
180 		add_my_handle(env, pd->node, current);
181 		install_property_vectors(env, current);
182 		current = current->parent;
183 	}
184 
185 	for (current = attach; current != NULL; current = current->parent) {
186 		install_node_data(env, current);
187 		if (current->parent)
188 			finish_device(env);
189 	}
190 
191 	FREE(node_array);
192 	cdp->init_done = 2;
193 	update_nodeids(env);
194 	cdp->init_done = 1;
195 	cdp->first_node = 1;
196 }
197 
198 void
build_tree(fcode_env_t * env)199 build_tree(fcode_env_t *env)
200 {
201 	common_data_t *cdp = env->private;
202 	instance_t *ih;
203 
204 	root_node(env);
205 	ih = open_instance_chain(env, env->current_device, 0);
206 	MYSELF = ih;
207 	build_nodes(env, cdp, cdp->attach);
208 	close_instance_chain(env, ih, 0);
209 	MYSELF = 0;
210 	device_end(env);
211 }
212 
213 /*
214  * Installs /openprom and /packages nodes and sub-nodes.
215  */
216 void
install_builtin_nodes(fcode_env_t * env)217 install_builtin_nodes(fcode_env_t *env)
218 {
219 	common_data_t *cdp = env->private;
220 	int saved_first_node;
221 	int saved_init_done;
222 
223 	if (cdp) {
224 		saved_first_node = cdp->first_node;
225 		saved_init_done = cdp->init_done;
226 		cdp->first_node = 0;
227 		cdp->init_done = 2;
228 		install_openprom_nodes(env);
229 		install_package_nodes(env);
230 		cdp->first_node = saved_first_node;
231 		cdp->init_done = saved_init_done;
232 	}
233 }
234 
235 
236 #pragma init(_init)
237 
238 static void
_init(void)239 _init(void)
240 {
241 	fcode_env_t *env = initial_env;
242 
243 	ASSERT(env);
244 	NOTICE;
245 
246 	FORTH(0,		"update-nodes",		update_nodeids);
247 	FORTH(0,		"build-tree",		build_tree);
248 }
249