summaryrefslogtreecommitdiffstats
path: root/sys/boot/efi/libefi/efinet.c
blob: d729981982933b37b91ab2009ccdd3904e73416a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*-
 * Copyright (c) 2001 Doug Rabson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>

#include <stand.h>
#include <net.h>
#include <netif.h>

#include <efi.h>
#include <efilib.h>

extern struct netif_driver efi_net;

int
efinet_match(struct netif *nif, void *machdep_hint)
{

	return (1);
}

int
efinet_probe(struct netif *nif, void *machdep_hint)
{

	return (0);
}

int
efinet_put(struct iodesc *desc, void *pkt, size_t len)
{
	EFI_SIMPLE_NETWORK *net = desc->io_netif->nif_devdata;
	EFI_STATUS status;
	int i;

	status = net->Transmit(net, 0, len, pkt, 0, 0, 0);
	if (!EFI_ERROR(status))
		return len;
	else
		return -1;
}


int
efinet_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
{
	EFI_SIMPLE_NETWORK *net = desc->io_netif->nif_devdata;
	EFI_STATUS status;
	UINTN bufsz;
	time_t t;

	t = time(0);
	while ((time(0) - t) < timeout) {
		bufsz = len;
		status = net->Receive(net, 0, &bufsz, pkt, 0, 0, 0);
		if (status == EFI_SUCCESS)
			return bufsz;
		if (status != EFI_NOT_READY)
			return 0;
	}

	return 0;
}

void
efinet_init(struct iodesc *desc, void *machdep_hint)
{
	struct netif		*nif;
	EFI_SIMPLE_NETWORK	*net;
	int			i;

	nif = desc->io_netif;
	net = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
	desc->io_netif->nif_devdata = net;

	net->Start(net);
	net->Initialize(net, 0, 0);

	bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
	desc->xid = 1;

	return;
}

void
efinet_init_driver()
{
	EFI_STATUS	status;
	UINTN		sz;
	static EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
	EFI_HANDLE	*handles;
	int		nifs, i;
#define MAX_INTERFACES	4
	static struct netif_dif difs[MAX_INTERFACES];
	static struct netif_stats stats[MAX_INTERFACES];

	sz = 0;
	status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, 0);
	if (status != EFI_BUFFER_TOO_SMALL)
		return;
	handles = (EFI_HANDLE *) malloc(sz);
	status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, handles);
	if (EFI_ERROR(status)) {
		free(handles);
		return;
	}

	nifs = sz / sizeof(EFI_HANDLE);
	if (nifs > MAX_INTERFACES)
		nifs = MAX_INTERFACES;

	efi_net.netif_nifs = nifs;
	efi_net.netif_ifs = difs;

	bzero(stats, sizeof(stats));
	for (i = 0; i < nifs; i++) {
		struct netif_dif *dif = &efi_net.netif_ifs[i];
		dif->dif_unit = i;
		dif->dif_nsel = 1;
		dif->dif_stats = &stats[i];
		dif->dif_private = handles[i];

		BS->HandleProtocol(handles[i], &netid,
				   (VOID**) &dif->dif_private);
	}

	return;
}

void
efinet_end(struct netif *nif)
{
	EFI_SIMPLE_NETWORK *net = nif->nif_devdata;

	net->Shutdown(net);
}

struct netif_driver efi_net = {
	"net",			/* netif_bname */
	efinet_match,		/* netif_match */
	efinet_probe,		/* netif_probe */
	efinet_init,		/* netif_init */
	efinet_get,		/* netif_get */
	efinet_put,		/* netif_put */
	efinet_end,		/* netif_end */
	0,			/* netif_ifs */
	0			/* netif_nifs */
};

OpenPOWER on IntegriCloud