summaryrefslogtreecommitdiff
path: root/roms/SLOF/clients/net-snk/kernel/systemcall.c
blob: 644f459da0bf45e47da014522c2ab421bb5752e3 (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
/******************************************************************************
 * Copyright (c) 2004, 2008 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <of.h>
#include <systemcall.h>
#include <netdriver_int.h>
#include <fileio.h>
#include "modules.h"

extern int vsprintf(char *, const char *, va_list);
extern void _exit(int status);

long _system_call(long arg0, long arg1, long arg2, long arg3, 
		  long arg4, long arg5, long arg6, int nr);
void exit(int status);
int printk(const char*, ...);

static int
_syscall_open(const char* name, int flags)
{
	int fd, i;

	/* search free file descriptor */
	for(fd=0; fd<FILEIO_MAX; ++fd) {
		if(fd_array[fd].type == FILEIO_TYPE_EMPTY) {
			break;
		}
	}
	if(fd == FILEIO_MAX) {
		printk ("Can not open \"%s\" because file descriptor list is full\n", name);
		/* there is no free file descriptor available */
		return -2;
	}

	for(i=0; i<MODULES_MAX; ++i) {
		if(!snk_modules[i] || !snk_modules[i]->open) {
			continue;
		}

		if(snk_modules[i]->running == 0) {
			snk_modules[i]->init();
		}

		if(snk_modules[i]->open(&fd_array[fd], name, flags) == 0)
			break;
	}

	if(i==MODULES_MAX) {
		/* file not found */
		return -1;
	}

	return fd;
}

static int
_syscall_socket(int domain, int type, int proto, char *mac_addr)
{
	snk_module_t *net_module;

	net_module = get_module_by_type(MOD_TYPE_NETWORK);
	if( !net_module ||  !net_module->init) {
		printk("No net_init function available");
		return -1;
	}

	/* Init net device driver */
	if(net_module->running == 0) {
		net_module->init();
	}

	if(net_module->running == 0)
		return -2;

	memcpy(mac_addr, &net_module->mac_addr[0], 6);
	return 0;
}

static int
_syscall_close(int fd)
{
	if(fd < 0 || fd >= FILEIO_MAX
	|| fd_array[fd].type == FILEIO_TYPE_EMPTY
	|| fd_array[fd].close == 0)
		return -1;

	return fd_array[fd].close(&fd_array[fd]);
}

static long
_syscall_read (int fd, char *buf, long len)
{
	if(fd < 0 || fd >= FILEIO_MAX
	|| fd_array[fd].type == FILEIO_TYPE_EMPTY
	|| fd_array[fd].read == 0)
		return -1;

	return fd_array[fd].read(&fd_array[fd], buf, len);
}

static long
_syscall_write (int fd, char *buf, long len)
{
    char dest_buf[512];
    char *dest_buf_ptr;
    int i;
    if (fd == 1 || fd == 2)
    {
	dest_buf_ptr = &dest_buf[0];
        for (i = 0; i < len && i < 256; i++)
        {
            *dest_buf_ptr++ = *buf++;
            if (buf[-1] == '\n')
                *dest_buf_ptr++ = '\r';
	}
	len = dest_buf_ptr - &dest_buf[0];
	buf = &dest_buf[0];
    }

	if(fd < 0 || fd >= FILEIO_MAX
	|| fd_array[fd].type == FILEIO_TYPE_EMPTY
	|| fd_array[fd].write == 0)
		return -1;

	return fd_array[fd].write(&fd_array[fd], buf, len);
}

static long
_syscall_lseek (int fd, long offset, int whence)
{
	return 0; // this syscall is unused !!!
#if 0
    if (whence != 0)
	return -1;

    of_seek (fd_array[fd], (unsigned int) (offset>>32), (unsigned int) (offset & 0xffffffffULL));

    return offset;
#endif
}

static int
_syscall_ioctl (int fd, int request, void* data)
{
	if (fd < 0
	 || fd >= FILEIO_MAX
	 || fd_array[fd].type == FILEIO_TYPE_EMPTY)
		return -1;
	if (!fd_array[fd].ioctl) { /* for backwards compatibility with network modules */
		snk_module_t *net_module;

		net_module = get_module_by_type(MOD_TYPE_NETWORK);
		if ( !net_module || !net_module->ioctl ) {
			printk("No net_ioctl function available");
			return -1;
		}

		return net_module->ioctl(request, data);
	}

	return fd_array[fd].ioctl(&fd_array[fd], request, data);
}

static long
_syscall_recv(int fd, void *packet, int packet_len, int flags)
{
	snk_module_t *net_module;

	net_module = get_module_by_type(MOD_TYPE_NETWORK);
	if( !net_module || !net_module->read ) {
		printk("No net_receive function available");
		return -1;
	}

	return net_module->read(packet, packet_len);
}

static long
_syscall_send(int fd, void *packet, int packet_len, int flags)
{
	snk_module_t *net_module;

	net_module = get_module_by_type(MOD_TYPE_NETWORK);
	if( !net_module || !net_module->write ) {
		printk("No net_xmit function available");
		return -1;
	}

	return net_module->write(packet, packet_len);
}

static long
_syscall_sendto(int fd, void *packet, int packet_len, int flags,
	void *sock_addr, int sock_addr_len)
{
	return _syscall_send(fd, packet, packet_len, flags);
}








long
_system_call(long arg0, long arg1, long arg2, long arg3, 
	     long arg4, long arg5, long arg6, int nr)
{
	long rc = -1;

	switch (nr)
	{
	case _open_sc_nr:
		rc = _syscall_open ((void *) arg0, arg1);
		break;
	case _read_sc_nr:
		rc = _syscall_read (arg0, (void *) arg1, arg2);
		break;
	case _close_sc_nr:
		_syscall_close (arg0);
		break;
	case _lseek_sc_nr:
		rc = _syscall_lseek (arg0, arg1, arg2);
		break;
	case _write_sc_nr:
		rc = _syscall_write (arg0, (void *) arg1, arg2);
		break;
	case _ioctl_sc_nr:
		rc = _syscall_ioctl (arg0, arg1, (void *) arg2);
		break;
	case _socket_sc_nr:
		switch (arg0)
		{
		case _sock_sc_nr:
			rc = _syscall_socket (arg1, arg2, arg3, (char*) arg4);
			break;
		case _recv_sc_nr:
			rc = _syscall_recv (arg1, (void *) arg2, arg3, arg4);
			break;
		case _send_sc_nr:
			rc = _syscall_send (arg1, (void *) arg2, arg3, arg4);
			break;
		case _sendto_sc_nr:
			rc = _syscall_sendto (arg1, (void *) arg2, arg3, arg4, (void *) arg5, arg6);
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}

	return rc;
}

void 
exit(int status)
{
	_exit(status);
}

int
printk(const char* fmt, ...)
{
	int count;
	va_list ap;
	char buffer[256];
	va_start (ap, fmt);
	count=vsprintf(buffer, fmt, ap);
	_syscall_write (1, buffer, count);
	va_end (ap);
	return count;
}