forked from niftich/gigaboot20x6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetboot.c
197 lines (174 loc) · 4.94 KB
/
netboot.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
// Copyright 2016 The Fuchsia Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <string.h>
#include <inet6.h>
#include <netboot.h>
#include <netifc.h>
static uint32_t last_cookie = 0;
static uint32_t last_cmd = 0;
static uint32_t last_arg = 0;
static uint32_t last_ack_cmd = 0;
static uint32_t last_ack_arg = 0;
static int nb_boot_now = 0;
static int nb_active = 0;
// item being downloaded
static nbfile* item;
void udp6_recv(void* data, size_t len,
const ip6_addr* daddr, uint16_t dport,
const ip6_addr* saddr, uint16_t sport) {
nbmsg* msg = data;
nbmsg ack;
if (dport != NB_SERVER_PORT)
return;
if (len < sizeof(nbmsg))
return;
len -= sizeof(nbmsg);
//printf("netboot: MSG %08x %08x %08x %08x datalen %d\n",
// msg->magic, msg->cookie, msg->cmd, msg->arg, len);
if ((last_cookie == msg->cookie) &&
(last_cmd == msg->cmd) && (last_arg = msg->arg)) {
// host must have missed the ack. resend
ack.magic = NB_MAGIC;
ack.cookie = last_cookie;
ack.cmd = last_ack_cmd;
ack.arg = last_ack_arg;
goto transmit;
}
ack.cmd = NB_ACK;
ack.arg = 0;
switch (msg->cmd) {
case NB_COMMAND:
if (len == 0)
return;
msg->data[len - 1] = 0;
break;
case NB_SEND_FILE:
if (len == 0)
return;
msg->data[len - 1] = 0;
for (int i = 0; i < (len - 1); i++) {
if ((msg->data[i] < ' ') || (msg->data[i] > 127)) {
msg->data[i] = '.';
}
}
item = netboot_get_buffer((const char*) msg->data);
if (item) {
item->offset = 0;
printf("netboot: Receive File '%s'...\n", (char*) msg->data);
} else {
printf("netboot: Rejected File '%s'...\n", (char*) msg->data);
ack.cmd = NB_ERROR_BAD_FILE;
}
break;
case NB_DATA:
if (item == 0)
return;
if (msg->arg != item->offset)
return;
ack.arg = msg->arg;
if ((item->offset + len) > item->size) {
ack.cmd = NB_ERROR_TOO_LARGE;
} else {
memcpy(item->data + item->offset, msg->data, len);
item->offset += len;
ack.cmd = NB_ACK;
}
break;
case NB_BOOT:
nb_boot_now = 1;
printf("netboot: Boot Kernel...\n");
break;
default:
ack.cmd = NB_ERROR_BAD_CMD;
ack.arg = 0;
}
last_cookie = msg->cookie;
last_cmd = msg->cmd;
last_arg = msg->arg;
last_ack_cmd = ack.cmd;
last_ack_arg = ack.arg;
ack.cookie = msg->cookie;
ack.magic = NB_MAGIC;
transmit:
nb_active = 1;
udp6_send(&ack, sizeof(ack), saddr, sport, NB_SERVER_PORT);
}
static char advertise_data[] =
"version\00.1\0"
"serialno\0unknown\0"
"board\0unknown\0";
static void advertise(void) {
uint8_t buffer[256];
nbmsg* msg = (void*)buffer;
msg->magic = NB_MAGIC;
msg->cookie = 0;
msg->cmd = NB_ADVERTISE;
msg->arg = 0;
memcpy(msg->data, advertise_data, sizeof(advertise_data));
udp6_send(buffer, sizeof(nbmsg) + sizeof(advertise_data),
&ip6_ll_all_nodes, NB_ADVERT_PORT, NB_SERVER_PORT);
}
#define FAST_TICK 100
#define SLOW_TICK 1000
int netboot_init(void) {
if (netifc_open()) {
printf("netboot: Failed to open network interface\n");
return -1;
}
return 0;
}
static int nb_fastcount = 0;
static int nb_online = 0;
int netboot_poll(void) {
if (netifc_active()) {
if (nb_online == 0) {
printf("netboot: interface online\n");
nb_online = 1;
nb_fastcount = 20;
netifc_set_timer(FAST_TICK);
advertise();
}
} else {
if (nb_online == 1) {
printf("netboot: interface offline\n");
nb_online = 0;
}
return 0;
}
if (netifc_timer_expired()) {
if (nb_fastcount) {
nb_fastcount--;
netifc_set_timer(FAST_TICK);
} else {
netifc_set_timer(SLOW_TICK);
}
if (nb_active) {
// don't advertise if we're in a transfer
nb_active = 0;
} else {
advertise();
}
}
netifc_poll();
if (nb_boot_now) {
nb_boot_now = 0;
return 1;
} else {
return 0;
}
}
void netboot_close(void) {
netifc_close();
}