Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 3 | * Simple network protocol |
| 4 | * PXE base code protocol |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 5 | * |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 6 | * 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 Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 16 | */ |
| 17 | |
Heinrich Schuchardt | 955a321 | 2025-01-16 20:26:59 +0100 | [diff] [blame] | 18 | #define LOG_CATEGORY LOGC_EFI |
| 19 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 20 | #include <efi_loader.h> |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 21 | #include <dm.h> |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 22 | #include <linux/sizes.h> |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 23 | #include <malloc.h> |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 24 | #include <vsprintf.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 25 | #include <net.h> |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 26 | |
Adriano Cordova | 62e20fb | 2025-03-03 11:13:16 -0300 | [diff] [blame^] | 27 | #define MAX_NUM_DHCP_ENTRIES 10 |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 28 | #define MAX_NUM_DP_ENTRIES 10 |
| 29 | |
Adriano Cordova | 7f2bcd4 | 2025-03-03 11:13:11 -0300 | [diff] [blame] | 30 | const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID; |
Heinrich Schuchardt | 3127e7e | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 31 | static const efi_guid_t efi_pxe_base_code_protocol_guid = |
| 32 | EFI_PXE_BASE_CODE_PROTOCOL_GUID; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 33 | static void *new_tx_packet; |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 34 | static void *transmit_buffer; |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 35 | static uchar **receive_buffer; |
| 36 | static size_t *receive_lengths; |
| 37 | static int rx_packet_idx; |
| 38 | static int rx_packet_num; |
Heinrich Schuchardt | 2f1a93f | 2022-11-26 16:44:38 +0100 | [diff] [blame] | 39 | static struct efi_net_obj *netobj; |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 40 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 41 | struct dp_entry { |
| 42 | struct efi_device_path *net_dp; |
| 43 | struct udevice *dev; |
| 44 | bool is_valid; |
| 45 | }; |
| 46 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 47 | /* |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 48 | * 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 Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 52 | */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 53 | static struct dp_entry dp_cache[MAX_NUM_DP_ENTRIES]; |
| 54 | static int next_dp_entry; |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 55 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 56 | #if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL) |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 57 | static struct wget_http_info efi_wget_info = { |
| 58 | .set_bootdev = false, |
| 59 | .check_buffer_size = true, |
| 60 | |
| 61 | }; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 62 | #endif |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 63 | |
Adriano Cordova | 62e20fb | 2025-03-03 11:13:16 -0300 | [diff] [blame^] | 64 | struct dhcp_entry { |
| 65 | struct efi_pxe_packet *dhcp_ack; |
| 66 | struct udevice *dev; |
| 67 | bool is_valid; |
| 68 | }; |
| 69 | |
| 70 | static struct dhcp_entry dhcp_cache[MAX_NUM_DHCP_ENTRIES]; |
| 71 | static int next_dhcp_entry; |
| 72 | |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 73 | /* |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 74 | * The notification function of this event is called in every timer cycle |
| 75 | * to check if a new network packet has been received. |
| 76 | */ |
| 77 | static struct efi_event *network_timer_event; |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 78 | /* |
| 79 | * This event is signaled when a packet has been received. |
| 80 | */ |
| 81 | static struct efi_event *wait_for_packet; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 82 | |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 83 | /** |
| 84 | * struct efi_net_obj - EFI object representing a network interface |
| 85 | * |
Adriano Cordova | 9debc90 | 2024-12-04 00:05:25 -0300 | [diff] [blame] | 86 | * @header: EFI object header |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 87 | * @dev: net udevice |
Adriano Cordova | 9debc90 | 2024-12-04 00:05:25 -0300 | [diff] [blame] | 88 | * @net: simple network protocol interface |
| 89 | * @net_mode: status of the network interface |
| 90 | * @pxe: PXE base code protocol interface |
| 91 | * @pxe_mode: status of the PXE base code protocol |
| 92 | * @ip4_config2: IP4 Config2 protocol interface |
Adriano Cordova | e9b19eb | 2024-12-04 00:05:26 -0300 | [diff] [blame] | 93 | * @http_service_binding: Http service binding protocol interface |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 94 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 95 | struct efi_net_obj { |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 96 | struct efi_object header; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 97 | struct udevice *dev; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 98 | struct efi_simple_network net; |
| 99 | struct efi_simple_network_mode net_mode; |
Heinrich Schuchardt | 3127e7e | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 100 | struct efi_pxe_base_code_protocol pxe; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 101 | struct efi_pxe_mode pxe_mode; |
Adriano Cordova | 9debc90 | 2024-12-04 00:05:25 -0300 | [diff] [blame] | 102 | #if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL) |
| 103 | struct efi_ip4_config2_protocol ip4_config2; |
| 104 | #endif |
Adriano Cordova | e9b19eb | 2024-12-04 00:05:26 -0300 | [diff] [blame] | 105 | #if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL) |
| 106 | struct efi_service_binding_protocol http_service_binding; |
| 107 | #endif |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 108 | }; |
| 109 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 110 | /* |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 111 | * efi_netobj_is_active() - checks if a netobj is active in the efi subsystem |
| 112 | * |
| 113 | * @netobj: pointer to efi_net_obj |
| 114 | * Return: true if active |
| 115 | */ |
| 116 | static bool efi_netobj_is_active(struct efi_net_obj *netobj) |
| 117 | { |
| 118 | if (!netobj || !efi_search_obj(&netobj->header)) |
| 119 | return false; |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /* |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 126 | * efi_net_start() - start the network interface |
| 127 | * |
| 128 | * This function implements the Start service of the |
| 129 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 130 | * (UEFI) specification for details. |
| 131 | * |
| 132 | * @this: pointer to the protocol instance |
| 133 | * Return: status code |
| 134 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 135 | static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this) |
| 136 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 137 | efi_status_t ret = EFI_SUCCESS; |
| 138 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 139 | EFI_ENTRY("%p", this); |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 140 | /* Check parameters */ |
| 141 | if (!this) { |
| 142 | ret = EFI_INVALID_PARAMETER; |
| 143 | goto out; |
| 144 | } |
| 145 | |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 146 | if (this->mode->state != EFI_NETWORK_STOPPED) { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 147 | ret = EFI_ALREADY_STARTED; |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 148 | } else { |
| 149 | this->int_status = 0; |
| 150 | wait_for_packet->is_signaled = false; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 151 | this->mode->state = EFI_NETWORK_STARTED; |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 152 | } |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 153 | out: |
| 154 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 157 | /* |
| 158 | * efi_net_stop() - stop the network interface |
| 159 | * |
| 160 | * This function implements the Stop service of the |
| 161 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 162 | * (UEFI) specification for details. |
| 163 | * |
| 164 | * @this: pointer to the protocol instance |
| 165 | * Return: status code |
| 166 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 167 | static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this) |
| 168 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 169 | efi_status_t ret = EFI_SUCCESS; |
| 170 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 171 | EFI_ENTRY("%p", this); |
| 172 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 173 | /* Check parameters */ |
| 174 | if (!this) { |
| 175 | ret = EFI_INVALID_PARAMETER; |
| 176 | goto out; |
| 177 | } |
| 178 | |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 179 | if (this->mode->state == EFI_NETWORK_STOPPED) { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 180 | ret = EFI_NOT_STARTED; |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 181 | } else { |
| 182 | /* Disable hardware and put it into the reset state */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 183 | eth_set_dev(netobj->dev); |
| 184 | env_set("ethact", eth_get_name()); |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 185 | eth_halt(); |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 186 | /* Clear cache of packets */ |
| 187 | rx_packet_num = 0; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 188 | this->mode->state = EFI_NETWORK_STOPPED; |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 189 | } |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 190 | out: |
| 191 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 194 | /* |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 195 | * efi_net_initialize() - initialize the network interface |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 196 | * |
| 197 | * This function implements the Initialize service of the |
| 198 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 199 | * (UEFI) specification for details. |
| 200 | * |
| 201 | * @this: pointer to the protocol instance |
| 202 | * @extra_rx: extra receive buffer to be allocated |
| 203 | * @extra_tx: extra transmit buffer to be allocated |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 204 | * Return: status code |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 205 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 206 | static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this, |
| 207 | ulong extra_rx, ulong extra_tx) |
| 208 | { |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 209 | int ret; |
| 210 | efi_status_t r = EFI_SUCCESS; |
| 211 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 212 | EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx); |
| 213 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 214 | /* Check parameters */ |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 215 | if (!this) { |
| 216 | r = EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 217 | goto out; |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 220 | switch (this->mode->state) { |
| 221 | case EFI_NETWORK_INITIALIZED: |
| 222 | case EFI_NETWORK_STARTED: |
| 223 | break; |
| 224 | default: |
| 225 | r = EFI_NOT_STARTED; |
| 226 | goto out; |
| 227 | } |
| 228 | |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 229 | /* Setup packet buffers */ |
| 230 | net_init(); |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 231 | /* Clear cache of packets */ |
| 232 | rx_packet_num = 0; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 233 | /* Set the net device corresponding to the efi net object */ |
| 234 | eth_set_dev(netobj->dev); |
| 235 | env_set("ethact", eth_get_name()); |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 236 | /* Get hardware ready for send and receive operations */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 237 | ret = eth_start_udev(netobj->dev); |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 238 | if (ret < 0) { |
| 239 | eth_halt(); |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 240 | this->mode->state = EFI_NETWORK_STOPPED; |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 241 | r = EFI_DEVICE_ERROR; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 242 | goto out; |
| 243 | } else { |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 244 | this->int_status = 0; |
| 245 | wait_for_packet->is_signaled = false; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 246 | this->mode->state = EFI_NETWORK_INITIALIZED; |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 247 | } |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 248 | out: |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 249 | return EFI_EXIT(r); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 252 | /* |
| 253 | * efi_net_reset() - reinitialize the network interface |
| 254 | * |
| 255 | * This function implements the Reset service of the |
| 256 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 257 | * (UEFI) specification for details. |
| 258 | * |
| 259 | * @this: pointer to the protocol instance |
| 260 | * @extended_verification: execute exhaustive verification |
| 261 | * Return: status code |
| 262 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 263 | static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this, |
| 264 | int extended_verification) |
| 265 | { |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 266 | efi_status_t ret; |
| 267 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 268 | EFI_ENTRY("%p, %x", this, extended_verification); |
| 269 | |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 270 | /* Check parameters */ |
| 271 | if (!this) { |
| 272 | ret = EFI_INVALID_PARAMETER; |
| 273 | goto out; |
| 274 | } |
| 275 | |
| 276 | switch (this->mode->state) { |
| 277 | case EFI_NETWORK_INITIALIZED: |
| 278 | break; |
| 279 | case EFI_NETWORK_STOPPED: |
| 280 | ret = EFI_NOT_STARTED; |
| 281 | goto out; |
| 282 | default: |
| 283 | ret = EFI_DEVICE_ERROR; |
| 284 | goto out; |
| 285 | } |
| 286 | |
| 287 | this->mode->state = EFI_NETWORK_STARTED; |
| 288 | ret = EFI_CALL(efi_net_initialize(this, 0, 0)); |
| 289 | out: |
| 290 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 293 | /* |
| 294 | * efi_net_shutdown() - shut down the network interface |
| 295 | * |
| 296 | * This function implements the Shutdown service of the |
| 297 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 298 | * (UEFI) specification for details. |
| 299 | * |
| 300 | * @this: pointer to the protocol instance |
| 301 | * Return: status code |
| 302 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 303 | static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this) |
| 304 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 305 | efi_status_t ret = EFI_SUCCESS; |
| 306 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 307 | EFI_ENTRY("%p", this); |
| 308 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 309 | /* Check parameters */ |
| 310 | if (!this) { |
| 311 | ret = EFI_INVALID_PARAMETER; |
| 312 | goto out; |
| 313 | } |
| 314 | |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 315 | switch (this->mode->state) { |
| 316 | case EFI_NETWORK_INITIALIZED: |
| 317 | break; |
| 318 | case EFI_NETWORK_STOPPED: |
| 319 | ret = EFI_NOT_STARTED; |
| 320 | goto out; |
| 321 | default: |
| 322 | ret = EFI_DEVICE_ERROR; |
| 323 | goto out; |
| 324 | } |
| 325 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 326 | eth_set_dev(netobj->dev); |
| 327 | env_set("ethact", eth_get_name()); |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 328 | eth_halt(); |
Adriano Cordova | 62e20fb | 2025-03-03 11:13:16 -0300 | [diff] [blame^] | 329 | |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 330 | this->int_status = 0; |
| 331 | wait_for_packet->is_signaled = false; |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 332 | this->mode->state = EFI_NETWORK_STARTED; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 333 | |
| 334 | out: |
| 335 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 338 | /* |
| 339 | * efi_net_receive_filters() - mange multicast receive filters |
| 340 | * |
| 341 | * This function implements the ReceiveFilters service of the |
| 342 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 343 | * (UEFI) specification for details. |
| 344 | * |
| 345 | * @this: pointer to the protocol instance |
| 346 | * @enable: bit mask of receive filters to enable |
| 347 | * @disable: bit mask of receive filters to disable |
| 348 | * @reset_mcast_filter: true resets contents of the filters |
| 349 | * @mcast_filter_count: number of hardware MAC addresses in the new filters list |
| 350 | * @mcast_filter: list of new filters |
| 351 | * Return: status code |
| 352 | */ |
| 353 | static efi_status_t EFIAPI efi_net_receive_filters |
| 354 | (struct efi_simple_network *this, u32 enable, u32 disable, |
| 355 | int reset_mcast_filter, ulong mcast_filter_count, |
| 356 | struct efi_mac_address *mcast_filter) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 357 | { |
| 358 | EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable, |
| 359 | reset_mcast_filter, mcast_filter_count, mcast_filter); |
| 360 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 361 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 364 | /* |
| 365 | * efi_net_station_address() - set the hardware MAC address |
| 366 | * |
| 367 | * This function implements the StationAddress service of the |
| 368 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 369 | * (UEFI) specification for details. |
| 370 | * |
| 371 | * @this: pointer to the protocol instance |
| 372 | * @reset: if true reset the address to default |
| 373 | * @new_mac: new MAC address |
| 374 | * Return: status code |
| 375 | */ |
| 376 | static efi_status_t EFIAPI efi_net_station_address |
| 377 | (struct efi_simple_network *this, int reset, |
| 378 | struct efi_mac_address *new_mac) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 379 | { |
| 380 | EFI_ENTRY("%p, %x, %p", this, reset, new_mac); |
| 381 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 382 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 385 | /* |
| 386 | * efi_net_statistics() - reset or collect statistics of the network interface |
| 387 | * |
| 388 | * This function implements the Statistics service of the |
| 389 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 390 | * (UEFI) specification for details. |
| 391 | * |
| 392 | * @this: pointer to the protocol instance |
| 393 | * @reset: if true, the statistics are reset |
| 394 | * @stat_size: size of the statistics table |
| 395 | * @stat_table: table to receive the statistics |
| 396 | * Return: status code |
| 397 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 398 | static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this, |
| 399 | int reset, ulong *stat_size, |
| 400 | void *stat_table) |
| 401 | { |
| 402 | EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table); |
| 403 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 404 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 407 | /* |
| 408 | * efi_net_mcastiptomac() - translate multicast IP address to MAC address |
| 409 | * |
Heinrich Schuchardt | 33b318d | 2019-09-01 17:17:53 +0200 | [diff] [blame] | 410 | * This function implements the MCastIPtoMAC service of the |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 411 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 412 | * (UEFI) specification for details. |
| 413 | * |
| 414 | * @this: pointer to the protocol instance |
| 415 | * @ipv6: true if the IP address is an IPv6 address |
| 416 | * @ip: IP address |
| 417 | * @mac: MAC address |
| 418 | * Return: status code |
| 419 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 420 | static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this, |
| 421 | int ipv6, |
| 422 | struct efi_ip_address *ip, |
| 423 | struct efi_mac_address *mac) |
| 424 | { |
Heinrich Schuchardt | 33b318d | 2019-09-01 17:17:53 +0200 | [diff] [blame] | 425 | efi_status_t ret = EFI_SUCCESS; |
| 426 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 427 | EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac); |
| 428 | |
Heinrich Schuchardt | 33b318d | 2019-09-01 17:17:53 +0200 | [diff] [blame] | 429 | if (!this || !ip || !mac) { |
| 430 | ret = EFI_INVALID_PARAMETER; |
| 431 | goto out; |
| 432 | } |
| 433 | |
| 434 | if (ipv6) { |
| 435 | ret = EFI_UNSUPPORTED; |
| 436 | goto out; |
| 437 | } |
| 438 | |
| 439 | /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */ |
| 440 | if ((ip->ip_addr[0] & 0xf0) != 0xe0) { |
| 441 | ret = EFI_INVALID_PARAMETER; |
| 442 | goto out; |
| 443 | }; |
| 444 | |
| 445 | switch (this->mode->state) { |
| 446 | case EFI_NETWORK_INITIALIZED: |
| 447 | case EFI_NETWORK_STARTED: |
| 448 | break; |
| 449 | default: |
| 450 | ret = EFI_NOT_STARTED; |
| 451 | goto out; |
| 452 | } |
| 453 | |
| 454 | memset(mac, 0, sizeof(struct efi_mac_address)); |
| 455 | |
| 456 | /* |
| 457 | * Copy lower 23 bits of IPv4 multi-cast address |
| 458 | * RFC 1112, RFC 7042 2.1.1. |
| 459 | */ |
| 460 | mac->mac_addr[0] = 0x01; |
| 461 | mac->mac_addr[1] = 0x00; |
| 462 | mac->mac_addr[2] = 0x5E; |
| 463 | mac->mac_addr[3] = ip->ip_addr[1] & 0x7F; |
| 464 | mac->mac_addr[4] = ip->ip_addr[2]; |
| 465 | mac->mac_addr[5] = ip->ip_addr[3]; |
| 466 | out: |
| 467 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 470 | /** |
| 471 | * efi_net_nvdata() - read or write NVRAM |
| 472 | * |
| 473 | * This function implements the GetStatus service of the Simple Network |
| 474 | * Protocol. See the UEFI spec for details. |
| 475 | * |
| 476 | * @this: the instance of the Simple Network Protocol |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 477 | * @read_write: true for read, false for write |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 478 | * @offset: offset in NVRAM |
| 479 | * @buffer_size: size of buffer |
| 480 | * @buffer: buffer |
| 481 | * Return: status code |
| 482 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 483 | static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this, |
| 484 | int read_write, ulong offset, |
| 485 | ulong buffer_size, char *buffer) |
| 486 | { |
| 487 | EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size, |
| 488 | buffer); |
| 489 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 490 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 491 | } |
| 492 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 493 | /** |
| 494 | * efi_net_get_status() - get interrupt status |
| 495 | * |
| 496 | * This function implements the GetStatus service of the Simple Network |
| 497 | * Protocol. See the UEFI spec for details. |
| 498 | * |
| 499 | * @this: the instance of the Simple Network Protocol |
| 500 | * @int_status: interface status |
| 501 | * @txbuf: transmission buffer |
| 502 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 503 | static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this, |
| 504 | u32 *int_status, void **txbuf) |
| 505 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 506 | efi_status_t ret = EFI_SUCCESS; |
| 507 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 508 | EFI_ENTRY("%p, %p, %p", this, int_status, txbuf); |
| 509 | |
Heinrich Schuchardt | e371c5f | 2017-10-05 16:36:02 +0200 | [diff] [blame] | 510 | efi_timer_check(); |
| 511 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 512 | /* Check parameters */ |
| 513 | if (!this) { |
| 514 | ret = EFI_INVALID_PARAMETER; |
| 515 | goto out; |
| 516 | } |
| 517 | |
| 518 | switch (this->mode->state) { |
| 519 | case EFI_NETWORK_STOPPED: |
| 520 | ret = EFI_NOT_STARTED; |
| 521 | goto out; |
| 522 | case EFI_NETWORK_STARTED: |
| 523 | ret = EFI_DEVICE_ERROR; |
| 524 | goto out; |
| 525 | default: |
| 526 | break; |
| 527 | } |
| 528 | |
Heinrich Schuchardt | e371c5f | 2017-10-05 16:36:02 +0200 | [diff] [blame] | 529 | if (int_status) { |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 530 | *int_status = this->int_status; |
| 531 | this->int_status = 0; |
Heinrich Schuchardt | e371c5f | 2017-10-05 16:36:02 +0200 | [diff] [blame] | 532 | } |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 533 | if (txbuf) |
| 534 | *txbuf = new_tx_packet; |
| 535 | |
| 536 | new_tx_packet = NULL; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 537 | out: |
| 538 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 539 | } |
| 540 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 541 | /** |
| 542 | * efi_net_transmit() - transmit a packet |
| 543 | * |
| 544 | * This function implements the Transmit service of the Simple Network Protocol. |
| 545 | * See the UEFI spec for details. |
| 546 | * |
| 547 | * @this: the instance of the Simple Network Protocol |
| 548 | * @header_size: size of the media header |
| 549 | * @buffer_size: size of the buffer to receive the packet |
| 550 | * @buffer: buffer to receive the packet |
| 551 | * @src_addr: source hardware MAC address |
| 552 | * @dest_addr: destination hardware MAC address |
| 553 | * @protocol: type of header to build |
| 554 | * Return: status code |
| 555 | */ |
| 556 | static efi_status_t EFIAPI efi_net_transmit |
| 557 | (struct efi_simple_network *this, size_t header_size, |
| 558 | size_t buffer_size, void *buffer, |
| 559 | struct efi_mac_address *src_addr, |
| 560 | struct efi_mac_address *dest_addr, u16 *protocol) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 561 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 562 | efi_status_t ret = EFI_SUCCESS; |
| 563 | |
Heinrich Schuchardt | f66b2e3 | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 564 | EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this, |
| 565 | (unsigned long)header_size, (unsigned long)buffer_size, |
| 566 | buffer, src_addr, dest_addr, protocol); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 567 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 568 | efi_timer_check(); |
| 569 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 570 | /* Check parameters */ |
Heinrich Schuchardt | f286c2c | 2019-05-15 23:27:43 +0200 | [diff] [blame] | 571 | if (!this || !buffer) { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 572 | ret = EFI_INVALID_PARAMETER; |
| 573 | goto out; |
| 574 | } |
| 575 | |
| 576 | /* We do not support jumbo packets */ |
| 577 | if (buffer_size > PKTSIZE_ALIGN) { |
| 578 | ret = EFI_INVALID_PARAMETER; |
| 579 | goto out; |
| 580 | } |
| 581 | |
Heinrich Schuchardt | 0218a8f | 2019-08-31 10:55:29 +0200 | [diff] [blame] | 582 | /* At least the IP header has to fit into the buffer */ |
| 583 | if (buffer_size < this->mode->media_header_size) { |
| 584 | ret = EFI_BUFFER_TOO_SMALL; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 585 | goto out; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 586 | } |
| 587 | |
Heinrich Schuchardt | 0218a8f | 2019-08-31 10:55:29 +0200 | [diff] [blame] | 588 | /* |
| 589 | * TODO: |
| 590 | * Support VLANs. Use net_set_ether() for copying the header. Use a |
| 591 | * U_BOOT_ENV_CALLBACK to update the media header size. |
| 592 | */ |
| 593 | if (header_size) { |
| 594 | struct ethernet_hdr *header = buffer; |
| 595 | |
| 596 | if (!dest_addr || !protocol || |
| 597 | header_size != this->mode->media_header_size) { |
| 598 | ret = EFI_INVALID_PARAMETER; |
| 599 | goto out; |
| 600 | } |
| 601 | if (!src_addr) |
| 602 | src_addr = &this->mode->current_address; |
| 603 | |
| 604 | memcpy(header->et_dest, dest_addr, ARP_HLEN); |
| 605 | memcpy(header->et_src, src_addr, ARP_HLEN); |
| 606 | header->et_protlen = htons(*protocol); |
| 607 | } |
| 608 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 609 | switch (this->mode->state) { |
| 610 | case EFI_NETWORK_STOPPED: |
| 611 | ret = EFI_NOT_STARTED; |
| 612 | goto out; |
| 613 | case EFI_NETWORK_STARTED: |
| 614 | ret = EFI_DEVICE_ERROR; |
| 615 | goto out; |
| 616 | default: |
| 617 | break; |
| 618 | } |
| 619 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 620 | eth_set_dev(netobj->dev); |
| 621 | env_set("ethact", eth_get_name()); |
| 622 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 623 | /* Ethernet packets always fit, just bounce */ |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 624 | memcpy(transmit_buffer, buffer, buffer_size); |
| 625 | net_send_packet(transmit_buffer, buffer_size); |
Alexander Graf | 0b12b86 | 2016-09-06 14:26:27 +0200 | [diff] [blame] | 626 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 627 | new_tx_packet = buffer; |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 628 | this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 629 | out: |
| 630 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 633 | /** |
| 634 | * efi_net_receive() - receive a packet from a network interface |
Heinrich Schuchardt | f66b2e3 | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 635 | * |
| 636 | * This function implements the Receive service of the Simple Network Protocol. |
| 637 | * See the UEFI spec for details. |
| 638 | * |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 639 | * @this: the instance of the Simple Network Protocol |
| 640 | * @header_size: size of the media header |
| 641 | * @buffer_size: size of the buffer to receive the packet |
| 642 | * @buffer: buffer to receive the packet |
| 643 | * @src_addr: source MAC address |
| 644 | * @dest_addr: destination MAC address |
| 645 | * @protocol: protocol |
| 646 | * Return: status code |
Heinrich Schuchardt | f66b2e3 | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 647 | */ |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 648 | static efi_status_t EFIAPI efi_net_receive |
| 649 | (struct efi_simple_network *this, size_t *header_size, |
| 650 | size_t *buffer_size, void *buffer, |
| 651 | struct efi_mac_address *src_addr, |
| 652 | struct efi_mac_address *dest_addr, u16 *protocol) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 653 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 654 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 655 | struct ethernet_hdr *eth_hdr; |
| 656 | size_t hdr_size = sizeof(struct ethernet_hdr); |
| 657 | u16 protlen; |
| 658 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 659 | EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size, |
| 660 | buffer_size, buffer, src_addr, dest_addr, protocol); |
| 661 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 662 | /* Execute events */ |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 663 | efi_timer_check(); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 664 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 665 | /* Check parameters */ |
Heinrich Schuchardt | f286c2c | 2019-05-15 23:27:43 +0200 | [diff] [blame] | 666 | if (!this || !buffer || !buffer_size) { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 667 | ret = EFI_INVALID_PARAMETER; |
| 668 | goto out; |
| 669 | } |
| 670 | |
| 671 | switch (this->mode->state) { |
| 672 | case EFI_NETWORK_STOPPED: |
| 673 | ret = EFI_NOT_STARTED; |
| 674 | goto out; |
| 675 | case EFI_NETWORK_STARTED: |
| 676 | ret = EFI_DEVICE_ERROR; |
| 677 | goto out; |
| 678 | default: |
| 679 | break; |
| 680 | } |
| 681 | |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 682 | if (!rx_packet_num) { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 683 | ret = EFI_NOT_READY; |
| 684 | goto out; |
| 685 | } |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 686 | /* Fill export parameters */ |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 687 | eth_hdr = (struct ethernet_hdr *)receive_buffer[rx_packet_idx]; |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 688 | protlen = ntohs(eth_hdr->et_protlen); |
| 689 | if (protlen == 0x8100) { |
| 690 | hdr_size += 4; |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 691 | protlen = ntohs(*(u16 *)&receive_buffer[rx_packet_idx][hdr_size - 2]); |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 692 | } |
| 693 | if (header_size) |
| 694 | *header_size = hdr_size; |
| 695 | if (dest_addr) |
| 696 | memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN); |
| 697 | if (src_addr) |
| 698 | memcpy(src_addr, eth_hdr->et_src, ARP_HLEN); |
| 699 | if (protocol) |
| 700 | *protocol = protlen; |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 701 | if (*buffer_size < receive_lengths[rx_packet_idx]) { |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 702 | /* Packet doesn't fit, try again with bigger buffer */ |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 703 | *buffer_size = receive_lengths[rx_packet_idx]; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 704 | ret = EFI_BUFFER_TOO_SMALL; |
| 705 | goto out; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 706 | } |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 707 | /* Copy packet */ |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 708 | memcpy(buffer, receive_buffer[rx_packet_idx], |
| 709 | receive_lengths[rx_packet_idx]); |
| 710 | *buffer_size = receive_lengths[rx_packet_idx]; |
| 711 | rx_packet_idx = (rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV; |
| 712 | rx_packet_num--; |
| 713 | if (rx_packet_num) |
| 714 | wait_for_packet->is_signaled = true; |
| 715 | else |
| 716 | this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 717 | out: |
| 718 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 719 | } |
| 720 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 721 | /** |
| 722 | * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address |
| 723 | * |
| 724 | * This function is called by dhcp_handler(). |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 725 | * |
| 726 | * @pkt: packet received by dhcp_handler() |
| 727 | * @len: length of the packet received |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 728 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 729 | void efi_net_set_dhcp_ack(void *pkt, int len) |
| 730 | { |
Adriano Cordova | 62e20fb | 2025-03-03 11:13:16 -0300 | [diff] [blame^] | 731 | struct efi_pxe_packet **dhcp_ack; |
| 732 | struct udevice *dev; |
| 733 | |
| 734 | dhcp_ack = &dhcp_cache[next_dhcp_entry].dhcp_ack; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 735 | |
Adriano Cordova | 62e20fb | 2025-03-03 11:13:16 -0300 | [diff] [blame^] | 736 | /* For now this function gets called only by the current device */ |
| 737 | dev = eth_get_dev(); |
| 738 | |
| 739 | int maxsize = sizeof(**dhcp_ack); |
| 740 | |
| 741 | if (!*dhcp_ack) { |
| 742 | *dhcp_ack = malloc(maxsize); |
| 743 | if (!*dhcp_ack) |
Heinrich Schuchardt | 2f1a93f | 2022-11-26 16:44:38 +0100 | [diff] [blame] | 744 | return; |
| 745 | } |
Adriano Cordova | 62e20fb | 2025-03-03 11:13:16 -0300 | [diff] [blame^] | 746 | memset(*dhcp_ack, 0, maxsize); |
| 747 | memcpy(*dhcp_ack, pkt, min(len, maxsize)); |
Heinrich Schuchardt | 2f1a93f | 2022-11-26 16:44:38 +0100 | [diff] [blame] | 748 | |
Adriano Cordova | 62e20fb | 2025-03-03 11:13:16 -0300 | [diff] [blame^] | 749 | dhcp_cache[next_dhcp_entry].is_valid = true; |
| 750 | dhcp_cache[next_dhcp_entry].dev = dev; |
| 751 | next_dhcp_entry++; |
| 752 | next_dhcp_entry %= MAX_NUM_DHCP_ENTRIES; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 755 | /** |
| 756 | * efi_net_push() - callback for received network packet |
| 757 | * |
| 758 | * This function is called when a network packet is received by eth_rx(). |
| 759 | * |
| 760 | * @pkt: network packet |
| 761 | * @len: length |
| 762 | */ |
| 763 | static void efi_net_push(void *pkt, int len) |
| 764 | { |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 765 | int rx_packet_next; |
| 766 | |
| 767 | /* Check that we at least received an Ethernet header */ |
| 768 | if (len < sizeof(struct ethernet_hdr)) |
| 769 | return; |
| 770 | |
| 771 | /* Check that the buffer won't overflow */ |
| 772 | if (len > PKTSIZE_ALIGN) |
| 773 | return; |
| 774 | |
| 775 | /* Can't store more than pre-alloced buffer */ |
| 776 | if (rx_packet_num >= ETH_PACKETS_BATCH_RECV) |
| 777 | return; |
| 778 | |
| 779 | rx_packet_next = (rx_packet_idx + rx_packet_num) % |
| 780 | ETH_PACKETS_BATCH_RECV; |
| 781 | memcpy(receive_buffer[rx_packet_next], pkt, len); |
| 782 | receive_lengths[rx_packet_next] = len; |
| 783 | |
| 784 | rx_packet_num++; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /** |
| 788 | * efi_network_timer_notify() - check if a new network packet has been received |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 789 | * |
| 790 | * This notification function is called in every timer cycle. |
| 791 | * |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 792 | * @event: the event for which this notification function is registered |
| 793 | * @context: event context - not used in this function |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 794 | */ |
| 795 | static void EFIAPI efi_network_timer_notify(struct efi_event *event, |
| 796 | void *context) |
| 797 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 798 | struct efi_simple_network *this = (struct efi_simple_network *)context; |
| 799 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 800 | EFI_ENTRY("%p, %p", event, context); |
| 801 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 802 | /* |
| 803 | * Some network drivers do not support calling eth_rx() before |
| 804 | * initialization. |
| 805 | */ |
| 806 | if (!this || this->mode->state != EFI_NETWORK_INITIALIZED) |
| 807 | goto out; |
| 808 | |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 809 | if (!rx_packet_num) { |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 810 | eth_set_dev(netobj->dev); |
| 811 | env_set("ethact", eth_get_name()); |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 812 | push_packet = efi_net_push; |
| 813 | eth_rx(); |
| 814 | push_packet = NULL; |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 815 | if (rx_packet_num) { |
| 816 | this->int_status |= |
| 817 | EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT; |
| 818 | wait_for_packet->is_signaled = true; |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 819 | } |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 820 | } |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 821 | out: |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 822 | EFI_EXIT(EFI_SUCCESS); |
| 823 | } |
| 824 | |
Heinrich Schuchardt | 3127e7e | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 825 | static efi_status_t EFIAPI efi_pxe_base_code_start( |
| 826 | struct efi_pxe_base_code_protocol *this, |
| 827 | u8 use_ipv6) |
| 828 | { |
| 829 | return EFI_UNSUPPORTED; |
| 830 | } |
| 831 | |
| 832 | static efi_status_t EFIAPI efi_pxe_base_code_stop( |
| 833 | struct efi_pxe_base_code_protocol *this) |
| 834 | { |
| 835 | return EFI_UNSUPPORTED; |
| 836 | } |
| 837 | |
| 838 | static efi_status_t EFIAPI efi_pxe_base_code_dhcp( |
| 839 | struct efi_pxe_base_code_protocol *this, |
| 840 | u8 sort_offers) |
| 841 | { |
| 842 | return EFI_UNSUPPORTED; |
| 843 | } |
| 844 | |
| 845 | static efi_status_t EFIAPI efi_pxe_base_code_discover( |
| 846 | struct efi_pxe_base_code_protocol *this, |
| 847 | u16 type, u16 *layer, u8 bis, |
| 848 | struct efi_pxe_base_code_discover_info *info) |
| 849 | { |
| 850 | return EFI_UNSUPPORTED; |
| 851 | } |
| 852 | |
| 853 | static efi_status_t EFIAPI efi_pxe_base_code_mtftp( |
| 854 | struct efi_pxe_base_code_protocol *this, |
| 855 | u32 operation, void *buffer_ptr, |
| 856 | u8 overwrite, efi_uintn_t *buffer_size, |
| 857 | struct efi_ip_address server_ip, char *filename, |
| 858 | struct efi_pxe_base_code_mtftp_info *info, |
| 859 | u8 dont_use_buffer) |
| 860 | { |
| 861 | return EFI_UNSUPPORTED; |
| 862 | } |
| 863 | |
| 864 | static efi_status_t EFIAPI efi_pxe_base_code_udp_write( |
| 865 | struct efi_pxe_base_code_protocol *this, |
| 866 | u16 op_flags, struct efi_ip_address *dest_ip, |
| 867 | u16 *dest_port, |
| 868 | struct efi_ip_address *gateway_ip, |
| 869 | struct efi_ip_address *src_ip, u16 *src_port, |
| 870 | efi_uintn_t *header_size, void *header_ptr, |
| 871 | efi_uintn_t *buffer_size, void *buffer_ptr) |
| 872 | { |
| 873 | return EFI_UNSUPPORTED; |
| 874 | } |
| 875 | |
| 876 | static efi_status_t EFIAPI efi_pxe_base_code_udp_read( |
| 877 | struct efi_pxe_base_code_protocol *this, |
| 878 | u16 op_flags, struct efi_ip_address *dest_ip, |
| 879 | u16 *dest_port, struct efi_ip_address *src_ip, |
| 880 | u16 *src_port, efi_uintn_t *header_size, |
| 881 | void *header_ptr, efi_uintn_t *buffer_size, |
| 882 | void *buffer_ptr) |
| 883 | { |
| 884 | return EFI_UNSUPPORTED; |
| 885 | } |
| 886 | |
| 887 | static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter( |
| 888 | struct efi_pxe_base_code_protocol *this, |
| 889 | struct efi_pxe_base_code_filter *new_filter) |
| 890 | { |
| 891 | return EFI_UNSUPPORTED; |
| 892 | } |
| 893 | |
| 894 | static efi_status_t EFIAPI efi_pxe_base_code_arp( |
| 895 | struct efi_pxe_base_code_protocol *this, |
| 896 | struct efi_ip_address *ip_addr, |
| 897 | struct efi_mac_address *mac_addr) |
| 898 | { |
| 899 | return EFI_UNSUPPORTED; |
| 900 | } |
| 901 | |
| 902 | static efi_status_t EFIAPI efi_pxe_base_code_set_parameters( |
| 903 | struct efi_pxe_base_code_protocol *this, |
| 904 | u8 *new_auto_arp, u8 *new_send_guid, |
| 905 | u8 *new_ttl, u8 *new_tos, |
| 906 | u8 *new_make_callback) |
| 907 | { |
| 908 | return EFI_UNSUPPORTED; |
| 909 | } |
| 910 | |
| 911 | static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip( |
| 912 | struct efi_pxe_base_code_protocol *this, |
| 913 | struct efi_ip_address *new_station_ip, |
| 914 | struct efi_ip_address *new_subnet_mask) |
| 915 | { |
| 916 | return EFI_UNSUPPORTED; |
| 917 | } |
| 918 | |
| 919 | static efi_status_t EFIAPI efi_pxe_base_code_set_packets( |
| 920 | struct efi_pxe_base_code_protocol *this, |
| 921 | u8 *new_dhcp_discover_valid, |
| 922 | u8 *new_dhcp_ack_received, |
| 923 | u8 *new_proxy_offer_received, |
| 924 | u8 *new_pxe_discover_valid, |
| 925 | u8 *new_pxe_reply_received, |
| 926 | u8 *new_pxe_bis_reply_received, |
| 927 | EFI_PXE_BASE_CODE_PACKET *new_dchp_discover, |
| 928 | EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc, |
| 929 | EFI_PXE_BASE_CODE_PACKET *new_proxy_offer, |
| 930 | EFI_PXE_BASE_CODE_PACKET *new_pxe_discover, |
| 931 | EFI_PXE_BASE_CODE_PACKET *new_pxe_reply, |
| 932 | EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply) |
| 933 | { |
| 934 | return EFI_UNSUPPORTED; |
| 935 | } |
| 936 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 937 | /** |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 938 | * efi_netobj_set_dp() - set device path of a netobj |
| 939 | * |
| 940 | * @netobj: pointer to efi_net_obj |
| 941 | * @dp: device path to set, allocated by caller |
| 942 | * Return: status code |
| 943 | */ |
| 944 | efi_status_t efi_netobj_set_dp(struct efi_net_obj *netobj, struct efi_device_path *dp) |
| 945 | { |
| 946 | efi_status_t ret; |
| 947 | struct efi_handler *phandler; |
| 948 | struct efi_device_path *new_net_dp; |
| 949 | |
| 950 | if (!efi_netobj_is_active(netobj)) |
| 951 | return EFI_SUCCESS; |
| 952 | |
| 953 | // Create a device path for the netobj |
| 954 | new_net_dp = dp; |
| 955 | if (!new_net_dp) |
| 956 | return EFI_OUT_OF_RESOURCES; |
| 957 | |
| 958 | phandler = NULL; |
| 959 | efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler); |
| 960 | |
| 961 | // If the device path protocol is not yet installed, install it |
| 962 | if (!phandler) |
| 963 | goto add; |
| 964 | |
| 965 | // If it is already installed, try to update it |
| 966 | ret = efi_reinstall_protocol_interface(&netobj->header, &efi_guid_device_path, |
| 967 | phandler->protocol_interface, new_net_dp); |
| 968 | if (ret != EFI_SUCCESS) |
| 969 | return ret; |
| 970 | |
| 971 | return EFI_SUCCESS; |
| 972 | add: |
| 973 | ret = efi_add_protocol(&netobj->header, &efi_guid_device_path, |
| 974 | new_net_dp); |
| 975 | if (ret != EFI_SUCCESS) |
| 976 | return ret; |
| 977 | |
| 978 | return EFI_SUCCESS; |
| 979 | } |
| 980 | |
| 981 | /** |
| 982 | * efi_netobj_get_dp() - get device path of a netobj |
| 983 | * |
| 984 | * @netobj: pointer to efi_net_obj |
| 985 | * Return: device path, NULL on error |
| 986 | */ |
| 987 | static struct efi_device_path *efi_netobj_get_dp(struct efi_net_obj *netobj) |
| 988 | { |
| 989 | struct efi_handler *phandler; |
| 990 | |
| 991 | if (!efi_netobj_is_active(netobj)) |
| 992 | return NULL; |
| 993 | |
| 994 | phandler = NULL; |
| 995 | efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler); |
| 996 | |
| 997 | if (phandler && phandler->protocol_interface) |
| 998 | return efi_dp_dup(phandler->protocol_interface); |
| 999 | |
| 1000 | return NULL; |
| 1001 | } |
| 1002 | |
| 1003 | /** |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 1004 | * efi_net_do_start() - start the efi network stack |
| 1005 | * |
| 1006 | * This gets called from do_bootefi_exec() each time a payload gets executed. |
| 1007 | * |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1008 | * @dev: net udevice |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 1009 | * Return: status code |
| 1010 | */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1011 | efi_status_t efi_net_do_start(struct udevice *dev) |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 1012 | { |
| 1013 | efi_status_t r = EFI_SUCCESS; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1014 | struct efi_device_path *net_dp; |
| 1015 | |
| 1016 | if (dev != netobj->dev ) |
| 1017 | return r; |
| 1018 | |
| 1019 | efi_net_dp_from_dev(&net_dp, netobj->dev, true); |
| 1020 | // If no dp cache entry applies and there already |
| 1021 | // is a device path installed, continue |
| 1022 | if (!net_dp) { |
| 1023 | if (efi_netobj_get_dp(netobj)) |
| 1024 | goto set_addr; |
| 1025 | else |
| 1026 | net_dp = efi_dp_from_eth(netobj->dev); |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 1027 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | if (!net_dp) |
| 1031 | return EFI_OUT_OF_RESOURCES; |
| 1032 | |
| 1033 | r = efi_netobj_set_dp(netobj, net_dp); |
| 1034 | if (r != EFI_SUCCESS) |
| 1035 | return r; |
| 1036 | set_addr: |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 1037 | #ifdef CONFIG_EFI_HTTP_PROTOCOL |
| 1038 | /* |
| 1039 | * No harm on doing the following. If the PXE handle is present, the client could |
| 1040 | * find it and try to get its IP address from it. In here the PXE handle is present |
| 1041 | * but the PXE protocol is not yet implmenented, so we add this in the meantime. |
| 1042 | */ |
| 1043 | efi_net_get_addr((struct efi_ipv4_address *)&netobj->pxe_mode.station_ip, |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1044 | (struct efi_ipv4_address *)&netobj->pxe_mode.subnet_mask, NULL, dev); |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 1045 | #endif |
| 1046 | |
| 1047 | return r; |
| 1048 | } |
| 1049 | |
| 1050 | /** |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 1051 | * efi_net_register() - register the simple network protocol |
| 1052 | * |
| 1053 | * This gets called from do_bootefi_exec(). |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1054 | * @dev: net udevice |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 1055 | */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1056 | efi_status_t efi_net_register(struct udevice *dev) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1057 | { |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1058 | efi_status_t r; |
Adriano Cordova | 62e20fb | 2025-03-03 11:13:16 -0300 | [diff] [blame^] | 1059 | int i, j; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1060 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1061 | if (!dev) { |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 1062 | /* No network device active, don't expose any */ |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 1063 | return EFI_SUCCESS; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1064 | } |
| 1065 | |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 1066 | /* We only expose the "active" network device, so one is enough */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1067 | netobj = calloc(1, sizeof(*netobj)); |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 1068 | if (!netobj) |
| 1069 | goto out_of_resources; |
| 1070 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1071 | netobj->dev = dev; |
| 1072 | |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 1073 | /* Allocate an aligned transmit buffer */ |
| 1074 | transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN); |
| 1075 | if (!transmit_buffer) |
| 1076 | goto out_of_resources; |
| 1077 | transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN); |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 1078 | |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 1079 | /* Allocate a number of receive buffers */ |
| 1080 | receive_buffer = calloc(ETH_PACKETS_BATCH_RECV, |
| 1081 | sizeof(*receive_buffer)); |
| 1082 | if (!receive_buffer) |
| 1083 | goto out_of_resources; |
| 1084 | for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) { |
| 1085 | receive_buffer[i] = malloc(PKTSIZE_ALIGN); |
| 1086 | if (!receive_buffer[i]) |
| 1087 | goto out_of_resources; |
| 1088 | } |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1089 | |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 1090 | receive_lengths = calloc(ETH_PACKETS_BATCH_RECV, |
| 1091 | sizeof(*receive_lengths)); |
| 1092 | if (!receive_lengths) |
| 1093 | goto out_of_resources; |
| 1094 | |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 1095 | /* Hook net up to the device list */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 1096 | efi_add_handle(&netobj->header); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1097 | |
| 1098 | /* Fill in object data */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 1099 | r = efi_add_protocol(&netobj->header, &efi_net_guid, |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 1100 | &netobj->net); |
| 1101 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 1102 | goto failure_to_add_protocol; |
Adriano Cordova | 1738c7d | 2025-01-27 09:34:45 -0300 | [diff] [blame] | 1103 | |
Heinrich Schuchardt | 3127e7e | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 1104 | r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid, |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 1105 | &netobj->pxe); |
| 1106 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 1107 | goto failure_to_add_protocol; |
Heinrich Schuchardt | d54396a | 2017-10-05 16:35:57 +0200 | [diff] [blame] | 1108 | netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1109 | netobj->net.start = efi_net_start; |
| 1110 | netobj->net.stop = efi_net_stop; |
| 1111 | netobj->net.initialize = efi_net_initialize; |
| 1112 | netobj->net.reset = efi_net_reset; |
| 1113 | netobj->net.shutdown = efi_net_shutdown; |
| 1114 | netobj->net.receive_filters = efi_net_receive_filters; |
| 1115 | netobj->net.station_address = efi_net_station_address; |
| 1116 | netobj->net.statistics = efi_net_statistics; |
| 1117 | netobj->net.mcastiptomac = efi_net_mcastiptomac; |
| 1118 | netobj->net.nvdata = efi_net_nvdata; |
| 1119 | netobj->net.get_status = efi_net_get_status; |
| 1120 | netobj->net.transmit = efi_net_transmit; |
| 1121 | netobj->net.receive = efi_net_receive; |
| 1122 | netobj->net.mode = &netobj->net_mode; |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 1123 | netobj->net_mode.state = EFI_NETWORK_STOPPED; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1124 | if (dev_get_plat(dev)) |
| 1125 | memcpy(netobj->net_mode.current_address.mac_addr, |
| 1126 | ((struct eth_pdata *)dev_get_plat(dev))->enetaddr, 6); |
Heinrich Schuchardt | d1cc6cb | 2017-10-05 16:35:58 +0200 | [diff] [blame] | 1127 | netobj->net_mode.hwaddr_size = ARP_HLEN; |
Heinrich Schuchardt | 0218a8f | 2019-08-31 10:55:29 +0200 | [diff] [blame] | 1128 | netobj->net_mode.media_header_size = ETHER_HDR_SIZE; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1129 | netobj->net_mode.max_packet_size = PKTSIZE; |
Andrew Thomas | 27df0a7 | 2018-06-21 16:21:01 -0700 | [diff] [blame] | 1130 | netobj->net_mode.if_type = ARP_ETHER; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1131 | |
Heinrich Schuchardt | 3127e7e | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 1132 | netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION; |
| 1133 | netobj->pxe.start = efi_pxe_base_code_start; |
| 1134 | netobj->pxe.stop = efi_pxe_base_code_stop; |
| 1135 | netobj->pxe.dhcp = efi_pxe_base_code_dhcp; |
| 1136 | netobj->pxe.discover = efi_pxe_base_code_discover; |
| 1137 | netobj->pxe.mtftp = efi_pxe_base_code_mtftp; |
| 1138 | netobj->pxe.udp_write = efi_pxe_base_code_udp_write; |
| 1139 | netobj->pxe.udp_read = efi_pxe_base_code_udp_read; |
| 1140 | netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter; |
| 1141 | netobj->pxe.arp = efi_pxe_base_code_arp; |
| 1142 | netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters; |
| 1143 | netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip; |
| 1144 | netobj->pxe.set_packets = efi_pxe_base_code_set_packets; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1145 | netobj->pxe.mode = &netobj->pxe_mode; |
Adriano Cordova | 62e20fb | 2025-03-03 11:13:16 -0300 | [diff] [blame^] | 1146 | |
| 1147 | /* |
| 1148 | * Scan dhcp entries for one corresponding |
| 1149 | * to this udevice, from newest to oldest |
| 1150 | */ |
| 1151 | i = (next_dhcp_entry + MAX_NUM_DHCP_ENTRIES - 1) % MAX_NUM_DHCP_ENTRIES; |
| 1152 | for (j = 0; dhcp_cache[i].is_valid && j < MAX_NUM_DHCP_ENTRIES; |
| 1153 | i = (i + MAX_NUM_DHCP_ENTRIES - 1) % MAX_NUM_DHCP_ENTRIES, j++) { |
| 1154 | if (dev == dhcp_cache[i].dev) { |
| 1155 | netobj->pxe_mode.dhcp_ack = *dhcp_cache[i].dhcp_ack; |
| 1156 | break; |
| 1157 | } |
| 1158 | } |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1159 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1160 | /* |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 1161 | * Create WaitForPacket event. |
| 1162 | */ |
| 1163 | r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1164 | efi_network_timer_notify, NULL, NULL, |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 1165 | &wait_for_packet); |
| 1166 | if (r != EFI_SUCCESS) { |
| 1167 | printf("ERROR: Failed to register network event\n"); |
| 1168 | return r; |
| 1169 | } |
| 1170 | netobj->net.wait_for_packet = wait_for_packet; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1171 | |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 1172 | /* |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1173 | * Create a timer event. |
| 1174 | * |
| 1175 | * The notification function is used to check if a new network packet |
| 1176 | * has been received. |
Heinrich Schuchardt | 86df48c | 2018-03-24 18:40:22 +0100 | [diff] [blame] | 1177 | * |
| 1178 | * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL. |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1179 | */ |
Heinrich Schuchardt | 86df48c | 2018-03-24 18:40:22 +0100 | [diff] [blame] | 1180 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY, |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 1181 | efi_network_timer_notify, &netobj->net, NULL, |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1182 | &network_timer_event); |
| 1183 | if (r != EFI_SUCCESS) { |
| 1184 | printf("ERROR: Failed to register network event\n"); |
| 1185 | return r; |
| 1186 | } |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 1187 | /* Network is time critical, create event in every timer cycle */ |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1188 | r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0); |
| 1189 | if (r != EFI_SUCCESS) { |
| 1190 | printf("ERROR: Failed to set network timer\n"); |
| 1191 | return r; |
| 1192 | } |
| 1193 | |
Adriano Cordova | 9debc90 | 2024-12-04 00:05:25 -0300 | [diff] [blame] | 1194 | #if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL) |
| 1195 | r = efi_ipconfig_register(&netobj->header, &netobj->ip4_config2); |
| 1196 | if (r != EFI_SUCCESS) |
| 1197 | goto failure_to_add_protocol; |
Adriano Cordova | e9b19eb | 2024-12-04 00:05:26 -0300 | [diff] [blame] | 1198 | #endif |
| 1199 | |
| 1200 | #ifdef CONFIG_EFI_HTTP_PROTOCOL |
| 1201 | r = efi_http_register(&netobj->header, &netobj->http_service_binding); |
| 1202 | if (r != EFI_SUCCESS) |
| 1203 | goto failure_to_add_protocol; |
Adriano Cordova | 9debc90 | 2024-12-04 00:05:25 -0300 | [diff] [blame] | 1204 | #endif |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 1205 | return EFI_SUCCESS; |
| 1206 | failure_to_add_protocol: |
| 1207 | printf("ERROR: Failure to add protocol\n"); |
| 1208 | return r; |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 1209 | out_of_resources: |
| 1210 | free(netobj); |
Heinrich Schuchardt | 2f1a93f | 2022-11-26 16:44:38 +0100 | [diff] [blame] | 1211 | netobj = NULL; |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 1212 | free(transmit_buffer); |
| 1213 | if (receive_buffer) |
| 1214 | for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) |
| 1215 | free(receive_buffer[i]); |
| 1216 | free(receive_buffer); |
| 1217 | free(receive_lengths); |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 1218 | printf("ERROR: Out of memory\n"); |
| 1219 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1220 | } |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1221 | |
| 1222 | /** |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1223 | * efi_net_new_dp() - update device path associated to a net udevice |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1224 | * |
| 1225 | * This gets called to update the device path when a new boot |
| 1226 | * file is downloaded |
| 1227 | * |
| 1228 | * @dev: dev to set the device path from |
| 1229 | * @server: remote server address |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1230 | * @udev: net udevice |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1231 | * Return: status code |
| 1232 | */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1233 | efi_status_t efi_net_new_dp(const char *dev, const char *server, struct udevice *udev) |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1234 | { |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1235 | efi_status_t ret; |
| 1236 | struct efi_device_path *old_net_dp, *new_net_dp; |
| 1237 | struct efi_device_path **dp; |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1238 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1239 | dp = &dp_cache[next_dp_entry].net_dp; |
| 1240 | |
| 1241 | dp_cache[next_dp_entry].dev = udev; |
| 1242 | dp_cache[next_dp_entry].is_valid = true; |
| 1243 | next_dp_entry++; |
| 1244 | next_dp_entry %= MAX_NUM_DP_ENTRIES; |
| 1245 | |
| 1246 | old_net_dp = *dp; |
| 1247 | new_net_dp = NULL; |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1248 | if (!strcmp(dev, "Net")) |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1249 | new_net_dp = efi_dp_from_eth(udev); |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1250 | else if (!strcmp(dev, "Http")) |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1251 | new_net_dp = efi_dp_from_http(server, udev); |
| 1252 | if (!new_net_dp) |
| 1253 | return EFI_OUT_OF_RESOURCES; |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1254 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1255 | *dp = new_net_dp; |
| 1256 | // Free the old cache entry |
| 1257 | efi_free_pool(old_net_dp); |
| 1258 | |
| 1259 | if (!netobj || netobj->dev != udev) |
| 1260 | return EFI_SUCCESS; |
| 1261 | |
| 1262 | new_net_dp = efi_dp_dup(*dp); |
| 1263 | if (!new_net_dp) |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1264 | return EFI_OUT_OF_RESOURCES; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1265 | ret = efi_netobj_set_dp(netobj, new_net_dp); |
| 1266 | if (ret != EFI_SUCCESS) |
| 1267 | efi_free_pool(new_net_dp); |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1268 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1269 | return ret; |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | /** |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1273 | * efi_net_dp_from_dev() - get device path associated to a net udevice |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1274 | * |
| 1275 | * Produce a copy of the current device path |
| 1276 | * |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1277 | * @dp: copy of the current device path |
| 1278 | * @udev: net udevice |
| 1279 | * @cache_only: get device path from cache only |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1280 | */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1281 | void efi_net_dp_from_dev(struct efi_device_path **dp, struct udevice *udev, bool cache_only) |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1282 | { |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1283 | int i, j; |
| 1284 | |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1285 | if (!dp) |
| 1286 | return; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1287 | |
| 1288 | *dp = NULL; |
| 1289 | |
| 1290 | if (cache_only) |
| 1291 | goto cache; |
| 1292 | |
| 1293 | if (netobj && netobj->dev == udev) { |
| 1294 | *dp = efi_netobj_get_dp(netobj); |
| 1295 | if (*dp) |
| 1296 | return; |
| 1297 | } |
| 1298 | cache: |
| 1299 | // Search in the cache |
| 1300 | i = (next_dp_entry + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES; |
| 1301 | for (j = 0; dp_cache[i].is_valid && j < MAX_NUM_DP_ENTRIES; |
| 1302 | i = (i + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES, j++) { |
| 1303 | if (dp_cache[i].dev == udev) { |
| 1304 | *dp = efi_dp_dup(dp_cache[i].net_dp); |
| 1305 | return; |
| 1306 | } |
| 1307 | } |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | /** |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1311 | * efi_net_get_addr() - get IP address information |
| 1312 | * |
| 1313 | * Copy the current IP address, mask, and gateway into the |
| 1314 | * efi_ipv4_address structs pointed to by ip, mask and gw, |
| 1315 | * respectively. |
| 1316 | * |
| 1317 | * @ip: pointer to an efi_ipv4_address struct to |
| 1318 | * be filled with the current IP address |
| 1319 | * @mask: pointer to an efi_ipv4_address struct to |
| 1320 | * be filled with the current network mask |
| 1321 | * @gw: pointer to an efi_ipv4_address struct to be |
| 1322 | * filled with the current network gateway |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1323 | * @dev: udevice |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1324 | */ |
| 1325 | void efi_net_get_addr(struct efi_ipv4_address *ip, |
| 1326 | struct efi_ipv4_address *mask, |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1327 | struct efi_ipv4_address *gw, |
| 1328 | struct udevice *dev) |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1329 | { |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1330 | if (!dev) |
| 1331 | dev = eth_get_dev(); |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1332 | #ifdef CONFIG_NET_LWIP |
| 1333 | char ipstr[] = "ipaddr\0\0"; |
| 1334 | char maskstr[] = "netmask\0\0"; |
| 1335 | char gwstr[] = "gatewayip\0\0"; |
| 1336 | int idx; |
| 1337 | struct in_addr tmp; |
| 1338 | char *env; |
| 1339 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1340 | idx = dev_seq(dev); |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1341 | |
| 1342 | if (idx < 0 || idx > 99) { |
| 1343 | log_err("unexpected idx %d\n", idx); |
| 1344 | return; |
| 1345 | } |
| 1346 | |
| 1347 | if (idx) { |
| 1348 | sprintf(ipstr, "ipaddr%d", idx); |
| 1349 | sprintf(maskstr, "netmask%d", idx); |
| 1350 | sprintf(gwstr, "gatewayip%d", idx); |
| 1351 | } |
| 1352 | |
| 1353 | env = env_get(ipstr); |
| 1354 | if (env && ip) { |
| 1355 | tmp = string_to_ip(env); |
| 1356 | memcpy(ip, &tmp, sizeof(tmp)); |
| 1357 | } |
| 1358 | |
| 1359 | env = env_get(maskstr); |
| 1360 | if (env && mask) { |
| 1361 | tmp = string_to_ip(env); |
| 1362 | memcpy(mask, &tmp, sizeof(tmp)); |
| 1363 | } |
| 1364 | env = env_get(gwstr); |
| 1365 | if (env && gw) { |
| 1366 | tmp = string_to_ip(env); |
| 1367 | memcpy(gw, &tmp, sizeof(tmp)); |
| 1368 | } |
| 1369 | #else |
| 1370 | if (ip) |
| 1371 | memcpy(ip, &net_ip, sizeof(net_ip)); |
| 1372 | if (mask) |
| 1373 | memcpy(mask, &net_netmask, sizeof(net_netmask)); |
| 1374 | #endif |
| 1375 | } |
| 1376 | |
| 1377 | /** |
| 1378 | * efi_net_set_addr() - set IP address information |
| 1379 | * |
| 1380 | * Set the current IP address, mask, and gateway to the |
| 1381 | * efi_ipv4_address structs pointed to by ip, mask and gw, |
| 1382 | * respectively. |
| 1383 | * |
| 1384 | * @ip: pointer to new IP address |
| 1385 | * @mask: pointer to new network mask to set |
| 1386 | * @gw: pointer to new network gateway |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1387 | * @dev: udevice |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1388 | */ |
| 1389 | void efi_net_set_addr(struct efi_ipv4_address *ip, |
| 1390 | struct efi_ipv4_address *mask, |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1391 | struct efi_ipv4_address *gw, |
| 1392 | struct udevice *dev) |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1393 | { |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1394 | if (!dev) |
| 1395 | dev = eth_get_dev(); |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1396 | #ifdef CONFIG_NET_LWIP |
| 1397 | char ipstr[] = "ipaddr\0\0"; |
| 1398 | char maskstr[] = "netmask\0\0"; |
| 1399 | char gwstr[] = "gatewayip\0\0"; |
| 1400 | int idx; |
| 1401 | struct in_addr *addr; |
| 1402 | char tmp[46]; |
| 1403 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1404 | idx = dev_seq(dev); |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1405 | |
| 1406 | if (idx < 0 || idx > 99) { |
| 1407 | log_err("unexpected idx %d\n", idx); |
| 1408 | return; |
| 1409 | } |
| 1410 | |
| 1411 | if (idx) { |
| 1412 | sprintf(ipstr, "ipaddr%d", idx); |
| 1413 | sprintf(maskstr, "netmask%d", idx); |
| 1414 | sprintf(gwstr, "gatewayip%d", idx); |
| 1415 | } |
| 1416 | |
| 1417 | if (ip) { |
| 1418 | addr = (struct in_addr *)ip; |
| 1419 | ip_to_string(*addr, tmp); |
| 1420 | env_set(ipstr, tmp); |
| 1421 | } |
| 1422 | |
| 1423 | if (mask) { |
| 1424 | addr = (struct in_addr *)mask; |
| 1425 | ip_to_string(*addr, tmp); |
| 1426 | env_set(maskstr, tmp); |
| 1427 | } |
| 1428 | |
| 1429 | if (gw) { |
| 1430 | addr = (struct in_addr *)gw; |
| 1431 | ip_to_string(*addr, tmp); |
| 1432 | env_set(gwstr, tmp); |
| 1433 | } |
| 1434 | #else |
| 1435 | if (ip) |
| 1436 | memcpy(&net_ip, ip, sizeof(*ip)); |
| 1437 | if (mask) |
| 1438 | memcpy(&net_netmask, mask, sizeof(*mask)); |
| 1439 | #endif |
| 1440 | } |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1441 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1442 | #if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL) |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1443 | /** |
| 1444 | * efi_net_set_buffer() - allocate a buffer of min 64K |
| 1445 | * |
| 1446 | * @buffer: allocated buffer |
| 1447 | * @size: desired buffer size |
| 1448 | * Return: status code |
| 1449 | */ |
| 1450 | static efi_status_t efi_net_set_buffer(void **buffer, size_t size) |
| 1451 | { |
| 1452 | efi_status_t ret = EFI_SUCCESS; |
| 1453 | |
| 1454 | if (size < SZ_64K) |
| 1455 | size = SZ_64K; |
| 1456 | |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1457 | *buffer = efi_alloc(size); |
| 1458 | if (!*buffer) |
| 1459 | ret = EFI_OUT_OF_RESOURCES; |
| 1460 | |
| 1461 | efi_wget_info.buffer_size = (ulong)size; |
| 1462 | |
| 1463 | return ret; |
| 1464 | } |
| 1465 | |
| 1466 | /** |
| 1467 | * efi_net_parse_headers() - parse HTTP headers |
| 1468 | * |
| 1469 | * Parses the raw buffer efi_wget_info.headers into an array headers |
| 1470 | * of efi structs http_headers. The array should be at least |
| 1471 | * MAX_HTTP_HEADERS long. |
| 1472 | * |
| 1473 | * @num_headers: number of headers |
| 1474 | * @headers: caller provided array of struct http_headers |
| 1475 | */ |
| 1476 | void efi_net_parse_headers(ulong *num_headers, struct http_header *headers) |
| 1477 | { |
| 1478 | if (!num_headers || !headers) |
| 1479 | return; |
| 1480 | |
| 1481 | // Populate info with http headers. |
| 1482 | *num_headers = 0; |
| 1483 | const uchar *line_start = efi_wget_info.headers; |
| 1484 | const uchar *line_end; |
| 1485 | ulong count; |
| 1486 | struct http_header *current_header; |
| 1487 | const uchar *separator; |
| 1488 | size_t name_length, value_length; |
| 1489 | |
| 1490 | // Skip the first line (request or status line) |
| 1491 | line_end = strstr(line_start, "\r\n"); |
| 1492 | |
| 1493 | if (line_end) |
| 1494 | line_start = line_end + 2; |
| 1495 | |
| 1496 | while ((line_end = strstr(line_start, "\r\n")) != NULL) { |
| 1497 | count = *num_headers; |
| 1498 | if (line_start == line_end || count >= MAX_HTTP_HEADERS) |
| 1499 | break; |
| 1500 | current_header = headers + count; |
| 1501 | separator = strchr(line_start, ':'); |
| 1502 | if (separator) { |
| 1503 | name_length = separator - line_start; |
| 1504 | ++separator; |
| 1505 | while (*separator == ' ') |
| 1506 | ++separator; |
| 1507 | value_length = line_end - separator; |
| 1508 | if (name_length < MAX_HTTP_HEADER_NAME && |
| 1509 | value_length < MAX_HTTP_HEADER_VALUE) { |
| 1510 | strncpy(current_header->name, line_start, name_length); |
| 1511 | current_header->name[name_length] = '\0'; |
| 1512 | strncpy(current_header->value, separator, value_length); |
| 1513 | current_header->value[value_length] = '\0'; |
| 1514 | (*num_headers)++; |
| 1515 | } |
| 1516 | } |
| 1517 | line_start = line_end + 2; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | /** |
| 1522 | * efi_net_do_request() - issue an HTTP request using wget |
| 1523 | * |
| 1524 | * @url: url |
| 1525 | * @method: HTTP method |
| 1526 | * @buffer: data buffer |
| 1527 | * @status_code: HTTP status code |
| 1528 | * @file_size: file size in bytes |
| 1529 | * @headers_buffer: headers buffer |
| 1530 | * Return: status code |
| 1531 | */ |
| 1532 | efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, void **buffer, |
| 1533 | u32 *status_code, ulong *file_size, char *headers_buffer) |
| 1534 | { |
| 1535 | efi_status_t ret = EFI_SUCCESS; |
| 1536 | int wget_ret; |
| 1537 | static bool last_head; |
| 1538 | |
| 1539 | if (!buffer || !file_size) |
| 1540 | return EFI_ABORTED; |
| 1541 | |
| 1542 | efi_wget_info.method = (enum wget_http_method)method; |
| 1543 | efi_wget_info.headers = headers_buffer; |
| 1544 | |
| 1545 | switch (method) { |
| 1546 | case HTTP_METHOD_GET: |
| 1547 | ret = efi_net_set_buffer(buffer, last_head ? (size_t)efi_wget_info.hdr_cont_len : 0); |
| 1548 | if (ret != EFI_SUCCESS) |
| 1549 | goto out; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1550 | eth_set_dev(netobj->dev); |
| 1551 | env_set("ethact", eth_get_name()); |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1552 | wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info); |
| 1553 | if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) { |
| 1554 | // Try again with updated buffer size |
Adriano Cordova | e9b19eb | 2024-12-04 00:05:26 -0300 | [diff] [blame] | 1555 | efi_free_pool(*buffer); |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1556 | ret = efi_net_set_buffer(buffer, (size_t)efi_wget_info.hdr_cont_len); |
| 1557 | if (ret != EFI_SUCCESS) |
| 1558 | goto out; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1559 | eth_set_dev(netobj->dev); |
| 1560 | env_set("ethact", eth_get_name()); |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1561 | if (wget_request((ulong)*buffer, url, &efi_wget_info)) { |
| 1562 | efi_free_pool(*buffer); |
| 1563 | ret = EFI_DEVICE_ERROR; |
| 1564 | goto out; |
| 1565 | } |
| 1566 | } else if (wget_ret) { |
| 1567 | efi_free_pool(*buffer); |
| 1568 | ret = EFI_DEVICE_ERROR; |
| 1569 | goto out; |
| 1570 | } |
| 1571 | // Pass the actual number of received bytes to the application |
| 1572 | *file_size = efi_wget_info.file_size; |
| 1573 | *status_code = efi_wget_info.status_code; |
| 1574 | last_head = false; |
| 1575 | break; |
| 1576 | case HTTP_METHOD_HEAD: |
| 1577 | ret = efi_net_set_buffer(buffer, 0); |
| 1578 | if (ret != EFI_SUCCESS) |
| 1579 | goto out; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1580 | eth_set_dev(netobj->dev); |
| 1581 | env_set("ethact", eth_get_name()); |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1582 | wget_request((ulong)*buffer, url, &efi_wget_info); |
| 1583 | *file_size = 0; |
| 1584 | *status_code = efi_wget_info.status_code; |
| 1585 | last_head = true; |
| 1586 | break; |
| 1587 | default: |
| 1588 | ret = EFI_UNSUPPORTED; |
| 1589 | break; |
| 1590 | } |
| 1591 | |
| 1592 | out: |
| 1593 | return ret; |
| 1594 | } |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1595 | #endif |