blob: f65287ad6ab91fb3112bb880a180247495135a84 [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 Cordova54674692025-03-03 11:13:15 -030027#define MAX_NUM_DP_ENTRIES 10
28
Adriano Cordova7f2bcd42025-03-03 11:13:11 -030029const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020030static const efi_guid_t efi_pxe_base_code_protocol_guid =
31 EFI_PXE_BASE_CODE_PROTOCOL_GUID;
Alexander Graf94c4b992016-05-06 21:01:01 +020032static struct efi_pxe_packet *dhcp_ack;
Alexander Graf94c4b992016-05-06 21:01:01 +020033static void *new_tx_packet;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010034static void *transmit_buffer;
Patrick Wildtfab89102020-10-07 11:04:33 +020035static uchar **receive_buffer;
36static size_t *receive_lengths;
37static int rx_packet_idx;
38static int rx_packet_num;
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +010039static struct efi_net_obj *netobj;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010040
Adriano Cordova54674692025-03-03 11:13:15 -030041struct dp_entry {
42 struct efi_device_path *net_dp;
43 struct udevice *dev;
44 bool is_valid;
45};
46
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020047/*
Adriano Cordova54674692025-03-03 11:13:15 -030048 * The network device path cache. An entry is added when a new bootfile
49 * is downloaded from the network. If the bootfile is then loaded as an
50 * efi image, the most recent entry corresponding to the device is passed
51 * as the device path of the loaded image.
Adriano Cordova93cba0f2024-12-04 00:05:23 -030052 */
Adriano Cordova54674692025-03-03 11:13:15 -030053static struct dp_entry dp_cache[MAX_NUM_DP_ENTRIES];
54static int next_dp_entry;
Adriano Cordova93cba0f2024-12-04 00:05:23 -030055
Adriano Cordova54674692025-03-03 11:13:15 -030056#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
Adriano Cordova0d1f5092024-12-04 00:05:24 -030057static struct wget_http_info efi_wget_info = {
58 .set_bootdev = false,
59 .check_buffer_size = true,
60
61};
Adriano Cordova54674692025-03-03 11:13:15 -030062#endif
Adriano Cordova0d1f5092024-12-04 00:05:24 -030063
Adriano Cordova93cba0f2024-12-04 00:05:23 -030064/*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020065 * The notification function of this event is called in every timer cycle
66 * to check if a new network packet has been received.
67 */
68static struct efi_event *network_timer_event;
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +020069/*
70 * This event is signaled when a packet has been received.
71 */
72static struct efi_event *wait_for_packet;
Alexander Graf94c4b992016-05-06 21:01:01 +020073
Heinrich Schuchardt72928722018-09-26 05:27:56 +020074/**
75 * struct efi_net_obj - EFI object representing a network interface
76 *
Adriano Cordova9debc902024-12-04 00:05:25 -030077 * @header: EFI object header
Adriano Cordova54674692025-03-03 11:13:15 -030078 * @dev: net udevice
Adriano Cordova9debc902024-12-04 00:05:25 -030079 * @net: simple network protocol interface
80 * @net_mode: status of the network interface
81 * @pxe: PXE base code protocol interface
82 * @pxe_mode: status of the PXE base code protocol
83 * @ip4_config2: IP4 Config2 protocol interface
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030084 * @http_service_binding: Http service binding protocol interface
Heinrich Schuchardt72928722018-09-26 05:27:56 +020085 */
Alexander Graf94c4b992016-05-06 21:01:01 +020086struct efi_net_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020087 struct efi_object header;
Adriano Cordova54674692025-03-03 11:13:15 -030088 struct udevice *dev;
Alexander Graf94c4b992016-05-06 21:01:01 +020089 struct efi_simple_network net;
90 struct efi_simple_network_mode net_mode;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020091 struct efi_pxe_base_code_protocol pxe;
Alexander Graf94c4b992016-05-06 21:01:01 +020092 struct efi_pxe_mode pxe_mode;
Adriano Cordova9debc902024-12-04 00:05:25 -030093#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
94 struct efi_ip4_config2_protocol ip4_config2;
95#endif
Adriano Cordovae9b19eb2024-12-04 00:05:26 -030096#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
97 struct efi_service_binding_protocol http_service_binding;
98#endif
Alexander Graf94c4b992016-05-06 21:01:01 +020099};
100
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100101/*
Adriano Cordova54674692025-03-03 11:13:15 -0300102 * efi_netobj_is_active() - checks if a netobj is active in the efi subsystem
103 *
104 * @netobj: pointer to efi_net_obj
105 * Return: true if active
106 */
107static bool efi_netobj_is_active(struct efi_net_obj *netobj)
108{
109 if (!netobj || !efi_search_obj(&netobj->header))
110 return false;
111
112 return true;
113}
114
115
116/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100117 * efi_net_start() - start the network interface
118 *
119 * This function implements the Start service of the
120 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
121 * (UEFI) specification for details.
122 *
123 * @this: pointer to the protocol instance
124 * Return: status code
125 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200126static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
127{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100128 efi_status_t ret = EFI_SUCCESS;
129
Alexander Graf94c4b992016-05-06 21:01:01 +0200130 EFI_ENTRY("%p", this);
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100131 /* Check parameters */
132 if (!this) {
133 ret = EFI_INVALID_PARAMETER;
134 goto out;
135 }
136
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200137 if (this->mode->state != EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100138 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200139 } else {
140 this->int_status = 0;
141 wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100142 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200143 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100144out:
145 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200146}
147
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100148/*
149 * efi_net_stop() - stop the network interface
150 *
151 * This function implements the Stop service of the
152 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
153 * (UEFI) specification for details.
154 *
155 * @this: pointer to the protocol instance
156 * Return: status code
157 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200158static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
159{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100160 efi_status_t ret = EFI_SUCCESS;
161
Alexander Graf94c4b992016-05-06 21:01:01 +0200162 EFI_ENTRY("%p", this);
163
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100164 /* Check parameters */
165 if (!this) {
166 ret = EFI_INVALID_PARAMETER;
167 goto out;
168 }
169
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200170 if (this->mode->state == EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100171 ret = EFI_NOT_STARTED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200172 } else {
173 /* Disable hardware and put it into the reset state */
Adriano Cordova54674692025-03-03 11:13:15 -0300174 eth_set_dev(netobj->dev);
175 env_set("ethact", eth_get_name());
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200176 eth_halt();
Patrick Wildtfab89102020-10-07 11:04:33 +0200177 /* Clear cache of packets */
178 rx_packet_num = 0;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100179 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200180 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100181out:
182 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200183}
184
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200185/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100186 * efi_net_initialize() - initialize the network interface
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200187 *
188 * This function implements the Initialize service of the
189 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
190 * (UEFI) specification for details.
191 *
192 * @this: pointer to the protocol instance
193 * @extra_rx: extra receive buffer to be allocated
194 * @extra_tx: extra transmit buffer to be allocated
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100195 * Return: status code
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200196 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200197static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
198 ulong extra_rx, ulong extra_tx)
199{
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200200 int ret;
201 efi_status_t r = EFI_SUCCESS;
202
Alexander Graf94c4b992016-05-06 21:01:01 +0200203 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
204
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100205 /* Check parameters */
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200206 if (!this) {
207 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100208 goto out;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200209 }
210
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200211 switch (this->mode->state) {
212 case EFI_NETWORK_INITIALIZED:
213 case EFI_NETWORK_STARTED:
214 break;
215 default:
216 r = EFI_NOT_STARTED;
217 goto out;
218 }
219
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200220 /* Setup packet buffers */
221 net_init();
Patrick Wildtfab89102020-10-07 11:04:33 +0200222 /* Clear cache of packets */
223 rx_packet_num = 0;
Adriano Cordova54674692025-03-03 11:13:15 -0300224 /* Set the net device corresponding to the efi net object */
225 eth_set_dev(netobj->dev);
226 env_set("ethact", eth_get_name());
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200227 /* Get hardware ready for send and receive operations */
Adriano Cordova54674692025-03-03 11:13:15 -0300228 ret = eth_start_udev(netobj->dev);
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200229 if (ret < 0) {
230 eth_halt();
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100231 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200232 r = EFI_DEVICE_ERROR;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100233 goto out;
234 } else {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200235 this->int_status = 0;
236 wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100237 this->mode->state = EFI_NETWORK_INITIALIZED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200238 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100239out:
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200240 return EFI_EXIT(r);
Alexander Graf94c4b992016-05-06 21:01:01 +0200241}
242
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100243/*
244 * efi_net_reset() - reinitialize the network interface
245 *
246 * This function implements the Reset service of the
247 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
248 * (UEFI) specification for details.
249 *
250 * @this: pointer to the protocol instance
251 * @extended_verification: execute exhaustive verification
252 * Return: status code
253 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200254static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
255 int extended_verification)
256{
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200257 efi_status_t ret;
258
Alexander Graf94c4b992016-05-06 21:01:01 +0200259 EFI_ENTRY("%p, %x", this, extended_verification);
260
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200261 /* Check parameters */
262 if (!this) {
263 ret = EFI_INVALID_PARAMETER;
264 goto out;
265 }
266
267 switch (this->mode->state) {
268 case EFI_NETWORK_INITIALIZED:
269 break;
270 case EFI_NETWORK_STOPPED:
271 ret = EFI_NOT_STARTED;
272 goto out;
273 default:
274 ret = EFI_DEVICE_ERROR;
275 goto out;
276 }
277
278 this->mode->state = EFI_NETWORK_STARTED;
279 ret = EFI_CALL(efi_net_initialize(this, 0, 0));
280out:
281 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200282}
283
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100284/*
285 * efi_net_shutdown() - shut down the network interface
286 *
287 * This function implements the Shutdown 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 * Return: status code
293 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200294static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
295{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100296 efi_status_t ret = EFI_SUCCESS;
297
Alexander Graf94c4b992016-05-06 21:01:01 +0200298 EFI_ENTRY("%p", this);
299
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100300 /* Check parameters */
301 if (!this) {
302 ret = EFI_INVALID_PARAMETER;
303 goto out;
304 }
305
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200306 switch (this->mode->state) {
307 case EFI_NETWORK_INITIALIZED:
308 break;
309 case EFI_NETWORK_STOPPED:
310 ret = EFI_NOT_STARTED;
311 goto out;
312 default:
313 ret = EFI_DEVICE_ERROR;
314 goto out;
315 }
316
Adriano Cordova54674692025-03-03 11:13:15 -0300317 eth_set_dev(netobj->dev);
318 env_set("ethact", eth_get_name());
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100319 eth_halt();
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200320 this->int_status = 0;
321 wait_for_packet->is_signaled = false;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +0200322 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100323
324out:
325 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200326}
327
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100328/*
329 * efi_net_receive_filters() - mange multicast receive filters
330 *
331 * This function implements the ReceiveFilters service of the
332 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
333 * (UEFI) specification for details.
334 *
335 * @this: pointer to the protocol instance
336 * @enable: bit mask of receive filters to enable
337 * @disable: bit mask of receive filters to disable
338 * @reset_mcast_filter: true resets contents of the filters
339 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
340 * @mcast_filter: list of new filters
341 * Return: status code
342 */
343static efi_status_t EFIAPI efi_net_receive_filters
344 (struct efi_simple_network *this, u32 enable, u32 disable,
345 int reset_mcast_filter, ulong mcast_filter_count,
346 struct efi_mac_address *mcast_filter)
Alexander Graf94c4b992016-05-06 21:01:01 +0200347{
348 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
349 reset_mcast_filter, mcast_filter_count, mcast_filter);
350
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200351 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200352}
353
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100354/*
355 * efi_net_station_address() - set the hardware MAC address
356 *
357 * This function implements the StationAddress service of the
358 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
359 * (UEFI) specification for details.
360 *
361 * @this: pointer to the protocol instance
362 * @reset: if true reset the address to default
363 * @new_mac: new MAC address
364 * Return: status code
365 */
366static efi_status_t EFIAPI efi_net_station_address
367 (struct efi_simple_network *this, int reset,
368 struct efi_mac_address *new_mac)
Alexander Graf94c4b992016-05-06 21:01:01 +0200369{
370 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
371
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200372 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200373}
374
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100375/*
376 * efi_net_statistics() - reset or collect statistics of the network interface
377 *
378 * This function implements the Statistics service of the
379 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
380 * (UEFI) specification for details.
381 *
382 * @this: pointer to the protocol instance
383 * @reset: if true, the statistics are reset
384 * @stat_size: size of the statistics table
385 * @stat_table: table to receive the statistics
386 * Return: status code
387 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200388static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
389 int reset, ulong *stat_size,
390 void *stat_table)
391{
392 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
393
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200394 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200395}
396
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100397/*
398 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
399 *
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200400 * This function implements the MCastIPtoMAC service of the
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100401 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
402 * (UEFI) specification for details.
403 *
404 * @this: pointer to the protocol instance
405 * @ipv6: true if the IP address is an IPv6 address
406 * @ip: IP address
407 * @mac: MAC address
408 * Return: status code
409 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200410static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
411 int ipv6,
412 struct efi_ip_address *ip,
413 struct efi_mac_address *mac)
414{
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200415 efi_status_t ret = EFI_SUCCESS;
416
Alexander Graf94c4b992016-05-06 21:01:01 +0200417 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
418
Heinrich Schuchardt33b318d2019-09-01 17:17:53 +0200419 if (!this || !ip || !mac) {
420 ret = EFI_INVALID_PARAMETER;
421 goto out;
422 }
423
424 if (ipv6) {
425 ret = EFI_UNSUPPORTED;
426 goto out;
427 }
428
429 /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */
430 if ((ip->ip_addr[0] & 0xf0) != 0xe0) {
431 ret = EFI_INVALID_PARAMETER;
432 goto out;
433 };
434
435 switch (this->mode->state) {
436 case EFI_NETWORK_INITIALIZED:
437 case EFI_NETWORK_STARTED:
438 break;
439 default:
440 ret = EFI_NOT_STARTED;
441 goto out;
442 }
443
444 memset(mac, 0, sizeof(struct efi_mac_address));
445
446 /*
447 * Copy lower 23 bits of IPv4 multi-cast address
448 * RFC 1112, RFC 7042 2.1.1.
449 */
450 mac->mac_addr[0] = 0x01;
451 mac->mac_addr[1] = 0x00;
452 mac->mac_addr[2] = 0x5E;
453 mac->mac_addr[3] = ip->ip_addr[1] & 0x7F;
454 mac->mac_addr[4] = ip->ip_addr[2];
455 mac->mac_addr[5] = ip->ip_addr[3];
456out:
457 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200458}
459
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100460/**
461 * efi_net_nvdata() - read or write NVRAM
462 *
463 * This function implements the GetStatus service of the Simple Network
464 * Protocol. See the UEFI spec for details.
465 *
466 * @this: the instance of the Simple Network Protocol
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200467 * @read_write: true for read, false for write
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100468 * @offset: offset in NVRAM
469 * @buffer_size: size of buffer
470 * @buffer: buffer
471 * Return: status code
472 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200473static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
474 int read_write, ulong offset,
475 ulong buffer_size, char *buffer)
476{
477 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
478 buffer);
479
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200480 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200481}
482
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100483/**
484 * efi_net_get_status() - get interrupt status
485 *
486 * This function implements the GetStatus service of the Simple Network
487 * Protocol. See the UEFI spec for details.
488 *
489 * @this: the instance of the Simple Network Protocol
490 * @int_status: interface status
491 * @txbuf: transmission buffer
492 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200493static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
494 u32 *int_status, void **txbuf)
495{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100496 efi_status_t ret = EFI_SUCCESS;
497
Alexander Graf94c4b992016-05-06 21:01:01 +0200498 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
499
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200500 efi_timer_check();
501
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100502 /* Check parameters */
503 if (!this) {
504 ret = EFI_INVALID_PARAMETER;
505 goto out;
506 }
507
508 switch (this->mode->state) {
509 case EFI_NETWORK_STOPPED:
510 ret = EFI_NOT_STARTED;
511 goto out;
512 case EFI_NETWORK_STARTED:
513 ret = EFI_DEVICE_ERROR;
514 goto out;
515 default:
516 break;
517 }
518
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200519 if (int_status) {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200520 *int_status = this->int_status;
521 this->int_status = 0;
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200522 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200523 if (txbuf)
524 *txbuf = new_tx_packet;
525
526 new_tx_packet = NULL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100527out:
528 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200529}
530
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100531/**
532 * efi_net_transmit() - transmit a packet
533 *
534 * This function implements the Transmit service of the Simple Network Protocol.
535 * See the UEFI spec for details.
536 *
537 * @this: the instance of the Simple Network Protocol
538 * @header_size: size of the media header
539 * @buffer_size: size of the buffer to receive the packet
540 * @buffer: buffer to receive the packet
541 * @src_addr: source hardware MAC address
542 * @dest_addr: destination hardware MAC address
543 * @protocol: type of header to build
544 * Return: status code
545 */
546static efi_status_t EFIAPI efi_net_transmit
547 (struct efi_simple_network *this, size_t header_size,
548 size_t buffer_size, void *buffer,
549 struct efi_mac_address *src_addr,
550 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200551{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100552 efi_status_t ret = EFI_SUCCESS;
553
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200554 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
555 (unsigned long)header_size, (unsigned long)buffer_size,
556 buffer, src_addr, dest_addr, protocol);
Alexander Graf94c4b992016-05-06 21:01:01 +0200557
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200558 efi_timer_check();
559
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100560 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200561 if (!this || !buffer) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100562 ret = EFI_INVALID_PARAMETER;
563 goto out;
564 }
565
566 /* We do not support jumbo packets */
567 if (buffer_size > PKTSIZE_ALIGN) {
568 ret = EFI_INVALID_PARAMETER;
569 goto out;
570 }
571
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200572 /* At least the IP header has to fit into the buffer */
573 if (buffer_size < this->mode->media_header_size) {
574 ret = EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100575 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200576 }
577
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200578 /*
579 * TODO:
580 * Support VLANs. Use net_set_ether() for copying the header. Use a
581 * U_BOOT_ENV_CALLBACK to update the media header size.
582 */
583 if (header_size) {
584 struct ethernet_hdr *header = buffer;
585
586 if (!dest_addr || !protocol ||
587 header_size != this->mode->media_header_size) {
588 ret = EFI_INVALID_PARAMETER;
589 goto out;
590 }
591 if (!src_addr)
592 src_addr = &this->mode->current_address;
593
594 memcpy(header->et_dest, dest_addr, ARP_HLEN);
595 memcpy(header->et_src, src_addr, ARP_HLEN);
596 header->et_protlen = htons(*protocol);
597 }
598
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100599 switch (this->mode->state) {
600 case EFI_NETWORK_STOPPED:
601 ret = EFI_NOT_STARTED;
602 goto out;
603 case EFI_NETWORK_STARTED:
604 ret = EFI_DEVICE_ERROR;
605 goto out;
606 default:
607 break;
608 }
609
Adriano Cordova54674692025-03-03 11:13:15 -0300610 eth_set_dev(netobj->dev);
611 env_set("ethact", eth_get_name());
612
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100613 /* Ethernet packets always fit, just bounce */
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100614 memcpy(transmit_buffer, buffer, buffer_size);
615 net_send_packet(transmit_buffer, buffer_size);
Alexander Graf0b12b862016-09-06 14:26:27 +0200616
Alexander Graf94c4b992016-05-06 21:01:01 +0200617 new_tx_packet = buffer;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200618 this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100619out:
620 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200621}
622
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100623/**
624 * efi_net_receive() - receive a packet from a network interface
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200625 *
626 * This function implements the Receive service of the Simple Network Protocol.
627 * See the UEFI spec for details.
628 *
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100629 * @this: the instance of the Simple Network Protocol
630 * @header_size: size of the media header
631 * @buffer_size: size of the buffer to receive the packet
632 * @buffer: buffer to receive the packet
633 * @src_addr: source MAC address
634 * @dest_addr: destination MAC address
635 * @protocol: protocol
636 * Return: status code
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200637 */
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100638static efi_status_t EFIAPI efi_net_receive
639 (struct efi_simple_network *this, size_t *header_size,
640 size_t *buffer_size, void *buffer,
641 struct efi_mac_address *src_addr,
642 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200643{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100644 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200645 struct ethernet_hdr *eth_hdr;
646 size_t hdr_size = sizeof(struct ethernet_hdr);
647 u16 protlen;
648
Alexander Graf94c4b992016-05-06 21:01:01 +0200649 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
650 buffer_size, buffer, src_addr, dest_addr, protocol);
651
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100652 /* Execute events */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200653 efi_timer_check();
Alexander Graf94c4b992016-05-06 21:01:01 +0200654
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100655 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200656 if (!this || !buffer || !buffer_size) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100657 ret = EFI_INVALID_PARAMETER;
658 goto out;
659 }
660
661 switch (this->mode->state) {
662 case EFI_NETWORK_STOPPED:
663 ret = EFI_NOT_STARTED;
664 goto out;
665 case EFI_NETWORK_STARTED:
666 ret = EFI_DEVICE_ERROR;
667 goto out;
668 default:
669 break;
670 }
671
Patrick Wildtfab89102020-10-07 11:04:33 +0200672 if (!rx_packet_num) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100673 ret = EFI_NOT_READY;
674 goto out;
675 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200676 /* Fill export parameters */
Patrick Wildtfab89102020-10-07 11:04:33 +0200677 eth_hdr = (struct ethernet_hdr *)receive_buffer[rx_packet_idx];
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200678 protlen = ntohs(eth_hdr->et_protlen);
679 if (protlen == 0x8100) {
680 hdr_size += 4;
Patrick Wildtfab89102020-10-07 11:04:33 +0200681 protlen = ntohs(*(u16 *)&receive_buffer[rx_packet_idx][hdr_size - 2]);
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200682 }
683 if (header_size)
684 *header_size = hdr_size;
685 if (dest_addr)
686 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
687 if (src_addr)
688 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
689 if (protocol)
690 *protocol = protlen;
Patrick Wildtfab89102020-10-07 11:04:33 +0200691 if (*buffer_size < receive_lengths[rx_packet_idx]) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200692 /* Packet doesn't fit, try again with bigger buffer */
Patrick Wildtfab89102020-10-07 11:04:33 +0200693 *buffer_size = receive_lengths[rx_packet_idx];
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100694 ret = EFI_BUFFER_TOO_SMALL;
695 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200696 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200697 /* Copy packet */
Patrick Wildtfab89102020-10-07 11:04:33 +0200698 memcpy(buffer, receive_buffer[rx_packet_idx],
699 receive_lengths[rx_packet_idx]);
700 *buffer_size = receive_lengths[rx_packet_idx];
701 rx_packet_idx = (rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV;
702 rx_packet_num--;
703 if (rx_packet_num)
704 wait_for_packet->is_signaled = true;
705 else
706 this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100707out:
708 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200709}
710
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100711/**
712 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
713 *
714 * This function is called by dhcp_handler().
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200715 *
716 * @pkt: packet received by dhcp_handler()
717 * @len: length of the packet received
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100718 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200719void efi_net_set_dhcp_ack(void *pkt, int len)
720{
721 int maxsize = sizeof(*dhcp_ack);
722
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100723 if (!dhcp_ack) {
Alexander Graf94c4b992016-05-06 21:01:01 +0200724 dhcp_ack = malloc(maxsize);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100725 if (!dhcp_ack)
726 return;
727 }
728 memset(dhcp_ack, 0, maxsize);
Alexander Graf94c4b992016-05-06 21:01:01 +0200729 memcpy(dhcp_ack, pkt, min(len, maxsize));
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +0100730
731 if (netobj)
732 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
Alexander Graf94c4b992016-05-06 21:01:01 +0200733}
734
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100735/**
736 * efi_net_push() - callback for received network packet
737 *
738 * This function is called when a network packet is received by eth_rx().
739 *
740 * @pkt: network packet
741 * @len: length
742 */
743static void efi_net_push(void *pkt, int len)
744{
Patrick Wildtfab89102020-10-07 11:04:33 +0200745 int rx_packet_next;
746
747 /* Check that we at least received an Ethernet header */
748 if (len < sizeof(struct ethernet_hdr))
749 return;
750
751 /* Check that the buffer won't overflow */
752 if (len > PKTSIZE_ALIGN)
753 return;
754
755 /* Can't store more than pre-alloced buffer */
756 if (rx_packet_num >= ETH_PACKETS_BATCH_RECV)
757 return;
758
759 rx_packet_next = (rx_packet_idx + rx_packet_num) %
760 ETH_PACKETS_BATCH_RECV;
761 memcpy(receive_buffer[rx_packet_next], pkt, len);
762 receive_lengths[rx_packet_next] = len;
763
764 rx_packet_num++;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100765}
766
767/**
768 * efi_network_timer_notify() - check if a new network packet has been received
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200769 *
770 * This notification function is called in every timer cycle.
771 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200772 * @event: the event for which this notification function is registered
773 * @context: event context - not used in this function
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200774 */
775static void EFIAPI efi_network_timer_notify(struct efi_event *event,
776 void *context)
777{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100778 struct efi_simple_network *this = (struct efi_simple_network *)context;
779
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200780 EFI_ENTRY("%p, %p", event, context);
781
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100782 /*
783 * Some network drivers do not support calling eth_rx() before
784 * initialization.
785 */
786 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
787 goto out;
788
Patrick Wildtfab89102020-10-07 11:04:33 +0200789 if (!rx_packet_num) {
Adriano Cordova54674692025-03-03 11:13:15 -0300790 eth_set_dev(netobj->dev);
791 env_set("ethact", eth_get_name());
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200792 push_packet = efi_net_push;
793 eth_rx();
794 push_packet = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +0200795 if (rx_packet_num) {
796 this->int_status |=
797 EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
798 wait_for_packet->is_signaled = true;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200799 }
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200800 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100801out:
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200802 EFI_EXIT(EFI_SUCCESS);
803}
804
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200805static efi_status_t EFIAPI efi_pxe_base_code_start(
806 struct efi_pxe_base_code_protocol *this,
807 u8 use_ipv6)
808{
809 return EFI_UNSUPPORTED;
810}
811
812static efi_status_t EFIAPI efi_pxe_base_code_stop(
813 struct efi_pxe_base_code_protocol *this)
814{
815 return EFI_UNSUPPORTED;
816}
817
818static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
819 struct efi_pxe_base_code_protocol *this,
820 u8 sort_offers)
821{
822 return EFI_UNSUPPORTED;
823}
824
825static efi_status_t EFIAPI efi_pxe_base_code_discover(
826 struct efi_pxe_base_code_protocol *this,
827 u16 type, u16 *layer, u8 bis,
828 struct efi_pxe_base_code_discover_info *info)
829{
830 return EFI_UNSUPPORTED;
831}
832
833static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
834 struct efi_pxe_base_code_protocol *this,
835 u32 operation, void *buffer_ptr,
836 u8 overwrite, efi_uintn_t *buffer_size,
837 struct efi_ip_address server_ip, char *filename,
838 struct efi_pxe_base_code_mtftp_info *info,
839 u8 dont_use_buffer)
840{
841 return EFI_UNSUPPORTED;
842}
843
844static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
845 struct efi_pxe_base_code_protocol *this,
846 u16 op_flags, struct efi_ip_address *dest_ip,
847 u16 *dest_port,
848 struct efi_ip_address *gateway_ip,
849 struct efi_ip_address *src_ip, u16 *src_port,
850 efi_uintn_t *header_size, void *header_ptr,
851 efi_uintn_t *buffer_size, void *buffer_ptr)
852{
853 return EFI_UNSUPPORTED;
854}
855
856static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
857 struct efi_pxe_base_code_protocol *this,
858 u16 op_flags, struct efi_ip_address *dest_ip,
859 u16 *dest_port, struct efi_ip_address *src_ip,
860 u16 *src_port, efi_uintn_t *header_size,
861 void *header_ptr, efi_uintn_t *buffer_size,
862 void *buffer_ptr)
863{
864 return EFI_UNSUPPORTED;
865}
866
867static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
868 struct efi_pxe_base_code_protocol *this,
869 struct efi_pxe_base_code_filter *new_filter)
870{
871 return EFI_UNSUPPORTED;
872}
873
874static efi_status_t EFIAPI efi_pxe_base_code_arp(
875 struct efi_pxe_base_code_protocol *this,
876 struct efi_ip_address *ip_addr,
877 struct efi_mac_address *mac_addr)
878{
879 return EFI_UNSUPPORTED;
880}
881
882static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
883 struct efi_pxe_base_code_protocol *this,
884 u8 *new_auto_arp, u8 *new_send_guid,
885 u8 *new_ttl, u8 *new_tos,
886 u8 *new_make_callback)
887{
888 return EFI_UNSUPPORTED;
889}
890
891static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
892 struct efi_pxe_base_code_protocol *this,
893 struct efi_ip_address *new_station_ip,
894 struct efi_ip_address *new_subnet_mask)
895{
896 return EFI_UNSUPPORTED;
897}
898
899static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
900 struct efi_pxe_base_code_protocol *this,
901 u8 *new_dhcp_discover_valid,
902 u8 *new_dhcp_ack_received,
903 u8 *new_proxy_offer_received,
904 u8 *new_pxe_discover_valid,
905 u8 *new_pxe_reply_received,
906 u8 *new_pxe_bis_reply_received,
907 EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
908 EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
909 EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
910 EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
911 EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
912 EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
913{
914 return EFI_UNSUPPORTED;
915}
916
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100917/**
Adriano Cordova54674692025-03-03 11:13:15 -0300918 * efi_netobj_set_dp() - set device path of a netobj
919 *
920 * @netobj: pointer to efi_net_obj
921 * @dp: device path to set, allocated by caller
922 * Return: status code
923 */
924efi_status_t efi_netobj_set_dp(struct efi_net_obj *netobj, struct efi_device_path *dp)
925{
926 efi_status_t ret;
927 struct efi_handler *phandler;
928 struct efi_device_path *new_net_dp;
929
930 if (!efi_netobj_is_active(netobj))
931 return EFI_SUCCESS;
932
933 // Create a device path for the netobj
934 new_net_dp = dp;
935 if (!new_net_dp)
936 return EFI_OUT_OF_RESOURCES;
937
938 phandler = NULL;
939 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
940
941 // If the device path protocol is not yet installed, install it
942 if (!phandler)
943 goto add;
944
945 // If it is already installed, try to update it
946 ret = efi_reinstall_protocol_interface(&netobj->header, &efi_guid_device_path,
947 phandler->protocol_interface, new_net_dp);
948 if (ret != EFI_SUCCESS)
949 return ret;
950
951 return EFI_SUCCESS;
952add:
953 ret = efi_add_protocol(&netobj->header, &efi_guid_device_path,
954 new_net_dp);
955 if (ret != EFI_SUCCESS)
956 return ret;
957
958 return EFI_SUCCESS;
959}
960
961/**
962 * efi_netobj_get_dp() - get device path of a netobj
963 *
964 * @netobj: pointer to efi_net_obj
965 * Return: device path, NULL on error
966 */
967static struct efi_device_path *efi_netobj_get_dp(struct efi_net_obj *netobj)
968{
969 struct efi_handler *phandler;
970
971 if (!efi_netobj_is_active(netobj))
972 return NULL;
973
974 phandler = NULL;
975 efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
976
977 if (phandler && phandler->protocol_interface)
978 return efi_dp_dup(phandler->protocol_interface);
979
980 return NULL;
981}
982
983/**
Adriano Cordova1d70b682025-03-03 11:13:13 -0300984 * efi_net_do_start() - start the efi network stack
985 *
986 * This gets called from do_bootefi_exec() each time a payload gets executed.
987 *
Adriano Cordova54674692025-03-03 11:13:15 -0300988 * @dev: net udevice
Adriano Cordova1d70b682025-03-03 11:13:13 -0300989 * Return: status code
990 */
Adriano Cordova54674692025-03-03 11:13:15 -0300991efi_status_t efi_net_do_start(struct udevice *dev)
Adriano Cordova1d70b682025-03-03 11:13:13 -0300992{
993 efi_status_t r = EFI_SUCCESS;
Adriano Cordova54674692025-03-03 11:13:15 -0300994 struct efi_device_path *net_dp;
995
996 if (dev != netobj->dev )
997 return r;
998
999 efi_net_dp_from_dev(&net_dp, netobj->dev, true);
1000 // If no dp cache entry applies and there already
1001 // is a device path installed, continue
1002 if (!net_dp) {
1003 if (efi_netobj_get_dp(netobj))
1004 goto set_addr;
1005 else
1006 net_dp = efi_dp_from_eth(netobj->dev);
Adriano Cordova1d70b682025-03-03 11:13:13 -03001007
Adriano Cordova54674692025-03-03 11:13:15 -03001008 }
1009
1010 if (!net_dp)
1011 return EFI_OUT_OF_RESOURCES;
1012
1013 r = efi_netobj_set_dp(netobj, net_dp);
1014 if (r != EFI_SUCCESS)
1015 return r;
1016set_addr:
Adriano Cordova1d70b682025-03-03 11:13:13 -03001017#ifdef CONFIG_EFI_HTTP_PROTOCOL
1018 /*
1019 * No harm on doing the following. If the PXE handle is present, the client could
1020 * find it and try to get its IP address from it. In here the PXE handle is present
1021 * but the PXE protocol is not yet implmenented, so we add this in the meantime.
1022 */
1023 efi_net_get_addr((struct efi_ipv4_address *)&netobj->pxe_mode.station_ip,
Adriano Cordova54674692025-03-03 11:13:15 -03001024 (struct efi_ipv4_address *)&netobj->pxe_mode.subnet_mask, NULL, dev);
Adriano Cordova1d70b682025-03-03 11:13:13 -03001025#endif
1026
1027 return r;
1028}
1029
1030/**
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001031 * efi_net_register() - register the simple network protocol
1032 *
1033 * This gets called from do_bootefi_exec().
Adriano Cordova54674692025-03-03 11:13:15 -03001034 * @dev: net udevice
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001035 */
Adriano Cordova54674692025-03-03 11:13:15 -03001036efi_status_t efi_net_register(struct udevice *dev)
Alexander Graf94c4b992016-05-06 21:01:01 +02001037{
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001038 efi_status_t r;
Patrick Wildtfab89102020-10-07 11:04:33 +02001039 int i;
Alexander Graf94c4b992016-05-06 21:01:01 +02001040
Adriano Cordova54674692025-03-03 11:13:15 -03001041 if (!dev) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001042 /* No network device active, don't expose any */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001043 return EFI_SUCCESS;
Alexander Graf94c4b992016-05-06 21:01:01 +02001044 }
1045
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001046 /* We only expose the "active" network device, so one is enough */
Alexander Graf94c4b992016-05-06 21:01:01 +02001047 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001048 if (!netobj)
1049 goto out_of_resources;
1050
Adriano Cordova54674692025-03-03 11:13:15 -03001051 netobj->dev = dev;
1052
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001053 /* Allocate an aligned transmit buffer */
1054 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
1055 if (!transmit_buffer)
1056 goto out_of_resources;
1057 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001058
Patrick Wildtfab89102020-10-07 11:04:33 +02001059 /* Allocate a number of receive buffers */
1060 receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
1061 sizeof(*receive_buffer));
1062 if (!receive_buffer)
1063 goto out_of_resources;
1064 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
1065 receive_buffer[i] = malloc(PKTSIZE_ALIGN);
1066 if (!receive_buffer[i])
1067 goto out_of_resources;
1068 }
Adriano Cordova54674692025-03-03 11:13:15 -03001069
Patrick Wildtfab89102020-10-07 11:04:33 +02001070 receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
1071 sizeof(*receive_lengths));
1072 if (!receive_lengths)
1073 goto out_of_resources;
1074
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001075 /* Hook net up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001076 efi_add_handle(&netobj->header);
Alexander Graf94c4b992016-05-06 21:01:01 +02001077
1078 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001079 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001080 &netobj->net);
1081 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001082 goto failure_to_add_protocol;
Adriano Cordova1738c7d2025-01-27 09:34:45 -03001083
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +02001084 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +01001085 &netobj->pxe);
1086 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001087 goto failure_to_add_protocol;
Heinrich Schuchardtd54396a2017-10-05 16:35:57 +02001088 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf94c4b992016-05-06 21:01:01 +02001089 netobj->net.start = efi_net_start;
1090 netobj->net.stop = efi_net_stop;
1091 netobj->net.initialize = efi_net_initialize;
1092 netobj->net.reset = efi_net_reset;
1093 netobj->net.shutdown = efi_net_shutdown;
1094 netobj->net.receive_filters = efi_net_receive_filters;
1095 netobj->net.station_address = efi_net_station_address;
1096 netobj->net.statistics = efi_net_statistics;
1097 netobj->net.mcastiptomac = efi_net_mcastiptomac;
1098 netobj->net.nvdata = efi_net_nvdata;
1099 netobj->net.get_status = efi_net_get_status;
1100 netobj->net.transmit = efi_net_transmit;
1101 netobj->net.receive = efi_net_receive;
1102 netobj->net.mode = &netobj->net_mode;
Heinrich Schuchardt6a6e7d82019-09-01 15:24:47 +02001103 netobj->net_mode.state = EFI_NETWORK_STOPPED;
Adriano Cordova54674692025-03-03 11:13:15 -03001104 if (dev_get_plat(dev))
1105 memcpy(netobj->net_mode.current_address.mac_addr,
1106 ((struct eth_pdata *)dev_get_plat(dev))->enetaddr, 6);
Heinrich Schuchardtd1cc6cb2017-10-05 16:35:58 +02001107 netobj->net_mode.hwaddr_size = ARP_HLEN;
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +02001108 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
Alexander Graf94c4b992016-05-06 21:01:01 +02001109 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomas27df0a72018-06-21 16:21:01 -07001110 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf94c4b992016-05-06 21:01:01 +02001111
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +02001112 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
1113 netobj->pxe.start = efi_pxe_base_code_start;
1114 netobj->pxe.stop = efi_pxe_base_code_stop;
1115 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
1116 netobj->pxe.discover = efi_pxe_base_code_discover;
1117 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
1118 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
1119 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
1120 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
1121 netobj->pxe.arp = efi_pxe_base_code_arp;
1122 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
1123 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
1124 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf94c4b992016-05-06 21:01:01 +02001125 netobj->pxe.mode = &netobj->pxe_mode;
1126 if (dhcp_ack)
1127 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
1128
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001129 /*
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001130 * Create WaitForPacket event.
1131 */
1132 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001133 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001134 &wait_for_packet);
1135 if (r != EFI_SUCCESS) {
1136 printf("ERROR: Failed to register network event\n");
1137 return r;
1138 }
1139 netobj->net.wait_for_packet = wait_for_packet;
Adriano Cordova54674692025-03-03 11:13:15 -03001140
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +02001141 /*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001142 * Create a timer event.
1143 *
1144 * The notification function is used to check if a new network packet
1145 * has been received.
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001146 *
1147 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001148 */
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +01001149 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +01001150 efi_network_timer_notify, &netobj->net, NULL,
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001151 &network_timer_event);
1152 if (r != EFI_SUCCESS) {
1153 printf("ERROR: Failed to register network event\n");
1154 return r;
1155 }
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02001156 /* Network is time critical, create event in every timer cycle */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +02001157 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
1158 if (r != EFI_SUCCESS) {
1159 printf("ERROR: Failed to set network timer\n");
1160 return r;
1161 }
1162
Adriano Cordova9debc902024-12-04 00:05:25 -03001163#if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL)
1164 r = efi_ipconfig_register(&netobj->header, &netobj->ip4_config2);
1165 if (r != EFI_SUCCESS)
1166 goto failure_to_add_protocol;
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001167#endif
1168
1169#ifdef CONFIG_EFI_HTTP_PROTOCOL
1170 r = efi_http_register(&netobj->header, &netobj->http_service_binding);
1171 if (r != EFI_SUCCESS)
1172 goto failure_to_add_protocol;
Adriano Cordova9debc902024-12-04 00:05:25 -03001173#endif
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +01001174 return EFI_SUCCESS;
1175failure_to_add_protocol:
1176 printf("ERROR: Failure to add protocol\n");
1177 return r;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001178out_of_resources:
1179 free(netobj);
Heinrich Schuchardt2f1a93f2022-11-26 16:44:38 +01001180 netobj = NULL;
Patrick Wildtfab89102020-10-07 11:04:33 +02001181 free(transmit_buffer);
1182 if (receive_buffer)
1183 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
1184 free(receive_buffer[i]);
1185 free(receive_buffer);
1186 free(receive_lengths);
Heinrich Schuchardteb07c282018-12-01 00:16:32 +01001187 printf("ERROR: Out of memory\n");
1188 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +02001189}
Adriano Cordova3c951362024-12-04 00:05:21 -03001190
1191/**
Adriano Cordova54674692025-03-03 11:13:15 -03001192 * efi_net_new_dp() - update device path associated to a net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001193 *
1194 * This gets called to update the device path when a new boot
1195 * file is downloaded
1196 *
1197 * @dev: dev to set the device path from
1198 * @server: remote server address
Adriano Cordova54674692025-03-03 11:13:15 -03001199 * @udev: net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001200 * Return: status code
1201 */
Adriano Cordova54674692025-03-03 11:13:15 -03001202efi_status_t efi_net_new_dp(const char *dev, const char *server, struct udevice *udev)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001203{
Adriano Cordova54674692025-03-03 11:13:15 -03001204 efi_status_t ret;
1205 struct efi_device_path *old_net_dp, *new_net_dp;
1206 struct efi_device_path **dp;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001207
Adriano Cordova54674692025-03-03 11:13:15 -03001208 dp = &dp_cache[next_dp_entry].net_dp;
1209
1210 dp_cache[next_dp_entry].dev = udev;
1211 dp_cache[next_dp_entry].is_valid = true;
1212 next_dp_entry++;
1213 next_dp_entry %= MAX_NUM_DP_ENTRIES;
1214
1215 old_net_dp = *dp;
1216 new_net_dp = NULL;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001217 if (!strcmp(dev, "Net"))
Adriano Cordova54674692025-03-03 11:13:15 -03001218 new_net_dp = efi_dp_from_eth(udev);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001219 else if (!strcmp(dev, "Http"))
Adriano Cordova54674692025-03-03 11:13:15 -03001220 new_net_dp = efi_dp_from_http(server, udev);
1221 if (!new_net_dp)
1222 return EFI_OUT_OF_RESOURCES;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001223
Adriano Cordova54674692025-03-03 11:13:15 -03001224 *dp = new_net_dp;
1225 // Free the old cache entry
1226 efi_free_pool(old_net_dp);
1227
1228 if (!netobj || netobj->dev != udev)
1229 return EFI_SUCCESS;
1230
1231 new_net_dp = efi_dp_dup(*dp);
1232 if (!new_net_dp)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001233 return EFI_OUT_OF_RESOURCES;
Adriano Cordova54674692025-03-03 11:13:15 -03001234 ret = efi_netobj_set_dp(netobj, new_net_dp);
1235 if (ret != EFI_SUCCESS)
1236 efi_free_pool(new_net_dp);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001237
Adriano Cordova54674692025-03-03 11:13:15 -03001238 return ret;
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001239}
1240
1241/**
Adriano Cordova54674692025-03-03 11:13:15 -03001242 * efi_net_dp_from_dev() - get device path associated to a net udevice
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001243 *
1244 * Produce a copy of the current device path
1245 *
Adriano Cordova54674692025-03-03 11:13:15 -03001246 * @dp: copy of the current device path
1247 * @udev: net udevice
1248 * @cache_only: get device path from cache only
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001249 */
Adriano Cordova54674692025-03-03 11:13:15 -03001250void efi_net_dp_from_dev(struct efi_device_path **dp, struct udevice *udev, bool cache_only)
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001251{
Adriano Cordova54674692025-03-03 11:13:15 -03001252 int i, j;
1253
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001254 if (!dp)
1255 return;
Adriano Cordova54674692025-03-03 11:13:15 -03001256
1257 *dp = NULL;
1258
1259 if (cache_only)
1260 goto cache;
1261
1262 if (netobj && netobj->dev == udev) {
1263 *dp = efi_netobj_get_dp(netobj);
1264 if (*dp)
1265 return;
1266 }
1267cache:
1268 // Search in the cache
1269 i = (next_dp_entry + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES;
1270 for (j = 0; dp_cache[i].is_valid && j < MAX_NUM_DP_ENTRIES;
1271 i = (i + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES, j++) {
1272 if (dp_cache[i].dev == udev) {
1273 *dp = efi_dp_dup(dp_cache[i].net_dp);
1274 return;
1275 }
1276 }
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001277}
1278
1279/**
Adriano Cordova3c951362024-12-04 00:05:21 -03001280 * efi_net_get_addr() - get IP address information
1281 *
1282 * Copy the current IP address, mask, and gateway into the
1283 * efi_ipv4_address structs pointed to by ip, mask and gw,
1284 * respectively.
1285 *
1286 * @ip: pointer to an efi_ipv4_address struct to
1287 * be filled with the current IP address
1288 * @mask: pointer to an efi_ipv4_address struct to
1289 * be filled with the current network mask
1290 * @gw: pointer to an efi_ipv4_address struct to be
1291 * filled with the current network gateway
Adriano Cordova54674692025-03-03 11:13:15 -03001292 * @dev: udevice
Adriano Cordova3c951362024-12-04 00:05:21 -03001293 */
1294void efi_net_get_addr(struct efi_ipv4_address *ip,
1295 struct efi_ipv4_address *mask,
Adriano Cordova54674692025-03-03 11:13:15 -03001296 struct efi_ipv4_address *gw,
1297 struct udevice *dev)
Adriano Cordova3c951362024-12-04 00:05:21 -03001298{
Adriano Cordova54674692025-03-03 11:13:15 -03001299 if (!dev)
1300 dev = eth_get_dev();
Adriano Cordova3c951362024-12-04 00:05:21 -03001301#ifdef CONFIG_NET_LWIP
1302 char ipstr[] = "ipaddr\0\0";
1303 char maskstr[] = "netmask\0\0";
1304 char gwstr[] = "gatewayip\0\0";
1305 int idx;
1306 struct in_addr tmp;
1307 char *env;
1308
Adriano Cordova54674692025-03-03 11:13:15 -03001309 idx = dev_seq(dev);
Adriano Cordova3c951362024-12-04 00:05:21 -03001310
1311 if (idx < 0 || idx > 99) {
1312 log_err("unexpected idx %d\n", idx);
1313 return;
1314 }
1315
1316 if (idx) {
1317 sprintf(ipstr, "ipaddr%d", idx);
1318 sprintf(maskstr, "netmask%d", idx);
1319 sprintf(gwstr, "gatewayip%d", idx);
1320 }
1321
1322 env = env_get(ipstr);
1323 if (env && ip) {
1324 tmp = string_to_ip(env);
1325 memcpy(ip, &tmp, sizeof(tmp));
1326 }
1327
1328 env = env_get(maskstr);
1329 if (env && mask) {
1330 tmp = string_to_ip(env);
1331 memcpy(mask, &tmp, sizeof(tmp));
1332 }
1333 env = env_get(gwstr);
1334 if (env && gw) {
1335 tmp = string_to_ip(env);
1336 memcpy(gw, &tmp, sizeof(tmp));
1337 }
1338#else
1339 if (ip)
1340 memcpy(ip, &net_ip, sizeof(net_ip));
1341 if (mask)
1342 memcpy(mask, &net_netmask, sizeof(net_netmask));
1343#endif
1344}
1345
1346/**
1347 * efi_net_set_addr() - set IP address information
1348 *
1349 * Set the current IP address, mask, and gateway to the
1350 * efi_ipv4_address structs pointed to by ip, mask and gw,
1351 * respectively.
1352 *
1353 * @ip: pointer to new IP address
1354 * @mask: pointer to new network mask to set
1355 * @gw: pointer to new network gateway
Adriano Cordova54674692025-03-03 11:13:15 -03001356 * @dev: udevice
Adriano Cordova3c951362024-12-04 00:05:21 -03001357 */
1358void efi_net_set_addr(struct efi_ipv4_address *ip,
1359 struct efi_ipv4_address *mask,
Adriano Cordova54674692025-03-03 11:13:15 -03001360 struct efi_ipv4_address *gw,
1361 struct udevice *dev)
Adriano Cordova3c951362024-12-04 00:05:21 -03001362{
Adriano Cordova54674692025-03-03 11:13:15 -03001363 if (!dev)
1364 dev = eth_get_dev();
Adriano Cordova3c951362024-12-04 00:05:21 -03001365#ifdef CONFIG_NET_LWIP
1366 char ipstr[] = "ipaddr\0\0";
1367 char maskstr[] = "netmask\0\0";
1368 char gwstr[] = "gatewayip\0\0";
1369 int idx;
1370 struct in_addr *addr;
1371 char tmp[46];
1372
Adriano Cordova54674692025-03-03 11:13:15 -03001373 idx = dev_seq(dev);
Adriano Cordova3c951362024-12-04 00:05:21 -03001374
1375 if (idx < 0 || idx > 99) {
1376 log_err("unexpected idx %d\n", idx);
1377 return;
1378 }
1379
1380 if (idx) {
1381 sprintf(ipstr, "ipaddr%d", idx);
1382 sprintf(maskstr, "netmask%d", idx);
1383 sprintf(gwstr, "gatewayip%d", idx);
1384 }
1385
1386 if (ip) {
1387 addr = (struct in_addr *)ip;
1388 ip_to_string(*addr, tmp);
1389 env_set(ipstr, tmp);
1390 }
1391
1392 if (mask) {
1393 addr = (struct in_addr *)mask;
1394 ip_to_string(*addr, tmp);
1395 env_set(maskstr, tmp);
1396 }
1397
1398 if (gw) {
1399 addr = (struct in_addr *)gw;
1400 ip_to_string(*addr, tmp);
1401 env_set(gwstr, tmp);
1402 }
1403#else
1404 if (ip)
1405 memcpy(&net_ip, ip, sizeof(*ip));
1406 if (mask)
1407 memcpy(&net_netmask, mask, sizeof(*mask));
1408#endif
1409}
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001410
Adriano Cordova54674692025-03-03 11:13:15 -03001411#if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL)
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001412/**
1413 * efi_net_set_buffer() - allocate a buffer of min 64K
1414 *
1415 * @buffer: allocated buffer
1416 * @size: desired buffer size
1417 * Return: status code
1418 */
1419static efi_status_t efi_net_set_buffer(void **buffer, size_t size)
1420{
1421 efi_status_t ret = EFI_SUCCESS;
1422
1423 if (size < SZ_64K)
1424 size = SZ_64K;
1425
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001426 *buffer = efi_alloc(size);
1427 if (!*buffer)
1428 ret = EFI_OUT_OF_RESOURCES;
1429
1430 efi_wget_info.buffer_size = (ulong)size;
1431
1432 return ret;
1433}
1434
1435/**
1436 * efi_net_parse_headers() - parse HTTP headers
1437 *
1438 * Parses the raw buffer efi_wget_info.headers into an array headers
1439 * of efi structs http_headers. The array should be at least
1440 * MAX_HTTP_HEADERS long.
1441 *
1442 * @num_headers: number of headers
1443 * @headers: caller provided array of struct http_headers
1444 */
1445void efi_net_parse_headers(ulong *num_headers, struct http_header *headers)
1446{
1447 if (!num_headers || !headers)
1448 return;
1449
1450 // Populate info with http headers.
1451 *num_headers = 0;
1452 const uchar *line_start = efi_wget_info.headers;
1453 const uchar *line_end;
1454 ulong count;
1455 struct http_header *current_header;
1456 const uchar *separator;
1457 size_t name_length, value_length;
1458
1459 // Skip the first line (request or status line)
1460 line_end = strstr(line_start, "\r\n");
1461
1462 if (line_end)
1463 line_start = line_end + 2;
1464
1465 while ((line_end = strstr(line_start, "\r\n")) != NULL) {
1466 count = *num_headers;
1467 if (line_start == line_end || count >= MAX_HTTP_HEADERS)
1468 break;
1469 current_header = headers + count;
1470 separator = strchr(line_start, ':');
1471 if (separator) {
1472 name_length = separator - line_start;
1473 ++separator;
1474 while (*separator == ' ')
1475 ++separator;
1476 value_length = line_end - separator;
1477 if (name_length < MAX_HTTP_HEADER_NAME &&
1478 value_length < MAX_HTTP_HEADER_VALUE) {
1479 strncpy(current_header->name, line_start, name_length);
1480 current_header->name[name_length] = '\0';
1481 strncpy(current_header->value, separator, value_length);
1482 current_header->value[value_length] = '\0';
1483 (*num_headers)++;
1484 }
1485 }
1486 line_start = line_end + 2;
1487 }
1488}
1489
1490/**
1491 * efi_net_do_request() - issue an HTTP request using wget
1492 *
1493 * @url: url
1494 * @method: HTTP method
1495 * @buffer: data buffer
1496 * @status_code: HTTP status code
1497 * @file_size: file size in bytes
1498 * @headers_buffer: headers buffer
1499 * Return: status code
1500 */
1501efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, void **buffer,
1502 u32 *status_code, ulong *file_size, char *headers_buffer)
1503{
1504 efi_status_t ret = EFI_SUCCESS;
1505 int wget_ret;
1506 static bool last_head;
1507
1508 if (!buffer || !file_size)
1509 return EFI_ABORTED;
1510
1511 efi_wget_info.method = (enum wget_http_method)method;
1512 efi_wget_info.headers = headers_buffer;
1513
1514 switch (method) {
1515 case HTTP_METHOD_GET:
1516 ret = efi_net_set_buffer(buffer, last_head ? (size_t)efi_wget_info.hdr_cont_len : 0);
1517 if (ret != EFI_SUCCESS)
1518 goto out;
Adriano Cordova54674692025-03-03 11:13:15 -03001519 eth_set_dev(netobj->dev);
1520 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001521 wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info);
1522 if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) {
1523 // Try again with updated buffer size
Adriano Cordovae9b19eb2024-12-04 00:05:26 -03001524 efi_free_pool(*buffer);
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001525 ret = efi_net_set_buffer(buffer, (size_t)efi_wget_info.hdr_cont_len);
1526 if (ret != EFI_SUCCESS)
1527 goto out;
Adriano Cordova54674692025-03-03 11:13:15 -03001528 eth_set_dev(netobj->dev);
1529 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001530 if (wget_request((ulong)*buffer, url, &efi_wget_info)) {
1531 efi_free_pool(*buffer);
1532 ret = EFI_DEVICE_ERROR;
1533 goto out;
1534 }
1535 } else if (wget_ret) {
1536 efi_free_pool(*buffer);
1537 ret = EFI_DEVICE_ERROR;
1538 goto out;
1539 }
1540 // Pass the actual number of received bytes to the application
1541 *file_size = efi_wget_info.file_size;
1542 *status_code = efi_wget_info.status_code;
1543 last_head = false;
1544 break;
1545 case HTTP_METHOD_HEAD:
1546 ret = efi_net_set_buffer(buffer, 0);
1547 if (ret != EFI_SUCCESS)
1548 goto out;
Adriano Cordova54674692025-03-03 11:13:15 -03001549 eth_set_dev(netobj->dev);
1550 env_set("ethact", eth_get_name());
Adriano Cordova0d1f5092024-12-04 00:05:24 -03001551 wget_request((ulong)*buffer, url, &efi_wget_info);
1552 *file_size = 0;
1553 *status_code = efi_wget_info.status_code;
1554 last_head = true;
1555 break;
1556 default:
1557 ret = EFI_UNSUPPORTED;
1558 break;
1559 }
1560
1561out:
1562 return ret;
1563}
Adriano Cordova54674692025-03-03 11:13:15 -03001564#endif