summaryrefslogtreecommitdiffstats
path: root/qemu/roms/openbios/libopenbios/bindings.c
blob: 4f7a99379b211209722d3be52fd6d744504d4c7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
 *   Creation Date: <2003/11/24 12:30:18 samuel>
 *   Time-stamp: <2004/01/07 19:37:38 samuel>
 *
 *	<bindings.c>
 *
 *	Forth bindings
 *
 *   Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   version 2
 *
 */

#include "config.h"
#include "libopenbios/bindings.h"
#include "libc/string.h"
#include "libc/stdlib.h"
#include "libc/byteorder.h"


/************************************************************************/
/*	forth interface glue						*/
/************************************************************************/

void
push_str( const char *str )
{
	PUSH( pointer2cell(str) );
	PUSH( str ? strlen(str) : 0 );
}

/* WARNING: sloooow - AVOID */
cell
feval( const char *str )
{
	push_str( str );
	return eword("evaluate", 2);
}

cell
_eword( const char *word, xt_t *cache_xt, int nargs )
{
	static xt_t catch_xt = 0;
	cell ret = -1;

	if( !catch_xt )
		catch_xt = findword("catch");
	if( !*cache_xt )
		*cache_xt = findword( (char*)word );

	if( *cache_xt ) {
		PUSH_xt( *cache_xt );
		enterforth( catch_xt );
		if( (ret=POP()) )
			dstackcnt -= nargs;
	}
	return ret;
}

/* note: only the built-in dictionary is searched */
int
_fword( const char *word, xt_t *cache_xt )
{
	if( !*cache_xt )
		*cache_xt = findword( (char*)word );

	if( *cache_xt ) {
		enterforth( *cache_xt );
		return 0;
	}
	return -1;
}

int
_selfword( const char *method, xt_t *cache_xt )
{
	if( !*cache_xt )
		*cache_xt = find_ih_method( method, my_self() );
	if( *cache_xt ) {
		enterforth( *cache_xt );
		return 0;
	}
	return -1;
}

int
_parword( const char *method, xt_t *cache_xt )
{
	if( !*cache_xt )
		*cache_xt = find_ih_method( method, my_parent() );
	if( *cache_xt ) {
		enterforth( *cache_xt );
		return 0;
	}
	return -1;
}

void
bind_func( const char *name, void (*func)(void) )
{
	PUSH( pointer2cell(func) );
	push_str( name );
	fword("is-cfunc");
}

void
bind_xtfunc( const char *name, xt_t xt, ucell arg, void (*func)(void) )
{
	PUSH_xt( xt );
	PUSH( arg );
	PUSH( pointer2cell(func) );
	push_str( name );
	fword("is-xt-cfunc");
}

xt_t
bind_noname_func( void (*func)(void) )
{
	PUSH( pointer2cell(func) );
	fword("is-noname-cfunc");
	return POP_xt();
}

void
throw( int error )
{
	PUSH( error );
	fword("throw");
}


/************************************************************************/
/*	ihandle related							*/
/************************************************************************/

phandle_t
ih_to_phandle( ihandle_t ih )
{
	PUSH_ih( ih );
	fword("ihandle>phandle");
	return POP_ph();
}

ihandle_t
my_parent( void )
{
	fword("my-parent");
	return POP_ih();
}

ihandle_t
my_self( void )
{
	fword("my-self");
	return POP_ih();
}

xt_t
find_package_method( const char *method, phandle_t ph )
{
	push_str( method );
	PUSH_ph( ph );
	fword("find-method");
	if( POP() )
		return POP_xt();
	return 0;
}

xt_t
find_ih_method( const char *method, ihandle_t ih )
{
	return find_package_method( method, ih_to_phandle(ih) );
}


xt_t
find_parent_method( const char *method )
{
	return find_ih_method( method, my_parent() );
}

void
call_package( xt_t xt, ihandle_t ihandle )
{
	PUSH_xt( xt );
	PUSH_ih( ihandle );
	fword("call-package");
}

void
call_parent( xt_t xt )
{
	PUSH_xt( xt );
	fword("call-parent");
}

void
call_parent_method( const char *method )
{
	push_str( method );
	fword("$call-parent");
}


/************************************************************************/
/*	open/close package/dev						*/
/************************************************************************/

ihandle_t
open_dev( const char *spec )
{
	push_str( spec );
	fword("open-dev");
	return POP_ih();
}

void
close_dev( ihandle_t ih )
{
	PUSH_ih( ih );
	fword("close-dev");
}

ihandle_t
open_package( const char *argstr, phandle_t ph )
{
	push_str( argstr );
	PUSH_ph( ph );
	fword("open-package");
	return POP_ih();
}

void
close_package( ihandle_t ih )
{
	PUSH_ih( ih );
	fword("close-package");
}


/************************************************************************/
/*	ihandle arguments						*/
/************************************************************************/

char *
pop_fstr_copy( void )
{
	int len = POP();
	char *str, *p = (char*)cell2pointer(POP());
	if( !len )
		return NULL;
	str = malloc( len + 1 );
        if( !str )
                return NULL;
	memcpy( str, p, len );
	str[len] = 0;
	return str;
}

char *
my_args_copy( void )
{
	fword("my-args");
	return pop_fstr_copy();
}


/************************************************************************/
/*	properties							*/
/************************************************************************/

void
set_property( phandle_t ph, const char *name, const char *buf, int len )
{
	if( !ph ) {
		printk("set_property: NULL phandle\n");
		return;
	}
	PUSH(pointer2cell(buf));
	PUSH(len);
	push_str( name );
	PUSH_ph(ph);
	fword("set-property");
}

void
set_int_property( phandle_t ph, const char *name, u32 val )
{
	u32 swapped=__cpu_to_be32(val);
	set_property( ph, name, (char*)&swapped, sizeof(swapped) );
}

char *
get_property( phandle_t ph, const char *name, int *retlen )
{
	int len;

	if( retlen )
		*retlen = -1;

	push_str( name );
	PUSH_ph( ph );
	fword("get-package-property");
	if( POP() )
		return NULL;
	len = POP();
	if( retlen )
		*retlen = len;
	return (char*)cell2pointer(POP());
}

u32
get_int_property( phandle_t ph, const char *name, int *retlen )
{
	u32 *p;

	if( !(p=(u32 *)get_property(ph, name, retlen)) )
		return 0;
	return __be32_to_cpu(*p);
}


/************************************************************************/
/*	device selection / iteration					*/
/************************************************************************/

void
activate_dev( phandle_t ph )
{
	PUSH_ph( ph );
	fword("active-package!");
}

phandle_t
activate_device( const char *str )
{
	phandle_t ph = find_dev( str );
	activate_dev( ph );
	return ph;
}

void
device_end( void )
{
	fword("device-end");
}

phandle_t
get_cur_dev( void )
{
	fword("active-package");
	return POP_ph();
}

phandle_t
find_dev( const char *path )
{
	phandle_t ret = 0;
	push_str( path );
	fword("(find-dev)");
	if( POP() )
		return POP_ph();
	return ret;
}

char *
get_path_from_ph( phandle_t ph )
{
	PUSH(ph);
	fword("get-package-path");
	return pop_fstr_copy();
}

phandle_t
dt_iter_begin( void )
{
	fword("iterate-tree-begin");
	return POP_ph();
}

phandle_t
dt_iterate( phandle_t last_tree )
{
        if( !last_tree )
		return dt_iter_begin();

        PUSH_ph( last_tree );
	fword("iterate-tree");
	return POP_ph();
}

phandle_t
dt_iterate_type( phandle_t last_tree, const char *type )
{
        if( !last_tree )
                last_tree = dt_iter_begin();

	/* root node is never matched but we don't care about that */
        while( (last_tree = dt_iterate(last_tree)) ) {
                char *s = get_property( last_tree, "device_type", NULL );
		if( s && !strcmp(type, s) )
			break;
	}
        return last_tree;
}


/************************************************************************/
/*	node methods							*/
/************************************************************************/

void
make_openable( int only_parents )
{
	phandle_t ph, save_ph = get_cur_dev();
	PUSH_ph( save_ph );

	for( ;; ) {
		if( only_parents++ )
			fword("parent");
		if( !(ph=POP_ph()) )
			break;
		activate_dev( ph );
		PUSH_ph( ph );
		fword("is-open");
	}
	activate_dev( save_ph );
}

static void
call1_func( void )
{
	void (*func)(cell v);
	func = (void*)cell2pointer(POP());

	(*func)( POP() );
}


static void
add_methods( int flags, int size, const method_t *methods, int nmet )
{
	xt_t xt=0;
	int i;

	/* nodes might be matched multiple times */
	if( find_package_method(methods[0].name, get_cur_dev()) )
		return;

	if( size ) {
		PUSH( size );
		fword("is-ibuf");
		xt = POP_xt();
	}

	for( i=0; i<nmet; i++ ) {
		/* null-name methods specify static initializers */
		if( !methods[i].name ) {
			typedef void (*initfunc)( void *p );
			char *buf = NULL;
			if( xt ) {
				enterforth( xt );
				buf = (char*)cell2pointer(POP());
			}
			(*(initfunc)methods[i].func)( buf );
			continue;
		}
		if( !size )
			bind_func( methods[i].name, methods[i].func );
		else
			bind_xtfunc( methods[i].name, xt, pointer2cell(methods[i].func),
				     &call1_func );
	}

	if( flags & INSTALL_OPEN )
		make_openable(0);
}

void
bind_node( int flags, int size, const char * const *paths, int npaths,
	   const method_t *methods, int nmet )
{
	phandle_t save_ph = get_cur_dev();
	int i;

	for( i=0; i<npaths; i++ ) {
		const char *name = paths[i];

		/* type matching? */
		if( *name == 'T' ) {
			phandle_t ph = 0;
			name++;
			while( (ph=dt_iterate_type(ph, name)) ) {
				activate_dev( ph );
				add_methods( flags, size, methods, nmet );
			}
			continue;
		}

		/* path patching */
		if( activate_device(name) )
			add_methods( flags, size, methods, nmet );
		else if( *name == '+' ) {
			/* create node (and missing parents) */
			if( !activate_device(++name) ) {
				push_str( name );
				fword("create-node");
			}
			add_methods( flags, size, methods, nmet );
		}
	}
	activate_dev( save_ph );
}

phandle_t
bind_new_node( int flags, int size, const char *name,
	   const method_t *methods, int nmet )
{
	phandle_t save_ph = get_cur_dev();
	phandle_t new_ph;
	/* create node */
	push_str( name );
	fword("create-node");
	add_methods( flags, size, methods, nmet );
    new_ph = get_cur_dev();

	activate_dev( save_ph );
	return new_ph;
}