forked from alanc98/rki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtems_net.c
272 lines (226 loc) · 6.72 KB
/
rtems_net.c
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
/****************************************************************
** rtems_net.c
**
** Author: Alan Cudmore
**
** This module is responsible for RTEMS specific network initialization.
**
**
*/
#include "rki_config.h"
/*
** If the Network subsystem is not selected, just ifdef the whole thing out
*/
#ifdef RKI_INCLUDE_NETWORK
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <rtems/rtems_bsdnet.h>
#include <sys/domain.h>
#include <sys/mbuf.h>
#include <sys/socketvar.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/callout.h>
#include <sys/proc.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <vm/vm.h>
#include <arpa/inet.h>
#include <net/netisr.h>
#include <net/route.h>
#include <rtems/libio.h>
/*
** include network configuration
*/
#include "rtems_net_config.h"
/*
** Declare the network configuration structure
*/
struct rtems_bsdnet_config rtems_bsdnet_config;
/*
** Loopback interface config
*/
struct rtems_bsdnet_ifconfig loopback_config __attribute__((aligned(16)));
extern int rtems_bsdnet_loopattach(void * dummy);
/*
** ethernet interface config
*/
struct rtems_bsdnet_ifconfig if1_config __attribute__((aligned(16)));
extern int RTEMS_BSP_NETWORK_DRIVER_ATTACH (struct rtems_bsdnet_ifconfig *, int);
/*
** Ethernet address
*/
static char ethernet_address[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
/*
** Convert an ethernet address string
** from a form of "00:00:00:00:00:00" to a binary byte array.
*/
int rtems_convert_ethernet_addr( char ethernet_address[], char *AddressString )
{
int result;
int i;
unsigned int temp_eth_address[6];
printf("Convert ethernet address\n");
/*
** Do some basic format checking
*/
if ( strlen(AddressString) != 17 )
{
printf("Error: Invalid MAC Address: %s\n",AddressString);
return(-1);
}
if ( AddressString[2] != ':' || AddressString[5] != ':' ||
AddressString[8] != ':' || AddressString[11] != ':' ||
AddressString[14] != ':' )
{
printf("Error: Invalid MAC Address: %s\n",AddressString);
return(-1);
}
printf("Converting: %s\n",AddressString);
result = sscanf(AddressString, "%2x:%2x:%2x:%2x:%2x:%2x",
&temp_eth_address[0],
&temp_eth_address[1],
&temp_eth_address[2],
&temp_eth_address[3],
&temp_eth_address[4],
&temp_eth_address[5]);
if ( result < 6 )
{
printf("Error: Invalid MAC Address: %s\n",AddressString);
return(-1);
}
/*
** fill byte array
*/
for ( i = 0; i < 6; i++ )
{
ethernet_address[i] = temp_eth_address[i] & 0xFF;
}
return 0;
}
/*
** rtems_init_network
** Initialize the network subsystem and devices for RTEMS
**
*/
int rtems_init_network(char *EthernetAddress,
char *IPAddress,
char *HostName,
char *NetMask,
char *GateWay,
char *NameServer )
{
int status;
struct rtems_bsdnet_ifconfig *if1;
printf("Starting RTEMS network configuration\n");
/*
** Clear out the 3 config structures
*/
memset(&rtems_bsdnet_config,0,sizeof(struct rtems_bsdnet_config));
memset(&loopback_config, 0, sizeof(struct rtems_bsdnet_ifconfig));
memset(&if1_config, 0, sizeof(struct rtems_bsdnet_ifconfig));
/*
** Fill out the network config structure
*/
rtems_bsdnet_config.network_task_priority = 1;
rtems_bsdnet_config.mbuf_bytecount = 64*1024; /* Was 128K */
rtems_bsdnet_config.mbuf_cluster_bytecount = 256*1024;
rtems_bsdnet_config.hostname = HostName;
rtems_bsdnet_config.domainname = RTEMS_NET_DOMAINNAME;
rtems_bsdnet_config.gateway = GateWay;
rtems_bsdnet_config.name_server[0]= NameServer;
rtems_bsdnet_config.ifconfig = &loopback_config;
/*
** Configure the loopback device structure
*/
loopback_config.name = "lo0";
loopback_config.attach = (int (*)(struct rtems_bsdnet_ifconfig *, int))rtems_bsdnet_loopattach;
loopback_config.ip_address= "127.0.0.1";
loopback_config.ip_netmask= "255.0.0.0";
loopback_config.next = &if1_config;
/*
** Configure the ethernet device structure
*/
if1_config.name = RTEMS_BSP_NETWORK_DRIVER_NAME;
if1_config.attach = RTEMS_BSP_NETWORK_DRIVER_ATTACH;
if1_config.ip_address= IPAddress;
if1_config.ip_netmask= NetMask;
if1_config.rbuf_count = 32;
if1_config.xbuf_count = 32;
if1_config.next = NULL;
printf("RTEMS network config: All structures filled out, now calling init functions\n");
#if (defined (RTEMS_SET_ETHERNET_ADDRESS))
/*
** Convert the ethernet address string to the byte array
*/
status = rtems_convert_ethernet_addr(ethernet_address, EthernetAddress );
if ( status == 0 )
{
if1_config.hardware_address = ethernet_address;
}
#endif
/*
** Initialize the network
*/
printf("Calling rtems_bsdnet_initialize_network\n");
if( rtems_bsdnet_initialize_network() == -1 )
{
printf("RTEMS Network Initialization failed\n");
return(1);
}
else
{
printf("RTEMS bsdnet_initialize_network returned OK\n");
}
printf("Hostname is %s.%s\n", rtems_bsdnet_config.hostname, rtems_bsdnet_config.domainname );
/*
** Loop through the available devices and print the stats
*/
for( if1 = &loopback_config; if1; if1 = if1->next )
{
printf(" Device %s, netmask %s, address %s\n", if1->name, if1->ip_netmask, if1->ip_address );
}
/*
** Printout the gateway and DNS server(s)
*/
printf(" gateway %s, dns1 %s, dns2 %s\n\n\n", rtems_bsdnet_config.gateway,
rtems_bsdnet_config.name_server[0],
rtems_bsdnet_config.name_server[1] );
return(0);
}
/*
** rtems_auto_init_network
** This function is called from the RTEMS Init function if
** the user wishes to setup a static network address without
** using the shell configuration.
*/
int rtems_auto_init_network(void)
{
int status;
status = rtems_init_network(RTEMS_ETHERNET_ADDRESS,
RTEMS_NET_IP_ADDRESS,
RTEMS_NET_HOSTNAME,
RTEMS_NET_IP_NETMASK,
RTEMS_NET_GATEWAY,
RTEMS_NET_NAME_SERVER1);
return(status);
}
#else
/*
** no network config!
*/
int rtems_init_network( void )
{
return(0);
}
#endif