blob: 569bf367a843a3a84736ac769fd2e1fb70721f16 [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;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020029 /* PXE struct to transmit dhcp data */
30 struct efi_pxe pxe;
31 struct efi_pxe_mode pxe_mode;
32};
33
34static 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
41static 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
48static 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
58static 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
66static 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
73static 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
Heinrich Schuchardt61da6782017-10-05 16:35:59 +020081 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +020082}
83
84static 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
Heinrich Schuchardt61da6782017-10-05 16:35:59 +020090 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +020091}
92
93static 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
Heinrich Schuchardt61da6782017-10-05 16:35:59 +020099 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200100}
101
102static 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
112static 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
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200119 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200120}
121
122static 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
138static 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
Alexander Graf712cd292016-09-06 14:26:27 +0200151#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
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200156 net_send_packet(buffer, buffer_size);
Alexander Graf712cd292016-09-06 14:26:27 +0200157#endif
158
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200159 new_tx_packet = buffer;
160
161 return EFI_EXIT(EFI_SUCCESS);
162}
163
164static void efi_net_push(void *pkt, int len)
165{
166 new_rx_packet = true;
167}
168
169static 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
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200197void 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(). */
Rob Clark95c55532017-09-13 18:05:33 -0400208int efi_net_register(void)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200209{
210 struct efi_net_obj *netobj;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200211
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;
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200222 netobj->parent.protocols[0].protocol_interface = &netobj->net;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200223 netobj->parent.protocols[1].guid = &efi_guid_device_path;
Rob Clarke15fc332017-09-13 18:05:32 -0400224 netobj->parent.protocols[1].protocol_interface =
225 efi_dp_from_eth();
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200226 netobj->parent.protocols[2].guid = &efi_pxe_guid;
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200227 netobj->parent.protocols[2].protocol_interface = &netobj->pxe;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200228 netobj->parent.handle = &netobj->net;
Heinrich Schuchardtbdecf972017-10-05 16:35:57 +0200229 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200230 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;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200245 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardt5d4a5ea2017-10-05 16:35:58 +0200246 netobj->net_mode.hwaddr_size = ARP_HLEN;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200247 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
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200256 return 0;
257}