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