Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 2 | /* |
| 3 | * EFI application network access support |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <efi_loader.h> |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 12 | static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID; |
| 13 | static const efi_guid_t efi_pxe_guid = EFI_PXE_GUID; |
| 14 | static struct efi_pxe_packet *dhcp_ack; |
| 15 | static bool new_rx_packet; |
| 16 | static void *new_tx_packet; |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame^] | 17 | static void *transmit_buffer; |
| 18 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 19 | /* |
| 20 | * The notification function of this event is called in every timer cycle |
| 21 | * to check if a new network packet has been received. |
| 22 | */ |
| 23 | static struct efi_event *network_timer_event; |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 24 | /* |
| 25 | * This event is signaled when a packet has been received. |
| 26 | */ |
| 27 | static struct efi_event *wait_for_packet; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 28 | |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 29 | /** |
| 30 | * struct efi_net_obj - EFI object representing a network interface |
| 31 | * |
| 32 | * @header: EFI object header |
| 33 | * @net: simple network protocol interface |
| 34 | * @net_mode: status of the network interface |
| 35 | * @pxe: PXE base code protocol interface |
| 36 | * @pxe_mode: status of the PXE base code protocol |
| 37 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 38 | struct efi_net_obj { |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 39 | struct efi_object header; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 40 | struct efi_simple_network net; |
| 41 | struct efi_simple_network_mode net_mode; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 42 | struct efi_pxe pxe; |
| 43 | struct efi_pxe_mode pxe_mode; |
| 44 | }; |
| 45 | |
| 46 | static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this) |
| 47 | { |
| 48 | EFI_ENTRY("%p", this); |
| 49 | |
| 50 | return EFI_EXIT(EFI_SUCCESS); |
| 51 | } |
| 52 | |
| 53 | static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this) |
| 54 | { |
| 55 | EFI_ENTRY("%p", this); |
| 56 | |
| 57 | return EFI_EXIT(EFI_SUCCESS); |
| 58 | } |
| 59 | |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 60 | /* |
| 61 | * Initialize network adapter and allocate transmit and receive buffers. |
| 62 | * |
| 63 | * This function implements the Initialize service of the |
| 64 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 65 | * (UEFI) specification for details. |
| 66 | * |
| 67 | * @this: pointer to the protocol instance |
| 68 | * @extra_rx: extra receive buffer to be allocated |
| 69 | * @extra_tx: extra transmit buffer to be allocated |
| 70 | * @return: status code |
| 71 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 72 | static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this, |
| 73 | ulong extra_rx, ulong extra_tx) |
| 74 | { |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 75 | int ret; |
| 76 | efi_status_t r = EFI_SUCCESS; |
| 77 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 78 | EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx); |
| 79 | |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 80 | if (!this) { |
| 81 | r = EFI_INVALID_PARAMETER; |
| 82 | goto error; |
| 83 | } |
| 84 | |
| 85 | /* Setup packet buffers */ |
| 86 | net_init(); |
| 87 | /* Disable hardware and put it into the reset state */ |
| 88 | eth_halt(); |
| 89 | /* Set current device according to environment variables */ |
| 90 | eth_set_current(); |
| 91 | /* Get hardware ready for send and receive operations */ |
| 92 | ret = eth_init(); |
| 93 | if (ret < 0) { |
| 94 | eth_halt(); |
| 95 | r = EFI_DEVICE_ERROR; |
| 96 | } |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 97 | |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 98 | error: |
| 99 | return EFI_EXIT(r); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this, |
| 103 | int extended_verification) |
| 104 | { |
| 105 | EFI_ENTRY("%p, %x", this, extended_verification); |
| 106 | |
| 107 | return EFI_EXIT(EFI_SUCCESS); |
| 108 | } |
| 109 | |
| 110 | static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this) |
| 111 | { |
| 112 | EFI_ENTRY("%p", this); |
| 113 | |
| 114 | return EFI_EXIT(EFI_SUCCESS); |
| 115 | } |
| 116 | |
| 117 | static efi_status_t EFIAPI efi_net_receive_filters( |
| 118 | struct efi_simple_network *this, u32 enable, u32 disable, |
| 119 | int reset_mcast_filter, ulong mcast_filter_count, |
| 120 | struct efi_mac_address *mcast_filter) |
| 121 | { |
| 122 | EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable, |
| 123 | reset_mcast_filter, mcast_filter_count, mcast_filter); |
| 124 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 125 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static efi_status_t EFIAPI efi_net_station_address( |
| 129 | struct efi_simple_network *this, int reset, |
| 130 | struct efi_mac_address *new_mac) |
| 131 | { |
| 132 | EFI_ENTRY("%p, %x, %p", this, reset, new_mac); |
| 133 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 134 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this, |
| 138 | int reset, ulong *stat_size, |
| 139 | void *stat_table) |
| 140 | { |
| 141 | EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table); |
| 142 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 143 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this, |
| 147 | int ipv6, |
| 148 | struct efi_ip_address *ip, |
| 149 | struct efi_mac_address *mac) |
| 150 | { |
| 151 | EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac); |
| 152 | |
| 153 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 154 | } |
| 155 | |
| 156 | static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this, |
| 157 | int read_write, ulong offset, |
| 158 | ulong buffer_size, char *buffer) |
| 159 | { |
| 160 | EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size, |
| 161 | buffer); |
| 162 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 163 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this, |
| 167 | u32 *int_status, void **txbuf) |
| 168 | { |
| 169 | EFI_ENTRY("%p, %p, %p", this, int_status, txbuf); |
| 170 | |
Heinrich Schuchardt | e371c5f | 2017-10-05 16:36:02 +0200 | [diff] [blame] | 171 | efi_timer_check(); |
| 172 | |
| 173 | if (int_status) { |
| 174 | /* We send packets synchronously, so nothing is outstanding */ |
| 175 | *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT; |
| 176 | if (new_rx_packet) |
| 177 | *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT; |
| 178 | } |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 179 | if (txbuf) |
| 180 | *txbuf = new_tx_packet; |
| 181 | |
| 182 | new_tx_packet = NULL; |
| 183 | |
| 184 | return EFI_EXIT(EFI_SUCCESS); |
| 185 | } |
| 186 | |
| 187 | static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this, |
Heinrich Schuchardt | f66b2e3 | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 188 | size_t header_size, size_t buffer_size, void *buffer, |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 189 | struct efi_mac_address *src_addr, |
| 190 | struct efi_mac_address *dest_addr, u16 *protocol) |
| 191 | { |
Heinrich Schuchardt | f66b2e3 | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 192 | EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this, |
| 193 | (unsigned long)header_size, (unsigned long)buffer_size, |
| 194 | buffer, src_addr, dest_addr, protocol); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 195 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 196 | efi_timer_check(); |
| 197 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 198 | if (header_size) { |
| 199 | /* We would need to create the header if header_size != 0 */ |
| 200 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 201 | } |
| 202 | |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame^] | 203 | /* Ensure that the packet fits into the buffer */ |
| 204 | if (buffer_size > PKTSIZE_ALIGN) |
| 205 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 206 | memcpy(transmit_buffer, buffer, buffer_size); |
| 207 | net_send_packet(transmit_buffer, buffer_size); |
Alexander Graf | 0b12b86 | 2016-09-06 14:26:27 +0200 | [diff] [blame] | 208 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 209 | new_tx_packet = buffer; |
| 210 | |
| 211 | return EFI_EXIT(EFI_SUCCESS); |
| 212 | } |
| 213 | |
| 214 | static void efi_net_push(void *pkt, int len) |
| 215 | { |
| 216 | new_rx_packet = true; |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 217 | wait_for_packet->is_signaled = true; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Heinrich Schuchardt | f66b2e3 | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 220 | /* |
| 221 | * Receive a packet from a network interface. |
| 222 | * |
| 223 | * This function implements the Receive service of the Simple Network Protocol. |
| 224 | * See the UEFI spec for details. |
| 225 | * |
| 226 | * @this the instance of the Simple Network Protocol |
| 227 | * @header_size size of the media header |
| 228 | * @buffer_size size of the buffer to receive the packet |
| 229 | * @buffer buffer to receive the packet |
| 230 | * @src_addr source MAC address |
| 231 | * @dest_addr destination MAC address |
| 232 | * @protocol protocol |
| 233 | * @return status code |
| 234 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 235 | static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this, |
Heinrich Schuchardt | f66b2e3 | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 236 | size_t *header_size, size_t *buffer_size, void *buffer, |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 237 | struct efi_mac_address *src_addr, |
| 238 | struct efi_mac_address *dest_addr, u16 *protocol) |
| 239 | { |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 240 | struct ethernet_hdr *eth_hdr; |
| 241 | size_t hdr_size = sizeof(struct ethernet_hdr); |
| 242 | u16 protlen; |
| 243 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 244 | EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size, |
| 245 | buffer_size, buffer, src_addr, dest_addr, protocol); |
| 246 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 247 | efi_timer_check(); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 248 | |
| 249 | if (!new_rx_packet) |
| 250 | return EFI_EXIT(EFI_NOT_READY); |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 251 | /* Check that we at least received an Ethernet header */ |
| 252 | if (net_rx_packet_len < sizeof(struct ethernet_hdr)) { |
| 253 | new_rx_packet = false; |
| 254 | return EFI_EXIT(EFI_NOT_READY); |
| 255 | } |
| 256 | /* Fill export parameters */ |
| 257 | eth_hdr = (struct ethernet_hdr *)net_rx_packet; |
| 258 | protlen = ntohs(eth_hdr->et_protlen); |
| 259 | if (protlen == 0x8100) { |
| 260 | hdr_size += 4; |
| 261 | protlen = ntohs(*(u16 *)&net_rx_packet[hdr_size - 2]); |
| 262 | } |
| 263 | if (header_size) |
| 264 | *header_size = hdr_size; |
| 265 | if (dest_addr) |
| 266 | memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN); |
| 267 | if (src_addr) |
| 268 | memcpy(src_addr, eth_hdr->et_src, ARP_HLEN); |
| 269 | if (protocol) |
| 270 | *protocol = protlen; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 271 | if (*buffer_size < net_rx_packet_len) { |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 272 | /* Packet doesn't fit, try again with bigger buffer */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 273 | *buffer_size = net_rx_packet_len; |
| 274 | return EFI_EXIT(EFI_BUFFER_TOO_SMALL); |
| 275 | } |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 276 | /* Copy packet */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 277 | memcpy(buffer, net_rx_packet, net_rx_packet_len); |
| 278 | *buffer_size = net_rx_packet_len; |
| 279 | new_rx_packet = false; |
| 280 | |
| 281 | return EFI_EXIT(EFI_SUCCESS); |
| 282 | } |
| 283 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 284 | void efi_net_set_dhcp_ack(void *pkt, int len) |
| 285 | { |
| 286 | int maxsize = sizeof(*dhcp_ack); |
| 287 | |
| 288 | if (!dhcp_ack) |
| 289 | dhcp_ack = malloc(maxsize); |
| 290 | |
| 291 | memcpy(dhcp_ack, pkt, min(len, maxsize)); |
| 292 | } |
| 293 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 294 | /* |
| 295 | * Check if a new network packet has been received. |
| 296 | * |
| 297 | * This notification function is called in every timer cycle. |
| 298 | * |
| 299 | * @event the event for which this notification function is registered |
| 300 | * @context event context - not used in this function |
| 301 | */ |
| 302 | static void EFIAPI efi_network_timer_notify(struct efi_event *event, |
| 303 | void *context) |
| 304 | { |
| 305 | EFI_ENTRY("%p, %p", event, context); |
| 306 | |
| 307 | if (!new_rx_packet) { |
| 308 | push_packet = efi_net_push; |
| 309 | eth_rx(); |
| 310 | push_packet = NULL; |
| 311 | } |
| 312 | EFI_EXIT(EFI_SUCCESS); |
| 313 | } |
| 314 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 315 | /* This gets called from do_bootefi_exec(). */ |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 316 | efi_status_t efi_net_register(void) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 317 | { |
| 318 | struct efi_net_obj *netobj; |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 319 | efi_status_t r; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 320 | |
| 321 | if (!eth_get_dev()) { |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 322 | /* No network device active, don't expose any */ |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 323 | return EFI_SUCCESS; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 326 | /* We only expose the "active" network device, so one is enough */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 327 | netobj = calloc(1, sizeof(*netobj)); |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame^] | 328 | if (!netobj) |
| 329 | goto out_of_resources; |
| 330 | |
| 331 | /* Allocate an aligned transmit buffer */ |
| 332 | transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN); |
| 333 | if (!transmit_buffer) |
| 334 | goto out_of_resources; |
| 335 | transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN); |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 336 | |
| 337 | /* Hook net up to the device list */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 338 | efi_add_handle(&netobj->header); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 339 | |
| 340 | /* Fill in object data */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 341 | r = efi_add_protocol(&netobj->header, &efi_net_guid, |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 342 | &netobj->net); |
| 343 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 344 | goto failure_to_add_protocol; |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 345 | r = efi_add_protocol(&netobj->header, &efi_guid_device_path, |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 346 | efi_dp_from_eth()); |
| 347 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 348 | goto failure_to_add_protocol; |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 349 | r = efi_add_protocol(&netobj->header, &efi_pxe_guid, |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 350 | &netobj->pxe); |
| 351 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 352 | goto failure_to_add_protocol; |
Heinrich Schuchardt | d54396a | 2017-10-05 16:35:57 +0200 | [diff] [blame] | 353 | netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 354 | netobj->net.start = efi_net_start; |
| 355 | netobj->net.stop = efi_net_stop; |
| 356 | netobj->net.initialize = efi_net_initialize; |
| 357 | netobj->net.reset = efi_net_reset; |
| 358 | netobj->net.shutdown = efi_net_shutdown; |
| 359 | netobj->net.receive_filters = efi_net_receive_filters; |
| 360 | netobj->net.station_address = efi_net_station_address; |
| 361 | netobj->net.statistics = efi_net_statistics; |
| 362 | netobj->net.mcastiptomac = efi_net_mcastiptomac; |
| 363 | netobj->net.nvdata = efi_net_nvdata; |
| 364 | netobj->net.get_status = efi_net_get_status; |
| 365 | netobj->net.transmit = efi_net_transmit; |
| 366 | netobj->net.receive = efi_net_receive; |
| 367 | netobj->net.mode = &netobj->net_mode; |
| 368 | netobj->net_mode.state = EFI_NETWORK_STARTED; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 369 | memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6); |
Heinrich Schuchardt | d1cc6cb | 2017-10-05 16:35:58 +0200 | [diff] [blame] | 370 | netobj->net_mode.hwaddr_size = ARP_HLEN; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 371 | netobj->net_mode.max_packet_size = PKTSIZE; |
Andrew Thomas | 27df0a7 | 2018-06-21 16:21:01 -0700 | [diff] [blame] | 372 | netobj->net_mode.if_type = ARP_ETHER; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 373 | |
| 374 | netobj->pxe.mode = &netobj->pxe_mode; |
| 375 | if (dhcp_ack) |
| 376 | netobj->pxe_mode.dhcp_ack = *dhcp_ack; |
| 377 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 378 | /* |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 379 | * Create WaitForPacket event. |
| 380 | */ |
| 381 | r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 382 | efi_network_timer_notify, NULL, NULL, |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 383 | &wait_for_packet); |
| 384 | if (r != EFI_SUCCESS) { |
| 385 | printf("ERROR: Failed to register network event\n"); |
| 386 | return r; |
| 387 | } |
| 388 | netobj->net.wait_for_packet = wait_for_packet; |
| 389 | /* |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 390 | * Create a timer event. |
| 391 | * |
| 392 | * The notification function is used to check if a new network packet |
| 393 | * has been received. |
Heinrich Schuchardt | 86df48c | 2018-03-24 18:40:22 +0100 | [diff] [blame] | 394 | * |
| 395 | * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL. |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 396 | */ |
Heinrich Schuchardt | 86df48c | 2018-03-24 18:40:22 +0100 | [diff] [blame] | 397 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY, |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 398 | efi_network_timer_notify, NULL, NULL, |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 399 | &network_timer_event); |
| 400 | if (r != EFI_SUCCESS) { |
| 401 | printf("ERROR: Failed to register network event\n"); |
| 402 | return r; |
| 403 | } |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 404 | /* Network is time critical, create event in every timer cycle */ |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 405 | r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0); |
| 406 | if (r != EFI_SUCCESS) { |
| 407 | printf("ERROR: Failed to set network timer\n"); |
| 408 | return r; |
| 409 | } |
| 410 | |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 411 | return EFI_SUCCESS; |
| 412 | failure_to_add_protocol: |
| 413 | printf("ERROR: Failure to add protocol\n"); |
| 414 | return r; |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame^] | 415 | out_of_resources: |
| 416 | free(netobj); |
| 417 | /* free(transmit_buffer) not needed yet */ |
| 418 | printf("ERROR: Out of memory\n"); |
| 419 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 420 | } |