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