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