blob: 2fac39ae513cfbf5b1397dec105cb7e2c06c2f95 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf94c4b992016-05-06 21:01:01 +02002/*
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +02003 * Simple network protocol
4 * PXE base code protocol
Alexander Graf94c4b992016-05-06 21:01:01 +02005 *
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +02006 * Copyright (c) 2016 Alexander Graf
7 *
8 * The simple network protocol has the following statuses and services
9 * to move between them:
10 *
11 * Start(): EfiSimpleNetworkStopped -> EfiSimpleNetworkStarted
12 * Initialize(): EfiSimpleNetworkStarted -> EfiSimpleNetworkInitialized
13 * Shutdown(): EfiSimpleNetworkInitialized -> EfiSimpleNetworkStarted
14 * Stop(): EfiSimpleNetworkStarted -> EfiSimpleNetworkStopped
15 * Reset(): EfiSimpleNetworkInitialized -> EfiSimpleNetworkInitialized
Alexander Graf94c4b992016-05-06 21:01:01 +020016 */
17
Heinrich Schuchardt955a3212025-01-16 20:26:59 +010018#define LOG_CATEGORY LOGC_EFI
19
Alexander Graf94c4b992016-05-06 21:01:01 +020020#include <efi_loader.h>
Adriano Cordova93cba0f2024-12-04 00:05:23 -030021#include <dm.h>
Adriano Cordova0d1f5092024-12-04 00:05:24 -030022#include <linux/sizes.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020023#include <malloc.h>
Adriano Cordova3c951362024-12-04 00:05:21 -030024#include <vsprintf.h>
Simon Glass274e0b02020-05-10 11:39:56 -060025#include <net.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020026
Adriano Cordova62e20fb2025-03-03 11:13:16 -030027#define MAX_NUM_DHCP_ENTRIES 10
Adriano Cordova54674692025-03-03 11:13:15 -030028#define MAX_NUM_DP_ENTRIES 10
29
Adriano Cordova7f2bcd42025-03-03 11:13:11 -030030const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020031static const efi_guid_t efi_pxe_base_code_protocol_guid =
32 EFI_PXE_BASE_CODE_PROTOCOL_GUID;
Alexander Graf94c4b992016-05-06 21:01:01 +020033static void *new_tx_packet;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010034static void *transmit_buffer;
Patrick Wildtfab89102020-10-07 11:04:33 +020035static uchar **receive_buffer;
36static size_t *receive_lengths;
37static int rx_packet_idx;
38static int rx_packet_num;
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +010039static struct efi_net_obj *netobj;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010040
Adriano Cordova54674692025-03-03 11:13:15 -030041struct dp_entry {
42 struct efi_device_path *net_dp;
43 struct udevice *dev;
44 bool is_valid;
45};
46
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020047/*
Adriano Cordova54674692025-03-03 11:13:15 -030048 * The network device path cache. An entry is added when a new bootfile
49 * is downloaded from the network. If the bootfile is then loaded as an
50 * efi image, the most recent entry corresponding to the device is passed
51 * as the device path of the loaded image.
Adriano Cordova93cba0f2024-12-04 00:05:23 -030052 */
Adriano Cordova54674692025-03-03 11:13:15 -030053static struct dp_entry dp_cache[MAX_NUM_DP_ENTRIES];
54static int next_dp_entry;
Adriano Cordova93cba0f2024-12-04 00:05:23 -030055
Adriano Cordova54674692025-03-03 11:13:15 -030056#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
Adriano Cordova0d1f5092024-12-04 00:05:24 -030057static struct wget_http_info efi_wget_info = {
58 .set_bootdev = false,
59 .check_buffer_size = true,
60
61};
Adriano Cordova54674692025-03-03 11:13:15 -030062#endif
Adriano Cordova0d1f5092024-12-04 00:05:24 -030063
Adriano Cordova62e20fb2025-03-03 11:13:16 -030064struct dhcp_entry {
65 struct efi_pxe_packet *dhcp_ack;
66 struct udevice *dev;
67 bool is_valid;
68};
69
70static struct dhcp_entry dhcp_cache[MAX_NUM_DHCP_ENTRIES];
71static int next_dhcp_entry;
72
Adriano Cordova93cba0f2024-12-04 00:05:23 -030073/*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020074 * The notification function of this event is called in every timer cycle
75 * to check if a new network packet has been received.
76 */
77static struct efi_event *network_timer_event;
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +020078/*
79 * This event is signaled when a packet has been received.
80 */
81static struct efi_event *wait_for_packet;
Alexander Graf94c4b992016-05-06 21:01:01 +020082
Heinrich Schuchardt72928722018-09-26 05:27:56 +020083/**
84 * struct efi_net_obj - EFI object representing a network interface
85 *
Adriano Cordova9debc902024-12-04 00:05:25 -030086 * @header: EFI object header
Adriano Cordova54674692025-03-03 11:13:15 -030087 * @dev: net udevice
Adriano Cordova9debc902024-12-04 00:05:25 -030088 * @net: simple network protocol interface
89 * @net_mode: status of the network interface
90 * @pxe: PXE base code protocol interface
91 * @pxe_mode: status of the PXE base code protocol
92 * @ip4_config2: IP4 Config2 protocol interface
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030093 * @http_service_binding: Http service binding protocol interface
Heinrich Schuchardt72928722018-09-26 05:27:56 +020094 */
Alexander Graf94c4b992016-05-06 21:01:01 +020095struct efi_net_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020096 struct efi_object header;
Adriano Cordova54674692025-03-03 11:13:15 -030097 struct udevice *dev;
Alexander Graf94c4b992016-05-06 21:01:01 +020098 struct efi_simple_network net;
99 struct efi_simple_network_mode net_mode;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200100 struct efi_pxe_base_code_protocol pxe;
Alexander Graf94c4b992016-05-06 21:01:01 +0200101 struct efi_pxe_mode pxe_mode;
Adriano Cordova9debc902024-12-04 00:05:25 -0300102#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
103 struct efi_ip4_config2_protocol ip4_config2;
104#endif
Adriano Cordovae9b19eb2024-12-04 00:05:26 -0300105#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
106 struct efi_service_binding_protocol http_service_binding;
107#endif
Alexander Graf94c4b992016-05-06 21:01:01 +0200108};
109
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100110/*
Adriano Cordova54674692025-03-03 11:13:15 -0300111 * efi_netobj_is_active() - checks if a netobj is active in the efi subsystem
112 *
113 * @netobj: pointer to efi_net_obj
114 * Return: true if active
115 */
116static bool efi_netobj_is_active(struct efi_net_obj *netobj)
117{
118 if (!netobj || !efi_search_obj(&netobj->header))
119 return false;
120
121 return true;
122}
123
124
125/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100126 * efi_net_start() - start the network interface
127 *
128 * This function implements the Start service of the
129 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
130 * (UEFI) specification for details.
131 *
132 * @this: pointer to the protocol instance
133 * Return: status code
134 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200135static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
136{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100137 efi_status_t ret = EFI_SUCCESS;
138
Alexander Graf94c4b992016-05-06 21:01:01 +0200139 EFI_ENTRY("%p", this);
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100140 /* Check parameters */
141 if (!this) {
142 ret = EFI_INVALID_PARAMETER;
143 goto out;
144 }
145
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200146 if (this->mode->state != EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100147 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200148 } else {
149 this->int_status = 0;
150 wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100151 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200152 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100153out:
154 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200155}
156
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100157/*
158 * efi_net_stop() - stop the network interface
159 *
160 * This function implements the Stop service of the
161 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
162 * (UEFI) specification for details.
163 *
164 * @this: pointer to the protocol instance
165 * Return: status code
166 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200167static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
168{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100169 efi_status_t ret = EFI_SUCCESS;
170
Alexander Graf94c4b992016-05-06 21:01:01 +0200171 EFI_ENTRY("%p", this);
172
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100173 /* Check parameters */
174 if (!this) {
175 ret = EFI_INVALID_PARAMETER;
176 goto out;
177 }
178
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200179 if (this->mode->state == EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100180 ret = EFI_NOT_STARTED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200181 } else {
182 /* Disable hardware and put it into the reset state */
Adriano Cordova54674692025-03-03 11:13:15 -0300183 eth_set_dev(netobj->dev);
184 env_set("ethact", eth_get_name());
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200185 eth_halt();
Patrick Wildtfab89102020-10-07 11:04:33 +0200186 /* Clear cache of packets */
187 rx_packet_num = 0;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100188 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200189 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100190out:
191 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200192}
193
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200194/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100195 * efi_net_initialize() - initialize the network interface
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200196 *
197 * This function implements the Initialize service of the
198 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
199 * (UEFI) specification for details.
200 *
201 * @this: pointer to the protocol instance
202 * @extra_rx: extra receive buffer to be allocated
203 * @extra_tx: extra transmit buffer to be allocated
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100204 * Return: status code
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200205 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200206static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
207 ulong extra_rx, ulong extra_tx)
208{
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200209 int ret;
210 efi_status_t r = EFI_SUCCESS;
211
Alexander Graf94c4b992016-05-06 21:01:01 +0200212 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
213
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100214 /* Check parameters */
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200215 if (!this) {
216 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100217 goto out;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200218 }
219
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200220 switch (this->mode->state) {
221 case EFI_NETWORK_INITIALIZED:
222 case EFI_NETWORK_STARTED:
223 break;
224 default:
225 r = EFI_NOT_STARTED;
226 goto out;
227 }
228
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200229 /* Setup packet buffers */
230 net_init();
Patrick Wildtfab89102020-10-07 11:04:33 +0200231 /* Clear cache of packets */
232 rx_packet_num = 0;
Adriano Cordova54674692025-03-03 11:13:15 -0300233 /* Set the net device corresponding to the efi net object */
234 eth_set_dev(netobj->dev);
235 env_set("ethact", eth_get_name());
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200236 /* Get hardware ready for send and receive operations */
Adriano Cordova54674692025-03-03 11:13:15 -0300237 ret = eth_start_udev(netobj->dev);
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200238 if (ret < 0) {
239 eth_halt();
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100240 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200241 r = EFI_DEVICE_ERROR;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100242 goto out;
243 } else {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200244 this->int_status = 0;
245 wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100246 this->mode->state = EFI_NETWORK_INITIALIZED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200247 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100248out:
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200249 return EFI_EXIT(r);
Alexander Graf94c4b992016-05-06 21:01:01 +0200250}
251
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100252/*
253 * efi_net_reset() - reinitialize the network interface
254 *
255 * This function implements the Reset service of the
256 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
257 * (UEFI) specification for details.
258 *
259 * @this: pointer to the protocol instance
260 * @extended_verification: execute exhaustive verification
261 * Return: status code
262 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200263static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
264 int extended_verification)
265{
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200266 efi_status_t ret;
267
Alexander Graf94c4b992016-05-06 21:01:01 +0200268 EFI_ENTRY("%p, %x", this, extended_verification);
269
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200270 /* Check parameters */
271 if (!this) {
272 ret = EFI_INVALID_PARAMETER;
273 goto out;
274 }
275
276 switch (this->mode->state) {
277 case EFI_NETWORK_INITIALIZED:
278 break;
279 case EFI_NETWORK_STOPPED:
280 ret = EFI_NOT_STARTED;
281 goto out;
282 default:
283 ret = EFI_DEVICE_ERROR;
284 goto out;
285 }
286
287 this->mode->state = EFI_NETWORK_STARTED;
288 ret = EFI_CALL(efi_net_initialize(this, 0, 0));
289out:
290 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200291}
292
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100293/*
294 * efi_net_shutdown() - shut down the network interface
295 *
296 * This function implements the Shutdown service of the
297 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
298 * (UEFI) specification for details.
299 *
300 * @this: pointer to the protocol instance
301 * Return: status code
302 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200303static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
304{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100305 efi_status_t ret = EFI_SUCCESS;
306
Alexander Graf94c4b992016-05-06 21:01:01 +0200307 EFI_ENTRY("%p", this);
308
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100309 /* Check parameters */
310 if (!this) {
311 ret = EFI_INVALID_PARAMETER;
312 goto out;
313 }
314
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200315 switch (this->mode->state) {
316 case EFI_NETWORK_INITIALIZED:
317 break;
318 case EFI_NETWORK_STOPPED:
319 ret = EFI_NOT_STARTED;
320 goto out;
321 default:
322 ret = EFI_DEVICE_ERROR;
323 goto out;
324 }
325
Adriano Cordova54674692025-03-03 11:13:15 -0300326 eth_set_dev(netobj->dev);
327 env_set("ethact", eth_get_name());
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100328 eth_halt();
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300329
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200330 this->int_status = 0;
331 wait_for_packet->is_signaled = false;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200332 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100333
334out:
335 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200336}
337
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100338/*
339 * efi_net_receive_filters() - mange multicast receive filters
340 *
341 * This function implements the ReceiveFilters service of the
342 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
343 * (UEFI) specification for details.
344 *
345 * @this: pointer to the protocol instance
346 * @enable: bit mask of receive filters to enable
347 * @disable: bit mask of receive filters to disable
348 * @reset_mcast_filter: true resets contents of the filters
349 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
350 * @mcast_filter: list of new filters
351 * Return: status code
352 */
353static efi_status_t EFIAPI efi_net_receive_filters
354 (struct efi_simple_network *this, u32 enable, u32 disable,
355 int reset_mcast_filter, ulong mcast_filter_count,
356 struct efi_mac_address *mcast_filter)
Alexander Graf94c4b992016-05-06 21:01:01 +0200357{
358 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
359 reset_mcast_filter, mcast_filter_count, mcast_filter);
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_station_address() - set the hardware MAC address
366 *
367 * This function implements the StationAddress service of the
368 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
369 * (UEFI) specification for details.
370 *
371 * @this: pointer to the protocol instance
372 * @reset: if true reset the address to default
373 * @new_mac: new MAC address
374 * Return: status code
375 */
376static efi_status_t EFIAPI efi_net_station_address
377 (struct efi_simple_network *this, int reset,
378 struct efi_mac_address *new_mac)
Alexander Graf94c4b992016-05-06 21:01:01 +0200379{
380 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
381
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200382 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200383}
384
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100385/*
386 * efi_net_statistics() - reset or collect statistics of the network interface
387 *
388 * This function implements the Statistics service of the
389 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
390 * (UEFI) specification for details.
391 *
392 * @this: pointer to the protocol instance
393 * @reset: if true, the statistics are reset
394 * @stat_size: size of the statistics table
395 * @stat_table: table to receive the statistics
396 * Return: status code
397 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200398static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
399 int reset, ulong *stat_size,
400 void *stat_table)
401{
402 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
403
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200404 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200405}
406
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100407/*
408 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
409 *
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200410 * This function implements the MCastIPtoMAC service of the
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100411 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
412 * (UEFI) specification for details.
413 *
414 * @this: pointer to the protocol instance
415 * @ipv6: true if the IP address is an IPv6 address
416 * @ip: IP address
417 * @mac: MAC address
418 * Return: status code
419 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200420static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
421 int ipv6,
422 struct efi_ip_address *ip,
423 struct efi_mac_address *mac)
424{
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200425 efi_status_t ret = EFI_SUCCESS;
426
Alexander Graf94c4b992016-05-06 21:01:01 +0200427 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
428
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200429 if (!this || !ip || !mac) {
430 ret = EFI_INVALID_PARAMETER;
431 goto out;
432 }
433
434 if (ipv6) {
435 ret = EFI_UNSUPPORTED;
436 goto out;
437 }
438
439 /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */
440 if ((ip->ip_addr[0] & 0xf0) != 0xe0) {
441 ret = EFI_INVALID_PARAMETER;
442 goto out;
443 };
444
445 switch (this->mode->state) {
446 case EFI_NETWORK_INITIALIZED:
447 case EFI_NETWORK_STARTED:
448 break;
449 default:
450 ret = EFI_NOT_STARTED;
451 goto out;
452 }
453
454 memset(mac, 0, sizeof(struct efi_mac_address));
455
456 /*
457 * Copy lower 23 bits of IPv4 multi-cast address
458 * RFC 1112, RFC 7042 2.1.1.
459 */
460 mac->mac_addr[0] = 0x01;
461 mac->mac_addr[1] = 0x00;
462 mac->mac_addr[2] = 0x5E;
463 mac->mac_addr[3] = ip->ip_addr[1] & 0x7F;
464 mac->mac_addr[4] = ip->ip_addr[2];
465 mac->mac_addr[5] = ip->ip_addr[3];
466out:
467 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200468}
469
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100470/**
471 * efi_net_nvdata() - read or write NVRAM
472 *
473 * This function implements the GetStatus service of the Simple Network
474 * Protocol. See the UEFI spec for details.
475 *
476 * @this: the instance of the Simple Network Protocol
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200477 * @read_write: true for read, false for write
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100478 * @offset: offset in NVRAM
479 * @buffer_size: size of buffer
480 * @buffer: buffer
481 * Return: status code
482 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200483static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
484 int read_write, ulong offset,
485 ulong buffer_size, char *buffer)
486{
487 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
488 buffer);
489
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200490 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200491}
492
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100493/**
494 * efi_net_get_status() - get interrupt status
495 *
496 * This function implements the GetStatus service of the Simple Network
497 * Protocol. See the UEFI spec for details.
498 *
499 * @this: the instance of the Simple Network Protocol
500 * @int_status: interface status
501 * @txbuf: transmission buffer
502 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200503static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
504 u32 *int_status, void **txbuf)
505{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100506 efi_status_t ret = EFI_SUCCESS;
507
Alexander Graf94c4b992016-05-06 21:01:01 +0200508 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
509
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200510 efi_timer_check();
511
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100512 /* Check parameters */
513 if (!this) {
514 ret = EFI_INVALID_PARAMETER;
515 goto out;
516 }
517
518 switch (this->mode->state) {
519 case EFI_NETWORK_STOPPED:
520 ret = EFI_NOT_STARTED;
521 goto out;
522 case EFI_NETWORK_STARTED:
523 ret = EFI_DEVICE_ERROR;
524 goto out;
525 default:
526 break;
527 }
528
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200529 if (int_status) {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200530 *int_status = this->int_status;
531 this->int_status = 0;
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200532 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200533 if (txbuf)
534 *txbuf = new_tx_packet;
535
536 new_tx_packet = NULL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100537out:
538 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200539}
540
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100541/**
542 * efi_net_transmit() - transmit a packet
543 *
544 * This function implements the Transmit service of the Simple Network Protocol.
545 * See the UEFI spec for details.
546 *
547 * @this: the instance of the Simple Network Protocol
548 * @header_size: size of the media header
549 * @buffer_size: size of the buffer to receive the packet
550 * @buffer: buffer to receive the packet
551 * @src_addr: source hardware MAC address
552 * @dest_addr: destination hardware MAC address
553 * @protocol: type of header to build
554 * Return: status code
555 */
556static efi_status_t EFIAPI efi_net_transmit
557 (struct efi_simple_network *this, size_t header_size,
558 size_t buffer_size, void *buffer,
559 struct efi_mac_address *src_addr,
560 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200561{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100562 efi_status_t ret = EFI_SUCCESS;
563
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200564 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
565 (unsigned long)header_size, (unsigned long)buffer_size,
566 buffer, src_addr, dest_addr, protocol);
Alexander Graf94c4b992016-05-06 21:01:01 +0200567
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200568 efi_timer_check();
569
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100570 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200571 if (!this || !buffer) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100572 ret = EFI_INVALID_PARAMETER;
573 goto out;
574 }
575
576 /* We do not support jumbo packets */
577 if (buffer_size > PKTSIZE_ALIGN) {
578 ret = EFI_INVALID_PARAMETER;
579 goto out;
580 }
581
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200582 /* At least the IP header has to fit into the buffer */
583 if (buffer_size < this->mode->media_header_size) {
584 ret = EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100585 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200586 }
587
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200588 /*
589 * TODO:
590 * Support VLANs. Use net_set_ether() for copying the header. Use a
591 * U_BOOT_ENV_CALLBACK to update the media header size.
592 */
593 if (header_size) {
594 struct ethernet_hdr *header = buffer;
595
596 if (!dest_addr || !protocol ||
597 header_size != this->mode->media_header_size) {
598 ret = EFI_INVALID_PARAMETER;
599 goto out;
600 }
601 if (!src_addr)
602 src_addr = &this->mode->current_address;
603
604 memcpy(header->et_dest, dest_addr, ARP_HLEN);
605 memcpy(header->et_src, src_addr, ARP_HLEN);
606 header->et_protlen = htons(*protocol);
607 }
608
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100609 switch (this->mode->state) {
610 case EFI_NETWORK_STOPPED:
611 ret = EFI_NOT_STARTED;
612 goto out;
613 case EFI_NETWORK_STARTED:
614 ret = EFI_DEVICE_ERROR;
615 goto out;
616 default:
617 break;
618 }
619
Adriano Cordova54674692025-03-03 11:13:15 -0300620 eth_set_dev(netobj->dev);
621 env_set("ethact", eth_get_name());
622
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100623 /* Ethernet packets always fit, just bounce */
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100624 memcpy(transmit_buffer, buffer, buffer_size);
625 net_send_packet(transmit_buffer, buffer_size);
Alexander Graf0b12b862016-09-06 14:26:27 +0200626
Alexander Graf94c4b992016-05-06 21:01:01 +0200627 new_tx_packet = buffer;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200628 this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100629out:
630 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200631}
632
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100633/**
634 * efi_net_receive() - receive a packet from a network interface
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200635 *
636 * This function implements the Receive service of the Simple Network Protocol.
637 * See the UEFI spec for details.
638 *
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100639 * @this: the instance of the Simple Network Protocol
640 * @header_size: size of the media header
641 * @buffer_size: size of the buffer to receive the packet
642 * @buffer: buffer to receive the packet
643 * @src_addr: source MAC address
644 * @dest_addr: destination MAC address
645 * @protocol: protocol
646 * Return: status code
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200647 */
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100648static efi_status_t EFIAPI efi_net_receive
649 (struct efi_simple_network *this, size_t *header_size,
650 size_t *buffer_size, void *buffer,
651 struct efi_mac_address *src_addr,
652 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200653{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100654 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200655 struct ethernet_hdr *eth_hdr;
656 size_t hdr_size = sizeof(struct ethernet_hdr);
657 u16 protlen;
658
Alexander Graf94c4b992016-05-06 21:01:01 +0200659 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
660 buffer_size, buffer, src_addr, dest_addr, protocol);
661
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100662 /* Execute events */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200663 efi_timer_check();
Alexander Graf94c4b992016-05-06 21:01:01 +0200664
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100665 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200666 if (!this || !buffer || !buffer_size) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100667 ret = EFI_INVALID_PARAMETER;
668 goto out;
669 }
670
671 switch (this->mode->state) {
672 case EFI_NETWORK_STOPPED:
673 ret = EFI_NOT_STARTED;
674 goto out;
675 case EFI_NETWORK_STARTED:
676 ret = EFI_DEVICE_ERROR;
677 goto out;
678 default:
679 break;
680 }
681
Patrick Wildtfab89102020-10-07 11:04:33 +0200682 if (!rx_packet_num) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100683 ret = EFI_NOT_READY;
684 goto out;
685 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200686 /* Fill export parameters */
Patrick Wildtfab89102020-10-07 11:04:33 +0200687 eth_hdr = (struct ethernet_hdr *)receive_buffer[rx_packet_idx];
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200688 protlen = ntohs(eth_hdr->et_protlen);
689 if (protlen == 0x8100) {
690 hdr_size += 4;
Patrick Wildtfab89102020-10-07 11:04:33 +0200691 protlen = ntohs(*(u16 *)&receive_buffer[rx_packet_idx][hdr_size - 2]);
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200692 }
693 if (header_size)
694 *header_size = hdr_size;
695 if (dest_addr)
696 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
697 if (src_addr)
698 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
699 if (protocol)
700 *protocol = protlen;
Patrick Wildtfab89102020-10-07 11:04:33 +0200701 if (*buffer_size < receive_lengths[rx_packet_idx]) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200702 /* Packet doesn't fit, try again with bigger buffer */
Patrick Wildtfab89102020-10-07 11:04:33 +0200703 *buffer_size = receive_lengths[rx_packet_idx];
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100704 ret = EFI_BUFFER_TOO_SMALL;
705 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200706 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200707 /* Copy packet */
Patrick Wildtfab89102020-10-07 11:04:33 +0200708 memcpy(buffer, receive_buffer[rx_packet_idx],
709 receive_lengths[rx_packet_idx]);
710 *buffer_size = receive_lengths[rx_packet_idx];
711 rx_packet_idx = (rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV;
712 rx_packet_num--;
713 if (rx_packet_num)
714 wait_for_packet->is_signaled = true;
715 else
716 this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100717out:
718 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200719}
720
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100721/**
722 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
723 *
724 * This function is called by dhcp_handler().
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200725 *
726 * @pkt: packet received by dhcp_handler()
727 * @len: length of the packet received
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100728 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200729void efi_net_set_dhcp_ack(void *pkt, int len)
730{
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300731 struct efi_pxe_packet **dhcp_ack;
732 struct udevice *dev;
733
734 dhcp_ack = &dhcp_cache[next_dhcp_entry].dhcp_ack;
Alexander Graf94c4b992016-05-06 21:01:01 +0200735
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300736 /* For now this function gets called only by the current device */
737 dev = eth_get_dev();
738
739 int maxsize = sizeof(**dhcp_ack);
740
741 if (!*dhcp_ack) {
742 *dhcp_ack = malloc(maxsize);
743 if (!*dhcp_ack)
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100744 return;
745 }
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300746 memset(*dhcp_ack, 0, maxsize);
747 memcpy(*dhcp_ack, pkt, min(len, maxsize));
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100748
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300749 dhcp_cache[next_dhcp_entry].is_valid = true;
750 dhcp_cache[next_dhcp_entry].dev = dev;
751 next_dhcp_entry++;
752 next_dhcp_entry %= MAX_NUM_DHCP_ENTRIES;
Alexander Graf94c4b992016-05-06 21:01:01 +0200753}
754
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100755/**
756 * efi_net_push() - callback for received network packet
757 *
758 * This function is called when a network packet is received by eth_rx().
759 *
760 * @pkt: network packet
761 * @len: length
762 */
763static void efi_net_push(void *pkt, int len)
764{
Patrick Wildtfab89102020-10-07 11:04:33 +0200765 int rx_packet_next;
766
767 /* Check that we at least received an Ethernet header */
768 if (len < sizeof(struct ethernet_hdr))
769 return;
770
771 /* Check that the buffer won't overflow */
772 if (len > PKTSIZE_ALIGN)
773 return;
774
775 /* Can't store more than pre-alloced buffer */
776 if (rx_packet_num >= ETH_PACKETS_BATCH_RECV)
777 return;
778
779 rx_packet_next = (rx_packet_idx + rx_packet_num) %
780 ETH_PACKETS_BATCH_RECV;
781 memcpy(receive_buffer[rx_packet_next], pkt, len);
782 receive_lengths[rx_packet_next] = len;
783
784 rx_packet_num++;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100785}
786
787/**
788 * efi_network_timer_notify() - check if a new network packet has been received
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200789 *
790 * This notification function is called in every timer cycle.
791 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200792 * @event: the event for which this notification function is registered
793 * @context: event context - not used in this function
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200794 */
795static void EFIAPI efi_network_timer_notify(struct efi_event *event,
796 void *context)
797{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100798 struct efi_simple_network *this = (struct efi_simple_network *)context;
799
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200800 EFI_ENTRY("%p, %p", event, context);
801
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100802 /*
803 * Some network drivers do not support calling eth_rx() before
804 * initialization.
805 */
806 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
807 goto out;
808
Patrick Wildtfab89102020-10-07 11:04:33 +0200809 if (!rx_packet_num) {
Adriano Cordova54674692025-03-03 11:13:15 -0300810 eth_set_dev(netobj->dev);
811 env_set("ethact", eth_get_name());
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200812 push_packet = efi_net_push;
813 eth_rx();
814 push_packet = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +0200815 if (rx_packet_num) {
816 this->int_status |=
817 EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
818 wait_for_packet->is_signaled = true;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200819 }
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200820 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100821out:
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200822 EFI_EXIT(EFI_SUCCESS);
823}
824
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200825static efi_status_t EFIAPI efi_pxe_base_code_start(
826 struct efi_pxe_base_code_protocol *this,
827 u8 use_ipv6)
828{
829 return EFI_UNSUPPORTED;
830}
831
832static efi_status_t EFIAPI efi_pxe_base_code_stop(
833 struct efi_pxe_base_code_protocol *this)
834{
835 return EFI_UNSUPPORTED;
836}
837
838static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
839 struct efi_pxe_base_code_protocol *this,
840 u8 sort_offers)
841{
842 return EFI_UNSUPPORTED;
843}
844
845static efi_status_t EFIAPI efi_pxe_base_code_discover(
846 struct efi_pxe_base_code_protocol *this,
847 u16 type, u16 *layer, u8 bis,
848 struct efi_pxe_base_code_discover_info *info)
849{
850 return EFI_UNSUPPORTED;
851}
852
853static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
854 struct efi_pxe_base_code_protocol *this,
855 u32 operation, void *buffer_ptr,
856 u8 overwrite, efi_uintn_t *buffer_size,
857 struct efi_ip_address server_ip, char *filename,
858 struct efi_pxe_base_code_mtftp_info *info,
859 u8 dont_use_buffer)
860{
861 return EFI_UNSUPPORTED;
862}
863
864static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
865 struct efi_pxe_base_code_protocol *this,
866 u16 op_flags, struct efi_ip_address *dest_ip,
867 u16 *dest_port,
868 struct efi_ip_address *gateway_ip,
869 struct efi_ip_address *src_ip, u16 *src_port,
870 efi_uintn_t *header_size, void *header_ptr,
871 efi_uintn_t *buffer_size, void *buffer_ptr)
872{
873 return EFI_UNSUPPORTED;
874}
875
876static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
877 struct efi_pxe_base_code_protocol *this,
878 u16 op_flags, struct efi_ip_address *dest_ip,
879 u16 *dest_port, struct efi_ip_address *src_ip,
880 u16 *src_port, efi_uintn_t *header_size,
881 void *header_ptr, efi_uintn_t *buffer_size,
882 void *buffer_ptr)
883{
884 return EFI_UNSUPPORTED;
885}
886
887static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
888 struct efi_pxe_base_code_protocol *this,
889 struct efi_pxe_base_code_filter *new_filter)
890{
891 return EFI_UNSUPPORTED;
892}
893
894static efi_status_t EFIAPI efi_pxe_base_code_arp(
895 struct efi_pxe_base_code_protocol *this,
896 struct efi_ip_address *ip_addr,
897 struct efi_mac_address *mac_addr)
898{
899 return EFI_UNSUPPORTED;
900}
901
902static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
903 struct efi_pxe_base_code_protocol *this,
904 u8 *new_auto_arp, u8 *new_send_guid,
905 u8 *new_ttl, u8 *new_tos,
906 u8 *new_make_callback)
907{
908 return EFI_UNSUPPORTED;
909}
910
911static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
912 struct efi_pxe_base_code_protocol *this,
913 struct efi_ip_address *new_station_ip,
914 struct efi_ip_address *new_subnet_mask)
915{
916 return EFI_UNSUPPORTED;
917}
918
919static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
920 struct efi_pxe_base_code_protocol *this,
921 u8 *new_dhcp_discover_valid,
922 u8 *new_dhcp_ack_received,
923 u8 *new_proxy_offer_received,
924 u8 *new_pxe_discover_valid,
925 u8 *new_pxe_reply_received,
926 u8 *new_pxe_bis_reply_received,
927 EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
928 EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
929 EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
930 EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
931 EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
932 EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
933{
934 return EFI_UNSUPPORTED;
935}
936
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100937/**
Adriano Cordova54674692025-03-03 11:13:15 -0300938 * efi_netobj_set_dp() - set device path of a netobj
939 *
940 * @netobj: pointer to efi_net_obj
941 * @dp: device path to set, allocated by caller
942 * Return: status code
943 */
944efi_status_t efi_netobj_set_dp(struct efi_net_obj *netobj, struct efi_device_path *dp)
945{
946 efi_status_t ret;
947 struct efi_handler *phandler;
948 struct efi_device_path *new_net_dp;
949
950 if (!efi_netobj_is_active(netobj))
951 return EFI_SUCCESS;
952
953 // Create a device path for the netobj
954 new_net_dp = dp;
955 if (!new_net_dp)
956 return EFI_OUT_OF_RESOURCES;
957
958 phandler = NULL;
959 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
960
961 // If the device path protocol is not yet installed, install it
962 if (!phandler)
963 goto add;
964
965 // If it is already installed, try to update it
966 ret = efi_reinstall_protocol_interface(&netobj->header, &efi_guid_device_path,
967 phandler->protocol_interface, new_net_dp);
968 if (ret != EFI_SUCCESS)
969 return ret;
970
971 return EFI_SUCCESS;
972add:
973 ret = efi_add_protocol(&netobj->header, &efi_guid_device_path,
974 new_net_dp);
975 if (ret != EFI_SUCCESS)
976 return ret;
977
978 return EFI_SUCCESS;
979}
980
981/**
982 * efi_netobj_get_dp() - get device path of a netobj
983 *
984 * @netobj: pointer to efi_net_obj
985 * Return: device path, NULL on error
986 */
987static struct efi_device_path *efi_netobj_get_dp(struct efi_net_obj *netobj)
988{
989 struct efi_handler *phandler;
990
991 if (!efi_netobj_is_active(netobj))
992 return NULL;
993
994 phandler = NULL;
995 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
996
997 if (phandler && phandler->protocol_interface)
998 return efi_dp_dup(phandler->protocol_interface);
999
1000 return NULL;
1001}
1002
1003/**
Adriano Cordova1d70b682025-03-03 11:13:13 -03001004 * efi_net_do_start() - start the efi network stack
1005 *
1006 * This gets called from do_bootefi_exec() each time a payload gets executed.
1007 *
Adriano Cordova54674692025-03-03 11:13:15 -03001008 * @dev: net udevice
Adriano Cordova1d70b682025-03-03 11:13:13 -03001009 * Return: status code
1010 */
Adriano Cordova54674692025-03-03 11:13:15 -03001011efi_status_t efi_net_do_start(struct udevice *dev)
Adriano Cordova1d70b682025-03-03 11:13:13 -03001012{
1013 efi_status_t r = EFI_SUCCESS;
Adriano Cordova54674692025-03-03 11:13:15 -03001014 struct efi_device_path *net_dp;
1015
1016 if (dev != netobj->dev )
1017 return r;
1018
1019 efi_net_dp_from_dev(&net_dp, netobj->dev, true);
1020 // If no dp cache entry applies and there already
1021 // is a device path installed, continue
1022 if (!net_dp) {
1023 if (efi_netobj_get_dp(netobj))
1024 goto set_addr;
1025 else
1026 net_dp = efi_dp_from_eth(netobj->dev);
Adriano Cordova1d70b682025-03-03 11:13:13 -03001027
Adriano Cordova54674692025-03-03 11:13:15 -03001028 }
1029
1030 if (!net_dp)
1031 return EFI_OUT_OF_RESOURCES;
1032
1033 r = efi_netobj_set_dp(netobj, net_dp);
1034 if (r != EFI_SUCCESS)
1035 return r;
1036set_addr:
Adriano Cordova1d70b682025-03-03 11:13:13 -03001037#ifdef CONFIG_EFI_HTTP_PROTOCOL
1038 /*
1039 * No harm on doing the following. If the PXE handle is present, the client could
1040 * find it and try to get its IP address from it. In here the PXE handle is present
1041 * but the PXE protocol is not yet implmenented, so we add this in the meantime.
1042 */
1043 efi_net_get_addr((struct efi_ipv4_address *)&netobj->pxe_mode.station_ip,
Adriano Cordova54674692025-03-03 11:13:15 -03001044 (struct efi_ipv4_address *)&netobj->pxe_mode.subnet_mask, NULL, dev);
Adriano Cordova1d70b682025-03-03 11:13:13 -03001045#endif
1046
1047 return r;
1048}
1049
1050/**
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001051 * efi_net_register() - register the simple network protocol
1052 *
1053 * This gets called from do_bootefi_exec().
Adriano Cordova54674692025-03-03 11:13:15 -03001054 * @dev: net udevice
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001055 */
Adriano Cordova54674692025-03-03 11:13:15 -03001056efi_status_t efi_net_register(struct udevice *dev)
Alexander Graf94c4b992016-05-06 21:01:01 +02001057{
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001058 efi_status_t r;
Adriano Cordova62e20fb2025-03-03 11:13:16 -03001059 int i, j;
Alexander Graf94c4b992016-05-06 21:01:01 +02001060
Adriano Cordova54674692025-03-03 11:13:15 -03001061 if (!dev) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001062 /* No network device active, don't expose any */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001063 return EFI_SUCCESS;
Alexander Graf94c4b992016-05-06 21:01:01 +02001064 }
1065
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001066 /* We only expose the "active" network device, so one is enough */
Alexander Graf94c4b992016-05-06 21:01:01 +02001067 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001068 if (!netobj)
1069 goto out_of_resources;
1070
Adriano Cordova54674692025-03-03 11:13:15 -03001071 netobj->dev = dev;
1072
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001073 /* Allocate an aligned transmit buffer */
1074 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
1075 if (!transmit_buffer)
1076 goto out_of_resources;
1077 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001078
Patrick Wildtfab89102020-10-07 11:04:33 +02001079 /* Allocate a number of receive buffers */
1080 receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
1081 sizeof(*receive_buffer));
1082 if (!receive_buffer)
1083 goto out_of_resources;
1084 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
1085 receive_buffer[i] = malloc(PKTSIZE_ALIGN);
1086 if (!receive_buffer[i])
1087 goto out_of_resources;
1088 }
Adriano Cordova54674692025-03-03 11:13:15 -03001089
Patrick Wildtfab89102020-10-07 11:04:33 +02001090 receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
1091 sizeof(*receive_lengths));
1092 if (!receive_lengths)
1093 goto out_of_resources;
1094
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001095 /* Hook net up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001096 efi_add_handle(&netobj->header);
Alexander Graf94c4b992016-05-06 21:01:01 +02001097
1098 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001099 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001100 &netobj->net);
1101 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001102 goto failure_to_add_protocol;
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001103
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +02001104 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001105 &netobj->pxe);
1106 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001107 goto failure_to_add_protocol;
Heinrich Schuchardtd54396a2017-10-05 16:35:57 +02001108 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf94c4b992016-05-06 21:01:01 +02001109 netobj->net.start = efi_net_start;
1110 netobj->net.stop = efi_net_stop;
1111 netobj->net.initialize = efi_net_initialize;
1112 netobj->net.reset = efi_net_reset;
1113 netobj->net.shutdown = efi_net_shutdown;
1114 netobj->net.receive_filters = efi_net_receive_filters;
1115 netobj->net.station_address = efi_net_station_address;
1116 netobj->net.statistics = efi_net_statistics;
1117 netobj->net.mcastiptomac = efi_net_mcastiptomac;
1118 netobj->net.nvdata = efi_net_nvdata;
1119 netobj->net.get_status = efi_net_get_status;
1120 netobj->net.transmit = efi_net_transmit;
1121 netobj->net.receive = efi_net_receive;
1122 netobj->net.mode = &netobj->net_mode;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +02001123 netobj->net_mode.state = EFI_NETWORK_STOPPED;
Adriano Cordova54674692025-03-03 11:13:15 -03001124 if (dev_get_plat(dev))
1125 memcpy(netobj->net_mode.current_address.mac_addr,
1126 ((struct eth_pdata *)dev_get_plat(dev))->enetaddr, 6);
Heinrich Schuchardtd1cc6cb2017-10-05 16:35:58 +02001127 netobj->net_mode.hwaddr_size = ARP_HLEN;
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +02001128 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
Alexander Graf94c4b992016-05-06 21:01:01 +02001129 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomas27df0a72018-06-21 16:21:01 -07001130 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf94c4b992016-05-06 21:01:01 +02001131
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +02001132 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
1133 netobj->pxe.start = efi_pxe_base_code_start;
1134 netobj->pxe.stop = efi_pxe_base_code_stop;
1135 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
1136 netobj->pxe.discover = efi_pxe_base_code_discover;
1137 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
1138 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
1139 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
1140 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
1141 netobj->pxe.arp = efi_pxe_base_code_arp;
1142 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
1143 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
1144 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf94c4b992016-05-06 21:01:01 +02001145 netobj->pxe.mode = &netobj->pxe_mode;
Adriano Cordova62e20fb2025-03-03 11:13:16 -03001146
1147 /*
1148 * Scan dhcp entries for one corresponding
1149 * to this udevice, from newest to oldest
1150 */
1151 i = (next_dhcp_entry + MAX_NUM_DHCP_ENTRIES - 1) % MAX_NUM_DHCP_ENTRIES;
1152 for (j = 0; dhcp_cache[i].is_valid && j < MAX_NUM_DHCP_ENTRIES;
1153 i = (i + MAX_NUM_DHCP_ENTRIES - 1) % MAX_NUM_DHCP_ENTRIES, j++) {
1154 if (dev == dhcp_cache[i].dev) {
1155 netobj->pxe_mode.dhcp_ack = *dhcp_cache[i].dhcp_ack;
1156 break;
1157 }
1158 }
Alexander Graf94c4b992016-05-06 21:01:01 +02001159
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001160 /*
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001161 * Create WaitForPacket event.
1162 */
1163 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001164 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001165 &wait_for_packet);
1166 if (r != EFI_SUCCESS) {
1167 printf("ERROR: Failed to register network event\n");
1168 return r;
1169 }
1170 netobj->net.wait_for_packet = wait_for_packet;
Adriano Cordova54674692025-03-03 11:13:15 -03001171
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001172 /*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001173 * Create a timer event.
1174 *
1175 * The notification function is used to check if a new network packet
1176 * has been received.
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001177 *
1178 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001179 */
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001180 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001181 efi_network_timer_notify, &netobj->net, NULL,
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001182 &network_timer_event);
1183 if (r != EFI_SUCCESS) {
1184 printf("ERROR: Failed to register network event\n");
1185 return r;
1186 }
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001187 /* Network is time critical, create event in every timer cycle */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001188 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
1189 if (r != EFI_SUCCESS) {
1190 printf("ERROR: Failed to set network timer\n");
1191 return r;
1192 }
1193
Adriano Cordova9debc902024-12-04 00:05:25 -03001194#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
1195 r = efi_ipconfig_register(&netobj->header, &netobj->ip4_config2);
1196 if (r != EFI_SUCCESS)
1197 goto failure_to_add_protocol;
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001198#endif
1199
1200#ifdef CONFIG_EFI_HTTP_PROTOCOL
1201 r = efi_http_register(&netobj->header, &netobj->http_service_binding);
1202 if (r != EFI_SUCCESS)
1203 goto failure_to_add_protocol;
Adriano Cordova9debc902024-12-04 00:05:25 -03001204#endif
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001205 return EFI_SUCCESS;
1206failure_to_add_protocol:
1207 printf("ERROR: Failure to add protocol\n");
1208 return r;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001209out_of_resources:
1210 free(netobj);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +01001211 netobj = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +02001212 free(transmit_buffer);
1213 if (receive_buffer)
1214 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
1215 free(receive_buffer[i]);
1216 free(receive_buffer);
1217 free(receive_lengths);
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001218 printf("ERROR: Out of memory\n");
1219 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001220}
Adriano Cordova3c951362024-12-04 00:05:21 -03001221
1222/**
Adriano Cordova54674692025-03-03 11:13:15 -03001223 * efi_net_new_dp() - update device path associated to a net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001224 *
1225 * This gets called to update the device path when a new boot
1226 * file is downloaded
1227 *
1228 * @dev: dev to set the device path from
1229 * @server: remote server address
Adriano Cordova54674692025-03-03 11:13:15 -03001230 * @udev: net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001231 * Return: status code
1232 */
Adriano Cordova54674692025-03-03 11:13:15 -03001233efi_status_t efi_net_new_dp(const char *dev, const char *server, struct udevice *udev)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001234{
Adriano Cordova54674692025-03-03 11:13:15 -03001235 efi_status_t ret;
1236 struct efi_device_path *old_net_dp, *new_net_dp;
1237 struct efi_device_path **dp;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001238
Adriano Cordova54674692025-03-03 11:13:15 -03001239 dp = &dp_cache[next_dp_entry].net_dp;
1240
1241 dp_cache[next_dp_entry].dev = udev;
1242 dp_cache[next_dp_entry].is_valid = true;
1243 next_dp_entry++;
1244 next_dp_entry %= MAX_NUM_DP_ENTRIES;
1245
1246 old_net_dp = *dp;
1247 new_net_dp = NULL;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001248 if (!strcmp(dev, "Net"))
Adriano Cordova54674692025-03-03 11:13:15 -03001249 new_net_dp = efi_dp_from_eth(udev);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001250 else if (!strcmp(dev, "Http"))
Adriano Cordova54674692025-03-03 11:13:15 -03001251 new_net_dp = efi_dp_from_http(server, udev);
1252 if (!new_net_dp)
1253 return EFI_OUT_OF_RESOURCES;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001254
Adriano Cordova54674692025-03-03 11:13:15 -03001255 *dp = new_net_dp;
1256 // Free the old cache entry
1257 efi_free_pool(old_net_dp);
1258
1259 if (!netobj || netobj->dev != udev)
1260 return EFI_SUCCESS;
1261
1262 new_net_dp = efi_dp_dup(*dp);
1263 if (!new_net_dp)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001264 return EFI_OUT_OF_RESOURCES;
Adriano Cordova54674692025-03-03 11:13:15 -03001265 ret = efi_netobj_set_dp(netobj, new_net_dp);
1266 if (ret != EFI_SUCCESS)
1267 efi_free_pool(new_net_dp);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001268
Adriano Cordova54674692025-03-03 11:13:15 -03001269 return ret;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001270}
1271
1272/**
Adriano Cordova54674692025-03-03 11:13:15 -03001273 * efi_net_dp_from_dev() - get device path associated to a net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001274 *
1275 * Produce a copy of the current device path
1276 *
Adriano Cordova54674692025-03-03 11:13:15 -03001277 * @dp: copy of the current device path
1278 * @udev: net udevice
1279 * @cache_only: get device path from cache only
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001280 */
Adriano Cordova54674692025-03-03 11:13:15 -03001281void efi_net_dp_from_dev(struct efi_device_path **dp, struct udevice *udev, bool cache_only)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001282{
Adriano Cordova54674692025-03-03 11:13:15 -03001283 int i, j;
1284
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001285 if (!dp)
1286 return;
Adriano Cordova54674692025-03-03 11:13:15 -03001287
1288 *dp = NULL;
1289
1290 if (cache_only)
1291 goto cache;
1292
1293 if (netobj && netobj->dev == udev) {
1294 *dp = efi_netobj_get_dp(netobj);
1295 if (*dp)
1296 return;
1297 }
1298cache:
1299 // Search in the cache
1300 i = (next_dp_entry + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES;
1301 for (j = 0; dp_cache[i].is_valid && j < MAX_NUM_DP_ENTRIES;
1302 i = (i + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES, j++) {
1303 if (dp_cache[i].dev == udev) {
1304 *dp = efi_dp_dup(dp_cache[i].net_dp);
1305 return;
1306 }
1307 }
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001308}
1309
1310/**
Adriano Cordova3c951362024-12-04 00:05:21 -03001311 * efi_net_get_addr() - get IP address information
1312 *
1313 * Copy the current IP address, mask, and gateway into the
1314 * efi_ipv4_address structs pointed to by ip, mask and gw,
1315 * respectively.
1316 *
1317 * @ip: pointer to an efi_ipv4_address struct to
1318 * be filled with the current IP address
1319 * @mask: pointer to an efi_ipv4_address struct to
1320 * be filled with the current network mask
1321 * @gw: pointer to an efi_ipv4_address struct to be
1322 * filled with the current network gateway
Adriano Cordova54674692025-03-03 11:13:15 -03001323 * @dev: udevice
Adriano Cordova3c951362024-12-04 00:05:21 -03001324 */
1325void efi_net_get_addr(struct efi_ipv4_address *ip,
1326 struct efi_ipv4_address *mask,
Adriano Cordova54674692025-03-03 11:13:15 -03001327 struct efi_ipv4_address *gw,
1328 struct udevice *dev)
Adriano Cordova3c951362024-12-04 00:05:21 -03001329{
Adriano Cordova54674692025-03-03 11:13:15 -03001330 if (!dev)
1331 dev = eth_get_dev();
Adriano Cordova3c951362024-12-04 00:05:21 -03001332#ifdef CONFIG_NET_LWIP
1333 char ipstr[] = "ipaddr\0\0";
1334 char maskstr[] = "netmask\0\0";
1335 char gwstr[] = "gatewayip\0\0";
1336 int idx;
1337 struct in_addr tmp;
1338 char *env;
1339
Adriano Cordova54674692025-03-03 11:13:15 -03001340 idx = dev_seq(dev);
Adriano Cordova3c951362024-12-04 00:05:21 -03001341
1342 if (idx < 0 || idx > 99) {
1343 log_err("unexpected idx %d\n", idx);
1344 return;
1345 }
1346
1347 if (idx) {
1348 sprintf(ipstr, "ipaddr%d", idx);
1349 sprintf(maskstr, "netmask%d", idx);
1350 sprintf(gwstr, "gatewayip%d", idx);
1351 }
1352
1353 env = env_get(ipstr);
1354 if (env && ip) {
1355 tmp = string_to_ip(env);
1356 memcpy(ip, &tmp, sizeof(tmp));
1357 }
1358
1359 env = env_get(maskstr);
1360 if (env && mask) {
1361 tmp = string_to_ip(env);
1362 memcpy(mask, &tmp, sizeof(tmp));
1363 }
1364 env = env_get(gwstr);
1365 if (env && gw) {
1366 tmp = string_to_ip(env);
1367 memcpy(gw, &tmp, sizeof(tmp));
1368 }
1369#else
1370 if (ip)
1371 memcpy(ip, &net_ip, sizeof(net_ip));
1372 if (mask)
1373 memcpy(mask, &net_netmask, sizeof(net_netmask));
1374#endif
1375}
1376
1377/**
1378 * efi_net_set_addr() - set IP address information
1379 *
1380 * Set the current IP address, mask, and gateway to the
1381 * efi_ipv4_address structs pointed to by ip, mask and gw,
1382 * respectively.
1383 *
1384 * @ip: pointer to new IP address
1385 * @mask: pointer to new network mask to set
1386 * @gw: pointer to new network gateway
Adriano Cordova54674692025-03-03 11:13:15 -03001387 * @dev: udevice
Adriano Cordova3c951362024-12-04 00:05:21 -03001388 */
1389void efi_net_set_addr(struct efi_ipv4_address *ip,
1390 struct efi_ipv4_address *mask,
Adriano Cordova54674692025-03-03 11:13:15 -03001391 struct efi_ipv4_address *gw,
1392 struct udevice *dev)
Adriano Cordova3c951362024-12-04 00:05:21 -03001393{
Adriano Cordova54674692025-03-03 11:13:15 -03001394 if (!dev)
1395 dev = eth_get_dev();
Adriano Cordova3c951362024-12-04 00:05:21 -03001396#ifdef CONFIG_NET_LWIP
1397 char ipstr[] = "ipaddr\0\0";
1398 char maskstr[] = "netmask\0\0";
1399 char gwstr[] = "gatewayip\0\0";
1400 int idx;
1401 struct in_addr *addr;
1402 char tmp[46];
1403
Adriano Cordova54674692025-03-03 11:13:15 -03001404 idx = dev_seq(dev);
Adriano Cordova3c951362024-12-04 00:05:21 -03001405
1406 if (idx < 0 || idx > 99) {
1407 log_err("unexpected idx %d\n", idx);
1408 return;
1409 }
1410
1411 if (idx) {
1412 sprintf(ipstr, "ipaddr%d", idx);
1413 sprintf(maskstr, "netmask%d", idx);
1414 sprintf(gwstr, "gatewayip%d", idx);
1415 }
1416
1417 if (ip) {
1418 addr = (struct in_addr *)ip;
1419 ip_to_string(*addr, tmp);
1420 env_set(ipstr, tmp);
1421 }
1422
1423 if (mask) {
1424 addr = (struct in_addr *)mask;
1425 ip_to_string(*addr, tmp);
1426 env_set(maskstr, tmp);
1427 }
1428
1429 if (gw) {
1430 addr = (struct in_addr *)gw;
1431 ip_to_string(*addr, tmp);
1432 env_set(gwstr, tmp);
1433 }
1434#else
1435 if (ip)
1436 memcpy(&net_ip, ip, sizeof(*ip));
1437 if (mask)
1438 memcpy(&net_netmask, mask, sizeof(*mask));
1439#endif
1440}
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001441
Adriano Cordova54674692025-03-03 11:13:15 -03001442#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001443/**
1444 * efi_net_set_buffer() - allocate a buffer of min 64K
1445 *
1446 * @buffer: allocated buffer
1447 * @size: desired buffer size
1448 * Return: status code
1449 */
1450static efi_status_t efi_net_set_buffer(void **buffer, size_t size)
1451{
1452 efi_status_t ret = EFI_SUCCESS;
1453
1454 if (size < SZ_64K)
1455 size = SZ_64K;
1456
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001457 *buffer = efi_alloc(size);
1458 if (!*buffer)
1459 ret = EFI_OUT_OF_RESOURCES;
1460
1461 efi_wget_info.buffer_size = (ulong)size;
1462
1463 return ret;
1464}
1465
1466/**
1467 * efi_net_parse_headers() - parse HTTP headers
1468 *
1469 * Parses the raw buffer efi_wget_info.headers into an array headers
1470 * of efi structs http_headers. The array should be at least
1471 * MAX_HTTP_HEADERS long.
1472 *
1473 * @num_headers: number of headers
1474 * @headers: caller provided array of struct http_headers
1475 */
1476void efi_net_parse_headers(ulong *num_headers, struct http_header *headers)
1477{
1478 if (!num_headers || !headers)
1479 return;
1480
1481 // Populate info with http headers.
1482 *num_headers = 0;
1483 const uchar *line_start = efi_wget_info.headers;
1484 const uchar *line_end;
1485 ulong count;
1486 struct http_header *current_header;
1487 const uchar *separator;
1488 size_t name_length, value_length;
1489
1490 // Skip the first line (request or status line)
1491 line_end = strstr(line_start, "\r\n");
1492
1493 if (line_end)
1494 line_start = line_end + 2;
1495
1496 while ((line_end = strstr(line_start, "\r\n")) != NULL) {
1497 count = *num_headers;
1498 if (line_start == line_end || count >= MAX_HTTP_HEADERS)
1499 break;
1500 current_header = headers + count;
1501 separator = strchr(line_start, ':');
1502 if (separator) {
1503 name_length = separator - line_start;
1504 ++separator;
1505 while (*separator == ' ')
1506 ++separator;
1507 value_length = line_end - separator;
1508 if (name_length < MAX_HTTP_HEADER_NAME &&
1509 value_length < MAX_HTTP_HEADER_VALUE) {
1510 strncpy(current_header->name, line_start, name_length);
1511 current_header->name[name_length] = '\0';
1512 strncpy(current_header->value, separator, value_length);
1513 current_header->value[value_length] = '\0';
1514 (*num_headers)++;
1515 }
1516 }
1517 line_start = line_end + 2;
1518 }
1519}
1520
1521/**
1522 * efi_net_do_request() - issue an HTTP request using wget
1523 *
1524 * @url: url
1525 * @method: HTTP method
1526 * @buffer: data buffer
1527 * @status_code: HTTP status code
1528 * @file_size: file size in bytes
1529 * @headers_buffer: headers buffer
1530 * Return: status code
1531 */
1532efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, void **buffer,
1533 u32 *status_code, ulong *file_size, char *headers_buffer)
1534{
1535 efi_status_t ret = EFI_SUCCESS;
1536 int wget_ret;
1537 static bool last_head;
1538
1539 if (!buffer || !file_size)
1540 return EFI_ABORTED;
1541
1542 efi_wget_info.method = (enum wget_http_method)method;
1543 efi_wget_info.headers = headers_buffer;
1544
1545 switch (method) {
1546 case HTTP_METHOD_GET:
1547 ret = efi_net_set_buffer(buffer, last_head ? (size_t)efi_wget_info.hdr_cont_len : 0);
1548 if (ret != EFI_SUCCESS)
1549 goto out;
Adriano Cordova54674692025-03-03 11:13:15 -03001550 eth_set_dev(netobj->dev);
1551 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001552 wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info);
1553 if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) {
1554 // Try again with updated buffer size
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001555 efi_free_pool(*buffer);
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001556 ret = efi_net_set_buffer(buffer, (size_t)efi_wget_info.hdr_cont_len);
1557 if (ret != EFI_SUCCESS)
1558 goto out;
Adriano Cordova54674692025-03-03 11:13:15 -03001559 eth_set_dev(netobj->dev);
1560 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001561 if (wget_request((ulong)*buffer, url, &efi_wget_info)) {
1562 efi_free_pool(*buffer);
1563 ret = EFI_DEVICE_ERROR;
1564 goto out;
1565 }
1566 } else if (wget_ret) {
1567 efi_free_pool(*buffer);
1568 ret = EFI_DEVICE_ERROR;
1569 goto out;
1570 }
1571 // Pass the actual number of received bytes to the application
1572 *file_size = efi_wget_info.file_size;
1573 *status_code = efi_wget_info.status_code;
1574 last_head = false;
1575 break;
1576 case HTTP_METHOD_HEAD:
1577 ret = efi_net_set_buffer(buffer, 0);
1578 if (ret != EFI_SUCCESS)
1579 goto out;
Adriano Cordova54674692025-03-03 11:13:15 -03001580 eth_set_dev(netobj->dev);
1581 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001582 wget_request((ulong)*buffer, url, &efi_wget_info);
1583 *file_size = 0;
1584 *status_code = efi_wget_info.status_code;
1585 last_head = true;
1586 break;
1587 default:
1588 ret = EFI_UNSUPPORTED;
1589 break;
1590 }
1591
1592out:
1593 return ret;
1594}
Adriano Cordova54674692025-03-03 11:13:15 -03001595#endif