blob: 67593ef50c0dfb429dda78aa63314348baa35bf9 [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
Alexander Graf94c4b992016-05-06 21:01:01 +020018#include <efi_loader.h>
Adriano Cordova93cba0f2024-12-04 00:05:23 -030019#include <dm.h>
Adriano Cordova0d1f5092024-12-04 00:05:24 -030020#include <linux/sizes.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020021#include <malloc.h>
Adriano Cordova3c951362024-12-04 00:05:21 -030022#include <vsprintf.h>
Simon Glass274e0b02020-05-10 11:39:56 -060023#include <net.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020024
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020025static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020026static const efi_guid_t efi_pxe_base_code_protocol_guid =
27 EFI_PXE_BASE_CODE_PROTOCOL_GUID;
Alexander Graf94c4b992016-05-06 21:01:01 +020028static struct efi_pxe_packet *dhcp_ack;
Alexander Graf94c4b992016-05-06 21:01:01 +020029static void *new_tx_packet;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010030static void *transmit_buffer;
Patrick Wildtfab89102020-10-07 11:04:33 +020031static uchar **receive_buffer;
32static size_t *receive_lengths;
33static int rx_packet_idx;
34static int rx_packet_num;
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +010035static struct efi_net_obj *netobj;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010036
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020037/*
Adriano Cordova93cba0f2024-12-04 00:05:23 -030038 * The current network device path. This device path is updated when a new
39 * bootfile is downloaded from the network. If then the bootfile is loaded
40 * as an efi image, net_dp is passed as the device path of the loaded image.
41 */
42static struct efi_device_path *net_dp;
43
Adriano Cordova0d1f5092024-12-04 00:05:24 -030044static struct wget_http_info efi_wget_info = {
45 .set_bootdev = false,
46 .check_buffer_size = true,
47
48};
49
Adriano Cordova93cba0f2024-12-04 00:05:23 -030050/*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020051 * The notification function of this event is called in every timer cycle
52 * to check if a new network packet has been received.
53 */
54static struct efi_event *network_timer_event;
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +020055/*
56 * This event is signaled when a packet has been received.
57 */
58static struct efi_event *wait_for_packet;
Alexander Graf94c4b992016-05-06 21:01:01 +020059
Heinrich Schuchardt72928722018-09-26 05:27:56 +020060/**
61 * struct efi_net_obj - EFI object representing a network interface
62 *
Adriano Cordova9debc902024-12-04 00:05:25 -030063 * @header: EFI object header
64 * @net: simple network protocol interface
65 * @net_mode: status of the network interface
66 * @pxe: PXE base code protocol interface
67 * @pxe_mode: status of the PXE base code protocol
68 * @ip4_config2: IP4 Config2 protocol interface
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030069 * @http_service_binding: Http service binding protocol interface
Heinrich Schuchardt72928722018-09-26 05:27:56 +020070 */
Alexander Graf94c4b992016-05-06 21:01:01 +020071struct efi_net_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020072 struct efi_object header;
Alexander Graf94c4b992016-05-06 21:01:01 +020073 struct efi_simple_network net;
74 struct efi_simple_network_mode net_mode;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020075 struct efi_pxe_base_code_protocol pxe;
Alexander Graf94c4b992016-05-06 21:01:01 +020076 struct efi_pxe_mode pxe_mode;
Adriano Cordova9debc902024-12-04 00:05:25 -030077#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
78 struct efi_ip4_config2_protocol ip4_config2;
79#endif
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030080#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
81 struct efi_service_binding_protocol http_service_binding;
82#endif
Alexander Graf94c4b992016-05-06 21:01:01 +020083};
84
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010085/*
86 * efi_net_start() - start the network interface
87 *
88 * This function implements the Start service of the
89 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
90 * (UEFI) specification for details.
91 *
92 * @this: pointer to the protocol instance
93 * Return: status code
94 */
Alexander Graf94c4b992016-05-06 21:01:01 +020095static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
96{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010097 efi_status_t ret = EFI_SUCCESS;
98
Alexander Graf94c4b992016-05-06 21:01:01 +020099 EFI_ENTRY("%p", this);
100
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100101 /* Check parameters */
102 if (!this) {
103 ret = EFI_INVALID_PARAMETER;
104 goto out;
105 }
106
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200107 if (this->mode->state != EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100108 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200109 } else {
110 this->int_status = 0;
111 wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100112 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200113 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100114out:
115 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200116}
117
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100118/*
119 * efi_net_stop() - stop the network interface
120 *
121 * This function implements the Stop service of the
122 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
123 * (UEFI) specification for details.
124 *
125 * @this: pointer to the protocol instance
126 * Return: status code
127 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200128static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
129{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100130 efi_status_t ret = EFI_SUCCESS;
131
Alexander Graf94c4b992016-05-06 21:01:01 +0200132 EFI_ENTRY("%p", this);
133
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100134 /* Check parameters */
135 if (!this) {
136 ret = EFI_INVALID_PARAMETER;
137 goto out;
138 }
139
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200140 if (this->mode->state == EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100141 ret = EFI_NOT_STARTED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200142 } else {
143 /* Disable hardware and put it into the reset state */
144 eth_halt();
Patrick Wildtfab89102020-10-07 11:04:33 +0200145 /* Clear cache of packets */
146 rx_packet_num = 0;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100147 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200148 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100149out:
150 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200151}
152
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200153/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100154 * efi_net_initialize() - initialize the network interface
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200155 *
156 * This function implements the Initialize service of the
157 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
158 * (UEFI) specification for details.
159 *
160 * @this: pointer to the protocol instance
161 * @extra_rx: extra receive buffer to be allocated
162 * @extra_tx: extra transmit buffer to be allocated
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100163 * Return: status code
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200164 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200165static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
166 ulong extra_rx, ulong extra_tx)
167{
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200168 int ret;
169 efi_status_t r = EFI_SUCCESS;
170
Alexander Graf94c4b992016-05-06 21:01:01 +0200171 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
172
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100173 /* Check parameters */
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200174 if (!this) {
175 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100176 goto out;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200177 }
178
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200179 switch (this->mode->state) {
180 case EFI_NETWORK_INITIALIZED:
181 case EFI_NETWORK_STARTED:
182 break;
183 default:
184 r = EFI_NOT_STARTED;
185 goto out;
186 }
187
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200188 /* Setup packet buffers */
189 net_init();
190 /* Disable hardware and put it into the reset state */
191 eth_halt();
Patrick Wildtfab89102020-10-07 11:04:33 +0200192 /* Clear cache of packets */
193 rx_packet_num = 0;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200194 /* Set current device according to environment variables */
195 eth_set_current();
196 /* Get hardware ready for send and receive operations */
197 ret = eth_init();
198 if (ret < 0) {
199 eth_halt();
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100200 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200201 r = EFI_DEVICE_ERROR;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100202 goto out;
203 } else {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200204 this->int_status = 0;
205 wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100206 this->mode->state = EFI_NETWORK_INITIALIZED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200207 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100208out:
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200209 return EFI_EXIT(r);
Alexander Graf94c4b992016-05-06 21:01:01 +0200210}
211
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100212/*
213 * efi_net_reset() - reinitialize the network interface
214 *
215 * This function implements the Reset service of the
216 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
217 * (UEFI) specification for details.
218 *
219 * @this: pointer to the protocol instance
220 * @extended_verification: execute exhaustive verification
221 * Return: status code
222 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200223static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
224 int extended_verification)
225{
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200226 efi_status_t ret;
227
Alexander Graf94c4b992016-05-06 21:01:01 +0200228 EFI_ENTRY("%p, %x", this, extended_verification);
229
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200230 /* Check parameters */
231 if (!this) {
232 ret = EFI_INVALID_PARAMETER;
233 goto out;
234 }
235
236 switch (this->mode->state) {
237 case EFI_NETWORK_INITIALIZED:
238 break;
239 case EFI_NETWORK_STOPPED:
240 ret = EFI_NOT_STARTED;
241 goto out;
242 default:
243 ret = EFI_DEVICE_ERROR;
244 goto out;
245 }
246
247 this->mode->state = EFI_NETWORK_STARTED;
248 ret = EFI_CALL(efi_net_initialize(this, 0, 0));
249out:
250 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200251}
252
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100253/*
254 * efi_net_shutdown() - shut down the network interface
255 *
256 * This function implements the Shutdown service of the
257 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
258 * (UEFI) specification for details.
259 *
260 * @this: pointer to the protocol instance
261 * Return: status code
262 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200263static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
264{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100265 efi_status_t ret = EFI_SUCCESS;
266
Alexander Graf94c4b992016-05-06 21:01:01 +0200267 EFI_ENTRY("%p", this);
268
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100269 /* Check parameters */
270 if (!this) {
271 ret = EFI_INVALID_PARAMETER;
272 goto out;
273 }
274
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200275 switch (this->mode->state) {
276 case EFI_NETWORK_INITIALIZED:
277 break;
278 case EFI_NETWORK_STOPPED:
279 ret = EFI_NOT_STARTED;
280 goto out;
281 default:
282 ret = EFI_DEVICE_ERROR;
283 goto out;
284 }
285
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100286 eth_halt();
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200287 this->int_status = 0;
288 wait_for_packet->is_signaled = false;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200289 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100290
291out:
292 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200293}
294
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100295/*
296 * efi_net_receive_filters() - mange multicast receive filters
297 *
298 * This function implements the ReceiveFilters service of the
299 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
300 * (UEFI) specification for details.
301 *
302 * @this: pointer to the protocol instance
303 * @enable: bit mask of receive filters to enable
304 * @disable: bit mask of receive filters to disable
305 * @reset_mcast_filter: true resets contents of the filters
306 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
307 * @mcast_filter: list of new filters
308 * Return: status code
309 */
310static efi_status_t EFIAPI efi_net_receive_filters
311 (struct efi_simple_network *this, u32 enable, u32 disable,
312 int reset_mcast_filter, ulong mcast_filter_count,
313 struct efi_mac_address *mcast_filter)
Alexander Graf94c4b992016-05-06 21:01:01 +0200314{
315 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
316 reset_mcast_filter, mcast_filter_count, mcast_filter);
317
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200318 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200319}
320
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100321/*
322 * efi_net_station_address() - set the hardware MAC address
323 *
324 * This function implements the StationAddress service of the
325 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
326 * (UEFI) specification for details.
327 *
328 * @this: pointer to the protocol instance
329 * @reset: if true reset the address to default
330 * @new_mac: new MAC address
331 * Return: status code
332 */
333static efi_status_t EFIAPI efi_net_station_address
334 (struct efi_simple_network *this, int reset,
335 struct efi_mac_address *new_mac)
Alexander Graf94c4b992016-05-06 21:01:01 +0200336{
337 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
338
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200339 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200340}
341
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100342/*
343 * efi_net_statistics() - reset or collect statistics of the network interface
344 *
345 * This function implements the Statistics service of the
346 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
347 * (UEFI) specification for details.
348 *
349 * @this: pointer to the protocol instance
350 * @reset: if true, the statistics are reset
351 * @stat_size: size of the statistics table
352 * @stat_table: table to receive the statistics
353 * Return: status code
354 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200355static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
356 int reset, ulong *stat_size,
357 void *stat_table)
358{
359 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
360
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200361 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200362}
363
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100364/*
365 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
366 *
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200367 * This function implements the MCastIPtoMAC service of the
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100368 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
369 * (UEFI) specification for details.
370 *
371 * @this: pointer to the protocol instance
372 * @ipv6: true if the IP address is an IPv6 address
373 * @ip: IP address
374 * @mac: MAC address
375 * Return: status code
376 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200377static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
378 int ipv6,
379 struct efi_ip_address *ip,
380 struct efi_mac_address *mac)
381{
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200382 efi_status_t ret = EFI_SUCCESS;
383
Alexander Graf94c4b992016-05-06 21:01:01 +0200384 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
385
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200386 if (!this || !ip || !mac) {
387 ret = EFI_INVALID_PARAMETER;
388 goto out;
389 }
390
391 if (ipv6) {
392 ret = EFI_UNSUPPORTED;
393 goto out;
394 }
395
396 /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */
397 if ((ip->ip_addr[0] & 0xf0) != 0xe0) {
398 ret = EFI_INVALID_PARAMETER;
399 goto out;
400 };
401
402 switch (this->mode->state) {
403 case EFI_NETWORK_INITIALIZED:
404 case EFI_NETWORK_STARTED:
405 break;
406 default:
407 ret = EFI_NOT_STARTED;
408 goto out;
409 }
410
411 memset(mac, 0, sizeof(struct efi_mac_address));
412
413 /*
414 * Copy lower 23 bits of IPv4 multi-cast address
415 * RFC 1112, RFC 7042 2.1.1.
416 */
417 mac->mac_addr[0] = 0x01;
418 mac->mac_addr[1] = 0x00;
419 mac->mac_addr[2] = 0x5E;
420 mac->mac_addr[3] = ip->ip_addr[1] & 0x7F;
421 mac->mac_addr[4] = ip->ip_addr[2];
422 mac->mac_addr[5] = ip->ip_addr[3];
423out:
424 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200425}
426
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100427/**
428 * efi_net_nvdata() - read or write NVRAM
429 *
430 * This function implements the GetStatus service of the Simple Network
431 * Protocol. See the UEFI spec for details.
432 *
433 * @this: the instance of the Simple Network Protocol
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200434 * @read_write: true for read, false for write
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100435 * @offset: offset in NVRAM
436 * @buffer_size: size of buffer
437 * @buffer: buffer
438 * Return: status code
439 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200440static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
441 int read_write, ulong offset,
442 ulong buffer_size, char *buffer)
443{
444 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
445 buffer);
446
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200447 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200448}
449
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100450/**
451 * efi_net_get_status() - get interrupt status
452 *
453 * This function implements the GetStatus service of the Simple Network
454 * Protocol. See the UEFI spec for details.
455 *
456 * @this: the instance of the Simple Network Protocol
457 * @int_status: interface status
458 * @txbuf: transmission buffer
459 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200460static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
461 u32 *int_status, void **txbuf)
462{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100463 efi_status_t ret = EFI_SUCCESS;
464
Alexander Graf94c4b992016-05-06 21:01:01 +0200465 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
466
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200467 efi_timer_check();
468
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100469 /* Check parameters */
470 if (!this) {
471 ret = EFI_INVALID_PARAMETER;
472 goto out;
473 }
474
475 switch (this->mode->state) {
476 case EFI_NETWORK_STOPPED:
477 ret = EFI_NOT_STARTED;
478 goto out;
479 case EFI_NETWORK_STARTED:
480 ret = EFI_DEVICE_ERROR;
481 goto out;
482 default:
483 break;
484 }
485
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200486 if (int_status) {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200487 *int_status = this->int_status;
488 this->int_status = 0;
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200489 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200490 if (txbuf)
491 *txbuf = new_tx_packet;
492
493 new_tx_packet = NULL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100494out:
495 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200496}
497
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100498/**
499 * efi_net_transmit() - transmit a packet
500 *
501 * This function implements the Transmit service of the Simple Network Protocol.
502 * See the UEFI spec for details.
503 *
504 * @this: the instance of the Simple Network Protocol
505 * @header_size: size of the media header
506 * @buffer_size: size of the buffer to receive the packet
507 * @buffer: buffer to receive the packet
508 * @src_addr: source hardware MAC address
509 * @dest_addr: destination hardware MAC address
510 * @protocol: type of header to build
511 * Return: status code
512 */
513static efi_status_t EFIAPI efi_net_transmit
514 (struct efi_simple_network *this, size_t header_size,
515 size_t buffer_size, void *buffer,
516 struct efi_mac_address *src_addr,
517 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200518{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100519 efi_status_t ret = EFI_SUCCESS;
520
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200521 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
522 (unsigned long)header_size, (unsigned long)buffer_size,
523 buffer, src_addr, dest_addr, protocol);
Alexander Graf94c4b992016-05-06 21:01:01 +0200524
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200525 efi_timer_check();
526
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100527 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200528 if (!this || !buffer) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100529 ret = EFI_INVALID_PARAMETER;
530 goto out;
531 }
532
533 /* We do not support jumbo packets */
534 if (buffer_size > PKTSIZE_ALIGN) {
535 ret = EFI_INVALID_PARAMETER;
536 goto out;
537 }
538
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200539 /* At least the IP header has to fit into the buffer */
540 if (buffer_size < this->mode->media_header_size) {
541 ret = EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100542 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200543 }
544
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200545 /*
546 * TODO:
547 * Support VLANs. Use net_set_ether() for copying the header. Use a
548 * U_BOOT_ENV_CALLBACK to update the media header size.
549 */
550 if (header_size) {
551 struct ethernet_hdr *header = buffer;
552
553 if (!dest_addr || !protocol ||
554 header_size != this->mode->media_header_size) {
555 ret = EFI_INVALID_PARAMETER;
556 goto out;
557 }
558 if (!src_addr)
559 src_addr = &this->mode->current_address;
560
561 memcpy(header->et_dest, dest_addr, ARP_HLEN);
562 memcpy(header->et_src, src_addr, ARP_HLEN);
563 header->et_protlen = htons(*protocol);
564 }
565
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100566 switch (this->mode->state) {
567 case EFI_NETWORK_STOPPED:
568 ret = EFI_NOT_STARTED;
569 goto out;
570 case EFI_NETWORK_STARTED:
571 ret = EFI_DEVICE_ERROR;
572 goto out;
573 default:
574 break;
575 }
576
577 /* Ethernet packets always fit, just bounce */
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100578 memcpy(transmit_buffer, buffer, buffer_size);
579 net_send_packet(transmit_buffer, buffer_size);
Alexander Graf0b12b862016-09-06 14:26:27 +0200580
Alexander Graf94c4b992016-05-06 21:01:01 +0200581 new_tx_packet = buffer;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200582 this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100583out:
584 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200585}
586
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100587/**
588 * efi_net_receive() - receive a packet from a network interface
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200589 *
590 * This function implements the Receive service of the Simple Network Protocol.
591 * See the UEFI spec for details.
592 *
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100593 * @this: the instance of the Simple Network Protocol
594 * @header_size: size of the media header
595 * @buffer_size: size of the buffer to receive the packet
596 * @buffer: buffer to receive the packet
597 * @src_addr: source MAC address
598 * @dest_addr: destination MAC address
599 * @protocol: protocol
600 * Return: status code
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200601 */
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100602static efi_status_t EFIAPI efi_net_receive
603 (struct efi_simple_network *this, size_t *header_size,
604 size_t *buffer_size, void *buffer,
605 struct efi_mac_address *src_addr,
606 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200607{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100608 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200609 struct ethernet_hdr *eth_hdr;
610 size_t hdr_size = sizeof(struct ethernet_hdr);
611 u16 protlen;
612
Alexander Graf94c4b992016-05-06 21:01:01 +0200613 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
614 buffer_size, buffer, src_addr, dest_addr, protocol);
615
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100616 /* Execute events */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200617 efi_timer_check();
Alexander Graf94c4b992016-05-06 21:01:01 +0200618
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100619 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200620 if (!this || !buffer || !buffer_size) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100621 ret = EFI_INVALID_PARAMETER;
622 goto out;
623 }
624
625 switch (this->mode->state) {
626 case EFI_NETWORK_STOPPED:
627 ret = EFI_NOT_STARTED;
628 goto out;
629 case EFI_NETWORK_STARTED:
630 ret = EFI_DEVICE_ERROR;
631 goto out;
632 default:
633 break;
634 }
635
Patrick Wildtfab89102020-10-07 11:04:33 +0200636 if (!rx_packet_num) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100637 ret = EFI_NOT_READY;
638 goto out;
639 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200640 /* Fill export parameters */
Patrick Wildtfab89102020-10-07 11:04:33 +0200641 eth_hdr = (struct ethernet_hdr *)receive_buffer[rx_packet_idx];
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200642 protlen = ntohs(eth_hdr->et_protlen);
643 if (protlen == 0x8100) {
644 hdr_size += 4;
Patrick Wildtfab89102020-10-07 11:04:33 +0200645 protlen = ntohs(*(u16 *)&receive_buffer[rx_packet_idx][hdr_size - 2]);
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200646 }
647 if (header_size)
648 *header_size = hdr_size;
649 if (dest_addr)
650 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
651 if (src_addr)
652 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
653 if (protocol)
654 *protocol = protlen;
Patrick Wildtfab89102020-10-07 11:04:33 +0200655 if (*buffer_size < receive_lengths[rx_packet_idx]) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200656 /* Packet doesn't fit, try again with bigger buffer */
Patrick Wildtfab89102020-10-07 11:04:33 +0200657 *buffer_size = receive_lengths[rx_packet_idx];
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100658 ret = EFI_BUFFER_TOO_SMALL;
659 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200660 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200661 /* Copy packet */
Patrick Wildtfab89102020-10-07 11:04:33 +0200662 memcpy(buffer, receive_buffer[rx_packet_idx],
663 receive_lengths[rx_packet_idx]);
664 *buffer_size = receive_lengths[rx_packet_idx];
665 rx_packet_idx = (rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV;
666 rx_packet_num--;
667 if (rx_packet_num)
668 wait_for_packet->is_signaled = true;
669 else
670 this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100671out:
672 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200673}
674
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100675/**
676 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
677 *
678 * This function is called by dhcp_handler().
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200679 *
680 * @pkt: packet received by dhcp_handler()
681 * @len: length of the packet received
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100682 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200683void efi_net_set_dhcp_ack(void *pkt, int len)
684{
685 int maxsize = sizeof(*dhcp_ack);
686
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100687 if (!dhcp_ack) {
Alexander Graf94c4b992016-05-06 21:01:01 +0200688 dhcp_ack = malloc(maxsize);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100689 if (!dhcp_ack)
690 return;
691 }
692 memset(dhcp_ack, 0, maxsize);
Alexander Graf94c4b992016-05-06 21:01:01 +0200693 memcpy(dhcp_ack, pkt, min(len, maxsize));
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100694
695 if (netobj)
696 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
Alexander Graf94c4b992016-05-06 21:01:01 +0200697}
698
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100699/**
700 * efi_net_push() - callback for received network packet
701 *
702 * This function is called when a network packet is received by eth_rx().
703 *
704 * @pkt: network packet
705 * @len: length
706 */
707static void efi_net_push(void *pkt, int len)
708{
Patrick Wildtfab89102020-10-07 11:04:33 +0200709 int rx_packet_next;
710
711 /* Check that we at least received an Ethernet header */
712 if (len < sizeof(struct ethernet_hdr))
713 return;
714
715 /* Check that the buffer won't overflow */
716 if (len > PKTSIZE_ALIGN)
717 return;
718
719 /* Can't store more than pre-alloced buffer */
720 if (rx_packet_num >= ETH_PACKETS_BATCH_RECV)
721 return;
722
723 rx_packet_next = (rx_packet_idx + rx_packet_num) %
724 ETH_PACKETS_BATCH_RECV;
725 memcpy(receive_buffer[rx_packet_next], pkt, len);
726 receive_lengths[rx_packet_next] = len;
727
728 rx_packet_num++;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100729}
730
731/**
732 * efi_network_timer_notify() - check if a new network packet has been received
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200733 *
734 * This notification function is called in every timer cycle.
735 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200736 * @event: the event for which this notification function is registered
737 * @context: event context - not used in this function
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200738 */
739static void EFIAPI efi_network_timer_notify(struct efi_event *event,
740 void *context)
741{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100742 struct efi_simple_network *this = (struct efi_simple_network *)context;
743
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200744 EFI_ENTRY("%p, %p", event, context);
745
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100746 /*
747 * Some network drivers do not support calling eth_rx() before
748 * initialization.
749 */
750 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
751 goto out;
752
Patrick Wildtfab89102020-10-07 11:04:33 +0200753 if (!rx_packet_num) {
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200754 push_packet = efi_net_push;
755 eth_rx();
756 push_packet = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +0200757 if (rx_packet_num) {
758 this->int_status |=
759 EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
760 wait_for_packet->is_signaled = true;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200761 }
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200762 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100763out:
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200764 EFI_EXIT(EFI_SUCCESS);
765}
766
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200767static efi_status_t EFIAPI efi_pxe_base_code_start(
768 struct efi_pxe_base_code_protocol *this,
769 u8 use_ipv6)
770{
771 return EFI_UNSUPPORTED;
772}
773
774static efi_status_t EFIAPI efi_pxe_base_code_stop(
775 struct efi_pxe_base_code_protocol *this)
776{
777 return EFI_UNSUPPORTED;
778}
779
780static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
781 struct efi_pxe_base_code_protocol *this,
782 u8 sort_offers)
783{
784 return EFI_UNSUPPORTED;
785}
786
787static efi_status_t EFIAPI efi_pxe_base_code_discover(
788 struct efi_pxe_base_code_protocol *this,
789 u16 type, u16 *layer, u8 bis,
790 struct efi_pxe_base_code_discover_info *info)
791{
792 return EFI_UNSUPPORTED;
793}
794
795static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
796 struct efi_pxe_base_code_protocol *this,
797 u32 operation, void *buffer_ptr,
798 u8 overwrite, efi_uintn_t *buffer_size,
799 struct efi_ip_address server_ip, char *filename,
800 struct efi_pxe_base_code_mtftp_info *info,
801 u8 dont_use_buffer)
802{
803 return EFI_UNSUPPORTED;
804}
805
806static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
807 struct efi_pxe_base_code_protocol *this,
808 u16 op_flags, struct efi_ip_address *dest_ip,
809 u16 *dest_port,
810 struct efi_ip_address *gateway_ip,
811 struct efi_ip_address *src_ip, u16 *src_port,
812 efi_uintn_t *header_size, void *header_ptr,
813 efi_uintn_t *buffer_size, void *buffer_ptr)
814{
815 return EFI_UNSUPPORTED;
816}
817
818static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
819 struct efi_pxe_base_code_protocol *this,
820 u16 op_flags, struct efi_ip_address *dest_ip,
821 u16 *dest_port, struct efi_ip_address *src_ip,
822 u16 *src_port, efi_uintn_t *header_size,
823 void *header_ptr, efi_uintn_t *buffer_size,
824 void *buffer_ptr)
825{
826 return EFI_UNSUPPORTED;
827}
828
829static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
830 struct efi_pxe_base_code_protocol *this,
831 struct efi_pxe_base_code_filter *new_filter)
832{
833 return EFI_UNSUPPORTED;
834}
835
836static efi_status_t EFIAPI efi_pxe_base_code_arp(
837 struct efi_pxe_base_code_protocol *this,
838 struct efi_ip_address *ip_addr,
839 struct efi_mac_address *mac_addr)
840{
841 return EFI_UNSUPPORTED;
842}
843
844static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
845 struct efi_pxe_base_code_protocol *this,
846 u8 *new_auto_arp, u8 *new_send_guid,
847 u8 *new_ttl, u8 *new_tos,
848 u8 *new_make_callback)
849{
850 return EFI_UNSUPPORTED;
851}
852
853static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
854 struct efi_pxe_base_code_protocol *this,
855 struct efi_ip_address *new_station_ip,
856 struct efi_ip_address *new_subnet_mask)
857{
858 return EFI_UNSUPPORTED;
859}
860
861static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
862 struct efi_pxe_base_code_protocol *this,
863 u8 *new_dhcp_discover_valid,
864 u8 *new_dhcp_ack_received,
865 u8 *new_proxy_offer_received,
866 u8 *new_pxe_discover_valid,
867 u8 *new_pxe_reply_received,
868 u8 *new_pxe_bis_reply_received,
869 EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
870 EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
871 EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
872 EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
873 EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
874 EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
875{
876 return EFI_UNSUPPORTED;
877}
878
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100879/**
880 * efi_net_register() - register the simple network protocol
881 *
882 * This gets called from do_bootefi_exec().
883 */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100884efi_status_t efi_net_register(void)
Alexander Graf94c4b992016-05-06 21:01:01 +0200885{
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200886 efi_status_t r;
Patrick Wildtfab89102020-10-07 11:04:33 +0200887 int i;
Alexander Graf94c4b992016-05-06 21:01:01 +0200888
889 if (!eth_get_dev()) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200890 /* No network device active, don't expose any */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100891 return EFI_SUCCESS;
Alexander Graf94c4b992016-05-06 21:01:01 +0200892 }
893
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200894 /* We only expose the "active" network device, so one is enough */
Alexander Graf94c4b992016-05-06 21:01:01 +0200895 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100896 if (!netobj)
897 goto out_of_resources;
898
899 /* Allocate an aligned transmit buffer */
900 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
901 if (!transmit_buffer)
902 goto out_of_resources;
903 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100904
Patrick Wildtfab89102020-10-07 11:04:33 +0200905 /* Allocate a number of receive buffers */
906 receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
907 sizeof(*receive_buffer));
908 if (!receive_buffer)
909 goto out_of_resources;
910 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
911 receive_buffer[i] = malloc(PKTSIZE_ALIGN);
912 if (!receive_buffer[i])
913 goto out_of_resources;
914 }
915 receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
916 sizeof(*receive_lengths));
917 if (!receive_lengths)
918 goto out_of_resources;
919
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100920 /* Hook net up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200921 efi_add_handle(&netobj->header);
Alexander Graf94c4b992016-05-06 21:01:01 +0200922
923 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200924 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100925 &netobj->net);
926 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100927 goto failure_to_add_protocol;
Adriano Cordova93cba0f2024-12-04 00:05:23 -0300928 if (!net_dp)
929 efi_net_set_dp("Net", NULL);
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200930 r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
Adriano Cordova93cba0f2024-12-04 00:05:23 -0300931 net_dp);
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100932 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100933 goto failure_to_add_protocol;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200934 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100935 &netobj->pxe);
936 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100937 goto failure_to_add_protocol;
Heinrich Schuchardtd54396a2017-10-05 16:35:57 +0200938 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf94c4b992016-05-06 21:01:01 +0200939 netobj->net.start = efi_net_start;
940 netobj->net.stop = efi_net_stop;
941 netobj->net.initialize = efi_net_initialize;
942 netobj->net.reset = efi_net_reset;
943 netobj->net.shutdown = efi_net_shutdown;
944 netobj->net.receive_filters = efi_net_receive_filters;
945 netobj->net.station_address = efi_net_station_address;
946 netobj->net.statistics = efi_net_statistics;
947 netobj->net.mcastiptomac = efi_net_mcastiptomac;
948 netobj->net.nvdata = efi_net_nvdata;
949 netobj->net.get_status = efi_net_get_status;
950 netobj->net.transmit = efi_net_transmit;
951 netobj->net.receive = efi_net_receive;
952 netobj->net.mode = &netobj->net_mode;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200953 netobj->net_mode.state = EFI_NETWORK_STOPPED;
Alexander Graf94c4b992016-05-06 21:01:01 +0200954 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardtd1cc6cb2017-10-05 16:35:58 +0200955 netobj->net_mode.hwaddr_size = ARP_HLEN;
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200956 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
Alexander Graf94c4b992016-05-06 21:01:01 +0200957 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomas27df0a72018-06-21 16:21:01 -0700958 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf94c4b992016-05-06 21:01:01 +0200959
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200960 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
961 netobj->pxe.start = efi_pxe_base_code_start;
962 netobj->pxe.stop = efi_pxe_base_code_stop;
963 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
964 netobj->pxe.discover = efi_pxe_base_code_discover;
965 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
966 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
967 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
968 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
969 netobj->pxe.arp = efi_pxe_base_code_arp;
970 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
971 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
972 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf94c4b992016-05-06 21:01:01 +0200973 netobj->pxe.mode = &netobj->pxe_mode;
974 if (dhcp_ack)
975 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
976
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200977 /*
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +0200978 * Create WaitForPacket event.
979 */
980 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100981 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +0200982 &wait_for_packet);
983 if (r != EFI_SUCCESS) {
984 printf("ERROR: Failed to register network event\n");
985 return r;
986 }
987 netobj->net.wait_for_packet = wait_for_packet;
988 /*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200989 * Create a timer event.
990 *
991 * The notification function is used to check if a new network packet
992 * has been received.
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +0100993 *
994 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200995 */
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +0100996 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100997 efi_network_timer_notify, &netobj->net, NULL,
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200998 &network_timer_event);
999 if (r != EFI_SUCCESS) {
1000 printf("ERROR: Failed to register network event\n");
1001 return r;
1002 }
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001003 /* Network is time critical, create event in every timer cycle */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001004 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
1005 if (r != EFI_SUCCESS) {
1006 printf("ERROR: Failed to set network timer\n");
1007 return r;
1008 }
1009
Adriano Cordova9debc902024-12-04 00:05:25 -03001010#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
1011 r = efi_ipconfig_register(&netobj->header, &netobj->ip4_config2);
1012 if (r != EFI_SUCCESS)
1013 goto failure_to_add_protocol;
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001014#endif
1015
1016#ifdef CONFIG_EFI_HTTP_PROTOCOL
1017 r = efi_http_register(&netobj->header, &netobj->http_service_binding);
1018 if (r != EFI_SUCCESS)
1019 goto failure_to_add_protocol;
1020 /*
1021 * No harm on doing the following. If the PXE handle is present, the client could
1022 * find it and try to get its IP address from it. In here the PXE handle is present
1023 * but the PXE protocol is not yet implmenented, so we add this in the meantime.
1024 */
1025 efi_net_get_addr((struct efi_ipv4_address *)&netobj->pxe_mode.station_ip,
1026 (struct efi_ipv4_address *)&netobj->pxe_mode.subnet_mask, NULL);
Adriano Cordova9debc902024-12-04 00:05:25 -03001027#endif
1028
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001029 return EFI_SUCCESS;
1030failure_to_add_protocol:
1031 printf("ERROR: Failure to add protocol\n");
1032 return r;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001033out_of_resources:
1034 free(netobj);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +01001035 netobj = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +02001036 free(transmit_buffer);
1037 if (receive_buffer)
1038 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
1039 free(receive_buffer[i]);
1040 free(receive_buffer);
1041 free(receive_lengths);
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001042 printf("ERROR: Out of memory\n");
1043 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001044}
Adriano Cordova3c951362024-12-04 00:05:21 -03001045
1046/**
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001047 * efi_net_set_dp() - set device path of efi net device
1048 *
1049 * This gets called to update the device path when a new boot
1050 * file is downloaded
1051 *
1052 * @dev: dev to set the device path from
1053 * @server: remote server address
1054 * Return: status code
1055 */
1056efi_status_t efi_net_set_dp(const char *dev, const char *server)
1057{
1058 efi_free_pool(net_dp);
1059
1060 net_dp = NULL;
1061 if (!strcmp(dev, "Net"))
1062 net_dp = efi_dp_from_eth();
1063 else if (!strcmp(dev, "Http"))
1064 net_dp = efi_dp_from_http(server);
1065
1066 if (!net_dp)
1067 return EFI_OUT_OF_RESOURCES;
1068
1069 return EFI_SUCCESS;
1070}
1071
1072/**
1073 * efi_net_get_dp() - get device path of efi net device
1074 *
1075 * Produce a copy of the current device path
1076 *
1077 * @dp: copy of the current device path, or NULL on error
1078 */
1079void efi_net_get_dp(struct efi_device_path **dp)
1080{
1081 if (!dp)
1082 return;
1083 if (!net_dp)
1084 efi_net_set_dp("Net", NULL);
1085 if (net_dp)
1086 *dp = efi_dp_dup(net_dp);
1087}
1088
1089/**
Adriano Cordova3c951362024-12-04 00:05:21 -03001090 * efi_net_get_addr() - get IP address information
1091 *
1092 * Copy the current IP address, mask, and gateway into the
1093 * efi_ipv4_address structs pointed to by ip, mask and gw,
1094 * respectively.
1095 *
1096 * @ip: pointer to an efi_ipv4_address struct to
1097 * be filled with the current IP address
1098 * @mask: pointer to an efi_ipv4_address struct to
1099 * be filled with the current network mask
1100 * @gw: pointer to an efi_ipv4_address struct to be
1101 * filled with the current network gateway
1102 */
1103void efi_net_get_addr(struct efi_ipv4_address *ip,
1104 struct efi_ipv4_address *mask,
1105 struct efi_ipv4_address *gw)
1106{
1107#ifdef CONFIG_NET_LWIP
1108 char ipstr[] = "ipaddr\0\0";
1109 char maskstr[] = "netmask\0\0";
1110 char gwstr[] = "gatewayip\0\0";
1111 int idx;
1112 struct in_addr tmp;
1113 char *env;
1114
1115 idx = dev_seq(eth_get_dev());
1116
1117 if (idx < 0 || idx > 99) {
1118 log_err("unexpected idx %d\n", idx);
1119 return;
1120 }
1121
1122 if (idx) {
1123 sprintf(ipstr, "ipaddr%d", idx);
1124 sprintf(maskstr, "netmask%d", idx);
1125 sprintf(gwstr, "gatewayip%d", idx);
1126 }
1127
1128 env = env_get(ipstr);
1129 if (env && ip) {
1130 tmp = string_to_ip(env);
1131 memcpy(ip, &tmp, sizeof(tmp));
1132 }
1133
1134 env = env_get(maskstr);
1135 if (env && mask) {
1136 tmp = string_to_ip(env);
1137 memcpy(mask, &tmp, sizeof(tmp));
1138 }
1139 env = env_get(gwstr);
1140 if (env && gw) {
1141 tmp = string_to_ip(env);
1142 memcpy(gw, &tmp, sizeof(tmp));
1143 }
1144#else
1145 if (ip)
1146 memcpy(ip, &net_ip, sizeof(net_ip));
1147 if (mask)
1148 memcpy(mask, &net_netmask, sizeof(net_netmask));
1149#endif
1150}
1151
1152/**
1153 * efi_net_set_addr() - set IP address information
1154 *
1155 * Set the current IP address, mask, and gateway to the
1156 * efi_ipv4_address structs pointed to by ip, mask and gw,
1157 * respectively.
1158 *
1159 * @ip: pointer to new IP address
1160 * @mask: pointer to new network mask to set
1161 * @gw: pointer to new network gateway
1162 */
1163void efi_net_set_addr(struct efi_ipv4_address *ip,
1164 struct efi_ipv4_address *mask,
1165 struct efi_ipv4_address *gw)
1166{
1167#ifdef CONFIG_NET_LWIP
1168 char ipstr[] = "ipaddr\0\0";
1169 char maskstr[] = "netmask\0\0";
1170 char gwstr[] = "gatewayip\0\0";
1171 int idx;
1172 struct in_addr *addr;
1173 char tmp[46];
1174
1175 idx = dev_seq(eth_get_dev());
1176
1177 if (idx < 0 || idx > 99) {
1178 log_err("unexpected idx %d\n", idx);
1179 return;
1180 }
1181
1182 if (idx) {
1183 sprintf(ipstr, "ipaddr%d", idx);
1184 sprintf(maskstr, "netmask%d", idx);
1185 sprintf(gwstr, "gatewayip%d", idx);
1186 }
1187
1188 if (ip) {
1189 addr = (struct in_addr *)ip;
1190 ip_to_string(*addr, tmp);
1191 env_set(ipstr, tmp);
1192 }
1193
1194 if (mask) {
1195 addr = (struct in_addr *)mask;
1196 ip_to_string(*addr, tmp);
1197 env_set(maskstr, tmp);
1198 }
1199
1200 if (gw) {
1201 addr = (struct in_addr *)gw;
1202 ip_to_string(*addr, tmp);
1203 env_set(gwstr, tmp);
1204 }
1205#else
1206 if (ip)
1207 memcpy(&net_ip, ip, sizeof(*ip));
1208 if (mask)
1209 memcpy(&net_netmask, mask, sizeof(*mask));
1210#endif
1211}
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001212
1213/**
1214 * efi_net_set_buffer() - allocate a buffer of min 64K
1215 *
1216 * @buffer: allocated buffer
1217 * @size: desired buffer size
1218 * Return: status code
1219 */
1220static efi_status_t efi_net_set_buffer(void **buffer, size_t size)
1221{
1222 efi_status_t ret = EFI_SUCCESS;
1223
1224 if (size < SZ_64K)
1225 size = SZ_64K;
1226
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001227 *buffer = efi_alloc(size);
1228 if (!*buffer)
1229 ret = EFI_OUT_OF_RESOURCES;
1230
1231 efi_wget_info.buffer_size = (ulong)size;
1232
1233 return ret;
1234}
1235
1236/**
1237 * efi_net_parse_headers() - parse HTTP headers
1238 *
1239 * Parses the raw buffer efi_wget_info.headers into an array headers
1240 * of efi structs http_headers. The array should be at least
1241 * MAX_HTTP_HEADERS long.
1242 *
1243 * @num_headers: number of headers
1244 * @headers: caller provided array of struct http_headers
1245 */
1246void efi_net_parse_headers(ulong *num_headers, struct http_header *headers)
1247{
1248 if (!num_headers || !headers)
1249 return;
1250
1251 // Populate info with http headers.
1252 *num_headers = 0;
1253 const uchar *line_start = efi_wget_info.headers;
1254 const uchar *line_end;
1255 ulong count;
1256 struct http_header *current_header;
1257 const uchar *separator;
1258 size_t name_length, value_length;
1259
1260 // Skip the first line (request or status line)
1261 line_end = strstr(line_start, "\r\n");
1262
1263 if (line_end)
1264 line_start = line_end + 2;
1265
1266 while ((line_end = strstr(line_start, "\r\n")) != NULL) {
1267 count = *num_headers;
1268 if (line_start == line_end || count >= MAX_HTTP_HEADERS)
1269 break;
1270 current_header = headers + count;
1271 separator = strchr(line_start, ':');
1272 if (separator) {
1273 name_length = separator - line_start;
1274 ++separator;
1275 while (*separator == ' ')
1276 ++separator;
1277 value_length = line_end - separator;
1278 if (name_length < MAX_HTTP_HEADER_NAME &&
1279 value_length < MAX_HTTP_HEADER_VALUE) {
1280 strncpy(current_header->name, line_start, name_length);
1281 current_header->name[name_length] = '\0';
1282 strncpy(current_header->value, separator, value_length);
1283 current_header->value[value_length] = '\0';
1284 (*num_headers)++;
1285 }
1286 }
1287 line_start = line_end + 2;
1288 }
1289}
1290
1291/**
1292 * efi_net_do_request() - issue an HTTP request using wget
1293 *
1294 * @url: url
1295 * @method: HTTP method
1296 * @buffer: data buffer
1297 * @status_code: HTTP status code
1298 * @file_size: file size in bytes
1299 * @headers_buffer: headers buffer
1300 * Return: status code
1301 */
1302efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, void **buffer,
1303 u32 *status_code, ulong *file_size, char *headers_buffer)
1304{
1305 efi_status_t ret = EFI_SUCCESS;
1306 int wget_ret;
1307 static bool last_head;
1308
1309 if (!buffer || !file_size)
1310 return EFI_ABORTED;
1311
1312 efi_wget_info.method = (enum wget_http_method)method;
1313 efi_wget_info.headers = headers_buffer;
1314
1315 switch (method) {
1316 case HTTP_METHOD_GET:
1317 ret = efi_net_set_buffer(buffer, last_head ? (size_t)efi_wget_info.hdr_cont_len : 0);
1318 if (ret != EFI_SUCCESS)
1319 goto out;
1320 wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info);
1321 if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) {
1322 // Try again with updated buffer size
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001323 efi_free_pool(*buffer);
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001324 ret = efi_net_set_buffer(buffer, (size_t)efi_wget_info.hdr_cont_len);
1325 if (ret != EFI_SUCCESS)
1326 goto out;
1327 if (wget_request((ulong)*buffer, url, &efi_wget_info)) {
1328 efi_free_pool(*buffer);
1329 ret = EFI_DEVICE_ERROR;
1330 goto out;
1331 }
1332 } else if (wget_ret) {
1333 efi_free_pool(*buffer);
1334 ret = EFI_DEVICE_ERROR;
1335 goto out;
1336 }
1337 // Pass the actual number of received bytes to the application
1338 *file_size = efi_wget_info.file_size;
1339 *status_code = efi_wget_info.status_code;
1340 last_head = false;
1341 break;
1342 case HTTP_METHOD_HEAD:
1343 ret = efi_net_set_buffer(buffer, 0);
1344 if (ret != EFI_SUCCESS)
1345 goto out;
1346 wget_request((ulong)*buffer, url, &efi_wget_info);
1347 *file_size = 0;
1348 *status_code = efi_wget_info.status_code;
1349 last_head = true;
1350 break;
1351 default:
1352 ret = EFI_UNSUPPORTED;
1353 break;
1354 }
1355
1356out:
1357 return ret;
1358}