blob: 4546b436031d1cdd001d1bd7c72b40f5950dd70e [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
57static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
58 ulong extra_rx, ulong extra_tx)
59{
60 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
61
62 eth_init();
63
64 return EFI_EXIT(EFI_SUCCESS);
65}
66
67static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
68 int extended_verification)
69{
70 EFI_ENTRY("%p, %x", this, extended_verification);
71
72 return EFI_EXIT(EFI_SUCCESS);
73}
74
75static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
76{
77 EFI_ENTRY("%p", this);
78
79 return EFI_EXIT(EFI_SUCCESS);
80}
81
82static efi_status_t EFIAPI efi_net_receive_filters(
83 struct efi_simple_network *this, u32 enable, u32 disable,
84 int reset_mcast_filter, ulong mcast_filter_count,
85 struct efi_mac_address *mcast_filter)
86{
87 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
88 reset_mcast_filter, mcast_filter_count, mcast_filter);
89
Heinrich Schuchardt61da6782017-10-05 16:35:59 +020090 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +020091}
92
93static efi_status_t EFIAPI efi_net_station_address(
94 struct efi_simple_network *this, int reset,
95 struct efi_mac_address *new_mac)
96{
97 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
98
Heinrich Schuchardt61da6782017-10-05 16:35:59 +020099 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200100}
101
102static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
103 int reset, ulong *stat_size,
104 void *stat_table)
105{
106 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
107
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200108 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200109}
110
111static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
112 int ipv6,
113 struct efi_ip_address *ip,
114 struct efi_mac_address *mac)
115{
116 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
117
118 return EFI_EXIT(EFI_INVALID_PARAMETER);
119}
120
121static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
122 int read_write, ulong offset,
123 ulong buffer_size, char *buffer)
124{
125 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
126 buffer);
127
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200128 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200129}
130
131static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
132 u32 *int_status, void **txbuf)
133{
134 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
135
Heinrich Schuchardt891b3d92017-10-05 16:36:02 +0200136 efi_timer_check();
137
138 if (int_status) {
139 /* We send packets synchronously, so nothing is outstanding */
140 *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
141 if (new_rx_packet)
142 *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
143 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200144 if (txbuf)
145 *txbuf = new_tx_packet;
146
147 new_tx_packet = NULL;
148
149 return EFI_EXIT(EFI_SUCCESS);
150}
151
152static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200153 size_t header_size, size_t buffer_size, void *buffer,
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200154 struct efi_mac_address *src_addr,
155 struct efi_mac_address *dest_addr, u16 *protocol)
156{
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200157 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
158 (unsigned long)header_size, (unsigned long)buffer_size,
159 buffer, src_addr, dest_addr, protocol);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200160
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200161 efi_timer_check();
162
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200163 if (header_size) {
164 /* We would need to create the header if header_size != 0 */
165 return EFI_EXIT(EFI_INVALID_PARAMETER);
166 }
167
Alexander Graf712cd292016-09-06 14:26:27 +0200168#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
169 /* Ethernet packets always fit, just bounce */
170 memcpy(efi_bounce_buffer, buffer, buffer_size);
171 net_send_packet(efi_bounce_buffer, buffer_size);
172#else
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200173 net_send_packet(buffer, buffer_size);
Alexander Graf712cd292016-09-06 14:26:27 +0200174#endif
175
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200176 new_tx_packet = buffer;
177
178 return EFI_EXIT(EFI_SUCCESS);
179}
180
181static void efi_net_push(void *pkt, int len)
182{
183 new_rx_packet = true;
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200184 wait_for_packet->is_signaled = true;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200185}
186
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200187/*
188 * Receive a packet from a network interface.
189 *
190 * This function implements the Receive service of the Simple Network Protocol.
191 * See the UEFI spec for details.
192 *
193 * @this the instance of the Simple Network Protocol
194 * @header_size size of the media header
195 * @buffer_size size of the buffer to receive the packet
196 * @buffer buffer to receive the packet
197 * @src_addr source MAC address
198 * @dest_addr destination MAC address
199 * @protocol protocol
200 * @return status code
201 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200202static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200203 size_t *header_size, size_t *buffer_size, void *buffer,
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200204 struct efi_mac_address *src_addr,
205 struct efi_mac_address *dest_addr, u16 *protocol)
206{
207 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
208 buffer_size, buffer, src_addr, dest_addr, protocol);
209
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200210 efi_timer_check();
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200211
212 if (!new_rx_packet)
213 return EFI_EXIT(EFI_NOT_READY);
214
215 if (*buffer_size < net_rx_packet_len) {
216 /* Packet doesn't fit, try again with bigger buf */
217 *buffer_size = net_rx_packet_len;
218 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
219 }
220
221 memcpy(buffer, net_rx_packet, net_rx_packet_len);
222 *buffer_size = net_rx_packet_len;
223 new_rx_packet = false;
224
225 return EFI_EXIT(EFI_SUCCESS);
226}
227
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200228void efi_net_set_dhcp_ack(void *pkt, int len)
229{
230 int maxsize = sizeof(*dhcp_ack);
231
232 if (!dhcp_ack)
233 dhcp_ack = malloc(maxsize);
234
235 memcpy(dhcp_ack, pkt, min(len, maxsize));
236}
237
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200238/*
239 * Check if a new network packet has been received.
240 *
241 * This notification function is called in every timer cycle.
242 *
243 * @event the event for which this notification function is registered
244 * @context event context - not used in this function
245 */
246static void EFIAPI efi_network_timer_notify(struct efi_event *event,
247 void *context)
248{
249 EFI_ENTRY("%p, %p", event, context);
250
251 if (!new_rx_packet) {
252 push_packet = efi_net_push;
253 eth_rx();
254 push_packet = NULL;
255 }
256 EFI_EXIT(EFI_SUCCESS);
257}
258
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200259/* This gets called from do_bootefi_exec(). */
Rob Clark95c55532017-09-13 18:05:33 -0400260int efi_net_register(void)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200261{
262 struct efi_net_obj *netobj;
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200263 efi_status_t r;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200264
265 if (!eth_get_dev()) {
266 /* No eth device active, don't expose any */
267 return 0;
268 }
269
270 /* We only expose the "active" eth device, so one is enough */
271 netobj = calloc(1, sizeof(*netobj));
272
273 /* Fill in object data */
274 netobj->parent.protocols[0].guid = &efi_net_guid;
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200275 netobj->parent.protocols[0].protocol_interface = &netobj->net;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200276 netobj->parent.protocols[1].guid = &efi_guid_device_path;
Rob Clarke15fc332017-09-13 18:05:32 -0400277 netobj->parent.protocols[1].protocol_interface =
278 efi_dp_from_eth();
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200279 netobj->parent.protocols[2].guid = &efi_pxe_guid;
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200280 netobj->parent.protocols[2].protocol_interface = &netobj->pxe;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200281 netobj->parent.handle = &netobj->net;
Heinrich Schuchardtbdecf972017-10-05 16:35:57 +0200282 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200283 netobj->net.start = efi_net_start;
284 netobj->net.stop = efi_net_stop;
285 netobj->net.initialize = efi_net_initialize;
286 netobj->net.reset = efi_net_reset;
287 netobj->net.shutdown = efi_net_shutdown;
288 netobj->net.receive_filters = efi_net_receive_filters;
289 netobj->net.station_address = efi_net_station_address;
290 netobj->net.statistics = efi_net_statistics;
291 netobj->net.mcastiptomac = efi_net_mcastiptomac;
292 netobj->net.nvdata = efi_net_nvdata;
293 netobj->net.get_status = efi_net_get_status;
294 netobj->net.transmit = efi_net_transmit;
295 netobj->net.receive = efi_net_receive;
296 netobj->net.mode = &netobj->net_mode;
297 netobj->net_mode.state = EFI_NETWORK_STARTED;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200298 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardt5d4a5ea2017-10-05 16:35:58 +0200299 netobj->net_mode.hwaddr_size = ARP_HLEN;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200300 netobj->net_mode.max_packet_size = PKTSIZE;
301
302 netobj->pxe.mode = &netobj->pxe_mode;
303 if (dhcp_ack)
304 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
305
306 /* Hook net up to the device list */
307 list_add_tail(&netobj->parent.link, &efi_obj_list);
308
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200309 /*
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200310 * Create WaitForPacket event.
311 */
312 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
313 efi_network_timer_notify, NULL,
314 &wait_for_packet);
315 if (r != EFI_SUCCESS) {
316 printf("ERROR: Failed to register network event\n");
317 return r;
318 }
319 netobj->net.wait_for_packet = wait_for_packet;
320 /*
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200321 * Create a timer event.
322 *
323 * The notification function is used to check if a new network packet
324 * has been received.
325 */
326 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
327 efi_network_timer_notify, NULL,
328 &network_timer_event);
329 if (r != EFI_SUCCESS) {
330 printf("ERROR: Failed to register network event\n");
331 return r;
332 }
333 /* Network is time critical, create event in every timer cyle */
334 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
335 if (r != EFI_SUCCESS) {
336 printf("ERROR: Failed to set network timer\n");
337 return r;
338 }
339
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200340 return 0;
341}