Copyright clean-up (part 1):
[AROS.git] / arch / ppc-chrp / boot / openfirmware / src / of1275.c
blob31f1e5cc58105232952b7094b0227a199f8bcfc3
1 /*
2 Copyright © 2008-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <inttypes.h>
7 #include <of1275.h>
8 #include <stdarg.h>
9 #include <support.h>
11 static int32_t (*ofw_call)(void *args);
13 void *stdin;
14 void *stdout;
15 void *stderr;
17 void ofw_init(void *ofw)
19 void *handle;
21 ofw_call = ofw;
23 handle = ofw_find_device("/chosen");
25 if (handle)
27 ofw_get_prop(handle, "stdin", &stdin, sizeof(stdin));
28 ofw_get_prop(handle, "stdout", &stdout, sizeof(stdout));
29 stderr = stdout;
33 int32_t ofw_test(const char *name)
35 struct args
37 char *service;
38 int32_t nargs;
39 int32_t nret;
41 const char *name;
42 int32_t missing;
44 a = {
45 "test", 1, 1,
46 name,
50 ofw_call(&a);
52 return a.missing;
55 void * ofw_peer(void * phandle)
57 struct args
59 char *service;
60 int32_t nargs;
61 int32_t nret;
63 void * phandle;
65 void * sibling_phandle;
67 args = {
68 "peer", 1, 1,
69 phandle,
70 (void *)-1
73 ofw_call(&args);
75 return args.sibling_phandle;
78 void * ofw_child(void * phandle)
80 struct args
82 char *service;
83 int32_t nargs;
84 int32_t nret;
86 void * phandle;
88 void * child_phandle;
90 args = {
91 "child", 1, 1,
92 phandle,
93 (void *)-1
96 ofw_call(&args);
98 return args.child_phandle;
101 void * ofw_parent(void * phandle)
103 struct args
105 char *service;
106 int32_t nargs;
107 int32_t nret;
109 void * phandle;
111 void * parent_phandle;
113 args = {
114 "parent", 1, 1,
115 phandle,
116 (void *)-1
119 ofw_call(&args);
121 return args.parent_phandle;
124 int32_t ofw_get_prop_len(void * phandle, const char *name)
126 struct args
128 char *service;
129 int32_t nargs;
130 int32_t nret;
132 void * phandle;
133 const char *name;
135 int32_t proplen;
137 args = {
138 "getproplen", 2, 1,
139 phandle, name,
143 ofw_call(&args);
145 return args.proplen;
148 int32_t ofw_get_prop(void * phandle, const char *name, void * buf, uint32_t buflen)
150 struct args
152 char *service;
153 int32_t nargs;
154 int32_t nret;
156 void * phandle;
157 const char *name;
158 void * buf;
159 uint32_t buflen;
161 int32_t size;
163 args = {
164 "getprop", 4, 1,
165 phandle, name, buf, buflen,
169 ofw_call(&args);
171 return args.size;
174 int32_t ofw_next_prop(void * phandle, const char *previous, void * buf)
176 struct args
178 char *service;
179 int32_t nargs;
180 int32_t nret;
182 void * phandle;
183 const char *previous;
184 void * buf;
186 int32_t flag;
188 args = {
189 "nextprop", 3, 1,
190 phandle, previous, buf,
194 ofw_call(&args);
196 return args.flag;
199 int32_t ofw_set_prop(void * phandle, const char *name, void * buf, uint32_t buflen)
201 struct args
203 char *service;
204 int32_t nargs;
205 int32_t nret;
207 void * phandle;
208 const char *name;
209 void * buf;
210 uint32_t buflen;
212 int32_t size;
214 args = {
215 "setprop", 4, 1,
216 phandle, name, buf, buflen,
220 ofw_call(&args);
222 return args.size;
225 void * ofw_find_device(char *dev)
227 struct args
229 char *service;
230 int32_t nargs;
231 int32_t nret;
233 char *dev;
236 void * phandle;
238 args = {
239 "finddevice", 1, 1,
240 dev,
241 (void *)-1
244 ofw_call(&args);
246 return args.phandle;
249 void * ofw_open(const char *dev)
251 struct args
253 char *service;
254 int32_t nargs;
255 int32_t nret;
257 const char *dev;
259 void * ihandle;
261 args = {
262 "open", 1, 1,
263 dev,
264 (void *)-1
267 ofw_call(&args);
269 return args.ihandle;
272 void ofw_close(void * ihandle)
274 struct args
276 char *service;
277 int32_t nargs;
278 int32_t nret;
280 void * ihandle;
282 args = {
283 "close", 1, 0,
284 ihandle
287 ofw_call(&args);
290 int32_t ofw_read(void * ihandle, void * addr, uint32_t len)
292 struct args
294 char *service;
295 int32_t nargs;
296 int32_t nret;
298 void * ihandle;
299 void * addr;
300 uint32_t len;
302 int32_t actual;
304 args = {
305 "read", 3, 1,
306 ihandle, addr, len,
310 if (ofw_call(&args))
311 return -1;
312 else
313 return args.actual;
316 int32_t ofw_write(void * ihandle, void * addr, uint32_t len)
318 struct args
320 char *service;
321 int32_t nargs;
322 int32_t nret;
324 void * ihandle;
325 void * addr;
326 uint32_t len;
328 int32_t actual;
330 args = {
331 "write", 3, 1,
332 ihandle, addr, len,
336 if (ofw_call(&args))
337 return -1;
338 else
339 return args.actual;
342 int32_t ofw_seek(void * ihandle, uint32_t pos_hi, uint32_t pos_lo)
344 struct args
346 char *service;
347 int32_t nargs;
348 int32_t nret;
350 void * ihandle;
351 uint32_t pos_hi;
352 uint32_t pos_lo;
354 int32_t status;
356 args = {
357 "seek", 3, 1,
358 ihandle, pos_hi, pos_lo,
362 ofw_call(&args);
364 return args.status;
367 typedef struct __pool {
368 struct __pool *next;
369 char *first_free;
370 uint32_t length;
371 uint32_t free;
372 } pool_t;
374 pool_t *mempool = NULL;
376 void * __claim(uint32_t size)
378 void *ptr = NULL;
379 pool_t *p;
381 if (!mempool) {
382 mempool = ofw_claim(0, 4096, 4096);
383 if (mempool != (void*)-1)
385 mempool->first_free = (char*)mempool + sizeof(pool_t);
386 mempool->length = 4096;
387 mempool->free = 4096 - sizeof(pool_t);
388 mempool->next = NULL;
392 if (size >= (4096 - sizeof(pool_t)))
394 ptr = ofw_claim(0, size, 4096);
395 if (ptr == (void*)-1)
396 ptr = NULL;
398 else
400 p = mempool;
404 if (p->free >= size)
405 break;
407 p = p->next;
408 } while(p);
410 if (!p)
412 p = ofw_claim(0, 4096, 4096);
413 if (p != (void*)-1)
415 p->next = mempool;
416 p->first_free = (char*)p + sizeof(pool_t);
417 p->length = 4096;
418 p->free = 4096 - sizeof(pool_t);
419 mempool = p;
421 else p = NULL;
424 if (p)
426 ptr = p->first_free;
427 p->first_free += size;
428 p->free -= size;
432 return ptr;
435 void * ofw_claim(void * virt, uint32_t size, uint32_t align)
437 struct
439 char *service;
440 int32_t nargs;
441 int32_t nret;
443 void * virt;
444 uint32_t size;
445 uint32_t align;
447 void * baseaddr;
449 args = {
450 "claim", 3, 1,
451 virt, size, align,
452 (void *)-1
455 ofw_call(&args);
457 return args.baseaddr;
460 void ofw_release(void * virt, uint32_t size)
462 struct args
464 char *service;
465 int32_t nargs;
466 int32_t nret;
468 void * virt;
469 uint32_t size;
471 args = {
472 "release", 2, 0,
473 virt, size
476 ofw_call(&args);
479 int32_t ofw_instantiate_rtas(void * ihandle, void * rtas_base, void **rtas_entry)
481 struct args
483 char *service;
484 int32_t nargs;
485 int32_t nret;
487 char *method;
488 void * ihandle;
489 void *rtas_base;
491 int32_t callback;
492 void *rtas_entry;
494 args = {
495 "call-method", 3, 2,
496 "instantiate-rtas", ihandle, rtas_base,
497 -1, (void*)-1
500 ofw_call(&args);
501 *rtas_entry = args.rtas_entry;
502 return args.callback;
505 int32_t ofw_load(void * ihandle, void * addr, int32_t *size)
507 struct args
509 char *service;
510 int32_t nargs;
511 int32_t nret;
513 char *method;
514 void * ihandle;
515 void *addr;
517 int32_t callback;
518 int32_t size;
520 args = {
521 "call-method", 3, 2,
522 "load", ihandle, addr,
523 -1, -1
526 ofw_call(&args);
527 *size = args.size;
528 return args.callback;
531 int32_t ofw_interpret(const char *cmd)
533 struct args
535 char *service;
536 int32_t nargs;
537 int32_t nret;
539 const char *cmd;
541 int32_t retval;
543 args = {
544 "interpret", 1, 1,
545 cmd,
549 ofw_call(&args);
551 return args.retval;
554 int32_t ofw_rtas_token(const char *service)
556 void *handle = ofw_find_device("/rtas");
557 int32_t token;
559 if (handle)
561 ofw_get_prop(handle, service, &token, sizeof(token));
564 return token;
569 int ofw_get_properties(void *phandle, ofw_node_t *node)
571 char property[33];
572 char *previous = NULL;
573 ofw_property_t *prop = NULL;
574 int retval = 0;
576 while(ofw_next_prop(phandle, previous, property) > 0)
578 int name_len = strlen(property) + 1;
579 int prop_len = ofw_get_prop_len(phandle, property);
581 // printf(" property '%s'(%d) length %d\n", property, name_len, prop_len);
583 prop = __claim(sizeof(ofw_property_t) + name_len + prop_len);
584 if (!prop)
586 retval = -1;
587 break;
590 prop->op_name = (char *)&prop->op_storage[0];
591 prop->op_value = &prop->op_storage[name_len];
593 memcpy(prop->op_name, property, name_len);
594 prop->op_length = prop_len;
595 ofw_get_prop(phandle, property, prop->op_value, prop_len);
597 add_tail(&node->on_properties, &prop->op_node);
598 previous = prop->op_name;
601 return retval;
604 int ofw_build_tree(void *phandle, ofw_node_t *node, int depth)
606 int retval = 0;
607 void *hnd;
608 ofw_node_t *child = NULL;
609 int i;
611 for (i=0; i < depth; i++)
612 printf(" ");
614 // printf("ofw_build_tree(%p, %p('%s'), %d)\n", phandle, node, node->on_name, depth);
616 /* Get properties of the node */
617 ofw_get_properties(phandle, node);
619 hnd = ofw_child(phandle);
620 if (hnd) do
622 int name_len = ofw_get_prop_len(hnd, "name");
624 if (name_len > 0)
625 name_len++;
627 child = __claim(sizeof(ofw_node_t) + name_len);
629 if (!child)
631 retval = -1;
632 break;
635 if (name_len > 1)
637 child->on_name = (char*)child->on_storage;
638 ofw_get_prop(hnd, "name", child->on_name, name_len-1);
640 else
641 child->on_name = NULL;
643 new_list(&child->on_children);
644 new_list(&child->on_properties);
646 add_tail(&node->on_children, &child->on_node);
648 retval = ofw_build_tree(hnd, child, depth + 1);
650 if (retval)
651 break;
653 hnd = ofw_peer(hnd);
654 } while(hnd);
656 return retval;
659 ofw_node_t *ofw_scan_tree()
661 void *p_root = ofw_peer(NULL);
662 ofw_node_t *root = __claim(sizeof(ofw_node_t) + 2);
664 if (root)
666 memcpy(root->on_storage, "/", 2);
667 root->on_name = (char*)root->on_storage;
668 new_list(&root->on_children);
669 new_list(&root->on_properties);
671 ofw_build_tree(p_root, root, 0);
674 return root;
677 /* Copy a string property into allocated memory */
678 char *ofw_GetString(void *handle, const char *prop)
680 int32_t len = ofw_get_prop_len(handle, prop);
681 char *str = ofw_claim(NULL, len + 1, 4);
683 ofw_get_prop(handle, prop, str, 255);
684 str[len] = 0;
686 return str;