blob: 96a5bcca275861f6296e2235da3f7f121a9f155e [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf0efe1bc2016-05-06 21:01:01 +02002/*
Heinrich Schuchardt72a8f162019-09-01 15:24:47 +02003 * Simple network protocol
4 * PXE base code protocol
Alexander Graf0efe1bc2016-05-06 21:01:01 +02005 *
Heinrich Schuchardt72a8f162019-09-01 15:24:47 +02006 * Copyright (c) 2016 Alexander Graf
7 *
8 * The simple network protocol has the following statuses and services
9 * to move between them:
10 *
11 * Start(): EfiSimpleNetworkStopped -> EfiSimpleNetworkStarted
12 * Initialize(): EfiSimpleNetworkStarted -> EfiSimpleNetworkInitialized
13 * Shutdown(): EfiSimpleNetworkInitialized -> EfiSimpleNetworkStarted
14 * Stop(): EfiSimpleNetworkStarted -> EfiSimpleNetworkStopped
15 * Reset(): EfiSimpleNetworkInitialized -> EfiSimpleNetworkInitialized
Alexander Graf0efe1bc2016-05-06 21:01:01 +020016 */
17
18#include <common.h>
19#include <efi_loader.h>
Alexander Graf0efe1bc2016-05-06 21:01:01 +020020#include <malloc.h>
Simon Glass90526e92020-05-10 11:39:56 -060021#include <net.h>
Alexander Graf0efe1bc2016-05-06 21:01:01 +020022
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +020023static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
Heinrich Schuchardta6d37092019-08-06 08:13:33 +020024static const efi_guid_t efi_pxe_base_code_protocol_guid =
25 EFI_PXE_BASE_CODE_PROTOCOL_GUID;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020026static struct efi_pxe_packet *dhcp_ack;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020027static void *new_tx_packet;
Heinrich Schuchardt622fe622018-12-01 00:16:32 +010028static void *transmit_buffer;
Patrick Wildt42f804f2020-10-07 11:04:33 +020029static uchar **receive_buffer;
30static size_t *receive_lengths;
31static int rx_packet_idx;
32static int rx_packet_num;
Heinrich Schuchardt17020552022-11-26 16:44:38 +010033static struct efi_net_obj *netobj;
Heinrich Schuchardt622fe622018-12-01 00:16:32 +010034
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +020035/*
36 * The notification function of this event is called in every timer cycle
37 * to check if a new network packet has been received.
38 */
39static struct efi_event *network_timer_event;
Heinrich Schuchardte5c21602017-10-05 16:36:01 +020040/*
41 * This event is signaled when a packet has been received.
42 */
43static struct efi_event *wait_for_packet;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020044
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020045/**
46 * struct efi_net_obj - EFI object representing a network interface
47 *
48 * @header: EFI object header
49 * @net: simple network protocol interface
50 * @net_mode: status of the network interface
51 * @pxe: PXE base code protocol interface
52 * @pxe_mode: status of the PXE base code protocol
53 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +020054struct efi_net_obj {
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020055 struct efi_object header;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020056 struct efi_simple_network net;
57 struct efi_simple_network_mode net_mode;
Heinrich Schuchardta6d37092019-08-06 08:13:33 +020058 struct efi_pxe_base_code_protocol pxe;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020059 struct efi_pxe_mode pxe_mode;
60};
61
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010062/*
63 * efi_net_start() - start the network interface
64 *
65 * This function implements the Start service of the
66 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
67 * (UEFI) specification for details.
68 *
69 * @this: pointer to the protocol instance
70 * Return: status code
71 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +020072static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
73{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010074 efi_status_t ret = EFI_SUCCESS;
75
Alexander Graf0efe1bc2016-05-06 21:01:01 +020076 EFI_ENTRY("%p", this);
77
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010078 /* Check parameters */
79 if (!this) {
80 ret = EFI_INVALID_PARAMETER;
81 goto out;
82 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +020083
Heinrich Schuchardt7f6d8742019-08-31 09:56:30 +020084 if (this->mode->state != EFI_NETWORK_STOPPED) {
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010085 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardt7f6d8742019-08-31 09:56:30 +020086 } else {
87 this->int_status = 0;
88 wait_for_packet->is_signaled = false;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010089 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardt7f6d8742019-08-31 09:56:30 +020090 }
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010091out:
92 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +020093}
94
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020095/*
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010096 * efi_net_stop() - stop the network interface
97 *
98 * This function implements the Stop service of the
99 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
100 * (UEFI) specification for details.
101 *
102 * @this: pointer to the protocol instance
103 * Return: status code
104 */
105static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
106{
107 efi_status_t ret = EFI_SUCCESS;
108
109 EFI_ENTRY("%p", this);
110
111 /* Check parameters */
112 if (!this) {
113 ret = EFI_INVALID_PARAMETER;
114 goto out;
115 }
116
Heinrich Schuchardt72a8f162019-09-01 15:24:47 +0200117 if (this->mode->state == EFI_NETWORK_STOPPED) {
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100118 ret = EFI_NOT_STARTED;
Heinrich Schuchardt72a8f162019-09-01 15:24:47 +0200119 } else {
120 /* Disable hardware and put it into the reset state */
121 eth_halt();
Patrick Wildt42f804f2020-10-07 11:04:33 +0200122 /* Clear cache of packets */
123 rx_packet_num = 0;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100124 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardt72a8f162019-09-01 15:24:47 +0200125 }
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100126out:
127 return EFI_EXIT(ret);
128}
129
130/*
131 * efi_net_initialize() - initialize the network interface
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200132 *
133 * This function implements the Initialize service of the
134 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
135 * (UEFI) specification for details.
136 *
137 * @this: pointer to the protocol instance
138 * @extra_rx: extra receive buffer to be allocated
139 * @extra_tx: extra transmit buffer to be allocated
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100140 * Return: status code
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200141 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200142static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
143 ulong extra_rx, ulong extra_tx)
144{
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200145 int ret;
146 efi_status_t r = EFI_SUCCESS;
147
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200148 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
149
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100150 /* Check parameters */
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200151 if (!this) {
152 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100153 goto out;
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200154 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200155
Heinrich Schuchardt72a8f162019-09-01 15:24:47 +0200156 switch (this->mode->state) {
157 case EFI_NETWORK_INITIALIZED:
158 case EFI_NETWORK_STARTED:
159 break;
160 default:
161 r = EFI_NOT_STARTED;
162 goto out;
163 }
164
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200165 /* Setup packet buffers */
166 net_init();
167 /* Disable hardware and put it into the reset state */
168 eth_halt();
Patrick Wildt42f804f2020-10-07 11:04:33 +0200169 /* Clear cache of packets */
170 rx_packet_num = 0;
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200171 /* Set current device according to environment variables */
172 eth_set_current();
173 /* Get hardware ready for send and receive operations */
174 ret = eth_init();
175 if (ret < 0) {
176 eth_halt();
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100177 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200178 r = EFI_DEVICE_ERROR;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100179 goto out;
180 } else {
Heinrich Schuchardt7f6d8742019-08-31 09:56:30 +0200181 this->int_status = 0;
182 wait_for_packet->is_signaled = false;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100183 this->mode->state = EFI_NETWORK_INITIALIZED;
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200184 }
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100185out:
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200186 return EFI_EXIT(r);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200187}
188
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100189/*
190 * efi_net_reset() - reinitialize the network interface
191 *
192 * This function implements the Reset service of the
193 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
194 * (UEFI) specification for details.
195 *
196 * @this: pointer to the protocol instance
197 * @extended_verification: execute exhaustive verification
198 * Return: status code
199 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200200static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
201 int extended_verification)
202{
Heinrich Schuchardt72a8f162019-09-01 15:24:47 +0200203 efi_status_t ret;
204
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200205 EFI_ENTRY("%p, %x", this, extended_verification);
206
Heinrich Schuchardt72a8f162019-09-01 15:24:47 +0200207 /* Check parameters */
208 if (!this) {
209 ret = EFI_INVALID_PARAMETER;
210 goto out;
211 }
212
213 switch (this->mode->state) {
214 case EFI_NETWORK_INITIALIZED:
215 break;
216 case EFI_NETWORK_STOPPED:
217 ret = EFI_NOT_STARTED;
218 goto out;
219 default:
220 ret = EFI_DEVICE_ERROR;
221 goto out;
222 }
223
224 this->mode->state = EFI_NETWORK_STARTED;
225 ret = EFI_CALL(efi_net_initialize(this, 0, 0));
226out:
227 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200228}
229
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100230/*
231 * efi_net_shutdown() - shut down the network interface
232 *
233 * This function implements the Shutdown service of the
234 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
235 * (UEFI) specification for details.
236 *
237 * @this: pointer to the protocol instance
238 * Return: status code
239 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200240static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
241{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100242 efi_status_t ret = EFI_SUCCESS;
243
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200244 EFI_ENTRY("%p", this);
245
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100246 /* Check parameters */
247 if (!this) {
248 ret = EFI_INVALID_PARAMETER;
249 goto out;
250 }
251
Heinrich Schuchardt72a8f162019-09-01 15:24:47 +0200252 switch (this->mode->state) {
253 case EFI_NETWORK_INITIALIZED:
254 break;
255 case EFI_NETWORK_STOPPED:
256 ret = EFI_NOT_STARTED;
257 goto out;
258 default:
259 ret = EFI_DEVICE_ERROR;
260 goto out;
261 }
262
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100263 eth_halt();
Heinrich Schuchardt7f6d8742019-08-31 09:56:30 +0200264 this->int_status = 0;
265 wait_for_packet->is_signaled = false;
Heinrich Schuchardt72a8f162019-09-01 15:24:47 +0200266 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100267
268out:
269 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200270}
271
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100272/*
273 * efi_net_receive_filters() - mange multicast receive filters
274 *
275 * This function implements the ReceiveFilters service of the
276 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
277 * (UEFI) specification for details.
278 *
279 * @this: pointer to the protocol instance
280 * @enable: bit mask of receive filters to enable
281 * @disable: bit mask of receive filters to disable
282 * @reset_mcast_filter: true resets contents of the filters
283 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
284 * @mcast_filter: list of new filters
285 * Return: status code
286 */
287static efi_status_t EFIAPI efi_net_receive_filters
288 (struct efi_simple_network *this, u32 enable, u32 disable,
289 int reset_mcast_filter, ulong mcast_filter_count,
290 struct efi_mac_address *mcast_filter)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200291{
292 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
293 reset_mcast_filter, mcast_filter_count, mcast_filter);
294
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200295 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200296}
297
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100298/*
299 * efi_net_station_address() - set the hardware MAC address
300 *
301 * This function implements the StationAddress service of the
302 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
303 * (UEFI) specification for details.
304 *
305 * @this: pointer to the protocol instance
306 * @reset: if true reset the address to default
307 * @new_mac: new MAC address
308 * Return: status code
309 */
310static efi_status_t EFIAPI efi_net_station_address
311 (struct efi_simple_network *this, int reset,
312 struct efi_mac_address *new_mac)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200313{
314 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
315
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200316 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200317}
318
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100319/*
320 * efi_net_statistics() - reset or collect statistics of the network interface
321 *
322 * This function implements the Statistics service of the
323 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
324 * (UEFI) specification for details.
325 *
326 * @this: pointer to the protocol instance
327 * @reset: if true, the statistics are reset
328 * @stat_size: size of the statistics table
329 * @stat_table: table to receive the statistics
330 * Return: status code
331 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200332static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
333 int reset, ulong *stat_size,
334 void *stat_table)
335{
336 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
337
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200338 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200339}
340
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100341/*
342 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
343 *
Heinrich Schuchardt5b4746f2019-09-01 17:17:53 +0200344 * This function implements the MCastIPtoMAC service of the
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100345 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
346 * (UEFI) specification for details.
347 *
348 * @this: pointer to the protocol instance
349 * @ipv6: true if the IP address is an IPv6 address
350 * @ip: IP address
351 * @mac: MAC address
352 * Return: status code
353 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200354static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
355 int ipv6,
356 struct efi_ip_address *ip,
357 struct efi_mac_address *mac)
358{
Heinrich Schuchardt5b4746f2019-09-01 17:17:53 +0200359 efi_status_t ret = EFI_SUCCESS;
360
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200361 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
362
Heinrich Schuchardt5b4746f2019-09-01 17:17:53 +0200363 if (!this || !ip || !mac) {
364 ret = EFI_INVALID_PARAMETER;
365 goto out;
366 }
367
368 if (ipv6) {
369 ret = EFI_UNSUPPORTED;
370 goto out;
371 }
372
373 /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */
374 if ((ip->ip_addr[0] & 0xf0) != 0xe0) {
375 ret = EFI_INVALID_PARAMETER;
376 goto out;
377 };
378
379 switch (this->mode->state) {
380 case EFI_NETWORK_INITIALIZED:
381 case EFI_NETWORK_STARTED:
382 break;
383 default:
384 ret = EFI_NOT_STARTED;
385 goto out;
386 }
387
388 memset(mac, 0, sizeof(struct efi_mac_address));
389
390 /*
391 * Copy lower 23 bits of IPv4 multi-cast address
392 * RFC 1112, RFC 7042 2.1.1.
393 */
394 mac->mac_addr[0] = 0x01;
395 mac->mac_addr[1] = 0x00;
396 mac->mac_addr[2] = 0x5E;
397 mac->mac_addr[3] = ip->ip_addr[1] & 0x7F;
398 mac->mac_addr[4] = ip->ip_addr[2];
399 mac->mac_addr[5] = ip->ip_addr[3];
400out:
401 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200402}
403
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100404/**
405 * efi_net_nvdata() - read or write NVRAM
406 *
407 * This function implements the GetStatus service of the Simple Network
408 * Protocol. See the UEFI spec for details.
409 *
410 * @this: the instance of the Simple Network Protocol
Heinrich Schuchardtfe1a81c2019-09-05 20:37:13 +0200411 * @read_write: true for read, false for write
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100412 * @offset: offset in NVRAM
413 * @buffer_size: size of buffer
414 * @buffer: buffer
415 * Return: status code
416 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200417static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
418 int read_write, ulong offset,
419 ulong buffer_size, char *buffer)
420{
421 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
422 buffer);
423
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200424 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200425}
426
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100427/**
428 * efi_net_get_status() - get interrupt status
429 *
430 * This function implements the GetStatus service of the Simple Network
431 * Protocol. See the UEFI spec for details.
432 *
433 * @this: the instance of the Simple Network Protocol
434 * @int_status: interface status
435 * @txbuf: transmission buffer
436 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200437static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
438 u32 *int_status, void **txbuf)
439{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100440 efi_status_t ret = EFI_SUCCESS;
441
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200442 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
443
Heinrich Schuchardt891b3d92017-10-05 16:36:02 +0200444 efi_timer_check();
445
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100446 /* Check parameters */
447 if (!this) {
448 ret = EFI_INVALID_PARAMETER;
449 goto out;
450 }
451
452 switch (this->mode->state) {
453 case EFI_NETWORK_STOPPED:
454 ret = EFI_NOT_STARTED;
455 goto out;
456 case EFI_NETWORK_STARTED:
457 ret = EFI_DEVICE_ERROR;
458 goto out;
459 default:
460 break;
461 }
462
Heinrich Schuchardt891b3d92017-10-05 16:36:02 +0200463 if (int_status) {
Heinrich Schuchardt7f6d8742019-08-31 09:56:30 +0200464 *int_status = this->int_status;
465 this->int_status = 0;
Heinrich Schuchardt891b3d92017-10-05 16:36:02 +0200466 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200467 if (txbuf)
468 *txbuf = new_tx_packet;
469
470 new_tx_packet = NULL;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100471out:
472 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200473}
474
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100475/**
476 * efi_net_transmit() - transmit a packet
477 *
478 * This function implements the Transmit service of the Simple Network Protocol.
479 * See the UEFI spec for details.
480 *
481 * @this: the instance of the Simple Network Protocol
482 * @header_size: size of the media header
483 * @buffer_size: size of the buffer to receive the packet
484 * @buffer: buffer to receive the packet
485 * @src_addr: source hardware MAC address
486 * @dest_addr: destination hardware MAC address
487 * @protocol: type of header to build
488 * Return: status code
489 */
490static efi_status_t EFIAPI efi_net_transmit
491 (struct efi_simple_network *this, size_t header_size,
492 size_t buffer_size, void *buffer,
493 struct efi_mac_address *src_addr,
494 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200495{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100496 efi_status_t ret = EFI_SUCCESS;
497
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200498 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
499 (unsigned long)header_size, (unsigned long)buffer_size,
500 buffer, src_addr, dest_addr, protocol);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200501
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200502 efi_timer_check();
503
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100504 /* Check parameters */
Heinrich Schuchardtce54fdc2019-05-15 23:27:43 +0200505 if (!this || !buffer) {
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100506 ret = EFI_INVALID_PARAMETER;
507 goto out;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200508 }
509
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100510 /* We do not support jumbo packets */
511 if (buffer_size > PKTSIZE_ALIGN) {
512 ret = EFI_INVALID_PARAMETER;
513 goto out;
514 }
515
Heinrich Schuchardt5947b492019-08-31 10:55:29 +0200516 /* At least the IP header has to fit into the buffer */
517 if (buffer_size < this->mode->media_header_size) {
518 ret = EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100519 goto out;
520 }
521
Heinrich Schuchardt5947b492019-08-31 10:55:29 +0200522 /*
523 * TODO:
524 * Support VLANs. Use net_set_ether() for copying the header. Use a
525 * U_BOOT_ENV_CALLBACK to update the media header size.
526 */
527 if (header_size) {
528 struct ethernet_hdr *header = buffer;
529
530 if (!dest_addr || !protocol ||
531 header_size != this->mode->media_header_size) {
532 ret = EFI_INVALID_PARAMETER;
533 goto out;
534 }
535 if (!src_addr)
536 src_addr = &this->mode->current_address;
537
538 memcpy(header->et_dest, dest_addr, ARP_HLEN);
539 memcpy(header->et_src, src_addr, ARP_HLEN);
540 header->et_protlen = htons(*protocol);
541 }
542
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100543 switch (this->mode->state) {
544 case EFI_NETWORK_STOPPED:
545 ret = EFI_NOT_STARTED;
546 goto out;
547 case EFI_NETWORK_STARTED:
548 ret = EFI_DEVICE_ERROR;
549 goto out;
550 default:
551 break;
552 }
553
554 /* Ethernet packets always fit, just bounce */
Heinrich Schuchardt622fe622018-12-01 00:16:32 +0100555 memcpy(transmit_buffer, buffer, buffer_size);
556 net_send_packet(transmit_buffer, buffer_size);
Alexander Graf712cd292016-09-06 14:26:27 +0200557
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200558 new_tx_packet = buffer;
Heinrich Schuchardt7f6d8742019-08-31 09:56:30 +0200559 this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100560out:
561 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200562}
563
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100564/**
565 * efi_net_receive() - receive a packet from a network interface
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200566 *
567 * This function implements the Receive service of the Simple Network Protocol.
568 * See the UEFI spec for details.
569 *
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100570 * @this: the instance of the Simple Network Protocol
571 * @header_size: size of the media header
572 * @buffer_size: size of the buffer to receive the packet
573 * @buffer: buffer to receive the packet
574 * @src_addr: source MAC address
575 * @dest_addr: destination MAC address
576 * @protocol: protocol
577 * Return: status code
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200578 */
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100579static efi_status_t EFIAPI efi_net_receive
580 (struct efi_simple_network *this, size_t *header_size,
581 size_t *buffer_size, void *buffer,
582 struct efi_mac_address *src_addr,
583 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200584{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100585 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200586 struct ethernet_hdr *eth_hdr;
587 size_t hdr_size = sizeof(struct ethernet_hdr);
588 u16 protlen;
589
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200590 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
591 buffer_size, buffer, src_addr, dest_addr, protocol);
592
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100593 /* Execute events */
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200594 efi_timer_check();
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200595
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100596 /* Check parameters */
Heinrich Schuchardtce54fdc2019-05-15 23:27:43 +0200597 if (!this || !buffer || !buffer_size) {
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100598 ret = EFI_INVALID_PARAMETER;
599 goto out;
600 }
601
602 switch (this->mode->state) {
603 case EFI_NETWORK_STOPPED:
604 ret = EFI_NOT_STARTED;
605 goto out;
606 case EFI_NETWORK_STARTED:
607 ret = EFI_DEVICE_ERROR;
608 goto out;
609 default:
610 break;
611 }
612
Patrick Wildt42f804f2020-10-07 11:04:33 +0200613 if (!rx_packet_num) {
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100614 ret = EFI_NOT_READY;
615 goto out;
616 }
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200617 /* Fill export parameters */
Patrick Wildt42f804f2020-10-07 11:04:33 +0200618 eth_hdr = (struct ethernet_hdr *)receive_buffer[rx_packet_idx];
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200619 protlen = ntohs(eth_hdr->et_protlen);
620 if (protlen == 0x8100) {
621 hdr_size += 4;
Patrick Wildt42f804f2020-10-07 11:04:33 +0200622 protlen = ntohs(*(u16 *)&receive_buffer[rx_packet_idx][hdr_size - 2]);
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200623 }
624 if (header_size)
625 *header_size = hdr_size;
626 if (dest_addr)
627 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
628 if (src_addr)
629 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
630 if (protocol)
631 *protocol = protlen;
Patrick Wildt42f804f2020-10-07 11:04:33 +0200632 if (*buffer_size < receive_lengths[rx_packet_idx]) {
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200633 /* Packet doesn't fit, try again with bigger buffer */
Patrick Wildt42f804f2020-10-07 11:04:33 +0200634 *buffer_size = receive_lengths[rx_packet_idx];
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100635 ret = EFI_BUFFER_TOO_SMALL;
636 goto out;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200637 }
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200638 /* Copy packet */
Patrick Wildt42f804f2020-10-07 11:04:33 +0200639 memcpy(buffer, receive_buffer[rx_packet_idx],
640 receive_lengths[rx_packet_idx]);
641 *buffer_size = receive_lengths[rx_packet_idx];
642 rx_packet_idx = (rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV;
643 rx_packet_num--;
644 if (rx_packet_num)
645 wait_for_packet->is_signaled = true;
646 else
647 this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100648out:
649 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200650}
651
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100652/**
653 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
654 *
655 * This function is called by dhcp_handler().
Heinrich Schuchardtfe1a81c2019-09-05 20:37:13 +0200656 *
657 * @pkt: packet received by dhcp_handler()
658 * @len: length of the packet received
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100659 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200660void efi_net_set_dhcp_ack(void *pkt, int len)
661{
662 int maxsize = sizeof(*dhcp_ack);
663
Heinrich Schuchardt17020552022-11-26 16:44:38 +0100664 if (!dhcp_ack) {
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200665 dhcp_ack = malloc(maxsize);
Heinrich Schuchardt17020552022-11-26 16:44:38 +0100666 if (!dhcp_ack)
667 return;
668 }
669 memset(dhcp_ack, 0, maxsize);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200670 memcpy(dhcp_ack, pkt, min(len, maxsize));
Heinrich Schuchardt17020552022-11-26 16:44:38 +0100671
672 if (netobj)
673 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200674}
675
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100676/**
677 * efi_net_push() - callback for received network packet
678 *
679 * This function is called when a network packet is received by eth_rx().
680 *
681 * @pkt: network packet
682 * @len: length
683 */
684static void efi_net_push(void *pkt, int len)
685{
Patrick Wildt42f804f2020-10-07 11:04:33 +0200686 int rx_packet_next;
687
688 /* Check that we at least received an Ethernet header */
689 if (len < sizeof(struct ethernet_hdr))
690 return;
691
692 /* Check that the buffer won't overflow */
693 if (len > PKTSIZE_ALIGN)
694 return;
695
696 /* Can't store more than pre-alloced buffer */
697 if (rx_packet_num >= ETH_PACKETS_BATCH_RECV)
698 return;
699
700 rx_packet_next = (rx_packet_idx + rx_packet_num) %
701 ETH_PACKETS_BATCH_RECV;
702 memcpy(receive_buffer[rx_packet_next], pkt, len);
703 receive_lengths[rx_packet_next] = len;
704
705 rx_packet_num++;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100706}
707
708/**
709 * efi_network_timer_notify() - check if a new network packet has been received
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200710 *
711 * This notification function is called in every timer cycle.
712 *
Heinrich Schuchardtfe1a81c2019-09-05 20:37:13 +0200713 * @event: the event for which this notification function is registered
714 * @context: event context - not used in this function
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200715 */
716static void EFIAPI efi_network_timer_notify(struct efi_event *event,
717 void *context)
718{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100719 struct efi_simple_network *this = (struct efi_simple_network *)context;
720
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200721 EFI_ENTRY("%p, %p", event, context);
722
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100723 /*
724 * Some network drivers do not support calling eth_rx() before
725 * initialization.
726 */
727 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
728 goto out;
729
Patrick Wildt42f804f2020-10-07 11:04:33 +0200730 if (!rx_packet_num) {
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200731 push_packet = efi_net_push;
732 eth_rx();
733 push_packet = NULL;
Patrick Wildt42f804f2020-10-07 11:04:33 +0200734 if (rx_packet_num) {
735 this->int_status |=
736 EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
737 wait_for_packet->is_signaled = true;
Heinrich Schuchardt7f6d8742019-08-31 09:56:30 +0200738 }
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200739 }
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100740out:
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200741 EFI_EXIT(EFI_SUCCESS);
742}
743
Heinrich Schuchardta6d37092019-08-06 08:13:33 +0200744static efi_status_t EFIAPI efi_pxe_base_code_start(
745 struct efi_pxe_base_code_protocol *this,
746 u8 use_ipv6)
747{
748 return EFI_UNSUPPORTED;
749}
750
751static efi_status_t EFIAPI efi_pxe_base_code_stop(
752 struct efi_pxe_base_code_protocol *this)
753{
754 return EFI_UNSUPPORTED;
755}
756
757static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
758 struct efi_pxe_base_code_protocol *this,
759 u8 sort_offers)
760{
761 return EFI_UNSUPPORTED;
762}
763
764static efi_status_t EFIAPI efi_pxe_base_code_discover(
765 struct efi_pxe_base_code_protocol *this,
766 u16 type, u16 *layer, u8 bis,
767 struct efi_pxe_base_code_discover_info *info)
768{
769 return EFI_UNSUPPORTED;
770}
771
772static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
773 struct efi_pxe_base_code_protocol *this,
774 u32 operation, void *buffer_ptr,
775 u8 overwrite, efi_uintn_t *buffer_size,
776 struct efi_ip_address server_ip, char *filename,
777 struct efi_pxe_base_code_mtftp_info *info,
778 u8 dont_use_buffer)
779{
780 return EFI_UNSUPPORTED;
781}
782
783static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
784 struct efi_pxe_base_code_protocol *this,
785 u16 op_flags, struct efi_ip_address *dest_ip,
786 u16 *dest_port,
787 struct efi_ip_address *gateway_ip,
788 struct efi_ip_address *src_ip, u16 *src_port,
789 efi_uintn_t *header_size, void *header_ptr,
790 efi_uintn_t *buffer_size, void *buffer_ptr)
791{
792 return EFI_UNSUPPORTED;
793}
794
795static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
796 struct efi_pxe_base_code_protocol *this,
797 u16 op_flags, struct efi_ip_address *dest_ip,
798 u16 *dest_port, struct efi_ip_address *src_ip,
799 u16 *src_port, efi_uintn_t *header_size,
800 void *header_ptr, efi_uintn_t *buffer_size,
801 void *buffer_ptr)
802{
803 return EFI_UNSUPPORTED;
804}
805
806static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
807 struct efi_pxe_base_code_protocol *this,
808 struct efi_pxe_base_code_filter *new_filter)
809{
810 return EFI_UNSUPPORTED;
811}
812
813static efi_status_t EFIAPI efi_pxe_base_code_arp(
814 struct efi_pxe_base_code_protocol *this,
815 struct efi_ip_address *ip_addr,
816 struct efi_mac_address *mac_addr)
817{
818 return EFI_UNSUPPORTED;
819}
820
821static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
822 struct efi_pxe_base_code_protocol *this,
823 u8 *new_auto_arp, u8 *new_send_guid,
824 u8 *new_ttl, u8 *new_tos,
825 u8 *new_make_callback)
826{
827 return EFI_UNSUPPORTED;
828}
829
830static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
831 struct efi_pxe_base_code_protocol *this,
832 struct efi_ip_address *new_station_ip,
833 struct efi_ip_address *new_subnet_mask)
834{
835 return EFI_UNSUPPORTED;
836}
837
838static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
839 struct efi_pxe_base_code_protocol *this,
840 u8 *new_dhcp_discover_valid,
841 u8 *new_dhcp_ack_received,
842 u8 *new_proxy_offer_received,
843 u8 *new_pxe_discover_valid,
844 u8 *new_pxe_reply_received,
845 u8 *new_pxe_bis_reply_received,
846 EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
847 EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
848 EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
849 EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
850 EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
851 EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
852{
853 return EFI_UNSUPPORTED;
854}
855
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100856/**
857 * efi_net_register() - register the simple network protocol
858 *
859 * This gets called from do_bootefi_exec().
860 */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100861efi_status_t efi_net_register(void)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200862{
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200863 efi_status_t r;
Patrick Wildt42f804f2020-10-07 11:04:33 +0200864 int i;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200865
866 if (!eth_get_dev()) {
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200867 /* No network device active, don't expose any */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100868 return EFI_SUCCESS;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200869 }
870
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200871 /* We only expose the "active" network device, so one is enough */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200872 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardt622fe622018-12-01 00:16:32 +0100873 if (!netobj)
874 goto out_of_resources;
875
876 /* Allocate an aligned transmit buffer */
877 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
878 if (!transmit_buffer)
879 goto out_of_resources;
880 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100881
Patrick Wildt42f804f2020-10-07 11:04:33 +0200882 /* Allocate a number of receive buffers */
883 receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
884 sizeof(*receive_buffer));
885 if (!receive_buffer)
886 goto out_of_resources;
887 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
888 receive_buffer[i] = malloc(PKTSIZE_ALIGN);
889 if (!receive_buffer[i])
890 goto out_of_resources;
891 }
892 receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
893 sizeof(*receive_lengths));
894 if (!receive_lengths)
895 goto out_of_resources;
896
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100897 /* Hook net up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200898 efi_add_handle(&netobj->header);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200899
900 /* Fill in object data */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200901 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100902 &netobj->net);
903 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100904 goto failure_to_add_protocol;
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200905 r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100906 efi_dp_from_eth());
907 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100908 goto failure_to_add_protocol;
Heinrich Schuchardta6d37092019-08-06 08:13:33 +0200909 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100910 &netobj->pxe);
911 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100912 goto failure_to_add_protocol;
Heinrich Schuchardtbdecf972017-10-05 16:35:57 +0200913 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200914 netobj->net.start = efi_net_start;
915 netobj->net.stop = efi_net_stop;
916 netobj->net.initialize = efi_net_initialize;
917 netobj->net.reset = efi_net_reset;
918 netobj->net.shutdown = efi_net_shutdown;
919 netobj->net.receive_filters = efi_net_receive_filters;
920 netobj->net.station_address = efi_net_station_address;
921 netobj->net.statistics = efi_net_statistics;
922 netobj->net.mcastiptomac = efi_net_mcastiptomac;
923 netobj->net.nvdata = efi_net_nvdata;
924 netobj->net.get_status = efi_net_get_status;
925 netobj->net.transmit = efi_net_transmit;
926 netobj->net.receive = efi_net_receive;
927 netobj->net.mode = &netobj->net_mode;
Heinrich Schuchardt72a8f162019-09-01 15:24:47 +0200928 netobj->net_mode.state = EFI_NETWORK_STOPPED;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200929 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardt5d4a5ea2017-10-05 16:35:58 +0200930 netobj->net_mode.hwaddr_size = ARP_HLEN;
Heinrich Schuchardt5947b492019-08-31 10:55:29 +0200931 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200932 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomasf25ddca2018-06-21 16:21:01 -0700933 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200934
Heinrich Schuchardta6d37092019-08-06 08:13:33 +0200935 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
936 netobj->pxe.start = efi_pxe_base_code_start;
937 netobj->pxe.stop = efi_pxe_base_code_stop;
938 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
939 netobj->pxe.discover = efi_pxe_base_code_discover;
940 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
941 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
942 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
943 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
944 netobj->pxe.arp = efi_pxe_base_code_arp;
945 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
946 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
947 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200948 netobj->pxe.mode = &netobj->pxe_mode;
949 if (dhcp_ack)
950 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
951
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200952 /*
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200953 * Create WaitForPacket event.
954 */
955 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100956 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200957 &wait_for_packet);
958 if (r != EFI_SUCCESS) {
959 printf("ERROR: Failed to register network event\n");
960 return r;
961 }
962 netobj->net.wait_for_packet = wait_for_packet;
963 /*
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200964 * Create a timer event.
965 *
966 * The notification function is used to check if a new network packet
967 * has been received.
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100968 *
969 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200970 */
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100971 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100972 efi_network_timer_notify, &netobj->net, NULL,
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200973 &network_timer_event);
974 if (r != EFI_SUCCESS) {
975 printf("ERROR: Failed to register network event\n");
976 return r;
977 }
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200978 /* Network is time critical, create event in every timer cycle */
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200979 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
980 if (r != EFI_SUCCESS) {
981 printf("ERROR: Failed to set network timer\n");
982 return r;
983 }
984
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100985 return EFI_SUCCESS;
986failure_to_add_protocol:
987 printf("ERROR: Failure to add protocol\n");
988 return r;
Heinrich Schuchardt622fe622018-12-01 00:16:32 +0100989out_of_resources:
990 free(netobj);
Heinrich Schuchardt17020552022-11-26 16:44:38 +0100991 netobj = NULL;
Patrick Wildt42f804f2020-10-07 11:04:33 +0200992 free(transmit_buffer);
993 if (receive_buffer)
994 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
995 free(receive_buffer[i]);
996 free(receive_buffer);
997 free(receive_lengths);
Heinrich Schuchardt622fe622018-12-01 00:16:32 +0100998 printf("ERROR: Out of memory\n");
999 return EFI_OUT_OF_RESOURCES;
Alexander Graf0efe1bc2016-05-06 21:01:01 +02001000}