blob: e3132e6c172e526de5a5ebe46f5278e2f764294e [file] [log] [blame]
Alexander Graf0efe1bc2016-05-06 21:01:01 +02001/*
2 * EFI application network access support
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <efi_loader.h>
11#include <inttypes.h>
12#include <lcd.h>
13#include <malloc.h>
14
Alexander Graf0efe1bc2016-05-06 21:01:01 +020015static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID;
16static const efi_guid_t efi_pxe_guid = EFI_PXE_GUID;
17static struct efi_pxe_packet *dhcp_ack;
18static bool new_rx_packet;
19static void *new_tx_packet;
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +020020/*
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 */
24static struct efi_event *network_timer_event;
Heinrich Schuchardte5c21602017-10-05 16:36:01 +020025/*
26 * This event is signaled when a packet has been received.
27 */
28static struct efi_event *wait_for_packet;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020029
30struct efi_net_obj {
31 /* Generic EFI object parent class data */
32 struct efi_object parent;
33 /* EFI Interface callback struct for network */
34 struct efi_simple_network net;
35 struct efi_simple_network_mode net_mode;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020036 /* PXE struct to transmit dhcp data */
37 struct efi_pxe pxe;
38 struct efi_pxe_mode pxe_mode;
39};
40
41static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
42{
43 EFI_ENTRY("%p", this);
44
45 return EFI_EXIT(EFI_SUCCESS);
46}
47
48static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
49{
50 EFI_ENTRY("%p", this);
51
52 return EFI_EXIT(EFI_SUCCESS);
53}
54
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020055/*
56 * Initialize network adapter and allocate transmit and receive buffers.
57 *
58 * This function implements the Initialize service of the
59 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
60 * (UEFI) specification for details.
61 *
62 * @this: pointer to the protocol instance
63 * @extra_rx: extra receive buffer to be allocated
64 * @extra_tx: extra transmit buffer to be allocated
65 * @return: status code
66 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +020067static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
68 ulong extra_rx, ulong extra_tx)
69{
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020070 int ret;
71 efi_status_t r = EFI_SUCCESS;
72
Alexander Graf0efe1bc2016-05-06 21:01:01 +020073 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
74
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020075 if (!this) {
76 r = EFI_INVALID_PARAMETER;
77 goto error;
78 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +020079
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020080 /* Setup packet buffers */
81 net_init();
82 /* Disable hardware and put it into the reset state */
83 eth_halt();
84 /* Set current device according to environment variables */
85 eth_set_current();
86 /* Get hardware ready for send and receive operations */
87 ret = eth_init();
88 if (ret < 0) {
89 eth_halt();
90 r = EFI_DEVICE_ERROR;
91 }
92
93error:
94 return EFI_EXIT(r);
Alexander Graf0efe1bc2016-05-06 21:01:01 +020095}
96
97static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
98 int extended_verification)
99{
100 EFI_ENTRY("%p, %x", this, extended_verification);
101
102 return EFI_EXIT(EFI_SUCCESS);
103}
104
105static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
106{
107 EFI_ENTRY("%p", this);
108
109 return EFI_EXIT(EFI_SUCCESS);
110}
111
112static efi_status_t EFIAPI efi_net_receive_filters(
113 struct efi_simple_network *this, u32 enable, u32 disable,
114 int reset_mcast_filter, ulong mcast_filter_count,
115 struct efi_mac_address *mcast_filter)
116{
117 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
118 reset_mcast_filter, mcast_filter_count, mcast_filter);
119
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200120 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200121}
122
123static efi_status_t EFIAPI efi_net_station_address(
124 struct efi_simple_network *this, int reset,
125 struct efi_mac_address *new_mac)
126{
127 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
128
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200129 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200130}
131
132static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
133 int reset, ulong *stat_size,
134 void *stat_table)
135{
136 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
137
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200138 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200139}
140
141static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
142 int ipv6,
143 struct efi_ip_address *ip,
144 struct efi_mac_address *mac)
145{
146 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
147
148 return EFI_EXIT(EFI_INVALID_PARAMETER);
149}
150
151static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
152 int read_write, ulong offset,
153 ulong buffer_size, char *buffer)
154{
155 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
156 buffer);
157
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200158 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200159}
160
161static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
162 u32 *int_status, void **txbuf)
163{
164 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
165
Heinrich Schuchardt891b3d92017-10-05 16:36:02 +0200166 efi_timer_check();
167
168 if (int_status) {
169 /* We send packets synchronously, so nothing is outstanding */
170 *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
171 if (new_rx_packet)
172 *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
173 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200174 if (txbuf)
175 *txbuf = new_tx_packet;
176
177 new_tx_packet = NULL;
178
179 return EFI_EXIT(EFI_SUCCESS);
180}
181
182static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200183 size_t header_size, size_t buffer_size, void *buffer,
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200184 struct efi_mac_address *src_addr,
185 struct efi_mac_address *dest_addr, u16 *protocol)
186{
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200187 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
188 (unsigned long)header_size, (unsigned long)buffer_size,
189 buffer, src_addr, dest_addr, protocol);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200190
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200191 efi_timer_check();
192
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200193 if (header_size) {
194 /* We would need to create the header if header_size != 0 */
195 return EFI_EXIT(EFI_INVALID_PARAMETER);
196 }
197
Alexander Graf712cd292016-09-06 14:26:27 +0200198#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
199 /* Ethernet packets always fit, just bounce */
200 memcpy(efi_bounce_buffer, buffer, buffer_size);
201 net_send_packet(efi_bounce_buffer, buffer_size);
202#else
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200203 net_send_packet(buffer, buffer_size);
Alexander Graf712cd292016-09-06 14:26:27 +0200204#endif
205
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200206 new_tx_packet = buffer;
207
208 return EFI_EXIT(EFI_SUCCESS);
209}
210
211static void efi_net_push(void *pkt, int len)
212{
213 new_rx_packet = true;
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200214 wait_for_packet->is_signaled = true;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200215}
216
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200217/*
218 * Receive a packet from a network interface.
219 *
220 * This function implements the Receive service of the Simple Network Protocol.
221 * See the UEFI spec for details.
222 *
223 * @this the instance of the Simple Network Protocol
224 * @header_size size of the media header
225 * @buffer_size size of the buffer to receive the packet
226 * @buffer buffer to receive the packet
227 * @src_addr source MAC address
228 * @dest_addr destination MAC address
229 * @protocol protocol
230 * @return status code
231 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200232static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200233 size_t *header_size, size_t *buffer_size, void *buffer,
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200234 struct efi_mac_address *src_addr,
235 struct efi_mac_address *dest_addr, u16 *protocol)
236{
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200237 struct ethernet_hdr *eth_hdr;
238 size_t hdr_size = sizeof(struct ethernet_hdr);
239 u16 protlen;
240
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200241 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
242 buffer_size, buffer, src_addr, dest_addr, protocol);
243
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200244 efi_timer_check();
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200245
246 if (!new_rx_packet)
247 return EFI_EXIT(EFI_NOT_READY);
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200248 /* Check that we at least received an Ethernet header */
249 if (net_rx_packet_len < sizeof(struct ethernet_hdr)) {
250 new_rx_packet = false;
251 return EFI_EXIT(EFI_NOT_READY);
252 }
253 /* Fill export parameters */
254 eth_hdr = (struct ethernet_hdr *)net_rx_packet;
255 protlen = ntohs(eth_hdr->et_protlen);
256 if (protlen == 0x8100) {
257 hdr_size += 4;
258 protlen = ntohs(*(u16 *)&net_rx_packet[hdr_size - 2]);
259 }
260 if (header_size)
261 *header_size = hdr_size;
262 if (dest_addr)
263 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
264 if (src_addr)
265 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
266 if (protocol)
267 *protocol = protlen;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200268 if (*buffer_size < net_rx_packet_len) {
269 /* Packet doesn't fit, try again with bigger buf */
270 *buffer_size = net_rx_packet_len;
271 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
272 }
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200273 /* Copy packet */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200274 memcpy(buffer, net_rx_packet, net_rx_packet_len);
275 *buffer_size = net_rx_packet_len;
276 new_rx_packet = false;
277
278 return EFI_EXIT(EFI_SUCCESS);
279}
280
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200281void efi_net_set_dhcp_ack(void *pkt, int len)
282{
283 int maxsize = sizeof(*dhcp_ack);
284
285 if (!dhcp_ack)
286 dhcp_ack = malloc(maxsize);
287
288 memcpy(dhcp_ack, pkt, min(len, maxsize));
289}
290
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200291/*
292 * Check if a new network packet has been received.
293 *
294 * This notification function is called in every timer cycle.
295 *
296 * @event the event for which this notification function is registered
297 * @context event context - not used in this function
298 */
299static void EFIAPI efi_network_timer_notify(struct efi_event *event,
300 void *context)
301{
302 EFI_ENTRY("%p, %p", event, context);
303
304 if (!new_rx_packet) {
305 push_packet = efi_net_push;
306 eth_rx();
307 push_packet = NULL;
308 }
309 EFI_EXIT(EFI_SUCCESS);
310}
311
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200312/* This gets called from do_bootefi_exec(). */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100313efi_status_t efi_net_register(void)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200314{
315 struct efi_net_obj *netobj;
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200316 efi_status_t r;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200317
318 if (!eth_get_dev()) {
319 /* No eth device active, don't expose any */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100320 return EFI_SUCCESS;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200321 }
322
323 /* We only expose the "active" eth device, so one is enough */
324 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100325 if (!netobj) {
326 printf("ERROR: Out of memory\n");
327 return EFI_OUT_OF_RESOURCES;
328 }
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100329
330 /* Hook net up to the device list */
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100331 efi_add_handle(&netobj->parent);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200332
333 /* Fill in object data */
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100334 r = efi_add_protocol(netobj->parent.handle, &efi_net_guid,
335 &netobj->net);
336 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100337 goto failure_to_add_protocol;
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100338 r = efi_add_protocol(netobj->parent.handle, &efi_guid_device_path,
339 efi_dp_from_eth());
340 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100341 goto failure_to_add_protocol;
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100342 r = efi_add_protocol(netobj->parent.handle, &efi_pxe_guid,
343 &netobj->pxe);
344 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100345 goto failure_to_add_protocol;
Heinrich Schuchardtbdecf972017-10-05 16:35:57 +0200346 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200347 netobj->net.start = efi_net_start;
348 netobj->net.stop = efi_net_stop;
349 netobj->net.initialize = efi_net_initialize;
350 netobj->net.reset = efi_net_reset;
351 netobj->net.shutdown = efi_net_shutdown;
352 netobj->net.receive_filters = efi_net_receive_filters;
353 netobj->net.station_address = efi_net_station_address;
354 netobj->net.statistics = efi_net_statistics;
355 netobj->net.mcastiptomac = efi_net_mcastiptomac;
356 netobj->net.nvdata = efi_net_nvdata;
357 netobj->net.get_status = efi_net_get_status;
358 netobj->net.transmit = efi_net_transmit;
359 netobj->net.receive = efi_net_receive;
360 netobj->net.mode = &netobj->net_mode;
361 netobj->net_mode.state = EFI_NETWORK_STARTED;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200362 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardt5d4a5ea2017-10-05 16:35:58 +0200363 netobj->net_mode.hwaddr_size = ARP_HLEN;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200364 netobj->net_mode.max_packet_size = PKTSIZE;
365
366 netobj->pxe.mode = &netobj->pxe_mode;
367 if (dhcp_ack)
368 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
369
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200370 /*
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200371 * Create WaitForPacket event.
372 */
373 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100374 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200375 &wait_for_packet);
376 if (r != EFI_SUCCESS) {
377 printf("ERROR: Failed to register network event\n");
378 return r;
379 }
380 netobj->net.wait_for_packet = wait_for_packet;
381 /*
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200382 * Create a timer event.
383 *
384 * The notification function is used to check if a new network packet
385 * has been received.
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100386 *
387 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200388 */
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100389 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100390 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200391 &network_timer_event);
392 if (r != EFI_SUCCESS) {
393 printf("ERROR: Failed to register network event\n");
394 return r;
395 }
396 /* Network is time critical, create event in every timer cyle */
397 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
398 if (r != EFI_SUCCESS) {
399 printf("ERROR: Failed to set network timer\n");
400 return r;
401 }
402
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100403 return EFI_SUCCESS;
404failure_to_add_protocol:
405 printf("ERROR: Failure to add protocol\n");
406 return r;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200407}