source: grub-pc/trunk/fuentes/grub-core/net/drivers/emu/emunet.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

File size: 2.8 KB
Line 
1/*
2 *  GRUB  --  GRand Unified Bootloader
3 *  Copyright (C) 2010,2011,2012,2013  Free Software Foundation, Inc.
4 *
5 *  GRUB is free software: you can redistribute it and/or modify
6 *  it under the terms of the GNU General Public License as published by
7 *  the Free Software Foundation, either version 3 of the License, or
8 *  (at your option) any later version.
9 *
10 *  GRUB is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *  GNU General Public License for more details.
14 *
15 *  You should have received a copy of the GNU General Public License
16 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <grub/dl.h>
20#include <grub/net/netbuff.h>
21#include <grub/net.h>
22#include <grub/term.h>
23#include <grub/i18n.h>
24#include <grub/emu/net.h>
25
26GRUB_MOD_LICENSE ("GPLv3+");
27
28static grub_err_t
29send_card_buffer (struct grub_net_card *dev __attribute__ ((unused)),
30                  struct grub_net_buff *pack);
31
32static struct grub_net_buff *
33get_card_packet (struct grub_net_card *dev __attribute__ ((unused)));
34
35static struct grub_net_card_driver emudriver = 
36  {
37    .name = "emu",
38    .send = send_card_buffer,
39    .recv = get_card_packet
40  };
41
42static struct grub_net_card emucard = 
43  {
44    .name = "emu0",
45    .driver = &emudriver,
46    .mtu = 1500,
47    .default_address = {
48                         .type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET,
49                         {.mac = {0, 1, 2, 3, 4, 5}}
50                       },
51    .flags = 0
52  };
53
54static grub_err_t
55send_card_buffer (struct grub_net_card *dev __attribute__ ((unused)),
56                  struct grub_net_buff *pack)
57{
58  grub_ssize_t actual;
59
60  actual = grub_emunet_send (pack->data, pack->tail - pack->data);
61  if (actual < 0)
62    return grub_error (GRUB_ERR_IO, N_("couldn't send network packet"));
63
64  return GRUB_ERR_NONE;
65}
66
67static struct grub_net_buff *
68get_card_packet (struct grub_net_card *dev __attribute__ ((unused)))
69{
70  grub_ssize_t actual;
71  struct grub_net_buff *nb;
72
73  nb = grub_netbuff_alloc (emucard.mtu + 36 + 2);
74  if (!nb)
75    return NULL;
76
77  /* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
78     by 4. So that IP header is aligned on 4 bytes. */
79  grub_netbuff_reserve (nb, 2);
80  if (!nb)
81    {
82      grub_netbuff_free (nb);
83      return NULL;
84    }
85
86  actual = grub_emunet_receive (nb->data, emucard.mtu + 36);
87  if (actual < 0)
88    {
89      grub_netbuff_free (nb);
90      return NULL;
91    }
92  grub_netbuff_put (nb, actual);
93
94  return nb;
95}
96
97static int registered = 0;
98
99GRUB_MOD_INIT(emunet)
100{
101  if (grub_emunet_create (&emucard.mtu))
102    {
103      grub_net_card_register (&emucard);
104      registered = 1;
105    }
106}
107
108GRUB_MOD_FINI(emunet)
109{
110  if (registered)
111    {
112      grub_emunet_close ();
113      grub_net_card_unregister (&emucard);
114      registered = 0;
115    }
116}
Note: See TracBrowser for help on using the repository browser.