blob: 6a8a0d7b1d7b8f42211171ec498dd975cfe0b848 [file] [log] [blame]
Alexander Graf94c4b992016-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;
22
23struct 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 /* Device path to the network adapter */
30 struct efi_device_path_file_path dp[2];
31 /* PXE struct to transmit dhcp data */
32 struct efi_pxe pxe;
33 struct efi_pxe_mode pxe_mode;
34};
35
36static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
37{
38 EFI_ENTRY("%p", this);
39
40 return EFI_EXIT(EFI_SUCCESS);
41}
42
43static efi_status_t EFIAPI efi_net_stop(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_initialize(struct efi_simple_network *this,
51 ulong extra_rx, ulong extra_tx)
52{
53 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
54
55 eth_init();
56
57 return EFI_EXIT(EFI_SUCCESS);
58}
59
60static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
61 int extended_verification)
62{
63 EFI_ENTRY("%p, %x", this, extended_verification);
64
65 return EFI_EXIT(EFI_SUCCESS);
66}
67
68static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
69{
70 EFI_ENTRY("%p", this);
71
72 return EFI_EXIT(EFI_SUCCESS);
73}
74
75static efi_status_t EFIAPI efi_net_receive_filters(
76 struct efi_simple_network *this, u32 enable, u32 disable,
77 int reset_mcast_filter, ulong mcast_filter_count,
78 struct efi_mac_address *mcast_filter)
79{
80 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
81 reset_mcast_filter, mcast_filter_count, mcast_filter);
82
83 /* XXX Do we care? */
84
85 return EFI_EXIT(EFI_SUCCESS);
86}
87
88static efi_status_t EFIAPI efi_net_station_address(
89 struct efi_simple_network *this, int reset,
90 struct efi_mac_address *new_mac)
91{
92 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
93
94 return EFI_EXIT(EFI_INVALID_PARAMETER);
95}
96
97static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
98 int reset, ulong *stat_size,
99 void *stat_table)
100{
101 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
102
103 return EFI_EXIT(EFI_INVALID_PARAMETER);
104}
105
106static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
107 int ipv6,
108 struct efi_ip_address *ip,
109 struct efi_mac_address *mac)
110{
111 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
112
113 return EFI_EXIT(EFI_INVALID_PARAMETER);
114}
115
116static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
117 int read_write, ulong offset,
118 ulong buffer_size, char *buffer)
119{
120 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
121 buffer);
122
123 return EFI_EXIT(EFI_INVALID_PARAMETER);
124}
125
126static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
127 u32 *int_status, void **txbuf)
128{
129 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
130
131 /* We send packets synchronously, so nothing is outstanding */
132 if (int_status)
133 *int_status = 0;
134 if (txbuf)
135 *txbuf = new_tx_packet;
136
137 new_tx_packet = NULL;
138
139 return EFI_EXIT(EFI_SUCCESS);
140}
141
142static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
143 ulong header_size, ulong buffer_size, void *buffer,
144 struct efi_mac_address *src_addr,
145 struct efi_mac_address *dest_addr, u16 *protocol)
146{
147 EFI_ENTRY("%p, %lx, %lx, %p, %p, %p, %p", this, header_size,
148 buffer_size, buffer, src_addr, dest_addr, protocol);
149
150 if (header_size) {
151 /* We would need to create the header if header_size != 0 */
152 return EFI_EXIT(EFI_INVALID_PARAMETER);
153 }
154
Alexander Graf0b12b862016-09-06 14:26:27 +0200155#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
156 /* Ethernet packets always fit, just bounce */
157 memcpy(efi_bounce_buffer, buffer, buffer_size);
158 net_send_packet(efi_bounce_buffer, buffer_size);
159#else
Alexander Graf94c4b992016-05-06 21:01:01 +0200160 net_send_packet(buffer, buffer_size);
Alexander Graf0b12b862016-09-06 14:26:27 +0200161#endif
162
Alexander Graf94c4b992016-05-06 21:01:01 +0200163 new_tx_packet = buffer;
164
165 return EFI_EXIT(EFI_SUCCESS);
166}
167
168static void efi_net_push(void *pkt, int len)
169{
170 new_rx_packet = true;
171}
172
173static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
174 ulong *header_size, ulong *buffer_size, void *buffer,
175 struct efi_mac_address *src_addr,
176 struct efi_mac_address *dest_addr, u16 *protocol)
177{
178 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
179 buffer_size, buffer, src_addr, dest_addr, protocol);
180
181 push_packet = efi_net_push;
182 eth_rx();
183 push_packet = NULL;
184
185 if (!new_rx_packet)
186 return EFI_EXIT(EFI_NOT_READY);
187
188 if (*buffer_size < net_rx_packet_len) {
189 /* Packet doesn't fit, try again with bigger buf */
190 *buffer_size = net_rx_packet_len;
191 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
192 }
193
194 memcpy(buffer, net_rx_packet, net_rx_packet_len);
195 *buffer_size = net_rx_packet_len;
196 new_rx_packet = false;
197
198 return EFI_EXIT(EFI_SUCCESS);
199}
200
201static efi_status_t efi_net_open_dp(void *handle, efi_guid_t *protocol,
202 void **protocol_interface, void *agent_handle,
203 void *controller_handle, uint32_t attributes)
204{
205 struct efi_simple_network *net = handle;
206 struct efi_net_obj *netobj = container_of(net, struct efi_net_obj, net);
207
208 *protocol_interface = netobj->dp;
209
210 return EFI_SUCCESS;
211}
212
213static efi_status_t efi_net_open_pxe(void *handle, efi_guid_t *protocol,
214 void **protocol_interface, void *agent_handle,
215 void *controller_handle, uint32_t attributes)
216{
217 struct efi_simple_network *net = handle;
218 struct efi_net_obj *netobj = container_of(net, struct efi_net_obj, net);
219
220 *protocol_interface = &netobj->pxe;
221
222 return EFI_SUCCESS;
223}
224
225void efi_net_set_dhcp_ack(void *pkt, int len)
226{
227 int maxsize = sizeof(*dhcp_ack);
228
229 if (!dhcp_ack)
230 dhcp_ack = malloc(maxsize);
231
232 memcpy(dhcp_ack, pkt, min(len, maxsize));
233}
234
235/* This gets called from do_bootefi_exec(). */
236int efi_net_register(void **handle)
237{
238 struct efi_net_obj *netobj;
239 struct efi_device_path_file_path dp_net = {
240 .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
241 .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
242 .dp.length = sizeof(dp_net),
243 .str = { 'N', 'e', 't' },
244 };
245 struct efi_device_path_file_path dp_end = {
246 .dp.type = DEVICE_PATH_TYPE_END,
247 .dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
248 .dp.length = sizeof(dp_end),
249 };
250
251 if (!eth_get_dev()) {
252 /* No eth device active, don't expose any */
253 return 0;
254 }
255
256 /* We only expose the "active" eth device, so one is enough */
257 netobj = calloc(1, sizeof(*netobj));
258
259 /* Fill in object data */
260 netobj->parent.protocols[0].guid = &efi_net_guid;
261 netobj->parent.protocols[0].open = efi_return_handle;
262 netobj->parent.protocols[1].guid = &efi_guid_device_path;
263 netobj->parent.protocols[1].open = efi_net_open_dp;
264 netobj->parent.protocols[2].guid = &efi_pxe_guid;
265 netobj->parent.protocols[2].open = efi_net_open_pxe;
266 netobj->parent.handle = &netobj->net;
267 netobj->net.start = efi_net_start;
268 netobj->net.stop = efi_net_stop;
269 netobj->net.initialize = efi_net_initialize;
270 netobj->net.reset = efi_net_reset;
271 netobj->net.shutdown = efi_net_shutdown;
272 netobj->net.receive_filters = efi_net_receive_filters;
273 netobj->net.station_address = efi_net_station_address;
274 netobj->net.statistics = efi_net_statistics;
275 netobj->net.mcastiptomac = efi_net_mcastiptomac;
276 netobj->net.nvdata = efi_net_nvdata;
277 netobj->net.get_status = efi_net_get_status;
278 netobj->net.transmit = efi_net_transmit;
279 netobj->net.receive = efi_net_receive;
280 netobj->net.mode = &netobj->net_mode;
281 netobj->net_mode.state = EFI_NETWORK_STARTED;
282 netobj->dp[0] = dp_net;
283 netobj->dp[1] = dp_end;
284 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
285 netobj->net_mode.max_packet_size = PKTSIZE;
286
287 netobj->pxe.mode = &netobj->pxe_mode;
288 if (dhcp_ack)
289 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
290
291 /* Hook net up to the device list */
292 list_add_tail(&netobj->parent.link, &efi_obj_list);
293
294 if (handle)
295 *handle = &netobj->net;
296
297 return 0;
298}