blob: 034d0d2ed03004aff1a24efc609942626798b580 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf0efe1bc2016-05-06 21:01:01 +02002/*
3 * EFI application network access support
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf0efe1bc2016-05-06 21:01:01 +02006 */
7
8#include <common.h>
9#include <efi_loader.h>
Alexander Graf0efe1bc2016-05-06 21:01:01 +020010#include <lcd.h>
11#include <malloc.h>
12
Alexander Graf0efe1bc2016-05-06 21:01:01 +020013static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID;
14static const efi_guid_t efi_pxe_guid = EFI_PXE_GUID;
15static struct efi_pxe_packet *dhcp_ack;
16static bool new_rx_packet;
17static void *new_tx_packet;
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +020018/*
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 */
22static struct efi_event *network_timer_event;
Heinrich Schuchardte5c21602017-10-05 16:36:01 +020023/*
24 * This event is signaled when a packet has been received.
25 */
26static struct efi_event *wait_for_packet;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020027
28struct 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 Graf0efe1bc2016-05-06 21:01:01 +020034 /* PXE struct to transmit dhcp data */
35 struct efi_pxe pxe;
36 struct efi_pxe_mode pxe_mode;
37};
38
39static 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
46static 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 Schuchardt0c5d2a32018-04-03 22:06:52 +020053/*
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 Graf0efe1bc2016-05-06 21:01:01 +020065static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
66 ulong extra_rx, ulong extra_tx)
67{
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020068 int ret;
69 efi_status_t r = EFI_SUCCESS;
70
Alexander Graf0efe1bc2016-05-06 21:01:01 +020071 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
72
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020073 if (!this) {
74 r = EFI_INVALID_PARAMETER;
75 goto error;
76 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +020077
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020078 /* 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
91error:
92 return EFI_EXIT(r);
Alexander Graf0efe1bc2016-05-06 21:01:01 +020093}
94
95static 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
103static 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
110static 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 Schuchardt61da6782017-10-05 16:35:59 +0200118 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200119}
120
121static 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 Schuchardt61da6782017-10-05 16:35:59 +0200127 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200128}
129
130static 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 Schuchardt61da6782017-10-05 16:35:59 +0200136 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200137}
138
139static 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
149static 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 Schuchardt61da6782017-10-05 16:35:59 +0200156 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200157}
158
159static 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 Schuchardt891b3d92017-10-05 16:36:02 +0200164 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 Graf0efe1bc2016-05-06 21:01:01 +0200172 if (txbuf)
173 *txbuf = new_tx_packet;
174
175 new_tx_packet = NULL;
176
177 return EFI_EXIT(EFI_SUCCESS);
178}
179
180static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200181 size_t header_size, size_t buffer_size, void *buffer,
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200182 struct efi_mac_address *src_addr,
183 struct efi_mac_address *dest_addr, u16 *protocol)
184{
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200185 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 Graf0efe1bc2016-05-06 21:01:01 +0200188
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200189 efi_timer_check();
190
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200191 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 Graf712cd292016-09-06 14:26:27 +0200196#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 Graf0efe1bc2016-05-06 21:01:01 +0200201 net_send_packet(buffer, buffer_size);
Alexander Graf712cd292016-09-06 14:26:27 +0200202#endif
203
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200204 new_tx_packet = buffer;
205
206 return EFI_EXIT(EFI_SUCCESS);
207}
208
209static void efi_net_push(void *pkt, int len)
210{
211 new_rx_packet = true;
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200212 wait_for_packet->is_signaled = true;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200213}
214
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200215/*
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 Graf0efe1bc2016-05-06 21:01:01 +0200230static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200231 size_t *header_size, size_t *buffer_size, void *buffer,
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200232 struct efi_mac_address *src_addr,
233 struct efi_mac_address *dest_addr, u16 *protocol)
234{
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200235 struct ethernet_hdr *eth_hdr;
236 size_t hdr_size = sizeof(struct ethernet_hdr);
237 u16 protlen;
238
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200239 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
240 buffer_size, buffer, src_addr, dest_addr, protocol);
241
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200242 efi_timer_check();
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200243
244 if (!new_rx_packet)
245 return EFI_EXIT(EFI_NOT_READY);
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200246 /* 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 Graf0efe1bc2016-05-06 21:01:01 +0200266 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 Schuchardt336d9df2017-10-05 16:36:04 +0200271 /* Copy packet */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200272 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 Graf0efe1bc2016-05-06 21:01:01 +0200279void 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 Schuchardta0549ef2017-10-05 16:36:00 +0200289/*
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 */
297static 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 Graf0efe1bc2016-05-06 21:01:01 +0200310/* This gets called from do_bootefi_exec(). */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100311efi_status_t efi_net_register(void)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200312{
313 struct efi_net_obj *netobj;
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200314 efi_status_t r;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200315
316 if (!eth_get_dev()) {
317 /* No eth device active, don't expose any */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100318 return EFI_SUCCESS;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200319 }
320
321 /* We only expose the "active" eth device, so one is enough */
322 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100323 if (!netobj) {
324 printf("ERROR: Out of memory\n");
325 return EFI_OUT_OF_RESOURCES;
326 }
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100327
328 /* Hook net up to the device list */
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100329 efi_add_handle(&netobj->parent);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200330
331 /* Fill in object data */
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100332 r = efi_add_protocol(netobj->parent.handle, &efi_net_guid,
333 &netobj->net);
334 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100335 goto failure_to_add_protocol;
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100336 r = efi_add_protocol(netobj->parent.handle, &efi_guid_device_path,
337 efi_dp_from_eth());
338 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100339 goto failure_to_add_protocol;
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100340 r = efi_add_protocol(netobj->parent.handle, &efi_pxe_guid,
341 &netobj->pxe);
342 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100343 goto failure_to_add_protocol;
Heinrich Schuchardtbdecf972017-10-05 16:35:57 +0200344 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200345 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 Graf0efe1bc2016-05-06 21:01:01 +0200360 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardt5d4a5ea2017-10-05 16:35:58 +0200361 netobj->net_mode.hwaddr_size = ARP_HLEN;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200362 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomasf25ddca2018-06-21 16:21:01 -0700363 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200364
365 netobj->pxe.mode = &netobj->pxe_mode;
366 if (dhcp_ack)
367 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
368
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200369 /*
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200370 * Create WaitForPacket event.
371 */
372 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100373 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200374 &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 Schuchardta0549ef2017-10-05 16:36:00 +0200381 * Create a timer event.
382 *
383 * The notification function is used to check if a new network packet
384 * has been received.
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100385 *
386 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200387 */
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100388 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100389 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200390 &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 Schuchardt075d4252018-03-03 15:28:56 +0100402 return EFI_SUCCESS;
403failure_to_add_protocol:
404 printf("ERROR: Failure to add protocol\n");
405 return r;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200406}