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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
#include <u.h>
#include "fns.h"
#include "efi.h"
typedef UINT16 EFI_PXE_BASE_CODE_UDP_PORT;
typedef struct {
UINT8 Addr[4];
} EFI_IPv4_ADDRESS;
typedef struct {
UINT8 Addr[16];
} EFI_IPv6_ADDRESS;
typedef union {
UINT32 Addr[4];
EFI_IPv4_ADDRESS v4;
EFI_IPv6_ADDRESS v6;
} EFI_IP_ADDRESS;
typedef struct {
UINT8 Addr[32];
} EFI_MAC_ADDRESS;
typedef struct {
UINT8 BootpOpcode;
UINT8 BootpHwType;
UINT8 BootpHwAddrLen;
UINT8 BootpGateHops;
UINT32 BootpIdent;
UINT16 BootpSeconds;
UINT16 BootpFlags;
UINT8 BootpCiAddr[4];
UINT8 BootpYiAddr[4];
UINT8 BootpSiAddr[4];
UINT8 BootpGiAddr[4];
UINT8 BootpHwAddr[16];
UINT8 BootpSrvName[64];
UINT8 BootpBootFile[128];
UINT32 DhcpMagik;
UINT8 DhcpOptions[56];
} EFI_PXE_BASE_CODE_DHCPV4_PACKET;
typedef struct {
BOOLEAN Started;
BOOLEAN Ipv6Available;
BOOLEAN Ipv6Supported;
BOOLEAN UsingIpv6;
BOOLEAN BisSupported;
BOOLEAN BisDetected;
BOOLEAN AutoArp;
BOOLEAN SendGUID;
BOOLEAN DhcpDiscoverValid;
BOOLEAN DhcpAckReceived;
BOOLEAN ProxyOfferReceived;
BOOLEAN PxeDiscoverValid;
BOOLEAN PxeReplyReceived;
BOOLEAN PxeBisReplyReceived;
BOOLEAN IcmpErrorReceived;
BOOLEAN TftpErrorReceived;
BOOLEAN MakeCallbacks;
UINT8 TTL;
UINT8 ToS;
UINT8 Reserved;
UINT8 StationIp[16];
UINT8 SubnetMask[16];
UINT8 DhcpDiscover[1472];
UINT8 DhcpAck[1472];
UINT8 ProxyOffer[1472];
UINT8 PxeDiscover[1472];
UINT8 PxeReply[1472];
UINT8 PxeBisReply[1472];
} EFI_PXE_BASE_CODE_MODE;
typedef struct {
UINT64 Revision;
void *Start;
void *Stop;
void *Dhcp;
void *Discover;
void *Mtftp;
void *UdpWrite;
void *UdpRead;
void *SetIpFilter;
void *Arp;
void *SetParameters;
void *SetStationIp;
void *SetPackets;
EFI_PXE_BASE_CODE_MODE *Mode;
} EFI_PXE_BASE_CODE_PROTOCOL;
enum {
Tftp_READ = 1,
Tftp_WRITE = 2,
Tftp_DATA = 3,
Tftp_ACK = 4,
Tftp_ERROR = 5,
Tftp_OACK = 6,
TftpPort = 69,
Segsize = 512,
};
static
EFI_GUID EFI_PXE_BASE_CODE_PROTOCOL_GUID = {
0x03C4E603, 0xAC28, 0x11D3,
0x9A, 0x2D, 0x00, 0x90,
0x27, 0x3F, 0xC1, 0x4D,
};
static
EFI_PXE_BASE_CODE_PROTOCOL *pxe;
static
EFI_PXE_BASE_CODE_DHCPV4_PACKET *dhcp;
typedef struct Tftp Tftp;
struct Tftp
{
EFI_IP_ADDRESS sip;
EFI_IP_ADDRESS dip;
EFI_PXE_BASE_CODE_UDP_PORT sport;
EFI_PXE_BASE_CODE_UDP_PORT dport;
char *rp;
char *ep;
int seq;
int eof;
char pkt[2+2+Segsize];
char nul;
};
static void
puts(void *x, ushort v)
{
uchar *p = x;
p[1] = (v>>8) & 0xFF;
p[0] = v & 0xFF;
}
static ushort
gets(void *x)
{
uchar *p = x;
return p[1]<<8 | p[0];
}
static void
hnputs(void *x, ushort v)
{
uchar *p = x;
p[0] = (v>>8) & 0xFF;
p[1] = v & 0xFF;
}
static ushort
nhgets(void *x)
{
uchar *p = x;
return p[0]<<8 | p[1];
}
enum {
ANY_SRC_IP = 0x0001,
ANY_SRC_PORT = 0x0002,
ANY_DEST_IP = 0x0004,
ANY_DEST_PORT = 0x0008,
USE_FILTER = 0x0010,
MAY_FRAGMENT = 0x0020,
};
static int
udpread(EFI_IP_ADDRESS *sip, EFI_IP_ADDRESS *dip,
EFI_PXE_BASE_CODE_UDP_PORT *sport,
EFI_PXE_BASE_CODE_UDP_PORT dport,
int *len, void *data)
{
UINTN size;
size = *len;
if(eficall(pxe->UdpRead, pxe, (UINTN)ANY_SRC_PORT, dip, &dport, sip, sport, nil, nil, &size, data))
return -1;
*len = size;
return 0;
}
static int
udpwrite(EFI_IP_ADDRESS *dip,
EFI_PXE_BASE_CODE_UDP_PORT sport,
EFI_PXE_BASE_CODE_UDP_PORT dport,
int len, void *data)
{
UINTN size;
size = len;
if(eficall(pxe->UdpWrite, pxe, (UINTN)MAY_FRAGMENT, dip, &dport, nil, nil, &sport, nil, nil, &size, data))
return -1;
return 0;
}
static int
pxeread(void *f, void *data, int len)
{
Tftp *t = f;
int seq, n;
while(!t->eof && t->rp >= t->ep){
for(;;){
n = sizeof(t->pkt);
if(udpread(&t->dip, &t->sip, &t->dport, t->sport, &n, t->pkt))
continue;
if(n >= 4)
break;
}
switch(nhgets(t->pkt)){
case Tftp_DATA:
seq = nhgets(t->pkt+2);
if(seq > t->seq){
putc('?');
continue;
}
hnputs(t->pkt, Tftp_ACK);
while(udpwrite(&t->dip, t->sport, t->dport, 4, t->pkt))
putc('!');
if(seq < t->seq){
putc('@');
continue;
}
t->seq = seq+1;
n -= 4;
t->rp = t->pkt + 4;
t->ep = t->rp + n;
t->eof = n < Segsize;
break;
case Tftp_ERROR:
print(t->pkt+4);
print("\n");
default:
t->eof = 1;
return -1;
}
break;
}
n = t->ep - t->rp;
if(len > n)
len = n;
memmove(data, t->rp, len);
t->rp += len;
return len;
}
static void
pxeclose(void *f)
{
Tftp *t = f;
t->eof = 1;
}
static int
tftpopen(Tftp *t, char *path)
{
static EFI_PXE_BASE_CODE_UDP_PORT xport = 6666;
int r, n;
char *p;
t->sport = xport++;
t->dport = 0;
t->rp = t->ep = 0;
t->seq = 1;
t->eof = 0;
t->nul = 0;
p = t->pkt;
hnputs(p, Tftp_READ); p += 2;
n = strlen(path)+1;
memmove(p, path, n); p += n;
memmove(p, "octet", 6); p += 6;
n = p - t->pkt;
for(;;){
if(r = udpwrite(&t->dip, t->sport, TftpPort, n, t->pkt))
break;
if(r = pxeread(t, 0, 0))
break;
return 0;
}
pxeclose(t);
return r;
}
static void*
pxeopen(char *name)
{
static uchar buf[sizeof(Tftp)+8];
Tftp *t = (Tftp*)((uintptr)(buf+7)&~7);
memset(t, 0, sizeof(Tftp));
memmove(&t->dip, dhcp->BootpSiAddr, 4);
memmove(&t->sip, pxe->Mode->StationIp, 4);
if(tftpopen(t, name))
return nil;
return t;
}
int
pxeinit(void **pf)
{
EFI_PXE_BASE_CODE_MODE *mode;
EFI_HANDLE *Handles;
UINTN Count;
int i;
pxe = nil;
dhcp = nil;
Count = 0;
Handles = nil;
if(eficall(ST->BootServices->LocateHandleBuffer,
ByProtocol, &EFI_PXE_BASE_CODE_PROTOCOL_GUID, nil, &Count, &Handles))
return -1;
for(i=0; i<Count; i++){
pxe = nil;
if(eficall(ST->BootServices->HandleProtocol,
Handles[i], &EFI_PXE_BASE_CODE_PROTOCOL_GUID, &pxe))
continue;
mode = pxe->Mode;
if(mode == nil || mode->UsingIpv6 || mode->Started == 0)
continue;
if(mode->DhcpAckReceived){
dhcp = (EFI_PXE_BASE_CODE_DHCPV4_PACKET*)mode->DhcpAck;
goto Found;
}
if(mode->PxeReplyReceived){
dhcp = (EFI_PXE_BASE_CODE_DHCPV4_PACKET*)mode->PxeReply;
goto Found;
}
}
return -1;
Found:
open = pxeopen;
read = pxeread;
close = pxeclose;
if(pf != nil){
char ini[24];
uchar *mac;
mac = dhcp->BootpHwAddr;
memmove(ini, "/cfg/pxe/", 9);
for(i=0; i<6; i++){
ini[9+i*2+0] = hex[mac[i] >> 4];
ini[9+i*2+1] = hex[mac[i] & 0xF];
}
ini[9+12] = '\0';
if((*pf = pxeopen(ini)) == nil)
*pf = pxeopen("/cfg/pxe/default");
}
return 0;
}
|