Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | 72a8f16 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 3 | * Simple network protocol |
| 4 | * PXE base code protocol |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 5 | * |
Heinrich Schuchardt | 72a8f16 | 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 | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <common.h> |
| 19 | #include <efi_loader.h> |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 20 | #include <malloc.h> |
| 21 | |
Heinrich Schuchardt | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 22 | static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID; |
Heinrich Schuchardt | a6d3709 | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 23 | static const efi_guid_t efi_pxe_base_code_protocol_guid = |
| 24 | EFI_PXE_BASE_CODE_PROTOCOL_GUID; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 25 | static struct efi_pxe_packet *dhcp_ack; |
| 26 | static bool new_rx_packet; |
| 27 | static void *new_tx_packet; |
Heinrich Schuchardt | 622fe62 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 28 | static void *transmit_buffer; |
| 29 | |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 30 | /* |
| 31 | * The notification function of this event is called in every timer cycle |
| 32 | * to check if a new network packet has been received. |
| 33 | */ |
| 34 | static struct efi_event *network_timer_event; |
Heinrich Schuchardt | e5c2160 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 35 | /* |
| 36 | * This event is signaled when a packet has been received. |
| 37 | */ |
| 38 | static struct efi_event *wait_for_packet; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 39 | |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 40 | /** |
| 41 | * struct efi_net_obj - EFI object representing a network interface |
| 42 | * |
| 43 | * @header: EFI object header |
| 44 | * @net: simple network protocol interface |
| 45 | * @net_mode: status of the network interface |
| 46 | * @pxe: PXE base code protocol interface |
| 47 | * @pxe_mode: status of the PXE base code protocol |
| 48 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 49 | struct efi_net_obj { |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 50 | struct efi_object header; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 51 | struct efi_simple_network net; |
| 52 | struct efi_simple_network_mode net_mode; |
Heinrich Schuchardt | a6d3709 | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 53 | struct efi_pxe_base_code_protocol pxe; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 54 | struct efi_pxe_mode pxe_mode; |
| 55 | }; |
| 56 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 57 | /* |
| 58 | * efi_net_start() - start the network interface |
| 59 | * |
| 60 | * This function implements the Start service of the |
| 61 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 62 | * (UEFI) specification for details. |
| 63 | * |
| 64 | * @this: pointer to the protocol instance |
| 65 | * Return: status code |
| 66 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 67 | static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this) |
| 68 | { |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 69 | efi_status_t ret = EFI_SUCCESS; |
| 70 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 71 | EFI_ENTRY("%p", this); |
| 72 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 73 | /* Check parameters */ |
| 74 | if (!this) { |
| 75 | ret = EFI_INVALID_PARAMETER; |
| 76 | goto out; |
| 77 | } |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 78 | |
Heinrich Schuchardt | 7f6d874 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 79 | if (this->mode->state != EFI_NETWORK_STOPPED) { |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 80 | ret = EFI_ALREADY_STARTED; |
Heinrich Schuchardt | 7f6d874 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 81 | } else { |
| 82 | this->int_status = 0; |
| 83 | wait_for_packet->is_signaled = false; |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 84 | this->mode->state = EFI_NETWORK_STARTED; |
Heinrich Schuchardt | 7f6d874 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 85 | } |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 86 | out: |
| 87 | return EFI_EXIT(ret); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 90 | /* |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 91 | * efi_net_stop() - stop the network interface |
| 92 | * |
| 93 | * This function implements the Stop service of the |
| 94 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 95 | * (UEFI) specification for details. |
| 96 | * |
| 97 | * @this: pointer to the protocol instance |
| 98 | * Return: status code |
| 99 | */ |
| 100 | static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this) |
| 101 | { |
| 102 | efi_status_t ret = EFI_SUCCESS; |
| 103 | |
| 104 | EFI_ENTRY("%p", this); |
| 105 | |
| 106 | /* Check parameters */ |
| 107 | if (!this) { |
| 108 | ret = EFI_INVALID_PARAMETER; |
| 109 | goto out; |
| 110 | } |
| 111 | |
Heinrich Schuchardt | 72a8f16 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 112 | if (this->mode->state == EFI_NETWORK_STOPPED) { |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 113 | ret = EFI_NOT_STARTED; |
Heinrich Schuchardt | 72a8f16 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 114 | } else { |
| 115 | /* Disable hardware and put it into the reset state */ |
| 116 | eth_halt(); |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 117 | this->mode->state = EFI_NETWORK_STOPPED; |
Heinrich Schuchardt | 72a8f16 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 118 | } |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 119 | out: |
| 120 | return EFI_EXIT(ret); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * efi_net_initialize() - initialize the network interface |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 125 | * |
| 126 | * This function implements the Initialize service of the |
| 127 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 128 | * (UEFI) specification for details. |
| 129 | * |
| 130 | * @this: pointer to the protocol instance |
| 131 | * @extra_rx: extra receive buffer to be allocated |
| 132 | * @extra_tx: extra transmit buffer to be allocated |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 133 | * Return: status code |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 134 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 135 | static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this, |
| 136 | ulong extra_rx, ulong extra_tx) |
| 137 | { |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 138 | int ret; |
| 139 | efi_status_t r = EFI_SUCCESS; |
| 140 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 141 | EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx); |
| 142 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 143 | /* Check parameters */ |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 144 | if (!this) { |
| 145 | r = EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 146 | goto out; |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 147 | } |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 148 | |
Heinrich Schuchardt | 72a8f16 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 149 | switch (this->mode->state) { |
| 150 | case EFI_NETWORK_INITIALIZED: |
| 151 | case EFI_NETWORK_STARTED: |
| 152 | break; |
| 153 | default: |
| 154 | r = EFI_NOT_STARTED; |
| 155 | goto out; |
| 156 | } |
| 157 | |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 158 | /* Setup packet buffers */ |
| 159 | net_init(); |
| 160 | /* Disable hardware and put it into the reset state */ |
| 161 | eth_halt(); |
| 162 | /* Set current device according to environment variables */ |
| 163 | eth_set_current(); |
| 164 | /* Get hardware ready for send and receive operations */ |
| 165 | ret = eth_init(); |
| 166 | if (ret < 0) { |
| 167 | eth_halt(); |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 168 | this->mode->state = EFI_NETWORK_STOPPED; |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 169 | r = EFI_DEVICE_ERROR; |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 170 | goto out; |
| 171 | } else { |
Heinrich Schuchardt | 7f6d874 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 172 | this->int_status = 0; |
| 173 | wait_for_packet->is_signaled = false; |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 174 | this->mode->state = EFI_NETWORK_INITIALIZED; |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 175 | } |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 176 | out: |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 177 | return EFI_EXIT(r); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 180 | /* |
| 181 | * efi_net_reset() - reinitialize the network interface |
| 182 | * |
| 183 | * This function implements the Reset service of the |
| 184 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 185 | * (UEFI) specification for details. |
| 186 | * |
| 187 | * @this: pointer to the protocol instance |
| 188 | * @extended_verification: execute exhaustive verification |
| 189 | * Return: status code |
| 190 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 191 | static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this, |
| 192 | int extended_verification) |
| 193 | { |
Heinrich Schuchardt | 72a8f16 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 194 | efi_status_t ret; |
| 195 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 196 | EFI_ENTRY("%p, %x", this, extended_verification); |
| 197 | |
Heinrich Schuchardt | 72a8f16 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 198 | /* Check parameters */ |
| 199 | if (!this) { |
| 200 | ret = EFI_INVALID_PARAMETER; |
| 201 | goto out; |
| 202 | } |
| 203 | |
| 204 | switch (this->mode->state) { |
| 205 | case EFI_NETWORK_INITIALIZED: |
| 206 | break; |
| 207 | case EFI_NETWORK_STOPPED: |
| 208 | ret = EFI_NOT_STARTED; |
| 209 | goto out; |
| 210 | default: |
| 211 | ret = EFI_DEVICE_ERROR; |
| 212 | goto out; |
| 213 | } |
| 214 | |
| 215 | this->mode->state = EFI_NETWORK_STARTED; |
| 216 | ret = EFI_CALL(efi_net_initialize(this, 0, 0)); |
| 217 | out: |
| 218 | return EFI_EXIT(ret); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 221 | /* |
| 222 | * efi_net_shutdown() - shut down the network interface |
| 223 | * |
| 224 | * This function implements the Shutdown service of the |
| 225 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 226 | * (UEFI) specification for details. |
| 227 | * |
| 228 | * @this: pointer to the protocol instance |
| 229 | * Return: status code |
| 230 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 231 | static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this) |
| 232 | { |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 233 | efi_status_t ret = EFI_SUCCESS; |
| 234 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 235 | EFI_ENTRY("%p", this); |
| 236 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 237 | /* Check parameters */ |
| 238 | if (!this) { |
| 239 | ret = EFI_INVALID_PARAMETER; |
| 240 | goto out; |
| 241 | } |
| 242 | |
Heinrich Schuchardt | 72a8f16 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 243 | switch (this->mode->state) { |
| 244 | case EFI_NETWORK_INITIALIZED: |
| 245 | break; |
| 246 | case EFI_NETWORK_STOPPED: |
| 247 | ret = EFI_NOT_STARTED; |
| 248 | goto out; |
| 249 | default: |
| 250 | ret = EFI_DEVICE_ERROR; |
| 251 | goto out; |
| 252 | } |
| 253 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 254 | eth_halt(); |
Heinrich Schuchardt | 7f6d874 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 255 | this->int_status = 0; |
| 256 | wait_for_packet->is_signaled = false; |
Heinrich Schuchardt | 72a8f16 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 257 | this->mode->state = EFI_NETWORK_STARTED; |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 258 | |
| 259 | out: |
| 260 | return EFI_EXIT(ret); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 263 | /* |
| 264 | * efi_net_receive_filters() - mange multicast receive filters |
| 265 | * |
| 266 | * This function implements the ReceiveFilters service of the |
| 267 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 268 | * (UEFI) specification for details. |
| 269 | * |
| 270 | * @this: pointer to the protocol instance |
| 271 | * @enable: bit mask of receive filters to enable |
| 272 | * @disable: bit mask of receive filters to disable |
| 273 | * @reset_mcast_filter: true resets contents of the filters |
| 274 | * @mcast_filter_count: number of hardware MAC addresses in the new filters list |
| 275 | * @mcast_filter: list of new filters |
| 276 | * Return: status code |
| 277 | */ |
| 278 | static efi_status_t EFIAPI efi_net_receive_filters |
| 279 | (struct efi_simple_network *this, u32 enable, u32 disable, |
| 280 | int reset_mcast_filter, ulong mcast_filter_count, |
| 281 | struct efi_mac_address *mcast_filter) |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 282 | { |
| 283 | EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable, |
| 284 | reset_mcast_filter, mcast_filter_count, mcast_filter); |
| 285 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 286 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 289 | /* |
| 290 | * efi_net_station_address() - set the hardware MAC address |
| 291 | * |
| 292 | * This function implements the StationAddress service of the |
| 293 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 294 | * (UEFI) specification for details. |
| 295 | * |
| 296 | * @this: pointer to the protocol instance |
| 297 | * @reset: if true reset the address to default |
| 298 | * @new_mac: new MAC address |
| 299 | * Return: status code |
| 300 | */ |
| 301 | static efi_status_t EFIAPI efi_net_station_address |
| 302 | (struct efi_simple_network *this, int reset, |
| 303 | struct efi_mac_address *new_mac) |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 304 | { |
| 305 | EFI_ENTRY("%p, %x, %p", this, reset, new_mac); |
| 306 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 307 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 310 | /* |
| 311 | * efi_net_statistics() - reset or collect statistics of the network interface |
| 312 | * |
| 313 | * This function implements the Statistics service of the |
| 314 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 315 | * (UEFI) specification for details. |
| 316 | * |
| 317 | * @this: pointer to the protocol instance |
| 318 | * @reset: if true, the statistics are reset |
| 319 | * @stat_size: size of the statistics table |
| 320 | * @stat_table: table to receive the statistics |
| 321 | * Return: status code |
| 322 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 323 | static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this, |
| 324 | int reset, ulong *stat_size, |
| 325 | void *stat_table) |
| 326 | { |
| 327 | EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table); |
| 328 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 329 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 332 | /* |
| 333 | * efi_net_mcastiptomac() - translate multicast IP address to MAC address |
| 334 | * |
Heinrich Schuchardt | 5b4746f | 2019-09-01 17:17:53 +0200 | [diff] [blame] | 335 | * This function implements the MCastIPtoMAC service of the |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 336 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 337 | * (UEFI) specification for details. |
| 338 | * |
| 339 | * @this: pointer to the protocol instance |
| 340 | * @ipv6: true if the IP address is an IPv6 address |
| 341 | * @ip: IP address |
| 342 | * @mac: MAC address |
| 343 | * Return: status code |
| 344 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 345 | static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this, |
| 346 | int ipv6, |
| 347 | struct efi_ip_address *ip, |
| 348 | struct efi_mac_address *mac) |
| 349 | { |
Heinrich Schuchardt | 5b4746f | 2019-09-01 17:17:53 +0200 | [diff] [blame] | 350 | efi_status_t ret = EFI_SUCCESS; |
| 351 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 352 | EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac); |
| 353 | |
Heinrich Schuchardt | 5b4746f | 2019-09-01 17:17:53 +0200 | [diff] [blame] | 354 | if (!this || !ip || !mac) { |
| 355 | ret = EFI_INVALID_PARAMETER; |
| 356 | goto out; |
| 357 | } |
| 358 | |
| 359 | if (ipv6) { |
| 360 | ret = EFI_UNSUPPORTED; |
| 361 | goto out; |
| 362 | } |
| 363 | |
| 364 | /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */ |
| 365 | if ((ip->ip_addr[0] & 0xf0) != 0xe0) { |
| 366 | ret = EFI_INVALID_PARAMETER; |
| 367 | goto out; |
| 368 | }; |
| 369 | |
| 370 | switch (this->mode->state) { |
| 371 | case EFI_NETWORK_INITIALIZED: |
| 372 | case EFI_NETWORK_STARTED: |
| 373 | break; |
| 374 | default: |
| 375 | ret = EFI_NOT_STARTED; |
| 376 | goto out; |
| 377 | } |
| 378 | |
| 379 | memset(mac, 0, sizeof(struct efi_mac_address)); |
| 380 | |
| 381 | /* |
| 382 | * Copy lower 23 bits of IPv4 multi-cast address |
| 383 | * RFC 1112, RFC 7042 2.1.1. |
| 384 | */ |
| 385 | mac->mac_addr[0] = 0x01; |
| 386 | mac->mac_addr[1] = 0x00; |
| 387 | mac->mac_addr[2] = 0x5E; |
| 388 | mac->mac_addr[3] = ip->ip_addr[1] & 0x7F; |
| 389 | mac->mac_addr[4] = ip->ip_addr[2]; |
| 390 | mac->mac_addr[5] = ip->ip_addr[3]; |
| 391 | out: |
| 392 | return EFI_EXIT(ret); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 395 | /** |
| 396 | * efi_net_nvdata() - read or write NVRAM |
| 397 | * |
| 398 | * This function implements the GetStatus service of the Simple Network |
| 399 | * Protocol. See the UEFI spec for details. |
| 400 | * |
| 401 | * @this: the instance of the Simple Network Protocol |
Heinrich Schuchardt | fe1a81c | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 402 | * @read_write: true for read, false for write |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 403 | * @offset: offset in NVRAM |
| 404 | * @buffer_size: size of buffer |
| 405 | * @buffer: buffer |
| 406 | * Return: status code |
| 407 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 408 | static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this, |
| 409 | int read_write, ulong offset, |
| 410 | ulong buffer_size, char *buffer) |
| 411 | { |
| 412 | EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size, |
| 413 | buffer); |
| 414 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 415 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 416 | } |
| 417 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 418 | /** |
| 419 | * efi_net_get_status() - get interrupt status |
| 420 | * |
| 421 | * This function implements the GetStatus service of the Simple Network |
| 422 | * Protocol. See the UEFI spec for details. |
| 423 | * |
| 424 | * @this: the instance of the Simple Network Protocol |
| 425 | * @int_status: interface status |
| 426 | * @txbuf: transmission buffer |
| 427 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 428 | static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this, |
| 429 | u32 *int_status, void **txbuf) |
| 430 | { |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 431 | efi_status_t ret = EFI_SUCCESS; |
| 432 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 433 | EFI_ENTRY("%p, %p, %p", this, int_status, txbuf); |
| 434 | |
Heinrich Schuchardt | 891b3d9 | 2017-10-05 16:36:02 +0200 | [diff] [blame] | 435 | efi_timer_check(); |
| 436 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 437 | /* Check parameters */ |
| 438 | if (!this) { |
| 439 | ret = EFI_INVALID_PARAMETER; |
| 440 | goto out; |
| 441 | } |
| 442 | |
| 443 | switch (this->mode->state) { |
| 444 | case EFI_NETWORK_STOPPED: |
| 445 | ret = EFI_NOT_STARTED; |
| 446 | goto out; |
| 447 | case EFI_NETWORK_STARTED: |
| 448 | ret = EFI_DEVICE_ERROR; |
| 449 | goto out; |
| 450 | default: |
| 451 | break; |
| 452 | } |
| 453 | |
Heinrich Schuchardt | 891b3d9 | 2017-10-05 16:36:02 +0200 | [diff] [blame] | 454 | if (int_status) { |
Heinrich Schuchardt | 7f6d874 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 455 | *int_status = this->int_status; |
| 456 | this->int_status = 0; |
Heinrich Schuchardt | 891b3d9 | 2017-10-05 16:36:02 +0200 | [diff] [blame] | 457 | } |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 458 | if (txbuf) |
| 459 | *txbuf = new_tx_packet; |
| 460 | |
| 461 | new_tx_packet = NULL; |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 462 | out: |
| 463 | return EFI_EXIT(ret); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 466 | /** |
| 467 | * efi_net_transmit() - transmit a packet |
| 468 | * |
| 469 | * This function implements the Transmit service of the Simple Network Protocol. |
| 470 | * See the UEFI spec for details. |
| 471 | * |
| 472 | * @this: the instance of the Simple Network Protocol |
| 473 | * @header_size: size of the media header |
| 474 | * @buffer_size: size of the buffer to receive the packet |
| 475 | * @buffer: buffer to receive the packet |
| 476 | * @src_addr: source hardware MAC address |
| 477 | * @dest_addr: destination hardware MAC address |
| 478 | * @protocol: type of header to build |
| 479 | * Return: status code |
| 480 | */ |
| 481 | static efi_status_t EFIAPI efi_net_transmit |
| 482 | (struct efi_simple_network *this, size_t header_size, |
| 483 | size_t buffer_size, void *buffer, |
| 484 | struct efi_mac_address *src_addr, |
| 485 | struct efi_mac_address *dest_addr, u16 *protocol) |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 486 | { |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 487 | efi_status_t ret = EFI_SUCCESS; |
| 488 | |
Heinrich Schuchardt | 8db174d | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 489 | EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this, |
| 490 | (unsigned long)header_size, (unsigned long)buffer_size, |
| 491 | buffer, src_addr, dest_addr, protocol); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 492 | |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 493 | efi_timer_check(); |
| 494 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 495 | /* Check parameters */ |
Heinrich Schuchardt | ce54fdc | 2019-05-15 23:27:43 +0200 | [diff] [blame] | 496 | if (!this || !buffer) { |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 497 | ret = EFI_INVALID_PARAMETER; |
| 498 | goto out; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 499 | } |
| 500 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 501 | /* We do not support jumbo packets */ |
| 502 | if (buffer_size > PKTSIZE_ALIGN) { |
| 503 | ret = EFI_INVALID_PARAMETER; |
| 504 | goto out; |
| 505 | } |
| 506 | |
Heinrich Schuchardt | 5947b49 | 2019-08-31 10:55:29 +0200 | [diff] [blame] | 507 | /* At least the IP header has to fit into the buffer */ |
| 508 | if (buffer_size < this->mode->media_header_size) { |
| 509 | ret = EFI_BUFFER_TOO_SMALL; |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 510 | goto out; |
| 511 | } |
| 512 | |
Heinrich Schuchardt | 5947b49 | 2019-08-31 10:55:29 +0200 | [diff] [blame] | 513 | /* |
| 514 | * TODO: |
| 515 | * Support VLANs. Use net_set_ether() for copying the header. Use a |
| 516 | * U_BOOT_ENV_CALLBACK to update the media header size. |
| 517 | */ |
| 518 | if (header_size) { |
| 519 | struct ethernet_hdr *header = buffer; |
| 520 | |
| 521 | if (!dest_addr || !protocol || |
| 522 | header_size != this->mode->media_header_size) { |
| 523 | ret = EFI_INVALID_PARAMETER; |
| 524 | goto out; |
| 525 | } |
| 526 | if (!src_addr) |
| 527 | src_addr = &this->mode->current_address; |
| 528 | |
| 529 | memcpy(header->et_dest, dest_addr, ARP_HLEN); |
| 530 | memcpy(header->et_src, src_addr, ARP_HLEN); |
| 531 | header->et_protlen = htons(*protocol); |
| 532 | } |
| 533 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 534 | switch (this->mode->state) { |
| 535 | case EFI_NETWORK_STOPPED: |
| 536 | ret = EFI_NOT_STARTED; |
| 537 | goto out; |
| 538 | case EFI_NETWORK_STARTED: |
| 539 | ret = EFI_DEVICE_ERROR; |
| 540 | goto out; |
| 541 | default: |
| 542 | break; |
| 543 | } |
| 544 | |
| 545 | /* Ethernet packets always fit, just bounce */ |
Heinrich Schuchardt | 622fe62 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 546 | memcpy(transmit_buffer, buffer, buffer_size); |
| 547 | net_send_packet(transmit_buffer, buffer_size); |
Alexander Graf | 712cd29 | 2016-09-06 14:26:27 +0200 | [diff] [blame] | 548 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 549 | new_tx_packet = buffer; |
Heinrich Schuchardt | 7f6d874 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 550 | this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT; |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 551 | out: |
| 552 | return EFI_EXIT(ret); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 555 | /** |
| 556 | * efi_net_receive() - receive a packet from a network interface |
Heinrich Schuchardt | 8db174d | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 557 | * |
| 558 | * This function implements the Receive service of the Simple Network Protocol. |
| 559 | * See the UEFI spec for details. |
| 560 | * |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 561 | * @this: the instance of the Simple Network Protocol |
| 562 | * @header_size: size of the media header |
| 563 | * @buffer_size: size of the buffer to receive the packet |
| 564 | * @buffer: buffer to receive the packet |
| 565 | * @src_addr: source MAC address |
| 566 | * @dest_addr: destination MAC address |
| 567 | * @protocol: protocol |
| 568 | * Return: status code |
Heinrich Schuchardt | 8db174d | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 569 | */ |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 570 | static efi_status_t EFIAPI efi_net_receive |
| 571 | (struct efi_simple_network *this, size_t *header_size, |
| 572 | size_t *buffer_size, void *buffer, |
| 573 | struct efi_mac_address *src_addr, |
| 574 | struct efi_mac_address *dest_addr, u16 *protocol) |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 575 | { |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 576 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 336d9df | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 577 | struct ethernet_hdr *eth_hdr; |
| 578 | size_t hdr_size = sizeof(struct ethernet_hdr); |
| 579 | u16 protlen; |
| 580 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 581 | EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size, |
| 582 | buffer_size, buffer, src_addr, dest_addr, protocol); |
| 583 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 584 | /* Execute events */ |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 585 | efi_timer_check(); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 586 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 587 | /* Check parameters */ |
Heinrich Schuchardt | ce54fdc | 2019-05-15 23:27:43 +0200 | [diff] [blame] | 588 | if (!this || !buffer || !buffer_size) { |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 589 | ret = EFI_INVALID_PARAMETER; |
| 590 | goto out; |
| 591 | } |
| 592 | |
| 593 | switch (this->mode->state) { |
| 594 | case EFI_NETWORK_STOPPED: |
| 595 | ret = EFI_NOT_STARTED; |
| 596 | goto out; |
| 597 | case EFI_NETWORK_STARTED: |
| 598 | ret = EFI_DEVICE_ERROR; |
| 599 | goto out; |
| 600 | default: |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | if (!new_rx_packet) { |
| 605 | ret = EFI_NOT_READY; |
| 606 | goto out; |
| 607 | } |
Heinrich Schuchardt | 336d9df | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 608 | /* Fill export parameters */ |
| 609 | eth_hdr = (struct ethernet_hdr *)net_rx_packet; |
| 610 | protlen = ntohs(eth_hdr->et_protlen); |
| 611 | if (protlen == 0x8100) { |
| 612 | hdr_size += 4; |
| 613 | protlen = ntohs(*(u16 *)&net_rx_packet[hdr_size - 2]); |
| 614 | } |
| 615 | if (header_size) |
| 616 | *header_size = hdr_size; |
| 617 | if (dest_addr) |
| 618 | memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN); |
| 619 | if (src_addr) |
| 620 | memcpy(src_addr, eth_hdr->et_src, ARP_HLEN); |
| 621 | if (protocol) |
| 622 | *protocol = protlen; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 623 | if (*buffer_size < net_rx_packet_len) { |
Heinrich Schuchardt | e1fec15 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 624 | /* Packet doesn't fit, try again with bigger buffer */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 625 | *buffer_size = net_rx_packet_len; |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 626 | ret = EFI_BUFFER_TOO_SMALL; |
| 627 | goto out; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 628 | } |
Heinrich Schuchardt | 336d9df | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 629 | /* Copy packet */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 630 | memcpy(buffer, net_rx_packet, net_rx_packet_len); |
| 631 | *buffer_size = net_rx_packet_len; |
Heinrich Schuchardt | 7f6d874 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 632 | new_rx_packet = 0; |
| 633 | this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT; |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 634 | out: |
| 635 | return EFI_EXIT(ret); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 638 | /** |
| 639 | * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address |
| 640 | * |
| 641 | * This function is called by dhcp_handler(). |
Heinrich Schuchardt | fe1a81c | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 642 | * |
| 643 | * @pkt: packet received by dhcp_handler() |
| 644 | * @len: length of the packet received |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 645 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 646 | void efi_net_set_dhcp_ack(void *pkt, int len) |
| 647 | { |
| 648 | int maxsize = sizeof(*dhcp_ack); |
| 649 | |
| 650 | if (!dhcp_ack) |
| 651 | dhcp_ack = malloc(maxsize); |
| 652 | |
| 653 | memcpy(dhcp_ack, pkt, min(len, maxsize)); |
| 654 | } |
| 655 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 656 | /** |
| 657 | * efi_net_push() - callback for received network packet |
| 658 | * |
| 659 | * This function is called when a network packet is received by eth_rx(). |
| 660 | * |
| 661 | * @pkt: network packet |
| 662 | * @len: length |
| 663 | */ |
| 664 | static void efi_net_push(void *pkt, int len) |
| 665 | { |
| 666 | new_rx_packet = true; |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /** |
| 670 | * efi_network_timer_notify() - check if a new network packet has been received |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 671 | * |
| 672 | * This notification function is called in every timer cycle. |
| 673 | * |
Heinrich Schuchardt | fe1a81c | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 674 | * @event: the event for which this notification function is registered |
| 675 | * @context: event context - not used in this function |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 676 | */ |
| 677 | static void EFIAPI efi_network_timer_notify(struct efi_event *event, |
| 678 | void *context) |
| 679 | { |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 680 | struct efi_simple_network *this = (struct efi_simple_network *)context; |
| 681 | |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 682 | EFI_ENTRY("%p, %p", event, context); |
| 683 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 684 | /* |
| 685 | * Some network drivers do not support calling eth_rx() before |
| 686 | * initialization. |
| 687 | */ |
| 688 | if (!this || this->mode->state != EFI_NETWORK_INITIALIZED) |
| 689 | goto out; |
| 690 | |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 691 | if (!new_rx_packet) { |
| 692 | push_packet = efi_net_push; |
| 693 | eth_rx(); |
| 694 | push_packet = NULL; |
Heinrich Schuchardt | 7f6d874 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 695 | if (new_rx_packet) { |
| 696 | /* Check that we at least received an Ethernet header */ |
| 697 | if (net_rx_packet_len >= |
| 698 | sizeof(struct ethernet_hdr)) { |
| 699 | this->int_status |= |
| 700 | EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT; |
| 701 | wait_for_packet->is_signaled = true; |
| 702 | } else { |
| 703 | new_rx_packet = 0; |
| 704 | } |
| 705 | } |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 706 | } |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 707 | out: |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 708 | EFI_EXIT(EFI_SUCCESS); |
| 709 | } |
| 710 | |
Heinrich Schuchardt | a6d3709 | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 711 | static efi_status_t EFIAPI efi_pxe_base_code_start( |
| 712 | struct efi_pxe_base_code_protocol *this, |
| 713 | u8 use_ipv6) |
| 714 | { |
| 715 | return EFI_UNSUPPORTED; |
| 716 | } |
| 717 | |
| 718 | static efi_status_t EFIAPI efi_pxe_base_code_stop( |
| 719 | struct efi_pxe_base_code_protocol *this) |
| 720 | { |
| 721 | return EFI_UNSUPPORTED; |
| 722 | } |
| 723 | |
| 724 | static efi_status_t EFIAPI efi_pxe_base_code_dhcp( |
| 725 | struct efi_pxe_base_code_protocol *this, |
| 726 | u8 sort_offers) |
| 727 | { |
| 728 | return EFI_UNSUPPORTED; |
| 729 | } |
| 730 | |
| 731 | static efi_status_t EFIAPI efi_pxe_base_code_discover( |
| 732 | struct efi_pxe_base_code_protocol *this, |
| 733 | u16 type, u16 *layer, u8 bis, |
| 734 | struct efi_pxe_base_code_discover_info *info) |
| 735 | { |
| 736 | return EFI_UNSUPPORTED; |
| 737 | } |
| 738 | |
| 739 | static efi_status_t EFIAPI efi_pxe_base_code_mtftp( |
| 740 | struct efi_pxe_base_code_protocol *this, |
| 741 | u32 operation, void *buffer_ptr, |
| 742 | u8 overwrite, efi_uintn_t *buffer_size, |
| 743 | struct efi_ip_address server_ip, char *filename, |
| 744 | struct efi_pxe_base_code_mtftp_info *info, |
| 745 | u8 dont_use_buffer) |
| 746 | { |
| 747 | return EFI_UNSUPPORTED; |
| 748 | } |
| 749 | |
| 750 | static efi_status_t EFIAPI efi_pxe_base_code_udp_write( |
| 751 | struct efi_pxe_base_code_protocol *this, |
| 752 | u16 op_flags, struct efi_ip_address *dest_ip, |
| 753 | u16 *dest_port, |
| 754 | struct efi_ip_address *gateway_ip, |
| 755 | struct efi_ip_address *src_ip, u16 *src_port, |
| 756 | efi_uintn_t *header_size, void *header_ptr, |
| 757 | efi_uintn_t *buffer_size, void *buffer_ptr) |
| 758 | { |
| 759 | return EFI_UNSUPPORTED; |
| 760 | } |
| 761 | |
| 762 | static efi_status_t EFIAPI efi_pxe_base_code_udp_read( |
| 763 | struct efi_pxe_base_code_protocol *this, |
| 764 | u16 op_flags, struct efi_ip_address *dest_ip, |
| 765 | u16 *dest_port, struct efi_ip_address *src_ip, |
| 766 | u16 *src_port, efi_uintn_t *header_size, |
| 767 | void *header_ptr, efi_uintn_t *buffer_size, |
| 768 | void *buffer_ptr) |
| 769 | { |
| 770 | return EFI_UNSUPPORTED; |
| 771 | } |
| 772 | |
| 773 | static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter( |
| 774 | struct efi_pxe_base_code_protocol *this, |
| 775 | struct efi_pxe_base_code_filter *new_filter) |
| 776 | { |
| 777 | return EFI_UNSUPPORTED; |
| 778 | } |
| 779 | |
| 780 | static efi_status_t EFIAPI efi_pxe_base_code_arp( |
| 781 | struct efi_pxe_base_code_protocol *this, |
| 782 | struct efi_ip_address *ip_addr, |
| 783 | struct efi_mac_address *mac_addr) |
| 784 | { |
| 785 | return EFI_UNSUPPORTED; |
| 786 | } |
| 787 | |
| 788 | static efi_status_t EFIAPI efi_pxe_base_code_set_parameters( |
| 789 | struct efi_pxe_base_code_protocol *this, |
| 790 | u8 *new_auto_arp, u8 *new_send_guid, |
| 791 | u8 *new_ttl, u8 *new_tos, |
| 792 | u8 *new_make_callback) |
| 793 | { |
| 794 | return EFI_UNSUPPORTED; |
| 795 | } |
| 796 | |
| 797 | static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip( |
| 798 | struct efi_pxe_base_code_protocol *this, |
| 799 | struct efi_ip_address *new_station_ip, |
| 800 | struct efi_ip_address *new_subnet_mask) |
| 801 | { |
| 802 | return EFI_UNSUPPORTED; |
| 803 | } |
| 804 | |
| 805 | static efi_status_t EFIAPI efi_pxe_base_code_set_packets( |
| 806 | struct efi_pxe_base_code_protocol *this, |
| 807 | u8 *new_dhcp_discover_valid, |
| 808 | u8 *new_dhcp_ack_received, |
| 809 | u8 *new_proxy_offer_received, |
| 810 | u8 *new_pxe_discover_valid, |
| 811 | u8 *new_pxe_reply_received, |
| 812 | u8 *new_pxe_bis_reply_received, |
| 813 | EFI_PXE_BASE_CODE_PACKET *new_dchp_discover, |
| 814 | EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc, |
| 815 | EFI_PXE_BASE_CODE_PACKET *new_proxy_offer, |
| 816 | EFI_PXE_BASE_CODE_PACKET *new_pxe_discover, |
| 817 | EFI_PXE_BASE_CODE_PACKET *new_pxe_reply, |
| 818 | EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply) |
| 819 | { |
| 820 | return EFI_UNSUPPORTED; |
| 821 | } |
| 822 | |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 823 | /** |
| 824 | * efi_net_register() - register the simple network protocol |
| 825 | * |
| 826 | * This gets called from do_bootefi_exec(). |
| 827 | */ |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 828 | efi_status_t efi_net_register(void) |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 829 | { |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 830 | struct efi_net_obj *netobj = NULL; |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 831 | efi_status_t r; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 832 | |
| 833 | if (!eth_get_dev()) { |
Heinrich Schuchardt | e1fec15 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 834 | /* No network device active, don't expose any */ |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 835 | return EFI_SUCCESS; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 836 | } |
| 837 | |
Heinrich Schuchardt | e1fec15 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 838 | /* We only expose the "active" network device, so one is enough */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 839 | netobj = calloc(1, sizeof(*netobj)); |
Heinrich Schuchardt | 622fe62 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 840 | if (!netobj) |
| 841 | goto out_of_resources; |
| 842 | |
| 843 | /* Allocate an aligned transmit buffer */ |
| 844 | transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN); |
| 845 | if (!transmit_buffer) |
| 846 | goto out_of_resources; |
| 847 | transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN); |
Heinrich Schuchardt | 84d1456 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 848 | |
| 849 | /* Hook net up to the device list */ |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 850 | efi_add_handle(&netobj->header); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 851 | |
| 852 | /* Fill in object data */ |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 853 | r = efi_add_protocol(&netobj->header, &efi_net_guid, |
Heinrich Schuchardt | 84d1456 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 854 | &netobj->net); |
| 855 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 856 | goto failure_to_add_protocol; |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 857 | r = efi_add_protocol(&netobj->header, &efi_guid_device_path, |
Heinrich Schuchardt | 84d1456 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 858 | efi_dp_from_eth()); |
| 859 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 860 | goto failure_to_add_protocol; |
Heinrich Schuchardt | a6d3709 | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 861 | r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid, |
Heinrich Schuchardt | 84d1456 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 862 | &netobj->pxe); |
| 863 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 864 | goto failure_to_add_protocol; |
Heinrich Schuchardt | bdecf97 | 2017-10-05 16:35:57 +0200 | [diff] [blame] | 865 | netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 866 | netobj->net.start = efi_net_start; |
| 867 | netobj->net.stop = efi_net_stop; |
| 868 | netobj->net.initialize = efi_net_initialize; |
| 869 | netobj->net.reset = efi_net_reset; |
| 870 | netobj->net.shutdown = efi_net_shutdown; |
| 871 | netobj->net.receive_filters = efi_net_receive_filters; |
| 872 | netobj->net.station_address = efi_net_station_address; |
| 873 | netobj->net.statistics = efi_net_statistics; |
| 874 | netobj->net.mcastiptomac = efi_net_mcastiptomac; |
| 875 | netobj->net.nvdata = efi_net_nvdata; |
| 876 | netobj->net.get_status = efi_net_get_status; |
| 877 | netobj->net.transmit = efi_net_transmit; |
| 878 | netobj->net.receive = efi_net_receive; |
| 879 | netobj->net.mode = &netobj->net_mode; |
Heinrich Schuchardt | 72a8f16 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 880 | netobj->net_mode.state = EFI_NETWORK_STOPPED; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 881 | memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6); |
Heinrich Schuchardt | 5d4a5ea | 2017-10-05 16:35:58 +0200 | [diff] [blame] | 882 | netobj->net_mode.hwaddr_size = ARP_HLEN; |
Heinrich Schuchardt | 5947b49 | 2019-08-31 10:55:29 +0200 | [diff] [blame] | 883 | netobj->net_mode.media_header_size = ETHER_HDR_SIZE; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 884 | netobj->net_mode.max_packet_size = PKTSIZE; |
Andrew Thomas | f25ddca | 2018-06-21 16:21:01 -0700 | [diff] [blame] | 885 | netobj->net_mode.if_type = ARP_ETHER; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 886 | |
Heinrich Schuchardt | a6d3709 | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 887 | netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION; |
| 888 | netobj->pxe.start = efi_pxe_base_code_start; |
| 889 | netobj->pxe.stop = efi_pxe_base_code_stop; |
| 890 | netobj->pxe.dhcp = efi_pxe_base_code_dhcp; |
| 891 | netobj->pxe.discover = efi_pxe_base_code_discover; |
| 892 | netobj->pxe.mtftp = efi_pxe_base_code_mtftp; |
| 893 | netobj->pxe.udp_write = efi_pxe_base_code_udp_write; |
| 894 | netobj->pxe.udp_read = efi_pxe_base_code_udp_read; |
| 895 | netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter; |
| 896 | netobj->pxe.arp = efi_pxe_base_code_arp; |
| 897 | netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters; |
| 898 | netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip; |
| 899 | netobj->pxe.set_packets = efi_pxe_base_code_set_packets; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 900 | netobj->pxe.mode = &netobj->pxe_mode; |
| 901 | if (dhcp_ack) |
| 902 | netobj->pxe_mode.dhcp_ack = *dhcp_ack; |
| 903 | |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 904 | /* |
Heinrich Schuchardt | e5c2160 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 905 | * Create WaitForPacket event. |
| 906 | */ |
| 907 | r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 908 | efi_network_timer_notify, NULL, NULL, |
Heinrich Schuchardt | e5c2160 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 909 | &wait_for_packet); |
| 910 | if (r != EFI_SUCCESS) { |
| 911 | printf("ERROR: Failed to register network event\n"); |
| 912 | return r; |
| 913 | } |
| 914 | netobj->net.wait_for_packet = wait_for_packet; |
| 915 | /* |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 916 | * Create a timer event. |
| 917 | * |
| 918 | * The notification function is used to check if a new network packet |
| 919 | * has been received. |
Heinrich Schuchardt | ee3db4f | 2018-03-24 18:40:22 +0100 | [diff] [blame] | 920 | * |
| 921 | * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL. |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 922 | */ |
Heinrich Schuchardt | ee3db4f | 2018-03-24 18:40:22 +0100 | [diff] [blame] | 923 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY, |
Heinrich Schuchardt | 41b0587 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 924 | efi_network_timer_notify, &netobj->net, NULL, |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 925 | &network_timer_event); |
| 926 | if (r != EFI_SUCCESS) { |
| 927 | printf("ERROR: Failed to register network event\n"); |
| 928 | return r; |
| 929 | } |
Heinrich Schuchardt | e1fec15 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 930 | /* Network is time critical, create event in every timer cycle */ |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 931 | r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0); |
| 932 | if (r != EFI_SUCCESS) { |
| 933 | printf("ERROR: Failed to set network timer\n"); |
| 934 | return r; |
| 935 | } |
| 936 | |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 937 | return EFI_SUCCESS; |
| 938 | failure_to_add_protocol: |
| 939 | printf("ERROR: Failure to add protocol\n"); |
| 940 | return r; |
Heinrich Schuchardt | 622fe62 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 941 | out_of_resources: |
| 942 | free(netobj); |
| 943 | /* free(transmit_buffer) not needed yet */ |
| 944 | printf("ERROR: Out of memory\n"); |
| 945 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 946 | } |