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