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
|
/* $OpenBSD: efipxe.c,v 1.6 2019/08/20 23:38:19 patrick Exp $ */
/*
* Copyright (c) 2017 Patrick Wildt <patrick@blueri.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/disklabel.h>
#include <machine/biosvar.h>
#include <libsa.h>
#include <lib/libsa/tftp.h>
#include "disk.h"
#include <efi.h>
#include <efiapi.h>
#include "eficall.h"
#include "efiboot.h"
extern EFI_BOOT_SERVICES *BS;
extern EFI_DEVICE_PATH *efi_bootdp;
extern char *bootmac;
static UINT8 boothw[16];
static EFI_IP_ADDRESS bootip, servip;
static EFI_GUID devp_guid = DEVICE_PATH_PROTOCOL;
static EFI_GUID pxe_guid = EFI_PXE_BASE_CODE_PROTOCOL;
static EFI_PXE_BASE_CODE *PXE = NULL;
extern int efi_device_path_depth(EFI_DEVICE_PATH *dp, int);
extern int efi_device_path_ncmp(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *, int);
/*
* TFTP initial probe. This function discovers PXE handles and tries
* to figure out if there has already been a successfull PXE handshake.
* If so, set the PXE variable.
*/
void
efi_pxeprobe(void)
{
EFI_PXE_BASE_CODE *pxe;
EFI_DEVICE_PATH *dp0;
EFI_HANDLE *handles;
EFI_STATUS status;
UINTN nhandles;
int i, depth;
if (efi_bootdp == NULL)
return;
status = EFI_CALL(BS->LocateHandleBuffer, ByProtocol, &pxe_guid, NULL,
&nhandles, &handles);
if (status != EFI_SUCCESS)
return;
for (i = 0; i < nhandles; i++) {
EFI_PXE_BASE_CODE_DHCPV4_PACKET *dhcp;
status = EFI_CALL(BS->HandleProtocol, handles[i],
&devp_guid, (void **)&dp0);
if (status != EFI_SUCCESS)
continue;
depth = efi_device_path_depth(efi_bootdp, MESSAGING_DEVICE_PATH);
if (depth == -1 || efi_device_path_ncmp(efi_bootdp, dp0, depth))
continue;
status = EFI_CALL(BS->HandleProtocol, handles[i], &pxe_guid,
(void **)&pxe);
if (status != EFI_SUCCESS)
continue;
if (pxe->Mode == NULL)
continue;
dhcp = (EFI_PXE_BASE_CODE_DHCPV4_PACKET *)&pxe->Mode->DhcpAck;
memcpy(&bootip, dhcp->BootpYiAddr, sizeof(bootip));
memcpy(&servip, dhcp->BootpSiAddr, sizeof(servip));
memcpy(boothw, dhcp->BootpHwAddr, sizeof(boothw));
bootmac = boothw;
PXE = pxe;
break;
}
}
/*
* TFTP filesystem layer implementation.
*/
struct tftp_handle {
unsigned char *inbuf; /* input buffer */
size_t inbufsize;
off_t inbufoff;
};
struct fs_ops tftp_fs = {
tftp_open, tftp_close, tftp_read, tftp_write, tftp_seek,
tftp_stat, tftp_readdir
};
int
tftp_open(char *path, struct open_file *f)
{
struct tftp_handle *tftpfile;
EFI_PHYSICAL_ADDRESS addr;
EFI_STATUS status;
UINT64 size;
if (strcmp("TFTP", f->f_dev->dv_name) != 0)
return ENXIO;
if (PXE == NULL)
return ENXIO;
tftpfile = alloc(sizeof(*tftpfile));
if (tftpfile == NULL)
return ENOMEM;
memset(tftpfile, 0, sizeof(*tftpfile));
status = EFI_CALL(PXE->Mtftp, PXE, EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE,
NULL, FALSE, &size, NULL, &servip, path, NULL, FALSE);
if (status != EFI_SUCCESS) {
free(tftpfile, sizeof(*tftpfile));
return ENOENT;
}
tftpfile->inbufsize = size;
if (tftpfile->inbufsize == 0)
goto out;
status = EFI_CALL(BS->AllocatePages, AllocateAnyPages, EfiLoaderData,
EFI_SIZE_TO_PAGES(tftpfile->inbufsize), &addr);
if (status != EFI_SUCCESS) {
free(tftpfile, sizeof(*tftpfile));
return ENOMEM;
}
tftpfile->inbuf = (unsigned char *)((paddr_t)addr);
status = EFI_CALL(PXE->Mtftp, PXE, EFI_PXE_BASE_CODE_TFTP_READ_FILE,
tftpfile->inbuf, FALSE, &size, NULL, &servip, path, NULL, FALSE);
if (status != EFI_SUCCESS) {
free(tftpfile, sizeof(*tftpfile));
return ENXIO;
}
out:
f->f_fsdata = tftpfile;
return 0;
}
int
tftp_close(struct open_file *f)
{
struct tftp_handle *tftpfile = f->f_fsdata;
if (tftpfile->inbuf != NULL)
EFI_CALL(BS->FreePages, (paddr_t)tftpfile->inbuf,
EFI_SIZE_TO_PAGES(tftpfile->inbufsize));
free(tftpfile, sizeof(*tftpfile));
return 0;
}
int
tftp_read(struct open_file *f, void *addr, size_t size, size_t *resid)
{
struct tftp_handle *tftpfile = f->f_fsdata;
size_t toread;
if (size > tftpfile->inbufsize - tftpfile->inbufoff)
toread = tftpfile->inbufsize - tftpfile->inbufoff;
else
toread = size;
if (toread != 0) {
memcpy(addr, tftpfile->inbuf + tftpfile->inbufoff, toread);
tftpfile->inbufoff += toread;
}
if (resid != NULL)
*resid = size - toread;
return 0;
}
int
tftp_write(struct open_file *f, void *start, size_t size, size_t *resid)
{
return EROFS;
}
off_t
tftp_seek(struct open_file *f, off_t offset, int where)
{
struct tftp_handle *tftpfile = f->f_fsdata;
switch(where) {
case SEEK_CUR:
if (tftpfile->inbufoff + offset < 0 ||
tftpfile->inbufoff + offset > tftpfile->inbufsize) {
errno = EOFFSET;
break;
}
tftpfile->inbufoff += offset;
return (tftpfile->inbufoff);
case SEEK_SET:
if (offset < 0 || offset > tftpfile->inbufsize) {
errno = EOFFSET;
break;
}
tftpfile->inbufoff = offset;
return (tftpfile->inbufoff);
case SEEK_END:
tftpfile->inbufoff = tftpfile->inbufsize;
return (tftpfile->inbufoff);
default:
errno = EINVAL;
}
return((off_t)-1);
}
int
tftp_stat(struct open_file *f, struct stat *sb)
{
struct tftp_handle *tftpfile = f->f_fsdata;
sb->st_mode = 0444;
sb->st_nlink = 1;
sb->st_uid = 0;
sb->st_gid = 0;
sb->st_size = tftpfile->inbufsize;
return 0;
}
int
tftp_readdir(struct open_file *f, char *name)
{
return EOPNOTSUPP;
}
/*
* Dummy TFTP network device.
*/
int
tftpopen(struct open_file *f, ...)
{
char **fname, *p;
va_list ap;
va_start(ap, f);
fname = va_arg(ap, char **);
va_end(ap);
/* No PXE set -> no PXE available */
if (PXE == NULL)
return 1;
/* Parse tftp:bsd into "tftp" and "bsd" */
for (p = *fname; *p != ':' && *p != '\0'; p++)
;
if (*p != ':')
return 1;
if (strncmp(*fname, "tftp", p - *fname) != 0)
return 1;
*fname = p + 1;
return 0;
}
int
tftpclose(struct open_file *f)
{
return 0;
}
int
tftpioctl(struct open_file *f, u_long cmd, void *data)
{
return EOPNOTSUPP;
}
int
tftpstrategy(void *devdata, int rw, daddr32_t blk, size_t size, void *buf,
size_t *rsize)
{
return EOPNOTSUPP;
}
|