blob: ce9272fa2409122ebd78a315c24d60f4b93d97a2 [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
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020027static const 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 Cordova93cba0f2024-12-04 00:05:23 -0300930 if (!net_dp)
931 efi_net_set_dp("Net", NULL);
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200932 r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
Adriano Cordova93cba0f2024-12-04 00:05:23 -0300933 net_dp);
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100934 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100935 goto failure_to_add_protocol;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200936 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100937 &netobj->pxe);
938 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100939 goto failure_to_add_protocol;
Heinrich Schuchardtd54396a2017-10-05 16:35:57 +0200940 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf94c4b992016-05-06 21:01:01 +0200941 netobj->net.start = efi_net_start;
942 netobj->net.stop = efi_net_stop;
943 netobj->net.initialize = efi_net_initialize;
944 netobj->net.reset = efi_net_reset;
945 netobj->net.shutdown = efi_net_shutdown;
946 netobj->net.receive_filters = efi_net_receive_filters;
947 netobj->net.station_address = efi_net_station_address;
948 netobj->net.statistics = efi_net_statistics;
949 netobj->net.mcastiptomac = efi_net_mcastiptomac;
950 netobj->net.nvdata = efi_net_nvdata;
951 netobj->net.get_status = efi_net_get_status;
952 netobj->net.transmit = efi_net_transmit;
953 netobj->net.receive = efi_net_receive;
954 netobj->net.mode = &netobj->net_mode;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200955 netobj->net_mode.state = EFI_NETWORK_STOPPED;
Alexander Graf94c4b992016-05-06 21:01:01 +0200956 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardtd1cc6cb2017-10-05 16:35:58 +0200957 netobj->net_mode.hwaddr_size = ARP_HLEN;
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200958 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
Alexander Graf94c4b992016-05-06 21:01:01 +0200959 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomas27df0a72018-06-21 16:21:01 -0700960 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf94c4b992016-05-06 21:01:01 +0200961
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200962 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
963 netobj->pxe.start = efi_pxe_base_code_start;
964 netobj->pxe.stop = efi_pxe_base_code_stop;
965 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
966 netobj->pxe.discover = efi_pxe_base_code_discover;
967 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
968 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
969 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
970 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
971 netobj->pxe.arp = efi_pxe_base_code_arp;
972 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
973 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
974 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf94c4b992016-05-06 21:01:01 +0200975 netobj->pxe.mode = &netobj->pxe_mode;
976 if (dhcp_ack)
977 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
978
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200979 /*
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +0200980 * Create WaitForPacket event.
981 */
982 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100983 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +0200984 &wait_for_packet);
985 if (r != EFI_SUCCESS) {
986 printf("ERROR: Failed to register network event\n");
987 return r;
988 }
989 netobj->net.wait_for_packet = wait_for_packet;
990 /*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200991 * Create a timer event.
992 *
993 * The notification function is used to check if a new network packet
994 * has been received.
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +0100995 *
996 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200997 */
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +0100998 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100999 efi_network_timer_notify, &netobj->net, NULL,
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001000 &network_timer_event);
1001 if (r != EFI_SUCCESS) {
1002 printf("ERROR: Failed to register network event\n");
1003 return r;
1004 }
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001005 /* Network is time critical, create event in every timer cycle */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001006 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
1007 if (r != EFI_SUCCESS) {
1008 printf("ERROR: Failed to set network timer\n");
1009 return r;
1010 }
1011
Adriano Cordova9debc902024-12-04 00:05:25 -03001012#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
1013 r = efi_ipconfig_register(&netobj->header, &netobj->ip4_config2);
1014 if (r != EFI_SUCCESS)
1015 goto failure_to_add_protocol;
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001016#endif
1017
1018#ifdef CONFIG_EFI_HTTP_PROTOCOL
1019 r = efi_http_register(&netobj->header, &netobj->http_service_binding);
1020 if (r != EFI_SUCCESS)
1021 goto failure_to_add_protocol;
1022 /*
1023 * No harm on doing the following. If the PXE handle is present, the client could
1024 * find it and try to get its IP address from it. In here the PXE handle is present
1025 * but the PXE protocol is not yet implmenented, so we add this in the meantime.
1026 */
1027 efi_net_get_addr((struct efi_ipv4_address *)&netobj->pxe_mode.station_ip,
1028 (struct efi_ipv4_address *)&netobj->pxe_mode.subnet_mask, NULL);
Adriano Cordova9debc902024-12-04 00:05:25 -03001029#endif
1030
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001031 return EFI_SUCCESS;
1032failure_to_add_protocol:
1033 printf("ERROR: Failure to add protocol\n");
1034 return r;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001035out_of_resources:
1036 free(netobj);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +01001037 netobj = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +02001038 free(transmit_buffer);
1039 if (receive_buffer)
1040 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
1041 free(receive_buffer[i]);
1042 free(receive_buffer);
1043 free(receive_lengths);
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001044 printf("ERROR: Out of memory\n");
1045 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001046}
Adriano Cordova3c951362024-12-04 00:05:21 -03001047
1048/**
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001049 * efi_net_set_dp() - set device path of efi net device
1050 *
1051 * This gets called to update the device path when a new boot
1052 * file is downloaded
1053 *
1054 * @dev: dev to set the device path from
1055 * @server: remote server address
1056 * Return: status code
1057 */
1058efi_status_t efi_net_set_dp(const char *dev, const char *server)
1059{
1060 efi_free_pool(net_dp);
1061
1062 net_dp = NULL;
1063 if (!strcmp(dev, "Net"))
1064 net_dp = efi_dp_from_eth();
1065 else if (!strcmp(dev, "Http"))
1066 net_dp = efi_dp_from_http(server);
1067
1068 if (!net_dp)
1069 return EFI_OUT_OF_RESOURCES;
1070
1071 return EFI_SUCCESS;
1072}
1073
1074/**
1075 * efi_net_get_dp() - get device path of efi net device
1076 *
1077 * Produce a copy of the current device path
1078 *
1079 * @dp: copy of the current device path, or NULL on error
1080 */
1081void efi_net_get_dp(struct efi_device_path **dp)
1082{
1083 if (!dp)
1084 return;
1085 if (!net_dp)
1086 efi_net_set_dp("Net", NULL);
1087 if (net_dp)
1088 *dp = efi_dp_dup(net_dp);
1089}
1090
1091/**
Adriano Cordova3c951362024-12-04 00:05:21 -03001092 * efi_net_get_addr() - get IP address information
1093 *
1094 * Copy the current IP address, mask, and gateway into the
1095 * efi_ipv4_address structs pointed to by ip, mask and gw,
1096 * respectively.
1097 *
1098 * @ip: pointer to an efi_ipv4_address struct to
1099 * be filled with the current IP address
1100 * @mask: pointer to an efi_ipv4_address struct to
1101 * be filled with the current network mask
1102 * @gw: pointer to an efi_ipv4_address struct to be
1103 * filled with the current network gateway
1104 */
1105void efi_net_get_addr(struct efi_ipv4_address *ip,
1106 struct efi_ipv4_address *mask,
1107 struct efi_ipv4_address *gw)
1108{
1109#ifdef CONFIG_NET_LWIP
1110 char ipstr[] = "ipaddr\0\0";
1111 char maskstr[] = "netmask\0\0";
1112 char gwstr[] = "gatewayip\0\0";
1113 int idx;
1114 struct in_addr tmp;
1115 char *env;
1116
1117 idx = dev_seq(eth_get_dev());
1118
1119 if (idx < 0 || idx > 99) {
1120 log_err("unexpected idx %d\n", idx);
1121 return;
1122 }
1123
1124 if (idx) {
1125 sprintf(ipstr, "ipaddr%d", idx);
1126 sprintf(maskstr, "netmask%d", idx);
1127 sprintf(gwstr, "gatewayip%d", idx);
1128 }
1129
1130 env = env_get(ipstr);
1131 if (env && ip) {
1132 tmp = string_to_ip(env);
1133 memcpy(ip, &tmp, sizeof(tmp));
1134 }
1135
1136 env = env_get(maskstr);
1137 if (env && mask) {
1138 tmp = string_to_ip(env);
1139 memcpy(mask, &tmp, sizeof(tmp));
1140 }
1141 env = env_get(gwstr);
1142 if (env && gw) {
1143 tmp = string_to_ip(env);
1144 memcpy(gw, &tmp, sizeof(tmp));
1145 }
1146#else
1147 if (ip)
1148 memcpy(ip, &net_ip, sizeof(net_ip));
1149 if (mask)
1150 memcpy(mask, &net_netmask, sizeof(net_netmask));
1151#endif
1152}
1153
1154/**
1155 * efi_net_set_addr() - set IP address information
1156 *
1157 * Set the current IP address, mask, and gateway to the
1158 * efi_ipv4_address structs pointed to by ip, mask and gw,
1159 * respectively.
1160 *
1161 * @ip: pointer to new IP address
1162 * @mask: pointer to new network mask to set
1163 * @gw: pointer to new network gateway
1164 */
1165void efi_net_set_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 *addr;
1175 char tmp[46];
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 if (ip) {
1191 addr = (struct in_addr *)ip;
1192 ip_to_string(*addr, tmp);
1193 env_set(ipstr, tmp);
1194 }
1195
1196 if (mask) {
1197 addr = (struct in_addr *)mask;
1198 ip_to_string(*addr, tmp);
1199 env_set(maskstr, tmp);
1200 }
1201
1202 if (gw) {
1203 addr = (struct in_addr *)gw;
1204 ip_to_string(*addr, tmp);
1205 env_set(gwstr, tmp);
1206 }
1207#else
1208 if (ip)
1209 memcpy(&net_ip, ip, sizeof(*ip));
1210 if (mask)
1211 memcpy(&net_netmask, mask, sizeof(*mask));
1212#endif
1213}
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001214
1215/**
1216 * efi_net_set_buffer() - allocate a buffer of min 64K
1217 *
1218 * @buffer: allocated buffer
1219 * @size: desired buffer size
1220 * Return: status code
1221 */
1222static efi_status_t efi_net_set_buffer(void **buffer, size_t size)
1223{
1224 efi_status_t ret = EFI_SUCCESS;
1225
1226 if (size < SZ_64K)
1227 size = SZ_64K;
1228
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001229 *buffer = efi_alloc(size);
1230 if (!*buffer)
1231 ret = EFI_OUT_OF_RESOURCES;
1232
1233 efi_wget_info.buffer_size = (ulong)size;
1234
1235 return ret;
1236}
1237
1238/**
1239 * efi_net_parse_headers() - parse HTTP headers
1240 *
1241 * Parses the raw buffer efi_wget_info.headers into an array headers
1242 * of efi structs http_headers. The array should be at least
1243 * MAX_HTTP_HEADERS long.
1244 *
1245 * @num_headers: number of headers
1246 * @headers: caller provided array of struct http_headers
1247 */
1248void efi_net_parse_headers(ulong *num_headers, struct http_header *headers)
1249{
1250 if (!num_headers || !headers)
1251 return;
1252
1253 // Populate info with http headers.
1254 *num_headers = 0;
1255 const uchar *line_start = efi_wget_info.headers;
1256 const uchar *line_end;
1257 ulong count;
1258 struct http_header *current_header;
1259 const uchar *separator;
1260 size_t name_length, value_length;
1261
1262 // Skip the first line (request or status line)
1263 line_end = strstr(line_start, "\r\n");
1264
1265 if (line_end)
1266 line_start = line_end + 2;
1267
1268 while ((line_end = strstr(line_start, "\r\n")) != NULL) {
1269 count = *num_headers;
1270 if (line_start == line_end || count >= MAX_HTTP_HEADERS)
1271 break;
1272 current_header = headers + count;
1273 separator = strchr(line_start, ':');
1274 if (separator) {
1275 name_length = separator - line_start;
1276 ++separator;
1277 while (*separator == ' ')
1278 ++separator;
1279 value_length = line_end - separator;
1280 if (name_length < MAX_HTTP_HEADER_NAME &&
1281 value_length < MAX_HTTP_HEADER_VALUE) {
1282 strncpy(current_header->name, line_start, name_length);
1283 current_header->name[name_length] = '\0';
1284 strncpy(current_header->value, separator, value_length);
1285 current_header->value[value_length] = '\0';
1286 (*num_headers)++;
1287 }
1288 }
1289 line_start = line_end + 2;
1290 }
1291}
1292
1293/**
1294 * efi_net_do_request() - issue an HTTP request using wget
1295 *
1296 * @url: url
1297 * @method: HTTP method
1298 * @buffer: data buffer
1299 * @status_code: HTTP status code
1300 * @file_size: file size in bytes
1301 * @headers_buffer: headers buffer
1302 * Return: status code
1303 */
1304efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, void **buffer,
1305 u32 *status_code, ulong *file_size, char *headers_buffer)
1306{
1307 efi_status_t ret = EFI_SUCCESS;
1308 int wget_ret;
1309 static bool last_head;
1310
1311 if (!buffer || !file_size)
1312 return EFI_ABORTED;
1313
1314 efi_wget_info.method = (enum wget_http_method)method;
1315 efi_wget_info.headers = headers_buffer;
1316
1317 switch (method) {
1318 case HTTP_METHOD_GET:
1319 ret = efi_net_set_buffer(buffer, last_head ? (size_t)efi_wget_info.hdr_cont_len : 0);
1320 if (ret != EFI_SUCCESS)
1321 goto out;
1322 wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info);
1323 if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) {
1324 // Try again with updated buffer size
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001325 efi_free_pool(*buffer);
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001326 ret = efi_net_set_buffer(buffer, (size_t)efi_wget_info.hdr_cont_len);
1327 if (ret != EFI_SUCCESS)
1328 goto out;
1329 if (wget_request((ulong)*buffer, url, &efi_wget_info)) {
1330 efi_free_pool(*buffer);
1331 ret = EFI_DEVICE_ERROR;
1332 goto out;
1333 }
1334 } else if (wget_ret) {
1335 efi_free_pool(*buffer);
1336 ret = EFI_DEVICE_ERROR;
1337 goto out;
1338 }
1339 // Pass the actual number of received bytes to the application
1340 *file_size = efi_wget_info.file_size;
1341 *status_code = efi_wget_info.status_code;
1342 last_head = false;
1343 break;
1344 case HTTP_METHOD_HEAD:
1345 ret = efi_net_set_buffer(buffer, 0);
1346 if (ret != EFI_SUCCESS)
1347 goto out;
1348 wget_request((ulong)*buffer, url, &efi_wget_info);
1349 *file_size = 0;
1350 *status_code = efi_wget_info.status_code;
1351 last_head = true;
1352 break;
1353 default:
1354 ret = EFI_UNSUPPORTED;
1355 break;
1356 }
1357
1358out:
1359 return ret;
1360}