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 <stdlib.h>
31*0Sstevel@tonic-gate #include <unistd.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 static device_t *builtin_driver_device;
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate static int
is_device_builtin_package(fcode_env_t * env,device_t * d)42*0Sstevel@tonic-gate is_device_builtin_package(fcode_env_t *env, device_t *d)
43*0Sstevel@tonic-gate {
44*0Sstevel@tonic-gate return (d == builtin_driver_device);
45*0Sstevel@tonic-gate }
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate static char *dropin_name;
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate * do-builtin-dropin ( -- )
51*0Sstevel@tonic-gate * Convoluted name just in case someone has "do-dropin" word in Fcode.
52*0Sstevel@tonic-gate * Somewhat different from do-dropin in OBP, as we just load the Fcode, we
53*0Sstevel@tonic-gate * don't do a byte-load.
54*0Sstevel@tonic-gate */
55*0Sstevel@tonic-gate static void
do_builtin_dropin(fcode_env_t * env)56*0Sstevel@tonic-gate do_builtin_dropin(fcode_env_t *env)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate fc_cell_t len, result;
59*0Sstevel@tonic-gate char *buf;
60*0Sstevel@tonic-gate int error;
61*0Sstevel@tonic-gate static char func_name[] = "do-builtin-dropin";
62*0Sstevel@tonic-gate extern int check_fcode_header(char *, uchar_t *, int);
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate if (dropin_name == NULL) {
65*0Sstevel@tonic-gate log_message(MSG_ERROR, "%s: dropin_name not set\n", func_name);
66*0Sstevel@tonic-gate return;
67*0Sstevel@tonic-gate }
68*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s'\n", func_name, dropin_name);
69*0Sstevel@tonic-gate error = fc_run_priv(env->private, "sunos,get-fcode-size", 1, 1,
70*0Sstevel@tonic-gate fc_ptr2cell(dropin_name), &len);
71*0Sstevel@tonic-gate if (error)
72*0Sstevel@tonic-gate return;
73*0Sstevel@tonic-gate if (len == 0) {
74*0Sstevel@tonic-gate log_message(MSG_WARN, "%s: '%s' zero length Fcode\n",
75*0Sstevel@tonic-gate func_name, dropin_name);
76*0Sstevel@tonic-gate return;
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate buf = MALLOC(len);
79*0Sstevel@tonic-gate error = fc_run_priv(env->private, "sunos,get-fcode", 3, 1,
80*0Sstevel@tonic-gate fc_ptr2cell(dropin_name), fc_ptr2cell(buf), len, &result);
81*0Sstevel@tonic-gate if (error) {
82*0Sstevel@tonic-gate FREE(buf);
83*0Sstevel@tonic-gate return;
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate if (check_fcode_header(dropin_name, (uchar_t *)buf, len) == 0)
87*0Sstevel@tonic-gate log_message(MSG_WARN, "%s: '%s' fcode header NOT OK\n",
88*0Sstevel@tonic-gate func_name, dropin_name);
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE,
91*0Sstevel@tonic-gate "%s: '%s' doing byte-load len: %x\n", func_name, dropin_name,
92*0Sstevel@tonic-gate (int)len);
93*0Sstevel@tonic-gate PUSH(DS, (fstack_t)buf);
94*0Sstevel@tonic-gate PUSH(DS, 1);
95*0Sstevel@tonic-gate byte_load(env);
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate static void
do_builtin_file(fcode_env_t * env)99*0Sstevel@tonic-gate do_builtin_file(fcode_env_t *env)
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate char *fname;
102*0Sstevel@tonic-gate static char func_name[] = "do-builtin-file";
103*0Sstevel@tonic-gate fstack_t d;
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate if (dropin_name == NULL) {
106*0Sstevel@tonic-gate log_message(MSG_ERROR, "%s: dropin_name not set\n", func_name);
107*0Sstevel@tonic-gate return;
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s'\n", func_name, dropin_name);
110*0Sstevel@tonic-gate push_a_string(env, dropin_name);
111*0Sstevel@tonic-gate load_file(env);
112*0Sstevel@tonic-gate d = POP(DS);
113*0Sstevel@tonic-gate if (d) {
114*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: byte-load '%s'\n", func_name,
115*0Sstevel@tonic-gate dropin_name);
116*0Sstevel@tonic-gate PUSH(DS, 1);
117*0Sstevel@tonic-gate byte_load(env);
118*0Sstevel@tonic-gate } else
119*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: load_file '%s' FAIL\n",
120*0Sstevel@tonic-gate func_name, dropin_name);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate /*
124*0Sstevel@tonic-gate * We need to lookup the builtin name via an FC_RUN_PRIV call to make sure
125*0Sstevel@tonic-gate * the builtin exists. If it exists, then we need to leave the xt of
126*0Sstevel@tonic-gate * do-builtin-dropin on the stack and remember the name for do-dropin. This is
127*0Sstevel@tonic-gate * extremely convoluted because we can't a priori populate
128*0Sstevel@tonic-gate * SUNW,builtin-drivers.
129*0Sstevel@tonic-gate */
130*0Sstevel@tonic-gate static void
builtin_driver_method_hook(fcode_env_t * env)131*0Sstevel@tonic-gate builtin_driver_method_hook(fcode_env_t *env)
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate device_t *device;
134*0Sstevel@tonic-gate char *method, *path;
135*0Sstevel@tonic-gate fc_cell_t len;
136*0Sstevel@tonic-gate fstack_t d;
137*0Sstevel@tonic-gate int error;
138*0Sstevel@tonic-gate static char func_name[] = "builtin-driver-method-hook";
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate d = POP(DS);
141*0Sstevel@tonic-gate CONVERT_PHANDLE(env, device, d);
142*0Sstevel@tonic-gate if (!is_device_builtin_package(env, device)) {
143*0Sstevel@tonic-gate PUSH(DS, d);
144*0Sstevel@tonic-gate PUSH(DS, FALSE);
145*0Sstevel@tonic-gate return;
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate method = pop_a_string(env, NULL);
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate /*
151*0Sstevel@tonic-gate * Check for file in filesystem. If it exists, we'll just try to do
152*0Sstevel@tonic-gate * a do-dropin-file.
153*0Sstevel@tonic-gate */
154*0Sstevel@tonic-gate if ((path = search_for_fcode_file(env, method)) != NULL) {
155*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s' file: '%s'\n", func_name,
156*0Sstevel@tonic-gate method, path);
157*0Sstevel@tonic-gate if (dropin_name) {
158*0Sstevel@tonic-gate FREE(dropin_name);
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate dropin_name = STRDUP(path);
161*0Sstevel@tonic-gate push_a_string(env, "do-builtin-file");
162*0Sstevel@tonic-gate dollar_find(env);
163*0Sstevel@tonic-gate return;
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate error = fc_run_priv(env->private, "sunos,get-fcode-size", 1, 1,
167*0Sstevel@tonic-gate fc_ptr2cell(method), &len);
168*0Sstevel@tonic-gate if (error || len == 0) {
169*0Sstevel@tonic-gate if (len == 0)
170*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s' NOT FOUND\n",
171*0Sstevel@tonic-gate func_name, method);
172*0Sstevel@tonic-gate push_a_string(env, method);
173*0Sstevel@tonic-gate PUSH(DS, d);
174*0Sstevel@tonic-gate PUSH(DS, FALSE);
175*0Sstevel@tonic-gate } else {
176*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s' FOUND len: %x\n",
177*0Sstevel@tonic-gate func_name, method, (int)len);
178*0Sstevel@tonic-gate if (dropin_name) {
179*0Sstevel@tonic-gate FREE(dropin_name);
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate dropin_name = STRDUP(method);
182*0Sstevel@tonic-gate push_a_string(env, "do-builtin-dropin");
183*0Sstevel@tonic-gate dollar_find(env);
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate void
make_a_node(fcode_env_t * env,char * name,int finish)188*0Sstevel@tonic-gate make_a_node(fcode_env_t *env, char *name, int finish)
189*0Sstevel@tonic-gate {
190*0Sstevel@tonic-gate new_device(env);
191*0Sstevel@tonic-gate push_a_string(env, name);
192*0Sstevel@tonic-gate device_name(env);
193*0Sstevel@tonic-gate if (finish)
194*0Sstevel@tonic-gate finish_device(env);
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate void
install_package_nodes(fcode_env_t * env)198*0Sstevel@tonic-gate install_package_nodes(fcode_env_t *env)
199*0Sstevel@tonic-gate {
200*0Sstevel@tonic-gate MYSELF = open_instance_chain(env, env->root_node, 0);
201*0Sstevel@tonic-gate if (MYSELF != NULL) {
202*0Sstevel@tonic-gate make_a_node(env, "packages", 0);
203*0Sstevel@tonic-gate make_a_node(env, "disk-label", 0);
204*0Sstevel@tonic-gate finish_device(env);
205*0Sstevel@tonic-gate make_a_node(env, "SUNW,builtin-drivers", 0);
206*0Sstevel@tonic-gate builtin_driver_device = env->current_device;
207*0Sstevel@tonic-gate finish_device(env);
208*0Sstevel@tonic-gate finish_device(env);
209*0Sstevel@tonic-gate close_instance_chain(env, MYSELF, 0);
210*0Sstevel@tonic-gate device_end(env);
211*0Sstevel@tonic-gate MYSELF = 0;
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate
215*0Sstevel@tonic-gate /*
216*0Sstevel@tonic-gate * find-builtin-driver ( str len -- xt true | false )
217*0Sstevel@tonic-gate */
218*0Sstevel@tonic-gate void
find_builtin_driver(fcode_env_t * env)219*0Sstevel@tonic-gate find_builtin_driver(fcode_env_t *env)
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate fstack_t d;
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gate CHECK_DEPTH(env, 2, "find-builtin-driver");
224*0Sstevel@tonic-gate push_a_string(env, "SUNW,builtin-drivers");
225*0Sstevel@tonic-gate find_package(env);
226*0Sstevel@tonic-gate d = POP(DS);
227*0Sstevel@tonic-gate if (d) {
228*0Sstevel@tonic-gate find_method(env);
229*0Sstevel@tonic-gate } else {
230*0Sstevel@tonic-gate two_drop(env);
231*0Sstevel@tonic-gate PUSH(DS, FALSE);
232*0Sstevel@tonic-gate }
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate
235*0Sstevel@tonic-gate void
exec_builtin_driver(fcode_env_t * env)236*0Sstevel@tonic-gate exec_builtin_driver(fcode_env_t *env)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate fstack_t d;
239*0Sstevel@tonic-gate char *method, *path, *buf;
240*0Sstevel@tonic-gate fc_cell_t len, result;
241*0Sstevel@tonic-gate int error;
242*0Sstevel@tonic-gate static char func_name[] = "exec-builtin-driver";
243*0Sstevel@tonic-gate extern int check_fcode_header(char *, uchar_t *, int);
244*0Sstevel@tonic-gate
245*0Sstevel@tonic-gate CHECK_DEPTH(env, 2, func_name);
246*0Sstevel@tonic-gate method = pop_a_string(env, NULL);
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate /*
249*0Sstevel@tonic-gate * Check for file in filesystem. If it exists, we'll just try to do
250*0Sstevel@tonic-gate * a do-dropin-file.
251*0Sstevel@tonic-gate */
252*0Sstevel@tonic-gate if ((path = search_for_fcode_file(env, method)) != NULL) {
253*0Sstevel@tonic-gate push_a_string(env, path);
254*0Sstevel@tonic-gate load_file(env);
255*0Sstevel@tonic-gate return;
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gate error = fc_run_priv(env->private, "sunos,get-fcode-size", 1, 1,
259*0Sstevel@tonic-gate fc_ptr2cell(method), &len);
260*0Sstevel@tonic-gate if (error || len == 0) {
261*0Sstevel@tonic-gate if (len == 0)
262*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s' NOT FOUND\n",
263*0Sstevel@tonic-gate func_name, method);
264*0Sstevel@tonic-gate PUSH(DS, 0);
265*0Sstevel@tonic-gate return;
266*0Sstevel@tonic-gate }
267*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s' FOUND len: %x\n",
268*0Sstevel@tonic-gate func_name, method, (int)len);
269*0Sstevel@tonic-gate buf = MALLOC(len);
270*0Sstevel@tonic-gate error = fc_run_priv(env->private, "sunos,get-fcode", 3, 1,
271*0Sstevel@tonic-gate fc_ptr2cell(method), fc_ptr2cell(buf), len, &result);
272*0Sstevel@tonic-gate if (error) {
273*0Sstevel@tonic-gate FREE(buf);
274*0Sstevel@tonic-gate PUSH(DS, 0);
275*0Sstevel@tonic-gate return;
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gate if (check_fcode_header(dropin_name, (uchar_t *)buf, len) == 0)
279*0Sstevel@tonic-gate log_message(MSG_WARN, "%s: '%s' fcode header NOT OK\n",
280*0Sstevel@tonic-gate func_name, method);
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s' dropin Fcode: 0x%p/0x%x\n",
283*0Sstevel@tonic-gate func_name, method, buf, (int)len);
284*0Sstevel@tonic-gate PUSH(DS, (fstack_t)buf);
285*0Sstevel@tonic-gate PUSH(DS, len);
286*0Sstevel@tonic-gate }
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gate #pragma init(_init)
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate static void
_init(void)291*0Sstevel@tonic-gate _init(void)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate extern void set_find_method_hook(fcode_env_t *,
294*0Sstevel@tonic-gate void (*)(fcode_env_t *));
295*0Sstevel@tonic-gate fcode_env_t *env = initial_env;
296*0Sstevel@tonic-gate fstack_t d;
297*0Sstevel@tonic-gate
298*0Sstevel@tonic-gate ASSERT(env);
299*0Sstevel@tonic-gate NOTICE;
300*0Sstevel@tonic-gate
301*0Sstevel@tonic-gate set_find_method_hook(env, builtin_driver_method_hook);
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate FORTH(0, "install-package-nodes", install_package_nodes);
304*0Sstevel@tonic-gate FORTH(0, "find-builtin-driver", find_builtin_driver);
305*0Sstevel@tonic-gate FORTH(0, "exec-builtin-driver", exec_builtin_driver);
306*0Sstevel@tonic-gate FORTH(0, "builtin-driver-method-hook",
307*0Sstevel@tonic-gate builtin_driver_method_hook);
308*0Sstevel@tonic-gate FORTH(0, "do-builtin-dropin", do_builtin_dropin);
309*0Sstevel@tonic-gate FORTH(0, "do-builtin-file", do_builtin_file);
310*0Sstevel@tonic-gate }
311