2 * Creation Date: <2003/11/24 12:30:18 samuel>
3 * Time-stamp: <2004/01/07 19:37:38 samuel>
9 * Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
18 #include "libopenbios/bindings.h"
19 #include "libc/string.h"
20 #include "libc/stdlib.h"
21 #include "libc/byteorder.h"
24 /************************************************************************/
25 /* forth interface glue */
26 /************************************************************************/
29 push_str( const char *str
)
32 PUSH( str
? strlen(str
) : 0 );
35 /* WARNING: sloooow - AVOID */
37 feval( const char *str
)
40 return eword("evaluate", 2);
44 _eword( const char *word
, xt_t
*cache_xt
, int nargs
)
46 static xt_t catch_xt
= 0;
50 catch_xt
= findword("catch");
52 *cache_xt
= findword( (char*)word
);
56 enterforth( catch_xt
);
63 /* note: only the built-in dictionary is searched */
65 _fword( const char *word
, xt_t
*cache_xt
)
68 *cache_xt
= findword( (char*)word
);
71 enterforth( *cache_xt
);
78 _selfword( const char *method
, xt_t
*cache_xt
)
81 *cache_xt
= find_ih_method( method
, my_self() );
83 enterforth( *cache_xt
);
90 _parword( const char *method
, xt_t
*cache_xt
)
93 *cache_xt
= find_ih_method( method
, my_parent() );
95 enterforth( *cache_xt
);
102 bind_func( const char *name
, void (*func
)(void) )
110 bind_xtfunc( const char *name
, xt_t xt
, ucell arg
, void (*func
)(void) )
116 fword("is-xt-cfunc");
120 bind_noname_func( void (*func
)(void) )
123 fword("is-noname-cfunc");
135 /************************************************************************/
136 /* ihandle related */
137 /************************************************************************/
140 ih_to_phandle( ihandle_t ih
)
143 fword("ihandle>phandle");
162 find_package_method( const char *method
, phandle_t ph
)
166 fword("find-method");
173 find_ih_method( const char *method
, ihandle_t ih
)
175 return find_package_method( method
, ih_to_phandle(ih
) );
180 find_parent_method( const char *method
)
182 return find_ih_method( method
, my_parent() );
186 call_package( xt_t xt
, ihandle_t ihandle
)
190 fword("call-package");
194 call_parent( xt_t xt
)
197 fword("call-parent");
201 call_parent_method( const char *method
)
204 fword("$call-parent");
208 /************************************************************************/
209 /* open/close package/dev */
210 /************************************************************************/
213 open_dev( const char *spec
)
221 close_dev( ihandle_t ih
)
228 open_package( const char *argstr
, phandle_t ph
)
232 fword("open-package");
237 close_package( ihandle_t ih
)
240 fword("close-package");
244 /************************************************************************/
245 /* ihandle arguments */
246 /************************************************************************/
249 pop_fstr_copy( void )
252 char *str
, *p
= (char*)POP();
255 str
= malloc( len
+ 1 );
258 memcpy( str
, p
, len
);
267 return pop_fstr_copy();
271 /************************************************************************/
273 /************************************************************************/
276 set_property( phandle_t ph
, const char *name
, const char *buf
, int len
)
279 printk("set_property: NULL phandle\n");
286 fword("set-property");
290 set_int_property( phandle_t ph
, const char *name
, u32 val
)
292 u32 swapped
=__cpu_to_be32(val
);
293 set_property( ph
, name
, (char*)&swapped
, sizeof(swapped
) );
297 get_property( phandle_t ph
, const char *name
, int *retlen
)
306 fword("get-package-property");
316 get_int_property( phandle_t ph
, const char *name
, int *retlen
)
320 if( !(p
=(u32
*)get_property(ph
, name
, retlen
)) )
322 return __be32_to_cpu(*p
);
326 /************************************************************************/
327 /* device selection / iteration */
328 /************************************************************************/
331 activate_dev( phandle_t ph
)
334 fword("active-package!");
338 activate_device( const char *str
)
340 phandle_t ph
= find_dev( str
);
354 fword("active-package");
359 find_dev( const char *path
)
370 dt_iter_begin( void )
372 fword("iterate-tree-begin");
377 dt_iterate( phandle_t last_tree
)
380 return dt_iter_begin();
382 PUSH_ph( last_tree
);
383 fword("iterate-tree");
388 dt_iterate_type( phandle_t last_tree
, const char *type
)
391 last_tree
= dt_iter_begin();
393 /* root node is never matched but we don't care about that */
394 while( (last_tree
= dt_iterate(last_tree
)) ) {
395 char *s
= get_property( last_tree
, "device_type", NULL
);
396 if( s
&& !strcmp(type
, s
) )
403 /************************************************************************/
405 /************************************************************************/
408 make_openable( int only_parents
)
410 phandle_t ph
, save_ph
= get_cur_dev();
422 activate_dev( save_ph
);
428 void (*func
)(cell v
);
436 add_methods( int flags
, int size
, const method_t
*methods
, int nmet
)
441 /* nodes might be matched multiple times */
442 if( find_package_method(methods
[0].name
, get_cur_dev()) )
451 for( i
=0; i
<nmet
; i
++ ) {
452 /* null-name methods specify static initializers */
453 if( !methods
[i
].name
) {
454 typedef void (*initfunc
)( void *p
);
460 (*(initfunc
)methods
[i
].func
)( buf
);
464 bind_func( methods
[i
].name
, methods
[i
].func
);
466 bind_xtfunc( methods
[i
].name
, xt
, (ucell
)methods
[i
].func
,
470 if( flags
& INSTALL_OPEN
)
475 bind_node( int flags
, int size
, const char * const *paths
, int npaths
,
476 const method_t
*methods
, int nmet
)
478 phandle_t save_ph
= get_cur_dev();
481 for( i
=0; i
<npaths
; i
++ ) {
482 const char *name
= paths
[i
];
488 while( (ph
=dt_iterate_type(ph
, name
)) ) {
490 add_methods( flags
, size
, methods
, nmet
);
496 if( activate_device(name
) )
497 add_methods( flags
, size
, methods
, nmet
);
498 else if( *name
== '+' ) {
499 /* create node (and missing parents) */
500 if( !activate_device(++name
) ) {
502 fword("create-node");
504 add_methods( flags
, size
, methods
, nmet
);
507 activate_dev( save_ph
);
511 bind_new_node( int flags
, int size
, const char *name
,
512 const method_t
*methods
, int nmet
)
514 phandle_t save_ph
= get_cur_dev();
518 fword("create-node");
519 add_methods( flags
, size
, methods
, nmet
);
520 new_ph
= get_cur_dev();
522 activate_dev( save_ph
);