grub/grub-core/net/drivers/efi/efinet.c
Vladimir 'phcoder' Serbinenko 9d22909b85 efinet support
2011-06-26 22:42:04 +02:00

165 lines
4.1 KiB
C

/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2010,2011 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/net/netbuff.h>
#include <grub/dl.h>
#include <grub/net.h>
#include <grub/time.h>
#include <grub/efi/api.h>
#include <grub/efi/efi.h>
GRUB_MOD_LICENSE ("GPLv3+");
/* GUID. */
static grub_efi_guid_t net_io_guid = GRUB_EFI_SIMPLE_NETWORK_GUID;
static grub_err_t
send_card_buffer (const struct grub_net_card *dev,
struct grub_net_buff *pack)
{
grub_efi_status_t st;
grub_efi_simple_network_t *net = dev->data;
st = efi_call_7 (net->transmit, net, 0, pack->tail - pack->data,
pack->data, NULL, NULL, NULL);
if (st != GRUB_EFI_SUCCESS)
return grub_error (GRUB_ERR_IO, "Couldn't send network packet.");
return GRUB_ERR_NONE;
}
static grub_ssize_t
get_card_packet (const struct grub_net_card *dev,
struct grub_net_buff *nb)
{
grub_efi_simple_network_t *net = dev->data;
grub_err_t err;
grub_efi_status_t st;
grub_efi_uintn_t bufsize = 1500;
err = grub_netbuff_clear (nb);
if (err)
return -1;
err = grub_netbuff_put (nb, 1500);
if (err)
return -1;
st = efi_call_7 (net->receive, net, NULL, &bufsize,
nb->data, NULL, NULL, NULL);
if (st == GRUB_EFI_BUFFER_TOO_SMALL)
{
err = grub_netbuff_put (nb, bufsize - 1500);
if (err)
return -1;
st = efi_call_7 (net->receive, net, NULL, &bufsize,
nb->data, NULL, NULL, NULL);
}
if (st != GRUB_EFI_SUCCESS)
{
grub_netbuff_clear (nb);
return -1;
}
err = grub_netbuff_unput (nb, (nb->tail - nb->data) - bufsize);
if (err)
return -1;
return bufsize;
}
static struct grub_net_card_driver efidriver =
{
.name = "efinet",
.send = send_card_buffer,
.recv = get_card_packet
};
static void
grub_efinet_findcards (void)
{
grub_efi_uintn_t num_handles;
grub_efi_handle_t *handles;
grub_efi_handle_t *handle;
int i = 0;
/* Find handles which support the disk io interface. */
handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &net_io_guid,
0, &num_handles);
if (! handles)
return;
for (handle = handles; num_handles--; handle++)
{
grub_efi_simple_network_t *net;
struct grub_net_card *card;
net = grub_efi_open_protocol (*handle, &net_io_guid,
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (! net)
/* This should not happen... Why? */
continue;
if (net->mode->state == GRUB_EFI_NETWORK_STOPPED
&& efi_call_1 (net->start, net) != GRUB_EFI_SUCCESS)
continue;
if (net->mode->state == GRUB_EFI_NETWORK_STOPPED)
continue;
if (net->mode->state == GRUB_EFI_NETWORK_STARTED
&& efi_call_3 (net->initialize, net, 0, 0) != GRUB_EFI_SUCCESS)
continue;
card = grub_zalloc (sizeof (struct grub_net_card));
if (!card)
{
grub_print_error ();
grub_free (handles);
return;
}
card->name = grub_xasprintf ("efinet%d", i++);
card->driver = &efidriver;
card->flags = 0;
card->default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
grub_memcpy (card->default_address.mac,
net->mode->current_address,
sizeof (card->default_address.mac));
card->data = net;
grub_net_card_register (card);
}
grub_free (handles);
}
GRUB_MOD_INIT(efinet)
{
grub_efinet_findcards ();
}
GRUB_MOD_FINI(ofnet)
{
struct grub_net_card *card;
FOR_NET_CARDS (card)
if (card->driver && !grub_strcmp (card->driver->name, "efinet"))
{
card->driver->fini (card);
card->driver = NULL;
}
grub_net_card_driver_unregister (&efidriver);
}