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/param.h>
29 #include <net/ethernet.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
42 static EFI_GUID sn_guid = EFI_SIMPLE_NETWORK_PROTOCOL;
44 static void efinet_end(struct netif *);
45 static ssize_t efinet_get(struct iodesc *, void **, time_t);
46 static void efinet_init(struct iodesc *, void *);
47 static int efinet_match(struct netif *, void *);
48 static int efinet_probe(struct netif *, void *);
49 static ssize_t efinet_put(struct iodesc *, void *, size_t);
51 struct netif_driver efinetif = {
52 .netif_bname = "efinet",
53 .netif_match = efinet_match,
54 .netif_probe = efinet_probe,
55 .netif_init = efinet_init,
56 .netif_get = efinet_get,
57 .netif_put = efinet_put,
58 .netif_end = efinet_end,
65 dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
69 printf("State = %x\n", mode->State);
70 printf("HwAddressSize = %u\n", mode->HwAddressSize);
71 printf("MediaHeaderSize = %u\n", mode->MediaHeaderSize);
72 printf("MaxPacketSize = %u\n", mode->MaxPacketSize);
73 printf("NvRamSize = %u\n", mode->NvRamSize);
74 printf("NvRamAccessSize = %u\n", mode->NvRamAccessSize);
75 printf("ReceiveFilterMask = %x\n", mode->ReceiveFilterMask);
76 printf("ReceiveFilterSetting = %u\n", mode->ReceiveFilterSetting);
77 printf("MaxMCastFilterCount = %u\n", mode->MaxMCastFilterCount);
78 printf("MCastFilterCount = %u\n", mode->MCastFilterCount);
79 printf("MCastFilter = {");
80 for (i = 0; i < mode->MCastFilterCount; i++)
81 printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
83 printf("CurrentAddress = %s\n",
84 ether_sprintf(mode->CurrentAddress.Addr));
85 printf("BroadcastAddress = %s\n",
86 ether_sprintf(mode->BroadcastAddress.Addr));
87 printf("PermanentAddress = %s\n",
88 ether_sprintf(mode->PermanentAddress.Addr));
89 printf("IfType = %u\n", mode->IfType);
90 printf("MacAddressChangeable = %d\n", mode->MacAddressChangeable);
91 printf("MultipleTxSupported = %d\n", mode->MultipleTxSupported);
92 printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
93 printf("MediaPresent = %d\n", mode->MediaPresent);
98 efinet_match(struct netif *nif, void *machdep_hint)
100 struct devdesc *dev = machdep_hint;
102 if (dev->d_unit == nif->nif_unit)
108 efinet_probe(struct netif *nif, void *machdep_hint)
110 EFI_SIMPLE_NETWORK *net;
114 h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
116 * Open the network device in exclusive mode. Without this
117 * we will be racing with the UEFI network stack. It will
118 * pull packets off the network leading to lost packets.
120 status = BS->OpenProtocol(h, &sn_guid, (void **)&net,
121 IH, NULL, EFI_OPEN_PROTOCOL_EXCLUSIVE);
122 if (status != EFI_SUCCESS) {
123 printf("Unable to open network interface %d for "
124 "exclusive access: %lu\n", nif->nif_unit,
125 EFI_ERROR_CODE(status));
126 return (efi_status_to_errno(status));
133 efinet_put(struct iodesc *desc, void *pkt, size_t len)
135 struct netif *nif = desc->io_netif;
136 EFI_SIMPLE_NETWORK *net;
140 net = nif->nif_devdata;
144 status = net->Transmit(net, 0, len, pkt, NULL, NULL, NULL);
145 if (status != EFI_SUCCESS)
148 /* Wait for the buffer to be transmitted */
150 buf = NULL; /* XXX Is this needed? */
151 status = net->GetStatus(net, NULL, &buf);
153 * XXX EFI1.1 and the E1000 card returns a different
154 * address than we gave. Sigh.
156 } while (status == EFI_SUCCESS && buf == NULL);
158 /* XXX How do we deal with status != EFI_SUCCESS now? */
159 return ((status == EFI_SUCCESS) ? len : -1);
163 efinet_get(struct iodesc *desc, void **pkt, time_t timeout)
165 struct netif *nif = desc->io_netif;
166 EFI_SIMPLE_NETWORK *net;
173 net = nif->nif_devdata;
177 bufsz = net->Mode->MaxPacketSize + ETHER_HDR_LEN + ETHER_CRC_LEN;
178 buf = malloc(bufsz + ETHER_ALIGN);
181 ptr = buf + ETHER_ALIGN;
184 while ((getsecs() - t) < timeout) {
185 status = net->Receive(net, NULL, &bufsz, ptr, NULL, NULL, NULL);
186 if (status == EFI_SUCCESS) {
188 ret = (ssize_t)bufsz;
191 if (status != EFI_NOT_READY)
201 * Loader uses BOOTP/DHCP and also uses RARP as a fallback to populate
202 * network parameters and problems with DHCP servers can cause the loader
203 * to fail to populate them. Allow the device to ask about the basic
204 * network parameters and if present use them.
207 efi_env_net_params(struct iodesc *desc)
210 in_addr_t ipaddr, mask, gwaddr, serveraddr;
213 if ((envstr = getenv("rootpath")) != NULL)
214 strlcpy(rootpath, envstr, sizeof(rootpath));
217 * Get network parameters.
219 envstr = getenv("ipaddr");
220 ipaddr = (envstr != NULL) ? inet_addr(envstr) : 0;
222 envstr = getenv("netmask");
223 mask = (envstr != NULL) ? inet_addr(envstr) : 0;
225 envstr = getenv("gatewayip");
226 gwaddr = (envstr != NULL) ? inet_addr(envstr) : 0;
228 envstr = getenv("serverip");
229 serveraddr = (envstr != NULL) ? inet_addr(envstr) : 0;
231 /* No network params. */
232 if (ipaddr == 0 && mask == 0 && gwaddr == 0 && serveraddr == 0)
235 /* Partial network params. */
236 if (ipaddr == 0 || mask == 0 || gwaddr == 0 || serveraddr == 0) {
237 printf("Incomplete network settings from U-Boot\n");
242 * Set network parameters.
244 myip.s_addr = ipaddr;
246 gateip.s_addr = gwaddr;
247 servip.s_addr = serveraddr;
250 * There must be a rootpath. It may be ip:/path or it may be just the
251 * path in which case the ip needs to be serverip.
253 rootaddr = net_parse_rootpath();
254 if (rootaddr == INADDR_NONE)
255 rootaddr = serveraddr;
256 rootip.s_addr = rootaddr;
259 printf("%s: ip=%s\n", __func__, inet_ntoa(myip));
260 printf("%s: mask=%s\n", __func__, intoa(netmask));
261 printf("%s: gateway=%s\n", __func__, inet_ntoa(gateip));
262 printf("%s: server=%s\n", __func__, inet_ntoa(servip));
269 efinet_init(struct iodesc *desc, void *machdep_hint)
271 struct netif *nif = desc->io_netif;
272 EFI_SIMPLE_NETWORK *net;
277 /* Attempt to get netboot params from env */
278 efi_env_net_params(desc);
280 if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) {
281 printf("Invalid network interface %d\n", nif->nif_unit);
285 h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
286 status = OpenProtocolByHandle(h, &sn_guid, (void **)&nif->nif_devdata);
287 if (status != EFI_SUCCESS) {
288 printf("net%d: cannot fetch interface data (status=%lu)\n",
289 nif->nif_unit, EFI_ERROR_CODE(status));
293 net = nif->nif_devdata;
294 if (net->Mode->State == EfiSimpleNetworkStopped) {
295 status = net->Start(net);
296 if (status != EFI_SUCCESS) {
297 printf("net%d: cannot start interface (status=%lu)\n",
298 nif->nif_unit, EFI_ERROR_CODE(status));
303 if (net->Mode->State != EfiSimpleNetworkInitialized) {
304 status = net->Initialize(net, 0, 0);
305 if (status != EFI_SUCCESS) {
306 printf("net%d: cannot init. interface (status=%lu)\n",
307 nif->nif_unit, EFI_ERROR_CODE(status));
312 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
313 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
315 status = net->ReceiveFilters(net, mask, 0, FALSE, 0, NULL);
316 if (status != EFI_SUCCESS)
317 printf("net%d: cannot set rx. filters (status=%lu)\n",
318 nif->nif_unit, EFI_ERROR_CODE(status));
321 dump_mode(net->Mode);
324 bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
329 efinet_end(struct netif *nif)
331 EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
339 static int efinet_dev_init(void);
340 static int efinet_dev_print(int);
342 struct devsw efinet_dev = {
345 .dv_init = efinet_dev_init,
346 .dv_strategy = NULL, /* Will be set in efinet_dev_init */
347 .dv_open = NULL, /* Will be set in efinet_dev_init */
348 .dv_close = NULL, /* Will be set in efinet_dev_init */
350 .dv_print = efinet_dev_print,
351 .dv_cleanup = nullsys,
357 struct netif_dif *dif;
358 struct netif_stats *stats;
359 EFI_DEVICE_PATH *devpath, *node;
360 EFI_HANDLE *handles, *handles2;
364 extern struct devsw netdev;
368 status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz, NULL);
369 if (status == EFI_BUFFER_TOO_SMALL) {
370 handles = (EFI_HANDLE *)malloc(sz);
373 status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz,
375 if (EFI_ERROR(status))
378 if (EFI_ERROR(status))
379 return (efi_status_to_errno(status));
380 handles2 = (EFI_HANDLE *)malloc(sz);
381 if (handles2 == NULL) {
386 for (i = 0; i < sz / sizeof(EFI_HANDLE); i++) {
387 devpath = efi_lookup_devpath(handles[i]);
390 if ((node = efi_devpath_last_node(devpath)) == NULL)
393 if (DevicePathType(node) != MESSAGING_DEVICE_PATH ||
394 DevicePathSubType(node) != MSG_MAC_ADDR_DP)
397 handles2[nifs] = handles[i];
406 err = efi_register_handles(&efinet_dev, handles2, NULL, nifs);
410 efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif));
411 stats = calloc(nifs, sizeof(struct netif_stats));
412 if (efinetif.netif_ifs == NULL || stats == NULL) {
413 free(efinetif.netif_ifs);
415 efinetif.netif_ifs = NULL;
419 efinetif.netif_nifs = nifs;
421 for (i = 0; i < nifs; i++) {
423 dif = &efinetif.netif_ifs[i];
426 dif->dif_stats = &stats[i];
427 dif->dif_private = handles2[i];
430 efinet_dev.dv_open = netdev.dv_open;
431 efinet_dev.dv_close = netdev.dv_close;
432 efinet_dev.dv_strategy = netdev.dv_strategy;
440 efinet_dev_print(int verbose)
446 printf("%s devices:", efinet_dev.dv_name);
447 if ((ret = pager_output("\n")) != 0)
450 for (unit = 0, h = efi_find_handle(&efinet_dev, 0);
451 h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) {
452 printf(" %s%d:", efinet_dev.dv_name, unit);
454 text = efi_devpath_name(efi_lookup_devpath(h));
457 efi_free_devpath_name(text);
460 if ((ret = pager_output("\n")) != 0)