blob: 27fc401411275b4111fe29f09dd394b6b5b99ab6 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf94c4b992016-05-06 21:01:01 +02002/*
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +02003 * Simple network protocol
4 * PXE base code protocol
Alexander Graf94c4b992016-05-06 21:01:01 +02005 *
Heinrich Schuchardt6a6e7d82019-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 Graf94c4b992016-05-06 21:01:01 +020016 */
17
Heinrich Schuchardt955a3212025-01-16 20:26:59 +010018#define LOG_CATEGORY LOGC_EFI
19
Alexander Graf94c4b992016-05-06 21:01:01 +020020#include <efi_loader.h>
Adriano Cordova93cba0f2024-12-04 00:05:23 -030021#include <dm.h>
Adriano Cordova0d1f5092024-12-04 00:05:24 -030022#include <linux/sizes.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020023#include <malloc.h>
Adriano Cordova3c951362024-12-04 00:05:21 -030024#include <vsprintf.h>
Simon Glass274e0b02020-05-10 11:39:56 -060025#include <net.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020026
Adriano Cordova7f2bcd42025-03-03 11:13:11 -030027const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020028static const efi_guid_t efi_pxe_base_code_protocol_guid =
29 EFI_PXE_BASE_CODE_PROTOCOL_GUID;
Alexander Graf94c4b992016-05-06 21:01:01 +020030static struct efi_pxe_packet *dhcp_ack;
Alexander Graf94c4b992016-05-06 21:01:01 +020031static void *new_tx_packet;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010032static void *transmit_buffer;
Patrick Wildtfab89102020-10-07 11:04:33 +020033static uchar **receive_buffer;
34static size_t *receive_lengths;
35static int rx_packet_idx;
36static int rx_packet_num;
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +010037static struct efi_net_obj *netobj;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010038
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020039/*
Adriano Cordova93cba0f2024-12-04 00:05:23 -030040 * The current network device path. This device path is updated when a new
41 * bootfile is downloaded from the network. If then the bootfile is loaded
42 * as an efi image, net_dp is passed as the device path of the loaded image.
43 */
44static struct efi_device_path *net_dp;
45
Adriano Cordova0d1f5092024-12-04 00:05:24 -030046static struct wget_http_info efi_wget_info = {
47 .set_bootdev = false,
48 .check_buffer_size = true,
49
50};
51
Adriano Cordova93cba0f2024-12-04 00:05:23 -030052/*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020053 * The notification function of this event is called in every timer cycle
54 * to check if a new network packet has been received.
55 */
56static struct efi_event *network_timer_event;
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +020057/*
58 * This event is signaled when a packet has been received.
59 */
60static struct efi_event *wait_for_packet;
Alexander Graf94c4b992016-05-06 21:01:01 +020061
Heinrich Schuchardt72928722018-09-26 05:27:56 +020062/**
63 * struct efi_net_obj - EFI object representing a network interface
64 *
Adriano Cordova9debc902024-12-04 00:05:25 -030065 * @header: EFI object header
66 * @net: simple network protocol interface
67 * @net_mode: status of the network interface
68 * @pxe: PXE base code protocol interface
69 * @pxe_mode: status of the PXE base code protocol
70 * @ip4_config2: IP4 Config2 protocol interface
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030071 * @http_service_binding: Http service binding protocol interface
Heinrich Schuchardt72928722018-09-26 05:27:56 +020072 */
Alexander Graf94c4b992016-05-06 21:01:01 +020073struct efi_net_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020074 struct efi_object header;
Alexander Graf94c4b992016-05-06 21:01:01 +020075 struct efi_simple_network net;
76 struct efi_simple_network_mode net_mode;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020077 struct efi_pxe_base_code_protocol pxe;
Alexander Graf94c4b992016-05-06 21:01:01 +020078 struct efi_pxe_mode pxe_mode;
Adriano Cordova9debc902024-12-04 00:05:25 -030079#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
80 struct efi_ip4_config2_protocol ip4_config2;
81#endif
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030082#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
83 struct efi_service_binding_protocol http_service_binding;
84#endif
Alexander Graf94c4b992016-05-06 21:01:01 +020085};
86
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010087/*
88 * efi_net_start() - start the network interface
89 *
90 * This function implements the Start service of the
91 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
92 * (UEFI) specification for details.
93 *
94 * @this: pointer to the protocol instance
95 * Return: status code
96 */
Alexander Graf94c4b992016-05-06 21:01:01 +020097static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
98{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010099 efi_status_t ret = EFI_SUCCESS;
100
Alexander Graf94c4b992016-05-06 21:01:01 +0200101 EFI_ENTRY("%p", this);
102
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100103 /* Check parameters */
104 if (!this) {
105 ret = EFI_INVALID_PARAMETER;
106 goto out;
107 }
108
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200109 if (this->mode->state != EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100110 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200111 } else {
112 this->int_status = 0;
113 wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100114 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200115 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100116out:
117 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200118}
119
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100120/*
121 * efi_net_stop() - stop the network interface
122 *
123 * This function implements the Stop service of the
124 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
125 * (UEFI) specification for details.
126 *
127 * @this: pointer to the protocol instance
128 * Return: status code
129 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200130static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
131{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100132 efi_status_t ret = EFI_SUCCESS;
133
Alexander Graf94c4b992016-05-06 21:01:01 +0200134 EFI_ENTRY("%p", this);
135
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100136 /* Check parameters */
137 if (!this) {
138 ret = EFI_INVALID_PARAMETER;
139 goto out;
140 }
141
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200142 if (this->mode->state == EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100143 ret = EFI_NOT_STARTED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200144 } else {
145 /* Disable hardware and put it into the reset state */
146 eth_halt();
Patrick Wildtfab89102020-10-07 11:04:33 +0200147 /* Clear cache of packets */
148 rx_packet_num = 0;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100149 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200150 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100151out:
152 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200153}
154
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200155/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100156 * efi_net_initialize() - initialize the network interface
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200157 *
158 * This function implements the Initialize service of the
159 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
160 * (UEFI) specification for details.
161 *
162 * @this: pointer to the protocol instance
163 * @extra_rx: extra receive buffer to be allocated
164 * @extra_tx: extra transmit buffer to be allocated
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100165 * Return: status code
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200166 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200167static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
168 ulong extra_rx, ulong extra_tx)
169{
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200170 int ret;
171 efi_status_t r = EFI_SUCCESS;
172
Alexander Graf94c4b992016-05-06 21:01:01 +0200173 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
174
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100175 /* Check parameters */
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200176 if (!this) {
177 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100178 goto out;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200179 }
180
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200181 switch (this->mode->state) {
182 case EFI_NETWORK_INITIALIZED:
183 case EFI_NETWORK_STARTED:
184 break;
185 default:
186 r = EFI_NOT_STARTED;
187 goto out;
188 }
189
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200190 /* Setup packet buffers */
191 net_init();
192 /* Disable hardware and put it into the reset state */
193 eth_halt();
Patrick Wildtfab89102020-10-07 11:04:33 +0200194 /* Clear cache of packets */
195 rx_packet_num = 0;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200196 /* Set current device according to environment variables */
197 eth_set_current();
198 /* Get hardware ready for send and receive operations */
199 ret = eth_init();
200 if (ret < 0) {
201 eth_halt();
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100202 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200203 r = EFI_DEVICE_ERROR;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100204 goto out;
205 } else {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200206 this->int_status = 0;
207 wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100208 this->mode->state = EFI_NETWORK_INITIALIZED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200209 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100210out:
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200211 return EFI_EXIT(r);
Alexander Graf94c4b992016-05-06 21:01:01 +0200212}
213
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100214/*
215 * efi_net_reset() - reinitialize the network interface
216 *
217 * This function implements the Reset service of the
218 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
219 * (UEFI) specification for details.
220 *
221 * @this: pointer to the protocol instance
222 * @extended_verification: execute exhaustive verification
223 * Return: status code
224 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200225static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
226 int extended_verification)
227{
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200228 efi_status_t ret;
229
Alexander Graf94c4b992016-05-06 21:01:01 +0200230 EFI_ENTRY("%p, %x", this, extended_verification);
231
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200232 /* Check parameters */
233 if (!this) {
234 ret = EFI_INVALID_PARAMETER;
235 goto out;
236 }
237
238 switch (this->mode->state) {
239 case EFI_NETWORK_INITIALIZED:
240 break;
241 case EFI_NETWORK_STOPPED:
242 ret = EFI_NOT_STARTED;
243 goto out;
244 default:
245 ret = EFI_DEVICE_ERROR;
246 goto out;
247 }
248
249 this->mode->state = EFI_NETWORK_STARTED;
250 ret = EFI_CALL(efi_net_initialize(this, 0, 0));
251out:
252 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200253}
254
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100255/*
256 * efi_net_shutdown() - shut down the network interface
257 *
258 * This function implements the Shutdown service of the
259 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
260 * (UEFI) specification for details.
261 *
262 * @this: pointer to the protocol instance
263 * Return: status code
264 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200265static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
266{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100267 efi_status_t ret = EFI_SUCCESS;
268
Alexander Graf94c4b992016-05-06 21:01:01 +0200269 EFI_ENTRY("%p", this);
270
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100271 /* Check parameters */
272 if (!this) {
273 ret = EFI_INVALID_PARAMETER;
274 goto out;
275 }
276
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200277 switch (this->mode->state) {
278 case EFI_NETWORK_INITIALIZED:
279 break;
280 case EFI_NETWORK_STOPPED:
281 ret = EFI_NOT_STARTED;
282 goto out;
283 default:
284 ret = EFI_DEVICE_ERROR;
285 goto out;
286 }
287
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100288 eth_halt();
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200289 this->int_status = 0;
290 wait_for_packet->is_signaled = false;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200291 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100292
293out:
294 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200295}
296
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100297/*
298 * efi_net_receive_filters() - mange multicast receive filters
299 *
300 * This function implements the ReceiveFilters service of the
301 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
302 * (UEFI) specification for details.
303 *
304 * @this: pointer to the protocol instance
305 * @enable: bit mask of receive filters to enable
306 * @disable: bit mask of receive filters to disable
307 * @reset_mcast_filter: true resets contents of the filters
308 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
309 * @mcast_filter: list of new filters
310 * Return: status code
311 */
312static efi_status_t EFIAPI efi_net_receive_filters
313 (struct efi_simple_network *this, u32 enable, u32 disable,
314 int reset_mcast_filter, ulong mcast_filter_count,
315 struct efi_mac_address *mcast_filter)
Alexander Graf94c4b992016-05-06 21:01:01 +0200316{
317 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
318 reset_mcast_filter, mcast_filter_count, mcast_filter);
319
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200320 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200321}
322
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100323/*
324 * efi_net_station_address() - set the hardware MAC address
325 *
326 * This function implements the StationAddress service of the
327 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
328 * (UEFI) specification for details.
329 *
330 * @this: pointer to the protocol instance
331 * @reset: if true reset the address to default
332 * @new_mac: new MAC address
333 * Return: status code
334 */
335static efi_status_t EFIAPI efi_net_station_address
336 (struct efi_simple_network *this, int reset,
337 struct efi_mac_address *new_mac)
Alexander Graf94c4b992016-05-06 21:01:01 +0200338{
339 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
340
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200341 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200342}
343
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100344/*
345 * efi_net_statistics() - reset or collect statistics of the network interface
346 *
347 * This function implements the Statistics service of the
348 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
349 * (UEFI) specification for details.
350 *
351 * @this: pointer to the protocol instance
352 * @reset: if true, the statistics are reset
353 * @stat_size: size of the statistics table
354 * @stat_table: table to receive the statistics
355 * Return: status code
356 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200357static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
358 int reset, ulong *stat_size,
359 void *stat_table)
360{
361 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
362
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200363 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200364}
365
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100366/*
367 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
368 *
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200369 * This function implements the MCastIPtoMAC service of the
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100370 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
371 * (UEFI) specification for details.
372 *
373 * @this: pointer to the protocol instance
374 * @ipv6: true if the IP address is an IPv6 address
375 * @ip: IP address
376 * @mac: MAC address
377 * Return: status code
378 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200379static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
380 int ipv6,
381 struct efi_ip_address *ip,
382 struct efi_mac_address *mac)
383{
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200384 efi_status_t ret = EFI_SUCCESS;
385
Alexander Graf94c4b992016-05-06 21:01:01 +0200386 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
387
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200388 if (!this || !ip || !mac) {
389 ret = EFI_INVALID_PARAMETER;
390 goto out;
391 }
392
393 if (ipv6) {
394 ret = EFI_UNSUPPORTED;
395 goto out;
396 }
397
398 /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */
399 if ((ip->ip_addr[0] & 0xf0) != 0xe0) {
400 ret = EFI_INVALID_PARAMETER;
401 goto out;
402 };
403
404 switch (this->mode->state) {
405 case EFI_NETWORK_INITIALIZED:
406 case EFI_NETWORK_STARTED:
407 break;
408 default:
409 ret = EFI_NOT_STARTED;
410 goto out;
411 }
412
413 memset(mac, 0, sizeof(struct efi_mac_address));
414
415 /*
416 * Copy lower 23 bits of IPv4 multi-cast address
417 * RFC 1112, RFC 7042 2.1.1.
418 */
419 mac->mac_addr[0] = 0x01;
420 mac->mac_addr[1] = 0x00;
421 mac->mac_addr[2] = 0x5E;
422 mac->mac_addr[3] = ip->ip_addr[1] & 0x7F;
423 mac->mac_addr[4] = ip->ip_addr[2];
424 mac->mac_addr[5] = ip->ip_addr[3];
425out:
426 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200427}
428
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100429/**
430 * efi_net_nvdata() - read or write NVRAM
431 *
432 * This function implements the GetStatus service of the Simple Network
433 * Protocol. See the UEFI spec for details.
434 *
435 * @this: the instance of the Simple Network Protocol
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200436 * @read_write: true for read, false for write
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100437 * @offset: offset in NVRAM
438 * @buffer_size: size of buffer
439 * @buffer: buffer
440 * Return: status code
441 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200442static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
443 int read_write, ulong offset,
444 ulong buffer_size, char *buffer)
445{
446 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
447 buffer);
448
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200449 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200450}
451
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100452/**
453 * efi_net_get_status() - get interrupt status
454 *
455 * This function implements the GetStatus service of the Simple Network
456 * Protocol. See the UEFI spec for details.
457 *
458 * @this: the instance of the Simple Network Protocol
459 * @int_status: interface status
460 * @txbuf: transmission buffer
461 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200462static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
463 u32 *int_status, void **txbuf)
464{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100465 efi_status_t ret = EFI_SUCCESS;
466
Alexander Graf94c4b992016-05-06 21:01:01 +0200467 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
468
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200469 efi_timer_check();
470
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100471 /* Check parameters */
472 if (!this) {
473 ret = EFI_INVALID_PARAMETER;
474 goto out;
475 }
476
477 switch (this->mode->state) {
478 case EFI_NETWORK_STOPPED:
479 ret = EFI_NOT_STARTED;
480 goto out;
481 case EFI_NETWORK_STARTED:
482 ret = EFI_DEVICE_ERROR;
483 goto out;
484 default:
485 break;
486 }
487
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200488 if (int_status) {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200489 *int_status = this->int_status;
490 this->int_status = 0;
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200491 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200492 if (txbuf)
493 *txbuf = new_tx_packet;
494
495 new_tx_packet = NULL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100496out:
497 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200498}
499
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100500/**
501 * efi_net_transmit() - transmit a packet
502 *
503 * This function implements the Transmit service of the Simple Network Protocol.
504 * See the UEFI spec for details.
505 *
506 * @this: the instance of the Simple Network Protocol
507 * @header_size: size of the media header
508 * @buffer_size: size of the buffer to receive the packet
509 * @buffer: buffer to receive the packet
510 * @src_addr: source hardware MAC address
511 * @dest_addr: destination hardware MAC address
512 * @protocol: type of header to build
513 * Return: status code
514 */
515static efi_status_t EFIAPI efi_net_transmit
516 (struct efi_simple_network *this, size_t header_size,
517 size_t buffer_size, void *buffer,
518 struct efi_mac_address *src_addr,
519 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200520{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100521 efi_status_t ret = EFI_SUCCESS;
522
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200523 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
524 (unsigned long)header_size, (unsigned long)buffer_size,
525 buffer, src_addr, dest_addr, protocol);
Alexander Graf94c4b992016-05-06 21:01:01 +0200526
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200527 efi_timer_check();
528
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100529 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200530 if (!this || !buffer) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100531 ret = EFI_INVALID_PARAMETER;
532 goto out;
533 }
534
535 /* We do not support jumbo packets */
536 if (buffer_size > PKTSIZE_ALIGN) {
537 ret = EFI_INVALID_PARAMETER;
538 goto out;
539 }
540
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200541 /* At least the IP header has to fit into the buffer */
542 if (buffer_size < this->mode->media_header_size) {
543 ret = EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100544 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200545 }
546
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200547 /*
548 * TODO:
549 * Support VLANs. Use net_set_ether() for copying the header. Use a
550 * U_BOOT_ENV_CALLBACK to update the media header size.
551 */
552 if (header_size) {
553 struct ethernet_hdr *header = buffer;
554
555 if (!dest_addr || !protocol ||
556 header_size != this->mode->media_header_size) {
557 ret = EFI_INVALID_PARAMETER;
558 goto out;
559 }
560 if (!src_addr)
561 src_addr = &this->mode->current_address;
562
563 memcpy(header->et_dest, dest_addr, ARP_HLEN);
564 memcpy(header->et_src, src_addr, ARP_HLEN);
565 header->et_protlen = htons(*protocol);
566 }
567
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100568 switch (this->mode->state) {
569 case EFI_NETWORK_STOPPED:
570 ret = EFI_NOT_STARTED;
571 goto out;
572 case EFI_NETWORK_STARTED:
573 ret = EFI_DEVICE_ERROR;
574 goto out;
575 default:
576 break;
577 }
578
579 /* Ethernet packets always fit, just bounce */
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100580 memcpy(transmit_buffer, buffer, buffer_size);
581 net_send_packet(transmit_buffer, buffer_size);
Alexander Graf0b12b862016-09-06 14:26:27 +0200582
Alexander Graf94c4b992016-05-06 21:01:01 +0200583 new_tx_packet = buffer;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200584 this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100585out:
586 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200587}
588
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100589/**
590 * efi_net_receive() - receive a packet from a network interface
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200591 *
592 * This function implements the Receive service of the Simple Network Protocol.
593 * See the UEFI spec for details.
594 *
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100595 * @this: the instance of the Simple Network Protocol
596 * @header_size: size of the media header
597 * @buffer_size: size of the buffer to receive the packet
598 * @buffer: buffer to receive the packet
599 * @src_addr: source MAC address
600 * @dest_addr: destination MAC address
601 * @protocol: protocol
602 * Return: status code
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200603 */
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100604static efi_status_t EFIAPI efi_net_receive
605 (struct efi_simple_network *this, size_t *header_size,
606 size_t *buffer_size, void *buffer,
607 struct efi_mac_address *src_addr,
608 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200609{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100610 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200611 struct ethernet_hdr *eth_hdr;
612 size_t hdr_size = sizeof(struct ethernet_hdr);
613 u16 protlen;
614
Alexander Graf94c4b992016-05-06 21:01:01 +0200615 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
616 buffer_size, buffer, src_addr, dest_addr, protocol);
617
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100618 /* Execute events */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200619 efi_timer_check();
Alexander Graf94c4b992016-05-06 21:01:01 +0200620
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100621 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200622 if (!this || !buffer || !buffer_size) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100623 ret = EFI_INVALID_PARAMETER;
624 goto out;
625 }
626
627 switch (this->mode->state) {
628 case EFI_NETWORK_STOPPED:
629 ret = EFI_NOT_STARTED;
630 goto out;
631 case EFI_NETWORK_STARTED:
632 ret = EFI_DEVICE_ERROR;
633 goto out;
634 default:
635 break;
636 }
637
Patrick Wildtfab89102020-10-07 11:04:33 +0200638 if (!rx_packet_num) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100639 ret = EFI_NOT_READY;
640 goto out;
641 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200642 /* Fill export parameters */
Patrick Wildtfab89102020-10-07 11:04:33 +0200643 eth_hdr = (struct ethernet_hdr *)receive_buffer[rx_packet_idx];
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200644 protlen = ntohs(eth_hdr->et_protlen);
645 if (protlen == 0x8100) {
646 hdr_size += 4;
Patrick Wildtfab89102020-10-07 11:04:33 +0200647 protlen = ntohs(*(u16 *)&receive_buffer[rx_packet_idx][hdr_size - 2]);
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200648 }
649 if (header_size)
650 *header_size = hdr_size;
651 if (dest_addr)
652 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
653 if (src_addr)
654 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
655 if (protocol)
656 *protocol = protlen;
Patrick Wildtfab89102020-10-07 11:04:33 +0200657 if (*buffer_size < receive_lengths[rx_packet_idx]) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200658 /* Packet doesn't fit, try again with bigger buffer */
Patrick Wildtfab89102020-10-07 11:04:33 +0200659 *buffer_size = receive_lengths[rx_packet_idx];
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100660 ret = EFI_BUFFER_TOO_SMALL;
661 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200662 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200663 /* Copy packet */
Patrick Wildtfab89102020-10-07 11:04:33 +0200664 memcpy(buffer, receive_buffer[rx_packet_idx],
665 receive_lengths[rx_packet_idx]);
666 *buffer_size = receive_lengths[rx_packet_idx];
667 rx_packet_idx = (rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV;
668 rx_packet_num--;
669 if (rx_packet_num)
670 wait_for_packet->is_signaled = true;
671 else
672 this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100673out:
674 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200675}
676
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100677/**
678 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
679 *
680 * This function is called by dhcp_handler().
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200681 *
682 * @pkt: packet received by dhcp_handler()
683 * @len: length of the packet received
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100684 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200685void efi_net_set_dhcp_ack(void *pkt, int len)
686{
687 int maxsize = sizeof(*dhcp_ack);
688
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100689 if (!dhcp_ack) {
Alexander Graf94c4b992016-05-06 21:01:01 +0200690 dhcp_ack = malloc(maxsize);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100691 if (!dhcp_ack)
692 return;
693 }
694 memset(dhcp_ack, 0, maxsize);
Alexander Graf94c4b992016-05-06 21:01:01 +0200695 memcpy(dhcp_ack, pkt, min(len, maxsize));
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100696
697 if (netobj)
698 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
Alexander Graf94c4b992016-05-06 21:01:01 +0200699}
700
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100701/**
702 * efi_net_push() - callback for received network packet
703 *
704 * This function is called when a network packet is received by eth_rx().
705 *
706 * @pkt: network packet
707 * @len: length
708 */
709static void efi_net_push(void *pkt, int len)
710{
Patrick Wildtfab89102020-10-07 11:04:33 +0200711 int rx_packet_next;
712
713 /* Check that we at least received an Ethernet header */
714 if (len < sizeof(struct ethernet_hdr))
715 return;
716
717 /* Check that the buffer won't overflow */
718 if (len > PKTSIZE_ALIGN)
719 return;
720
721 /* Can't store more than pre-alloced buffer */
722 if (rx_packet_num >= ETH_PACKETS_BATCH_RECV)
723 return;
724
725 rx_packet_next = (rx_packet_idx + rx_packet_num) %
726 ETH_PACKETS_BATCH_RECV;
727 memcpy(receive_buffer[rx_packet_next], pkt, len);
728 receive_lengths[rx_packet_next] = len;
729
730 rx_packet_num++;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100731}
732
733/**
734 * efi_network_timer_notify() - check if a new network packet has been received
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200735 *
736 * This notification function is called in every timer cycle.
737 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200738 * @event: the event for which this notification function is registered
739 * @context: event context - not used in this function
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200740 */
741static void EFIAPI efi_network_timer_notify(struct efi_event *event,
742 void *context)
743{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100744 struct efi_simple_network *this = (struct efi_simple_network *)context;
745
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200746 EFI_ENTRY("%p, %p", event, context);
747
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100748 /*
749 * Some network drivers do not support calling eth_rx() before
750 * initialization.
751 */
752 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
753 goto out;
754
Patrick Wildtfab89102020-10-07 11:04:33 +0200755 if (!rx_packet_num) {
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200756 push_packet = efi_net_push;
757 eth_rx();
758 push_packet = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +0200759 if (rx_packet_num) {
760 this->int_status |=
761 EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
762 wait_for_packet->is_signaled = true;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200763 }
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200764 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100765out:
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200766 EFI_EXIT(EFI_SUCCESS);
767}
768
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200769static efi_status_t EFIAPI efi_pxe_base_code_start(
770 struct efi_pxe_base_code_protocol *this,
771 u8 use_ipv6)
772{
773 return EFI_UNSUPPORTED;
774}
775
776static efi_status_t EFIAPI efi_pxe_base_code_stop(
777 struct efi_pxe_base_code_protocol *this)
778{
779 return EFI_UNSUPPORTED;
780}
781
782static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
783 struct efi_pxe_base_code_protocol *this,
784 u8 sort_offers)
785{
786 return EFI_UNSUPPORTED;
787}
788
789static efi_status_t EFIAPI efi_pxe_base_code_discover(
790 struct efi_pxe_base_code_protocol *this,
791 u16 type, u16 *layer, u8 bis,
792 struct efi_pxe_base_code_discover_info *info)
793{
794 return EFI_UNSUPPORTED;
795}
796
797static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
798 struct efi_pxe_base_code_protocol *this,
799 u32 operation, void *buffer_ptr,
800 u8 overwrite, efi_uintn_t *buffer_size,
801 struct efi_ip_address server_ip, char *filename,
802 struct efi_pxe_base_code_mtftp_info *info,
803 u8 dont_use_buffer)
804{
805 return EFI_UNSUPPORTED;
806}
807
808static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
809 struct efi_pxe_base_code_protocol *this,
810 u16 op_flags, struct efi_ip_address *dest_ip,
811 u16 *dest_port,
812 struct efi_ip_address *gateway_ip,
813 struct efi_ip_address *src_ip, u16 *src_port,
814 efi_uintn_t *header_size, void *header_ptr,
815 efi_uintn_t *buffer_size, void *buffer_ptr)
816{
817 return EFI_UNSUPPORTED;
818}
819
820static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
821 struct efi_pxe_base_code_protocol *this,
822 u16 op_flags, struct efi_ip_address *dest_ip,
823 u16 *dest_port, struct efi_ip_address *src_ip,
824 u16 *src_port, efi_uintn_t *header_size,
825 void *header_ptr, efi_uintn_t *buffer_size,
826 void *buffer_ptr)
827{
828 return EFI_UNSUPPORTED;
829}
830
831static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
832 struct efi_pxe_base_code_protocol *this,
833 struct efi_pxe_base_code_filter *new_filter)
834{
835 return EFI_UNSUPPORTED;
836}
837
838static efi_status_t EFIAPI efi_pxe_base_code_arp(
839 struct efi_pxe_base_code_protocol *this,
840 struct efi_ip_address *ip_addr,
841 struct efi_mac_address *mac_addr)
842{
843 return EFI_UNSUPPORTED;
844}
845
846static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
847 struct efi_pxe_base_code_protocol *this,
848 u8 *new_auto_arp, u8 *new_send_guid,
849 u8 *new_ttl, u8 *new_tos,
850 u8 *new_make_callback)
851{
852 return EFI_UNSUPPORTED;
853}
854
855static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
856 struct efi_pxe_base_code_protocol *this,
857 struct efi_ip_address *new_station_ip,
858 struct efi_ip_address *new_subnet_mask)
859{
860 return EFI_UNSUPPORTED;
861}
862
863static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
864 struct efi_pxe_base_code_protocol *this,
865 u8 *new_dhcp_discover_valid,
866 u8 *new_dhcp_ack_received,
867 u8 *new_proxy_offer_received,
868 u8 *new_pxe_discover_valid,
869 u8 *new_pxe_reply_received,
870 u8 *new_pxe_bis_reply_received,
871 EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
872 EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
873 EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
874 EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
875 EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
876 EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
877{
878 return EFI_UNSUPPORTED;
879}
880
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100881/**
Adriano Cordova1d70b682025-03-03 11:13:13 -0300882 * efi_net_do_start() - start the efi network stack
883 *
884 * This gets called from do_bootefi_exec() each time a payload gets executed.
885 *
886 * Return: status code
887 */
888efi_status_t efi_net_do_start(void)
889{
890 efi_status_t r = EFI_SUCCESS;
891
892#ifdef CONFIG_EFI_HTTP_PROTOCOL
893 /*
894 * No harm on doing the following. If the PXE handle is present, the client could
895 * find it and try to get its IP address from it. In here the PXE handle is present
896 * but the PXE protocol is not yet implmenented, so we add this in the meantime.
897 */
898 efi_net_get_addr((struct efi_ipv4_address *)&netobj->pxe_mode.station_ip,
899 (struct efi_ipv4_address *)&netobj->pxe_mode.subnet_mask, NULL);
900#endif
901
902 return r;
903}
904
905/**
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100906 * efi_net_register() - register the simple network protocol
907 *
908 * This gets called from do_bootefi_exec().
909 */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100910efi_status_t efi_net_register(void)
Alexander Graf94c4b992016-05-06 21:01:01 +0200911{
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200912 efi_status_t r;
Patrick Wildtfab89102020-10-07 11:04:33 +0200913 int i;
Alexander Graf94c4b992016-05-06 21:01:01 +0200914
915 if (!eth_get_dev()) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200916 /* No network device active, don't expose any */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100917 return EFI_SUCCESS;
Alexander Graf94c4b992016-05-06 21:01:01 +0200918 }
919
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200920 /* We only expose the "active" network device, so one is enough */
Alexander Graf94c4b992016-05-06 21:01:01 +0200921 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100922 if (!netobj)
923 goto out_of_resources;
924
925 /* Allocate an aligned transmit buffer */
926 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
927 if (!transmit_buffer)
928 goto out_of_resources;
929 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100930
Patrick Wildtfab89102020-10-07 11:04:33 +0200931 /* Allocate a number of receive buffers */
932 receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
933 sizeof(*receive_buffer));
934 if (!receive_buffer)
935 goto out_of_resources;
936 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
937 receive_buffer[i] = malloc(PKTSIZE_ALIGN);
938 if (!receive_buffer[i])
939 goto out_of_resources;
940 }
941 receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
942 sizeof(*receive_lengths));
943 if (!receive_lengths)
944 goto out_of_resources;
945
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100946 /* Hook net up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200947 efi_add_handle(&netobj->header);
Alexander Graf94c4b992016-05-06 21:01:01 +0200948
949 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200950 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100951 &netobj->net);
952 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100953 goto failure_to_add_protocol;
Adriano Cordova1738c7d2025-01-27 09:34:45 -0300954
955 if (net_dp)
956 r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
957 net_dp);
958 else
959 r = efi_net_set_dp("Net", NULL);
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100960 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100961 goto failure_to_add_protocol;
Adriano Cordova1738c7d2025-01-27 09:34:45 -0300962
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200963 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100964 &netobj->pxe);
965 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100966 goto failure_to_add_protocol;
Heinrich Schuchardtd54396a2017-10-05 16:35:57 +0200967 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf94c4b992016-05-06 21:01:01 +0200968 netobj->net.start = efi_net_start;
969 netobj->net.stop = efi_net_stop;
970 netobj->net.initialize = efi_net_initialize;
971 netobj->net.reset = efi_net_reset;
972 netobj->net.shutdown = efi_net_shutdown;
973 netobj->net.receive_filters = efi_net_receive_filters;
974 netobj->net.station_address = efi_net_station_address;
975 netobj->net.statistics = efi_net_statistics;
976 netobj->net.mcastiptomac = efi_net_mcastiptomac;
977 netobj->net.nvdata = efi_net_nvdata;
978 netobj->net.get_status = efi_net_get_status;
979 netobj->net.transmit = efi_net_transmit;
980 netobj->net.receive = efi_net_receive;
981 netobj->net.mode = &netobj->net_mode;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200982 netobj->net_mode.state = EFI_NETWORK_STOPPED;
Alexander Graf94c4b992016-05-06 21:01:01 +0200983 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardtd1cc6cb2017-10-05 16:35:58 +0200984 netobj->net_mode.hwaddr_size = ARP_HLEN;
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200985 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
Alexander Graf94c4b992016-05-06 21:01:01 +0200986 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomas27df0a72018-06-21 16:21:01 -0700987 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf94c4b992016-05-06 21:01:01 +0200988
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200989 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
990 netobj->pxe.start = efi_pxe_base_code_start;
991 netobj->pxe.stop = efi_pxe_base_code_stop;
992 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
993 netobj->pxe.discover = efi_pxe_base_code_discover;
994 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
995 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
996 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
997 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
998 netobj->pxe.arp = efi_pxe_base_code_arp;
999 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
1000 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
1001 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf94c4b992016-05-06 21:01:01 +02001002 netobj->pxe.mode = &netobj->pxe_mode;
1003 if (dhcp_ack)
1004 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
1005
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001006 /*
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001007 * Create WaitForPacket event.
1008 */
1009 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001010 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001011 &wait_for_packet);
1012 if (r != EFI_SUCCESS) {
1013 printf("ERROR: Failed to register network event\n");
1014 return r;
1015 }
1016 netobj->net.wait_for_packet = wait_for_packet;
1017 /*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001018 * Create a timer event.
1019 *
1020 * The notification function is used to check if a new network packet
1021 * has been received.
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001022 *
1023 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001024 */
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001025 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001026 efi_network_timer_notify, &netobj->net, NULL,
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001027 &network_timer_event);
1028 if (r != EFI_SUCCESS) {
1029 printf("ERROR: Failed to register network event\n");
1030 return r;
1031 }
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001032 /* Network is time critical, create event in every timer cycle */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001033 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
1034 if (r != EFI_SUCCESS) {
1035 printf("ERROR: Failed to set network timer\n");
1036 return r;
1037 }
1038
Adriano Cordova9debc902024-12-04 00:05:25 -03001039#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
1040 r = efi_ipconfig_register(&netobj->header, &netobj->ip4_config2);
1041 if (r != EFI_SUCCESS)
1042 goto failure_to_add_protocol;
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001043#endif
1044
1045#ifdef CONFIG_EFI_HTTP_PROTOCOL
1046 r = efi_http_register(&netobj->header, &netobj->http_service_binding);
1047 if (r != EFI_SUCCESS)
1048 goto failure_to_add_protocol;
Adriano Cordova9debc902024-12-04 00:05:25 -03001049#endif
1050
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001051 return EFI_SUCCESS;
1052failure_to_add_protocol:
1053 printf("ERROR: Failure to add protocol\n");
1054 return r;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001055out_of_resources:
1056 free(netobj);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +01001057 netobj = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +02001058 free(transmit_buffer);
1059 if (receive_buffer)
1060 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
1061 free(receive_buffer[i]);
1062 free(receive_buffer);
1063 free(receive_lengths);
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001064 printf("ERROR: Out of memory\n");
1065 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001066}
Adriano Cordova3c951362024-12-04 00:05:21 -03001067
1068/**
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001069 * efi_net_set_dp() - set device path of efi net device
1070 *
1071 * This gets called to update the device path when a new boot
1072 * file is downloaded
1073 *
1074 * @dev: dev to set the device path from
1075 * @server: remote server address
1076 * Return: status code
1077 */
1078efi_status_t efi_net_set_dp(const char *dev, const char *server)
1079{
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001080 efi_status_t ret = EFI_SUCCESS;
1081 struct efi_handler *phandler;
1082 struct efi_device_path *old_net_dp, *new_net_dp;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001083
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001084 old_net_dp = net_dp;
1085 new_net_dp = NULL;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001086 if (!strcmp(dev, "Net"))
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001087 new_net_dp = efi_dp_from_eth();
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001088 else if (!strcmp(dev, "Http"))
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001089 new_net_dp = efi_dp_from_http(server);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001090
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001091 if (!new_net_dp) {
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001092 return EFI_OUT_OF_RESOURCES;
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001093 }
1094
1095 // If netobj is not started yet, end here.
1096 if (!netobj) {
1097 goto exit;
1098 }
1099
1100 phandler = NULL;
1101 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
1102
1103 // If the device path protocol is not yet installed, install it
1104 if (!phandler)
1105 goto add;
1106
1107 // If it is already installed, try to update it
1108 ret = efi_reinstall_protocol_interface(&netobj->header, &efi_guid_device_path,
1109 old_net_dp, new_net_dp);
1110 if (ret != EFI_SUCCESS)
1111 goto error;
1112
1113 net_dp = new_net_dp;
1114 efi_free_pool(old_net_dp);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001115
1116 return EFI_SUCCESS;
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001117add:
1118 ret = efi_add_protocol(&netobj->header, &efi_guid_device_path,
1119 new_net_dp);
1120 if (ret != EFI_SUCCESS)
1121 goto error;
1122exit:
1123 net_dp = new_net_dp;
1124 efi_free_pool(old_net_dp);
1125
1126 return ret;
1127error:
1128 // Failed, restore
1129 efi_free_pool(new_net_dp);
1130
1131 return ret;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001132}
1133
1134/**
1135 * efi_net_get_dp() - get device path of efi net device
1136 *
1137 * Produce a copy of the current device path
1138 *
1139 * @dp: copy of the current device path, or NULL on error
1140 */
1141void efi_net_get_dp(struct efi_device_path **dp)
1142{
1143 if (!dp)
1144 return;
1145 if (!net_dp)
1146 efi_net_set_dp("Net", NULL);
1147 if (net_dp)
1148 *dp = efi_dp_dup(net_dp);
1149}
1150
1151/**
Adriano Cordova3c951362024-12-04 00:05:21 -03001152 * efi_net_get_addr() - get IP address information
1153 *
1154 * Copy the current IP address, mask, and gateway into the
1155 * efi_ipv4_address structs pointed to by ip, mask and gw,
1156 * respectively.
1157 *
1158 * @ip: pointer to an efi_ipv4_address struct to
1159 * be filled with the current IP address
1160 * @mask: pointer to an efi_ipv4_address struct to
1161 * be filled with the current network mask
1162 * @gw: pointer to an efi_ipv4_address struct to be
1163 * filled with the current network gateway
1164 */
1165void efi_net_get_addr(struct efi_ipv4_address *ip,
1166 struct efi_ipv4_address *mask,
1167 struct efi_ipv4_address *gw)
1168{
1169#ifdef CONFIG_NET_LWIP
1170 char ipstr[] = "ipaddr\0\0";
1171 char maskstr[] = "netmask\0\0";
1172 char gwstr[] = "gatewayip\0\0";
1173 int idx;
1174 struct in_addr tmp;
1175 char *env;
1176
1177 idx = dev_seq(eth_get_dev());
1178
1179 if (idx < 0 || idx > 99) {
1180 log_err("unexpected idx %d\n", idx);
1181 return;
1182 }
1183
1184 if (idx) {
1185 sprintf(ipstr, "ipaddr%d", idx);
1186 sprintf(maskstr, "netmask%d", idx);
1187 sprintf(gwstr, "gatewayip%d", idx);
1188 }
1189
1190 env = env_get(ipstr);
1191 if (env && ip) {
1192 tmp = string_to_ip(env);
1193 memcpy(ip, &tmp, sizeof(tmp));
1194 }
1195
1196 env = env_get(maskstr);
1197 if (env && mask) {
1198 tmp = string_to_ip(env);
1199 memcpy(mask, &tmp, sizeof(tmp));
1200 }
1201 env = env_get(gwstr);
1202 if (env && gw) {
1203 tmp = string_to_ip(env);
1204 memcpy(gw, &tmp, sizeof(tmp));
1205 }
1206#else
1207 if (ip)
1208 memcpy(ip, &net_ip, sizeof(net_ip));
1209 if (mask)
1210 memcpy(mask, &net_netmask, sizeof(net_netmask));
1211#endif
1212}
1213
1214/**
1215 * efi_net_set_addr() - set IP address information
1216 *
1217 * Set the current IP address, mask, and gateway to the
1218 * efi_ipv4_address structs pointed to by ip, mask and gw,
1219 * respectively.
1220 *
1221 * @ip: pointer to new IP address
1222 * @mask: pointer to new network mask to set
1223 * @gw: pointer to new network gateway
1224 */
1225void efi_net_set_addr(struct efi_ipv4_address *ip,
1226 struct efi_ipv4_address *mask,
1227 struct efi_ipv4_address *gw)
1228{
1229#ifdef CONFIG_NET_LWIP
1230 char ipstr[] = "ipaddr\0\0";
1231 char maskstr[] = "netmask\0\0";
1232 char gwstr[] = "gatewayip\0\0";
1233 int idx;
1234 struct in_addr *addr;
1235 char tmp[46];
1236
1237 idx = dev_seq(eth_get_dev());
1238
1239 if (idx < 0 || idx > 99) {
1240 log_err("unexpected idx %d\n", idx);
1241 return;
1242 }
1243
1244 if (idx) {
1245 sprintf(ipstr, "ipaddr%d", idx);
1246 sprintf(maskstr, "netmask%d", idx);
1247 sprintf(gwstr, "gatewayip%d", idx);
1248 }
1249
1250 if (ip) {
1251 addr = (struct in_addr *)ip;
1252 ip_to_string(*addr, tmp);
1253 env_set(ipstr, tmp);
1254 }
1255
1256 if (mask) {
1257 addr = (struct in_addr *)mask;
1258 ip_to_string(*addr, tmp);
1259 env_set(maskstr, tmp);
1260 }
1261
1262 if (gw) {
1263 addr = (struct in_addr *)gw;
1264 ip_to_string(*addr, tmp);
1265 env_set(gwstr, tmp);
1266 }
1267#else
1268 if (ip)
1269 memcpy(&net_ip, ip, sizeof(*ip));
1270 if (mask)
1271 memcpy(&net_netmask, mask, sizeof(*mask));
1272#endif
1273}
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001274
1275/**
1276 * efi_net_set_buffer() - allocate a buffer of min 64K
1277 *
1278 * @buffer: allocated buffer
1279 * @size: desired buffer size
1280 * Return: status code
1281 */
1282static efi_status_t efi_net_set_buffer(void **buffer, size_t size)
1283{
1284 efi_status_t ret = EFI_SUCCESS;
1285
1286 if (size < SZ_64K)
1287 size = SZ_64K;
1288
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001289 *buffer = efi_alloc(size);
1290 if (!*buffer)
1291 ret = EFI_OUT_OF_RESOURCES;
1292
1293 efi_wget_info.buffer_size = (ulong)size;
1294
1295 return ret;
1296}
1297
1298/**
1299 * efi_net_parse_headers() - parse HTTP headers
1300 *
1301 * Parses the raw buffer efi_wget_info.headers into an array headers
1302 * of efi structs http_headers. The array should be at least
1303 * MAX_HTTP_HEADERS long.
1304 *
1305 * @num_headers: number of headers
1306 * @headers: caller provided array of struct http_headers
1307 */
1308void efi_net_parse_headers(ulong *num_headers, struct http_header *headers)
1309{
1310 if (!num_headers || !headers)
1311 return;
1312
1313 // Populate info with http headers.
1314 *num_headers = 0;
1315 const uchar *line_start = efi_wget_info.headers;
1316 const uchar *line_end;
1317 ulong count;
1318 struct http_header *current_header;
1319 const uchar *separator;
1320 size_t name_length, value_length;
1321
1322 // Skip the first line (request or status line)
1323 line_end = strstr(line_start, "\r\n");
1324
1325 if (line_end)
1326 line_start = line_end + 2;
1327
1328 while ((line_end = strstr(line_start, "\r\n")) != NULL) {
1329 count = *num_headers;
1330 if (line_start == line_end || count >= MAX_HTTP_HEADERS)
1331 break;
1332 current_header = headers + count;
1333 separator = strchr(line_start, ':');
1334 if (separator) {
1335 name_length = separator - line_start;
1336 ++separator;
1337 while (*separator == ' ')
1338 ++separator;
1339 value_length = line_end - separator;
1340 if (name_length < MAX_HTTP_HEADER_NAME &&
1341 value_length < MAX_HTTP_HEADER_VALUE) {
1342 strncpy(current_header->name, line_start, name_length);
1343 current_header->name[name_length] = '\0';
1344 strncpy(current_header->value, separator, value_length);
1345 current_header->value[value_length] = '\0';
1346 (*num_headers)++;
1347 }
1348 }
1349 line_start = line_end + 2;
1350 }
1351}
1352
1353/**
1354 * efi_net_do_request() - issue an HTTP request using wget
1355 *
1356 * @url: url
1357 * @method: HTTP method
1358 * @buffer: data buffer
1359 * @status_code: HTTP status code
1360 * @file_size: file size in bytes
1361 * @headers_buffer: headers buffer
1362 * Return: status code
1363 */
1364efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, void **buffer,
1365 u32 *status_code, ulong *file_size, char *headers_buffer)
1366{
1367 efi_status_t ret = EFI_SUCCESS;
1368 int wget_ret;
1369 static bool last_head;
1370
1371 if (!buffer || !file_size)
1372 return EFI_ABORTED;
1373
1374 efi_wget_info.method = (enum wget_http_method)method;
1375 efi_wget_info.headers = headers_buffer;
1376
1377 switch (method) {
1378 case HTTP_METHOD_GET:
1379 ret = efi_net_set_buffer(buffer, last_head ? (size_t)efi_wget_info.hdr_cont_len : 0);
1380 if (ret != EFI_SUCCESS)
1381 goto out;
1382 wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info);
1383 if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) {
1384 // Try again with updated buffer size
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001385 efi_free_pool(*buffer);
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001386 ret = efi_net_set_buffer(buffer, (size_t)efi_wget_info.hdr_cont_len);
1387 if (ret != EFI_SUCCESS)
1388 goto out;
1389 if (wget_request((ulong)*buffer, url, &efi_wget_info)) {
1390 efi_free_pool(*buffer);
1391 ret = EFI_DEVICE_ERROR;
1392 goto out;
1393 }
1394 } else if (wget_ret) {
1395 efi_free_pool(*buffer);
1396 ret = EFI_DEVICE_ERROR;
1397 goto out;
1398 }
1399 // Pass the actual number of received bytes to the application
1400 *file_size = efi_wget_info.file_size;
1401 *status_code = efi_wget_info.status_code;
1402 last_head = false;
1403 break;
1404 case HTTP_METHOD_HEAD:
1405 ret = efi_net_set_buffer(buffer, 0);
1406 if (ret != EFI_SUCCESS)
1407 goto out;
1408 wget_request((ulong)*buffer, url, &efi_wget_info);
1409 *file_size = 0;
1410 *status_code = efi_wget_info.status_code;
1411 last_head = true;
1412 break;
1413 default:
1414 ret = EFI_UNSUPPORTED;
1415 break;
1416 }
1417
1418out:
1419 return ret;
1420}