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