Merge tag 'fpga-for-v2018.11' of git://git.denx.de/u-boot-microblaze
[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 <lcd.h>
11 #include <malloc.h>
12
13 static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID;
14 static const efi_guid_t efi_pxe_guid = EFI_PXE_GUID;
15 static struct efi_pxe_packet *dhcp_ack;
16 static bool new_rx_packet;
17 static void *new_tx_packet;
18 /*
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  */
22 static struct efi_event *network_timer_event;
23 /*
24  * This event is signaled when a packet has been received.
25  */
26 static struct efi_event *wait_for_packet;
27
28 struct 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;
34         /* PXE struct to transmit dhcp data */
35         struct efi_pxe pxe;
36         struct efi_pxe_mode pxe_mode;
37 };
38
39 static 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
46 static 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
53 /*
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  */
65 static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
66                                               ulong extra_rx, ulong extra_tx)
67 {
68         int ret;
69         efi_status_t r = EFI_SUCCESS;
70
71         EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
72
73         if (!this) {
74                 r = EFI_INVALID_PARAMETER;
75                 goto error;
76         }
77
78         /* 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
91 error:
92         return EFI_EXIT(r);
93 }
94
95 static 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
103 static 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
110 static 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
118         return EFI_EXIT(EFI_UNSUPPORTED);
119 }
120
121 static 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
127         return EFI_EXIT(EFI_UNSUPPORTED);
128 }
129
130 static 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
136         return EFI_EXIT(EFI_UNSUPPORTED);
137 }
138
139 static 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
149 static 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
156         return EFI_EXIT(EFI_UNSUPPORTED);
157 }
158
159 static 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
164         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         }
172         if (txbuf)
173                 *txbuf = new_tx_packet;
174
175         new_tx_packet = NULL;
176
177         return EFI_EXIT(EFI_SUCCESS);
178 }
179
180 static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
181                 size_t header_size, size_t buffer_size, void *buffer,
182                 struct efi_mac_address *src_addr,
183                 struct efi_mac_address *dest_addr, u16 *protocol)
184 {
185         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);
188
189         efi_timer_check();
190
191         if (header_size) {
192                 /* We would need to create the header if header_size != 0 */
193                 return EFI_EXIT(EFI_INVALID_PARAMETER);
194         }
195
196 #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
201         net_send_packet(buffer, buffer_size);
202 #endif
203
204         new_tx_packet = buffer;
205
206         return EFI_EXIT(EFI_SUCCESS);
207 }
208
209 static void efi_net_push(void *pkt, int len)
210 {
211         new_rx_packet = true;
212         wait_for_packet->is_signaled = true;
213 }
214
215 /*
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  */
230 static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
231                 size_t *header_size, size_t *buffer_size, void *buffer,
232                 struct efi_mac_address *src_addr,
233                 struct efi_mac_address *dest_addr, u16 *protocol)
234 {
235         struct ethernet_hdr *eth_hdr;
236         size_t hdr_size = sizeof(struct ethernet_hdr);
237         u16 protlen;
238
239         EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
240                   buffer_size, buffer, src_addr, dest_addr, protocol);
241
242         efi_timer_check();
243
244         if (!new_rx_packet)
245                 return EFI_EXIT(EFI_NOT_READY);
246         /* 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;
266         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         }
271         /* Copy packet */
272         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
279 void 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
289 /*
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  */
297 static 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
310 /* This gets called from do_bootefi_exec(). */
311 efi_status_t efi_net_register(void)
312 {
313         struct efi_net_obj *netobj;
314         efi_status_t r;
315
316         if (!eth_get_dev()) {
317                 /* No eth device active, don't expose any */
318                 return EFI_SUCCESS;
319         }
320
321         /* We only expose the "active" eth device, so one is enough */
322         netobj = calloc(1, sizeof(*netobj));
323         if (!netobj) {
324                 printf("ERROR: Out of memory\n");
325                 return EFI_OUT_OF_RESOURCES;
326         }
327
328         /* Hook net up to the device list */
329         efi_add_handle(&netobj->parent);
330
331         /* Fill in object data */
332         r = efi_add_protocol(netobj->parent.handle, &efi_net_guid,
333                              &netobj->net);
334         if (r != EFI_SUCCESS)
335                 goto failure_to_add_protocol;
336         r = efi_add_protocol(netobj->parent.handle, &efi_guid_device_path,
337                              efi_dp_from_eth());
338         if (r != EFI_SUCCESS)
339                 goto failure_to_add_protocol;
340         r = efi_add_protocol(netobj->parent.handle, &efi_pxe_guid,
341                              &netobj->pxe);
342         if (r != EFI_SUCCESS)
343                 goto failure_to_add_protocol;
344         netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
345         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;
360         memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
361         netobj->net_mode.hwaddr_size = ARP_HLEN;
362         netobj->net_mode.max_packet_size = PKTSIZE;
363         netobj->net_mode.if_type = ARP_ETHER;
364
365         netobj->pxe.mode = &netobj->pxe_mode;
366         if (dhcp_ack)
367                 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
368
369         /*
370          * Create WaitForPacket event.
371          */
372         r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
373                              efi_network_timer_notify, NULL, NULL,
374                              &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         /*
381          * Create a timer event.
382          *
383          * The notification function is used to check if a new network packet
384          * has been received.
385          *
386          * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
387          */
388         r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
389                              efi_network_timer_notify, NULL, NULL,
390                              &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
402         return EFI_SUCCESS;
403 failure_to_add_protocol:
404         printf("ERROR: Failure to add protocol\n");
405         return r;
406 }