blob: 9db738ead9dd535a1fdeb574243bb51807ebdb74 [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>
Tom Rinic31301c2025-05-15 17:31:50 -060021#include <env.h>
Adriano Cordova93cba0f2024-12-04 00:05:23 -030022#include <dm.h>
Adriano Cordova0d1f5092024-12-04 00:05:24 -030023#include <linux/sizes.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020024#include <malloc.h>
Adriano Cordova3c951362024-12-04 00:05:21 -030025#include <vsprintf.h>
Simon Glass274e0b02020-05-10 11:39:56 -060026#include <net.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020027
Adriano Cordova28d67772025-03-03 11:13:17 -030028#define MAX_EFI_NET_OBJS 10
Adriano Cordova62e20fb2025-03-03 11:13:16 -030029#define MAX_NUM_DHCP_ENTRIES 10
Adriano Cordova54674692025-03-03 11:13:15 -030030#define MAX_NUM_DP_ENTRIES 10
31
Adriano Cordova7f2bcd42025-03-03 11:13:11 -030032const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020033static const efi_guid_t efi_pxe_base_code_protocol_guid =
34 EFI_PXE_BASE_CODE_PROTOCOL_GUID;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010035
Adriano Cordova54674692025-03-03 11:13:15 -030036struct dp_entry {
37 struct efi_device_path *net_dp;
38 struct udevice *dev;
39 bool is_valid;
40};
41
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020042/*
Adriano Cordova54674692025-03-03 11:13:15 -030043 * The network device path cache. An entry is added when a new bootfile
44 * is downloaded from the network. If the bootfile is then loaded as an
45 * efi image, the most recent entry corresponding to the device is passed
46 * as the device path of the loaded image.
Adriano Cordova93cba0f2024-12-04 00:05:23 -030047 */
Adriano Cordova54674692025-03-03 11:13:15 -030048static struct dp_entry dp_cache[MAX_NUM_DP_ENTRIES];
49static int next_dp_entry;
Adriano Cordova93cba0f2024-12-04 00:05:23 -030050
Adriano Cordova54674692025-03-03 11:13:15 -030051#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
Adriano Cordova0d1f5092024-12-04 00:05:24 -030052static struct wget_http_info efi_wget_info = {
53 .set_bootdev = false,
54 .check_buffer_size = true,
55
56};
Adriano Cordova54674692025-03-03 11:13:15 -030057#endif
Adriano Cordova0d1f5092024-12-04 00:05:24 -030058
Adriano Cordova62e20fb2025-03-03 11:13:16 -030059struct dhcp_entry {
60 struct efi_pxe_packet *dhcp_ack;
61 struct udevice *dev;
62 bool is_valid;
63};
64
65static struct dhcp_entry dhcp_cache[MAX_NUM_DHCP_ENTRIES];
66static int next_dhcp_entry;
67
Heinrich Schuchardt72928722018-09-26 05:27:56 +020068/**
69 * struct efi_net_obj - EFI object representing a network interface
70 *
Adriano Cordova9debc902024-12-04 00:05:25 -030071 * @header: EFI object header
Adriano Cordova54674692025-03-03 11:13:15 -030072 * @dev: net udevice
Adriano Cordova9debc902024-12-04 00:05:25 -030073 * @net: simple network protocol interface
74 * @net_mode: status of the network interface
75 * @pxe: PXE base code protocol interface
76 * @pxe_mode: status of the PXE base code protocol
77 * @ip4_config2: IP4 Config2 protocol interface
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030078 * @http_service_binding: Http service binding protocol interface
Adriano Cordova28d67772025-03-03 11:13:17 -030079 * @new_tx_packet: new transmit packet
80 * @transmit_buffer: transmit buffer
81 * @receive_buffer: array of receive buffers
82 * @receive_lengths: array of lengths for received packets
83 * @rx_packet_idx: index of the current receive packet
84 * @rx_packet_num: number of received packets
85 * @wait_for_packet: signaled when a packet has been received
86 * @network_timer_event: event to check for new network packets.
87 * @efi_seq_num: sequence number of the EFI net object.
Heinrich Schuchardt72928722018-09-26 05:27:56 +020088 */
Alexander Graf94c4b992016-05-06 21:01:01 +020089struct efi_net_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020090 struct efi_object header;
Adriano Cordova54674692025-03-03 11:13:15 -030091 struct udevice *dev;
Alexander Graf94c4b992016-05-06 21:01:01 +020092 struct efi_simple_network net;
93 struct efi_simple_network_mode net_mode;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020094 struct efi_pxe_base_code_protocol pxe;
Alexander Graf94c4b992016-05-06 21:01:01 +020095 struct efi_pxe_mode pxe_mode;
Adriano Cordova9debc902024-12-04 00:05:25 -030096#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
97 struct efi_ip4_config2_protocol ip4_config2;
98#endif
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030099#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
100 struct efi_service_binding_protocol http_service_binding;
101#endif
Adriano Cordova28d67772025-03-03 11:13:17 -0300102 void *new_tx_packet;
103 void *transmit_buffer;
104 uchar **receive_buffer;
105 size_t *receive_lengths;
106 int rx_packet_idx;
107 int rx_packet_num;
108 struct efi_event *wait_for_packet;
109 struct efi_event *network_timer_event;
110 int efi_seq_num;
Alexander Graf94c4b992016-05-06 21:01:01 +0200111};
112
Adriano Cordova28d67772025-03-03 11:13:17 -0300113static int curr_efi_net_obj;
114static struct efi_net_obj *net_objs[MAX_EFI_NET_OBJS];
115
116/**
Adriano Cordova54674692025-03-03 11:13:15 -0300117 * efi_netobj_is_active() - checks if a netobj is active in the efi subsystem
118 *
Adriano Cordova28d67772025-03-03 11:13:17 -0300119 * @netobj: pointer to efi_net_obj
120 * Return: true if active
Adriano Cordova54674692025-03-03 11:13:15 -0300121 */
122static bool efi_netobj_is_active(struct efi_net_obj *netobj)
123{
124 if (!netobj || !efi_search_obj(&netobj->header))
125 return false;
126
127 return true;
128}
129
Adriano Cordova28d67772025-03-03 11:13:17 -0300130/*
131 * efi_netobj_from_snp() - get efi_net_obj from simple network protocol
132 *
133 *
134 * @snp: pointer to the simple network protocol
135 * Return: pointer to efi_net_obj, NULL on error
136 */
137static struct efi_net_obj *efi_netobj_from_snp(struct efi_simple_network *snp)
138{
139 int i;
140
141 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
142 if (net_objs[i] && &net_objs[i]->net == snp) {
143 // Do not register duplicate devices
144 return net_objs[i];
145 }
146 }
147 return NULL;
148}
Adriano Cordova54674692025-03-03 11:13:15 -0300149
150/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100151 * efi_net_start() - start the network interface
152 *
153 * This function implements the Start service of the
154 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
155 * (UEFI) specification for details.
156 *
157 * @this: pointer to the protocol instance
158 * Return: status code
159 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200160static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
161{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100162 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300163 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100164
Alexander Graf94c4b992016-05-06 21:01:01 +0200165 EFI_ENTRY("%p", this);
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100166 /* Check parameters */
167 if (!this) {
168 ret = EFI_INVALID_PARAMETER;
169 goto out;
170 }
171
Adriano Cordova28d67772025-03-03 11:13:17 -0300172 nt = efi_netobj_from_snp(this);
173
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200174 if (this->mode->state != EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100175 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200176 } else {
177 this->int_status = 0;
Adriano Cordova28d67772025-03-03 11:13:17 -0300178 nt->wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100179 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200180 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100181out:
182 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200183}
184
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100185/*
186 * efi_net_stop() - stop the network interface
187 *
188 * This function implements the Stop service of the
189 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
190 * (UEFI) specification for details.
191 *
192 * @this: pointer to the protocol instance
193 * Return: status code
194 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200195static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
196{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100197 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300198 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100199
Alexander Graf94c4b992016-05-06 21:01:01 +0200200 EFI_ENTRY("%p", this);
201
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100202 /* Check parameters */
203 if (!this) {
204 ret = EFI_INVALID_PARAMETER;
205 goto out;
206 }
207
Adriano Cordova28d67772025-03-03 11:13:17 -0300208 nt = efi_netobj_from_snp(this);
209
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200210 if (this->mode->state == EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100211 ret = EFI_NOT_STARTED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200212 } else {
213 /* Disable hardware and put it into the reset state */
Adriano Cordova28d67772025-03-03 11:13:17 -0300214 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300215 env_set("ethact", eth_get_name());
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200216 eth_halt();
Patrick Wildtfab89102020-10-07 11:04:33 +0200217 /* Clear cache of packets */
Adriano Cordova28d67772025-03-03 11:13:17 -0300218 nt->rx_packet_num = 0;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100219 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200220 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100221out:
222 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200223}
224
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200225/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100226 * efi_net_initialize() - initialize the network interface
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200227 *
228 * This function implements the Initialize service of the
229 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
230 * (UEFI) specification for details.
231 *
232 * @this: pointer to the protocol instance
233 * @extra_rx: extra receive buffer to be allocated
234 * @extra_tx: extra transmit buffer to be allocated
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100235 * Return: status code
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200236 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200237static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
238 ulong extra_rx, ulong extra_tx)
239{
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200240 int ret;
241 efi_status_t r = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300242 struct efi_net_obj *nt;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200243
Alexander Graf94c4b992016-05-06 21:01:01 +0200244 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
245
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100246 /* Check parameters */
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200247 if (!this) {
248 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100249 goto out;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200250 }
Adriano Cordova28d67772025-03-03 11:13:17 -0300251 nt = efi_netobj_from_snp(this);
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200252
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200253 switch (this->mode->state) {
254 case EFI_NETWORK_INITIALIZED:
255 case EFI_NETWORK_STARTED:
256 break;
257 default:
258 r = EFI_NOT_STARTED;
259 goto out;
260 }
261
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200262 /* Setup packet buffers */
263 net_init();
Patrick Wildtfab89102020-10-07 11:04:33 +0200264 /* Clear cache of packets */
Adriano Cordova28d67772025-03-03 11:13:17 -0300265 nt->rx_packet_num = 0;
Adriano Cordova54674692025-03-03 11:13:15 -0300266 /* Set the net device corresponding to the efi net object */
Adriano Cordova28d67772025-03-03 11:13:17 -0300267 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300268 env_set("ethact", eth_get_name());
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200269 /* Get hardware ready for send and receive operations */
Adriano Cordova28d67772025-03-03 11:13:17 -0300270 ret = eth_start_udev(nt->dev);
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200271 if (ret < 0) {
272 eth_halt();
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100273 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200274 r = EFI_DEVICE_ERROR;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100275 goto out;
276 } else {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200277 this->int_status = 0;
Adriano Cordova28d67772025-03-03 11:13:17 -0300278 nt->wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100279 this->mode->state = EFI_NETWORK_INITIALIZED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200280 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100281out:
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200282 return EFI_EXIT(r);
Alexander Graf94c4b992016-05-06 21:01:01 +0200283}
284
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100285/*
286 * efi_net_reset() - reinitialize the network interface
287 *
288 * This function implements the Reset service of the
289 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
290 * (UEFI) specification for details.
291 *
292 * @this: pointer to the protocol instance
293 * @extended_verification: execute exhaustive verification
294 * Return: status code
295 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200296static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
297 int extended_verification)
298{
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200299 efi_status_t ret;
300
Alexander Graf94c4b992016-05-06 21:01:01 +0200301 EFI_ENTRY("%p, %x", this, extended_verification);
302
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200303 /* Check parameters */
304 if (!this) {
305 ret = EFI_INVALID_PARAMETER;
306 goto out;
307 }
308
309 switch (this->mode->state) {
310 case EFI_NETWORK_INITIALIZED:
311 break;
312 case EFI_NETWORK_STOPPED:
313 ret = EFI_NOT_STARTED;
314 goto out;
315 default:
316 ret = EFI_DEVICE_ERROR;
317 goto out;
318 }
319
320 this->mode->state = EFI_NETWORK_STARTED;
321 ret = EFI_CALL(efi_net_initialize(this, 0, 0));
322out:
323 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200324}
325
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100326/*
327 * efi_net_shutdown() - shut down the network interface
328 *
329 * This function implements the Shutdown service of the
330 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
331 * (UEFI) specification for details.
332 *
333 * @this: pointer to the protocol instance
334 * Return: status code
335 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200336static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
337{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100338 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300339 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100340
Alexander Graf94c4b992016-05-06 21:01:01 +0200341 EFI_ENTRY("%p", this);
342
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100343 /* Check parameters */
344 if (!this) {
345 ret = EFI_INVALID_PARAMETER;
346 goto out;
347 }
Adriano Cordova28d67772025-03-03 11:13:17 -0300348 nt = efi_netobj_from_snp(this);
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100349
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200350 switch (this->mode->state) {
351 case EFI_NETWORK_INITIALIZED:
352 break;
353 case EFI_NETWORK_STOPPED:
354 ret = EFI_NOT_STARTED;
355 goto out;
356 default:
357 ret = EFI_DEVICE_ERROR;
358 goto out;
359 }
360
Adriano Cordova28d67772025-03-03 11:13:17 -0300361 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300362 env_set("ethact", eth_get_name());
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100363 eth_halt();
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300364
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200365 this->int_status = 0;
Adriano Cordova28d67772025-03-03 11:13:17 -0300366 nt->wait_for_packet->is_signaled = false;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200367 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100368
369out:
370 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200371}
372
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100373/*
374 * efi_net_receive_filters() - mange multicast receive filters
375 *
376 * This function implements the ReceiveFilters service of the
377 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
378 * (UEFI) specification for details.
379 *
380 * @this: pointer to the protocol instance
381 * @enable: bit mask of receive filters to enable
382 * @disable: bit mask of receive filters to disable
383 * @reset_mcast_filter: true resets contents of the filters
384 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
385 * @mcast_filter: list of new filters
386 * Return: status code
387 */
388static efi_status_t EFIAPI efi_net_receive_filters
389 (struct efi_simple_network *this, u32 enable, u32 disable,
390 int reset_mcast_filter, ulong mcast_filter_count,
391 struct efi_mac_address *mcast_filter)
Alexander Graf94c4b992016-05-06 21:01:01 +0200392{
393 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
394 reset_mcast_filter, mcast_filter_count, mcast_filter);
395
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200396 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200397}
398
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100399/*
400 * efi_net_station_address() - set the hardware MAC address
401 *
402 * This function implements the StationAddress service of the
403 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
404 * (UEFI) specification for details.
405 *
406 * @this: pointer to the protocol instance
407 * @reset: if true reset the address to default
408 * @new_mac: new MAC address
409 * Return: status code
410 */
411static efi_status_t EFIAPI efi_net_station_address
412 (struct efi_simple_network *this, int reset,
413 struct efi_mac_address *new_mac)
Alexander Graf94c4b992016-05-06 21:01:01 +0200414{
415 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
416
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200417 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200418}
419
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100420/*
421 * efi_net_statistics() - reset or collect statistics of the network interface
422 *
423 * This function implements the Statistics service of the
424 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
425 * (UEFI) specification for details.
426 *
427 * @this: pointer to the protocol instance
428 * @reset: if true, the statistics are reset
429 * @stat_size: size of the statistics table
430 * @stat_table: table to receive the statistics
431 * Return: status code
432 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200433static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
434 int reset, ulong *stat_size,
435 void *stat_table)
436{
437 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
438
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200439 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200440}
441
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100442/*
443 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
444 *
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200445 * This function implements the MCastIPtoMAC service of the
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100446 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
447 * (UEFI) specification for details.
448 *
449 * @this: pointer to the protocol instance
450 * @ipv6: true if the IP address is an IPv6 address
451 * @ip: IP address
452 * @mac: MAC address
453 * Return: status code
454 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200455static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
456 int ipv6,
457 struct efi_ip_address *ip,
458 struct efi_mac_address *mac)
459{
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200460 efi_status_t ret = EFI_SUCCESS;
461
Alexander Graf94c4b992016-05-06 21:01:01 +0200462 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
463
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200464 if (!this || !ip || !mac) {
465 ret = EFI_INVALID_PARAMETER;
466 goto out;
467 }
468
469 if (ipv6) {
470 ret = EFI_UNSUPPORTED;
471 goto out;
472 }
473
474 /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */
475 if ((ip->ip_addr[0] & 0xf0) != 0xe0) {
476 ret = EFI_INVALID_PARAMETER;
477 goto out;
478 };
479
480 switch (this->mode->state) {
481 case EFI_NETWORK_INITIALIZED:
482 case EFI_NETWORK_STARTED:
483 break;
484 default:
485 ret = EFI_NOT_STARTED;
486 goto out;
487 }
488
489 memset(mac, 0, sizeof(struct efi_mac_address));
490
491 /*
492 * Copy lower 23 bits of IPv4 multi-cast address
493 * RFC 1112, RFC 7042 2.1.1.
494 */
495 mac->mac_addr[0] = 0x01;
496 mac->mac_addr[1] = 0x00;
497 mac->mac_addr[2] = 0x5E;
498 mac->mac_addr[3] = ip->ip_addr[1] & 0x7F;
499 mac->mac_addr[4] = ip->ip_addr[2];
500 mac->mac_addr[5] = ip->ip_addr[3];
501out:
502 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200503}
504
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100505/**
506 * efi_net_nvdata() - read or write NVRAM
507 *
508 * This function implements the GetStatus service of the Simple Network
509 * Protocol. See the UEFI spec for details.
510 *
511 * @this: the instance of the Simple Network Protocol
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200512 * @read_write: true for read, false for write
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100513 * @offset: offset in NVRAM
514 * @buffer_size: size of buffer
515 * @buffer: buffer
516 * Return: status code
517 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200518static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
519 int read_write, ulong offset,
520 ulong buffer_size, char *buffer)
521{
522 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
523 buffer);
524
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200525 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200526}
527
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100528/**
529 * efi_net_get_status() - get interrupt status
530 *
531 * This function implements the GetStatus service of the Simple Network
532 * Protocol. See the UEFI spec for details.
533 *
534 * @this: the instance of the Simple Network Protocol
535 * @int_status: interface status
536 * @txbuf: transmission buffer
537 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200538static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
539 u32 *int_status, void **txbuf)
540{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100541 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300542 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100543
Alexander Graf94c4b992016-05-06 21:01:01 +0200544 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
545
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200546 efi_timer_check();
547
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100548 /* Check parameters */
549 if (!this) {
550 ret = EFI_INVALID_PARAMETER;
551 goto out;
552 }
553
Adriano Cordova28d67772025-03-03 11:13:17 -0300554 nt = efi_netobj_from_snp(this);
555
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100556 switch (this->mode->state) {
557 case EFI_NETWORK_STOPPED:
558 ret = EFI_NOT_STARTED;
559 goto out;
560 case EFI_NETWORK_STARTED:
561 ret = EFI_DEVICE_ERROR;
562 goto out;
563 default:
564 break;
565 }
566
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200567 if (int_status) {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200568 *int_status = this->int_status;
569 this->int_status = 0;
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200570 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200571 if (txbuf)
Adriano Cordova28d67772025-03-03 11:13:17 -0300572 *txbuf = nt->new_tx_packet;
Alexander Graf94c4b992016-05-06 21:01:01 +0200573
Adriano Cordova28d67772025-03-03 11:13:17 -0300574 nt->new_tx_packet = NULL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100575out:
576 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200577}
578
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100579/**
580 * efi_net_transmit() - transmit a packet
581 *
582 * This function implements the Transmit service of the Simple Network Protocol.
583 * See the UEFI spec for details.
584 *
585 * @this: the instance of the Simple Network Protocol
586 * @header_size: size of the media header
587 * @buffer_size: size of the buffer to receive the packet
588 * @buffer: buffer to receive the packet
589 * @src_addr: source hardware MAC address
590 * @dest_addr: destination hardware MAC address
591 * @protocol: type of header to build
592 * Return: status code
593 */
594static efi_status_t EFIAPI efi_net_transmit
595 (struct efi_simple_network *this, size_t header_size,
596 size_t buffer_size, void *buffer,
597 struct efi_mac_address *src_addr,
598 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200599{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100600 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300601 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100602
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200603 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
604 (unsigned long)header_size, (unsigned long)buffer_size,
605 buffer, src_addr, dest_addr, protocol);
Alexander Graf94c4b992016-05-06 21:01:01 +0200606
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200607 efi_timer_check();
608
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100609 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200610 if (!this || !buffer) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100611 ret = EFI_INVALID_PARAMETER;
612 goto out;
613 }
614
Adriano Cordova28d67772025-03-03 11:13:17 -0300615 nt = efi_netobj_from_snp(this);
616
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100617 /* We do not support jumbo packets */
618 if (buffer_size > PKTSIZE_ALIGN) {
619 ret = EFI_INVALID_PARAMETER;
620 goto out;
621 }
622
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200623 /* At least the IP header has to fit into the buffer */
624 if (buffer_size < this->mode->media_header_size) {
625 ret = EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100626 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200627 }
628
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200629 /*
630 * TODO:
631 * Support VLANs. Use net_set_ether() for copying the header. Use a
632 * U_BOOT_ENV_CALLBACK to update the media header size.
633 */
634 if (header_size) {
635 struct ethernet_hdr *header = buffer;
636
637 if (!dest_addr || !protocol ||
638 header_size != this->mode->media_header_size) {
639 ret = EFI_INVALID_PARAMETER;
640 goto out;
641 }
642 if (!src_addr)
643 src_addr = &this->mode->current_address;
644
645 memcpy(header->et_dest, dest_addr, ARP_HLEN);
646 memcpy(header->et_src, src_addr, ARP_HLEN);
647 header->et_protlen = htons(*protocol);
648 }
649
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100650 switch (this->mode->state) {
651 case EFI_NETWORK_STOPPED:
652 ret = EFI_NOT_STARTED;
653 goto out;
654 case EFI_NETWORK_STARTED:
655 ret = EFI_DEVICE_ERROR;
656 goto out;
657 default:
658 break;
659 }
660
Adriano Cordova28d67772025-03-03 11:13:17 -0300661 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300662 env_set("ethact", eth_get_name());
663
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100664 /* Ethernet packets always fit, just bounce */
Adriano Cordova28d67772025-03-03 11:13:17 -0300665 memcpy(nt->transmit_buffer, buffer, buffer_size);
666 net_send_packet(nt->transmit_buffer, buffer_size);
Alexander Graf0b12b862016-09-06 14:26:27 +0200667
Adriano Cordova28d67772025-03-03 11:13:17 -0300668 nt->new_tx_packet = buffer;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200669 this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100670out:
671 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200672}
673
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100674/**
675 * efi_net_receive() - receive a packet from a network interface
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200676 *
677 * This function implements the Receive service of the Simple Network Protocol.
678 * See the UEFI spec for details.
679 *
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100680 * @this: the instance of the Simple Network Protocol
681 * @header_size: size of the media header
682 * @buffer_size: size of the buffer to receive the packet
683 * @buffer: buffer to receive the packet
684 * @src_addr: source MAC address
685 * @dest_addr: destination MAC address
686 * @protocol: protocol
687 * Return: status code
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200688 */
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100689static efi_status_t EFIAPI efi_net_receive
690 (struct efi_simple_network *this, size_t *header_size,
691 size_t *buffer_size, void *buffer,
692 struct efi_mac_address *src_addr,
693 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200694{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100695 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200696 struct ethernet_hdr *eth_hdr;
697 size_t hdr_size = sizeof(struct ethernet_hdr);
698 u16 protlen;
Adriano Cordova28d67772025-03-03 11:13:17 -0300699 struct efi_net_obj *nt;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200700
Alexander Graf94c4b992016-05-06 21:01:01 +0200701 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
702 buffer_size, buffer, src_addr, dest_addr, protocol);
703
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100704 /* Execute events */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200705 efi_timer_check();
Alexander Graf94c4b992016-05-06 21:01:01 +0200706
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100707 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200708 if (!this || !buffer || !buffer_size) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100709 ret = EFI_INVALID_PARAMETER;
710 goto out;
711 }
712
Adriano Cordova28d67772025-03-03 11:13:17 -0300713 nt = efi_netobj_from_snp(this);
714
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100715 switch (this->mode->state) {
716 case EFI_NETWORK_STOPPED:
717 ret = EFI_NOT_STARTED;
718 goto out;
719 case EFI_NETWORK_STARTED:
720 ret = EFI_DEVICE_ERROR;
721 goto out;
722 default:
723 break;
724 }
725
Adriano Cordova28d67772025-03-03 11:13:17 -0300726 if (!nt->rx_packet_num) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100727 ret = EFI_NOT_READY;
728 goto out;
729 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200730 /* Fill export parameters */
Adriano Cordova28d67772025-03-03 11:13:17 -0300731 eth_hdr = (struct ethernet_hdr *)nt->receive_buffer[nt->rx_packet_idx];
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200732 protlen = ntohs(eth_hdr->et_protlen);
733 if (protlen == 0x8100) {
734 hdr_size += 4;
Adriano Cordova28d67772025-03-03 11:13:17 -0300735 protlen = ntohs(*(u16 *)&nt->receive_buffer[nt->rx_packet_idx][hdr_size - 2]);
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200736 }
737 if (header_size)
738 *header_size = hdr_size;
739 if (dest_addr)
740 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
741 if (src_addr)
742 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
743 if (protocol)
744 *protocol = protlen;
Adriano Cordova28d67772025-03-03 11:13:17 -0300745 if (*buffer_size < nt->receive_lengths[nt->rx_packet_idx]) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200746 /* Packet doesn't fit, try again with bigger buffer */
Adriano Cordova28d67772025-03-03 11:13:17 -0300747 *buffer_size = nt->receive_lengths[nt->rx_packet_idx];
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100748 ret = EFI_BUFFER_TOO_SMALL;
749 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200750 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200751 /* Copy packet */
Adriano Cordova28d67772025-03-03 11:13:17 -0300752 memcpy(buffer, nt->receive_buffer[nt->rx_packet_idx],
753 nt->receive_lengths[nt->rx_packet_idx]);
754 *buffer_size = nt->receive_lengths[nt->rx_packet_idx];
755 nt->rx_packet_idx = (nt->rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV;
756 nt->rx_packet_num--;
757 if (nt->rx_packet_num)
758 nt->wait_for_packet->is_signaled = true;
Patrick Wildtfab89102020-10-07 11:04:33 +0200759 else
760 this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100761out:
762 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200763}
764
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100765/**
766 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
767 *
768 * This function is called by dhcp_handler().
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200769 *
770 * @pkt: packet received by dhcp_handler()
771 * @len: length of the packet received
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100772 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200773void efi_net_set_dhcp_ack(void *pkt, int len)
774{
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300775 struct efi_pxe_packet **dhcp_ack;
776 struct udevice *dev;
Adriano Cordova28d67772025-03-03 11:13:17 -0300777 int i;
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300778
779 dhcp_ack = &dhcp_cache[next_dhcp_entry].dhcp_ack;
Alexander Graf94c4b992016-05-06 21:01:01 +0200780
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300781 /* For now this function gets called only by the current device */
782 dev = eth_get_dev();
783
784 int maxsize = sizeof(**dhcp_ack);
785
786 if (!*dhcp_ack) {
787 *dhcp_ack = malloc(maxsize);
788 if (!*dhcp_ack)
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100789 return;
790 }
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300791 memset(*dhcp_ack, 0, maxsize);
792 memcpy(*dhcp_ack, pkt, min(len, maxsize));
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100793
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300794 dhcp_cache[next_dhcp_entry].is_valid = true;
795 dhcp_cache[next_dhcp_entry].dev = dev;
796 next_dhcp_entry++;
797 next_dhcp_entry %= MAX_NUM_DHCP_ENTRIES;
Adriano Cordova28d67772025-03-03 11:13:17 -0300798
799 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
800 if (net_objs[i] && net_objs[i]->dev == dev) {
801 net_objs[i]->pxe_mode.dhcp_ack = **dhcp_ack;
802 }
803 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200804}
805
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100806/**
807 * efi_net_push() - callback for received network packet
808 *
809 * This function is called when a network packet is received by eth_rx().
810 *
811 * @pkt: network packet
812 * @len: length
813 */
814static void efi_net_push(void *pkt, int len)
815{
Patrick Wildtfab89102020-10-07 11:04:33 +0200816 int rx_packet_next;
Adriano Cordova28d67772025-03-03 11:13:17 -0300817 struct efi_net_obj *nt;
818
819 nt = net_objs[curr_efi_net_obj];
820 if (!nt)
821 return;
Patrick Wildtfab89102020-10-07 11:04:33 +0200822
823 /* Check that we at least received an Ethernet header */
824 if (len < sizeof(struct ethernet_hdr))
825 return;
826
827 /* Check that the buffer won't overflow */
828 if (len > PKTSIZE_ALIGN)
829 return;
830
831 /* Can't store more than pre-alloced buffer */
Adriano Cordova28d67772025-03-03 11:13:17 -0300832 if (nt->rx_packet_num >= ETH_PACKETS_BATCH_RECV)
Patrick Wildtfab89102020-10-07 11:04:33 +0200833 return;
834
Adriano Cordova28d67772025-03-03 11:13:17 -0300835 rx_packet_next = (nt->rx_packet_idx + nt->rx_packet_num) %
Patrick Wildtfab89102020-10-07 11:04:33 +0200836 ETH_PACKETS_BATCH_RECV;
Adriano Cordova28d67772025-03-03 11:13:17 -0300837 memcpy(nt->receive_buffer[rx_packet_next], pkt, len);
838 nt->receive_lengths[rx_packet_next] = len;
Patrick Wildtfab89102020-10-07 11:04:33 +0200839
Adriano Cordova28d67772025-03-03 11:13:17 -0300840 nt->rx_packet_num++;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100841}
842
843/**
844 * efi_network_timer_notify() - check if a new network packet has been received
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200845 *
846 * This notification function is called in every timer cycle.
847 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200848 * @event: the event for which this notification function is registered
849 * @context: event context - not used in this function
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200850 */
851static void EFIAPI efi_network_timer_notify(struct efi_event *event,
852 void *context)
853{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100854 struct efi_simple_network *this = (struct efi_simple_network *)context;
Adriano Cordova28d67772025-03-03 11:13:17 -0300855 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100856
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200857 EFI_ENTRY("%p, %p", event, context);
858
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100859 /*
860 * Some network drivers do not support calling eth_rx() before
861 * initialization.
862 */
863 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
864 goto out;
865
Adriano Cordova28d67772025-03-03 11:13:17 -0300866 nt = efi_netobj_from_snp(this);
867 curr_efi_net_obj = nt->efi_seq_num;
868
869 if (!nt->rx_packet_num) {
870 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300871 env_set("ethact", eth_get_name());
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200872 push_packet = efi_net_push;
873 eth_rx();
874 push_packet = NULL;
Adriano Cordova28d67772025-03-03 11:13:17 -0300875 if (nt->rx_packet_num) {
Patrick Wildtfab89102020-10-07 11:04:33 +0200876 this->int_status |=
877 EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Adriano Cordova28d67772025-03-03 11:13:17 -0300878 nt->wait_for_packet->is_signaled = true;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200879 }
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200880 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100881out:
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200882 EFI_EXIT(EFI_SUCCESS);
883}
884
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200885static efi_status_t EFIAPI efi_pxe_base_code_start(
886 struct efi_pxe_base_code_protocol *this,
887 u8 use_ipv6)
888{
889 return EFI_UNSUPPORTED;
890}
891
892static efi_status_t EFIAPI efi_pxe_base_code_stop(
893 struct efi_pxe_base_code_protocol *this)
894{
895 return EFI_UNSUPPORTED;
896}
897
898static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
899 struct efi_pxe_base_code_protocol *this,
900 u8 sort_offers)
901{
902 return EFI_UNSUPPORTED;
903}
904
905static efi_status_t EFIAPI efi_pxe_base_code_discover(
906 struct efi_pxe_base_code_protocol *this,
907 u16 type, u16 *layer, u8 bis,
908 struct efi_pxe_base_code_discover_info *info)
909{
910 return EFI_UNSUPPORTED;
911}
912
913static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
914 struct efi_pxe_base_code_protocol *this,
915 u32 operation, void *buffer_ptr,
916 u8 overwrite, efi_uintn_t *buffer_size,
917 struct efi_ip_address server_ip, char *filename,
918 struct efi_pxe_base_code_mtftp_info *info,
919 u8 dont_use_buffer)
920{
921 return EFI_UNSUPPORTED;
922}
923
924static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
925 struct efi_pxe_base_code_protocol *this,
926 u16 op_flags, struct efi_ip_address *dest_ip,
927 u16 *dest_port,
928 struct efi_ip_address *gateway_ip,
929 struct efi_ip_address *src_ip, u16 *src_port,
930 efi_uintn_t *header_size, void *header_ptr,
931 efi_uintn_t *buffer_size, void *buffer_ptr)
932{
933 return EFI_UNSUPPORTED;
934}
935
936static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
937 struct efi_pxe_base_code_protocol *this,
938 u16 op_flags, struct efi_ip_address *dest_ip,
939 u16 *dest_port, struct efi_ip_address *src_ip,
940 u16 *src_port, efi_uintn_t *header_size,
941 void *header_ptr, efi_uintn_t *buffer_size,
942 void *buffer_ptr)
943{
944 return EFI_UNSUPPORTED;
945}
946
947static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
948 struct efi_pxe_base_code_protocol *this,
949 struct efi_pxe_base_code_filter *new_filter)
950{
951 return EFI_UNSUPPORTED;
952}
953
954static efi_status_t EFIAPI efi_pxe_base_code_arp(
955 struct efi_pxe_base_code_protocol *this,
956 struct efi_ip_address *ip_addr,
957 struct efi_mac_address *mac_addr)
958{
959 return EFI_UNSUPPORTED;
960}
961
962static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
963 struct efi_pxe_base_code_protocol *this,
964 u8 *new_auto_arp, u8 *new_send_guid,
965 u8 *new_ttl, u8 *new_tos,
966 u8 *new_make_callback)
967{
968 return EFI_UNSUPPORTED;
969}
970
971static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
972 struct efi_pxe_base_code_protocol *this,
973 struct efi_ip_address *new_station_ip,
974 struct efi_ip_address *new_subnet_mask)
975{
976 return EFI_UNSUPPORTED;
977}
978
979static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
980 struct efi_pxe_base_code_protocol *this,
981 u8 *new_dhcp_discover_valid,
982 u8 *new_dhcp_ack_received,
983 u8 *new_proxy_offer_received,
984 u8 *new_pxe_discover_valid,
985 u8 *new_pxe_reply_received,
986 u8 *new_pxe_bis_reply_received,
987 EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
988 EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
989 EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
990 EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
991 EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
992 EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
993{
994 return EFI_UNSUPPORTED;
995}
996
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100997/**
Adriano Cordova54674692025-03-03 11:13:15 -0300998 * efi_netobj_set_dp() - set device path of a netobj
999 *
1000 * @netobj: pointer to efi_net_obj
1001 * @dp: device path to set, allocated by caller
1002 * Return: status code
1003 */
1004efi_status_t efi_netobj_set_dp(struct efi_net_obj *netobj, struct efi_device_path *dp)
1005{
1006 efi_status_t ret;
1007 struct efi_handler *phandler;
1008 struct efi_device_path *new_net_dp;
1009
1010 if (!efi_netobj_is_active(netobj))
1011 return EFI_SUCCESS;
1012
1013 // Create a device path for the netobj
1014 new_net_dp = dp;
1015 if (!new_net_dp)
1016 return EFI_OUT_OF_RESOURCES;
1017
1018 phandler = NULL;
1019 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
1020
1021 // If the device path protocol is not yet installed, install it
1022 if (!phandler)
1023 goto add;
1024
1025 // If it is already installed, try to update it
1026 ret = efi_reinstall_protocol_interface(&netobj->header, &efi_guid_device_path,
1027 phandler->protocol_interface, new_net_dp);
1028 if (ret != EFI_SUCCESS)
1029 return ret;
1030
1031 return EFI_SUCCESS;
1032add:
1033 ret = efi_add_protocol(&netobj->header, &efi_guid_device_path,
1034 new_net_dp);
1035 if (ret != EFI_SUCCESS)
1036 return ret;
1037
1038 return EFI_SUCCESS;
1039}
1040
1041/**
1042 * efi_netobj_get_dp() - get device path of a netobj
1043 *
1044 * @netobj: pointer to efi_net_obj
1045 * Return: device path, NULL on error
1046 */
1047static struct efi_device_path *efi_netobj_get_dp(struct efi_net_obj *netobj)
1048{
1049 struct efi_handler *phandler;
1050
1051 if (!efi_netobj_is_active(netobj))
1052 return NULL;
1053
1054 phandler = NULL;
1055 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
1056
1057 if (phandler && phandler->protocol_interface)
1058 return efi_dp_dup(phandler->protocol_interface);
1059
1060 return NULL;
1061}
1062
1063/**
Adriano Cordova1d70b682025-03-03 11:13:13 -03001064 * efi_net_do_start() - start the efi network stack
1065 *
1066 * This gets called from do_bootefi_exec() each time a payload gets executed.
1067 *
Adriano Cordova54674692025-03-03 11:13:15 -03001068 * @dev: net udevice
Adriano Cordova1d70b682025-03-03 11:13:13 -03001069 * Return: status code
1070 */
Adriano Cordova54674692025-03-03 11:13:15 -03001071efi_status_t efi_net_do_start(struct udevice *dev)
Adriano Cordova1d70b682025-03-03 11:13:13 -03001072{
1073 efi_status_t r = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -03001074 struct efi_net_obj *netobj;
Adriano Cordova54674692025-03-03 11:13:15 -03001075 struct efi_device_path *net_dp;
Adriano Cordova28d67772025-03-03 11:13:17 -03001076 int i;
Adriano Cordova54674692025-03-03 11:13:15 -03001077
Adriano Cordova28d67772025-03-03 11:13:17 -03001078 netobj = NULL;
1079 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1080 if (net_objs[i] && net_objs[i]->dev == dev) {
1081 netobj = net_objs[i];
1082 break;
1083 }
1084 }
1085
1086 if (!efi_netobj_is_active(netobj))
Adriano Cordova54674692025-03-03 11:13:15 -03001087 return r;
1088
1089 efi_net_dp_from_dev(&net_dp, netobj->dev, true);
1090 // If no dp cache entry applies and there already
1091 // is a device path installed, continue
1092 if (!net_dp) {
1093 if (efi_netobj_get_dp(netobj))
1094 goto set_addr;
1095 else
1096 net_dp = efi_dp_from_eth(netobj->dev);
Adriano Cordova1d70b682025-03-03 11:13:13 -03001097
Adriano Cordova54674692025-03-03 11:13:15 -03001098 }
1099
1100 if (!net_dp)
1101 return EFI_OUT_OF_RESOURCES;
1102
1103 r = efi_netobj_set_dp(netobj, net_dp);
1104 if (r != EFI_SUCCESS)
1105 return r;
1106set_addr:
Adriano Cordova1d70b682025-03-03 11:13:13 -03001107#ifdef CONFIG_EFI_HTTP_PROTOCOL
1108 /*
1109 * No harm on doing the following. If the PXE handle is present, the client could
1110 * find it and try to get its IP address from it. In here the PXE handle is present
1111 * but the PXE protocol is not yet implmenented, so we add this in the meantime.
1112 */
1113 efi_net_get_addr((struct efi_ipv4_address *)&netobj->pxe_mode.station_ip,
Adriano Cordova54674692025-03-03 11:13:15 -03001114 (struct efi_ipv4_address *)&netobj->pxe_mode.subnet_mask, NULL, dev);
Adriano Cordova1d70b682025-03-03 11:13:13 -03001115#endif
1116
1117 return r;
1118}
1119
1120/**
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001121 * efi_net_register() - register the simple network protocol
1122 *
1123 * This gets called from do_bootefi_exec().
Adriano Cordova54674692025-03-03 11:13:15 -03001124 * @dev: net udevice
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001125 */
Adriano Cordova54674692025-03-03 11:13:15 -03001126efi_status_t efi_net_register(struct udevice *dev)
Alexander Graf94c4b992016-05-06 21:01:01 +02001127{
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001128 efi_status_t r;
Adriano Cordova28d67772025-03-03 11:13:17 -03001129 int seq_num;
1130 struct efi_net_obj *netobj;
1131 void *transmit_buffer = NULL;
1132 uchar **receive_buffer = NULL;
1133 size_t *receive_lengths;
Adriano Cordova62e20fb2025-03-03 11:13:16 -03001134 int i, j;
Alexander Graf94c4b992016-05-06 21:01:01 +02001135
Adriano Cordova54674692025-03-03 11:13:15 -03001136 if (!dev) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001137 /* No network device active, don't expose any */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001138 return EFI_SUCCESS;
Alexander Graf94c4b992016-05-06 21:01:01 +02001139 }
Adriano Cordova28d67772025-03-03 11:13:17 -03001140
1141 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1142 if (net_objs[i] && net_objs[i]->dev == dev) {
1143 // Do not register duplicate devices
1144 return EFI_SUCCESS;
1145 }
1146 }
1147
1148 seq_num = -1;
1149 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1150 if (!net_objs[i]) {
1151 seq_num = i;
1152 break;
1153 }
1154 }
1155 if (seq_num < 0)
1156 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001157
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001158 /* We only expose the "active" network device, so one is enough */
Alexander Graf94c4b992016-05-06 21:01:01 +02001159 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001160 if (!netobj)
1161 goto out_of_resources;
1162
Adriano Cordova54674692025-03-03 11:13:15 -03001163 netobj->dev = dev;
1164
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001165 /* Allocate an aligned transmit buffer */
1166 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
1167 if (!transmit_buffer)
1168 goto out_of_resources;
1169 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Adriano Cordova28d67772025-03-03 11:13:17 -03001170 netobj->transmit_buffer = transmit_buffer;
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001171
Patrick Wildtfab89102020-10-07 11:04:33 +02001172 /* Allocate a number of receive buffers */
1173 receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
1174 sizeof(*receive_buffer));
1175 if (!receive_buffer)
1176 goto out_of_resources;
1177 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
1178 receive_buffer[i] = malloc(PKTSIZE_ALIGN);
1179 if (!receive_buffer[i])
1180 goto out_of_resources;
1181 }
Adriano Cordova28d67772025-03-03 11:13:17 -03001182 netobj->receive_buffer = receive_buffer;
Adriano Cordova54674692025-03-03 11:13:15 -03001183
Patrick Wildtfab89102020-10-07 11:04:33 +02001184 receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
1185 sizeof(*receive_lengths));
1186 if (!receive_lengths)
1187 goto out_of_resources;
Adriano Cordova28d67772025-03-03 11:13:17 -03001188 netobj->receive_lengths = receive_lengths;
Patrick Wildtfab89102020-10-07 11:04:33 +02001189
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001190 /* Hook net up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001191 efi_add_handle(&netobj->header);
Alexander Graf94c4b992016-05-06 21:01:01 +02001192
1193 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001194 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001195 &netobj->net);
1196 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001197 goto failure_to_add_protocol;
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001198
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +02001199 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001200 &netobj->pxe);
1201 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001202 goto failure_to_add_protocol;
Heinrich Schuchardtd54396a2017-10-05 16:35:57 +02001203 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf94c4b992016-05-06 21:01:01 +02001204 netobj->net.start = efi_net_start;
1205 netobj->net.stop = efi_net_stop;
1206 netobj->net.initialize = efi_net_initialize;
1207 netobj->net.reset = efi_net_reset;
1208 netobj->net.shutdown = efi_net_shutdown;
1209 netobj->net.receive_filters = efi_net_receive_filters;
1210 netobj->net.station_address = efi_net_station_address;
1211 netobj->net.statistics = efi_net_statistics;
1212 netobj->net.mcastiptomac = efi_net_mcastiptomac;
1213 netobj->net.nvdata = efi_net_nvdata;
1214 netobj->net.get_status = efi_net_get_status;
1215 netobj->net.transmit = efi_net_transmit;
1216 netobj->net.receive = efi_net_receive;
1217 netobj->net.mode = &netobj->net_mode;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +02001218 netobj->net_mode.state = EFI_NETWORK_STOPPED;
Adriano Cordova54674692025-03-03 11:13:15 -03001219 if (dev_get_plat(dev))
1220 memcpy(netobj->net_mode.current_address.mac_addr,
1221 ((struct eth_pdata *)dev_get_plat(dev))->enetaddr, 6);
Heinrich Schuchardtd1cc6cb2017-10-05 16:35:58 +02001222 netobj->net_mode.hwaddr_size = ARP_HLEN;
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +02001223 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
Alexander Graf94c4b992016-05-06 21:01:01 +02001224 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomas27df0a72018-06-21 16:21:01 -07001225 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf94c4b992016-05-06 21:01:01 +02001226
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +02001227 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
1228 netobj->pxe.start = efi_pxe_base_code_start;
1229 netobj->pxe.stop = efi_pxe_base_code_stop;
1230 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
1231 netobj->pxe.discover = efi_pxe_base_code_discover;
1232 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
1233 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
1234 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
1235 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
1236 netobj->pxe.arp = efi_pxe_base_code_arp;
1237 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
1238 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
1239 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf94c4b992016-05-06 21:01:01 +02001240 netobj->pxe.mode = &netobj->pxe_mode;
Adriano Cordova62e20fb2025-03-03 11:13:16 -03001241
1242 /*
1243 * Scan dhcp entries for one corresponding
1244 * to this udevice, from newest to oldest
1245 */
1246 i = (next_dhcp_entry + MAX_NUM_DHCP_ENTRIES - 1) % MAX_NUM_DHCP_ENTRIES;
1247 for (j = 0; dhcp_cache[i].is_valid && j < MAX_NUM_DHCP_ENTRIES;
1248 i = (i + MAX_NUM_DHCP_ENTRIES - 1) % MAX_NUM_DHCP_ENTRIES, j++) {
1249 if (dev == dhcp_cache[i].dev) {
1250 netobj->pxe_mode.dhcp_ack = *dhcp_cache[i].dhcp_ack;
1251 break;
1252 }
1253 }
Alexander Graf94c4b992016-05-06 21:01:01 +02001254
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001255 /*
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001256 * Create WaitForPacket event.
1257 */
1258 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001259 efi_network_timer_notify, NULL, NULL,
Adriano Cordova28d67772025-03-03 11:13:17 -03001260 &netobj->wait_for_packet);
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001261 if (r != EFI_SUCCESS) {
1262 printf("ERROR: Failed to register network event\n");
1263 return r;
1264 }
Adriano Cordova28d67772025-03-03 11:13:17 -03001265 netobj->net.wait_for_packet = netobj->wait_for_packet;
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001266 /*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001267 * Create a timer event.
1268 *
1269 * The notification function is used to check if a new network packet
1270 * has been received.
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001271 *
1272 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001273 */
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001274 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001275 efi_network_timer_notify, &netobj->net, NULL,
Adriano Cordova28d67772025-03-03 11:13:17 -03001276 &netobj->network_timer_event);
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001277 if (r != EFI_SUCCESS) {
1278 printf("ERROR: Failed to register network event\n");
1279 return r;
1280 }
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001281 /* Network is time critical, create event in every timer cycle */
Adriano Cordova28d67772025-03-03 11:13:17 -03001282 r = efi_set_timer(netobj->network_timer_event, EFI_TIMER_PERIODIC, 0);
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001283 if (r != EFI_SUCCESS) {
1284 printf("ERROR: Failed to set network timer\n");
1285 return r;
1286 }
1287
Adriano Cordova9debc902024-12-04 00:05:25 -03001288#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
1289 r = efi_ipconfig_register(&netobj->header, &netobj->ip4_config2);
1290 if (r != EFI_SUCCESS)
1291 goto failure_to_add_protocol;
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001292#endif
1293
1294#ifdef CONFIG_EFI_HTTP_PROTOCOL
1295 r = efi_http_register(&netobj->header, &netobj->http_service_binding);
1296 if (r != EFI_SUCCESS)
1297 goto failure_to_add_protocol;
Adriano Cordova9debc902024-12-04 00:05:25 -03001298#endif
Adriano Cordova28d67772025-03-03 11:13:17 -03001299 netobj->efi_seq_num = seq_num;
1300 net_objs[seq_num] = netobj;
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001301 return EFI_SUCCESS;
1302failure_to_add_protocol:
1303 printf("ERROR: Failure to add protocol\n");
1304 return r;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001305out_of_resources:
1306 free(netobj);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +01001307 netobj = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +02001308 free(transmit_buffer);
1309 if (receive_buffer)
1310 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
1311 free(receive_buffer[i]);
1312 free(receive_buffer);
1313 free(receive_lengths);
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001314 printf("ERROR: Out of memory\n");
1315 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001316}
Adriano Cordova3c951362024-12-04 00:05:21 -03001317
1318/**
Adriano Cordova54674692025-03-03 11:13:15 -03001319 * efi_net_new_dp() - update device path associated to a net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001320 *
1321 * This gets called to update the device path when a new boot
1322 * file is downloaded
1323 *
1324 * @dev: dev to set the device path from
1325 * @server: remote server address
Adriano Cordova54674692025-03-03 11:13:15 -03001326 * @udev: net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001327 * Return: status code
1328 */
Adriano Cordova54674692025-03-03 11:13:15 -03001329efi_status_t efi_net_new_dp(const char *dev, const char *server, struct udevice *udev)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001330{
Adriano Cordova54674692025-03-03 11:13:15 -03001331 efi_status_t ret;
Adriano Cordova28d67772025-03-03 11:13:17 -03001332 struct efi_net_obj *netobj;
Adriano Cordova54674692025-03-03 11:13:15 -03001333 struct efi_device_path *old_net_dp, *new_net_dp;
1334 struct efi_device_path **dp;
Adriano Cordova28d67772025-03-03 11:13:17 -03001335 int i;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001336
Adriano Cordova54674692025-03-03 11:13:15 -03001337 dp = &dp_cache[next_dp_entry].net_dp;
1338
1339 dp_cache[next_dp_entry].dev = udev;
1340 dp_cache[next_dp_entry].is_valid = true;
1341 next_dp_entry++;
1342 next_dp_entry %= MAX_NUM_DP_ENTRIES;
1343
1344 old_net_dp = *dp;
1345 new_net_dp = NULL;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001346 if (!strcmp(dev, "Net"))
Adriano Cordova54674692025-03-03 11:13:15 -03001347 new_net_dp = efi_dp_from_eth(udev);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001348 else if (!strcmp(dev, "Http"))
Adriano Cordova54674692025-03-03 11:13:15 -03001349 new_net_dp = efi_dp_from_http(server, udev);
1350 if (!new_net_dp)
1351 return EFI_OUT_OF_RESOURCES;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001352
Adriano Cordova54674692025-03-03 11:13:15 -03001353 *dp = new_net_dp;
1354 // Free the old cache entry
1355 efi_free_pool(old_net_dp);
1356
Adriano Cordova28d67772025-03-03 11:13:17 -03001357 netobj = NULL;
1358 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1359 if (net_objs[i] && net_objs[i]->dev == udev) {
1360 netobj = net_objs[i];
1361 break;
1362 }
1363 }
1364 if (!netobj)
Adriano Cordova54674692025-03-03 11:13:15 -03001365 return EFI_SUCCESS;
1366
1367 new_net_dp = efi_dp_dup(*dp);
1368 if (!new_net_dp)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001369 return EFI_OUT_OF_RESOURCES;
Adriano Cordova54674692025-03-03 11:13:15 -03001370 ret = efi_netobj_set_dp(netobj, new_net_dp);
1371 if (ret != EFI_SUCCESS)
1372 efi_free_pool(new_net_dp);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001373
Adriano Cordova54674692025-03-03 11:13:15 -03001374 return ret;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001375}
1376
1377/**
Adriano Cordova54674692025-03-03 11:13:15 -03001378 * efi_net_dp_from_dev() - get device path associated to a net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001379 *
1380 * Produce a copy of the current device path
1381 *
Adriano Cordova54674692025-03-03 11:13:15 -03001382 * @dp: copy of the current device path
1383 * @udev: net udevice
1384 * @cache_only: get device path from cache only
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001385 */
Adriano Cordova54674692025-03-03 11:13:15 -03001386void efi_net_dp_from_dev(struct efi_device_path **dp, struct udevice *udev, bool cache_only)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001387{
Adriano Cordova54674692025-03-03 11:13:15 -03001388 int i, j;
1389
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001390 if (!dp)
1391 return;
Adriano Cordova54674692025-03-03 11:13:15 -03001392
1393 *dp = NULL;
1394
1395 if (cache_only)
1396 goto cache;
1397
Adriano Cordova28d67772025-03-03 11:13:17 -03001398 // If a netobj matches:
1399 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1400 if (net_objs[i] && net_objs[i]->dev == udev) {
1401 *dp = efi_netobj_get_dp(net_objs[i]);
1402 if (*dp)
1403 return;
1404 }
Adriano Cordova54674692025-03-03 11:13:15 -03001405 }
1406cache:
1407 // Search in the cache
1408 i = (next_dp_entry + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES;
1409 for (j = 0; dp_cache[i].is_valid && j < MAX_NUM_DP_ENTRIES;
1410 i = (i + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES, j++) {
1411 if (dp_cache[i].dev == udev) {
1412 *dp = efi_dp_dup(dp_cache[i].net_dp);
1413 return;
1414 }
1415 }
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001416}
1417
1418/**
Adriano Cordova3c951362024-12-04 00:05:21 -03001419 * efi_net_get_addr() - get IP address information
1420 *
1421 * Copy the current IP address, mask, and gateway into the
1422 * efi_ipv4_address structs pointed to by ip, mask and gw,
1423 * respectively.
1424 *
1425 * @ip: pointer to an efi_ipv4_address struct to
1426 * be filled with the current IP address
1427 * @mask: pointer to an efi_ipv4_address struct to
1428 * be filled with the current network mask
1429 * @gw: pointer to an efi_ipv4_address struct to be
1430 * filled with the current network gateway
Adriano Cordova54674692025-03-03 11:13:15 -03001431 * @dev: udevice
Adriano Cordova3c951362024-12-04 00:05:21 -03001432 */
1433void efi_net_get_addr(struct efi_ipv4_address *ip,
1434 struct efi_ipv4_address *mask,
Adriano Cordova54674692025-03-03 11:13:15 -03001435 struct efi_ipv4_address *gw,
1436 struct udevice *dev)
Adriano Cordova3c951362024-12-04 00:05:21 -03001437{
Adriano Cordova54674692025-03-03 11:13:15 -03001438 if (!dev)
1439 dev = eth_get_dev();
Adriano Cordova3c951362024-12-04 00:05:21 -03001440#ifdef CONFIG_NET_LWIP
1441 char ipstr[] = "ipaddr\0\0";
1442 char maskstr[] = "netmask\0\0";
1443 char gwstr[] = "gatewayip\0\0";
1444 int idx;
1445 struct in_addr tmp;
1446 char *env;
1447
Adriano Cordova54674692025-03-03 11:13:15 -03001448 idx = dev_seq(dev);
Adriano Cordova3c951362024-12-04 00:05:21 -03001449
1450 if (idx < 0 || idx > 99) {
1451 log_err("unexpected idx %d\n", idx);
1452 return;
1453 }
1454
1455 if (idx) {
1456 sprintf(ipstr, "ipaddr%d", idx);
1457 sprintf(maskstr, "netmask%d", idx);
1458 sprintf(gwstr, "gatewayip%d", idx);
1459 }
1460
1461 env = env_get(ipstr);
1462 if (env && ip) {
1463 tmp = string_to_ip(env);
1464 memcpy(ip, &tmp, sizeof(tmp));
1465 }
1466
1467 env = env_get(maskstr);
1468 if (env && mask) {
1469 tmp = string_to_ip(env);
1470 memcpy(mask, &tmp, sizeof(tmp));
1471 }
1472 env = env_get(gwstr);
1473 if (env && gw) {
1474 tmp = string_to_ip(env);
1475 memcpy(gw, &tmp, sizeof(tmp));
1476 }
1477#else
1478 if (ip)
1479 memcpy(ip, &net_ip, sizeof(net_ip));
1480 if (mask)
1481 memcpy(mask, &net_netmask, sizeof(net_netmask));
1482#endif
1483}
1484
1485/**
1486 * efi_net_set_addr() - set IP address information
1487 *
1488 * Set the current IP address, mask, and gateway to the
1489 * efi_ipv4_address structs pointed to by ip, mask and gw,
1490 * respectively.
1491 *
1492 * @ip: pointer to new IP address
1493 * @mask: pointer to new network mask to set
1494 * @gw: pointer to new network gateway
Adriano Cordova54674692025-03-03 11:13:15 -03001495 * @dev: udevice
Adriano Cordova3c951362024-12-04 00:05:21 -03001496 */
1497void efi_net_set_addr(struct efi_ipv4_address *ip,
1498 struct efi_ipv4_address *mask,
Adriano Cordova54674692025-03-03 11:13:15 -03001499 struct efi_ipv4_address *gw,
1500 struct udevice *dev)
Adriano Cordova3c951362024-12-04 00:05:21 -03001501{
Adriano Cordova54674692025-03-03 11:13:15 -03001502 if (!dev)
1503 dev = eth_get_dev();
Adriano Cordova3c951362024-12-04 00:05:21 -03001504#ifdef CONFIG_NET_LWIP
1505 char ipstr[] = "ipaddr\0\0";
1506 char maskstr[] = "netmask\0\0";
1507 char gwstr[] = "gatewayip\0\0";
1508 int idx;
1509 struct in_addr *addr;
1510 char tmp[46];
1511
Adriano Cordova54674692025-03-03 11:13:15 -03001512 idx = dev_seq(dev);
Adriano Cordova3c951362024-12-04 00:05:21 -03001513
1514 if (idx < 0 || idx > 99) {
1515 log_err("unexpected idx %d\n", idx);
1516 return;
1517 }
1518
1519 if (idx) {
1520 sprintf(ipstr, "ipaddr%d", idx);
1521 sprintf(maskstr, "netmask%d", idx);
1522 sprintf(gwstr, "gatewayip%d", idx);
1523 }
1524
1525 if (ip) {
1526 addr = (struct in_addr *)ip;
1527 ip_to_string(*addr, tmp);
1528 env_set(ipstr, tmp);
1529 }
1530
1531 if (mask) {
1532 addr = (struct in_addr *)mask;
1533 ip_to_string(*addr, tmp);
1534 env_set(maskstr, tmp);
1535 }
1536
1537 if (gw) {
1538 addr = (struct in_addr *)gw;
1539 ip_to_string(*addr, tmp);
1540 env_set(gwstr, tmp);
1541 }
1542#else
1543 if (ip)
1544 memcpy(&net_ip, ip, sizeof(*ip));
1545 if (mask)
1546 memcpy(&net_netmask, mask, sizeof(*mask));
1547#endif
1548}
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001549
Adriano Cordova54674692025-03-03 11:13:15 -03001550#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001551/**
1552 * efi_net_set_buffer() - allocate a buffer of min 64K
1553 *
1554 * @buffer: allocated buffer
1555 * @size: desired buffer size
1556 * Return: status code
1557 */
1558static efi_status_t efi_net_set_buffer(void **buffer, size_t size)
1559{
1560 efi_status_t ret = EFI_SUCCESS;
1561
1562 if (size < SZ_64K)
1563 size = SZ_64K;
1564
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001565 *buffer = efi_alloc(size);
1566 if (!*buffer)
1567 ret = EFI_OUT_OF_RESOURCES;
1568
1569 efi_wget_info.buffer_size = (ulong)size;
1570
1571 return ret;
1572}
1573
1574/**
1575 * efi_net_parse_headers() - parse HTTP headers
1576 *
1577 * Parses the raw buffer efi_wget_info.headers into an array headers
1578 * of efi structs http_headers. The array should be at least
1579 * MAX_HTTP_HEADERS long.
1580 *
1581 * @num_headers: number of headers
1582 * @headers: caller provided array of struct http_headers
1583 */
1584void efi_net_parse_headers(ulong *num_headers, struct http_header *headers)
1585{
1586 if (!num_headers || !headers)
1587 return;
1588
1589 // Populate info with http headers.
1590 *num_headers = 0;
1591 const uchar *line_start = efi_wget_info.headers;
1592 const uchar *line_end;
1593 ulong count;
1594 struct http_header *current_header;
1595 const uchar *separator;
1596 size_t name_length, value_length;
1597
1598 // Skip the first line (request or status line)
1599 line_end = strstr(line_start, "\r\n");
1600
1601 if (line_end)
1602 line_start = line_end + 2;
1603
1604 while ((line_end = strstr(line_start, "\r\n")) != NULL) {
1605 count = *num_headers;
1606 if (line_start == line_end || count >= MAX_HTTP_HEADERS)
1607 break;
1608 current_header = headers + count;
1609 separator = strchr(line_start, ':');
1610 if (separator) {
1611 name_length = separator - line_start;
1612 ++separator;
1613 while (*separator == ' ')
1614 ++separator;
1615 value_length = line_end - separator;
1616 if (name_length < MAX_HTTP_HEADER_NAME &&
1617 value_length < MAX_HTTP_HEADER_VALUE) {
1618 strncpy(current_header->name, line_start, name_length);
1619 current_header->name[name_length] = '\0';
1620 strncpy(current_header->value, separator, value_length);
1621 current_header->value[value_length] = '\0';
1622 (*num_headers)++;
1623 }
1624 }
1625 line_start = line_end + 2;
1626 }
1627}
1628
1629/**
1630 * efi_net_do_request() - issue an HTTP request using wget
1631 *
1632 * @url: url
1633 * @method: HTTP method
1634 * @buffer: data buffer
1635 * @status_code: HTTP status code
1636 * @file_size: file size in bytes
1637 * @headers_buffer: headers buffer
Adriano Cordova28d67772025-03-03 11:13:17 -03001638 * @parent: service binding protocol
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001639 * Return: status code
1640 */
1641efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, void **buffer,
Adriano Cordova28d67772025-03-03 11:13:17 -03001642 u32 *status_code, ulong *file_size, char *headers_buffer,
1643 struct efi_service_binding_protocol *parent)
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001644{
1645 efi_status_t ret = EFI_SUCCESS;
1646 int wget_ret;
1647 static bool last_head;
Adriano Cordova28d67772025-03-03 11:13:17 -03001648 struct udevice *dev;
1649 int i;
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001650
Adriano Cordova28d67772025-03-03 11:13:17 -03001651 if (!buffer || !file_size || !parent)
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001652 return EFI_ABORTED;
1653
1654 efi_wget_info.method = (enum wget_http_method)method;
1655 efi_wget_info.headers = headers_buffer;
1656
Adriano Cordova28d67772025-03-03 11:13:17 -03001657 // Set corresponding udevice
1658 dev = NULL;
1659 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1660 if (net_objs[i] && &net_objs[i]->http_service_binding == parent)
1661 dev = net_objs[i]->dev;
1662 }
1663 if (!dev)
1664 return EFI_ABORTED;
1665
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001666 switch (method) {
1667 case HTTP_METHOD_GET:
1668 ret = efi_net_set_buffer(buffer, last_head ? (size_t)efi_wget_info.hdr_cont_len : 0);
1669 if (ret != EFI_SUCCESS)
1670 goto out;
Adriano Cordova28d67772025-03-03 11:13:17 -03001671 eth_set_dev(dev);
Adriano Cordova54674692025-03-03 11:13:15 -03001672 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001673 wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info);
1674 if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) {
1675 // Try again with updated buffer size
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001676 efi_free_pool(*buffer);
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001677 ret = efi_net_set_buffer(buffer, (size_t)efi_wget_info.hdr_cont_len);
1678 if (ret != EFI_SUCCESS)
1679 goto out;
Adriano Cordova28d67772025-03-03 11:13:17 -03001680 eth_set_dev(dev);
Adriano Cordova54674692025-03-03 11:13:15 -03001681 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001682 if (wget_request((ulong)*buffer, url, &efi_wget_info)) {
1683 efi_free_pool(*buffer);
1684 ret = EFI_DEVICE_ERROR;
1685 goto out;
1686 }
1687 } else if (wget_ret) {
1688 efi_free_pool(*buffer);
1689 ret = EFI_DEVICE_ERROR;
1690 goto out;
1691 }
1692 // Pass the actual number of received bytes to the application
1693 *file_size = efi_wget_info.file_size;
1694 *status_code = efi_wget_info.status_code;
1695 last_head = false;
1696 break;
1697 case HTTP_METHOD_HEAD:
1698 ret = efi_net_set_buffer(buffer, 0);
1699 if (ret != EFI_SUCCESS)
1700 goto out;
Adriano Cordova28d67772025-03-03 11:13:17 -03001701 eth_set_dev(dev);
Adriano Cordova54674692025-03-03 11:13:15 -03001702 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001703 wget_request((ulong)*buffer, url, &efi_wget_info);
1704 *file_size = 0;
1705 *status_code = efi_wget_info.status_code;
1706 last_head = true;
1707 break;
1708 default:
1709 ret = EFI_UNSUPPORTED;
1710 break;
1711 }
1712
1713out:
1714 return ret;
1715}
Adriano Cordova54674692025-03-03 11:13:15 -03001716#endif