Added boot process information to help someone find out what really happens.
[bootos.git] / stage2 / network.c
blobf58ca17a30423137d31ed30bc33f8bd80dda6cfe
1 /* network.c - Toplevel networking functions
3 Copyright (C) 2010-2011 Hector Martin "marcan" <hector@marcansoft.com>
5 This code is licensed to you under the terms of the GNU GPL, version 2;
6 see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
7 */
9 #include "types.h"
10 #include "debug.h"
11 #include "string.h"
12 #include "time.h"
14 #include "lwip/init.h"
15 #include "lwip/dhcp.h"
16 #include "lwip/tcp.h"
18 #include "gelic_netif.h"
19 #include "netif/etharp.h"
21 struct netif eth;
22 static struct ip_addr ipaddr, netmask, gw;
24 #if LWIP_TCP
25 static u64 last_tcp_time;
26 #endif
27 static u64 last_arp_time;
28 static u64 last_dhcp_coarse_time;
29 static u64 last_dhcp_fine_time;
31 void net_init(void) {
32 memset(&ipaddr, 0, sizeof(ipaddr));
33 memset(&netmask, 0, sizeof(ipaddr));
34 memset(&gw, 0, sizeof(ipaddr));
36 printf("Initializing network...\n");
37 lwip_init();
38 printf("lwIP Initialized\n");
39 netif_add(&eth, &ipaddr, &netmask, &gw, NULL, gelicif_init, ethernet_input);
40 netif_set_default(&eth);
41 netif_set_up(&eth);
42 printf("Ethernet interface initialized\n");
43 printf("Starting DHCP\n");
44 dhcp_start(&eth);
46 u64 now = gettb();
47 #if LWIP_TCP
48 last_tcp_time = now;
49 #endif
50 last_arp_time = last_dhcp_coarse_time = last_dhcp_fine_time = now;
53 void net_poll(void) {
55 u64 now = gettb();
57 gelicif_input(&eth);
58 if ((now - last_arp_time) >= (ARP_TMR_INTERVAL*TICKS_PER_MS)) {
59 etharp_tmr();
60 last_arp_time = now;
62 #if LWIP_TCP
63 if ((now - last_tcp_time) >= (TCP_TMR_INTERVAL*TICKS_PER_MS)) {
64 tcp_tmr();
65 last_tcp_time = now;
67 #endif
68 if ((now - last_dhcp_coarse_time) >= (DHCP_COARSE_TIMER_SECS*TICKS_PER_SEC)) {
69 dhcp_coarse_tmr();
70 last_dhcp_coarse_time = now;
72 if ((now - last_dhcp_fine_time) >= (DHCP_FINE_TIMER_MSECS*TICKS_PER_MS)) {
73 dhcp_fine_tmr();
74 last_dhcp_fine_time = now;
78 void net_shutdown(void) {
79 printf("Releasing DHCP lease...\n");
80 dhcp_release(&eth);
81 dhcp_stop(&eth);
82 printf("Shutting down network...\n");
83 netif_remove(&eth);
84 gelicif_shutdown(&eth);