-
Notifications
You must be signed in to change notification settings - Fork 2
/
tftptest.c
225 lines (191 loc) · 9.06 KB
/
tftptest.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
/*
* tftptest.efi
* EFI application to test an UEFI firmware's EFI_PXE_BASE_CODE_TFTP_READ_FILE function
*
* Copyright (c) 2020 by Pieter Hollants <[email protected]>
* Licensed under the GNU Public License (GPL) version 3
*/
#include <efi.h>
#include <efilib.h>
EFI_SYSTEM_TABLE *systab;
#define BOOTP_BOOTFILE_NAME_MAXLEN 256
#define DOWNLOADBUF_SIZE 65536
#define DEFAULT_BLOCKSIZE 1024
VOID
str2U(IN CHAR8 *src, OUT CHAR16 *dst, IN UINTN size)
{
while (size && size-- && (*dst++ = *src++) != '\0');
*dst = (CHAR16)0;
}
static VOID
wait_keypress(VOID)
{
EFI_INPUT_KEY key;
EFI_STATUS status;
Print(L"\nPress any key...");
while (uefi_call_wrapper(systab->ConIn->ReadKeyStroke, 2, systab->ConIn, &key) == EFI_SUCCESS);
while ((status=uefi_call_wrapper(systab->ConIn->ReadKeyStroke,2, systab->ConIn, &key)) == EFI_NOT_READY);
Print(L"\n");
}
EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *system_tab) {
EFI_STATUS status;
EFI_LOADED_IMAGE *info;
EFI_PXE_BASE_CODE *pxe;
CHAR16 bootp_bootfile_name[BOOTP_BOOTFILE_NAME_MAXLEN];
CHAR8 downloadbuf[DOWNLOADBUF_SIZE];
UINT64 downloadbuf_len = DOWNLOADBUF_SIZE;
UINTN blocksize = DEFAULT_BLOCKSIZE;
/* UEFI initialization */
systab = system_tab;
InitializeLib(image, system_tab);
/* Clear screen and write banner */
uefi_call_wrapper(systab->ConOut->Reset, 2, systab->ConOut, FALSE);
Print(L"\ntftptest.efi by Pieter Hollants <[email protected]>\n");
Print(L"EFI application to test the firmware's EFI_PXE_BASE_CODE_TFTP_READ_FILE function\n");
Print(L"[System Table] Firmware vendor: %s Revision: %d\n", systab->FirmwareVendor, systab->FirmwareRevision);
/* Determine device we were booted from */
status = uefi_call_wrapper(
BS->HandleProtocol, // BootServices function HandleProtocol
3, // 3 arguments following
image, // IN EFI_HANDLE *Handle
&LoadedImageProtocol, // IN EFI_GUID *Protocol
(VOID **)&info // OUT VOID **Interface
);
if (EFI_ERROR(status)) {
Print(L"\nHandleProtocol EFI_LOADED_IMAGE_PROTOCOL failed!\n");
wait_keypress();
return EFI_LOAD_ERROR;
}
/* Get PXE/DHCP information for this device */
status = uefi_call_wrapper(
BS->HandleProtocol, // BootServices function HandleProtocol
3, // 3 arguments following
info->DeviceHandle, // IN EFI_HANDLE *Handle
&PxeBaseCodeProtocol, // IN EFI_GUID *Protocol
(VOID **)&pxe // OUT VOID **Interface
);
if (EFI_ERROR(status)) {
Print(L"\nHandleProtocol EFI_PXE_BASE_CODE_PROTOCOL on device handle failed!\n");
wait_keypress();
return EFI_LOAD_ERROR;
}
/* Should never happen */
if (!pxe->Mode->DhcpDiscoverValid) {
Print(L"\npxe->Mode->DhcpDiscoverValid not TRUE ?!\n");
wait_keypress();
return EFI_LOAD_ERROR;
}
/* Print some information */
Print(
L"[DHCP] Client MAC: %02x:%02x:%02x:%02x:%02x:%02x IP: %d.%d.%d.%d Netmask: %d.%d.%d.%d\n",
pxe->Mode->DhcpDiscover.Dhcpv4.BootpHwAddr[0], pxe->Mode->DhcpDiscover.Dhcpv4.BootpHwAddr[1],
pxe->Mode->DhcpDiscover.Dhcpv4.BootpHwAddr[2], pxe->Mode->DhcpDiscover.Dhcpv4.BootpHwAddr[3],
pxe->Mode->DhcpDiscover.Dhcpv4.BootpHwAddr[4], pxe->Mode->DhcpDiscover.Dhcpv4.BootpHwAddr[5],
pxe->Mode->StationIp.v4.Addr[0] & 0xff, pxe->Mode->StationIp.v4.Addr[1] & 0xff,
pxe->Mode->StationIp.v4.Addr[2] & 0xff, pxe->Mode->StationIp.v4.Addr[3] & 0xff,
pxe->Mode->SubnetMask.v4.Addr[0] & 0xff, pxe->Mode->SubnetMask.v4.Addr[1] & 0xff,
pxe->Mode->SubnetMask.v4.Addr[2] & 0xff, pxe->Mode->SubnetMask.v4.Addr[3] & 0xff
);
str2U(pxe->Mode->DhcpAck.Dhcpv4.BootpBootFile, bootp_bootfile_name, BOOTP_BOOTFILE_NAME_MAXLEN);
Print(
L"[DHCP] TFTP Server IP: %d.%d.%d.%d Boot file (= us): %s\n",
pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[0] & 0xff, pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[1] & 0xff,
pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[2] & 0xff, pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[3] & 0xff,
bootp_bootfile_name
);
for (CHAR8 *p = downloadbuf; (p-downloadbuf) < DOWNLOADBUF_SIZE; p++)
*p = 0;
Print(
L"\ndownloadbuf[0-8]: %02x %02x %02x %02x %02x %02x %02x %02x -- all zeros!\n",
downloadbuf[0], downloadbuf[1], downloadbuf[2], downloadbuf[3],
downloadbuf[4], downloadbuf[5], downloadbuf[6], downloadbuf[7]
);
/* Download ourselves, an EXISTING file */
Print(
L"\nTFTP_READ_FILE \"%s\" from %d.%d.%d.%d... ",
bootp_bootfile_name,
pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[0] & 0xff, pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[1] & 0xff,
pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[2] & 0xff, pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[3] & 0xff
);
status = uefi_call_wrapper(
pxe->Mtftp, // EFI_PXE_BASE_CODE_PROTOCOL function Mtftp
10, // 10 arguments following
pxe, // IN EFI_PXE_BASE_CODE_PROTOCOL *this
EFI_PXE_BASE_CODE_TFTP_READ_FILE, // IN EFI_PXE_BASE_CODE_TFTP_OPCODE
&downloadbuf, // IN OUT VOID *BufferPtr OPTIONAL
FALSE, // IN BOOLEAN Overwrite
&downloadbuf_len, // IN OUT UINT64 *BufferSize
&blocksize, // IN UINTN *BlockSize OPTIONAL
pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr, // IN EFI_IP_ADDRESS *ServerIp
pxe->Mode->DhcpAck.Dhcpv4.BootpBootFile, // IN CHAR8 *Filename OPTIONAL
NULL, // IN EFI_PXE_BASE_CODE_MTFTP_INFO *Info OPTIONAL
FALSE // IN BOOLEAN DontUseBuffer
);
if (status != EFI_SUCCESS) {
Print(L"failed!\nError returned: %r\n", status);
wait_keypress();
return EFI_LOAD_ERROR;
}
Print(L"EFI_SUCCESS!\n");
Print(
L"\ndownloadbuf[0-8]: %02x %02x %02x %02x %02x %02x %02x %02x ",
downloadbuf[0], downloadbuf[1], downloadbuf[2], downloadbuf[3],
downloadbuf[4], downloadbuf[5], downloadbuf[6], downloadbuf[7]
);
/* Test for PE32 0x5A4D signature */
if (downloadbuf[0] == 0x4D || downloadbuf[1] != 0x5A) {
Print(L"-- PE32 sig found!\n");
} else {
Print(L"-- no PE32 sig ?!\n");
wait_keypress();
return EFI_LOAD_ERROR;
}
/* Now try to download a NON-EXISTING file */
Print(
L"\nTFTP_READ_FILE \"foobar.txt\" from %d.%d.%d.%d... ",
pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[0] & 0xff, pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[1] & 0xff,
pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[2] & 0xff, pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr[3] & 0xff
);
downloadbuf_len = DOWNLOADBUF_SIZE;
status = uefi_call_wrapper(
pxe->Mtftp, // EFI_PXE_BASE_CODE_PROTOCOL function Mtftp
10, // 10 arguments following
pxe, // IN EFI_PXE_BASE_CODE_PROTOCOL *this
EFI_PXE_BASE_CODE_TFTP_READ_FILE, // IN EFI_PXE_BASE_CODE_TFTP_OPCODE
&downloadbuf, // IN OUT VOID *BufferPtr OPTIONAL
FALSE, // IN BOOLEAN Overwrite
&downloadbuf_len, // IN OUT UINT64 *BufferSize
&blocksize, // IN UINTN *BlockSize OPTIONAL
pxe->Mode->DhcpAck.Dhcpv4.BootpSiAddr, // IN EFI_IP_ADDRESS *ServerIp
(CHAR8*)"foobar.txt", // IN CHAR8 *Filename OPTIONAL
NULL, // IN EFI_PXE_BASE_CODE_MTFTP_INFO *Info OPTIONAL
FALSE // IN BOOLEAN DontUseBuffer
);
if (status == EFI_SUCCESS)
{
Print(L"EFI_SUCCESS ?!\n\nThis shouldn't have happened if \"foobar.txt\" doesn't exist !!!");
Print(
L"\ndownloadbuf[0-8]: %02x %02x %02x %02x %02x %02x %02x %02x ",
downloadbuf[0], downloadbuf[1], downloadbuf[2], downloadbuf[3],
downloadbuf[4], downloadbuf[5], downloadbuf[6], downloadbuf[7]
);
/* Test for PE32 0x5A4D signature */
if (downloadbuf[0] == 0x4D || downloadbuf[1] != 0x5A) {
Print(L"-- still PE32 sig!\n");
} else {
Print(L"-- no PE32 sig ?!\n");
}
wait_keypress();
return EFI_LOAD_ERROR;
} else if (status == EFI_TFTP_ERROR) {
Print(L"EFI_TFTP_ERROR!\n\nGood! As expected!\n");
} else {
Print(L"%r ?!\n", status);
wait_keypress();
return EFI_LOAD_ERROR;
}
wait_keypress();
return EFI_SUCCESS;
}