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