blob: 86f0af9538c87d6a3bb230c9d2d905ba6f0eb635 [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
Simon Glass37972f42025-05-24 11:28:21 -060020#include <efi_device_path.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020021#include <efi_loader.h>
Tom Rinic31301c2025-05-15 17:31:50 -060022#include <env.h>
Adriano Cordova93cba0f2024-12-04 00:05:23 -030023#include <dm.h>
Adriano Cordova0d1f5092024-12-04 00:05:24 -030024#include <linux/sizes.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020025#include <malloc.h>
Adriano Cordova3c951362024-12-04 00:05:21 -030026#include <vsprintf.h>
Simon Glass274e0b02020-05-10 11:39:56 -060027#include <net.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020028
Adriano Cordova28d67772025-03-03 11:13:17 -030029#define MAX_EFI_NET_OBJS 10
Adriano Cordova62e20fb2025-03-03 11:13:16 -030030#define MAX_NUM_DHCP_ENTRIES 10
Adriano Cordova54674692025-03-03 11:13:15 -030031#define MAX_NUM_DP_ENTRIES 10
32
Adriano Cordova7f2bcd42025-03-03 11:13:11 -030033const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020034static const efi_guid_t efi_pxe_base_code_protocol_guid =
35 EFI_PXE_BASE_CODE_PROTOCOL_GUID;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010036
Adriano Cordova54674692025-03-03 11:13:15 -030037struct dp_entry {
38 struct efi_device_path *net_dp;
39 struct udevice *dev;
40 bool is_valid;
41};
42
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020043/*
Adriano Cordova54674692025-03-03 11:13:15 -030044 * The network device path cache. An entry is added when a new bootfile
45 * is downloaded from the network. If the bootfile is then loaded as an
46 * efi image, the most recent entry corresponding to the device is passed
47 * as the device path of the loaded image.
Adriano Cordova93cba0f2024-12-04 00:05:23 -030048 */
Adriano Cordova54674692025-03-03 11:13:15 -030049static struct dp_entry dp_cache[MAX_NUM_DP_ENTRIES];
50static int next_dp_entry;
Adriano Cordova93cba0f2024-12-04 00:05:23 -030051
Adriano Cordova54674692025-03-03 11:13:15 -030052#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
Adriano Cordova0d1f5092024-12-04 00:05:24 -030053static struct wget_http_info efi_wget_info = {
54 .set_bootdev = false,
55 .check_buffer_size = true,
Jerome Forissier95b10352025-04-17 15:26:58 +020056 .silent = true,
Adriano Cordova0d1f5092024-12-04 00:05:24 -030057};
Adriano Cordova54674692025-03-03 11:13:15 -030058#endif
Adriano Cordova0d1f5092024-12-04 00:05:24 -030059
Adriano Cordova62e20fb2025-03-03 11:13:16 -030060struct dhcp_entry {
61 struct efi_pxe_packet *dhcp_ack;
62 struct udevice *dev;
63 bool is_valid;
64};
65
66static struct dhcp_entry dhcp_cache[MAX_NUM_DHCP_ENTRIES];
67static int next_dhcp_entry;
68
Heinrich Schuchardt72928722018-09-26 05:27:56 +020069/**
70 * struct efi_net_obj - EFI object representing a network interface
71 *
Adriano Cordova9debc902024-12-04 00:05:25 -030072 * @header: EFI object header
Adriano Cordova54674692025-03-03 11:13:15 -030073 * @dev: net udevice
Adriano Cordova9debc902024-12-04 00:05:25 -030074 * @net: simple network protocol interface
75 * @net_mode: status of the network interface
76 * @pxe: PXE base code protocol interface
77 * @pxe_mode: status of the PXE base code protocol
78 * @ip4_config2: IP4 Config2 protocol interface
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030079 * @http_service_binding: Http service binding protocol interface
Adriano Cordova28d67772025-03-03 11:13:17 -030080 * @new_tx_packet: new transmit packet
81 * @transmit_buffer: transmit buffer
82 * @receive_buffer: array of receive buffers
83 * @receive_lengths: array of lengths for received packets
84 * @rx_packet_idx: index of the current receive packet
85 * @rx_packet_num: number of received packets
86 * @wait_for_packet: signaled when a packet has been received
87 * @network_timer_event: event to check for new network packets.
88 * @efi_seq_num: sequence number of the EFI net object.
Heinrich Schuchardt72928722018-09-26 05:27:56 +020089 */
Alexander Graf94c4b992016-05-06 21:01:01 +020090struct efi_net_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020091 struct efi_object header;
Adriano Cordova54674692025-03-03 11:13:15 -030092 struct udevice *dev;
Alexander Graf94c4b992016-05-06 21:01:01 +020093 struct efi_simple_network net;
94 struct efi_simple_network_mode net_mode;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020095 struct efi_pxe_base_code_protocol pxe;
Alexander Graf94c4b992016-05-06 21:01:01 +020096 struct efi_pxe_mode pxe_mode;
Adriano Cordova9debc902024-12-04 00:05:25 -030097#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
98 struct efi_ip4_config2_protocol ip4_config2;
99#endif
Adriano Cordovae9b19eb2024-12-04 00:05:26 -0300100#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
101 struct efi_service_binding_protocol http_service_binding;
102#endif
Adriano Cordova28d67772025-03-03 11:13:17 -0300103 void *new_tx_packet;
104 void *transmit_buffer;
105 uchar **receive_buffer;
106 size_t *receive_lengths;
107 int rx_packet_idx;
108 int rx_packet_num;
109 struct efi_event *wait_for_packet;
110 struct efi_event *network_timer_event;
111 int efi_seq_num;
Alexander Graf94c4b992016-05-06 21:01:01 +0200112};
113
Adriano Cordova28d67772025-03-03 11:13:17 -0300114static int curr_efi_net_obj;
115static struct efi_net_obj *net_objs[MAX_EFI_NET_OBJS];
116
117/**
Adriano Cordova54674692025-03-03 11:13:15 -0300118 * efi_netobj_is_active() - checks if a netobj is active in the efi subsystem
119 *
Adriano Cordova28d67772025-03-03 11:13:17 -0300120 * @netobj: pointer to efi_net_obj
121 * Return: true if active
Adriano Cordova54674692025-03-03 11:13:15 -0300122 */
123static bool efi_netobj_is_active(struct efi_net_obj *netobj)
124{
125 if (!netobj || !efi_search_obj(&netobj->header))
126 return false;
127
128 return true;
129}
130
Adriano Cordova28d67772025-03-03 11:13:17 -0300131/*
132 * efi_netobj_from_snp() - get efi_net_obj from simple network protocol
133 *
134 *
135 * @snp: pointer to the simple network protocol
136 * Return: pointer to efi_net_obj, NULL on error
137 */
138static struct efi_net_obj *efi_netobj_from_snp(struct efi_simple_network *snp)
139{
140 int i;
141
142 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
143 if (net_objs[i] && &net_objs[i]->net == snp) {
144 // Do not register duplicate devices
145 return net_objs[i];
146 }
147 }
148 return NULL;
149}
Adriano Cordova54674692025-03-03 11:13:15 -0300150
151/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100152 * efi_net_start() - start the network interface
153 *
154 * This function implements the Start service of the
155 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
156 * (UEFI) specification for details.
157 *
158 * @this: pointer to the protocol instance
159 * Return: status code
160 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200161static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
162{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100163 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300164 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100165
Alexander Graf94c4b992016-05-06 21:01:01 +0200166 EFI_ENTRY("%p", this);
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100167 /* Check parameters */
168 if (!this) {
169 ret = EFI_INVALID_PARAMETER;
170 goto out;
171 }
172
Adriano Cordova28d67772025-03-03 11:13:17 -0300173 nt = efi_netobj_from_snp(this);
174
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200175 if (this->mode->state != EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100176 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200177 } else {
178 this->int_status = 0;
Adriano Cordova28d67772025-03-03 11:13:17 -0300179 nt->wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100180 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200181 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100182out:
183 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200184}
185
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100186/*
187 * efi_net_stop() - stop the network interface
188 *
189 * This function implements the Stop service of the
190 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
191 * (UEFI) specification for details.
192 *
193 * @this: pointer to the protocol instance
194 * Return: status code
195 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200196static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
197{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100198 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300199 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100200
Alexander Graf94c4b992016-05-06 21:01:01 +0200201 EFI_ENTRY("%p", this);
202
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100203 /* Check parameters */
204 if (!this) {
205 ret = EFI_INVALID_PARAMETER;
206 goto out;
207 }
208
Adriano Cordova28d67772025-03-03 11:13:17 -0300209 nt = efi_netobj_from_snp(this);
210
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200211 if (this->mode->state == EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100212 ret = EFI_NOT_STARTED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200213 } else {
214 /* Disable hardware and put it into the reset state */
Adriano Cordova28d67772025-03-03 11:13:17 -0300215 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300216 env_set("ethact", eth_get_name());
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200217 eth_halt();
Patrick Wildtfab89102020-10-07 11:04:33 +0200218 /* Clear cache of packets */
Adriano Cordova28d67772025-03-03 11:13:17 -0300219 nt->rx_packet_num = 0;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100220 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200221 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100222out:
223 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200224}
225
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200226/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100227 * efi_net_initialize() - initialize the network interface
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200228 *
229 * This function implements the Initialize service of the
230 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
231 * (UEFI) specification for details.
232 *
233 * @this: pointer to the protocol instance
234 * @extra_rx: extra receive buffer to be allocated
235 * @extra_tx: extra transmit buffer to be allocated
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100236 * Return: status code
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200237 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200238static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
239 ulong extra_rx, ulong extra_tx)
240{
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200241 int ret;
242 efi_status_t r = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300243 struct efi_net_obj *nt;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200244
Alexander Graf94c4b992016-05-06 21:01:01 +0200245 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
246
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100247 /* Check parameters */
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200248 if (!this) {
249 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100250 goto out;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200251 }
Adriano Cordova28d67772025-03-03 11:13:17 -0300252 nt = efi_netobj_from_snp(this);
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200253
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200254 switch (this->mode->state) {
255 case EFI_NETWORK_INITIALIZED:
256 case EFI_NETWORK_STARTED:
257 break;
258 default:
259 r = EFI_NOT_STARTED;
260 goto out;
261 }
262
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200263 /* Setup packet buffers */
264 net_init();
Patrick Wildtfab89102020-10-07 11:04:33 +0200265 /* Clear cache of packets */
Adriano Cordova28d67772025-03-03 11:13:17 -0300266 nt->rx_packet_num = 0;
Adriano Cordova54674692025-03-03 11:13:15 -0300267 /* Set the net device corresponding to the efi net object */
Adriano Cordova28d67772025-03-03 11:13:17 -0300268 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300269 env_set("ethact", eth_get_name());
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200270 /* Get hardware ready for send and receive operations */
Adriano Cordova28d67772025-03-03 11:13:17 -0300271 ret = eth_start_udev(nt->dev);
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200272 if (ret < 0) {
273 eth_halt();
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100274 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200275 r = EFI_DEVICE_ERROR;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100276 goto out;
277 } else {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200278 this->int_status = 0;
Adriano Cordova28d67772025-03-03 11:13:17 -0300279 nt->wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100280 this->mode->state = EFI_NETWORK_INITIALIZED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200281 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100282out:
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200283 return EFI_EXIT(r);
Alexander Graf94c4b992016-05-06 21:01:01 +0200284}
285
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100286/*
287 * efi_net_reset() - reinitialize the network interface
288 *
289 * This function implements the Reset service of the
290 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
291 * (UEFI) specification for details.
292 *
293 * @this: pointer to the protocol instance
294 * @extended_verification: execute exhaustive verification
295 * Return: status code
296 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200297static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
298 int extended_verification)
299{
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200300 efi_status_t ret;
301
Alexander Graf94c4b992016-05-06 21:01:01 +0200302 EFI_ENTRY("%p, %x", this, extended_verification);
303
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200304 /* Check parameters */
305 if (!this) {
306 ret = EFI_INVALID_PARAMETER;
307 goto out;
308 }
309
310 switch (this->mode->state) {
311 case EFI_NETWORK_INITIALIZED:
312 break;
313 case EFI_NETWORK_STOPPED:
314 ret = EFI_NOT_STARTED;
315 goto out;
316 default:
317 ret = EFI_DEVICE_ERROR;
318 goto out;
319 }
320
321 this->mode->state = EFI_NETWORK_STARTED;
322 ret = EFI_CALL(efi_net_initialize(this, 0, 0));
323out:
324 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200325}
326
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100327/*
328 * efi_net_shutdown() - shut down the network interface
329 *
330 * This function implements the Shutdown service of the
331 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
332 * (UEFI) specification for details.
333 *
334 * @this: pointer to the protocol instance
335 * Return: status code
336 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200337static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
338{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100339 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300340 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100341
Alexander Graf94c4b992016-05-06 21:01:01 +0200342 EFI_ENTRY("%p", this);
343
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100344 /* Check parameters */
345 if (!this) {
346 ret = EFI_INVALID_PARAMETER;
347 goto out;
348 }
Adriano Cordova28d67772025-03-03 11:13:17 -0300349 nt = efi_netobj_from_snp(this);
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100350
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200351 switch (this->mode->state) {
352 case EFI_NETWORK_INITIALIZED:
353 break;
354 case EFI_NETWORK_STOPPED:
355 ret = EFI_NOT_STARTED;
356 goto out;
357 default:
358 ret = EFI_DEVICE_ERROR;
359 goto out;
360 }
361
Adriano Cordova28d67772025-03-03 11:13:17 -0300362 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300363 env_set("ethact", eth_get_name());
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100364 eth_halt();
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300365
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200366 this->int_status = 0;
Adriano Cordova28d67772025-03-03 11:13:17 -0300367 nt->wait_for_packet->is_signaled = false;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200368 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100369
370out:
371 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200372}
373
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100374/*
375 * efi_net_receive_filters() - mange multicast receive filters
376 *
377 * This function implements the ReceiveFilters service of the
378 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
379 * (UEFI) specification for details.
380 *
381 * @this: pointer to the protocol instance
382 * @enable: bit mask of receive filters to enable
383 * @disable: bit mask of receive filters to disable
384 * @reset_mcast_filter: true resets contents of the filters
385 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
386 * @mcast_filter: list of new filters
387 * Return: status code
388 */
389static efi_status_t EFIAPI efi_net_receive_filters
390 (struct efi_simple_network *this, u32 enable, u32 disable,
391 int reset_mcast_filter, ulong mcast_filter_count,
392 struct efi_mac_address *mcast_filter)
Alexander Graf94c4b992016-05-06 21:01:01 +0200393{
394 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
395 reset_mcast_filter, mcast_filter_count, mcast_filter);
396
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200397 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200398}
399
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100400/*
401 * efi_net_station_address() - set the hardware MAC address
402 *
403 * This function implements the StationAddress service of the
404 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
405 * (UEFI) specification for details.
406 *
407 * @this: pointer to the protocol instance
408 * @reset: if true reset the address to default
409 * @new_mac: new MAC address
410 * Return: status code
411 */
412static efi_status_t EFIAPI efi_net_station_address
413 (struct efi_simple_network *this, int reset,
414 struct efi_mac_address *new_mac)
Alexander Graf94c4b992016-05-06 21:01:01 +0200415{
416 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
417
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200418 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200419}
420
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100421/*
422 * efi_net_statistics() - reset or collect statistics of the network interface
423 *
424 * This function implements the Statistics service of the
425 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
426 * (UEFI) specification for details.
427 *
428 * @this: pointer to the protocol instance
429 * @reset: if true, the statistics are reset
430 * @stat_size: size of the statistics table
431 * @stat_table: table to receive the statistics
432 * Return: status code
433 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200434static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
435 int reset, ulong *stat_size,
436 void *stat_table)
437{
438 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
439
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200440 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200441}
442
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100443/*
444 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
445 *
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200446 * This function implements the MCastIPtoMAC service of the
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100447 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
448 * (UEFI) specification for details.
449 *
450 * @this: pointer to the protocol instance
451 * @ipv6: true if the IP address is an IPv6 address
452 * @ip: IP address
453 * @mac: MAC address
454 * Return: status code
455 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200456static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
457 int ipv6,
458 struct efi_ip_address *ip,
459 struct efi_mac_address *mac)
460{
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200461 efi_status_t ret = EFI_SUCCESS;
462
Alexander Graf94c4b992016-05-06 21:01:01 +0200463 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
464
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200465 if (!this || !ip || !mac) {
466 ret = EFI_INVALID_PARAMETER;
467 goto out;
468 }
469
470 if (ipv6) {
471 ret = EFI_UNSUPPORTED;
472 goto out;
473 }
474
475 /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */
476 if ((ip->ip_addr[0] & 0xf0) != 0xe0) {
477 ret = EFI_INVALID_PARAMETER;
478 goto out;
479 };
480
481 switch (this->mode->state) {
482 case EFI_NETWORK_INITIALIZED:
483 case EFI_NETWORK_STARTED:
484 break;
485 default:
486 ret = EFI_NOT_STARTED;
487 goto out;
488 }
489
490 memset(mac, 0, sizeof(struct efi_mac_address));
491
492 /*
493 * Copy lower 23 bits of IPv4 multi-cast address
494 * RFC 1112, RFC 7042 2.1.1.
495 */
496 mac->mac_addr[0] = 0x01;
497 mac->mac_addr[1] = 0x00;
498 mac->mac_addr[2] = 0x5E;
499 mac->mac_addr[3] = ip->ip_addr[1] & 0x7F;
500 mac->mac_addr[4] = ip->ip_addr[2];
501 mac->mac_addr[5] = ip->ip_addr[3];
502out:
503 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200504}
505
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100506/**
507 * efi_net_nvdata() - read or write NVRAM
508 *
509 * This function implements the GetStatus service of the Simple Network
510 * Protocol. See the UEFI spec for details.
511 *
512 * @this: the instance of the Simple Network Protocol
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200513 * @read_write: true for read, false for write
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100514 * @offset: offset in NVRAM
515 * @buffer_size: size of buffer
516 * @buffer: buffer
517 * Return: status code
518 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200519static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
520 int read_write, ulong offset,
521 ulong buffer_size, char *buffer)
522{
523 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
524 buffer);
525
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200526 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200527}
528
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100529/**
530 * efi_net_get_status() - get interrupt status
531 *
532 * This function implements the GetStatus service of the Simple Network
533 * Protocol. See the UEFI spec for details.
534 *
535 * @this: the instance of the Simple Network Protocol
536 * @int_status: interface status
537 * @txbuf: transmission buffer
538 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200539static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
540 u32 *int_status, void **txbuf)
541{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100542 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300543 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100544
Alexander Graf94c4b992016-05-06 21:01:01 +0200545 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
546
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200547 efi_timer_check();
548
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100549 /* Check parameters */
550 if (!this) {
551 ret = EFI_INVALID_PARAMETER;
552 goto out;
553 }
554
Adriano Cordova28d67772025-03-03 11:13:17 -0300555 nt = efi_netobj_from_snp(this);
556
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100557 switch (this->mode->state) {
558 case EFI_NETWORK_STOPPED:
559 ret = EFI_NOT_STARTED;
560 goto out;
561 case EFI_NETWORK_STARTED:
562 ret = EFI_DEVICE_ERROR;
563 goto out;
564 default:
565 break;
566 }
567
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200568 if (int_status) {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200569 *int_status = this->int_status;
570 this->int_status = 0;
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200571 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200572 if (txbuf)
Adriano Cordova28d67772025-03-03 11:13:17 -0300573 *txbuf = nt->new_tx_packet;
Alexander Graf94c4b992016-05-06 21:01:01 +0200574
Adriano Cordova28d67772025-03-03 11:13:17 -0300575 nt->new_tx_packet = NULL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100576out:
577 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200578}
579
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100580/**
581 * efi_net_transmit() - transmit a packet
582 *
583 * This function implements the Transmit service of the Simple Network Protocol.
584 * See the UEFI spec for details.
585 *
586 * @this: the instance of the Simple Network Protocol
587 * @header_size: size of the media header
588 * @buffer_size: size of the buffer to receive the packet
589 * @buffer: buffer to receive the packet
590 * @src_addr: source hardware MAC address
591 * @dest_addr: destination hardware MAC address
592 * @protocol: type of header to build
593 * Return: status code
594 */
595static efi_status_t EFIAPI efi_net_transmit
596 (struct efi_simple_network *this, size_t header_size,
597 size_t buffer_size, void *buffer,
598 struct efi_mac_address *src_addr,
599 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200600{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100601 efi_status_t ret = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -0300602 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100603
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200604 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
605 (unsigned long)header_size, (unsigned long)buffer_size,
606 buffer, src_addr, dest_addr, protocol);
Alexander Graf94c4b992016-05-06 21:01:01 +0200607
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200608 efi_timer_check();
609
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100610 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200611 if (!this || !buffer) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100612 ret = EFI_INVALID_PARAMETER;
613 goto out;
614 }
615
Adriano Cordova28d67772025-03-03 11:13:17 -0300616 nt = efi_netobj_from_snp(this);
617
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100618 /* We do not support jumbo packets */
619 if (buffer_size > PKTSIZE_ALIGN) {
620 ret = EFI_INVALID_PARAMETER;
621 goto out;
622 }
623
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200624 /* At least the IP header has to fit into the buffer */
625 if (buffer_size < this->mode->media_header_size) {
626 ret = EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100627 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200628 }
629
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200630 /*
631 * TODO:
632 * Support VLANs. Use net_set_ether() for copying the header. Use a
633 * U_BOOT_ENV_CALLBACK to update the media header size.
634 */
635 if (header_size) {
636 struct ethernet_hdr *header = buffer;
637
638 if (!dest_addr || !protocol ||
639 header_size != this->mode->media_header_size) {
640 ret = EFI_INVALID_PARAMETER;
641 goto out;
642 }
643 if (!src_addr)
644 src_addr = &this->mode->current_address;
645
646 memcpy(header->et_dest, dest_addr, ARP_HLEN);
647 memcpy(header->et_src, src_addr, ARP_HLEN);
648 header->et_protlen = htons(*protocol);
649 }
650
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100651 switch (this->mode->state) {
652 case EFI_NETWORK_STOPPED:
653 ret = EFI_NOT_STARTED;
654 goto out;
655 case EFI_NETWORK_STARTED:
656 ret = EFI_DEVICE_ERROR;
657 goto out;
658 default:
659 break;
660 }
661
Adriano Cordova28d67772025-03-03 11:13:17 -0300662 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300663 env_set("ethact", eth_get_name());
664
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100665 /* Ethernet packets always fit, just bounce */
Adriano Cordova28d67772025-03-03 11:13:17 -0300666 memcpy(nt->transmit_buffer, buffer, buffer_size);
667 net_send_packet(nt->transmit_buffer, buffer_size);
Alexander Graf0b12b862016-09-06 14:26:27 +0200668
Adriano Cordova28d67772025-03-03 11:13:17 -0300669 nt->new_tx_packet = buffer;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200670 this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100671out:
672 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200673}
674
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100675/**
676 * efi_net_receive() - receive a packet from a network interface
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200677 *
678 * This function implements the Receive service of the Simple Network Protocol.
679 * See the UEFI spec for details.
680 *
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100681 * @this: the instance of the Simple Network Protocol
682 * @header_size: size of the media header
683 * @buffer_size: size of the buffer to receive the packet
684 * @buffer: buffer to receive the packet
685 * @src_addr: source MAC address
686 * @dest_addr: destination MAC address
687 * @protocol: protocol
688 * Return: status code
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200689 */
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100690static efi_status_t EFIAPI efi_net_receive
691 (struct efi_simple_network *this, size_t *header_size,
692 size_t *buffer_size, void *buffer,
693 struct efi_mac_address *src_addr,
694 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200695{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100696 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200697 struct ethernet_hdr *eth_hdr;
698 size_t hdr_size = sizeof(struct ethernet_hdr);
699 u16 protlen;
Adriano Cordova28d67772025-03-03 11:13:17 -0300700 struct efi_net_obj *nt;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200701
Alexander Graf94c4b992016-05-06 21:01:01 +0200702 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
703 buffer_size, buffer, src_addr, dest_addr, protocol);
704
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100705 /* Execute events */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200706 efi_timer_check();
Alexander Graf94c4b992016-05-06 21:01:01 +0200707
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100708 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200709 if (!this || !buffer || !buffer_size) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100710 ret = EFI_INVALID_PARAMETER;
711 goto out;
712 }
713
Adriano Cordova28d67772025-03-03 11:13:17 -0300714 nt = efi_netobj_from_snp(this);
715
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100716 switch (this->mode->state) {
717 case EFI_NETWORK_STOPPED:
718 ret = EFI_NOT_STARTED;
719 goto out;
720 case EFI_NETWORK_STARTED:
721 ret = EFI_DEVICE_ERROR;
722 goto out;
723 default:
724 break;
725 }
726
Adriano Cordova28d67772025-03-03 11:13:17 -0300727 if (!nt->rx_packet_num) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100728 ret = EFI_NOT_READY;
729 goto out;
730 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200731 /* Fill export parameters */
Adriano Cordova28d67772025-03-03 11:13:17 -0300732 eth_hdr = (struct ethernet_hdr *)nt->receive_buffer[nt->rx_packet_idx];
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200733 protlen = ntohs(eth_hdr->et_protlen);
734 if (protlen == 0x8100) {
735 hdr_size += 4;
Adriano Cordova28d67772025-03-03 11:13:17 -0300736 protlen = ntohs(*(u16 *)&nt->receive_buffer[nt->rx_packet_idx][hdr_size - 2]);
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200737 }
738 if (header_size)
739 *header_size = hdr_size;
740 if (dest_addr)
741 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
742 if (src_addr)
743 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
744 if (protocol)
745 *protocol = protlen;
Adriano Cordova28d67772025-03-03 11:13:17 -0300746 if (*buffer_size < nt->receive_lengths[nt->rx_packet_idx]) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200747 /* Packet doesn't fit, try again with bigger buffer */
Adriano Cordova28d67772025-03-03 11:13:17 -0300748 *buffer_size = nt->receive_lengths[nt->rx_packet_idx];
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100749 ret = EFI_BUFFER_TOO_SMALL;
750 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200751 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200752 /* Copy packet */
Adriano Cordova28d67772025-03-03 11:13:17 -0300753 memcpy(buffer, nt->receive_buffer[nt->rx_packet_idx],
754 nt->receive_lengths[nt->rx_packet_idx]);
755 *buffer_size = nt->receive_lengths[nt->rx_packet_idx];
756 nt->rx_packet_idx = (nt->rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV;
757 nt->rx_packet_num--;
758 if (nt->rx_packet_num)
759 nt->wait_for_packet->is_signaled = true;
Patrick Wildtfab89102020-10-07 11:04:33 +0200760 else
761 this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100762out:
763 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200764}
765
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100766/**
767 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
768 *
769 * This function is called by dhcp_handler().
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200770 *
771 * @pkt: packet received by dhcp_handler()
772 * @len: length of the packet received
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100773 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200774void efi_net_set_dhcp_ack(void *pkt, int len)
775{
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300776 struct efi_pxe_packet **dhcp_ack;
777 struct udevice *dev;
Adriano Cordova28d67772025-03-03 11:13:17 -0300778 int i;
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300779
780 dhcp_ack = &dhcp_cache[next_dhcp_entry].dhcp_ack;
Alexander Graf94c4b992016-05-06 21:01:01 +0200781
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300782 /* For now this function gets called only by the current device */
783 dev = eth_get_dev();
784
785 int maxsize = sizeof(**dhcp_ack);
786
787 if (!*dhcp_ack) {
788 *dhcp_ack = malloc(maxsize);
789 if (!*dhcp_ack)
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100790 return;
791 }
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300792 memset(*dhcp_ack, 0, maxsize);
793 memcpy(*dhcp_ack, pkt, min(len, maxsize));
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100794
Adriano Cordova62e20fb2025-03-03 11:13:16 -0300795 dhcp_cache[next_dhcp_entry].is_valid = true;
796 dhcp_cache[next_dhcp_entry].dev = dev;
797 next_dhcp_entry++;
798 next_dhcp_entry %= MAX_NUM_DHCP_ENTRIES;
Adriano Cordova28d67772025-03-03 11:13:17 -0300799
800 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
801 if (net_objs[i] && net_objs[i]->dev == dev) {
802 net_objs[i]->pxe_mode.dhcp_ack = **dhcp_ack;
803 }
804 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200805}
806
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100807/**
808 * efi_net_push() - callback for received network packet
809 *
810 * This function is called when a network packet is received by eth_rx().
811 *
812 * @pkt: network packet
813 * @len: length
814 */
815static void efi_net_push(void *pkt, int len)
816{
Patrick Wildtfab89102020-10-07 11:04:33 +0200817 int rx_packet_next;
Adriano Cordova28d67772025-03-03 11:13:17 -0300818 struct efi_net_obj *nt;
819
820 nt = net_objs[curr_efi_net_obj];
821 if (!nt)
822 return;
Patrick Wildtfab89102020-10-07 11:04:33 +0200823
824 /* Check that we at least received an Ethernet header */
825 if (len < sizeof(struct ethernet_hdr))
826 return;
827
828 /* Check that the buffer won't overflow */
829 if (len > PKTSIZE_ALIGN)
830 return;
831
832 /* Can't store more than pre-alloced buffer */
Adriano Cordova28d67772025-03-03 11:13:17 -0300833 if (nt->rx_packet_num >= ETH_PACKETS_BATCH_RECV)
Patrick Wildtfab89102020-10-07 11:04:33 +0200834 return;
835
Adriano Cordova28d67772025-03-03 11:13:17 -0300836 rx_packet_next = (nt->rx_packet_idx + nt->rx_packet_num) %
Patrick Wildtfab89102020-10-07 11:04:33 +0200837 ETH_PACKETS_BATCH_RECV;
Adriano Cordova28d67772025-03-03 11:13:17 -0300838 memcpy(nt->receive_buffer[rx_packet_next], pkt, len);
839 nt->receive_lengths[rx_packet_next] = len;
Patrick Wildtfab89102020-10-07 11:04:33 +0200840
Adriano Cordova28d67772025-03-03 11:13:17 -0300841 nt->rx_packet_num++;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100842}
843
844/**
845 * efi_network_timer_notify() - check if a new network packet has been received
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200846 *
847 * This notification function is called in every timer cycle.
848 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200849 * @event: the event for which this notification function is registered
850 * @context: event context - not used in this function
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200851 */
852static void EFIAPI efi_network_timer_notify(struct efi_event *event,
853 void *context)
854{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100855 struct efi_simple_network *this = (struct efi_simple_network *)context;
Adriano Cordova28d67772025-03-03 11:13:17 -0300856 struct efi_net_obj *nt;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100857
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200858 EFI_ENTRY("%p, %p", event, context);
859
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100860 /*
861 * Some network drivers do not support calling eth_rx() before
862 * initialization.
863 */
864 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
865 goto out;
866
Adriano Cordova28d67772025-03-03 11:13:17 -0300867 nt = efi_netobj_from_snp(this);
868 curr_efi_net_obj = nt->efi_seq_num;
869
870 if (!nt->rx_packet_num) {
871 eth_set_dev(nt->dev);
Adriano Cordova54674692025-03-03 11:13:15 -0300872 env_set("ethact", eth_get_name());
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200873 push_packet = efi_net_push;
874 eth_rx();
875 push_packet = NULL;
Adriano Cordova28d67772025-03-03 11:13:17 -0300876 if (nt->rx_packet_num) {
Patrick Wildtfab89102020-10-07 11:04:33 +0200877 this->int_status |=
878 EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Adriano Cordova28d67772025-03-03 11:13:17 -0300879 nt->wait_for_packet->is_signaled = true;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200880 }
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200881 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100882out:
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200883 EFI_EXIT(EFI_SUCCESS);
884}
885
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200886static efi_status_t EFIAPI efi_pxe_base_code_start(
887 struct efi_pxe_base_code_protocol *this,
888 u8 use_ipv6)
889{
890 return EFI_UNSUPPORTED;
891}
892
893static efi_status_t EFIAPI efi_pxe_base_code_stop(
894 struct efi_pxe_base_code_protocol *this)
895{
896 return EFI_UNSUPPORTED;
897}
898
899static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
900 struct efi_pxe_base_code_protocol *this,
901 u8 sort_offers)
902{
903 return EFI_UNSUPPORTED;
904}
905
906static efi_status_t EFIAPI efi_pxe_base_code_discover(
907 struct efi_pxe_base_code_protocol *this,
908 u16 type, u16 *layer, u8 bis,
909 struct efi_pxe_base_code_discover_info *info)
910{
911 return EFI_UNSUPPORTED;
912}
913
914static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
915 struct efi_pxe_base_code_protocol *this,
916 u32 operation, void *buffer_ptr,
917 u8 overwrite, efi_uintn_t *buffer_size,
918 struct efi_ip_address server_ip, char *filename,
919 struct efi_pxe_base_code_mtftp_info *info,
920 u8 dont_use_buffer)
921{
922 return EFI_UNSUPPORTED;
923}
924
925static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
926 struct efi_pxe_base_code_protocol *this,
927 u16 op_flags, struct efi_ip_address *dest_ip,
928 u16 *dest_port,
929 struct efi_ip_address *gateway_ip,
930 struct efi_ip_address *src_ip, u16 *src_port,
931 efi_uintn_t *header_size, void *header_ptr,
932 efi_uintn_t *buffer_size, void *buffer_ptr)
933{
934 return EFI_UNSUPPORTED;
935}
936
937static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
938 struct efi_pxe_base_code_protocol *this,
939 u16 op_flags, struct efi_ip_address *dest_ip,
940 u16 *dest_port, struct efi_ip_address *src_ip,
941 u16 *src_port, efi_uintn_t *header_size,
942 void *header_ptr, efi_uintn_t *buffer_size,
943 void *buffer_ptr)
944{
945 return EFI_UNSUPPORTED;
946}
947
948static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
949 struct efi_pxe_base_code_protocol *this,
950 struct efi_pxe_base_code_filter *new_filter)
951{
952 return EFI_UNSUPPORTED;
953}
954
955static efi_status_t EFIAPI efi_pxe_base_code_arp(
956 struct efi_pxe_base_code_protocol *this,
957 struct efi_ip_address *ip_addr,
958 struct efi_mac_address *mac_addr)
959{
960 return EFI_UNSUPPORTED;
961}
962
963static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
964 struct efi_pxe_base_code_protocol *this,
965 u8 *new_auto_arp, u8 *new_send_guid,
966 u8 *new_ttl, u8 *new_tos,
967 u8 *new_make_callback)
968{
969 return EFI_UNSUPPORTED;
970}
971
972static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
973 struct efi_pxe_base_code_protocol *this,
974 struct efi_ip_address *new_station_ip,
975 struct efi_ip_address *new_subnet_mask)
976{
977 return EFI_UNSUPPORTED;
978}
979
980static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
981 struct efi_pxe_base_code_protocol *this,
982 u8 *new_dhcp_discover_valid,
983 u8 *new_dhcp_ack_received,
984 u8 *new_proxy_offer_received,
985 u8 *new_pxe_discover_valid,
986 u8 *new_pxe_reply_received,
987 u8 *new_pxe_bis_reply_received,
988 EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
989 EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
990 EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
991 EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
992 EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
993 EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
994{
995 return EFI_UNSUPPORTED;
996}
997
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100998/**
Adriano Cordova54674692025-03-03 11:13:15 -0300999 * efi_netobj_set_dp() - set device path of a netobj
1000 *
1001 * @netobj: pointer to efi_net_obj
1002 * @dp: device path to set, allocated by caller
1003 * Return: status code
1004 */
1005efi_status_t efi_netobj_set_dp(struct efi_net_obj *netobj, struct efi_device_path *dp)
1006{
1007 efi_status_t ret;
1008 struct efi_handler *phandler;
1009 struct efi_device_path *new_net_dp;
1010
1011 if (!efi_netobj_is_active(netobj))
1012 return EFI_SUCCESS;
1013
1014 // Create a device path for the netobj
1015 new_net_dp = dp;
1016 if (!new_net_dp)
1017 return EFI_OUT_OF_RESOURCES;
1018
1019 phandler = NULL;
1020 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
1021
1022 // If the device path protocol is not yet installed, install it
1023 if (!phandler)
1024 goto add;
1025
1026 // If it is already installed, try to update it
1027 ret = efi_reinstall_protocol_interface(&netobj->header, &efi_guid_device_path,
1028 phandler->protocol_interface, new_net_dp);
1029 if (ret != EFI_SUCCESS)
1030 return ret;
1031
1032 return EFI_SUCCESS;
1033add:
1034 ret = efi_add_protocol(&netobj->header, &efi_guid_device_path,
1035 new_net_dp);
1036 if (ret != EFI_SUCCESS)
1037 return ret;
1038
1039 return EFI_SUCCESS;
1040}
1041
1042/**
1043 * efi_netobj_get_dp() - get device path of a netobj
1044 *
1045 * @netobj: pointer to efi_net_obj
1046 * Return: device path, NULL on error
1047 */
1048static struct efi_device_path *efi_netobj_get_dp(struct efi_net_obj *netobj)
1049{
1050 struct efi_handler *phandler;
1051
1052 if (!efi_netobj_is_active(netobj))
1053 return NULL;
1054
1055 phandler = NULL;
1056 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
1057
1058 if (phandler && phandler->protocol_interface)
1059 return efi_dp_dup(phandler->protocol_interface);
1060
1061 return NULL;
1062}
1063
1064/**
Adriano Cordova1d70b682025-03-03 11:13:13 -03001065 * efi_net_do_start() - start the efi network stack
1066 *
1067 * This gets called from do_bootefi_exec() each time a payload gets executed.
1068 *
Adriano Cordova54674692025-03-03 11:13:15 -03001069 * @dev: net udevice
Adriano Cordova1d70b682025-03-03 11:13:13 -03001070 * Return: status code
1071 */
Adriano Cordova54674692025-03-03 11:13:15 -03001072efi_status_t efi_net_do_start(struct udevice *dev)
Adriano Cordova1d70b682025-03-03 11:13:13 -03001073{
1074 efi_status_t r = EFI_SUCCESS;
Adriano Cordova28d67772025-03-03 11:13:17 -03001075 struct efi_net_obj *netobj;
Adriano Cordova54674692025-03-03 11:13:15 -03001076 struct efi_device_path *net_dp;
Adriano Cordova28d67772025-03-03 11:13:17 -03001077 int i;
Adriano Cordova54674692025-03-03 11:13:15 -03001078
Adriano Cordova28d67772025-03-03 11:13:17 -03001079 netobj = NULL;
1080 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1081 if (net_objs[i] && net_objs[i]->dev == dev) {
1082 netobj = net_objs[i];
1083 break;
1084 }
1085 }
1086
1087 if (!efi_netobj_is_active(netobj))
Adriano Cordova54674692025-03-03 11:13:15 -03001088 return r;
1089
1090 efi_net_dp_from_dev(&net_dp, netobj->dev, true);
1091 // If no dp cache entry applies and there already
1092 // is a device path installed, continue
1093 if (!net_dp) {
1094 if (efi_netobj_get_dp(netobj))
1095 goto set_addr;
1096 else
1097 net_dp = efi_dp_from_eth(netobj->dev);
Adriano Cordova1d70b682025-03-03 11:13:13 -03001098
Adriano Cordova54674692025-03-03 11:13:15 -03001099 }
1100
1101 if (!net_dp)
1102 return EFI_OUT_OF_RESOURCES;
1103
1104 r = efi_netobj_set_dp(netobj, net_dp);
1105 if (r != EFI_SUCCESS)
1106 return r;
1107set_addr:
Adriano Cordova1d70b682025-03-03 11:13:13 -03001108#ifdef CONFIG_EFI_HTTP_PROTOCOL
1109 /*
1110 * No harm on doing the following. If the PXE handle is present, the client could
1111 * find it and try to get its IP address from it. In here the PXE handle is present
1112 * but the PXE protocol is not yet implmenented, so we add this in the meantime.
1113 */
1114 efi_net_get_addr((struct efi_ipv4_address *)&netobj->pxe_mode.station_ip,
Adriano Cordova54674692025-03-03 11:13:15 -03001115 (struct efi_ipv4_address *)&netobj->pxe_mode.subnet_mask, NULL, dev);
Adriano Cordova1d70b682025-03-03 11:13:13 -03001116#endif
1117
1118 return r;
1119}
1120
1121/**
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001122 * efi_net_register() - register the simple network protocol
1123 *
1124 * This gets called from do_bootefi_exec().
Adriano Cordova54674692025-03-03 11:13:15 -03001125 * @dev: net udevice
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001126 */
Adriano Cordova54674692025-03-03 11:13:15 -03001127efi_status_t efi_net_register(struct udevice *dev)
Alexander Graf94c4b992016-05-06 21:01:01 +02001128{
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001129 efi_status_t r;
Adriano Cordova28d67772025-03-03 11:13:17 -03001130 int seq_num;
1131 struct efi_net_obj *netobj;
1132 void *transmit_buffer = NULL;
1133 uchar **receive_buffer = NULL;
1134 size_t *receive_lengths;
Adriano Cordova62e20fb2025-03-03 11:13:16 -03001135 int i, j;
Alexander Graf94c4b992016-05-06 21:01:01 +02001136
Adriano Cordova54674692025-03-03 11:13:15 -03001137 if (!dev) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001138 /* No network device active, don't expose any */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001139 return EFI_SUCCESS;
Alexander Graf94c4b992016-05-06 21:01:01 +02001140 }
Adriano Cordova28d67772025-03-03 11:13:17 -03001141
1142 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1143 if (net_objs[i] && net_objs[i]->dev == dev) {
1144 // Do not register duplicate devices
1145 return EFI_SUCCESS;
1146 }
1147 }
1148
1149 seq_num = -1;
1150 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1151 if (!net_objs[i]) {
1152 seq_num = i;
1153 break;
1154 }
1155 }
1156 if (seq_num < 0)
1157 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001158
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001159 /* We only expose the "active" network device, so one is enough */
Alexander Graf94c4b992016-05-06 21:01:01 +02001160 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001161 if (!netobj)
1162 goto out_of_resources;
1163
Adriano Cordova54674692025-03-03 11:13:15 -03001164 netobj->dev = dev;
1165
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001166 /* Allocate an aligned transmit buffer */
1167 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
1168 if (!transmit_buffer)
1169 goto out_of_resources;
1170 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Adriano Cordova28d67772025-03-03 11:13:17 -03001171 netobj->transmit_buffer = transmit_buffer;
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001172
Patrick Wildtfab89102020-10-07 11:04:33 +02001173 /* Allocate a number of receive buffers */
1174 receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
1175 sizeof(*receive_buffer));
1176 if (!receive_buffer)
1177 goto out_of_resources;
1178 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
1179 receive_buffer[i] = malloc(PKTSIZE_ALIGN);
1180 if (!receive_buffer[i])
1181 goto out_of_resources;
1182 }
Adriano Cordova28d67772025-03-03 11:13:17 -03001183 netobj->receive_buffer = receive_buffer;
Adriano Cordova54674692025-03-03 11:13:15 -03001184
Patrick Wildtfab89102020-10-07 11:04:33 +02001185 receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
1186 sizeof(*receive_lengths));
1187 if (!receive_lengths)
1188 goto out_of_resources;
Adriano Cordova28d67772025-03-03 11:13:17 -03001189 netobj->receive_lengths = receive_lengths;
Patrick Wildtfab89102020-10-07 11:04:33 +02001190
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001191 /* Hook net up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001192 efi_add_handle(&netobj->header);
Alexander Graf94c4b992016-05-06 21:01:01 +02001193
1194 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001195 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001196 &netobj->net);
1197 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001198 goto failure_to_add_protocol;
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001199
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +02001200 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001201 &netobj->pxe);
1202 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001203 goto failure_to_add_protocol;
Heinrich Schuchardtd54396a2017-10-05 16:35:57 +02001204 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf94c4b992016-05-06 21:01:01 +02001205 netobj->net.start = efi_net_start;
1206 netobj->net.stop = efi_net_stop;
1207 netobj->net.initialize = efi_net_initialize;
1208 netobj->net.reset = efi_net_reset;
1209 netobj->net.shutdown = efi_net_shutdown;
1210 netobj->net.receive_filters = efi_net_receive_filters;
1211 netobj->net.station_address = efi_net_station_address;
1212 netobj->net.statistics = efi_net_statistics;
1213 netobj->net.mcastiptomac = efi_net_mcastiptomac;
1214 netobj->net.nvdata = efi_net_nvdata;
1215 netobj->net.get_status = efi_net_get_status;
1216 netobj->net.transmit = efi_net_transmit;
1217 netobj->net.receive = efi_net_receive;
1218 netobj->net.mode = &netobj->net_mode;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +02001219 netobj->net_mode.state = EFI_NETWORK_STOPPED;
Adriano Cordova54674692025-03-03 11:13:15 -03001220 if (dev_get_plat(dev))
1221 memcpy(netobj->net_mode.current_address.mac_addr,
1222 ((struct eth_pdata *)dev_get_plat(dev))->enetaddr, 6);
Heinrich Schuchardtd1cc6cb2017-10-05 16:35:58 +02001223 netobj->net_mode.hwaddr_size = ARP_HLEN;
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +02001224 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
Alexander Graf94c4b992016-05-06 21:01:01 +02001225 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomas27df0a72018-06-21 16:21:01 -07001226 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf94c4b992016-05-06 21:01:01 +02001227
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +02001228 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
1229 netobj->pxe.start = efi_pxe_base_code_start;
1230 netobj->pxe.stop = efi_pxe_base_code_stop;
1231 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
1232 netobj->pxe.discover = efi_pxe_base_code_discover;
1233 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
1234 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
1235 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
1236 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
1237 netobj->pxe.arp = efi_pxe_base_code_arp;
1238 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
1239 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
1240 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf94c4b992016-05-06 21:01:01 +02001241 netobj->pxe.mode = &netobj->pxe_mode;
Adriano Cordova62e20fb2025-03-03 11:13:16 -03001242
1243 /*
1244 * Scan dhcp entries for one corresponding
1245 * to this udevice, from newest to oldest
1246 */
1247 i = (next_dhcp_entry + MAX_NUM_DHCP_ENTRIES - 1) % MAX_NUM_DHCP_ENTRIES;
1248 for (j = 0; dhcp_cache[i].is_valid && j < MAX_NUM_DHCP_ENTRIES;
1249 i = (i + MAX_NUM_DHCP_ENTRIES - 1) % MAX_NUM_DHCP_ENTRIES, j++) {
1250 if (dev == dhcp_cache[i].dev) {
1251 netobj->pxe_mode.dhcp_ack = *dhcp_cache[i].dhcp_ack;
1252 break;
1253 }
1254 }
Alexander Graf94c4b992016-05-06 21:01:01 +02001255
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001256 /*
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001257 * Create WaitForPacket event.
1258 */
1259 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001260 efi_network_timer_notify, NULL, NULL,
Adriano Cordova28d67772025-03-03 11:13:17 -03001261 &netobj->wait_for_packet);
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001262 if (r != EFI_SUCCESS) {
1263 printf("ERROR: Failed to register network event\n");
1264 return r;
1265 }
Adriano Cordova28d67772025-03-03 11:13:17 -03001266 netobj->net.wait_for_packet = netobj->wait_for_packet;
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001267 /*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001268 * Create a timer event.
1269 *
1270 * The notification function is used to check if a new network packet
1271 * has been received.
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001272 *
1273 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001274 */
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001275 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001276 efi_network_timer_notify, &netobj->net, NULL,
Adriano Cordova28d67772025-03-03 11:13:17 -03001277 &netobj->network_timer_event);
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001278 if (r != EFI_SUCCESS) {
1279 printf("ERROR: Failed to register network event\n");
1280 return r;
1281 }
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001282 /* Network is time critical, create event in every timer cycle */
Adriano Cordova28d67772025-03-03 11:13:17 -03001283 r = efi_set_timer(netobj->network_timer_event, EFI_TIMER_PERIODIC, 0);
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001284 if (r != EFI_SUCCESS) {
1285 printf("ERROR: Failed to set network timer\n");
1286 return r;
1287 }
1288
Adriano Cordova9debc902024-12-04 00:05:25 -03001289#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
1290 r = efi_ipconfig_register(&netobj->header, &netobj->ip4_config2);
1291 if (r != EFI_SUCCESS)
1292 goto failure_to_add_protocol;
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001293#endif
1294
1295#ifdef CONFIG_EFI_HTTP_PROTOCOL
1296 r = efi_http_register(&netobj->header, &netobj->http_service_binding);
1297 if (r != EFI_SUCCESS)
1298 goto failure_to_add_protocol;
Adriano Cordova9debc902024-12-04 00:05:25 -03001299#endif
Adriano Cordova28d67772025-03-03 11:13:17 -03001300 netobj->efi_seq_num = seq_num;
1301 net_objs[seq_num] = netobj;
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001302 return EFI_SUCCESS;
1303failure_to_add_protocol:
1304 printf("ERROR: Failure to add protocol\n");
1305 return r;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001306out_of_resources:
1307 free(netobj);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +01001308 netobj = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +02001309 free(transmit_buffer);
1310 if (receive_buffer)
1311 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
1312 free(receive_buffer[i]);
1313 free(receive_buffer);
1314 free(receive_lengths);
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001315 printf("ERROR: Out of memory\n");
1316 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001317}
Adriano Cordova3c951362024-12-04 00:05:21 -03001318
1319/**
Adriano Cordova54674692025-03-03 11:13:15 -03001320 * efi_net_new_dp() - update device path associated to a net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001321 *
1322 * This gets called to update the device path when a new boot
1323 * file is downloaded
1324 *
1325 * @dev: dev to set the device path from
1326 * @server: remote server address
Adriano Cordova54674692025-03-03 11:13:15 -03001327 * @udev: net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001328 * Return: status code
1329 */
Adriano Cordova54674692025-03-03 11:13:15 -03001330efi_status_t efi_net_new_dp(const char *dev, const char *server, struct udevice *udev)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001331{
Adriano Cordova54674692025-03-03 11:13:15 -03001332 efi_status_t ret;
Adriano Cordova28d67772025-03-03 11:13:17 -03001333 struct efi_net_obj *netobj;
Adriano Cordova54674692025-03-03 11:13:15 -03001334 struct efi_device_path *old_net_dp, *new_net_dp;
1335 struct efi_device_path **dp;
Adriano Cordova28d67772025-03-03 11:13:17 -03001336 int i;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001337
Adriano Cordova54674692025-03-03 11:13:15 -03001338 dp = &dp_cache[next_dp_entry].net_dp;
1339
1340 dp_cache[next_dp_entry].dev = udev;
1341 dp_cache[next_dp_entry].is_valid = true;
1342 next_dp_entry++;
1343 next_dp_entry %= MAX_NUM_DP_ENTRIES;
1344
1345 old_net_dp = *dp;
1346 new_net_dp = NULL;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001347 if (!strcmp(dev, "Net"))
Adriano Cordova54674692025-03-03 11:13:15 -03001348 new_net_dp = efi_dp_from_eth(udev);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001349 else if (!strcmp(dev, "Http"))
Adriano Cordova54674692025-03-03 11:13:15 -03001350 new_net_dp = efi_dp_from_http(server, udev);
1351 if (!new_net_dp)
1352 return EFI_OUT_OF_RESOURCES;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001353
Adriano Cordova54674692025-03-03 11:13:15 -03001354 *dp = new_net_dp;
1355 // Free the old cache entry
1356 efi_free_pool(old_net_dp);
1357
Adriano Cordova28d67772025-03-03 11:13:17 -03001358 netobj = NULL;
1359 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1360 if (net_objs[i] && net_objs[i]->dev == udev) {
1361 netobj = net_objs[i];
1362 break;
1363 }
1364 }
1365 if (!netobj)
Adriano Cordova54674692025-03-03 11:13:15 -03001366 return EFI_SUCCESS;
1367
1368 new_net_dp = efi_dp_dup(*dp);
1369 if (!new_net_dp)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001370 return EFI_OUT_OF_RESOURCES;
Adriano Cordova54674692025-03-03 11:13:15 -03001371 ret = efi_netobj_set_dp(netobj, new_net_dp);
1372 if (ret != EFI_SUCCESS)
1373 efi_free_pool(new_net_dp);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001374
Adriano Cordova54674692025-03-03 11:13:15 -03001375 return ret;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001376}
1377
1378/**
Adriano Cordova54674692025-03-03 11:13:15 -03001379 * efi_net_dp_from_dev() - get device path associated to a net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001380 *
1381 * Produce a copy of the current device path
1382 *
Adriano Cordova54674692025-03-03 11:13:15 -03001383 * @dp: copy of the current device path
1384 * @udev: net udevice
1385 * @cache_only: get device path from cache only
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001386 */
Adriano Cordova54674692025-03-03 11:13:15 -03001387void efi_net_dp_from_dev(struct efi_device_path **dp, struct udevice *udev, bool cache_only)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001388{
Adriano Cordova54674692025-03-03 11:13:15 -03001389 int i, j;
1390
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001391 if (!dp)
1392 return;
Adriano Cordova54674692025-03-03 11:13:15 -03001393
1394 *dp = NULL;
1395
1396 if (cache_only)
1397 goto cache;
1398
Adriano Cordova28d67772025-03-03 11:13:17 -03001399 // If a netobj matches:
1400 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1401 if (net_objs[i] && net_objs[i]->dev == udev) {
1402 *dp = efi_netobj_get_dp(net_objs[i]);
1403 if (*dp)
1404 return;
1405 }
Adriano Cordova54674692025-03-03 11:13:15 -03001406 }
1407cache:
1408 // Search in the cache
1409 i = (next_dp_entry + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES;
1410 for (j = 0; dp_cache[i].is_valid && j < MAX_NUM_DP_ENTRIES;
1411 i = (i + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES, j++) {
1412 if (dp_cache[i].dev == udev) {
1413 *dp = efi_dp_dup(dp_cache[i].net_dp);
1414 return;
1415 }
1416 }
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001417}
1418
1419/**
Adriano Cordova3c951362024-12-04 00:05:21 -03001420 * efi_net_get_addr() - get IP address information
1421 *
1422 * Copy the current IP address, mask, and gateway into the
1423 * efi_ipv4_address structs pointed to by ip, mask and gw,
1424 * respectively.
1425 *
1426 * @ip: pointer to an efi_ipv4_address struct to
1427 * be filled with the current IP address
1428 * @mask: pointer to an efi_ipv4_address struct to
1429 * be filled with the current network mask
1430 * @gw: pointer to an efi_ipv4_address struct to be
1431 * filled with the current network gateway
Adriano Cordova54674692025-03-03 11:13:15 -03001432 * @dev: udevice
Adriano Cordova3c951362024-12-04 00:05:21 -03001433 */
1434void efi_net_get_addr(struct efi_ipv4_address *ip,
1435 struct efi_ipv4_address *mask,
Adriano Cordova54674692025-03-03 11:13:15 -03001436 struct efi_ipv4_address *gw,
1437 struct udevice *dev)
Adriano Cordova3c951362024-12-04 00:05:21 -03001438{
Adriano Cordova54674692025-03-03 11:13:15 -03001439 if (!dev)
1440 dev = eth_get_dev();
Adriano Cordova3c951362024-12-04 00:05:21 -03001441#ifdef CONFIG_NET_LWIP
1442 char ipstr[] = "ipaddr\0\0";
1443 char maskstr[] = "netmask\0\0";
1444 char gwstr[] = "gatewayip\0\0";
1445 int idx;
1446 struct in_addr tmp;
1447 char *env;
1448
Adriano Cordova54674692025-03-03 11:13:15 -03001449 idx = dev_seq(dev);
Adriano Cordova3c951362024-12-04 00:05:21 -03001450
1451 if (idx < 0 || idx > 99) {
1452 log_err("unexpected idx %d\n", idx);
1453 return;
1454 }
1455
1456 if (idx) {
1457 sprintf(ipstr, "ipaddr%d", idx);
1458 sprintf(maskstr, "netmask%d", idx);
1459 sprintf(gwstr, "gatewayip%d", idx);
1460 }
1461
1462 env = env_get(ipstr);
1463 if (env && ip) {
1464 tmp = string_to_ip(env);
1465 memcpy(ip, &tmp, sizeof(tmp));
1466 }
1467
1468 env = env_get(maskstr);
1469 if (env && mask) {
1470 tmp = string_to_ip(env);
1471 memcpy(mask, &tmp, sizeof(tmp));
1472 }
1473 env = env_get(gwstr);
1474 if (env && gw) {
1475 tmp = string_to_ip(env);
1476 memcpy(gw, &tmp, sizeof(tmp));
1477 }
1478#else
1479 if (ip)
1480 memcpy(ip, &net_ip, sizeof(net_ip));
1481 if (mask)
1482 memcpy(mask, &net_netmask, sizeof(net_netmask));
1483#endif
1484}
1485
1486/**
1487 * efi_net_set_addr() - set IP address information
1488 *
1489 * Set the current IP address, mask, and gateway to the
1490 * efi_ipv4_address structs pointed to by ip, mask and gw,
1491 * respectively.
1492 *
1493 * @ip: pointer to new IP address
1494 * @mask: pointer to new network mask to set
1495 * @gw: pointer to new network gateway
Adriano Cordova54674692025-03-03 11:13:15 -03001496 * @dev: udevice
Adriano Cordova3c951362024-12-04 00:05:21 -03001497 */
1498void efi_net_set_addr(struct efi_ipv4_address *ip,
1499 struct efi_ipv4_address *mask,
Adriano Cordova54674692025-03-03 11:13:15 -03001500 struct efi_ipv4_address *gw,
1501 struct udevice *dev)
Adriano Cordova3c951362024-12-04 00:05:21 -03001502{
Adriano Cordova54674692025-03-03 11:13:15 -03001503 if (!dev)
1504 dev = eth_get_dev();
Adriano Cordova3c951362024-12-04 00:05:21 -03001505#ifdef CONFIG_NET_LWIP
1506 char ipstr[] = "ipaddr\0\0";
1507 char maskstr[] = "netmask\0\0";
1508 char gwstr[] = "gatewayip\0\0";
1509 int idx;
1510 struct in_addr *addr;
1511 char tmp[46];
1512
Adriano Cordova54674692025-03-03 11:13:15 -03001513 idx = dev_seq(dev);
Adriano Cordova3c951362024-12-04 00:05:21 -03001514
1515 if (idx < 0 || idx > 99) {
1516 log_err("unexpected idx %d\n", idx);
1517 return;
1518 }
1519
1520 if (idx) {
1521 sprintf(ipstr, "ipaddr%d", idx);
1522 sprintf(maskstr, "netmask%d", idx);
1523 sprintf(gwstr, "gatewayip%d", idx);
1524 }
1525
1526 if (ip) {
1527 addr = (struct in_addr *)ip;
1528 ip_to_string(*addr, tmp);
1529 env_set(ipstr, tmp);
1530 }
1531
1532 if (mask) {
1533 addr = (struct in_addr *)mask;
1534 ip_to_string(*addr, tmp);
1535 env_set(maskstr, tmp);
1536 }
1537
1538 if (gw) {
1539 addr = (struct in_addr *)gw;
1540 ip_to_string(*addr, tmp);
1541 env_set(gwstr, tmp);
1542 }
1543#else
1544 if (ip)
1545 memcpy(&net_ip, ip, sizeof(*ip));
1546 if (mask)
1547 memcpy(&net_netmask, mask, sizeof(*mask));
1548#endif
1549}
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001550
Adriano Cordova54674692025-03-03 11:13:15 -03001551#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001552/**
1553 * efi_net_set_buffer() - allocate a buffer of min 64K
1554 *
1555 * @buffer: allocated buffer
1556 * @size: desired buffer size
1557 * Return: status code
1558 */
1559static efi_status_t efi_net_set_buffer(void **buffer, size_t size)
1560{
1561 efi_status_t ret = EFI_SUCCESS;
1562
1563 if (size < SZ_64K)
1564 size = SZ_64K;
1565
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001566 *buffer = efi_alloc(size);
1567 if (!*buffer)
1568 ret = EFI_OUT_OF_RESOURCES;
1569
1570 efi_wget_info.buffer_size = (ulong)size;
1571
1572 return ret;
1573}
1574
1575/**
1576 * efi_net_parse_headers() - parse HTTP headers
1577 *
1578 * Parses the raw buffer efi_wget_info.headers into an array headers
1579 * of efi structs http_headers. The array should be at least
1580 * MAX_HTTP_HEADERS long.
1581 *
1582 * @num_headers: number of headers
1583 * @headers: caller provided array of struct http_headers
1584 */
1585void efi_net_parse_headers(ulong *num_headers, struct http_header *headers)
1586{
1587 if (!num_headers || !headers)
1588 return;
1589
1590 // Populate info with http headers.
1591 *num_headers = 0;
1592 const uchar *line_start = efi_wget_info.headers;
1593 const uchar *line_end;
1594 ulong count;
1595 struct http_header *current_header;
1596 const uchar *separator;
1597 size_t name_length, value_length;
1598
1599 // Skip the first line (request or status line)
1600 line_end = strstr(line_start, "\r\n");
1601
1602 if (line_end)
1603 line_start = line_end + 2;
1604
1605 while ((line_end = strstr(line_start, "\r\n")) != NULL) {
1606 count = *num_headers;
1607 if (line_start == line_end || count >= MAX_HTTP_HEADERS)
1608 break;
1609 current_header = headers + count;
1610 separator = strchr(line_start, ':');
1611 if (separator) {
1612 name_length = separator - line_start;
1613 ++separator;
1614 while (*separator == ' ')
1615 ++separator;
1616 value_length = line_end - separator;
1617 if (name_length < MAX_HTTP_HEADER_NAME &&
1618 value_length < MAX_HTTP_HEADER_VALUE) {
1619 strncpy(current_header->name, line_start, name_length);
1620 current_header->name[name_length] = '\0';
1621 strncpy(current_header->value, separator, value_length);
1622 current_header->value[value_length] = '\0';
1623 (*num_headers)++;
1624 }
1625 }
1626 line_start = line_end + 2;
1627 }
1628}
1629
1630/**
1631 * efi_net_do_request() - issue an HTTP request using wget
1632 *
1633 * @url: url
1634 * @method: HTTP method
1635 * @buffer: data buffer
1636 * @status_code: HTTP status code
1637 * @file_size: file size in bytes
1638 * @headers_buffer: headers buffer
Adriano Cordova28d67772025-03-03 11:13:17 -03001639 * @parent: service binding protocol
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001640 * Return: status code
1641 */
1642efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, void **buffer,
Adriano Cordova28d67772025-03-03 11:13:17 -03001643 u32 *status_code, ulong *file_size, char *headers_buffer,
1644 struct efi_service_binding_protocol *parent)
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001645{
1646 efi_status_t ret = EFI_SUCCESS;
1647 int wget_ret;
1648 static bool last_head;
Adriano Cordova28d67772025-03-03 11:13:17 -03001649 struct udevice *dev;
1650 int i;
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001651
Adriano Cordova28d67772025-03-03 11:13:17 -03001652 if (!buffer || !file_size || !parent)
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001653 return EFI_ABORTED;
1654
1655 efi_wget_info.method = (enum wget_http_method)method;
1656 efi_wget_info.headers = headers_buffer;
1657
Adriano Cordova28d67772025-03-03 11:13:17 -03001658 // Set corresponding udevice
1659 dev = NULL;
1660 for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
1661 if (net_objs[i] && &net_objs[i]->http_service_binding == parent)
1662 dev = net_objs[i]->dev;
1663 }
1664 if (!dev)
1665 return EFI_ABORTED;
1666
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001667 switch (method) {
1668 case HTTP_METHOD_GET:
1669 ret = efi_net_set_buffer(buffer, last_head ? (size_t)efi_wget_info.hdr_cont_len : 0);
1670 if (ret != EFI_SUCCESS)
1671 goto out;
Adriano Cordova28d67772025-03-03 11:13:17 -03001672 eth_set_dev(dev);
Adriano Cordova54674692025-03-03 11:13:15 -03001673 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001674 wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info);
1675 if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) {
1676 // Try again with updated buffer size
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001677 efi_free_pool(*buffer);
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001678 ret = efi_net_set_buffer(buffer, (size_t)efi_wget_info.hdr_cont_len);
1679 if (ret != EFI_SUCCESS)
1680 goto out;
Adriano Cordova28d67772025-03-03 11:13:17 -03001681 eth_set_dev(dev);
Adriano Cordova54674692025-03-03 11:13:15 -03001682 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001683 if (wget_request((ulong)*buffer, url, &efi_wget_info)) {
1684 efi_free_pool(*buffer);
1685 ret = EFI_DEVICE_ERROR;
1686 goto out;
1687 }
1688 } else if (wget_ret) {
1689 efi_free_pool(*buffer);
1690 ret = EFI_DEVICE_ERROR;
1691 goto out;
1692 }
1693 // Pass the actual number of received bytes to the application
1694 *file_size = efi_wget_info.file_size;
1695 *status_code = efi_wget_info.status_code;
1696 last_head = false;
1697 break;
1698 case HTTP_METHOD_HEAD:
1699 ret = efi_net_set_buffer(buffer, 0);
1700 if (ret != EFI_SUCCESS)
1701 goto out;
Adriano Cordova28d67772025-03-03 11:13:17 -03001702 eth_set_dev(dev);
Adriano Cordova54674692025-03-03 11:13:15 -03001703 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001704 wget_request((ulong)*buffer, url, &efi_wget_info);
1705 *file_size = 0;
1706 *status_code = efi_wget_info.status_code;
1707 last_head = true;
1708 break;
1709 default:
1710 ret = EFI_UNSUPPORTED;
1711 break;
1712 }
1713
1714out:
1715 return ret;
1716}
Adriano Cordova54674692025-03-03 11:13:15 -03001717#endif