blob: b3291b4f1d527aa09d9953b4e32d24a4930a31b0 [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 Cordova28d67772025-03-03 11:13:17 -030027#define MAX_EFI_NET_OBJS 10
Adriano Cordova62e20fb2025-03-03 11:13:16 -030028#define MAX_NUM_DHCP_ENTRIES 10
Adriano Cordova54674692025-03-03 11:13:15 -030029#define MAX_NUM_DP_ENTRIES 10
30
Adriano Cordova7f2bcd42025-03-03 11:13:11 -030031const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020032static const efi_guid_t efi_pxe_base_code_protocol_guid =
33 EFI_PXE_BASE_CODE_PROTOCOL_GUID;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010034
Adriano Cordova54674692025-03-03 11:13:15 -030035struct dp_entry {
36 struct efi_device_path *net_dp;
37 struct udevice *dev;
38 bool is_valid;
39};
40
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020041/*
Adriano Cordova54674692025-03-03 11:13:15 -030042 * The network device path cache. An entry is added when a new bootfile
43 * is downloaded from the network. If the bootfile is then loaded as an
44 * efi image, the most recent entry corresponding to the device is passed
45 * as the device path of the loaded image.
Adriano Cordova93cba0f2024-12-04 00:05:23 -030046 */
Adriano Cordova54674692025-03-03 11:13:15 -030047static struct dp_entry dp_cache[MAX_NUM_DP_ENTRIES];
48static int next_dp_entry;
Adriano Cordova93cba0f2024-12-04 00:05:23 -030049
Adriano Cordova54674692025-03-03 11:13:15 -030050#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
Adriano Cordova0d1f5092024-12-04 00:05:24 -030051static struct wget_http_info efi_wget_info = {
52 .set_bootdev = false,
53 .check_buffer_size = true,
54
55};
Adriano Cordova54674692025-03-03 11:13:15 -030056#endif
Adriano Cordova0d1f5092024-12-04 00:05:24 -030057
Adriano Cordova62e20fb2025-03-03 11:13:16 -030058struct dhcp_entry {
59 struct efi_pxe_packet *dhcp_ack;
60 struct udevice *dev;
61 bool is_valid;
62};
63
64static struct dhcp_entry dhcp_cache[MAX_NUM_DHCP_ENTRIES];
65static int next_dhcp_entry;
66
Heinrich Schuchardt72928722018-09-26 05:27:56 +020067/**
68 * struct efi_net_obj - EFI object representing a network interface
69 *
Adriano Cordova9debc902024-12-04 00:05:25 -030070 * @header: EFI object header
Adriano Cordova54674692025-03-03 11:13:15 -030071 * @dev: net udevice
Adriano Cordova9debc902024-12-04 00:05:25 -030072 * @net: simple network protocol interface
73 * @net_mode: status of the network interface
74 * @pxe: PXE base code protocol interface
75 * @pxe_mode: status of the PXE base code protocol
76 * @ip4_config2: IP4 Config2 protocol interface
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030077 * @http_service_binding: Http service binding protocol interface
Adriano Cordova28d67772025-03-03 11:13:17 -030078 * @new_tx_packet: new transmit packet
79 * @transmit_buffer: transmit buffer
80 * @receive_buffer: array of receive buffers
81 * @receive_lengths: array of lengths for received packets
82 * @rx_packet_idx: index of the current receive packet
83 * @rx_packet_num: number of received packets
84 * @wait_for_packet: signaled when a packet has been received
85 * @network_timer_event: event to check for new network packets.
86 * @efi_seq_num: sequence number of the EFI net object.
Heinrich Schuchardt72928722018-09-26 05:27:56 +020087 */
Alexander Graf94c4b992016-05-06 21:01:01 +020088struct efi_net_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020089 struct efi_object header;
Adriano Cordova54674692025-03-03 11:13:15 -030090 struct udevice *dev;
Alexander Graf94c4b992016-05-06 21:01:01 +020091 struct efi_simple_network net;
92 struct efi_simple_network_mode net_mode;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020093 struct efi_pxe_base_code_protocol pxe;
Alexander Graf94c4b992016-05-06 21:01:01 +020094 struct efi_pxe_mode pxe_mode;
Adriano Cordova9debc902024-12-04 00:05:25 -030095#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
96 struct efi_ip4_config2_protocol ip4_config2;
97#endif
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030098#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
99 struct efi_service_binding_protocol http_service_binding;
100#endif
Adriano Cordova28d67772025-03-03 11:13:17 -0300101 void *new_tx_packet;
102 void *transmit_buffer;
103 uchar **receive_buffer;
104 size_t *receive_lengths;
105 int rx_packet_idx;
106 int rx_packet_num;
107 struct efi_event *wait_for_packet;
108 struct efi_event *network_timer_event;
109 int efi_seq_num;
Alexander Graf94c4b992016-05-06 21:01:01 +0200110};
111
Adriano Cordova28d67772025-03-03 11:13:17 -0300112static int curr_efi_net_obj;
113static struct efi_net_obj *net_objs[MAX_EFI_NET_OBJS];
114
115/**
Adriano Cordova54674692025-03-03 11:13:15 -0300116 * efi_netobj_is_active() - checks if a netobj is active in the efi subsystem
117 *
Adriano Cordova28d67772025-03-03 11:13:17 -0300118 * @netobj: pointer to efi_net_obj
119 * Return: true if active
Adriano Cordova54674692025-03-03 11:13:15 -0300120 */
121static bool efi_netobj_is_active(struct efi_net_obj *netobj)
122{
123 if (!netobj || !efi_search_obj(&netobj->header))
124 return false;
125
126 return true;
127}
128
Adriano Cordova28d67772025-03-03 11:13:17 -0300129/*
130 * efi_netobj_from_snp() - get efi_net_obj from simple network protocol
131 *
132 *
133 * @snp: pointer to the simple network protocol
134 * Return: pointer to efi_net_obj, NULL on error
135 */
136static struct efi_net_obj *efi_netobj_from_snp(struct efi_simple_network *snp)
137{
138 int i;
139
140 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
141 if (net_objs[i] && &net_objs[i]->net == snp) {
142 // Do not register duplicate devices
143 return net_objs[i];
144 }
145 }
146 return NULL;
147}
Adriano Cordova54674692025-03-03 11:13:15 -0300148
149/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100150 * efi_net_start() - start the network interface
151 *
152 * This function implements the Start service of the
153 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
154 * (UEFI) specification for details.
155 *
156 * @this: pointer to the protocol instance
157 * Return: status code
158 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200159static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
160{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100161 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300162 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100163
Alexander Graf94c4b992016-05-06 21:01:01 +0200164 EFI_ENTRY("%p", this);
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100165 /* Check parameters */
166 if (!this) {
167 ret = EFI_INVALID_PARAMETER;
168 goto out;
169 }
170
Adriano Cordova28d67772025-03-03 11:13:17 -0300171 nt = efi_netobj_from_snp(this);
172
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200173 if (this->mode->state != EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100174 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200175 } else {
176 this->int_status = 0;
Adriano Cordova28d67772025-03-03 11:13:17 -0300177 nt->wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100178 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200179 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100180out:
181 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200182}
183
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100184/*
185 * efi_net_stop() - stop the network interface
186 *
187 * This function implements the Stop service of the
188 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
189 * (UEFI) specification for details.
190 *
191 * @this: pointer to the protocol instance
192 * Return: status code
193 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200194static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
195{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100196 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300197 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100198
Alexander Graf94c4b992016-05-06 21:01:01 +0200199 EFI_ENTRY("%p", this);
200
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100201 /* Check parameters */
202 if (!this) {
203 ret = EFI_INVALID_PARAMETER;
204 goto out;
205 }
206
Adriano Cordova28d67772025-03-03 11:13:17 -0300207 nt = efi_netobj_from_snp(this);
208
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200209 if (this->mode->state == EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100210 ret = EFI_NOT_STARTED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200211 } else {
212 /* Disable hardware and put it into the reset state */
Adriano Cordova28d67772025-03-03 11:13:17 -0300213 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300214 env_set("ethact", eth_get_name());
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200215 eth_halt();
Patrick Wildtfab89102020-10-07 11:04:33 +0200216 /* Clear cache of packets */
Adriano Cordova28d67772025-03-03 11:13:17 -0300217 nt->rx_packet_num = 0;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100218 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200219 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100220out:
221 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200222}
223
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200224/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100225 * efi_net_initialize() - initialize the network interface
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200226 *
227 * This function implements the Initialize service of the
228 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
229 * (UEFI) specification for details.
230 *
231 * @this: pointer to the protocol instance
232 * @extra_rx: extra receive buffer to be allocated
233 * @extra_tx: extra transmit buffer to be allocated
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100234 * Return: status code
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200235 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200236static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
237 ulong extra_rx, ulong extra_tx)
238{
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200239 int ret;
240 efi_status_t r = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300241 struct efi_net_obj *nt;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200242
Alexander Graf94c4b992016-05-06 21:01:01 +0200243 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
244
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100245 /* Check parameters */
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200246 if (!this) {
247 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100248 goto out;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200249 }
Adriano Cordova28d67772025-03-03 11:13:17 -0300250 nt = efi_netobj_from_snp(this);
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200251
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200252 switch (this->mode->state) {
253 case EFI_NETWORK_INITIALIZED:
254 case EFI_NETWORK_STARTED:
255 break;
256 default:
257 r = EFI_NOT_STARTED;
258 goto out;
259 }
260
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200261 /* Setup packet buffers */
262 net_init();
Patrick Wildtfab89102020-10-07 11:04:33 +0200263 /* Clear cache of packets */
Adriano Cordova28d67772025-03-03 11:13:17 -0300264 nt->rx_packet_num = 0;
Adriano Cordova54674692025-03-03 11:13:15 -0300265 /* Set the net device corresponding to the efi net object */
Adriano Cordova28d67772025-03-03 11:13:17 -0300266 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300267 env_set("ethact", eth_get_name());
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200268 /* Get hardware ready for send and receive operations */
Adriano Cordova28d67772025-03-03 11:13:17 -0300269 ret = eth_start_udev(nt->dev);
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200270 if (ret < 0) {
271 eth_halt();
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100272 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200273 r = EFI_DEVICE_ERROR;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100274 goto out;
275 } else {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200276 this->int_status = 0;
Adriano Cordova28d67772025-03-03 11:13:17 -0300277 nt->wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100278 this->mode->state = EFI_NETWORK_INITIALIZED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200279 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100280out:
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200281 return EFI_EXIT(r);
Alexander Graf94c4b992016-05-06 21:01:01 +0200282}
283
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100284/*
285 * efi_net_reset() - reinitialize the network interface
286 *
287 * This function implements the Reset service of the
288 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
289 * (UEFI) specification for details.
290 *
291 * @this: pointer to the protocol instance
292 * @extended_verification: execute exhaustive verification
293 * Return: status code
294 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200295static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
296 int extended_verification)
297{
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200298 efi_status_t ret;
299
Alexander Graf94c4b992016-05-06 21:01:01 +0200300 EFI_ENTRY("%p, %x", this, extended_verification);
301
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200302 /* Check parameters */
303 if (!this) {
304 ret = EFI_INVALID_PARAMETER;
305 goto out;
306 }
307
308 switch (this->mode->state) {
309 case EFI_NETWORK_INITIALIZED:
310 break;
311 case EFI_NETWORK_STOPPED:
312 ret = EFI_NOT_STARTED;
313 goto out;
314 default:
315 ret = EFI_DEVICE_ERROR;
316 goto out;
317 }
318
319 this->mode->state = EFI_NETWORK_STARTED;
320 ret = EFI_CALL(efi_net_initialize(this, 0, 0));
321out:
322 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200323}
324
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100325/*
326 * efi_net_shutdown() - shut down the network interface
327 *
328 * This function implements the Shutdown service of the
329 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
330 * (UEFI) specification for details.
331 *
332 * @this: pointer to the protocol instance
333 * Return: status code
334 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200335static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
336{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100337 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300338 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100339
Alexander Graf94c4b992016-05-06 21:01:01 +0200340 EFI_ENTRY("%p", this);
341
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100342 /* Check parameters */
343 if (!this) {
344 ret = EFI_INVALID_PARAMETER;
345 goto out;
346 }
Adriano Cordova28d67772025-03-03 11:13:17 -0300347 nt = efi_netobj_from_snp(this);
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100348
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200349 switch (this->mode->state) {
350 case EFI_NETWORK_INITIALIZED:
351 break;
352 case EFI_NETWORK_STOPPED:
353 ret = EFI_NOT_STARTED;
354 goto out;
355 default:
356 ret = EFI_DEVICE_ERROR;
357 goto out;
358 }
359
Adriano Cordova28d67772025-03-03 11:13:17 -0300360 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300361 env_set("ethact", eth_get_name());
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100362 eth_halt();
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300363
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200364 this->int_status = 0;
Adriano Cordova28d67772025-03-03 11:13:17 -0300365 nt->wait_for_packet->is_signaled = false;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200366 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100367
368out:
369 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200370}
371
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100372/*
373 * efi_net_receive_filters() - mange multicast receive filters
374 *
375 * This function implements the ReceiveFilters service of the
376 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
377 * (UEFI) specification for details.
378 *
379 * @this: pointer to the protocol instance
380 * @enable: bit mask of receive filters to enable
381 * @disable: bit mask of receive filters to disable
382 * @reset_mcast_filter: true resets contents of the filters
383 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
384 * @mcast_filter: list of new filters
385 * Return: status code
386 */
387static efi_status_t EFIAPI efi_net_receive_filters
388 (struct efi_simple_network *this, u32 enable, u32 disable,
389 int reset_mcast_filter, ulong mcast_filter_count,
390 struct efi_mac_address *mcast_filter)
Alexander Graf94c4b992016-05-06 21:01:01 +0200391{
392 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
393 reset_mcast_filter, mcast_filter_count, mcast_filter);
394
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200395 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200396}
397
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100398/*
399 * efi_net_station_address() - set the hardware MAC address
400 *
401 * This function implements the StationAddress service of the
402 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
403 * (UEFI) specification for details.
404 *
405 * @this: pointer to the protocol instance
406 * @reset: if true reset the address to default
407 * @new_mac: new MAC address
408 * Return: status code
409 */
410static efi_status_t EFIAPI efi_net_station_address
411 (struct efi_simple_network *this, int reset,
412 struct efi_mac_address *new_mac)
Alexander Graf94c4b992016-05-06 21:01:01 +0200413{
414 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
415
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200416 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200417}
418
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100419/*
420 * efi_net_statistics() - reset or collect statistics of the network interface
421 *
422 * This function implements the Statistics service of the
423 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
424 * (UEFI) specification for details.
425 *
426 * @this: pointer to the protocol instance
427 * @reset: if true, the statistics are reset
428 * @stat_size: size of the statistics table
429 * @stat_table: table to receive the statistics
430 * Return: status code
431 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200432static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
433 int reset, ulong *stat_size,
434 void *stat_table)
435{
436 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
437
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200438 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200439}
440
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100441/*
442 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
443 *
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200444 * This function implements the MCastIPtoMAC service of the
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100445 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
446 * (UEFI) specification for details.
447 *
448 * @this: pointer to the protocol instance
449 * @ipv6: true if the IP address is an IPv6 address
450 * @ip: IP address
451 * @mac: MAC address
452 * Return: status code
453 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200454static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
455 int ipv6,
456 struct efi_ip_address *ip,
457 struct efi_mac_address *mac)
458{
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200459 efi_status_t ret = EFI_SUCCESS;
460
Alexander Graf94c4b992016-05-06 21:01:01 +0200461 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
462
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200463 if (!this || !ip || !mac) {
464 ret = EFI_INVALID_PARAMETER;
465 goto out;
466 }
467
468 if (ipv6) {
469 ret = EFI_UNSUPPORTED;
470 goto out;
471 }
472
473 /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */
474 if ((ip->ip_addr[0] & 0xf0) != 0xe0) {
475 ret = EFI_INVALID_PARAMETER;
476 goto out;
477 };
478
479 switch (this->mode->state) {
480 case EFI_NETWORK_INITIALIZED:
481 case EFI_NETWORK_STARTED:
482 break;
483 default:
484 ret = EFI_NOT_STARTED;
485 goto out;
486 }
487
488 memset(mac, 0, sizeof(struct efi_mac_address));
489
490 /*
491 * Copy lower 23 bits of IPv4 multi-cast address
492 * RFC 1112, RFC 7042 2.1.1.
493 */
494 mac->mac_addr[0] = 0x01;
495 mac->mac_addr[1] = 0x00;
496 mac->mac_addr[2] = 0x5E;
497 mac->mac_addr[3] = ip->ip_addr[1] & 0x7F;
498 mac->mac_addr[4] = ip->ip_addr[2];
499 mac->mac_addr[5] = ip->ip_addr[3];
500out:
501 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200502}
503
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100504/**
505 * efi_net_nvdata() - read or write NVRAM
506 *
507 * This function implements the GetStatus service of the Simple Network
508 * Protocol. See the UEFI spec for details.
509 *
510 * @this: the instance of the Simple Network Protocol
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200511 * @read_write: true for read, false for write
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100512 * @offset: offset in NVRAM
513 * @buffer_size: size of buffer
514 * @buffer: buffer
515 * Return: status code
516 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200517static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
518 int read_write, ulong offset,
519 ulong buffer_size, char *buffer)
520{
521 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
522 buffer);
523
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200524 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200525}
526
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100527/**
528 * efi_net_get_status() - get interrupt status
529 *
530 * This function implements the GetStatus service of the Simple Network
531 * Protocol. See the UEFI spec for details.
532 *
533 * @this: the instance of the Simple Network Protocol
534 * @int_status: interface status
535 * @txbuf: transmission buffer
536 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200537static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
538 u32 *int_status, void **txbuf)
539{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100540 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300541 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100542
Alexander Graf94c4b992016-05-06 21:01:01 +0200543 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
544
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200545 efi_timer_check();
546
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100547 /* Check parameters */
548 if (!this) {
549 ret = EFI_INVALID_PARAMETER;
550 goto out;
551 }
552
Adriano Cordova28d67772025-03-03 11:13:17 -0300553 nt = efi_netobj_from_snp(this);
554
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100555 switch (this->mode->state) {
556 case EFI_NETWORK_STOPPED:
557 ret = EFI_NOT_STARTED;
558 goto out;
559 case EFI_NETWORK_STARTED:
560 ret = EFI_DEVICE_ERROR;
561 goto out;
562 default:
563 break;
564 }
565
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200566 if (int_status) {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200567 *int_status = this->int_status;
568 this->int_status = 0;
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200569 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200570 if (txbuf)
Adriano Cordova28d67772025-03-03 11:13:17 -0300571 *txbuf = nt->new_tx_packet;
Alexander Graf94c4b992016-05-06 21:01:01 +0200572
Adriano Cordova28d67772025-03-03 11:13:17 -0300573 nt->new_tx_packet = NULL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100574out:
575 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200576}
577
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100578/**
579 * efi_net_transmit() - transmit a packet
580 *
581 * This function implements the Transmit service of the Simple Network Protocol.
582 * See the UEFI spec for details.
583 *
584 * @this: the instance of the Simple Network Protocol
585 * @header_size: size of the media header
586 * @buffer_size: size of the buffer to receive the packet
587 * @buffer: buffer to receive the packet
588 * @src_addr: source hardware MAC address
589 * @dest_addr: destination hardware MAC address
590 * @protocol: type of header to build
591 * Return: status code
592 */
593static efi_status_t EFIAPI efi_net_transmit
594 (struct efi_simple_network *this, size_t header_size,
595 size_t buffer_size, void *buffer,
596 struct efi_mac_address *src_addr,
597 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200598{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100599 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300600 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100601
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200602 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
603 (unsigned long)header_size, (unsigned long)buffer_size,
604 buffer, src_addr, dest_addr, protocol);
Alexander Graf94c4b992016-05-06 21:01:01 +0200605
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200606 efi_timer_check();
607
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100608 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200609 if (!this || !buffer) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100610 ret = EFI_INVALID_PARAMETER;
611 goto out;
612 }
613
Adriano Cordova28d67772025-03-03 11:13:17 -0300614 nt = efi_netobj_from_snp(this);
615
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100616 /* We do not support jumbo packets */
617 if (buffer_size > PKTSIZE_ALIGN) {
618 ret = EFI_INVALID_PARAMETER;
619 goto out;
620 }
621
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200622 /* At least the IP header has to fit into the buffer */
623 if (buffer_size < this->mode->media_header_size) {
624 ret = EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100625 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200626 }
627
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200628 /*
629 * TODO:
630 * Support VLANs. Use net_set_ether() for copying the header. Use a
631 * U_BOOT_ENV_CALLBACK to update the media header size.
632 */
633 if (header_size) {
634 struct ethernet_hdr *header = buffer;
635
636 if (!dest_addr || !protocol ||
637 header_size != this->mode->media_header_size) {
638 ret = EFI_INVALID_PARAMETER;
639 goto out;
640 }
641 if (!src_addr)
642 src_addr = &this->mode->current_address;
643
644 memcpy(header->et_dest, dest_addr, ARP_HLEN);
645 memcpy(header->et_src, src_addr, ARP_HLEN);
646 header->et_protlen = htons(*protocol);
647 }
648
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100649 switch (this->mode->state) {
650 case EFI_NETWORK_STOPPED:
651 ret = EFI_NOT_STARTED;
652 goto out;
653 case EFI_NETWORK_STARTED:
654 ret = EFI_DEVICE_ERROR;
655 goto out;
656 default:
657 break;
658 }
659
Adriano Cordova28d67772025-03-03 11:13:17 -0300660 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300661 env_set("ethact", eth_get_name());
662
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100663 /* Ethernet packets always fit, just bounce */
Adriano Cordova28d67772025-03-03 11:13:17 -0300664 memcpy(nt->transmit_buffer, buffer, buffer_size);
665 net_send_packet(nt->transmit_buffer, buffer_size);
Alexander Graf0b12b862016-09-06 14:26:27 +0200666
Adriano Cordova28d67772025-03-03 11:13:17 -0300667 nt->new_tx_packet = buffer;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200668 this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100669out:
670 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200671}
672
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100673/**
674 * efi_net_receive() - receive a packet from a network interface
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200675 *
676 * This function implements the Receive service of the Simple Network Protocol.
677 * See the UEFI spec for details.
678 *
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100679 * @this: the instance of the Simple Network Protocol
680 * @header_size: size of the media header
681 * @buffer_size: size of the buffer to receive the packet
682 * @buffer: buffer to receive the packet
683 * @src_addr: source MAC address
684 * @dest_addr: destination MAC address
685 * @protocol: protocol
686 * Return: status code
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200687 */
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100688static efi_status_t EFIAPI efi_net_receive
689 (struct efi_simple_network *this, size_t *header_size,
690 size_t *buffer_size, void *buffer,
691 struct efi_mac_address *src_addr,
692 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200693{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100694 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200695 struct ethernet_hdr *eth_hdr;
696 size_t hdr_size = sizeof(struct ethernet_hdr);
697 u16 protlen;
Adriano Cordova28d67772025-03-03 11:13:17 -0300698 struct efi_net_obj *nt;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200699
Alexander Graf94c4b992016-05-06 21:01:01 +0200700 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
701 buffer_size, buffer, src_addr, dest_addr, protocol);
702
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100703 /* Execute events */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200704 efi_timer_check();
Alexander Graf94c4b992016-05-06 21:01:01 +0200705
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100706 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200707 if (!this || !buffer || !buffer_size) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100708 ret = EFI_INVALID_PARAMETER;
709 goto out;
710 }
711
Adriano Cordova28d67772025-03-03 11:13:17 -0300712 nt = efi_netobj_from_snp(this);
713
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100714 switch (this->mode->state) {
715 case EFI_NETWORK_STOPPED:
716 ret = EFI_NOT_STARTED;
717 goto out;
718 case EFI_NETWORK_STARTED:
719 ret = EFI_DEVICE_ERROR;
720 goto out;
721 default:
722 break;
723 }
724
Adriano Cordova28d67772025-03-03 11:13:17 -0300725 if (!nt->rx_packet_num) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100726 ret = EFI_NOT_READY;
727 goto out;
728 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200729 /* Fill export parameters */
Adriano Cordova28d67772025-03-03 11:13:17 -0300730 eth_hdr = (struct ethernet_hdr *)nt->receive_buffer[nt->rx_packet_idx];
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200731 protlen = ntohs(eth_hdr->et_protlen);
732 if (protlen == 0x8100) {
733 hdr_size += 4;
Adriano Cordova28d67772025-03-03 11:13:17 -0300734 protlen = ntohs(*(u16 *)&nt->receive_buffer[nt->rx_packet_idx][hdr_size - 2]);
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200735 }
736 if (header_size)
737 *header_size = hdr_size;
738 if (dest_addr)
739 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
740 if (src_addr)
741 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
742 if (protocol)
743 *protocol = protlen;
Adriano Cordova28d67772025-03-03 11:13:17 -0300744 if (*buffer_size < nt->receive_lengths[nt->rx_packet_idx]) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200745 /* Packet doesn't fit, try again with bigger buffer */
Adriano Cordova28d67772025-03-03 11:13:17 -0300746 *buffer_size = nt->receive_lengths[nt->rx_packet_idx];
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100747 ret = EFI_BUFFER_TOO_SMALL;
748 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200749 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200750 /* Copy packet */
Adriano Cordova28d67772025-03-03 11:13:17 -0300751 memcpy(buffer, nt->receive_buffer[nt->rx_packet_idx],
752 nt->receive_lengths[nt->rx_packet_idx]);
753 *buffer_size = nt->receive_lengths[nt->rx_packet_idx];
754 nt->rx_packet_idx = (nt->rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV;
755 nt->rx_packet_num--;
756 if (nt->rx_packet_num)
757 nt->wait_for_packet->is_signaled = true;
Patrick Wildtfab89102020-10-07 11:04:33 +0200758 else
759 this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100760out:
761 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200762}
763
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100764/**
765 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
766 *
767 * This function is called by dhcp_handler().
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200768 *
769 * @pkt: packet received by dhcp_handler()
770 * @len: length of the packet received
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100771 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200772void efi_net_set_dhcp_ack(void *pkt, int len)
773{
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300774 struct efi_pxe_packet **dhcp_ack;
775 struct udevice *dev;
Adriano Cordova28d67772025-03-03 11:13:17 -0300776 int i;
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300777
778 dhcp_ack = &dhcp_cache[next_dhcp_entry].dhcp_ack;
Alexander Graf94c4b992016-05-06 21:01:01 +0200779
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300780 /* For now this function gets called only by the current device */
781 dev = eth_get_dev();
782
783 int maxsize = sizeof(**dhcp_ack);
784
785 if (!*dhcp_ack) {
786 *dhcp_ack = malloc(maxsize);
787 if (!*dhcp_ack)
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100788 return;
789 }
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300790 memset(*dhcp_ack, 0, maxsize);
791 memcpy(*dhcp_ack, pkt, min(len, maxsize));
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100792
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300793 dhcp_cache[next_dhcp_entry].is_valid = true;
794 dhcp_cache[next_dhcp_entry].dev = dev;
795 next_dhcp_entry++;
796 next_dhcp_entry %= MAX_NUM_DHCP_ENTRIES;
Adriano Cordova28d67772025-03-03 11:13:17 -0300797
798 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
799 if (net_objs[i] && net_objs[i]->dev == dev) {
800 net_objs[i]->pxe_mode.dhcp_ack = **dhcp_ack;
801 }
802 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200803}
804
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100805/**
806 * efi_net_push() - callback for received network packet
807 *
808 * This function is called when a network packet is received by eth_rx().
809 *
810 * @pkt: network packet
811 * @len: length
812 */
813static void efi_net_push(void *pkt, int len)
814{
Patrick Wildtfab89102020-10-07 11:04:33 +0200815 int rx_packet_next;
Adriano Cordova28d67772025-03-03 11:13:17 -0300816 struct efi_net_obj *nt;
817
818 nt = net_objs[curr_efi_net_obj];
819 if (!nt)
820 return;
Patrick Wildtfab89102020-10-07 11:04:33 +0200821
822 /* Check that we at least received an Ethernet header */
823 if (len < sizeof(struct ethernet_hdr))
824 return;
825
826 /* Check that the buffer won't overflow */
827 if (len > PKTSIZE_ALIGN)
828 return;
829
830 /* Can't store more than pre-alloced buffer */
Adriano Cordova28d67772025-03-03 11:13:17 -0300831 if (nt->rx_packet_num >= ETH_PACKETS_BATCH_RECV)
Patrick Wildtfab89102020-10-07 11:04:33 +0200832 return;
833
Adriano Cordova28d67772025-03-03 11:13:17 -0300834 rx_packet_next = (nt->rx_packet_idx + nt->rx_packet_num) %
Patrick Wildtfab89102020-10-07 11:04:33 +0200835 ETH_PACKETS_BATCH_RECV;
Adriano Cordova28d67772025-03-03 11:13:17 -0300836 memcpy(nt->receive_buffer[rx_packet_next], pkt, len);
837 nt->receive_lengths[rx_packet_next] = len;
Patrick Wildtfab89102020-10-07 11:04:33 +0200838
Adriano Cordova28d67772025-03-03 11:13:17 -0300839 nt->rx_packet_num++;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100840}
841
842/**
843 * efi_network_timer_notify() - check if a new network packet has been received
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200844 *
845 * This notification function is called in every timer cycle.
846 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200847 * @event: the event for which this notification function is registered
848 * @context: event context - not used in this function
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200849 */
850static void EFIAPI efi_network_timer_notify(struct efi_event *event,
851 void *context)
852{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100853 struct efi_simple_network *this = (struct efi_simple_network *)context;
Adriano Cordova28d67772025-03-03 11:13:17 -0300854 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100855
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200856 EFI_ENTRY("%p, %p", event, context);
857
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100858 /*
859 * Some network drivers do not support calling eth_rx() before
860 * initialization.
861 */
862 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
863 goto out;
864
Adriano Cordova28d67772025-03-03 11:13:17 -0300865 nt = efi_netobj_from_snp(this);
866 curr_efi_net_obj = nt->efi_seq_num;
867
868 if (!nt->rx_packet_num) {
869 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300870 env_set("ethact", eth_get_name());
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200871 push_packet = efi_net_push;
872 eth_rx();
873 push_packet = NULL;
Adriano Cordova28d67772025-03-03 11:13:17 -0300874 if (nt->rx_packet_num) {
Patrick Wildtfab89102020-10-07 11:04:33 +0200875 this->int_status |=
876 EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Adriano Cordova28d67772025-03-03 11:13:17 -0300877 nt->wait_for_packet->is_signaled = true;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200878 }
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200879 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100880out:
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200881 EFI_EXIT(EFI_SUCCESS);
882}
883
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200884static efi_status_t EFIAPI efi_pxe_base_code_start(
885 struct efi_pxe_base_code_protocol *this,
886 u8 use_ipv6)
887{
888 return EFI_UNSUPPORTED;
889}
890
891static efi_status_t EFIAPI efi_pxe_base_code_stop(
892 struct efi_pxe_base_code_protocol *this)
893{
894 return EFI_UNSUPPORTED;
895}
896
897static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
898 struct efi_pxe_base_code_protocol *this,
899 u8 sort_offers)
900{
901 return EFI_UNSUPPORTED;
902}
903
904static efi_status_t EFIAPI efi_pxe_base_code_discover(
905 struct efi_pxe_base_code_protocol *this,
906 u16 type, u16 *layer, u8 bis,
907 struct efi_pxe_base_code_discover_info *info)
908{
909 return EFI_UNSUPPORTED;
910}
911
912static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
913 struct efi_pxe_base_code_protocol *this,
914 u32 operation, void *buffer_ptr,
915 u8 overwrite, efi_uintn_t *buffer_size,
916 struct efi_ip_address server_ip, char *filename,
917 struct efi_pxe_base_code_mtftp_info *info,
918 u8 dont_use_buffer)
919{
920 return EFI_UNSUPPORTED;
921}
922
923static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
924 struct efi_pxe_base_code_protocol *this,
925 u16 op_flags, struct efi_ip_address *dest_ip,
926 u16 *dest_port,
927 struct efi_ip_address *gateway_ip,
928 struct efi_ip_address *src_ip, u16 *src_port,
929 efi_uintn_t *header_size, void *header_ptr,
930 efi_uintn_t *buffer_size, void *buffer_ptr)
931{
932 return EFI_UNSUPPORTED;
933}
934
935static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
936 struct efi_pxe_base_code_protocol *this,
937 u16 op_flags, struct efi_ip_address *dest_ip,
938 u16 *dest_port, struct efi_ip_address *src_ip,
939 u16 *src_port, efi_uintn_t *header_size,
940 void *header_ptr, efi_uintn_t *buffer_size,
941 void *buffer_ptr)
942{
943 return EFI_UNSUPPORTED;
944}
945
946static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
947 struct efi_pxe_base_code_protocol *this,
948 struct efi_pxe_base_code_filter *new_filter)
949{
950 return EFI_UNSUPPORTED;
951}
952
953static efi_status_t EFIAPI efi_pxe_base_code_arp(
954 struct efi_pxe_base_code_protocol *this,
955 struct efi_ip_address *ip_addr,
956 struct efi_mac_address *mac_addr)
957{
958 return EFI_UNSUPPORTED;
959}
960
961static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
962 struct efi_pxe_base_code_protocol *this,
963 u8 *new_auto_arp, u8 *new_send_guid,
964 u8 *new_ttl, u8 *new_tos,
965 u8 *new_make_callback)
966{
967 return EFI_UNSUPPORTED;
968}
969
970static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
971 struct efi_pxe_base_code_protocol *this,
972 struct efi_ip_address *new_station_ip,
973 struct efi_ip_address *new_subnet_mask)
974{
975 return EFI_UNSUPPORTED;
976}
977
978static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
979 struct efi_pxe_base_code_protocol *this,
980 u8 *new_dhcp_discover_valid,
981 u8 *new_dhcp_ack_received,
982 u8 *new_proxy_offer_received,
983 u8 *new_pxe_discover_valid,
984 u8 *new_pxe_reply_received,
985 u8 *new_pxe_bis_reply_received,
986 EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
987 EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
988 EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
989 EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
990 EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
991 EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
992{
993 return EFI_UNSUPPORTED;
994}
995
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100996/**
Adriano Cordova54674692025-03-03 11:13:15 -0300997 * efi_netobj_set_dp() - set device path of a netobj
998 *
999 * @netobj: pointer to efi_net_obj
1000 * @dp: device path to set, allocated by caller
1001 * Return: status code
1002 */
1003efi_status_t efi_netobj_set_dp(struct efi_net_obj *netobj, struct efi_device_path *dp)
1004{
1005 efi_status_t ret;
1006 struct efi_handler *phandler;
1007 struct efi_device_path *new_net_dp;
1008
1009 if (!efi_netobj_is_active(netobj))
1010 return EFI_SUCCESS;
1011
1012 // Create a device path for the netobj
1013 new_net_dp = dp;
1014 if (!new_net_dp)
1015 return EFI_OUT_OF_RESOURCES;
1016
1017 phandler = NULL;
1018 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
1019
1020 // If the device path protocol is not yet installed, install it
1021 if (!phandler)
1022 goto add;
1023
1024 // If it is already installed, try to update it
1025 ret = efi_reinstall_protocol_interface(&netobj->header, &efi_guid_device_path,
1026 phandler->protocol_interface, new_net_dp);
1027 if (ret != EFI_SUCCESS)
1028 return ret;
1029
1030 return EFI_SUCCESS;
1031add:
1032 ret = efi_add_protocol(&netobj->header, &efi_guid_device_path,
1033 new_net_dp);
1034 if (ret != EFI_SUCCESS)
1035 return ret;
1036
1037 return EFI_SUCCESS;
1038}
1039
1040/**
1041 * efi_netobj_get_dp() - get device path of a netobj
1042 *
1043 * @netobj: pointer to efi_net_obj
1044 * Return: device path, NULL on error
1045 */
1046static struct efi_device_path *efi_netobj_get_dp(struct efi_net_obj *netobj)
1047{
1048 struct efi_handler *phandler;
1049
1050 if (!efi_netobj_is_active(netobj))
1051 return NULL;
1052
1053 phandler = NULL;
1054 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
1055
1056 if (phandler && phandler->protocol_interface)
1057 return efi_dp_dup(phandler->protocol_interface);
1058
1059 return NULL;
1060}
1061
1062/**
Adriano Cordova1d70b682025-03-03 11:13:13 -03001063 * efi_net_do_start() - start the efi network stack
1064 *
1065 * This gets called from do_bootefi_exec() each time a payload gets executed.
1066 *
Adriano Cordova54674692025-03-03 11:13:15 -03001067 * @dev: net udevice
Adriano Cordova1d70b682025-03-03 11:13:13 -03001068 * Return: status code
1069 */
Adriano Cordova54674692025-03-03 11:13:15 -03001070efi_status_t efi_net_do_start(struct udevice *dev)
Adriano Cordova1d70b682025-03-03 11:13:13 -03001071{
1072 efi_status_t r = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -03001073 struct efi_net_obj *netobj;
Adriano Cordova54674692025-03-03 11:13:15 -03001074 struct efi_device_path *net_dp;
Adriano Cordova28d67772025-03-03 11:13:17 -03001075 int i;
Adriano Cordova54674692025-03-03 11:13:15 -03001076
Adriano Cordova28d67772025-03-03 11:13:17 -03001077 netobj = NULL;
1078 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1079 if (net_objs[i] && net_objs[i]->dev == dev) {
1080 netobj = net_objs[i];
1081 break;
1082 }
1083 }
1084
1085 if (!efi_netobj_is_active(netobj))
Adriano Cordova54674692025-03-03 11:13:15 -03001086 return r;
1087
1088 efi_net_dp_from_dev(&net_dp, netobj->dev, true);
1089 // If no dp cache entry applies and there already
1090 // is a device path installed, continue
1091 if (!net_dp) {
1092 if (efi_netobj_get_dp(netobj))
1093 goto set_addr;
1094 else
1095 net_dp = efi_dp_from_eth(netobj->dev);
Adriano Cordova1d70b682025-03-03 11:13:13 -03001096
Adriano Cordova54674692025-03-03 11:13:15 -03001097 }
1098
1099 if (!net_dp)
1100 return EFI_OUT_OF_RESOURCES;
1101
1102 r = efi_netobj_set_dp(netobj, net_dp);
1103 if (r != EFI_SUCCESS)
1104 return r;
1105set_addr:
Adriano Cordova1d70b682025-03-03 11:13:13 -03001106#ifdef CONFIG_EFI_HTTP_PROTOCOL
1107 /*
1108 * No harm on doing the following. If the PXE handle is present, the client could
1109 * find it and try to get its IP address from it. In here the PXE handle is present
1110 * but the PXE protocol is not yet implmenented, so we add this in the meantime.
1111 */
1112 efi_net_get_addr((struct efi_ipv4_address *)&netobj->pxe_mode.station_ip,
Adriano Cordova54674692025-03-03 11:13:15 -03001113 (struct efi_ipv4_address *)&netobj->pxe_mode.subnet_mask, NULL, dev);
Adriano Cordova1d70b682025-03-03 11:13:13 -03001114#endif
1115
1116 return r;
1117}
1118
1119/**
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001120 * efi_net_register() - register the simple network protocol
1121 *
1122 * This gets called from do_bootefi_exec().
Adriano Cordova54674692025-03-03 11:13:15 -03001123 * @dev: net udevice
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001124 */
Adriano Cordova54674692025-03-03 11:13:15 -03001125efi_status_t efi_net_register(struct udevice *dev)
Alexander Graf94c4b992016-05-06 21:01:01 +02001126{
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001127 efi_status_t r;
Adriano Cordova28d67772025-03-03 11:13:17 -03001128 int seq_num;
1129 struct efi_net_obj *netobj;
1130 void *transmit_buffer = NULL;
1131 uchar **receive_buffer = NULL;
1132 size_t *receive_lengths;
Adriano Cordova62e20fb2025-03-03 11:13:16 -03001133 int i, j;
Alexander Graf94c4b992016-05-06 21:01:01 +02001134
Adriano Cordova54674692025-03-03 11:13:15 -03001135 if (!dev) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001136 /* No network device active, don't expose any */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001137 return EFI_SUCCESS;
Alexander Graf94c4b992016-05-06 21:01:01 +02001138 }
Adriano Cordova28d67772025-03-03 11:13:17 -03001139
1140 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1141 if (net_objs[i] && net_objs[i]->dev == dev) {
1142 // Do not register duplicate devices
1143 return EFI_SUCCESS;
1144 }
1145 }
1146
1147 seq_num = -1;
1148 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1149 if (!net_objs[i]) {
1150 seq_num = i;
1151 break;
1152 }
1153 }
1154 if (seq_num < 0)
1155 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001156
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001157 /* We only expose the "active" network device, so one is enough */
Alexander Graf94c4b992016-05-06 21:01:01 +02001158 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001159 if (!netobj)
1160 goto out_of_resources;
1161
Adriano Cordova54674692025-03-03 11:13:15 -03001162 netobj->dev = dev;
1163
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001164 /* Allocate an aligned transmit buffer */
1165 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
1166 if (!transmit_buffer)
1167 goto out_of_resources;
1168 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Adriano Cordova28d67772025-03-03 11:13:17 -03001169 netobj->transmit_buffer = transmit_buffer;
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001170
Patrick Wildtfab89102020-10-07 11:04:33 +02001171 /* Allocate a number of receive buffers */
1172 receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
1173 sizeof(*receive_buffer));
1174 if (!receive_buffer)
1175 goto out_of_resources;
1176 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
1177 receive_buffer[i] = malloc(PKTSIZE_ALIGN);
1178 if (!receive_buffer[i])
1179 goto out_of_resources;
1180 }
Adriano Cordova28d67772025-03-03 11:13:17 -03001181 netobj->receive_buffer = receive_buffer;
Adriano Cordova54674692025-03-03 11:13:15 -03001182
Patrick Wildtfab89102020-10-07 11:04:33 +02001183 receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
1184 sizeof(*receive_lengths));
1185 if (!receive_lengths)
1186 goto out_of_resources;
Adriano Cordova28d67772025-03-03 11:13:17 -03001187 netobj->receive_lengths = receive_lengths;
Patrick Wildtfab89102020-10-07 11:04:33 +02001188
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001189 /* Hook net up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001190 efi_add_handle(&netobj->header);
Alexander Graf94c4b992016-05-06 21:01:01 +02001191
1192 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001193 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001194 &netobj->net);
1195 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001196 goto failure_to_add_protocol;
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001197
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +02001198 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001199 &netobj->pxe);
1200 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001201 goto failure_to_add_protocol;
Heinrich Schuchardtd54396a2017-10-05 16:35:57 +02001202 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf94c4b992016-05-06 21:01:01 +02001203 netobj->net.start = efi_net_start;
1204 netobj->net.stop = efi_net_stop;
1205 netobj->net.initialize = efi_net_initialize;
1206 netobj->net.reset = efi_net_reset;
1207 netobj->net.shutdown = efi_net_shutdown;
1208 netobj->net.receive_filters = efi_net_receive_filters;
1209 netobj->net.station_address = efi_net_station_address;
1210 netobj->net.statistics = efi_net_statistics;
1211 netobj->net.mcastiptomac = efi_net_mcastiptomac;
1212 netobj->net.nvdata = efi_net_nvdata;
1213 netobj->net.get_status = efi_net_get_status;
1214 netobj->net.transmit = efi_net_transmit;
1215 netobj->net.receive = efi_net_receive;
1216 netobj->net.mode = &netobj->net_mode;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +02001217 netobj->net_mode.state = EFI_NETWORK_STOPPED;
Adriano Cordova54674692025-03-03 11:13:15 -03001218 if (dev_get_plat(dev))
1219 memcpy(netobj->net_mode.current_address.mac_addr,
1220 ((struct eth_pdata *)dev_get_plat(dev))->enetaddr, 6);
Heinrich Schuchardtd1cc6cb2017-10-05 16:35:58 +02001221 netobj->net_mode.hwaddr_size = ARP_HLEN;
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +02001222 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
Alexander Graf94c4b992016-05-06 21:01:01 +02001223 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomas27df0a72018-06-21 16:21:01 -07001224 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf94c4b992016-05-06 21:01:01 +02001225
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +02001226 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
1227 netobj->pxe.start = efi_pxe_base_code_start;
1228 netobj->pxe.stop = efi_pxe_base_code_stop;
1229 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
1230 netobj->pxe.discover = efi_pxe_base_code_discover;
1231 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
1232 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
1233 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
1234 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
1235 netobj->pxe.arp = efi_pxe_base_code_arp;
1236 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
1237 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
1238 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf94c4b992016-05-06 21:01:01 +02001239 netobj->pxe.mode = &netobj->pxe_mode;
Adriano Cordova62e20fb2025-03-03 11:13:16 -03001240
1241 /*
1242 * Scan dhcp entries for one corresponding
1243 * to this udevice, from newest to oldest
1244 */
1245 i = (next_dhcp_entry + MAX_NUM_DHCP_ENTRIES - 1) % MAX_NUM_DHCP_ENTRIES;
1246 for (j = 0; dhcp_cache[i].is_valid && j < MAX_NUM_DHCP_ENTRIES;
1247 i = (i + MAX_NUM_DHCP_ENTRIES - 1) % MAX_NUM_DHCP_ENTRIES, j++) {
1248 if (dev == dhcp_cache[i].dev) {
1249 netobj->pxe_mode.dhcp_ack = *dhcp_cache[i].dhcp_ack;
1250 break;
1251 }
1252 }
Alexander Graf94c4b992016-05-06 21:01:01 +02001253
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001254 /*
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001255 * Create WaitForPacket event.
1256 */
1257 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001258 efi_network_timer_notify, NULL, NULL,
Adriano Cordova28d67772025-03-03 11:13:17 -03001259 &netobj->wait_for_packet);
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001260 if (r != EFI_SUCCESS) {
1261 printf("ERROR: Failed to register network event\n");
1262 return r;
1263 }
Adriano Cordova28d67772025-03-03 11:13:17 -03001264 netobj->net.wait_for_packet = netobj->wait_for_packet;
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001265 /*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001266 * Create a timer event.
1267 *
1268 * The notification function is used to check if a new network packet
1269 * has been received.
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001270 *
1271 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001272 */
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001273 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001274 efi_network_timer_notify, &netobj->net, NULL,
Adriano Cordova28d67772025-03-03 11:13:17 -03001275 &netobj->network_timer_event);
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001276 if (r != EFI_SUCCESS) {
1277 printf("ERROR: Failed to register network event\n");
1278 return r;
1279 }
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001280 /* Network is time critical, create event in every timer cycle */
Adriano Cordova28d67772025-03-03 11:13:17 -03001281 r = efi_set_timer(netobj->network_timer_event, EFI_TIMER_PERIODIC, 0);
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001282 if (r != EFI_SUCCESS) {
1283 printf("ERROR: Failed to set network timer\n");
1284 return r;
1285 }
1286
Adriano Cordova9debc902024-12-04 00:05:25 -03001287#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
1288 r = efi_ipconfig_register(&netobj->header, &netobj->ip4_config2);
1289 if (r != EFI_SUCCESS)
1290 goto failure_to_add_protocol;
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001291#endif
1292
1293#ifdef CONFIG_EFI_HTTP_PROTOCOL
1294 r = efi_http_register(&netobj->header, &netobj->http_service_binding);
1295 if (r != EFI_SUCCESS)
1296 goto failure_to_add_protocol;
Adriano Cordova9debc902024-12-04 00:05:25 -03001297#endif
Adriano Cordova28d67772025-03-03 11:13:17 -03001298 netobj->efi_seq_num = seq_num;
1299 net_objs[seq_num] = netobj;
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001300 return EFI_SUCCESS;
1301failure_to_add_protocol:
1302 printf("ERROR: Failure to add protocol\n");
1303 return r;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001304out_of_resources:
1305 free(netobj);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +01001306 netobj = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +02001307 free(transmit_buffer);
1308 if (receive_buffer)
1309 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
1310 free(receive_buffer[i]);
1311 free(receive_buffer);
1312 free(receive_lengths);
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001313 printf("ERROR: Out of memory\n");
1314 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001315}
Adriano Cordova3c951362024-12-04 00:05:21 -03001316
1317/**
Adriano Cordova54674692025-03-03 11:13:15 -03001318 * efi_net_new_dp() - update device path associated to a net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001319 *
1320 * This gets called to update the device path when a new boot
1321 * file is downloaded
1322 *
1323 * @dev: dev to set the device path from
1324 * @server: remote server address
Adriano Cordova54674692025-03-03 11:13:15 -03001325 * @udev: net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001326 * Return: status code
1327 */
Adriano Cordova54674692025-03-03 11:13:15 -03001328efi_status_t efi_net_new_dp(const char *dev, const char *server, struct udevice *udev)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001329{
Adriano Cordova54674692025-03-03 11:13:15 -03001330 efi_status_t ret;
Adriano Cordova28d67772025-03-03 11:13:17 -03001331 struct efi_net_obj *netobj;
Adriano Cordova54674692025-03-03 11:13:15 -03001332 struct efi_device_path *old_net_dp, *new_net_dp;
1333 struct efi_device_path **dp;
Adriano Cordova28d67772025-03-03 11:13:17 -03001334 int i;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001335
Adriano Cordova54674692025-03-03 11:13:15 -03001336 dp = &dp_cache[next_dp_entry].net_dp;
1337
1338 dp_cache[next_dp_entry].dev = udev;
1339 dp_cache[next_dp_entry].is_valid = true;
1340 next_dp_entry++;
1341 next_dp_entry %= MAX_NUM_DP_ENTRIES;
1342
1343 old_net_dp = *dp;
1344 new_net_dp = NULL;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001345 if (!strcmp(dev, "Net"))
Adriano Cordova54674692025-03-03 11:13:15 -03001346 new_net_dp = efi_dp_from_eth(udev);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001347 else if (!strcmp(dev, "Http"))
Adriano Cordova54674692025-03-03 11:13:15 -03001348 new_net_dp = efi_dp_from_http(server, udev);
1349 if (!new_net_dp)
1350 return EFI_OUT_OF_RESOURCES;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001351
Adriano Cordova54674692025-03-03 11:13:15 -03001352 *dp = new_net_dp;
1353 // Free the old cache entry
1354 efi_free_pool(old_net_dp);
1355
Adriano Cordova28d67772025-03-03 11:13:17 -03001356 netobj = NULL;
1357 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1358 if (net_objs[i] && net_objs[i]->dev == udev) {
1359 netobj = net_objs[i];
1360 break;
1361 }
1362 }
1363 if (!netobj)
Adriano Cordova54674692025-03-03 11:13:15 -03001364 return EFI_SUCCESS;
1365
1366 new_net_dp = efi_dp_dup(*dp);
1367 if (!new_net_dp)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001368 return EFI_OUT_OF_RESOURCES;
Adriano Cordova54674692025-03-03 11:13:15 -03001369 ret = efi_netobj_set_dp(netobj, new_net_dp);
1370 if (ret != EFI_SUCCESS)
1371 efi_free_pool(new_net_dp);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001372
Adriano Cordova54674692025-03-03 11:13:15 -03001373 return ret;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001374}
1375
1376/**
Adriano Cordova54674692025-03-03 11:13:15 -03001377 * efi_net_dp_from_dev() - get device path associated to a net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001378 *
1379 * Produce a copy of the current device path
1380 *
Adriano Cordova54674692025-03-03 11:13:15 -03001381 * @dp: copy of the current device path
1382 * @udev: net udevice
1383 * @cache_only: get device path from cache only
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001384 */
Adriano Cordova54674692025-03-03 11:13:15 -03001385void efi_net_dp_from_dev(struct efi_device_path **dp, struct udevice *udev, bool cache_only)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001386{
Adriano Cordova54674692025-03-03 11:13:15 -03001387 int i, j;
1388
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001389 if (!dp)
1390 return;
Adriano Cordova54674692025-03-03 11:13:15 -03001391
1392 *dp = NULL;
1393
1394 if (cache_only)
1395 goto cache;
1396
Adriano Cordova28d67772025-03-03 11:13:17 -03001397 // If a netobj matches:
1398 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1399 if (net_objs[i] && net_objs[i]->dev == udev) {
1400 *dp = efi_netobj_get_dp(net_objs[i]);
1401 if (*dp)
1402 return;
1403 }
Adriano Cordova54674692025-03-03 11:13:15 -03001404 }
1405cache:
1406 // Search in the cache
1407 i = (next_dp_entry + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES;
1408 for (j = 0; dp_cache[i].is_valid && j < MAX_NUM_DP_ENTRIES;
1409 i = (i + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES, j++) {
1410 if (dp_cache[i].dev == udev) {
1411 *dp = efi_dp_dup(dp_cache[i].net_dp);
1412 return;
1413 }
1414 }
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001415}
1416
1417/**
Adriano Cordova3c951362024-12-04 00:05:21 -03001418 * efi_net_get_addr() - get IP address information
1419 *
1420 * Copy the current IP address, mask, and gateway into the
1421 * efi_ipv4_address structs pointed to by ip, mask and gw,
1422 * respectively.
1423 *
1424 * @ip: pointer to an efi_ipv4_address struct to
1425 * be filled with the current IP address
1426 * @mask: pointer to an efi_ipv4_address struct to
1427 * be filled with the current network mask
1428 * @gw: pointer to an efi_ipv4_address struct to be
1429 * filled with the current network gateway
Adriano Cordova54674692025-03-03 11:13:15 -03001430 * @dev: udevice
Adriano Cordova3c951362024-12-04 00:05:21 -03001431 */
1432void efi_net_get_addr(struct efi_ipv4_address *ip,
1433 struct efi_ipv4_address *mask,
Adriano Cordova54674692025-03-03 11:13:15 -03001434 struct efi_ipv4_address *gw,
1435 struct udevice *dev)
Adriano Cordova3c951362024-12-04 00:05:21 -03001436{
Adriano Cordova54674692025-03-03 11:13:15 -03001437 if (!dev)
1438 dev = eth_get_dev();
Adriano Cordova3c951362024-12-04 00:05:21 -03001439#ifdef CONFIG_NET_LWIP
1440 char ipstr[] = "ipaddr\0\0";
1441 char maskstr[] = "netmask\0\0";
1442 char gwstr[] = "gatewayip\0\0";
1443 int idx;
1444 struct in_addr tmp;
1445 char *env;
1446
Adriano Cordova54674692025-03-03 11:13:15 -03001447 idx = dev_seq(dev);
Adriano Cordova3c951362024-12-04 00:05:21 -03001448
1449 if (idx < 0 || idx > 99) {
1450 log_err("unexpected idx %d\n", idx);
1451 return;
1452 }
1453
1454 if (idx) {
1455 sprintf(ipstr, "ipaddr%d", idx);
1456 sprintf(maskstr, "netmask%d", idx);
1457 sprintf(gwstr, "gatewayip%d", idx);
1458 }
1459
1460 env = env_get(ipstr);
1461 if (env && ip) {
1462 tmp = string_to_ip(env);
1463 memcpy(ip, &tmp, sizeof(tmp));
1464 }
1465
1466 env = env_get(maskstr);
1467 if (env && mask) {
1468 tmp = string_to_ip(env);
1469 memcpy(mask, &tmp, sizeof(tmp));
1470 }
1471 env = env_get(gwstr);
1472 if (env && gw) {
1473 tmp = string_to_ip(env);
1474 memcpy(gw, &tmp, sizeof(tmp));
1475 }
1476#else
1477 if (ip)
1478 memcpy(ip, &net_ip, sizeof(net_ip));
1479 if (mask)
1480 memcpy(mask, &net_netmask, sizeof(net_netmask));
1481#endif
1482}
1483
1484/**
1485 * efi_net_set_addr() - set IP address information
1486 *
1487 * Set the current IP address, mask, and gateway to the
1488 * efi_ipv4_address structs pointed to by ip, mask and gw,
1489 * respectively.
1490 *
1491 * @ip: pointer to new IP address
1492 * @mask: pointer to new network mask to set
1493 * @gw: pointer to new network gateway
Adriano Cordova54674692025-03-03 11:13:15 -03001494 * @dev: udevice
Adriano Cordova3c951362024-12-04 00:05:21 -03001495 */
1496void efi_net_set_addr(struct efi_ipv4_address *ip,
1497 struct efi_ipv4_address *mask,
Adriano Cordova54674692025-03-03 11:13:15 -03001498 struct efi_ipv4_address *gw,
1499 struct udevice *dev)
Adriano Cordova3c951362024-12-04 00:05:21 -03001500{
Adriano Cordova54674692025-03-03 11:13:15 -03001501 if (!dev)
1502 dev = eth_get_dev();
Adriano Cordova3c951362024-12-04 00:05:21 -03001503#ifdef CONFIG_NET_LWIP
1504 char ipstr[] = "ipaddr\0\0";
1505 char maskstr[] = "netmask\0\0";
1506 char gwstr[] = "gatewayip\0\0";
1507 int idx;
1508 struct in_addr *addr;
1509 char tmp[46];
1510
Adriano Cordova54674692025-03-03 11:13:15 -03001511 idx = dev_seq(dev);
Adriano Cordova3c951362024-12-04 00:05:21 -03001512
1513 if (idx < 0 || idx > 99) {
1514 log_err("unexpected idx %d\n", idx);
1515 return;
1516 }
1517
1518 if (idx) {
1519 sprintf(ipstr, "ipaddr%d", idx);
1520 sprintf(maskstr, "netmask%d", idx);
1521 sprintf(gwstr, "gatewayip%d", idx);
1522 }
1523
1524 if (ip) {
1525 addr = (struct in_addr *)ip;
1526 ip_to_string(*addr, tmp);
1527 env_set(ipstr, tmp);
1528 }
1529
1530 if (mask) {
1531 addr = (struct in_addr *)mask;
1532 ip_to_string(*addr, tmp);
1533 env_set(maskstr, tmp);
1534 }
1535
1536 if (gw) {
1537 addr = (struct in_addr *)gw;
1538 ip_to_string(*addr, tmp);
1539 env_set(gwstr, tmp);
1540 }
1541#else
1542 if (ip)
1543 memcpy(&net_ip, ip, sizeof(*ip));
1544 if (mask)
1545 memcpy(&net_netmask, mask, sizeof(*mask));
1546#endif
1547}
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001548
Adriano Cordova54674692025-03-03 11:13:15 -03001549#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001550/**
1551 * efi_net_set_buffer() - allocate a buffer of min 64K
1552 *
1553 * @buffer: allocated buffer
1554 * @size: desired buffer size
1555 * Return: status code
1556 */
1557static efi_status_t efi_net_set_buffer(void **buffer, size_t size)
1558{
1559 efi_status_t ret = EFI_SUCCESS;
1560
1561 if (size < SZ_64K)
1562 size = SZ_64K;
1563
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001564 *buffer = efi_alloc(size);
1565 if (!*buffer)
1566 ret = EFI_OUT_OF_RESOURCES;
1567
1568 efi_wget_info.buffer_size = (ulong)size;
1569
1570 return ret;
1571}
1572
1573/**
1574 * efi_net_parse_headers() - parse HTTP headers
1575 *
1576 * Parses the raw buffer efi_wget_info.headers into an array headers
1577 * of efi structs http_headers. The array should be at least
1578 * MAX_HTTP_HEADERS long.
1579 *
1580 * @num_headers: number of headers
1581 * @headers: caller provided array of struct http_headers
1582 */
1583void efi_net_parse_headers(ulong *num_headers, struct http_header *headers)
1584{
1585 if (!num_headers || !headers)
1586 return;
1587
1588 // Populate info with http headers.
1589 *num_headers = 0;
1590 const uchar *line_start = efi_wget_info.headers;
1591 const uchar *line_end;
1592 ulong count;
1593 struct http_header *current_header;
1594 const uchar *separator;
1595 size_t name_length, value_length;
1596
1597 // Skip the first line (request or status line)
1598 line_end = strstr(line_start, "\r\n");
1599
1600 if (line_end)
1601 line_start = line_end + 2;
1602
1603 while ((line_end = strstr(line_start, "\r\n")) != NULL) {
1604 count = *num_headers;
1605 if (line_start == line_end || count >= MAX_HTTP_HEADERS)
1606 break;
1607 current_header = headers + count;
1608 separator = strchr(line_start, ':');
1609 if (separator) {
1610 name_length = separator - line_start;
1611 ++separator;
1612 while (*separator == ' ')
1613 ++separator;
1614 value_length = line_end - separator;
1615 if (name_length < MAX_HTTP_HEADER_NAME &&
1616 value_length < MAX_HTTP_HEADER_VALUE) {
1617 strncpy(current_header->name, line_start, name_length);
1618 current_header->name[name_length] = '\0';
1619 strncpy(current_header->value, separator, value_length);
1620 current_header->value[value_length] = '\0';
1621 (*num_headers)++;
1622 }
1623 }
1624 line_start = line_end + 2;
1625 }
1626}
1627
1628/**
1629 * efi_net_do_request() - issue an HTTP request using wget
1630 *
1631 * @url: url
1632 * @method: HTTP method
1633 * @buffer: data buffer
1634 * @status_code: HTTP status code
1635 * @file_size: file size in bytes
1636 * @headers_buffer: headers buffer
Adriano Cordova28d67772025-03-03 11:13:17 -03001637 * @parent: service binding protocol
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001638 * Return: status code
1639 */
1640efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, void **buffer,
Adriano Cordova28d67772025-03-03 11:13:17 -03001641 u32 *status_code, ulong *file_size, char *headers_buffer,
1642 struct efi_service_binding_protocol *parent)
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001643{
1644 efi_status_t ret = EFI_SUCCESS;
1645 int wget_ret;
1646 static bool last_head;
Adriano Cordova28d67772025-03-03 11:13:17 -03001647 struct udevice *dev;
1648 int i;
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001649
Adriano Cordova28d67772025-03-03 11:13:17 -03001650 if (!buffer || !file_size || !parent)
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001651 return EFI_ABORTED;
1652
1653 efi_wget_info.method = (enum wget_http_method)method;
1654 efi_wget_info.headers = headers_buffer;
1655
Adriano Cordova28d67772025-03-03 11:13:17 -03001656 // Set corresponding udevice
1657 dev = NULL;
1658 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1659 if (net_objs[i] && &net_objs[i]->http_service_binding == parent)
1660 dev = net_objs[i]->dev;
1661 }
1662 if (!dev)
1663 return EFI_ABORTED;
1664
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001665 switch (method) {
1666 case HTTP_METHOD_GET:
1667 ret = efi_net_set_buffer(buffer, last_head ? (size_t)efi_wget_info.hdr_cont_len : 0);
1668 if (ret != EFI_SUCCESS)
1669 goto out;
Adriano Cordova28d67772025-03-03 11:13:17 -03001670 eth_set_dev(dev);
Adriano Cordova54674692025-03-03 11:13:15 -03001671 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001672 wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info);
1673 if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) {
1674 // Try again with updated buffer size
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001675 efi_free_pool(*buffer);
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001676 ret = efi_net_set_buffer(buffer, (size_t)efi_wget_info.hdr_cont_len);
1677 if (ret != EFI_SUCCESS)
1678 goto out;
Adriano Cordova28d67772025-03-03 11:13:17 -03001679 eth_set_dev(dev);
Adriano Cordova54674692025-03-03 11:13:15 -03001680 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001681 if (wget_request((ulong)*buffer, url, &efi_wget_info)) {
1682 efi_free_pool(*buffer);
1683 ret = EFI_DEVICE_ERROR;
1684 goto out;
1685 }
1686 } else if (wget_ret) {
1687 efi_free_pool(*buffer);
1688 ret = EFI_DEVICE_ERROR;
1689 goto out;
1690 }
1691 // Pass the actual number of received bytes to the application
1692 *file_size = efi_wget_info.file_size;
1693 *status_code = efi_wget_info.status_code;
1694 last_head = false;
1695 break;
1696 case HTTP_METHOD_HEAD:
1697 ret = efi_net_set_buffer(buffer, 0);
1698 if (ret != EFI_SUCCESS)
1699 goto out;
Adriano Cordova28d67772025-03-03 11:13:17 -03001700 eth_set_dev(dev);
Adriano Cordova54674692025-03-03 11:13:15 -03001701 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001702 wget_request((ulong)*buffer, url, &efi_wget_info);
1703 *file_size = 0;
1704 *status_code = efi_wget_info.status_code;
1705 last_head = true;
1706 break;
1707 default:
1708 ret = EFI_UNSUPPORTED;
1709 break;
1710 }
1711
1712out:
1713 return ret;
1714}
Adriano Cordova54674692025-03-03 11:13:15 -03001715#endif