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