2 * Copyright (c) 2001 Doug Rabson
3 * Copyright (c) 2002, 2006 Marcel Moolenaar
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <net/ethernet.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
45 static EFI_GUID sn_guid = EFI_SIMPLE_NETWORK_PROTOCOL;
47 static void efinet_end(struct netif *);
48 static ssize_t efinet_get(struct iodesc *, void **, time_t);
49 static void efinet_init(struct iodesc *, void *);
50 static int efinet_match(struct netif *, void *);
51 static int efinet_probe(struct netif *, void *);
52 static ssize_t efinet_put(struct iodesc *, void *, size_t);
54 struct netif_driver efinetif = {
55 .netif_bname = "efinet",
56 .netif_match = efinet_match,
57 .netif_probe = efinet_probe,
58 .netif_init = efinet_init,
59 .netif_get = efinet_get,
60 .netif_put = efinet_put,
61 .netif_end = efinet_end,
68 dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
72 printf("State = %x\n", mode->State);
73 printf("HwAddressSize = %u\n", mode->HwAddressSize);
74 printf("MediaHeaderSize = %u\n", mode->MediaHeaderSize);
75 printf("MaxPacketSize = %u\n", mode->MaxPacketSize);
76 printf("NvRamSize = %u\n", mode->NvRamSize);
77 printf("NvRamAccessSize = %u\n", mode->NvRamAccessSize);
78 printf("ReceiveFilterMask = %x\n", mode->ReceiveFilterMask);
79 printf("ReceiveFilterSetting = %u\n", mode->ReceiveFilterSetting);
80 printf("MaxMCastFilterCount = %u\n", mode->MaxMCastFilterCount);
81 printf("MCastFilterCount = %u\n", mode->MCastFilterCount);
82 printf("MCastFilter = {");
83 for (i = 0; i < mode->MCastFilterCount; i++)
84 printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
86 printf("CurrentAddress = %s\n",
87 ether_sprintf(mode->CurrentAddress.Addr));
88 printf("BroadcastAddress = %s\n",
89 ether_sprintf(mode->BroadcastAddress.Addr));
90 printf("PermanentAddress = %s\n",
91 ether_sprintf(mode->PermanentAddress.Addr));
92 printf("IfType = %u\n", mode->IfType);
93 printf("MacAddressChangeable = %d\n", mode->MacAddressChangeable);
94 printf("MultipleTxSupported = %d\n", mode->MultipleTxSupported);
95 printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
96 printf("MediaPresent = %d\n", mode->MediaPresent);
101 efinet_match(struct netif *nif, void *machdep_hint)
103 struct devdesc *dev = machdep_hint;
105 if (dev->d_unit == nif->nif_unit)
111 efinet_probe(struct netif *nif, void *machdep_hint)
113 EFI_SIMPLE_NETWORK *net;
117 h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
119 * Open the network device in exclusive mode. Without this
120 * we will be racing with the UEFI network stack. It will
121 * pull packets off the network leading to lost packets.
123 status = BS->OpenProtocol(h, &sn_guid, (void **)&net,
124 IH, NULL, EFI_OPEN_PROTOCOL_EXCLUSIVE);
125 if (status != EFI_SUCCESS) {
126 printf("Unable to open network interface %d for "
127 "exclusive access: %lu\n", nif->nif_unit,
128 EFI_ERROR_CODE(status));
129 return (efi_status_to_errno(status));
136 efinet_put(struct iodesc *desc, void *pkt, size_t len)
138 struct netif *nif = desc->io_netif;
139 EFI_SIMPLE_NETWORK *net;
143 net = nif->nif_devdata;
147 status = net->Transmit(net, 0, len, pkt, NULL, NULL, NULL);
148 if (status != EFI_SUCCESS)
151 /* Wait for the buffer to be transmitted */
153 buf = NULL; /* XXX Is this needed? */
154 status = net->GetStatus(net, NULL, &buf);
156 * XXX EFI1.1 and the E1000 card returns a different
157 * address than we gave. Sigh.
159 } while (status == EFI_SUCCESS && buf == NULL);
161 /* XXX How do we deal with status != EFI_SUCCESS now? */
162 return ((status == EFI_SUCCESS) ? len : -1);
166 efinet_get(struct iodesc *desc, void **pkt, time_t timeout)
168 struct netif *nif = desc->io_netif;
169 EFI_SIMPLE_NETWORK *net;
176 net = nif->nif_devdata;
180 bufsz = net->Mode->MaxPacketSize + ETHER_HDR_LEN + ETHER_CRC_LEN;
181 buf = malloc(bufsz + ETHER_ALIGN);
184 ptr = buf + ETHER_ALIGN;
187 while ((getsecs() - t) < timeout) {
188 status = net->Receive(net, NULL, &bufsz, ptr, NULL, NULL, NULL);
189 if (status == EFI_SUCCESS) {
191 ret = (ssize_t)bufsz;
194 if (status != EFI_NOT_READY)
204 * Loader uses BOOTP/DHCP and also uses RARP as a fallback to populate
205 * network parameters and problems with DHCP servers can cause the loader
206 * to fail to populate them. Allow the device to ask about the basic
207 * network parameters and if present use them.
210 efi_env_net_params(struct iodesc *desc)
213 in_addr_t ipaddr, mask, gwaddr, serveraddr;
216 if ((envstr = getenv("rootpath")) != NULL)
217 strlcpy(rootpath, envstr, sizeof(rootpath));
220 * Get network parameters.
222 envstr = getenv("ipaddr");
223 ipaddr = (envstr != NULL) ? inet_addr(envstr) : 0;
225 envstr = getenv("netmask");
226 mask = (envstr != NULL) ? inet_addr(envstr) : 0;
228 envstr = getenv("gatewayip");
229 gwaddr = (envstr != NULL) ? inet_addr(envstr) : 0;
231 envstr = getenv("serverip");
232 serveraddr = (envstr != NULL) ? inet_addr(envstr) : 0;
234 /* No network params. */
235 if (ipaddr == 0 && mask == 0 && gwaddr == 0 && serveraddr == 0)
238 /* Partial network params. */
239 if (ipaddr == 0 || mask == 0 || gwaddr == 0 || serveraddr == 0) {
240 printf("Incomplete network settings from U-Boot\n");
245 * Set network parameters.
247 myip.s_addr = ipaddr;
249 gateip.s_addr = gwaddr;
250 servip.s_addr = serveraddr;
253 * There must be a rootpath. It may be ip:/path or it may be just the
254 * path in which case the ip needs to be serverip.
256 rootaddr = net_parse_rootpath();
257 if (rootaddr == INADDR_NONE)
258 rootaddr = serveraddr;
259 rootip.s_addr = rootaddr;
262 printf("%s: ip=%s\n", __func__, inet_ntoa(myip));
263 printf("%s: mask=%s\n", __func__, intoa(netmask));
264 printf("%s: gateway=%s\n", __func__, inet_ntoa(gateip));
265 printf("%s: server=%s\n", __func__, inet_ntoa(servip));
272 efinet_init(struct iodesc *desc, void *machdep_hint)
274 struct netif *nif = desc->io_netif;
275 EFI_SIMPLE_NETWORK *net;
280 /* Attempt to get netboot params from env */
281 efi_env_net_params(desc);
283 if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) {
284 printf("Invalid network interface %d\n", nif->nif_unit);
288 h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
289 status = OpenProtocolByHandle(h, &sn_guid, (void **)&nif->nif_devdata);
290 if (status != EFI_SUCCESS) {
291 printf("net%d: cannot fetch interface data (status=%lu)\n",
292 nif->nif_unit, EFI_ERROR_CODE(status));
296 net = nif->nif_devdata;
297 if (net->Mode->State == EfiSimpleNetworkStopped) {
298 status = net->Start(net);
299 if (status != EFI_SUCCESS) {
300 printf("net%d: cannot start interface (status=%lu)\n",
301 nif->nif_unit, EFI_ERROR_CODE(status));
306 if (net->Mode->State != EfiSimpleNetworkInitialized) {
307 status = net->Initialize(net, 0, 0);
308 if (status != EFI_SUCCESS) {
309 printf("net%d: cannot init. interface (status=%lu)\n",
310 nif->nif_unit, EFI_ERROR_CODE(status));
315 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
316 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
318 status = net->ReceiveFilters(net, mask, 0, FALSE, 0, NULL);
319 if (status != EFI_SUCCESS)
320 printf("net%d: cannot set rx. filters (status=%lu)\n",
321 nif->nif_unit, EFI_ERROR_CODE(status));
324 dump_mode(net->Mode);
327 bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
332 efinet_end(struct netif *nif)
334 EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
342 static int efinet_dev_init(void);
343 static int efinet_dev_print(int);
345 struct devsw efinet_dev = {
348 .dv_init = efinet_dev_init,
349 .dv_strategy = NULL, /* Will be set in efinet_dev_init */
350 .dv_open = NULL, /* Will be set in efinet_dev_init */
351 .dv_close = NULL, /* Will be set in efinet_dev_init */
353 .dv_print = efinet_dev_print,
360 struct netif_dif *dif;
361 struct netif_stats *stats;
362 EFI_DEVICE_PATH *devpath, *node;
363 EFI_HANDLE *handles, *handles2;
367 extern struct devsw netdev;
371 status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz, NULL);
372 if (status == EFI_BUFFER_TOO_SMALL) {
373 handles = (EFI_HANDLE *)malloc(sz);
376 status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz,
378 if (EFI_ERROR(status))
381 if (EFI_ERROR(status))
382 return (efi_status_to_errno(status));
383 handles2 = (EFI_HANDLE *)malloc(sz);
384 if (handles2 == NULL) {
389 for (i = 0; i < sz / sizeof(EFI_HANDLE); i++) {
390 devpath = efi_lookup_devpath(handles[i]);
393 if ((node = efi_devpath_last_node(devpath)) == NULL)
396 if (DevicePathType(node) != MESSAGING_DEVICE_PATH ||
397 DevicePathSubType(node) != MSG_MAC_ADDR_DP)
400 handles2[nifs] = handles[i];
409 err = efi_register_handles(&efinet_dev, handles2, NULL, nifs);
413 efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif));
414 stats = calloc(nifs, sizeof(struct netif_stats));
415 if (efinetif.netif_ifs == NULL || stats == NULL) {
416 free(efinetif.netif_ifs);
418 efinetif.netif_ifs = NULL;
422 efinetif.netif_nifs = nifs;
424 for (i = 0; i < nifs; i++) {
426 dif = &efinetif.netif_ifs[i];
429 dif->dif_stats = &stats[i];
430 dif->dif_private = handles2[i];
433 efinet_dev.dv_open = netdev.dv_open;
434 efinet_dev.dv_close = netdev.dv_close;
435 efinet_dev.dv_strategy = netdev.dv_strategy;
443 efinet_dev_print(int verbose)
449 printf("%s devices:", efinet_dev.dv_name);
450 if ((ret = pager_output("\n")) != 0)
453 for (unit = 0, h = efi_find_handle(&efinet_dev, 0);
454 h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) {
455 printf(" %s%d:", efinet_dev.dv_name, unit);
457 text = efi_devpath_name(efi_lookup_devpath(h));
460 efi_free_devpath_name(text);
463 if ((ret = pager_output("\n")) != 0)