2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2010,2011 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/net/netbuff.h>
2N/A#include <grub/ieee1275/ieee1275.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/net.h>
2N/A#include <grub/time.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astruct grub_ofnetcard_data
2N/A{
2N/A char *path;
2N/A grub_ieee1275_ihandle_t handle;
2N/A};
2N/A
2N/Astatic grub_err_t
2N/Acard_open (const struct grub_net_card *dev)
2N/A{
2N/A int status;
2N/A struct grub_ofnetcard_data *data = dev->data;
2N/A char path[grub_strlen (data->path) +
2N/A 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];
2N/A
2N/A /* The full string will prevent a bootp packet to be sent. Just put some valid ip in there. */
2N/A grub_snprintf (path, sizeof (path), "%s%s", data->path,
2N/A ":speed=auto,duplex=auto,1.1.1.1,dummy,1.1.1.1,1.1.1.1,5,5,1.1.1.1,512");
2N/A status = grub_ieee1275_open (path, &(data->handle));
2N/A
2N/A if (status)
2N/A return grub_error (GRUB_ERR_IO, "Couldn't open network card.");
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic void
2N/Acard_close (const struct grub_net_card *dev)
2N/A{
2N/A struct grub_ofnetcard_data *data = dev->data;
2N/A
2N/A if (data->handle)
2N/A grub_ieee1275_close (data->handle);
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Asend_card_buffer (const struct grub_net_card *dev, struct grub_net_buff *pack)
2N/A{
2N/A grub_ssize_t actual;
2N/A int status;
2N/A struct grub_ofnetcard_data *data = dev->data;
2N/A
2N/A status = grub_ieee1275_write (data->handle, pack->data,
2N/A pack->tail - pack->data, &actual);
2N/A
2N/A if (status)
2N/A return grub_error (GRUB_ERR_IO, "Couldn't send network packet.");
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic struct grub_net_buff *
2N/Aget_card_packet (const struct grub_net_card *dev)
2N/A{
2N/A grub_ssize_t actual;
2N/A int rc;
2N/A struct grub_ofnetcard_data *data = dev->data;
2N/A grub_uint64_t start_time;
2N/A struct grub_net_buff *nb;
2N/A
2N/A nb = grub_netbuff_alloc (dev->mtu + 64);
2N/A /* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
2N/A by 4. So that IP header is aligned on 4 bytes. */
2N/A grub_netbuff_reserve (nb, 2);
2N/A if (!nb)
2N/A {
2N/A grub_netbuff_free (nb);
2N/A return NULL;
2N/A }
2N/A start_time = grub_get_time_ms ();
2N/A do
2N/A rc = grub_ieee1275_read (data->handle, nb->data, dev->mtu + 64, &actual);
2N/A while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));
2N/A if (actual)
2N/A {
2N/A grub_netbuff_put (nb, actual);
2N/A return nb;
2N/A }
2N/A grub_netbuff_free (nb);
2N/A return NULL;
2N/A}
2N/A
2N/Astatic struct grub_net_card_driver ofdriver =
2N/A {
2N/A .name = "ofnet",
2N/A .open = card_open,
2N/A .close = card_close,
2N/A .send = send_card_buffer,
2N/A .recv = get_card_packet
2N/A };
2N/A
2N/Astatic const struct
2N/A{
2N/A const char *name;
2N/A int offset;
2N/A}
2N/A
2N/Abootp_response_properties[] =
2N/A {
2N/A { .name = "bootp-response", .offset = 0},
2N/A { .name = "dhcp-response", .offset = 0},
2N/A { .name = "bootpreply-packet", .offset = 0x2a},
2N/A };
2N/A
2N/Astatic void
2N/Agrub_ieee1275_net_config_real (const char *devpath, char **device, char **path)
2N/A{
2N/A struct grub_net_card *card;
2N/A
2N/A /* FIXME: Check that it's the right card. */
2N/A FOR_NET_CARDS (card)
2N/A {
2N/A char *bootp_response;
2N/A char *cardpath;
2N/A char *canon;
2N/A
2N/A grub_ssize_t size = -1;
2N/A unsigned int i;
2N/A
2N/A if (card->driver != &ofdriver)
2N/A continue;
2N/A
2N/A cardpath = ((struct grub_ofnetcard_data *) card->data)->path;
2N/A canon = grub_ieee1275_canonicalise_devname (cardpath);
2N/A if (grub_strcmp (devpath, canon) != 0)
2N/A {
2N/A grub_free (canon);
2N/A continue;
2N/A }
2N/A grub_free (canon);
2N/A
2N/A for (i = 0; i < ARRAY_SIZE (bootp_response_properties); i++)
2N/A if (grub_ieee1275_get_property_length (grub_ieee1275_chosen,
2N/A bootp_response_properties[i].name,
2N/A &size) >= 0)
2N/A break;
2N/A
2N/A if (size < 0)
2N/A return;
2N/A
2N/A bootp_response = grub_malloc (size);
2N/A if (!bootp_response)
2N/A {
2N/A grub_print_error ();
2N/A return;
2N/A }
2N/A if (grub_ieee1275_get_property (grub_ieee1275_chosen,
2N/A bootp_response_properties[i].name,
2N/A bootp_response, size, 0) < 0)
2N/A return;
2N/A
2N/A grub_net_configure_by_dhcp_ack (card->name, card, 0,
2N/A (struct grub_net_bootp_packet *)
2N/A &bootp_response
2N/A + bootp_response_properties[i].offset,
2N/A size - bootp_response_properties[i].offset,
2N/A 1, device, path);
2N/A return;
2N/A }
2N/A}
2N/A
2N/Astatic char *
2N/Afind_alias (const char *fullname)
2N/A{
2N/A char *ret = NULL;
2N/A auto int find_alias_hook (struct grub_ieee1275_devalias *alias);
2N/A
2N/A int find_alias_hook (struct grub_ieee1275_devalias *alias)
2N/A {
2N/A if (grub_strcmp (alias->path, fullname) == 0)
2N/A {
2N/A ret = grub_strdup (alias->name);
2N/A return 1;
2N/A }
2N/A return 0;
2N/A }
2N/A
2N/A grub_devalias_iterate (find_alias_hook);
2N/A grub_errno = GRUB_ERR_NONE;
2N/A return ret;
2N/A}
2N/A
2N/Astatic void
2N/Agrub_ofnet_findcards (void)
2N/A{
2N/A auto int search_net_devices (struct grub_ieee1275_devalias *alias);
2N/A
2N/A int search_net_devices (struct grub_ieee1275_devalias *alias)
2N/A {
2N/A if (!grub_strcmp (alias->type, "network"))
2N/A {
2N/A struct grub_ofnetcard_data *ofdata;
2N/A struct grub_net_card *card;
2N/A grub_ieee1275_phandle_t devhandle;
2N/A grub_net_link_level_address_t lla;
2N/A char *shortname;
2N/A
2N/A ofdata = grub_malloc (sizeof (struct grub_ofnetcard_data));
2N/A if (!ofdata)
2N/A {
2N/A grub_print_error ();
2N/A return 1;
2N/A }
2N/A card = grub_zalloc (sizeof (struct grub_net_card));
2N/A if (!card)
2N/A {
2N/A grub_free (ofdata);
2N/A grub_print_error ();
2N/A return 1;
2N/A }
2N/A
2N/A ofdata->path = grub_strdup (alias->path);
2N/A
2N/A grub_ieee1275_finddevice (ofdata->path, &devhandle);
2N/A
2N/A {
2N/A grub_uint32_t t;
2N/A if (grub_ieee1275_get_integer_property (devhandle,
2N/A "max-frame-size", &t,
2N/A sizeof (t), 0))
2N/A card->mtu = 1500;
2N/A else
2N/A card->mtu = t;
2N/A }
2N/A
2N/A if (grub_ieee1275_get_property (devhandle, "mac-address",
2N/A &(lla.mac), 6, 0)
2N/A && grub_ieee1275_get_property (devhandle, "local-mac-address",
2N/A &(lla.mac), 6, 0))
2N/A {
2N/A grub_error (GRUB_ERR_IO, "Couldn't retrieve mac address.");
2N/A grub_print_error ();
2N/A return 0;
2N/A }
2N/A
2N/A lla.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
2N/A card->default_address = lla;
2N/A
2N/A card->driver = NULL;
2N/A card->data = ofdata;
2N/A card->flags = 0;
2N/A shortname = find_alias (alias->path);
2N/A card->name = grub_xasprintf ("ofnet_%s", shortname ? : alias->path);
2N/A card->idle_poll_delay_ms = 1;
2N/A grub_free (shortname);
2N/A
2N/A card->driver = &ofdriver;
2N/A grub_net_card_register (card);
2N/A return 0;
2N/A }
2N/A return 0;
2N/A }
2N/A
2N/A /* Look at all nodes for devices of the type network. */
2N/A grub_ieee1275_devices_iterate (search_net_devices);
2N/A}
2N/A
2N/AGRUB_MOD_INIT(ofnet)
2N/A{
2N/A grub_ofnet_findcards ();
2N/A grub_ieee1275_net_config = grub_ieee1275_net_config_real;
2N/A}
2N/A
2N/AGRUB_MOD_FINI(ofnet)
2N/A{
2N/A struct grub_net_card *card, *next;
2N/A
2N/A FOR_NET_CARDS_SAFE (card, next)
2N/A if (card->driver && grub_strcmp (card->driver->name, "ofnet") == 0)
2N/A grub_net_card_unregister (card);
2N/A grub_ieee1275_net_config = 0;
2N/A}