]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_net.c
569bf367a843a3a84736ac769fd2e1fb70721f16
[u-boot] / lib / efi_loader / efi_net.c
1 /*
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
15 DECLARE_GLOBAL_DATA_PTR;
16
17 static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID;
18 static const efi_guid_t efi_pxe_guid = EFI_PXE_GUID;
19 static struct efi_pxe_packet *dhcp_ack;
20 static bool new_rx_packet;
21 static void *new_tx_packet;
22
23 struct efi_net_obj {
24         /* Generic EFI object parent class data */
25         struct efi_object parent;
26         /* EFI Interface callback struct for network */
27         struct efi_simple_network net;
28         struct efi_simple_network_mode net_mode;
29         /* PXE struct to transmit dhcp data */
30         struct efi_pxe pxe;
31         struct efi_pxe_mode pxe_mode;
32 };
33
34 static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
35 {
36         EFI_ENTRY("%p", this);
37
38         return EFI_EXIT(EFI_SUCCESS);
39 }
40
41 static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
42 {
43         EFI_ENTRY("%p", this);
44
45         return EFI_EXIT(EFI_SUCCESS);
46 }
47
48 static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
49                                               ulong extra_rx, ulong extra_tx)
50 {
51         EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
52
53         eth_init();
54
55         return EFI_EXIT(EFI_SUCCESS);
56 }
57
58 static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
59                                          int extended_verification)
60 {
61         EFI_ENTRY("%p, %x", this, extended_verification);
62
63         return EFI_EXIT(EFI_SUCCESS);
64 }
65
66 static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
67 {
68         EFI_ENTRY("%p", this);
69
70         return EFI_EXIT(EFI_SUCCESS);
71 }
72
73 static efi_status_t EFIAPI efi_net_receive_filters(
74                 struct efi_simple_network *this, u32 enable, u32 disable,
75                 int reset_mcast_filter, ulong mcast_filter_count,
76                 struct efi_mac_address *mcast_filter)
77 {
78         EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
79                   reset_mcast_filter, mcast_filter_count, mcast_filter);
80
81         return EFI_EXIT(EFI_UNSUPPORTED);
82 }
83
84 static efi_status_t EFIAPI efi_net_station_address(
85                 struct efi_simple_network *this, int reset,
86                 struct efi_mac_address *new_mac)
87 {
88         EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
89
90         return EFI_EXIT(EFI_UNSUPPORTED);
91 }
92
93 static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
94                                               int reset, ulong *stat_size,
95                                               void *stat_table)
96 {
97         EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
98
99         return EFI_EXIT(EFI_UNSUPPORTED);
100 }
101
102 static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
103                                                 int ipv6,
104                                                 struct efi_ip_address *ip,
105                                                 struct efi_mac_address *mac)
106 {
107         EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
108
109         return EFI_EXIT(EFI_INVALID_PARAMETER);
110 }
111
112 static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
113                                           int read_write, ulong offset,
114                                           ulong buffer_size, char *buffer)
115 {
116         EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
117                   buffer);
118
119         return EFI_EXIT(EFI_UNSUPPORTED);
120 }
121
122 static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
123                                               u32 *int_status, void **txbuf)
124 {
125         EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
126
127         /* We send packets synchronously, so nothing is outstanding */
128         if (int_status)
129                 *int_status = 0;
130         if (txbuf)
131                 *txbuf = new_tx_packet;
132
133         new_tx_packet = NULL;
134
135         return EFI_EXIT(EFI_SUCCESS);
136 }
137
138 static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
139                 ulong header_size, ulong buffer_size, void *buffer,
140                 struct efi_mac_address *src_addr,
141                 struct efi_mac_address *dest_addr, u16 *protocol)
142 {
143         EFI_ENTRY("%p, %lx, %lx, %p, %p, %p, %p", this, header_size,
144                   buffer_size, buffer, src_addr, dest_addr, protocol);
145
146         if (header_size) {
147                 /* We would need to create the header if header_size != 0 */
148                 return EFI_EXIT(EFI_INVALID_PARAMETER);
149         }
150
151 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
152         /* Ethernet packets always fit, just bounce */
153         memcpy(efi_bounce_buffer, buffer, buffer_size);
154         net_send_packet(efi_bounce_buffer, buffer_size);
155 #else
156         net_send_packet(buffer, buffer_size);
157 #endif
158
159         new_tx_packet = buffer;
160
161         return EFI_EXIT(EFI_SUCCESS);
162 }
163
164 static void efi_net_push(void *pkt, int len)
165 {
166         new_rx_packet = true;
167 }
168
169 static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
170                 ulong *header_size, ulong *buffer_size, void *buffer,
171                 struct efi_mac_address *src_addr,
172                 struct efi_mac_address *dest_addr, u16 *protocol)
173 {
174         EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
175                   buffer_size, buffer, src_addr, dest_addr, protocol);
176
177         push_packet = efi_net_push;
178         eth_rx();
179         push_packet = NULL;
180
181         if (!new_rx_packet)
182                 return EFI_EXIT(EFI_NOT_READY);
183
184         if (*buffer_size < net_rx_packet_len) {
185                 /* Packet doesn't fit, try again with bigger buf */
186                 *buffer_size = net_rx_packet_len;
187                 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
188         }
189
190         memcpy(buffer, net_rx_packet, net_rx_packet_len);
191         *buffer_size = net_rx_packet_len;
192         new_rx_packet = false;
193
194         return EFI_EXIT(EFI_SUCCESS);
195 }
196
197 void efi_net_set_dhcp_ack(void *pkt, int len)
198 {
199         int maxsize = sizeof(*dhcp_ack);
200
201         if (!dhcp_ack)
202                 dhcp_ack = malloc(maxsize);
203
204         memcpy(dhcp_ack, pkt, min(len, maxsize));
205 }
206
207 /* This gets called from do_bootefi_exec(). */
208 int efi_net_register(void)
209 {
210         struct efi_net_obj *netobj;
211
212         if (!eth_get_dev()) {
213                 /* No eth device active, don't expose any */
214                 return 0;
215         }
216
217         /* We only expose the "active" eth device, so one is enough */
218         netobj = calloc(1, sizeof(*netobj));
219
220         /* Fill in object data */
221         netobj->parent.protocols[0].guid = &efi_net_guid;
222         netobj->parent.protocols[0].protocol_interface = &netobj->net;
223         netobj->parent.protocols[1].guid = &efi_guid_device_path;
224         netobj->parent.protocols[1].protocol_interface =
225                 efi_dp_from_eth();
226         netobj->parent.protocols[2].guid = &efi_pxe_guid;
227         netobj->parent.protocols[2].protocol_interface = &netobj->pxe;
228         netobj->parent.handle = &netobj->net;
229         netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
230         netobj->net.start = efi_net_start;
231         netobj->net.stop = efi_net_stop;
232         netobj->net.initialize = efi_net_initialize;
233         netobj->net.reset = efi_net_reset;
234         netobj->net.shutdown = efi_net_shutdown;
235         netobj->net.receive_filters = efi_net_receive_filters;
236         netobj->net.station_address = efi_net_station_address;
237         netobj->net.statistics = efi_net_statistics;
238         netobj->net.mcastiptomac = efi_net_mcastiptomac;
239         netobj->net.nvdata = efi_net_nvdata;
240         netobj->net.get_status = efi_net_get_status;
241         netobj->net.transmit = efi_net_transmit;
242         netobj->net.receive = efi_net_receive;
243         netobj->net.mode = &netobj->net_mode;
244         netobj->net_mode.state = EFI_NETWORK_STARTED;
245         memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
246         netobj->net_mode.hwaddr_size = ARP_HLEN;
247         netobj->net_mode.max_packet_size = PKTSIZE;
248
249         netobj->pxe.mode = &netobj->pxe_mode;
250         if (dhcp_ack)
251                 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
252
253         /* Hook net up to the device list */
254         list_add_tail(&netobj->parent.link, &efi_obj_list);
255
256         return 0;
257 }