2011-06-26 20:38:43 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-09-16 19:57:31 +00:00
|
|
|
#include <grub/net/netbuff.h>
|
|
|
|
#include <grub/ieee1275/ieee1275.h>
|
2011-05-10 12:45:57 +00:00
|
|
|
#include <grub/dl.h>
|
2010-09-16 19:57:31 +00:00
|
|
|
#include <grub/net.h>
|
2011-05-10 12:45:57 +00:00
|
|
|
#include <grub/time.h>
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-05-12 18:40:54 +00:00
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
2011-07-05 14:37:14 +00:00
|
|
|
struct grub_ofnetcard_data
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
grub_ieee1275_ihandle_t handle;
|
|
|
|
grub_uint32_t mtu;
|
|
|
|
};
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
static grub_err_t
|
2011-07-05 14:37:14 +00:00
|
|
|
card_open (const struct grub_net_card *dev)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2010-09-22 15:14:14 +00:00
|
|
|
int status;
|
2010-09-16 19:57:31 +00:00
|
|
|
struct grub_ofnetcard_data *data = dev->data;
|
2011-06-08 00:59:53 +00:00
|
|
|
char path[grub_strlen (data->path) +
|
|
|
|
grub_strlen (":speed=auto,duplex=auto,1.1.1.1,dummy,1.1.1.1,1.1.1.1,5,5,1.1.1.1,512") + 1];
|
|
|
|
|
2011-05-10 12:45:57 +00:00
|
|
|
/* The full string will prevent a bootp packet to be sent. Just put some valid ip in there. */
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_snprintf (path, sizeof (path), "%s%s", data->path,
|
|
|
|
":speed=auto,duplex=auto,1.1.1.1,dummy,1.1.1.1,1.1.1.1,5,5,1.1.1.1,512");
|
|
|
|
status = grub_ieee1275_open (path, &(data->handle));
|
2010-09-22 15:14:14 +00:00
|
|
|
|
|
|
|
if (status)
|
2011-05-10 12:45:57 +00:00
|
|
|
return grub_error (GRUB_ERR_IO, "Couldn't open network card.");
|
|
|
|
|
2010-09-22 15:14:14 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-07-05 14:37:14 +00:00
|
|
|
static void
|
|
|
|
card_close (const struct grub_net_card *dev)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
|
|
|
struct grub_ofnetcard_data *data = dev->data;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2010-09-16 19:57:31 +00:00
|
|
|
if (data->handle)
|
|
|
|
grub_ieee1275_close (data->handle);
|
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
static grub_err_t
|
2011-06-27 06:40:17 +00:00
|
|
|
send_card_buffer (const struct grub_net_card *dev, struct grub_net_buff *pack)
|
2011-06-08 00:59:53 +00:00
|
|
|
{
|
2011-06-27 10:13:21 +00:00
|
|
|
grub_ssize_t actual;
|
2010-09-22 15:14:14 +00:00
|
|
|
int status;
|
2010-09-16 19:57:31 +00:00
|
|
|
struct grub_ofnetcard_data *data = dev->data;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2010-09-22 15:14:14 +00:00
|
|
|
status = grub_ieee1275_write (data->handle, pack->data,
|
|
|
|
pack->tail - pack->data, &actual);
|
|
|
|
|
|
|
|
if (status)
|
2011-05-10 12:50:18 +00:00
|
|
|
return grub_error (GRUB_ERR_IO, "Couldn't send network packet.");
|
2010-09-22 15:14:14 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-05-19 13:39:34 +00:00
|
|
|
static grub_ssize_t
|
2011-06-27 06:40:17 +00:00
|
|
|
get_card_packet (const struct grub_net_card *dev, struct grub_net_buff *nb)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-06-27 10:13:21 +00:00
|
|
|
grub_ssize_t actual;
|
|
|
|
int rc;
|
2010-09-16 19:57:31 +00:00
|
|
|
struct grub_ofnetcard_data *data = dev->data;
|
2011-05-10 12:50:18 +00:00
|
|
|
grub_uint64_t start_time;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_netbuff_clear (nb);
|
2011-05-10 12:50:18 +00:00
|
|
|
start_time = grub_get_time_ms ();
|
2010-09-16 19:57:31 +00:00
|
|
|
do
|
2011-05-10 12:50:18 +00:00
|
|
|
rc = grub_ieee1275_read (data->handle, nb->data, data->mtu, &actual);
|
|
|
|
while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));
|
|
|
|
if (actual)
|
|
|
|
{
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_netbuff_put (nb, actual);
|
2011-05-19 13:39:34 +00:00
|
|
|
return actual;
|
2011-05-10 12:50:18 +00:00
|
|
|
}
|
2011-05-19 13:39:34 +00:00
|
|
|
return -1;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
static struct grub_net_card_driver ofdriver =
|
|
|
|
{
|
|
|
|
.name = "ofnet",
|
2011-07-05 14:37:14 +00:00
|
|
|
.open = card_open,
|
|
|
|
.close = card_close,
|
2011-06-08 00:59:53 +00:00
|
|
|
.send = send_card_buffer,
|
|
|
|
.recv = get_card_packet
|
|
|
|
};
|
2010-09-16 19:57:31 +00:00
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
2011-12-13 00:07:06 +00:00
|
|
|
const char *name;
|
2010-09-16 19:57:31 +00:00
|
|
|
int offset;
|
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
bootp_response_properties[] =
|
|
|
|
{
|
|
|
|
{ .name = "bootp-response", .offset = 0},
|
|
|
|
{ .name = "dhcp-response", .offset = 0},
|
|
|
|
{ .name = "bootpreply-packet", .offset = 0x2a},
|
|
|
|
};
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-07-05 14:37:14 +00:00
|
|
|
static void
|
|
|
|
grub_ieee1275_net_config_real (const char *devpath, char **device, char **path)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-07-05 14:37:14 +00:00
|
|
|
struct grub_net_card *card;
|
|
|
|
|
|
|
|
/* FIXME: Check that it's the right card. */
|
|
|
|
FOR_NET_CARDS (card)
|
|
|
|
{
|
|
|
|
char *bootp_response;
|
|
|
|
char *cardpath;
|
|
|
|
char *canon;
|
|
|
|
|
|
|
|
grub_ssize_t size = -1;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (card->driver != &ofdriver)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
cardpath = ((struct grub_ofnetcard_data *) card->data)->path;
|
|
|
|
canon = grub_ieee1275_canonicalise_devname (cardpath);
|
|
|
|
if (grub_strcmp (devpath, canon) != 0)
|
|
|
|
{
|
|
|
|
grub_free (canon);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
grub_free (canon);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE (bootp_response_properties); i++)
|
|
|
|
if (grub_ieee1275_get_property_length (grub_ieee1275_chosen,
|
|
|
|
bootp_response_properties[i].name,
|
|
|
|
&size) >= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (size < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bootp_response = grub_malloc (size);
|
|
|
|
if (!bootp_response)
|
|
|
|
{
|
|
|
|
grub_print_error ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (grub_ieee1275_get_property (grub_ieee1275_chosen,
|
|
|
|
bootp_response_properties[i].name,
|
|
|
|
bootp_response, size, 0) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
grub_net_configure_by_dhcp_ack (card->name, card, 0,
|
|
|
|
(struct grub_net_bootp_packet *)
|
|
|
|
&bootp_response
|
|
|
|
+ bootp_response_properties[i].offset,
|
|
|
|
size - bootp_response_properties[i].offset,
|
|
|
|
1, device, path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
find_alias (const char *fullname)
|
|
|
|
{
|
|
|
|
char *ret = NULL;
|
|
|
|
auto int find_alias_hook (struct grub_ieee1275_devalias *alias);
|
|
|
|
|
|
|
|
int find_alias_hook (struct grub_ieee1275_devalias *alias)
|
|
|
|
{
|
|
|
|
if (grub_strcmp (alias->path, fullname) == 0)
|
|
|
|
{
|
|
|
|
ret = grub_strdup (alias->name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_devalias_iterate (find_alias_hook);
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
return ret;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
static void
|
|
|
|
grub_ofnet_findcards (void)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
|
|
|
auto int search_net_devices (struct grub_ieee1275_devalias *alias);
|
|
|
|
|
|
|
|
int search_net_devices (struct grub_ieee1275_devalias *alias)
|
2011-06-08 00:59:53 +00:00
|
|
|
{
|
|
|
|
if (!grub_strcmp (alias->type, "network"))
|
|
|
|
{
|
2011-06-26 20:38:43 +00:00
|
|
|
struct grub_ofnetcard_data *ofdata;
|
|
|
|
struct grub_net_card *card;
|
|
|
|
grub_ieee1275_phandle_t devhandle;
|
|
|
|
grub_net_link_level_address_t lla;
|
2011-07-05 14:37:14 +00:00
|
|
|
char *shortname;
|
2011-06-26 20:38:43 +00:00
|
|
|
|
|
|
|
ofdata = grub_malloc (sizeof (struct grub_ofnetcard_data));
|
|
|
|
if (!ofdata)
|
|
|
|
{
|
|
|
|
grub_print_error ();
|
|
|
|
return 1;
|
|
|
|
}
|
2011-07-05 14:37:14 +00:00
|
|
|
card = grub_zalloc (sizeof (struct grub_net_card));
|
2011-06-26 20:38:43 +00:00
|
|
|
if (!card)
|
|
|
|
{
|
|
|
|
grub_free (ofdata);
|
|
|
|
grub_print_error ();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
ofdata->path = grub_strdup (alias->path);
|
|
|
|
|
|
|
|
grub_ieee1275_finddevice (ofdata->path, &devhandle);
|
|
|
|
|
|
|
|
if (grub_ieee1275_get_integer_property
|
|
|
|
(devhandle, "max-frame-size", &(ofdata->mtu),
|
|
|
|
sizeof (ofdata->mtu), 0))
|
2011-07-05 14:37:14 +00:00
|
|
|
{
|
|
|
|
ofdata->mtu = 1500;
|
|
|
|
}
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2011-07-05 14:37:14 +00:00
|
|
|
if (grub_ieee1275_get_property (devhandle, "mac-address",
|
|
|
|
&(lla.mac), 6, 0)
|
|
|
|
&& grub_ieee1275_get_property (devhandle, "local-mac-address",
|
|
|
|
&(lla.mac), 6, 0))
|
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_IO, "Couldn't retrieve mac address.");
|
|
|
|
grub_print_error ();
|
|
|
|
return 0;
|
|
|
|
}
|
2011-06-08 00:59:53 +00:00
|
|
|
|
|
|
|
lla.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
|
|
|
|
card->default_address = lla;
|
|
|
|
|
|
|
|
card->driver = NULL;
|
|
|
|
card->data = ofdata;
|
2011-04-01 08:39:06 +00:00
|
|
|
card->flags = 0;
|
2011-07-05 14:37:14 +00:00
|
|
|
shortname = find_alias (alias->path);
|
|
|
|
card->name = grub_xasprintf ("ofnet_%s", shortname ? : alias->path);
|
2011-07-06 16:21:07 +00:00
|
|
|
card->idle_poll_delay_ms = 1;
|
2011-07-05 14:37:14 +00:00
|
|
|
grub_free (shortname);
|
|
|
|
|
|
|
|
card->driver = &ofdriver;
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_net_card_register (card);
|
2011-05-10 12:54:32 +00:00
|
|
|
return 0;
|
2011-06-08 00:59:53 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-04-01 08:39:06 +00:00
|
|
|
/* Look at all nodes for devices of the type network. */
|
2010-09-16 19:57:31 +00:00
|
|
|
grub_ieee1275_devices_iterate (search_net_devices);
|
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
GRUB_MOD_INIT(ofnet)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_ofnet_findcards ();
|
2011-07-05 14:37:14 +00:00
|
|
|
grub_ieee1275_net_config = grub_ieee1275_net_config_real;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
GRUB_MOD_FINI(ofnet)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-07-05 14:37:14 +00:00
|
|
|
struct grub_net_card *card, *next;
|
|
|
|
|
|
|
|
FOR_NET_CARDS_SAFE (card, next)
|
|
|
|
if (card->driver && grub_strcmp (card->driver->name, "ofnet") == 0)
|
|
|
|
grub_net_card_unregister (card);
|
|
|
|
grub_ieee1275_net_config = 0;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|