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 | |
Alexander Graf | 0efe1bc | 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 | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 17 | /* |
| 18 | * The notification function of this event is called in every timer cycle |
| 19 | * to check if a new network packet has been received. |
| 20 | */ |
| 21 | static struct efi_event *network_timer_event; |
Heinrich Schuchardt | e5c2160 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 22 | /* |
| 23 | * This event is signaled when a packet has been received. |
| 24 | */ |
| 25 | static struct efi_event *wait_for_packet; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 26 | |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 27 | /** |
| 28 | * struct efi_net_obj - EFI object representing a network interface |
| 29 | * |
| 30 | * @header: EFI object header |
| 31 | * @net: simple network protocol interface |
| 32 | * @net_mode: status of the network interface |
| 33 | * @pxe: PXE base code protocol interface |
| 34 | * @pxe_mode: status of the PXE base code protocol |
| 35 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 36 | struct efi_net_obj { |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 37 | struct efi_object header; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 38 | struct efi_simple_network net; |
| 39 | struct efi_simple_network_mode net_mode; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 40 | struct efi_pxe pxe; |
| 41 | struct efi_pxe_mode pxe_mode; |
| 42 | }; |
| 43 | |
| 44 | static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this) |
| 45 | { |
| 46 | EFI_ENTRY("%p", this); |
| 47 | |
| 48 | return EFI_EXIT(EFI_SUCCESS); |
| 49 | } |
| 50 | |
| 51 | static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this) |
| 52 | { |
| 53 | EFI_ENTRY("%p", this); |
| 54 | |
| 55 | return EFI_EXIT(EFI_SUCCESS); |
| 56 | } |
| 57 | |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 58 | /* |
| 59 | * Initialize network adapter and allocate transmit and receive buffers. |
| 60 | * |
| 61 | * This function implements the Initialize service of the |
| 62 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 63 | * (UEFI) specification for details. |
| 64 | * |
| 65 | * @this: pointer to the protocol instance |
| 66 | * @extra_rx: extra receive buffer to be allocated |
| 67 | * @extra_tx: extra transmit buffer to be allocated |
| 68 | * @return: status code |
| 69 | */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 70 | static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this, |
| 71 | ulong extra_rx, ulong extra_tx) |
| 72 | { |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 73 | int ret; |
| 74 | efi_status_t r = EFI_SUCCESS; |
| 75 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 76 | EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx); |
| 77 | |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 78 | if (!this) { |
| 79 | r = EFI_INVALID_PARAMETER; |
| 80 | goto error; |
| 81 | } |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 82 | |
Heinrich Schuchardt | 0c5d2a3 | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 83 | /* Setup packet buffers */ |
| 84 | net_init(); |
| 85 | /* Disable hardware and put it into the reset state */ |
| 86 | eth_halt(); |
| 87 | /* Set current device according to environment variables */ |
| 88 | eth_set_current(); |
| 89 | /* Get hardware ready for send and receive operations */ |
| 90 | ret = eth_init(); |
| 91 | if (ret < 0) { |
| 92 | eth_halt(); |
| 93 | r = EFI_DEVICE_ERROR; |
| 94 | } |
| 95 | |
| 96 | error: |
| 97 | return EFI_EXIT(r); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this, |
| 101 | int extended_verification) |
| 102 | { |
| 103 | EFI_ENTRY("%p, %x", this, extended_verification); |
| 104 | |
| 105 | return EFI_EXIT(EFI_SUCCESS); |
| 106 | } |
| 107 | |
| 108 | static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this) |
| 109 | { |
| 110 | EFI_ENTRY("%p", this); |
| 111 | |
| 112 | return EFI_EXIT(EFI_SUCCESS); |
| 113 | } |
| 114 | |
| 115 | static efi_status_t EFIAPI efi_net_receive_filters( |
| 116 | struct efi_simple_network *this, u32 enable, u32 disable, |
| 117 | int reset_mcast_filter, ulong mcast_filter_count, |
| 118 | struct efi_mac_address *mcast_filter) |
| 119 | { |
| 120 | EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable, |
| 121 | reset_mcast_filter, mcast_filter_count, mcast_filter); |
| 122 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 123 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static efi_status_t EFIAPI efi_net_station_address( |
| 127 | struct efi_simple_network *this, int reset, |
| 128 | struct efi_mac_address *new_mac) |
| 129 | { |
| 130 | EFI_ENTRY("%p, %x, %p", this, reset, new_mac); |
| 131 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 132 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this, |
| 136 | int reset, ulong *stat_size, |
| 137 | void *stat_table) |
| 138 | { |
| 139 | EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table); |
| 140 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 141 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this, |
| 145 | int ipv6, |
| 146 | struct efi_ip_address *ip, |
| 147 | struct efi_mac_address *mac) |
| 148 | { |
| 149 | EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac); |
| 150 | |
| 151 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 152 | } |
| 153 | |
| 154 | static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this, |
| 155 | int read_write, ulong offset, |
| 156 | ulong buffer_size, char *buffer) |
| 157 | { |
| 158 | EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size, |
| 159 | buffer); |
| 160 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 161 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this, |
| 165 | u32 *int_status, void **txbuf) |
| 166 | { |
| 167 | EFI_ENTRY("%p, %p, %p", this, int_status, txbuf); |
| 168 | |
Heinrich Schuchardt | 891b3d9 | 2017-10-05 16:36:02 +0200 | [diff] [blame] | 169 | efi_timer_check(); |
| 170 | |
| 171 | if (int_status) { |
| 172 | /* We send packets synchronously, so nothing is outstanding */ |
| 173 | *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT; |
| 174 | if (new_rx_packet) |
| 175 | *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT; |
| 176 | } |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 177 | if (txbuf) |
| 178 | *txbuf = new_tx_packet; |
| 179 | |
| 180 | new_tx_packet = NULL; |
| 181 | |
| 182 | return EFI_EXIT(EFI_SUCCESS); |
| 183 | } |
| 184 | |
| 185 | static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this, |
Heinrich Schuchardt | 8db174d | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 186 | size_t header_size, size_t buffer_size, void *buffer, |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 187 | struct efi_mac_address *src_addr, |
| 188 | struct efi_mac_address *dest_addr, u16 *protocol) |
| 189 | { |
Heinrich Schuchardt | 8db174d | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 190 | EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this, |
| 191 | (unsigned long)header_size, (unsigned long)buffer_size, |
| 192 | buffer, src_addr, dest_addr, protocol); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 193 | |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 194 | efi_timer_check(); |
| 195 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 196 | if (header_size) { |
| 197 | /* We would need to create the header if header_size != 0 */ |
| 198 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 199 | } |
| 200 | |
Alexander Graf | 712cd29 | 2016-09-06 14:26:27 +0200 | [diff] [blame] | 201 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 202 | /* Ethernet packets always fit, just bounce */ |
| 203 | memcpy(efi_bounce_buffer, buffer, buffer_size); |
| 204 | net_send_packet(efi_bounce_buffer, buffer_size); |
| 205 | #else |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 206 | net_send_packet(buffer, buffer_size); |
Alexander Graf | 712cd29 | 2016-09-06 14:26:27 +0200 | [diff] [blame] | 207 | #endif |
| 208 | |
Alexander Graf | 0efe1bc | 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 | e5c2160 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 217 | wait_for_packet->is_signaled = true; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Heinrich Schuchardt | 8db174d | 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 | 0efe1bc | 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 | 8db174d | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 236 | size_t *header_size, size_t *buffer_size, void *buffer, |
Alexander Graf | 0efe1bc | 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 | 336d9df | 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 | 0efe1bc | 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 | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 247 | efi_timer_check(); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 248 | |
| 249 | if (!new_rx_packet) |
| 250 | return EFI_EXIT(EFI_NOT_READY); |
Heinrich Schuchardt | 336d9df | 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 | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 271 | if (*buffer_size < net_rx_packet_len) { |
Heinrich Schuchardt | e1fec15 | 2018-10-18 21:51:38 +0200 | [diff] [blame^] | 272 | /* Packet doesn't fit, try again with bigger buffer */ |
Alexander Graf | 0efe1bc | 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 | 336d9df | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 276 | /* Copy packet */ |
Alexander Graf | 0efe1bc | 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 | 0efe1bc | 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 | a0549ef | 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 | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 315 | /* This gets called from do_bootefi_exec(). */ |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 316 | efi_status_t efi_net_register(void) |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 317 | { |
| 318 | struct efi_net_obj *netobj; |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 319 | efi_status_t r; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 320 | |
| 321 | if (!eth_get_dev()) { |
Heinrich Schuchardt | e1fec15 | 2018-10-18 21:51:38 +0200 | [diff] [blame^] | 322 | /* No network device active, don't expose any */ |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 323 | return EFI_SUCCESS; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Heinrich Schuchardt | e1fec15 | 2018-10-18 21:51:38 +0200 | [diff] [blame^] | 326 | /* We only expose the "active" network device, so one is enough */ |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 327 | netobj = calloc(1, sizeof(*netobj)); |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 328 | if (!netobj) { |
| 329 | printf("ERROR: Out of memory\n"); |
| 330 | return EFI_OUT_OF_RESOURCES; |
| 331 | } |
Heinrich Schuchardt | 84d1456 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 332 | |
| 333 | /* Hook net up to the device list */ |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 334 | efi_add_handle(&netobj->header); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 335 | |
| 336 | /* Fill in object data */ |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 337 | r = efi_add_protocol(&netobj->header, &efi_net_guid, |
Heinrich Schuchardt | 84d1456 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 338 | &netobj->net); |
| 339 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 340 | goto failure_to_add_protocol; |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 341 | r = efi_add_protocol(&netobj->header, &efi_guid_device_path, |
Heinrich Schuchardt | 84d1456 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 342 | efi_dp_from_eth()); |
| 343 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 344 | goto failure_to_add_protocol; |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 345 | r = efi_add_protocol(&netobj->header, &efi_pxe_guid, |
Heinrich Schuchardt | 84d1456 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 346 | &netobj->pxe); |
| 347 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 348 | goto failure_to_add_protocol; |
Heinrich Schuchardt | bdecf97 | 2017-10-05 16:35:57 +0200 | [diff] [blame] | 349 | netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 350 | netobj->net.start = efi_net_start; |
| 351 | netobj->net.stop = efi_net_stop; |
| 352 | netobj->net.initialize = efi_net_initialize; |
| 353 | netobj->net.reset = efi_net_reset; |
| 354 | netobj->net.shutdown = efi_net_shutdown; |
| 355 | netobj->net.receive_filters = efi_net_receive_filters; |
| 356 | netobj->net.station_address = efi_net_station_address; |
| 357 | netobj->net.statistics = efi_net_statistics; |
| 358 | netobj->net.mcastiptomac = efi_net_mcastiptomac; |
| 359 | netobj->net.nvdata = efi_net_nvdata; |
| 360 | netobj->net.get_status = efi_net_get_status; |
| 361 | netobj->net.transmit = efi_net_transmit; |
| 362 | netobj->net.receive = efi_net_receive; |
| 363 | netobj->net.mode = &netobj->net_mode; |
| 364 | netobj->net_mode.state = EFI_NETWORK_STARTED; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 365 | memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6); |
Heinrich Schuchardt | 5d4a5ea | 2017-10-05 16:35:58 +0200 | [diff] [blame] | 366 | netobj->net_mode.hwaddr_size = ARP_HLEN; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 367 | netobj->net_mode.max_packet_size = PKTSIZE; |
Andrew Thomas | f25ddca | 2018-06-21 16:21:01 -0700 | [diff] [blame] | 368 | netobj->net_mode.if_type = ARP_ETHER; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 369 | |
| 370 | netobj->pxe.mode = &netobj->pxe_mode; |
| 371 | if (dhcp_ack) |
| 372 | netobj->pxe_mode.dhcp_ack = *dhcp_ack; |
| 373 | |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 374 | /* |
Heinrich Schuchardt | e5c2160 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 375 | * Create WaitForPacket event. |
| 376 | */ |
| 377 | r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 378 | efi_network_timer_notify, NULL, NULL, |
Heinrich Schuchardt | e5c2160 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 379 | &wait_for_packet); |
| 380 | if (r != EFI_SUCCESS) { |
| 381 | printf("ERROR: Failed to register network event\n"); |
| 382 | return r; |
| 383 | } |
| 384 | netobj->net.wait_for_packet = wait_for_packet; |
| 385 | /* |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 386 | * Create a timer event. |
| 387 | * |
| 388 | * The notification function is used to check if a new network packet |
| 389 | * has been received. |
Heinrich Schuchardt | ee3db4f | 2018-03-24 18:40:22 +0100 | [diff] [blame] | 390 | * |
| 391 | * 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] | 392 | */ |
Heinrich Schuchardt | ee3db4f | 2018-03-24 18:40:22 +0100 | [diff] [blame] | 393 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY, |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 394 | efi_network_timer_notify, NULL, NULL, |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 395 | &network_timer_event); |
| 396 | if (r != EFI_SUCCESS) { |
| 397 | printf("ERROR: Failed to register network event\n"); |
| 398 | return r; |
| 399 | } |
Heinrich Schuchardt | e1fec15 | 2018-10-18 21:51:38 +0200 | [diff] [blame^] | 400 | /* Network is time critical, create event in every timer cycle */ |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 401 | r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0); |
| 402 | if (r != EFI_SUCCESS) { |
| 403 | printf("ERROR: Failed to set network timer\n"); |
| 404 | return r; |
| 405 | } |
| 406 | |
Heinrich Schuchardt | 075d425 | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 407 | return EFI_SUCCESS; |
| 408 | failure_to_add_protocol: |
| 409 | printf("ERROR: Failure to add protocol\n"); |
| 410 | return r; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 411 | } |