18abaaaf7SAxel Dörfler/*
27a3fa7d3SAxel Dörfler * Copyright 2003, Axel D��rfler, axeld@pinc-software.de. All rights reserved.
37a3fa7d3SAxel Dörfler * Distributed under the terms of the MIT License.
47a3fa7d3SAxel Dörfler */
58abaaaf7SAxel Dörfler
6957a1b17SIngo Weinhold#include <platform/openfirmware/openfirmware.h>
78abaaaf7SAxel Dörfler
88abaaaf7SAxel Dörfler#include <stdarg.h>
98abaaaf7SAxel Dörfler
108abaaaf7SAxel Dörfler
118abaaaf7SAxel Dörfler// OpenFirmware entry function
12bd185b41SIngo Weinholdstatic int (*gCallOpenFirmware)(void *) = 0;
1367e8a139SAxel Dörflerint gChosen;
148abaaaf7SAxel Dörfler
158abaaaf7SAxel Dörfler
1667e8a139SAxel Dörflerstatus_t
178abaaaf7SAxel Dörflerof_init(int (*openFirmwareEntry)(void *))
188abaaaf7SAxel Dörfler{
198abaaaf7SAxel Dörfler	gCallOpenFirmware = openFirmwareEntry;
2067e8a139SAxel Dörfler
2167e8a139SAxel Dörfler	gChosen = of_finddevice("/chosen");
2267e8a139SAxel Dörfler	if (gChosen == OF_FAILED)
2367e8a139SAxel Dörfler		return B_ERROR;
2467e8a139SAxel Dörfler
2567e8a139SAxel Dörfler	return B_OK;
268abaaaf7SAxel Dörfler}
278abaaaf7SAxel Dörfler
288abaaaf7SAxel Dörfler
298abaaaf7SAxel Dörflerint
3022a65222SIngo Weinholdof_call_client_function(const char *method, int numArgs, int numReturns, ...)
318abaaaf7SAxel Dörfler{
328abaaaf7SAxel Dörfler	struct {
338abaaaf7SAxel Dörfler		const char	*name;
348abaaaf7SAxel Dörfler		int			num_args;
358abaaaf7SAxel Dörfler		int			num_returns;
368abaaaf7SAxel Dörfler		void		*args[10];
378abaaaf7SAxel Dörfler	} args = {method, numArgs, numReturns};
388abaaaf7SAxel Dörfler	va_list list;
398abaaaf7SAxel Dörfler	int i;
408abaaaf7SAxel Dörfler
418abaaaf7SAxel Dörfler	// iterate over all arguments and copy them into the
428abaaaf7SAxel Dörfler	// structure passed over to the OpenFirmware
438abaaaf7SAxel Dörfler
448abaaaf7SAxel Dörfler	va_start(list, numReturns);
458abaaaf7SAxel Dörfler	for (i = 0; i < numArgs; i++) {
468abaaaf7SAxel Dörfler		// copy args
478abaaaf7SAxel Dörfler		args.args[i] = (void *)va_arg(list, void *);
488abaaaf7SAxel Dörfler	}
498abaaaf7SAxel Dörfler	for (i = numArgs; i < numArgs + numReturns; i++) {
508abaaaf7SAxel Dörfler		// clear return values
518abaaaf7SAxel Dörfler		args.args[i] = NULL;
528abaaaf7SAxel Dörfler	}
53bd185b41SIngo Weinhold
548abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
558abaaaf7SAxel Dörfler		return OF_FAILED;
568abaaaf7SAxel Dörfler
578abaaaf7SAxel Dörfler	if (numReturns > 0) {
588abaaaf7SAxel Dörfler		// copy return values over to the provided location
59bd185b41SIngo Weinhold
608abaaaf7SAxel Dörfler		for (i = numArgs; i < numArgs + numReturns; i++) {
618abaaaf7SAxel Dörfler			void **store = va_arg(list, void **);
628abaaaf7SAxel Dörfler			if (store)
638abaaaf7SAxel Dörfler				*store = args.args[i];
648abaaaf7SAxel Dörfler		}
658abaaaf7SAxel Dörfler	}
668abaaaf7SAxel Dörfler	va_end(list);
678abaaaf7SAxel Dörfler
688abaaaf7SAxel Dörfler	return 0;
698abaaaf7SAxel Dörfler}
708abaaaf7SAxel Dörfler
718abaaaf7SAxel Dörfler
72b821ceccSIngo Weinholdint
73b821ceccSIngo Weinholdof_interpret(const char *command, int numArgs, int numReturns, ...)
74b821ceccSIngo Weinhold{
75b821ceccSIngo Weinhold	struct {
76b821ceccSIngo Weinhold		const char	*name;
77b821ceccSIngo Weinhold		int			num_args;
78b821ceccSIngo Weinhold		int			num_returns;
79b821ceccSIngo Weinhold			// "IN:	[string] cmd, stack_arg1, ..., stack_argP
80b821ceccSIngo Weinhold			// OUT:	catch-result, stack_result1, ..., stack_resultQ
81b821ceccSIngo Weinhold			// [...]
82b821ceccSIngo Weinhold			// An implementation shall allow at least six stack_arg and six
83b821ceccSIngo Weinhold			// stack_result items."
84b821ceccSIngo Weinhold		const char	*command;
85b821ceccSIngo Weinhold		void		*args[13];
8622a65222SIngo Weinhold	} args = {"interpret", numArgs + 1, numReturns + 1, command};
8722a65222SIngo Weinhold	va_list list;
8822a65222SIngo Weinhold	int i;
8922a65222SIngo Weinhold
9022a65222SIngo Weinhold	// iterate over all arguments and copy them into the
9122a65222SIngo Weinhold	// structure passed over to the OpenFirmware
9222a65222SIngo Weinhold
9322a65222SIngo Weinhold	va_start(list, numReturns);
9422a65222SIngo Weinhold	for (i = 0; i < numArgs; i++) {
9522a65222SIngo Weinhold		// copy args
9622a65222SIngo Weinhold		args.args[i] = (void *)va_arg(list, void *);
9722a65222SIngo Weinhold	}
9822a65222SIngo Weinhold	for (i = numArgs; i < numArgs + numReturns + 1; i++) {
9922a65222SIngo Weinhold		// clear return values
10022a65222SIngo Weinhold		args.args[i] = NULL;
10122a65222SIngo Weinhold	}
10222a65222SIngo Weinhold
10322a65222SIngo Weinhold	// args.args[numArgs] is the "catch-result" return value
10422a65222SIngo Weinhold	if (gCallOpenFirmware(&args) == OF_FAILED || args.args[numArgs])
10522a65222SIngo Weinhold		return OF_FAILED;
10622a65222SIngo Weinhold
10722a65222SIngo Weinhold	if (numReturns > 0) {
10822a65222SIngo Weinhold		// copy return values over to the provided location
109bd185b41SIngo Weinhold
11022a65222SIngo Weinhold		for (i = numArgs + 1; i < numArgs + 1 + numReturns; i++) {
11122a65222SIngo Weinhold			void **store = va_arg(list, void **);
11222a65222SIngo Weinhold			if (store)
11322a65222SIngo Weinhold				*store = args.args[i];
11422a65222SIngo Weinhold		}
11522a65222SIngo Weinhold	}
11622a65222SIngo Weinhold	va_end(list);
11722a65222SIngo Weinhold
11822a65222SIngo Weinhold	return 0;
11922a65222SIngo Weinhold}
12022a65222SIngo Weinhold
12122a65222SIngo Weinhold
12222a65222SIngo Weinholdint
12322a65222SIngo Weinholdof_call_method(int handle, const char *method, int numArgs, int numReturns, ...)
12422a65222SIngo Weinhold{
12522a65222SIngo Weinhold	struct {
12622a65222SIngo Weinhold		const char	*name;
12722a65222SIngo Weinhold		int			num_args;
12822a65222SIngo Weinhold		int			num_returns;
12922a65222SIngo Weinhold			// "IN:	[string] method, ihandle, stack_arg1, ..., stack_argP
13022a65222SIngo Weinhold			// OUT:	catch-result, stack_result1, ..., stack_resultQ
13122a65222SIngo Weinhold			// [...]
13222a65222SIngo Weinhold			// An implementation shall allow at least six stack_arg and six
13322a65222SIngo Weinhold			// stack_result items."
13422a65222SIngo Weinhold		const char	*method;
13522a65222SIngo Weinhold		int			handle;
13622a65222SIngo Weinhold		void		*args[13];
13722a65222SIngo Weinhold	} args = {"call-method", numArgs + 2, numReturns + 1, method, handle};
138b821ceccSIngo Weinhold	va_list list;
139b821ceccSIngo Weinhold	int i;
140b821ceccSIngo Weinhold
141b821ceccSIngo Weinhold	// iterate over all arguments and copy them into the
142b821ceccSIngo Weinhold	// structure passed over to the OpenFirmware
143b821ceccSIngo Weinhold
144b821ceccSIngo Weinhold	va_start(list, numReturns);
145b821ceccSIngo Weinhold	for (i = 0; i < numArgs; i++) {
146b821ceccSIngo Weinhold		// copy args
147b821ceccSIngo Weinhold		args.args[i] = (void *)va_arg(list, void *);
148b821ceccSIngo Weinhold	}
149b821ceccSIngo Weinhold	for (i = numArgs; i < numArgs + numReturns + 1; i++) {
150b821ceccSIngo Weinhold		// clear return values
151b821ceccSIngo Weinhold		args.args[i] = NULL;
152b821ceccSIngo Weinhold	}
153b821ceccSIngo Weinhold
154b821ceccSIngo Weinhold	// args.args[numArgs] is the "catch-result" return value
155b821ceccSIngo Weinhold	if (gCallOpenFirmware(&args) == OF_FAILED || args.args[numArgs])
156b821ceccSIngo Weinhold		return OF_FAILED;
157b821ceccSIngo Weinhold
158b821ceccSIngo Weinhold	if (numReturns > 0) {
159b821ceccSIngo Weinhold		// copy return values over to the provided location
160bd185b41SIngo Weinhold
161b821ceccSIngo Weinhold		for (i = numArgs + 1; i < numArgs + 1 + numReturns; i++) {
162b821ceccSIngo Weinhold			void **store = va_arg(list, void **);
163b821ceccSIngo Weinhold			if (store)
164b821ceccSIngo Weinhold				*store = args.args[i];
165b821ceccSIngo Weinhold		}
166b821ceccSIngo Weinhold	}
167b821ceccSIngo Weinhold	va_end(list);
168b821ceccSIngo Weinhold
169b821ceccSIngo Weinhold	return 0;
170b821ceccSIngo Weinhold}
171b821ceccSIngo Weinhold
172b821ceccSIngo Weinhold
173bd185b41SIngo Weinholdint
1748abaaaf7SAxel Dörflerof_finddevice(const char *device)
1758abaaaf7SAxel Dörfler{
1768abaaaf7SAxel Dörfler	struct {
1778abaaaf7SAxel Dörfler		const char	*name;
1788abaaaf7SAxel Dörfler		int			num_args;
1798abaaaf7SAxel Dörfler		int			num_returns;
1808abaaaf7SAxel Dörfler		const char	*device;
1818abaaaf7SAxel Dörfler		int			handle;
1828abaaaf7SAxel Dörfler	} args = {"finddevice", 1, 1, device, 0};
1838abaaaf7SAxel Dörfler
1848abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
1858abaaaf7SAxel Dörfler		return OF_FAILED;
1868abaaaf7SAxel Dörfler
1878abaaaf7SAxel Dörfler	return args.handle;
1888abaaaf7SAxel Dörfler}
1898abaaaf7SAxel Dörfler
1908abaaaf7SAxel Dörfler
1918abaaaf7SAxel Dörfler/** Returns the first child of the given node
1928abaaaf7SAxel Dörfler */
1938abaaaf7SAxel Dörfler
1948abaaaf7SAxel Dörflerint
1958abaaaf7SAxel Dörflerof_child(int node)
1968abaaaf7SAxel Dörfler{
1978abaaaf7SAxel Dörfler	struct {
1988abaaaf7SAxel Dörfler		const char	*name;
1998abaaaf7SAxel Dörfler		int			num_args;
2008abaaaf7SAxel Dörfler		int			num_returns;
2018abaaaf7SAxel Dörfler		int			node;
2028abaaaf7SAxel Dörfler		int			child;
2038abaaaf7SAxel Dörfler	} args = {"child", 1, 1, node, 0};
2048abaaaf7SAxel Dörfler
2058abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
2068abaaaf7SAxel Dörfler		return OF_FAILED;
2078abaaaf7SAxel Dörfler
2088abaaaf7SAxel Dörfler	return args.child;
2098abaaaf7SAxel Dörfler}
2108abaaaf7SAxel Dörfler
2118abaaaf7SAxel Dörfler
2128abaaaf7SAxel Dörfler/** Returns the next sibling of the given node
2138abaaaf7SAxel Dörfler */
2148abaaaf7SAxel Dörfler
2158abaaaf7SAxel Dörflerint
2168abaaaf7SAxel Dörflerof_peer(int node)
2178abaaaf7SAxel Dörfler{
2188abaaaf7SAxel Dörfler	struct {
2198abaaaf7SAxel Dörfler		const char	*name;
2208abaaaf7SAxel Dörfler		int			num_args;
2218abaaaf7SAxel Dörfler		int			num_returns;
2228abaaaf7SAxel Dörfler		int			node;
2238abaaaf7SAxel Dörfler		int			next_sibling;
2248abaaaf7SAxel Dörfler	} args = {"peer", 1, 1, node, 0};
2258abaaaf7SAxel Dörfler
2268abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
2278abaaaf7SAxel Dörfler		return OF_FAILED;
2288abaaaf7SAxel Dörfler
2298abaaaf7SAxel Dörfler	return args.next_sibling;
2308abaaaf7SAxel Dörfler}
2318abaaaf7SAxel Dörfler
2328abaaaf7SAxel Dörfler
2338abaaaf7SAxel Dörfler/** Returns the parent of the given node
2348abaaaf7SAxel Dörfler */
2358abaaaf7SAxel Dörfler
2368abaaaf7SAxel Dörflerint
2378abaaaf7SAxel Dörflerof_parent(int node)
2388abaaaf7SAxel Dörfler{
2398abaaaf7SAxel Dörfler	struct {
2408abaaaf7SAxel Dörfler		const char	*name;
2418abaaaf7SAxel Dörfler		int			num_args;
2428abaaaf7SAxel Dörfler		int			num_returns;
2438abaaaf7SAxel Dörfler		int			node;
2448abaaaf7SAxel Dörfler		int			parent;
2458abaaaf7SAxel Dörfler	} args = {"parent", 1, 1, node, 0};
2468abaaaf7SAxel Dörfler
2478abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
2488abaaaf7SAxel Dörfler		return OF_FAILED;
2498abaaaf7SAxel Dörfler
2508abaaaf7SAxel Dörfler	return args.parent;
2518abaaaf7SAxel Dörfler}
2528abaaaf7SAxel Dörfler
2538abaaaf7SAxel Dörfler
2548abaaaf7SAxel Dörflerint
2558abaaaf7SAxel Dörflerof_instance_to_path(int instance, char *pathBuffer, int bufferSize)
2568abaaaf7SAxel Dörfler{
2578abaaaf7SAxel Dörfler	struct {
2588abaaaf7SAxel Dörfler		const char	*name;
2598abaaaf7SAxel Dörfler		int			num_args;
2608abaaaf7SAxel Dörfler		int			num_returns;
2618abaaaf7SAxel Dörfler		int			instance;
2628abaaaf7SAxel Dörfler		char		*path_buffer;
2638abaaaf7SAxel Dörfler		int			buffer_size;
2648abaaaf7SAxel Dörfler		int			size;
2658abaaaf7SAxel Dörfler	} args = {"instance-to-path", 3, 1, instance, pathBuffer, bufferSize, 0};
2668abaaaf7SAxel Dörfler
2678abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
2688abaaaf7SAxel Dörfler		return OF_FAILED;
2698abaaaf7SAxel Dörfler
2708abaaaf7SAxel Dörfler	return args.size;
2718abaaaf7SAxel Dörfler}
2728abaaaf7SAxel Dörfler
2738abaaaf7SAxel Dörfler
274bd185b41SIngo Weinholdint
2758abaaaf7SAxel Dörflerof_instance_to_package(int instance)
2768abaaaf7SAxel Dörfler{
2778abaaaf7SAxel Dörfler	struct {
2788abaaaf7SAxel Dörfler		const char	*name;
2798abaaaf7SAxel Dörfler		int			num_args;
2808abaaaf7SAxel Dörfler		int			num_returns;
2818abaaaf7SAxel Dörfler		int			instance;
2828abaaaf7SAxel Dörfler		int			package;
2838abaaaf7SAxel Dörfler	} args = {"instance-to-package", 1, 1, instance, 0};
2848abaaaf7SAxel Dörfler
2858abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
2868abaaaf7SAxel Dörfler		return OF_FAILED;
2878abaaaf7SAxel Dörfler
2888abaaaf7SAxel Dörfler	return args.package;
2898abaaaf7SAxel Dörfler}
2908abaaaf7SAxel Dörfler
2918abaaaf7SAxel Dörfler
292bd185b41SIngo Weinholdint
2938abaaaf7SAxel Dörflerof_getprop(int package, const char *property, void *buffer, int bufferSize)
2948abaaaf7SAxel Dörfler{
2958abaaaf7SAxel Dörfler	struct {
2968abaaaf7SAxel Dörfler		const char	*name;
2978abaaaf7SAxel Dörfler		int			num_args;
2988abaaaf7SAxel Dörfler		int			num_returns;
2998abaaaf7SAxel Dörfler		int			package;
3008abaaaf7SAxel Dörfler		const char	*property;
3018abaaaf7SAxel Dörfler		void		*buffer;
3028abaaaf7SAxel Dörfler		int			buffer_size;
3038abaaaf7SAxel Dörfler		int			size;
3048abaaaf7SAxel Dörfler	} args = {"getprop", 4, 1, package, property, buffer, bufferSize, 0};
3058abaaaf7SAxel Dörfler
3068abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
3078abaaaf7SAxel Dörfler		return OF_FAILED;
3088abaaaf7SAxel Dörfler
3098abaaaf7SAxel Dörfler	return args.size;
3108abaaaf7SAxel Dörfler}
3118abaaaf7SAxel Dörfler
3128abaaaf7SAxel Dörfler
313bd185b41SIngo Weinholdint
3148abaaaf7SAxel Dörflerof_setprop(int package, const char *property, const void *buffer, int bufferSize)
3158abaaaf7SAxel Dörfler{
3168abaaaf7SAxel Dörfler	struct {
3178abaaaf7SAxel Dörfler		const char	*name;
3188abaaaf7SAxel Dörfler		int			num_args;
3198abaaaf7SAxel Dörfler		int			num_returns;
3208abaaaf7SAxel Dörfler		int			package;
3218abaaaf7SAxel Dörfler		const char	*property;
3228abaaaf7SAxel Dörfler		const void	*buffer;
3238abaaaf7SAxel Dörfler		int			buffer_size;
3248abaaaf7SAxel Dörfler		int			size;
3258abaaaf7SAxel Dörfler	} args = {"setprop", 4, 1, package, property, buffer, bufferSize, 0};
3268abaaaf7SAxel Dörfler
3278abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
3288abaaaf7SAxel Dörfler		return OF_FAILED;
3298abaaaf7SAxel Dörfler
3308abaaaf7SAxel Dörfler	return args.size;
3318abaaaf7SAxel Dörfler}
3328abaaaf7SAxel Dörfler
3338abaaaf7SAxel Dörfler
3348abaaaf7SAxel Dörflerint
3358abaaaf7SAxel Dörflerof_getproplen(int package, const char *property)
3368abaaaf7SAxel Dörfler{
3378abaaaf7SAxel Dörfler	struct {
3388abaaaf7SAxel Dörfler		const char	*name;
3398abaaaf7SAxel Dörfler		int			num_args;
3408abaaaf7SAxel Dörfler		int			num_returns;
3418abaaaf7SAxel Dörfler		int			package;
3428abaaaf7SAxel Dörfler		const char	*property;
3438abaaaf7SAxel Dörfler		int			size;
3448abaaaf7SAxel Dörfler	} args = {"getproplen", 2, 1, package, property, 0};
3458abaaaf7SAxel Dörfler
3468abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
3478abaaaf7SAxel Dörfler		return OF_FAILED;
3488abaaaf7SAxel Dörfler
3498abaaaf7SAxel Dörfler	return args.size;
3508abaaaf7SAxel Dörfler}
3518abaaaf7SAxel Dörfler
3528abaaaf7SAxel Dörfler
3538abaaaf7SAxel Dörflerint
3548abaaaf7SAxel Dörflerof_nextprop(int package, const char *previousProperty, char *nextProperty)
3558abaaaf7SAxel Dörfler{
3568abaaaf7SAxel Dörfler	struct {
3578abaaaf7SAxel Dörfler		const char	*name;
3588abaaaf7SAxel Dörfler		int			num_args;
3598abaaaf7SAxel Dörfler		int			num_returns;
3608abaaaf7SAxel Dörfler		int			package;
3618abaaaf7SAxel Dörfler		const char	*previous_property;
3628abaaaf7SAxel Dörfler		char		*next_property;
3638abaaaf7SAxel Dörfler		int			flag;
3648abaaaf7SAxel Dörfler	} args = {"nextprop", 3, 1, package, previousProperty, nextProperty, 0};
3658abaaaf7SAxel Dörfler
3668abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
3678abaaaf7SAxel Dörfler		return OF_FAILED;
3688abaaaf7SAxel Dörfler
3698abaaaf7SAxel Dörfler	return args.flag;
3708abaaaf7SAxel Dörfler}
3718abaaaf7SAxel Dörfler
3728abaaaf7SAxel Dörfler
3738abaaaf7SAxel Dörflerint
3748abaaaf7SAxel Dörflerof_package_to_path(int package, char *pathBuffer, int bufferSize)
3758abaaaf7SAxel Dörfler{
3768abaaaf7SAxel Dörfler	struct {
3778abaaaf7SAxel Dörfler		const char	*name;
3788abaaaf7SAxel Dörfler		int			num_args;
3798abaaaf7SAxel Dörfler		int			num_returns;
3808abaaaf7SAxel Dörfler		int			package;
3818abaaaf7SAxel Dörfler		char		*path_buffer;
3828abaaaf7SAxel Dörfler		int			buffer_size;
3838abaaaf7SAxel Dörfler		int			size;
3848abaaaf7SAxel Dörfler	} args = {"package-to-path", 3, 1, package, pathBuffer, bufferSize, 0};
3858abaaaf7SAxel Dörfler
3868abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
3878abaaaf7SAxel Dörfler		return OF_FAILED;
3888abaaaf7SAxel Dörfler
3898abaaaf7SAxel Dörfler	return args.size;
3908abaaaf7SAxel Dörfler}
3918abaaaf7SAxel Dörfler
3928abaaaf7SAxel Dörfler
3938abaaaf7SAxel Dörfler//	I/O functions
3948abaaaf7SAxel Dörfler
3958abaaaf7SAxel Dörfler
396bd185b41SIngo Weinholdint
3978abaaaf7SAxel Dörflerof_open(const char *nodeName)
3988abaaaf7SAxel Dörfler{
3998abaaaf7SAxel Dörfler	struct {
4008abaaaf7SAxel Dörfler		const char	*name;
4018abaaaf7SAxel Dörfler		int			num_args;
4028abaaaf7SAxel Dörfler		int			num_returns;
4038abaaaf7SAxel Dörfler		const char	*node_name;
4048abaaaf7SAxel Dörfler		int			handle;
4058abaaaf7SAxel Dörfler	} args = {"open", 1, 1, nodeName, 0};
4068abaaaf7SAxel Dörfler
4078abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED || args.handle == 0)
4088abaaaf7SAxel Dörfler		return OF_FAILED;
4098abaaaf7SAxel Dörfler
4108abaaaf7SAxel Dörfler	return args.handle;
4118abaaaf7SAxel Dörfler}
4128abaaaf7SAxel Dörfler
4138abaaaf7SAxel Dörfler
4148abaaaf7SAxel Dörflervoid
4158abaaaf7SAxel Dörflerof_close(int handle)
4168abaaaf7SAxel Dörfler{
4178abaaaf7SAxel Dörfler	struct {
4188abaaaf7SAxel Dörfler		const char	*name;
4198abaaaf7SAxel Dörfler		int			num_args;
4208abaaaf7SAxel Dörfler		int			num_returns;
4218abaaaf7SAxel Dörfler		int			handle;
4228abaaaf7SAxel Dörfler	} args = {"close", 1, 0, handle};
4238abaaaf7SAxel Dörfler
4248abaaaf7SAxel Dörfler	gCallOpenFirmware(&args);
4258abaaaf7SAxel Dörfler}
4268abaaaf7SAxel Dörfler
4278abaaaf7SAxel Dörfler
428bd185b41SIngo Weinholdint
4298abaaaf7SAxel Dörflerof_read(int handle, void *buffer, int bufferSize)
4308abaaaf7SAxel Dörfler{
4318abaaaf7SAxel Dörfler	struct {
4328abaaaf7SAxel Dörfler		const char	*name;
4338abaaaf7SAxel Dörfler		int			num_args;
4348abaaaf7SAxel Dörfler		int			num_returns;
4358abaaaf7SAxel Dörfler		int			handle;
4368abaaaf7SAxel Dörfler		void		*buffer;
4378abaaaf7SAxel Dörfler		int			buffer_size;
4388abaaaf7SAxel Dörfler		int			size;
4398abaaaf7SAxel Dörfler	} args = {"read", 3, 1, handle, buffer, bufferSize, 0};
4408abaaaf7SAxel Dörfler
4418abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
4428abaaaf7SAxel Dörfler		return OF_FAILED;
4438abaaaf7SAxel Dörfler
4448abaaaf7SAxel Dörfler	return args.size;
4458abaaaf7SAxel Dörfler}
4468abaaaf7SAxel Dörfler
4478abaaaf7SAxel Dörfler
448bd185b41SIngo Weinholdint
4498abaaaf7SAxel Dörflerof_write(int handle, const void *buffer, int bufferSize)
4508abaaaf7SAxel Dörfler{
4518abaaaf7SAxel Dörfler	struct {
4528abaaaf7SAxel Dörfler		const char	*name;
4538abaaaf7SAxel Dörfler		int			num_args;
4548abaaaf7SAxel Dörfler		int			num_returns;
4558abaaaf7SAxel Dörfler		int			handle;
4568abaaaf7SAxel Dörfler		const void	*buffer;
4578abaaaf7SAxel Dörfler		int			buffer_size;
4588abaaaf7SAxel Dörfler		int			size;
4598abaaaf7SAxel Dörfler	} args = {"write", 3, 1, handle, buffer, bufferSize, 0};
4608abaaaf7SAxel Dörfler
4618abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
4628abaaaf7SAxel Dörfler		return OF_FAILED;
4638abaaaf7SAxel Dörfler
4648abaaaf7SAxel Dörfler	return args.size;
4658abaaaf7SAxel Dörfler}
4668abaaaf7SAxel Dörfler
4678abaaaf7SAxel Dörfler
468bd185b41SIngo Weinholdint
469d0ff1a1fSPulkoMandyof_seek(int handle, off_t pos)
4708abaaaf7SAxel Dörfler{
4718abaaaf7SAxel Dörfler	struct {
4728abaaaf7SAxel Dörfler		const char	*name;
4738abaaaf7SAxel Dörfler		int			num_args;
4748abaaaf7SAxel Dörfler		int			num_returns;
4758abaaaf7SAxel Dörfler		int			handle;
4768abaaaf7SAxel Dörfler		int64		pos;
4778abaaaf7SAxel Dörfler		int			status;
4788abaaaf7SAxel Dörfler	} args = {"seek", 3, 1, handle, pos, 0};
4798abaaaf7SAxel Dörfler
4808abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
4818abaaaf7SAxel Dörfler		return OF_FAILED;
4828abaaaf7SAxel Dörfler
4838abaaaf7SAxel Dörfler	return args.status;
4848abaaaf7SAxel Dörfler}
4858abaaaf7SAxel Dörfler
4868abaaaf7SAxel Dörfler
4878abaaaf7SAxel Dörfler// memory functions
4888abaaaf7SAxel Dörfler
4898abaaaf7SAxel Dörfler
4908abaaaf7SAxel Dörflerint
491bd185b41SIngo Weinholdof_release(void *virtualAddress, int size)
4928abaaaf7SAxel Dörfler{
4938abaaaf7SAxel Dörfler	struct {
4948abaaaf7SAxel Dörfler		const char *name;
4958abaaaf7SAxel Dörfler		int			num_args;
4968abaaaf7SAxel Dörfler		int			num_returns;
497bd185b41SIngo Weinhold		void		*virtualAddress;
4988abaaaf7SAxel Dörfler		int			size;
499bd185b41SIngo Weinhold	} args = {"release", 2, 0, virtualAddress, size};
500bd185b41SIngo Weinhold
5018abaaaf7SAxel Dörfler	return gCallOpenFirmware(&args);
5028abaaaf7SAxel Dörfler}
5038abaaaf7SAxel Dörfler
5048abaaaf7SAxel Dörfler
5058abaaaf7SAxel Dörflervoid *
506bd185b41SIngo Weinholdof_claim(void *virtualAddress, int size, int align)
5078abaaaf7SAxel Dörfler{
5088abaaaf7SAxel Dörfler	struct {
5098abaaaf7SAxel Dörfler		const char	*name;
5108abaaaf7SAxel Dörfler		int			num_args;
5118abaaaf7SAxel Dörfler		int			num_returns;
512bd185b41SIngo Weinhold		void		*virtualAddress;
5138abaaaf7SAxel Dörfler		int			size;
5148abaaaf7SAxel Dörfler		int			align;
5158abaaaf7SAxel Dörfler		void		*address;
516bd185b41SIngo Weinhold	} args = {"claim", 3, 1, virtualAddress, size, align};
5178abaaaf7SAxel Dörfler
5188abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
5198abaaaf7SAxel Dörfler		return NULL;
5208abaaaf7SAxel Dörfler
5218abaaaf7SAxel Dörfler	return args.address;
5228abaaaf7SAxel Dörfler}
5238abaaaf7SAxel Dörfler
5248abaaaf7SAxel Dörfler
5258abaaaf7SAxel Dörfler// misc functions
5268abaaaf7SAxel Dörfler
5278abaaaf7SAxel Dörfler
5288abaaaf7SAxel Dörfler/** tests if the given service is missing
5298abaaaf7SAxel Dörfler */
5308abaaaf7SAxel Dörfler
5318abaaaf7SAxel Dörflerint
5328abaaaf7SAxel Dörflerof_test(const char *service)
5338abaaaf7SAxel Dörfler{
5348abaaaf7SAxel Dörfler	struct {
5358abaaaf7SAxel Dörfler		const char	*name;
5368abaaaf7SAxel Dörfler		int			num_args;
5378abaaaf7SAxel Dörfler		int			num_returns;
5388abaaaf7SAxel Dörfler		const char	*service;
5398abaaaf7SAxel Dörfler		int			missing;
5408abaaaf7SAxel Dörfler	} args = {"test", 1, 1, service, 0};
5418abaaaf7SAxel Dörfler
5428abaaaf7SAxel Dörfler	if (gCallOpenFirmware(&args) == OF_FAILED)
5438abaaaf7SAxel Dörfler		return OF_FAILED;
5448abaaaf7SAxel Dörfler
5458abaaaf7SAxel Dörfler	return args.missing;
5468abaaaf7SAxel Dörfler}
5478abaaaf7SAxel Dörfler
5488abaaaf7SAxel Dörfler
5498abaaaf7SAxel Dörfler/** Returns the millisecond counter
5508abaaaf7SAxel Dörfler */
5518abaaaf7SAxel Dörfler
5528abaaaf7SAxel Dörflerint
5538abaaaf7SAxel Dörflerof_milliseconds(void)
5548abaaaf7SAxel Dörfler{
5558abaaaf7SAxel Dörfler	struct {
5568abaaaf7SAxel Dörfler		const char	*name;
5578abaaaf7SAxel Dörfler		int			num_args;
5588abaaaf7SAxel Dörfler		int			num_returns;
5598abaaaf7SAxel Dörfler		int			milliseconds;
5608abaaaf7SAxel Dörfler	} args = {"milliseconds", 0, 1, 0};
5618abaaaf7SAxel Dörfler
562b62981b9SAndreas Färber	if (gCallOpenFirmware(&args) == OF_FAILED)
563b62981b9SAndreas Färber		return OF_FAILED;
5648abaaaf7SAxel Dörfler
5658abaaaf7SAxel Dörfler	return args.milliseconds;
5668abaaaf7SAxel Dörfler}
5678abaaaf7SAxel Dörfler
5688abaaaf7SAxel Dörfler
5698abaaaf7SAxel Dörflervoid
5708abaaaf7SAxel Dörflerof_exit(void)
5718abaaaf7SAxel Dörfler{
5728abaaaf7SAxel Dörfler	struct {
5738abaaaf7SAxel Dörfler		const char	*name;
5748abaaaf7SAxel Dörfler		int			num_args;
5758abaaaf7SAxel Dörfler		int			num_returns;
5768abaaaf7SAxel Dörfler	} args = {"exit", 0, 0};
5778abaaaf7SAxel Dörfler
5788abaaaf7SAxel Dörfler	gCallOpenFirmware(&args);
5798abaaaf7SAxel Dörfler}
5808abaaaf7SAxel Dörfler