blob: 9afe76cdb31de77783e68338705f97b3f67a6686 [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
15DECLARE_GLOBAL_DATA_PTR;
16
17static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID;
18static const efi_guid_t efi_pxe_guid = EFI_PXE_GUID;
19static struct efi_pxe_packet *dhcp_ack;
20static bool new_rx_packet;
21static void *new_tx_packet;
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +020022/*
23 * The notification function of this event is called in every timer cycle
24 * to check if a new network packet has been received.
25 */
26static struct efi_event *network_timer_event;
Heinrich Schuchardte5c21602017-10-05 16:36:01 +020027/*
28 * This event is signaled when a packet has been received.
29 */
30static struct efi_event *wait_for_packet;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020031
32struct efi_net_obj {
33 /* Generic EFI object parent class data */
34 struct efi_object parent;
35 /* EFI Interface callback struct for network */
36 struct efi_simple_network net;
37 struct efi_simple_network_mode net_mode;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020038 /* PXE struct to transmit dhcp data */
39 struct efi_pxe pxe;
40 struct efi_pxe_mode pxe_mode;
41};
42
43static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
44{
45 EFI_ENTRY("%p", this);
46
47 return EFI_EXIT(EFI_SUCCESS);
48}
49
50static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
51{
52 EFI_ENTRY("%p", this);
53
54 return EFI_EXIT(EFI_SUCCESS);
55}
56
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020057/*
58 * Initialize network adapter and allocate transmit and receive buffers.
59 *
60 * This function implements the Initialize service of the
61 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
62 * (UEFI) specification for details.
63 *
64 * @this: pointer to the protocol instance
65 * @extra_rx: extra receive buffer to be allocated
66 * @extra_tx: extra transmit buffer to be allocated
67 * @return: status code
68 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +020069static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
70 ulong extra_rx, ulong extra_tx)
71{
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020072 int ret;
73 efi_status_t r = EFI_SUCCESS;
74
Alexander Graf0efe1bc2016-05-06 21:01:01 +020075 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
76
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020077 if (!this) {
78 r = EFI_INVALID_PARAMETER;
79 goto error;
80 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +020081
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020082 /* Setup packet buffers */
83 net_init();
84 /* Disable hardware and put it into the reset state */
85 eth_halt();
86 /* Set current device according to environment variables */
87 eth_set_current();
88 /* Get hardware ready for send and receive operations */
89 ret = eth_init();
90 if (ret < 0) {
91 eth_halt();
92 r = EFI_DEVICE_ERROR;
93 }
94
95error:
96 return EFI_EXIT(r);
Alexander Graf0efe1bc2016-05-06 21:01:01 +020097}
98
99static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
100 int extended_verification)
101{
102 EFI_ENTRY("%p, %x", this, extended_verification);
103
104 return EFI_EXIT(EFI_SUCCESS);
105}
106
107static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
108{
109 EFI_ENTRY("%p", this);
110
111 return EFI_EXIT(EFI_SUCCESS);
112}
113
114static efi_status_t EFIAPI efi_net_receive_filters(
115 struct efi_simple_network *this, u32 enable, u32 disable,
116 int reset_mcast_filter, ulong mcast_filter_count,
117 struct efi_mac_address *mcast_filter)
118{
119 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
120 reset_mcast_filter, mcast_filter_count, mcast_filter);
121
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200122 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200123}
124
125static efi_status_t EFIAPI efi_net_station_address(
126 struct efi_simple_network *this, int reset,
127 struct efi_mac_address *new_mac)
128{
129 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
130
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200131 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200132}
133
134static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
135 int reset, ulong *stat_size,
136 void *stat_table)
137{
138 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
139
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200140 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200141}
142
143static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
144 int ipv6,
145 struct efi_ip_address *ip,
146 struct efi_mac_address *mac)
147{
148 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
149
150 return EFI_EXIT(EFI_INVALID_PARAMETER);
151}
152
153static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
154 int read_write, ulong offset,
155 ulong buffer_size, char *buffer)
156{
157 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
158 buffer);
159
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200160 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200161}
162
163static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
164 u32 *int_status, void **txbuf)
165{
166 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
167
Heinrich Schuchardt891b3d92017-10-05 16:36:02 +0200168 efi_timer_check();
169
170 if (int_status) {
171 /* We send packets synchronously, so nothing is outstanding */
172 *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
173 if (new_rx_packet)
174 *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
175 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200176 if (txbuf)
177 *txbuf = new_tx_packet;
178
179 new_tx_packet = NULL;
180
181 return EFI_EXIT(EFI_SUCCESS);
182}
183
184static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200185 size_t header_size, size_t buffer_size, void *buffer,
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200186 struct efi_mac_address *src_addr,
187 struct efi_mac_address *dest_addr, u16 *protocol)
188{
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200189 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
190 (unsigned long)header_size, (unsigned long)buffer_size,
191 buffer, src_addr, dest_addr, protocol);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200192
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200193 efi_timer_check();
194
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200195 if (header_size) {
196 /* We would need to create the header if header_size != 0 */
197 return EFI_EXIT(EFI_INVALID_PARAMETER);
198 }
199
Alexander Graf712cd292016-09-06 14:26:27 +0200200#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
201 /* Ethernet packets always fit, just bounce */
202 memcpy(efi_bounce_buffer, buffer, buffer_size);
203 net_send_packet(efi_bounce_buffer, buffer_size);
204#else
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200205 net_send_packet(buffer, buffer_size);
Alexander Graf712cd292016-09-06 14:26:27 +0200206#endif
207
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200208 new_tx_packet = buffer;
209
210 return EFI_EXIT(EFI_SUCCESS);
211}
212
213static void efi_net_push(void *pkt, int len)
214{
215 new_rx_packet = true;
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200216 wait_for_packet->is_signaled = true;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200217}
218
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200219/*
220 * Receive a packet from a network interface.
221 *
222 * This function implements the Receive service of the Simple Network Protocol.
223 * See the UEFI spec for details.
224 *
225 * @this the instance of the Simple Network Protocol
226 * @header_size size of the media header
227 * @buffer_size size of the buffer to receive the packet
228 * @buffer buffer to receive the packet
229 * @src_addr source MAC address
230 * @dest_addr destination MAC address
231 * @protocol protocol
232 * @return status code
233 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200234static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200235 size_t *header_size, size_t *buffer_size, void *buffer,
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200236 struct efi_mac_address *src_addr,
237 struct efi_mac_address *dest_addr, u16 *protocol)
238{
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200239 struct ethernet_hdr *eth_hdr;
240 size_t hdr_size = sizeof(struct ethernet_hdr);
241 u16 protlen;
242
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200243 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
244 buffer_size, buffer, src_addr, dest_addr, protocol);
245
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200246 efi_timer_check();
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200247
248 if (!new_rx_packet)
249 return EFI_EXIT(EFI_NOT_READY);
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200250 /* Check that we at least received an Ethernet header */
251 if (net_rx_packet_len < sizeof(struct ethernet_hdr)) {
252 new_rx_packet = false;
253 return EFI_EXIT(EFI_NOT_READY);
254 }
255 /* Fill export parameters */
256 eth_hdr = (struct ethernet_hdr *)net_rx_packet;
257 protlen = ntohs(eth_hdr->et_protlen);
258 if (protlen == 0x8100) {
259 hdr_size += 4;
260 protlen = ntohs(*(u16 *)&net_rx_packet[hdr_size - 2]);
261 }
262 if (header_size)
263 *header_size = hdr_size;
264 if (dest_addr)
265 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
266 if (src_addr)
267 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
268 if (protocol)
269 *protocol = protlen;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200270 if (*buffer_size < net_rx_packet_len) {
271 /* Packet doesn't fit, try again with bigger buf */
272 *buffer_size = net_rx_packet_len;
273 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
274 }
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200275 /* Copy packet */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200276 memcpy(buffer, net_rx_packet, net_rx_packet_len);
277 *buffer_size = net_rx_packet_len;
278 new_rx_packet = false;
279
280 return EFI_EXIT(EFI_SUCCESS);
281}
282
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200283void efi_net_set_dhcp_ack(void *pkt, int len)
284{
285 int maxsize = sizeof(*dhcp_ack);
286
287 if (!dhcp_ack)
288 dhcp_ack = malloc(maxsize);
289
290 memcpy(dhcp_ack, pkt, min(len, maxsize));
291}
292
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200293/*
294 * Check if a new network packet has been received.
295 *
296 * This notification function is called in every timer cycle.
297 *
298 * @event the event for which this notification function is registered
299 * @context event context - not used in this function
300 */
301static void EFIAPI efi_network_timer_notify(struct efi_event *event,
302 void *context)
303{
304 EFI_ENTRY("%p, %p", event, context);
305
306 if (!new_rx_packet) {
307 push_packet = efi_net_push;
308 eth_rx();
309 push_packet = NULL;
310 }
311 EFI_EXIT(EFI_SUCCESS);
312}
313
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200314/* This gets called from do_bootefi_exec(). */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100315efi_status_t efi_net_register(void)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200316{
317 struct efi_net_obj *netobj;
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200318 efi_status_t r;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200319
320 if (!eth_get_dev()) {
321 /* No eth device active, don't expose any */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100322 return EFI_SUCCESS;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200323 }
324
325 /* We only expose the "active" eth device, so one is enough */
326 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100327 if (!netobj) {
328 printf("ERROR: Out of memory\n");
329 return EFI_OUT_OF_RESOURCES;
330 }
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100331
332 /* Hook net up to the device list */
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100333 efi_add_handle(&netobj->parent);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200334
335 /* Fill in object data */
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100336 r = efi_add_protocol(netobj->parent.handle, &efi_net_guid,
337 &netobj->net);
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_guid_device_path,
341 efi_dp_from_eth());
342 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100343 goto failure_to_add_protocol;
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100344 r = efi_add_protocol(netobj->parent.handle, &efi_pxe_guid,
345 &netobj->pxe);
346 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100347 goto failure_to_add_protocol;
Heinrich Schuchardtbdecf972017-10-05 16:35:57 +0200348 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200349 netobj->net.start = efi_net_start;
350 netobj->net.stop = efi_net_stop;
351 netobj->net.initialize = efi_net_initialize;
352 netobj->net.reset = efi_net_reset;
353 netobj->net.shutdown = efi_net_shutdown;
354 netobj->net.receive_filters = efi_net_receive_filters;
355 netobj->net.station_address = efi_net_station_address;
356 netobj->net.statistics = efi_net_statistics;
357 netobj->net.mcastiptomac = efi_net_mcastiptomac;
358 netobj->net.nvdata = efi_net_nvdata;
359 netobj->net.get_status = efi_net_get_status;
360 netobj->net.transmit = efi_net_transmit;
361 netobj->net.receive = efi_net_receive;
362 netobj->net.mode = &netobj->net_mode;
363 netobj->net_mode.state = EFI_NETWORK_STARTED;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200364 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardt5d4a5ea2017-10-05 16:35:58 +0200365 netobj->net_mode.hwaddr_size = ARP_HLEN;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200366 netobj->net_mode.max_packet_size = PKTSIZE;
367
368 netobj->pxe.mode = &netobj->pxe_mode;
369 if (dhcp_ack)
370 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
371
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200372 /*
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200373 * Create WaitForPacket event.
374 */
375 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100376 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200377 &wait_for_packet);
378 if (r != EFI_SUCCESS) {
379 printf("ERROR: Failed to register network event\n");
380 return r;
381 }
382 netobj->net.wait_for_packet = wait_for_packet;
383 /*
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200384 * Create a timer event.
385 *
386 * The notification function is used to check if a new network packet
387 * has been received.
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100388 *
389 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200390 */
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100391 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100392 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200393 &network_timer_event);
394 if (r != EFI_SUCCESS) {
395 printf("ERROR: Failed to register network event\n");
396 return r;
397 }
398 /* Network is time critical, create event in every timer cyle */
399 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
400 if (r != EFI_SUCCESS) {
401 printf("ERROR: Failed to set network timer\n");
402 return r;
403 }
404
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100405 return EFI_SUCCESS;
406failure_to_add_protocol:
407 printf("ERROR: Failure to add protocol\n");
408 return r;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200409}