1 /******************************************************************************
2  * Copyright (c) 2004, 2008 IBM Corporation
3  * All rights reserved.
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12 
13 #include <stdint.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <fileio.h>
17 #include <kernel.h>
18 #include <of.h>
19 #include <sys/socket.h>
20 
21 extern int vsprintf(char *, const char *, va_list);
22 extern void _exit(int status);
23 
24 void exit(int status);
25 
open(const char * name,int flags)26 int open(const char* name, int flags)
27 {
28 	int fd;
29 
30 	/* search free file descriptor */
31 	for (fd=0; fd<FILEIO_MAX; ++fd) {
32 		if(fd_array[fd].type == FILEIO_TYPE_EMPTY) {
33 			break;
34 		}
35 	}
36 	if (fd == FILEIO_MAX) {
37 		printf("Can not open \"%s\" because file descriptor list is full\n", name);
38 		/* there is no free file descriptor available */
39 		return -2;
40 	}
41 
42 	fd_array[fd].ih = of_open(name);
43 	if (fd_array[fd].ih == 0)
44 		return -1;
45 
46 	fd_array[fd].type = FILEIO_TYPE_FILE;
47 
48 	return fd;
49 }
50 
pre_open_ih(int fd,ihandle_t ih)51 int pre_open_ih(int fd, ihandle_t ih)
52 {
53 	if (fd_array[fd].type != FILEIO_TYPE_EMPTY)
54 		return -2;
55 	fd_array[fd].ih = ih;
56 	fd_array[fd].type = FILEIO_TYPE_FILE;
57 
58 	return fd;
59 }
60 
socket(int domain,int type,int proto,char * mac_addr)61 int socket(int domain, int type, int proto, char *mac_addr)
62 {
63 	uint8_t tmpbuf[8];
64 	int fd;
65 	phandle_t ph;
66 
67 	/* search free file descriptor */
68 	for (fd=0; fd<FILEIO_MAX; ++fd) {
69 		if(fd_array[fd].type == FILEIO_TYPE_EMPTY) {
70 			break;
71 		}
72 	}
73 	if (fd == FILEIO_MAX) {
74 		printf("Can not open socket, file descriptor list is full\n");
75 		/* there is no free file descriptor available */
76 		return -2;
77 	}
78 
79 	fd_array[fd].ih = of_interpret_1("my-parent", tmpbuf);
80 	if (fd_array[fd].ih == 0) {
81 		printf("Can not open socket, no parent instance\n");
82 		return -1;
83 	}
84 	ph = of_instance_to_package(fd_array[fd].ih);
85 	if (ph == -1) {
86 		printf("Can not open socket, no parent package\n");
87 		return -1;
88 	}
89 	if (of_get_mac(ph, mac_addr) < 0) {
90 		printf("Can not open socket, no MAC address\n");
91 		return -1;
92 	}
93 	fd_array[fd].type = FILEIO_TYPE_SOCKET;
94 
95 	return fd;
96 }
97 
close(int fd)98 int close(int fd)
99 {
100 	if (fd < 0 || fd >= FILEIO_MAX ||
101 	    fd_array[fd].type == FILEIO_TYPE_EMPTY)
102 		return -1;
103 	if (fd_array[fd].type == FILEIO_TYPE_FILE)
104 		of_close(fd_array[fd].ih);
105 	fd_array[fd].type = FILEIO_TYPE_EMPTY;
106 	return 0;
107 }
108 
read(int fd,void * buf,size_t len)109 ssize_t read(int fd, void *buf, size_t len)
110 {
111 	if (fd < 0 || fd >= FILEIO_MAX ||
112 	    fd_array[fd].type == FILEIO_TYPE_EMPTY)
113 		return -1;
114 
115 	return of_read(fd_array[fd].ih, buf, len);
116 }
117 
write(int fd,const void * buf,size_t len)118 ssize_t write (int fd, const void *buf, size_t len)
119 {
120 	char dest_buf[512];
121 	char *dest_buf_ptr;
122 	const char *dbuf = buf;
123 	int i;
124 
125 	if (fd == 1 || fd == 2) {
126 		dest_buf_ptr = &dest_buf[0];
127 		for (i = 0; i < len && i < 256; i++)
128 			{
129 				*dest_buf_ptr++ = *dbuf++;
130 				if (dbuf[-1] == '\n')
131 					*dest_buf_ptr++ = '\r';
132 			}
133 		len = dest_buf_ptr - &dest_buf[0];
134 		buf = &dest_buf[0];
135 	}
136 
137 	if(fd < 0 || fd >= FILEIO_MAX ||
138 	   fd_array[fd].type == FILEIO_TYPE_EMPTY)
139 		return -1;
140 
141 	return of_write(fd_array[fd].ih, (void *)buf, len);
142 }
143 
lseek(int fd,long offset,int whence)144 ssize_t lseek (int fd, long offset, int whence)
145 {
146 	return 0; // this syscall is unused !!!
147 #if 0
148     if (whence != 0)
149 	return -1;
150 
151     of_seek (fd_array[fd], (unsigned int) (offset>>32), (unsigned int) (offset & 0xffffffffULL));
152 
153     return offset;
154 #endif
155 }
156 
recv(int fd,void * packet,int packet_len,int flags)157 int recv(int fd, void *packet, int packet_len, int flags)
158 {
159 	return read(fd, packet, packet_len);
160 }
161 
send(int fd,const void * packet,int packet_len,int flags)162 int send(int fd, const void *packet, int packet_len, int flags)
163 {
164 	return write(fd, packet, packet_len);
165 }
166 
sendto(int fd,const void * packet,int packet_len,int flags,const void * sock_addr,int sock_addr_len)167 int sendto(int fd, const void *packet, int packet_len, int flags,
168 	   const void *sock_addr, int sock_addr_len)
169 {
170 	return send(fd, packet, packet_len, flags);
171 }
172 
exit(int status)173 void exit(int status)
174 {
175 	_exit(status);
176 }
177