blob: afca2000e6ff71638c0e765a03c1ebed758f70f7 [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/**
882 * efi_net_register() - register the simple network protocol
883 *
884 * This gets called from do_bootefi_exec().
885 */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100886efi_status_t efi_net_register(void)
Alexander Graf94c4b992016-05-06 21:01:01 +0200887{
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200888 efi_status_t r;
Patrick Wildtfab89102020-10-07 11:04:33 +0200889 int i;
Alexander Graf94c4b992016-05-06 21:01:01 +0200890
891 if (!eth_get_dev()) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200892 /* No network device active, don't expose any */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100893 return EFI_SUCCESS;
Alexander Graf94c4b992016-05-06 21:01:01 +0200894 }
895
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200896 /* We only expose the "active" network device, so one is enough */
Alexander Graf94c4b992016-05-06 21:01:01 +0200897 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100898 if (!netobj)
899 goto out_of_resources;
900
901 /* Allocate an aligned transmit buffer */
902 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
903 if (!transmit_buffer)
904 goto out_of_resources;
905 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100906
Patrick Wildtfab89102020-10-07 11:04:33 +0200907 /* Allocate a number of receive buffers */
908 receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
909 sizeof(*receive_buffer));
910 if (!receive_buffer)
911 goto out_of_resources;
912 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
913 receive_buffer[i] = malloc(PKTSIZE_ALIGN);
914 if (!receive_buffer[i])
915 goto out_of_resources;
916 }
917 receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
918 sizeof(*receive_lengths));
919 if (!receive_lengths)
920 goto out_of_resources;
921
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100922 /* Hook net up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200923 efi_add_handle(&netobj->header);
Alexander Graf94c4b992016-05-06 21:01:01 +0200924
925 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200926 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100927 &netobj->net);
928 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100929 goto failure_to_add_protocol;
Adriano Cordova1738c7d2025-01-27 09:34:45 -0300930
931 if (net_dp)
932 r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
933 net_dp);
934 else
935 r = efi_net_set_dp("Net", NULL);
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100936 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100937 goto failure_to_add_protocol;
Adriano Cordova1738c7d2025-01-27 09:34:45 -0300938
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200939 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100940 &netobj->pxe);
941 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100942 goto failure_to_add_protocol;
Heinrich Schuchardtd54396a2017-10-05 16:35:57 +0200943 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf94c4b992016-05-06 21:01:01 +0200944 netobj->net.start = efi_net_start;
945 netobj->net.stop = efi_net_stop;
946 netobj->net.initialize = efi_net_initialize;
947 netobj->net.reset = efi_net_reset;
948 netobj->net.shutdown = efi_net_shutdown;
949 netobj->net.receive_filters = efi_net_receive_filters;
950 netobj->net.station_address = efi_net_station_address;
951 netobj->net.statistics = efi_net_statistics;
952 netobj->net.mcastiptomac = efi_net_mcastiptomac;
953 netobj->net.nvdata = efi_net_nvdata;
954 netobj->net.get_status = efi_net_get_status;
955 netobj->net.transmit = efi_net_transmit;
956 netobj->net.receive = efi_net_receive;
957 netobj->net.mode = &netobj->net_mode;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200958 netobj->net_mode.state = EFI_NETWORK_STOPPED;
Alexander Graf94c4b992016-05-06 21:01:01 +0200959 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardtd1cc6cb2017-10-05 16:35:58 +0200960 netobj->net_mode.hwaddr_size = ARP_HLEN;
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200961 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
Alexander Graf94c4b992016-05-06 21:01:01 +0200962 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomas27df0a72018-06-21 16:21:01 -0700963 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf94c4b992016-05-06 21:01:01 +0200964
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200965 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
966 netobj->pxe.start = efi_pxe_base_code_start;
967 netobj->pxe.stop = efi_pxe_base_code_stop;
968 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
969 netobj->pxe.discover = efi_pxe_base_code_discover;
970 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
971 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
972 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
973 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
974 netobj->pxe.arp = efi_pxe_base_code_arp;
975 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
976 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
977 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf94c4b992016-05-06 21:01:01 +0200978 netobj->pxe.mode = &netobj->pxe_mode;
979 if (dhcp_ack)
980 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
981
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200982 /*
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +0200983 * Create WaitForPacket event.
984 */
985 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100986 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +0200987 &wait_for_packet);
988 if (r != EFI_SUCCESS) {
989 printf("ERROR: Failed to register network event\n");
990 return r;
991 }
992 netobj->net.wait_for_packet = wait_for_packet;
993 /*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200994 * Create a timer event.
995 *
996 * The notification function is used to check if a new network packet
997 * has been received.
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +0100998 *
999 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001000 */
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001001 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001002 efi_network_timer_notify, &netobj->net, NULL,
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001003 &network_timer_event);
1004 if (r != EFI_SUCCESS) {
1005 printf("ERROR: Failed to register network event\n");
1006 return r;
1007 }
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001008 /* Network is time critical, create event in every timer cycle */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001009 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
1010 if (r != EFI_SUCCESS) {
1011 printf("ERROR: Failed to set network timer\n");
1012 return r;
1013 }
1014
Adriano Cordova9debc902024-12-04 00:05:25 -03001015#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
1016 r = efi_ipconfig_register(&netobj->header, &netobj->ip4_config2);
1017 if (r != EFI_SUCCESS)
1018 goto failure_to_add_protocol;
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001019#endif
1020
1021#ifdef CONFIG_EFI_HTTP_PROTOCOL
1022 r = efi_http_register(&netobj->header, &netobj->http_service_binding);
1023 if (r != EFI_SUCCESS)
1024 goto failure_to_add_protocol;
1025 /*
1026 * No harm on doing the following. If the PXE handle is present, the client could
1027 * find it and try to get its IP address from it. In here the PXE handle is present
1028 * but the PXE protocol is not yet implmenented, so we add this in the meantime.
1029 */
1030 efi_net_get_addr((struct efi_ipv4_address *)&netobj->pxe_mode.station_ip,
1031 (struct efi_ipv4_address *)&netobj->pxe_mode.subnet_mask, NULL);
Adriano Cordova9debc902024-12-04 00:05:25 -03001032#endif
1033
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001034 return EFI_SUCCESS;
1035failure_to_add_protocol:
1036 printf("ERROR: Failure to add protocol\n");
1037 return r;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001038out_of_resources:
1039 free(netobj);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +01001040 netobj = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +02001041 free(transmit_buffer);
1042 if (receive_buffer)
1043 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
1044 free(receive_buffer[i]);
1045 free(receive_buffer);
1046 free(receive_lengths);
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001047 printf("ERROR: Out of memory\n");
1048 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001049}
Adriano Cordova3c951362024-12-04 00:05:21 -03001050
1051/**
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001052 * efi_net_set_dp() - set device path of efi net device
1053 *
1054 * This gets called to update the device path when a new boot
1055 * file is downloaded
1056 *
1057 * @dev: dev to set the device path from
1058 * @server: remote server address
1059 * Return: status code
1060 */
1061efi_status_t efi_net_set_dp(const char *dev, const char *server)
1062{
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001063 efi_status_t ret = EFI_SUCCESS;
1064 struct efi_handler *phandler;
1065 struct efi_device_path *old_net_dp, *new_net_dp;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001066
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001067 old_net_dp = net_dp;
1068 new_net_dp = NULL;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001069 if (!strcmp(dev, "Net"))
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001070 new_net_dp = efi_dp_from_eth();
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001071 else if (!strcmp(dev, "Http"))
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001072 new_net_dp = efi_dp_from_http(server);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001073
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001074 if (!new_net_dp) {
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001075 return EFI_OUT_OF_RESOURCES;
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001076 }
1077
1078 // If netobj is not started yet, end here.
1079 if (!netobj) {
1080 goto exit;
1081 }
1082
1083 phandler = NULL;
1084 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
1085
1086 // If the device path protocol is not yet installed, install it
1087 if (!phandler)
1088 goto add;
1089
1090 // If it is already installed, try to update it
1091 ret = efi_reinstall_protocol_interface(&netobj->header, &efi_guid_device_path,
1092 old_net_dp, new_net_dp);
1093 if (ret != EFI_SUCCESS)
1094 goto error;
1095
1096 net_dp = new_net_dp;
1097 efi_free_pool(old_net_dp);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001098
1099 return EFI_SUCCESS;
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001100add:
1101 ret = efi_add_protocol(&netobj->header, &efi_guid_device_path,
1102 new_net_dp);
1103 if (ret != EFI_SUCCESS)
1104 goto error;
1105exit:
1106 net_dp = new_net_dp;
1107 efi_free_pool(old_net_dp);
1108
1109 return ret;
1110error:
1111 // Failed, restore
1112 efi_free_pool(new_net_dp);
1113
1114 return ret;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001115}
1116
1117/**
1118 * efi_net_get_dp() - get device path of efi net device
1119 *
1120 * Produce a copy of the current device path
1121 *
1122 * @dp: copy of the current device path, or NULL on error
1123 */
1124void efi_net_get_dp(struct efi_device_path **dp)
1125{
1126 if (!dp)
1127 return;
1128 if (!net_dp)
1129 efi_net_set_dp("Net", NULL);
1130 if (net_dp)
1131 *dp = efi_dp_dup(net_dp);
1132}
1133
1134/**
Adriano Cordova3c951362024-12-04 00:05:21 -03001135 * efi_net_get_addr() - get IP address information
1136 *
1137 * Copy the current IP address, mask, and gateway into the
1138 * efi_ipv4_address structs pointed to by ip, mask and gw,
1139 * respectively.
1140 *
1141 * @ip: pointer to an efi_ipv4_address struct to
1142 * be filled with the current IP address
1143 * @mask: pointer to an efi_ipv4_address struct to
1144 * be filled with the current network mask
1145 * @gw: pointer to an efi_ipv4_address struct to be
1146 * filled with the current network gateway
1147 */
1148void efi_net_get_addr(struct efi_ipv4_address *ip,
1149 struct efi_ipv4_address *mask,
1150 struct efi_ipv4_address *gw)
1151{
1152#ifdef CONFIG_NET_LWIP
1153 char ipstr[] = "ipaddr\0\0";
1154 char maskstr[] = "netmask\0\0";
1155 char gwstr[] = "gatewayip\0\0";
1156 int idx;
1157 struct in_addr tmp;
1158 char *env;
1159
1160 idx = dev_seq(eth_get_dev());
1161
1162 if (idx < 0 || idx > 99) {
1163 log_err("unexpected idx %d\n", idx);
1164 return;
1165 }
1166
1167 if (idx) {
1168 sprintf(ipstr, "ipaddr%d", idx);
1169 sprintf(maskstr, "netmask%d", idx);
1170 sprintf(gwstr, "gatewayip%d", idx);
1171 }
1172
1173 env = env_get(ipstr);
1174 if (env && ip) {
1175 tmp = string_to_ip(env);
1176 memcpy(ip, &tmp, sizeof(tmp));
1177 }
1178
1179 env = env_get(maskstr);
1180 if (env && mask) {
1181 tmp = string_to_ip(env);
1182 memcpy(mask, &tmp, sizeof(tmp));
1183 }
1184 env = env_get(gwstr);
1185 if (env && gw) {
1186 tmp = string_to_ip(env);
1187 memcpy(gw, &tmp, sizeof(tmp));
1188 }
1189#else
1190 if (ip)
1191 memcpy(ip, &net_ip, sizeof(net_ip));
1192 if (mask)
1193 memcpy(mask, &net_netmask, sizeof(net_netmask));
1194#endif
1195}
1196
1197/**
1198 * efi_net_set_addr() - set IP address information
1199 *
1200 * Set the current IP address, mask, and gateway to the
1201 * efi_ipv4_address structs pointed to by ip, mask and gw,
1202 * respectively.
1203 *
1204 * @ip: pointer to new IP address
1205 * @mask: pointer to new network mask to set
1206 * @gw: pointer to new network gateway
1207 */
1208void efi_net_set_addr(struct efi_ipv4_address *ip,
1209 struct efi_ipv4_address *mask,
1210 struct efi_ipv4_address *gw)
1211{
1212#ifdef CONFIG_NET_LWIP
1213 char ipstr[] = "ipaddr\0\0";
1214 char maskstr[] = "netmask\0\0";
1215 char gwstr[] = "gatewayip\0\0";
1216 int idx;
1217 struct in_addr *addr;
1218 char tmp[46];
1219
1220 idx = dev_seq(eth_get_dev());
1221
1222 if (idx < 0 || idx > 99) {
1223 log_err("unexpected idx %d\n", idx);
1224 return;
1225 }
1226
1227 if (idx) {
1228 sprintf(ipstr, "ipaddr%d", idx);
1229 sprintf(maskstr, "netmask%d", idx);
1230 sprintf(gwstr, "gatewayip%d", idx);
1231 }
1232
1233 if (ip) {
1234 addr = (struct in_addr *)ip;
1235 ip_to_string(*addr, tmp);
1236 env_set(ipstr, tmp);
1237 }
1238
1239 if (mask) {
1240 addr = (struct in_addr *)mask;
1241 ip_to_string(*addr, tmp);
1242 env_set(maskstr, tmp);
1243 }
1244
1245 if (gw) {
1246 addr = (struct in_addr *)gw;
1247 ip_to_string(*addr, tmp);
1248 env_set(gwstr, tmp);
1249 }
1250#else
1251 if (ip)
1252 memcpy(&net_ip, ip, sizeof(*ip));
1253 if (mask)
1254 memcpy(&net_netmask, mask, sizeof(*mask));
1255#endif
1256}
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001257
1258/**
1259 * efi_net_set_buffer() - allocate a buffer of min 64K
1260 *
1261 * @buffer: allocated buffer
1262 * @size: desired buffer size
1263 * Return: status code
1264 */
1265static efi_status_t efi_net_set_buffer(void **buffer, size_t size)
1266{
1267 efi_status_t ret = EFI_SUCCESS;
1268
1269 if (size < SZ_64K)
1270 size = SZ_64K;
1271
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001272 *buffer = efi_alloc(size);
1273 if (!*buffer)
1274 ret = EFI_OUT_OF_RESOURCES;
1275
1276 efi_wget_info.buffer_size = (ulong)size;
1277
1278 return ret;
1279}
1280
1281/**
1282 * efi_net_parse_headers() - parse HTTP headers
1283 *
1284 * Parses the raw buffer efi_wget_info.headers into an array headers
1285 * of efi structs http_headers. The array should be at least
1286 * MAX_HTTP_HEADERS long.
1287 *
1288 * @num_headers: number of headers
1289 * @headers: caller provided array of struct http_headers
1290 */
1291void efi_net_parse_headers(ulong *num_headers, struct http_header *headers)
1292{
1293 if (!num_headers || !headers)
1294 return;
1295
1296 // Populate info with http headers.
1297 *num_headers = 0;
1298 const uchar *line_start = efi_wget_info.headers;
1299 const uchar *line_end;
1300 ulong count;
1301 struct http_header *current_header;
1302 const uchar *separator;
1303 size_t name_length, value_length;
1304
1305 // Skip the first line (request or status line)
1306 line_end = strstr(line_start, "\r\n");
1307
1308 if (line_end)
1309 line_start = line_end + 2;
1310
1311 while ((line_end = strstr(line_start, "\r\n")) != NULL) {
1312 count = *num_headers;
1313 if (line_start == line_end || count >= MAX_HTTP_HEADERS)
1314 break;
1315 current_header = headers + count;
1316 separator = strchr(line_start, ':');
1317 if (separator) {
1318 name_length = separator - line_start;
1319 ++separator;
1320 while (*separator == ' ')
1321 ++separator;
1322 value_length = line_end - separator;
1323 if (name_length < MAX_HTTP_HEADER_NAME &&
1324 value_length < MAX_HTTP_HEADER_VALUE) {
1325 strncpy(current_header->name, line_start, name_length);
1326 current_header->name[name_length] = '\0';
1327 strncpy(current_header->value, separator, value_length);
1328 current_header->value[value_length] = '\0';
1329 (*num_headers)++;
1330 }
1331 }
1332 line_start = line_end + 2;
1333 }
1334}
1335
1336/**
1337 * efi_net_do_request() - issue an HTTP request using wget
1338 *
1339 * @url: url
1340 * @method: HTTP method
1341 * @buffer: data buffer
1342 * @status_code: HTTP status code
1343 * @file_size: file size in bytes
1344 * @headers_buffer: headers buffer
1345 * Return: status code
1346 */
1347efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, void **buffer,
1348 u32 *status_code, ulong *file_size, char *headers_buffer)
1349{
1350 efi_status_t ret = EFI_SUCCESS;
1351 int wget_ret;
1352 static bool last_head;
1353
1354 if (!buffer || !file_size)
1355 return EFI_ABORTED;
1356
1357 efi_wget_info.method = (enum wget_http_method)method;
1358 efi_wget_info.headers = headers_buffer;
1359
1360 switch (method) {
1361 case HTTP_METHOD_GET:
1362 ret = efi_net_set_buffer(buffer, last_head ? (size_t)efi_wget_info.hdr_cont_len : 0);
1363 if (ret != EFI_SUCCESS)
1364 goto out;
1365 wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info);
1366 if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) {
1367 // Try again with updated buffer size
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001368 efi_free_pool(*buffer);
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001369 ret = efi_net_set_buffer(buffer, (size_t)efi_wget_info.hdr_cont_len);
1370 if (ret != EFI_SUCCESS)
1371 goto out;
1372 if (wget_request((ulong)*buffer, url, &efi_wget_info)) {
1373 efi_free_pool(*buffer);
1374 ret = EFI_DEVICE_ERROR;
1375 goto out;
1376 }
1377 } else if (wget_ret) {
1378 efi_free_pool(*buffer);
1379 ret = EFI_DEVICE_ERROR;
1380 goto out;
1381 }
1382 // Pass the actual number of received bytes to the application
1383 *file_size = efi_wget_info.file_size;
1384 *status_code = efi_wget_info.status_code;
1385 last_head = false;
1386 break;
1387 case HTTP_METHOD_HEAD:
1388 ret = efi_net_set_buffer(buffer, 0);
1389 if (ret != EFI_SUCCESS)
1390 goto out;
1391 wget_request((ulong)*buffer, url, &efi_wget_info);
1392 *file_size = 0;
1393 *status_code = efi_wget_info.status_code;
1394 last_head = true;
1395 break;
1396 default:
1397 ret = EFI_UNSUPPORTED;
1398 break;
1399 }
1400
1401out:
1402 return ret;
1403}