efi_loader: fix typos
[platform/kernel/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 /*
18  * The notification function of this event is called in every timer cycle
19  * to check if a new network packet has been received.
20  */
21 static struct efi_event *network_timer_event;
22 /*
23  * This event is signaled when a packet has been received.
24  */
25 static struct efi_event *wait_for_packet;
26
27 /**
28  * struct efi_net_obj - EFI object representing a network interface
29  *
30  * @header:     EFI object header
31  * @net:        simple network protocol interface
32  * @net_mode:   status of the network interface
33  * @pxe:        PXE base code protocol interface
34  * @pxe_mode:   status of the PXE base code protocol
35  */
36 struct efi_net_obj {
37         struct efi_object header;
38         struct efi_simple_network net;
39         struct efi_simple_network_mode net_mode;
40         struct efi_pxe pxe;
41         struct efi_pxe_mode pxe_mode;
42 };
43
44 static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
45 {
46         EFI_ENTRY("%p", this);
47
48         return EFI_EXIT(EFI_SUCCESS);
49 }
50
51 static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
52 {
53         EFI_ENTRY("%p", this);
54
55         return EFI_EXIT(EFI_SUCCESS);
56 }
57
58 /*
59  * Initialize network adapter and allocate transmit and receive buffers.
60  *
61  * This function implements the Initialize service of the
62  * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
63  * (UEFI) specification for details.
64  *
65  * @this:       pointer to the protocol instance
66  * @extra_rx:   extra receive buffer to be allocated
67  * @extra_tx:   extra transmit buffer to be allocated
68  * @return:     status code
69  */
70 static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
71                                               ulong extra_rx, ulong extra_tx)
72 {
73         int ret;
74         efi_status_t r = EFI_SUCCESS;
75
76         EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
77
78         if (!this) {
79                 r = EFI_INVALID_PARAMETER;
80                 goto error;
81         }
82
83         /* Setup packet buffers */
84         net_init();
85         /* Disable hardware and put it into the reset state */
86         eth_halt();
87         /* Set current device according to environment variables */
88         eth_set_current();
89         /* Get hardware ready for send and receive operations */
90         ret = eth_init();
91         if (ret < 0) {
92                 eth_halt();
93                 r = EFI_DEVICE_ERROR;
94         }
95
96 error:
97         return EFI_EXIT(r);
98 }
99
100 static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
101                                          int extended_verification)
102 {
103         EFI_ENTRY("%p, %x", this, extended_verification);
104
105         return EFI_EXIT(EFI_SUCCESS);
106 }
107
108 static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
109 {
110         EFI_ENTRY("%p", this);
111
112         return EFI_EXIT(EFI_SUCCESS);
113 }
114
115 static efi_status_t EFIAPI efi_net_receive_filters(
116                 struct efi_simple_network *this, u32 enable, u32 disable,
117                 int reset_mcast_filter, ulong mcast_filter_count,
118                 struct efi_mac_address *mcast_filter)
119 {
120         EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
121                   reset_mcast_filter, mcast_filter_count, mcast_filter);
122
123         return EFI_EXIT(EFI_UNSUPPORTED);
124 }
125
126 static efi_status_t EFIAPI efi_net_station_address(
127                 struct efi_simple_network *this, int reset,
128                 struct efi_mac_address *new_mac)
129 {
130         EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
131
132         return EFI_EXIT(EFI_UNSUPPORTED);
133 }
134
135 static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
136                                               int reset, ulong *stat_size,
137                                               void *stat_table)
138 {
139         EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
140
141         return EFI_EXIT(EFI_UNSUPPORTED);
142 }
143
144 static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
145                                                 int ipv6,
146                                                 struct efi_ip_address *ip,
147                                                 struct efi_mac_address *mac)
148 {
149         EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
150
151         return EFI_EXIT(EFI_INVALID_PARAMETER);
152 }
153
154 static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
155                                           int read_write, ulong offset,
156                                           ulong buffer_size, char *buffer)
157 {
158         EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
159                   buffer);
160
161         return EFI_EXIT(EFI_UNSUPPORTED);
162 }
163
164 static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
165                                               u32 *int_status, void **txbuf)
166 {
167         EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
168
169         efi_timer_check();
170
171         if (int_status) {
172                 /* We send packets synchronously, so nothing is outstanding */
173                 *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
174                 if (new_rx_packet)
175                         *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
176         }
177         if (txbuf)
178                 *txbuf = new_tx_packet;
179
180         new_tx_packet = NULL;
181
182         return EFI_EXIT(EFI_SUCCESS);
183 }
184
185 static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
186                 size_t header_size, size_t buffer_size, void *buffer,
187                 struct efi_mac_address *src_addr,
188                 struct efi_mac_address *dest_addr, u16 *protocol)
189 {
190         EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
191                   (unsigned long)header_size, (unsigned long)buffer_size,
192                   buffer, src_addr, dest_addr, protocol);
193
194         efi_timer_check();
195
196         if (header_size) {
197                 /* We would need to create the header if header_size != 0 */
198                 return EFI_EXIT(EFI_INVALID_PARAMETER);
199         }
200
201 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
202         /* Ethernet packets always fit, just bounce */
203         memcpy(efi_bounce_buffer, buffer, buffer_size);
204         net_send_packet(efi_bounce_buffer, buffer_size);
205 #else
206         net_send_packet(buffer, buffer_size);
207 #endif
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                 printf("ERROR: Out of memory\n");
330                 return EFI_OUT_OF_RESOURCES;
331         }
332
333         /* Hook net up to the device list */
334         efi_add_handle(&netobj->header);
335
336         /* Fill in object data */
337         r = efi_add_protocol(&netobj->header, &efi_net_guid,
338                              &netobj->net);
339         if (r != EFI_SUCCESS)
340                 goto failure_to_add_protocol;
341         r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
342                              efi_dp_from_eth());
343         if (r != EFI_SUCCESS)
344                 goto failure_to_add_protocol;
345         r = efi_add_protocol(&netobj->header, &efi_pxe_guid,
346                              &netobj->pxe);
347         if (r != EFI_SUCCESS)
348                 goto failure_to_add_protocol;
349         netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
350         netobj->net.start = efi_net_start;
351         netobj->net.stop = efi_net_stop;
352         netobj->net.initialize = efi_net_initialize;
353         netobj->net.reset = efi_net_reset;
354         netobj->net.shutdown = efi_net_shutdown;
355         netobj->net.receive_filters = efi_net_receive_filters;
356         netobj->net.station_address = efi_net_station_address;
357         netobj->net.statistics = efi_net_statistics;
358         netobj->net.mcastiptomac = efi_net_mcastiptomac;
359         netobj->net.nvdata = efi_net_nvdata;
360         netobj->net.get_status = efi_net_get_status;
361         netobj->net.transmit = efi_net_transmit;
362         netobj->net.receive = efi_net_receive;
363         netobj->net.mode = &netobj->net_mode;
364         netobj->net_mode.state = EFI_NETWORK_STARTED;
365         memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
366         netobj->net_mode.hwaddr_size = ARP_HLEN;
367         netobj->net_mode.max_packet_size = PKTSIZE;
368         netobj->net_mode.if_type = ARP_ETHER;
369
370         netobj->pxe.mode = &netobj->pxe_mode;
371         if (dhcp_ack)
372                 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
373
374         /*
375          * Create WaitForPacket event.
376          */
377         r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
378                              efi_network_timer_notify, NULL, NULL,
379                              &wait_for_packet);
380         if (r != EFI_SUCCESS) {
381                 printf("ERROR: Failed to register network event\n");
382                 return r;
383         }
384         netobj->net.wait_for_packet = wait_for_packet;
385         /*
386          * Create a timer event.
387          *
388          * The notification function is used to check if a new network packet
389          * has been received.
390          *
391          * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
392          */
393         r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
394                              efi_network_timer_notify, NULL, NULL,
395                              &network_timer_event);
396         if (r != EFI_SUCCESS) {
397                 printf("ERROR: Failed to register network event\n");
398                 return r;
399         }
400         /* Network is time critical, create event in every timer cycle */
401         r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
402         if (r != EFI_SUCCESS) {
403                 printf("ERROR: Failed to set network timer\n");
404                 return r;
405         }
406
407         return EFI_SUCCESS;
408 failure_to_add_protocol:
409         printf("ERROR: Failure to add protocol\n");
410         return r;
411 }