diff --git a/Makefile.util.def b/Makefile.util.def index 79ab040dd..058572f06 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -36,8 +36,6 @@ library = { common = grub-core/commands/blocklist.c; common = grub-core/commands/extcmd.c; common = grub-core/commands/ls.c; - common = grub-core/net/net.c; - common = grub-core/net/netbuff.c; common = grub-core/disk/dmraid_nvidia.c; common = grub-core/disk/loopback.c; common = grub-core/disk/lvm.c; diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am index 0fdb4b1db..d5cd25a70 100644 --- a/grub-core/Makefile.am +++ b/grub-core/Makefile.am @@ -167,6 +167,7 @@ endif if COND_emu KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/datetime.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/misc.h +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/export.h if COND_GRUB_EMU_SDL KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/sdl.h endif diff --git a/grub-core/commands/ls.c b/grub-core/commands/ls.c index 20a75733c..9df830612 100644 --- a/grub-core/commands/ls.c +++ b/grub-core/commands/ls.c @@ -48,9 +48,6 @@ static const char grub_human_sizes[] = {' ', 'K', 'M', 'G', 'T'}; static grub_err_t grub_ls_list_devices (int longlist) { - grub_net_app_level_t proto; - int first = 1; - auto int grub_ls_print_devices (const char *name); int grub_ls_print_devices (const char *name) { @@ -65,15 +62,20 @@ grub_ls_list_devices (int longlist) grub_device_iterate (grub_ls_print_devices); grub_xputs ("\n"); - FOR_NET_APP_LEVEL (proto) +#ifndef GRUB_UTIL { - if (first) - grub_puts_ (N_ ("Network protocols:")); - first = 0; - grub_printf ("%s ", proto->name); + grub_net_app_level_t proto; + int first = 1; + FOR_NET_APP_LEVEL (proto) + { + if (first) + grub_puts_ (N_ ("Network protocols:")); + first = 0; + grub_printf ("%s ", proto->name); + } + grub_xputs ("\n"); } - - grub_xputs ("\n"); +#endif grub_refresh (); diff --git a/grub-core/net/arp.c b/grub-core/net/arp.c index 8778a6e51..1300def25 100644 --- a/grub-core/net/arp.c +++ b/grub-core/net/arp.c @@ -87,11 +87,10 @@ grub_net_arp_resolve(struct grub_net_network_level_interface *inf, grub_memcpy (hw_addr, &entry->ll_address, sizeof (*hw_addr)); return GRUB_ERR_NONE; } - grub_net_pool_cards (200); + grub_net_poll_cards (200); + } - } - - return grub_error (GRUB_ERR_TIMEOUT, "Timeout: could not resolve hardware address."); + return grub_error (GRUB_ERR_TIMEOUT, "timeout: could not resolve hardware address"); } grub_err_t diff --git a/grub-core/net/drivers/emu/emunet.c b/grub-core/net/drivers/emu/emunet.c index 9e4e66dce..d707d8015 100644 --- a/grub-core/net/drivers/emu/emunet.c +++ b/grub-core/net/drivers/emu/emunet.c @@ -26,7 +26,7 @@ send_card_buffer (struct grub_net_card *dev __attribute__ ((unused)), return GRUB_ERR_NONE; } -static grub_size_t +static grub_ssize_t get_card_packet (struct grub_net_card *dev __attribute__ ((unused)), struct grub_net_buff *pack) { @@ -35,10 +35,7 @@ get_card_packet (struct grub_net_card *dev __attribute__ ((unused)), grub_netbuff_clear(pack); actual = read (fd, pack->data, 1500); if (actual < 0) - { - grub_error (GRUB_ERR_IO, "couldn't receive packets"); - return -1; - } + return -1; grub_netbuff_put (pack, actual); return actual; @@ -65,7 +62,6 @@ static struct grub_net_card emucard = GRUB_MOD_INIT(emunet) { struct ifreq ifr; - // char fullname[64]; fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK); if (fd < 0) return; diff --git a/grub-core/net/drivers/ieee1275/ofnet.c b/grub-core/net/drivers/ieee1275/ofnet.c index cb3882ceb..10d0b979e 100644 --- a/grub-core/net/drivers/ieee1275/ofnet.c +++ b/grub-core/net/drivers/ieee1275/ofnet.c @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -49,7 +48,7 @@ send_card_buffer (struct grub_net_card *dev, struct grub_net_buff *pack) return GRUB_ERR_NONE; } -static grub_err_t +static grub_ssize_t get_card_packet (struct grub_net_card *dev, struct grub_net_buff *nb) { @@ -65,9 +64,9 @@ get_card_packet (struct grub_net_card *dev, struct grub_net_buff *nb) if (actual) { grub_netbuff_put (nb, actual); - return grub_net_recv_ethernet_packet (nb); + return actual; } - return GRUB_ERR_TIMEOUT; + return -1; } static struct grub_net_card_driver ofdriver = diff --git a/grub-core/net/net.c b/grub-core/net/net.c index be5279c99..a82e73ab0 100644 --- a/grub-core/net/net.c +++ b/grub-core/net/net.c @@ -26,6 +26,7 @@ #include #include #include +#include GRUB_MOD_LICENSE ("GPLv3+"); @@ -587,9 +588,13 @@ grub_net_file_open_real (struct grub_file *file, const char *name) grub_net_network_level_address_t gateway; grub_net_socket_t socket; static int port = 25300; + const char *comma; - err = grub_net_resolve_address (file->device->net->name - + sizeof (file->device->net->protocol->name) + 1, &addr); + comma = grub_strchr (file->device->net->name, ','); + if (!comma) + return grub_error (GRUB_ERR_NET_BAD_ADDRESS, "no separator"); + + err = grub_net_resolve_address (comma + 1, &addr); if (err) return err; @@ -597,19 +602,22 @@ grub_net_file_open_real (struct grub_file *file, const char *name) if (err) return err; - if ((socket = (grub_net_socket_t) grub_malloc (sizeof (*socket))) == NULL) - return GRUB_ERR_OUT_OF_MEMORY; + socket = (grub_net_socket_t) grub_malloc (sizeof (*socket)); + if (socket == NULL) + return grub_errno; socket->inf = inf; socket->out_nla = addr; socket->in_port = port++; socket->status = 0; socket->app = file->device->net->protocol; - socket->packs = NULL; + socket->packs.first = NULL; + socket->packs.last = NULL; file->device->net->socket = socket; grub_net_socket_register (socket); - if ((err = file->device->net->protocol->open (file,name))) + err = file->device->net->protocol->open (file,name); + if (err) goto fail; file->not_easily_seekable = 1; @@ -625,10 +633,10 @@ static grub_err_t grub_net_file_close_real (grub_file_t file) { grub_net_socket_t sock = file->device->net->socket; - while (sock->packs->first) + while (sock->packs.first) { - grub_netbuff_free (sock->packs->first->nb); - grub_net_remove_packet (sock->packs->first); + grub_netbuff_free (sock->packs.first->nb); + grub_net_remove_packet (sock->packs.first); } grub_net_socket_unregister (sock); grub_free (sock); @@ -636,39 +644,44 @@ grub_net_file_close_real (grub_file_t file) } -static grub_err_t +static void receive_packets (struct grub_net_card *card) { /* Maybe should be better have a fixed number of packets for each card and just mark them as used and not used. */ struct grub_net_buff *nb; - grub_err_t err; + grub_ssize_t actual; nb = grub_netbuff_alloc (1500); if (!nb) - return grub_errno; + { + grub_print_error (); + return; + } - if ((err = card->driver->recv (card, nb)) != GRUB_ERR_NONE) + actual = card->driver->recv (card, nb); + if (actual < 0) grub_netbuff_free (nb); - return err; + else + grub_net_recv_ethernet_packet (nb); + grub_print_error (); } void -grub_net_pool_cards (unsigned time) +grub_net_poll_cards (unsigned time) { struct grub_net_card *card; grub_uint64_t start_time; FOR_NET_CARDS (card) { start_time = grub_get_time_ms (); - while( (grub_get_time_ms () - start_time) < time) - if( receive_packets (card) != GRUB_ERR_NONE) - break; + while ((grub_get_time_ms () - start_time) < time) + receive_packets (card); } } +/* Read from the packets list*/ static grub_ssize_t -process_packets (grub_file_t file, void *buf, grub_size_t len, - void *NESTED_FUNC_ATTR (hook) (void *dest, const void *src, grub_size_t n)) +grub_net_read_real (grub_file_t file, void *buf, grub_size_t len) { grub_net_socket_t sock = file->device->net->socket; struct grub_net_buff *nb; @@ -677,19 +690,24 @@ process_packets (grub_file_t file, void *buf, grub_size_t len, int try = 0; while (try <= 3) { - while (sock->packs->first) + while (sock->packs.first) { try = 0; - nb = sock->packs->first->nb; - amount = (grub_size_t)(len <= (grub_size_t) (nb->tail - nb->data))? len :(grub_size_t)(nb->tail - nb->data); + nb = sock->packs.first->nb; + amount = nb->tail - nb->data; + if (amount > len) + amount = len; len -= amount; total += amount; - hook (ptr, nb->data, amount); - ptr += amount; + if (buf) + { + grub_memcpy (ptr, nb->data, amount); + ptr += amount; + } if (amount == (grub_size_t) (nb->tail - nb->data)) { grub_netbuff_free (nb); - grub_net_remove_packet (sock->packs->first); + grub_net_remove_packet (sock->packs.first); } else nb->data += amount; @@ -700,7 +718,7 @@ process_packets (grub_file_t file, void *buf, grub_size_t len, if (sock->status == 1) { try++; - grub_net_pool_cards (200); + grub_net_poll_cards (200); } else return total; @@ -708,20 +726,6 @@ process_packets (grub_file_t file, void *buf, grub_size_t len, return total; } - -/* Read from the packets list*/ -static grub_ssize_t -grub_net_read_real (grub_file_t file, void *buf, grub_size_t len ) -{ - auto void *NESTED_FUNC_ATTR memcpy_hook (void *dest, const void *src, grub_size_t n); - void *NESTED_FUNC_ATTR memcpy_hook (void *dest __attribute__ ((unused)), const void *src __attribute__ ((unused)), - grub_size_t n __attribute__ ((unused))) - { - return grub_memcpy (dest, src, n); - } - return process_packets (file, buf, len, memcpy_hook); -} - /* Read from the packets list*/ static grub_err_t grub_net_seek_real (struct grub_file *file, grub_off_t offset) @@ -736,17 +740,11 @@ grub_net_seek_real (struct grub_file *file, grub_off_t offset) /* We cant seek backwards past the current packet. */ if (file->offset > offset) { - nb = sock->packs->first->nb; + nb = sock->packs.first->nb; return grub_netbuff_push (nb, file->offset - offset); } - auto void *NESTED_FUNC_ATTR dummy (void *dest, const void *src, grub_size_t n); - void *NESTED_FUNC_ATTR dummy (void *dest __attribute__ ((unused)), const void *src __attribute__ ((unused)), - grub_size_t n __attribute__ ((unused))) - { - return NULL; - } - process_packets (file, NULL, len, dummy); + grub_net_read_real (file, NULL, len); return GRUB_ERR_NONE; } diff --git a/grub-core/net/tftp.c b/grub-core/net/tftp.c index 278f6ed14..1daf11bc7 100644 --- a/grub-core/net/tftp.c +++ b/grub-core/net/tftp.c @@ -21,8 +21,12 @@ tftp_open (struct grub_file *file, const char *filename) int hdrlen; char open_data[1500]; struct grub_net_buff nb; - tftp_data_t data = grub_malloc (sizeof *data); + tftp_data_t data; grub_err_t err; + + data = grub_malloc (sizeof *data); + if (!data) + return grub_errno; file->device->net->socket->data = (void *) data; nb.head = open_data; @@ -74,7 +78,7 @@ tftp_open (struct grub_file *file, const char *filename) /* Receive OACK packet. */ for ( i = 0; i < 3; i++) { - grub_net_pool_cards (100); + grub_net_poll_cards (100); if (grub_errno) return grub_errno; if (file->device->net->socket->status != 0) @@ -110,10 +114,13 @@ tftp_receive (grub_net_socket_t sock, struct grub_net_buff *nb) switch (grub_be_to_cpu16 (tftph->opcode)) { case TFTP_OACK: - for (ptr = nb->data; ptr < nb->tail; ) + for (ptr = nb->data + sizeof (tftph->opcode); ptr < nb->tail; ) { if (grub_memcmp (ptr, "tsize\0", sizeof ("tsize\0") - 1) == 0) - data->file_size = grub_strtoul (ptr + sizeof ("tsize\0") - 1, 0, 0); + { + data->file_size = grub_strtoul (ptr + sizeof ("tsize\0") - 1, + 0, 0); + } while (ptr < nb->tail && *ptr) ptr++; ptr++; @@ -150,7 +157,7 @@ tftp_receive (grub_net_socket_t sock, struct grub_net_buff *nb) tftph = (struct tftphdr *) nb_ack.data; tftph->opcode = grub_cpu_to_be16 (TFTP_ACK); - tftph->u.ack.block = data->block; + tftph->u.ack.block = grub_cpu_to_be16 (data->block); err = grub_net_send_udp_packet (sock, &nb_ack); return err; diff --git a/grub-core/net/udp.c b/grub-core/net/udp.c index 16b2011ff..4477b3c2b 100644 --- a/grub-core/net/udp.c +++ b/grub-core/net/udp.c @@ -36,15 +36,14 @@ grub_net_recv_udp_packet (struct grub_net_buff *nb) if (grub_be_to_cpu16 (udph->dst) == sock->in_port) { if (sock->status == 0) - sock->out_port = udph->src; + sock->out_port = grub_be_to_cpu16 (udph->src); - /* App protocol remove its own reader. */ sock->app->read (sock,nb); /* If there is data, puts packet in socket list */ if ((nb->tail - nb->data) > 0) - grub_net_put_packet (sock->packs, nb); + grub_net_put_packet (&sock->packs, nb); else grub_netbuff_free (nb); return GRUB_ERR_NONE; diff --git a/include/grub/net.h b/include/grub/net.h index cb6db06d8..1b113972b 100644 --- a/include/grub/net.h +++ b/include/grub/net.h @@ -63,7 +63,7 @@ struct grub_net_card_driver grub_err_t (*init) (struct grub_net_card *dev); grub_err_t (*fini) (struct grub_net_card *dev); grub_err_t (*send) (struct grub_net_card *dev, struct grub_net_buff *buf); - grub_size_t (*recv) (struct grub_net_card *dev, struct grub_net_buff *buf); + grub_ssize_t (*recv) (struct grub_net_card *dev, struct grub_net_buff *buf); }; extern struct grub_net_card_driver *grub_net_card_drivers; @@ -207,24 +207,24 @@ struct grub_net_socket grub_net_app_level_t app; grub_net_network_level_address_t out_nla; struct grub_net_network_level_interface *inf; - grub_net_packets_t *packs; + grub_net_packets_t packs; void *data; }; extern struct grub_net_socket *grub_net_sockets; static inline void -grub_net_socket_register (grub_net_socket_t socket) +grub_net_socket_register (grub_net_socket_t sock) { grub_list_push (GRUB_AS_LIST_P (&grub_net_sockets), - GRUB_AS_LIST (socket)); + GRUB_AS_LIST (sock)); } static inline void -grub_net_socket_unregister (grub_net_socket_t socket) +grub_net_socket_unregister (grub_net_socket_t sock) { grub_list_remove (GRUB_AS_LIST_P (&grub_net_sockets), - GRUB_AS_LIST (socket)); + GRUB_AS_LIST (sock)); } #define FOR_NET_SOCKETS(var) for (var = grub_net_sockets; var; var = var->next) @@ -432,7 +432,7 @@ grub_net_send_ip_packet (struct grub_net_network_level_interface *inf, #define FOR_NET_NL_PACKETS(inf, var) FOR_PACKETS(inf->nl_pending, var) void -grub_net_pool_cards (unsigned time); +grub_net_poll_cards (unsigned time); extern grub_err_t (*EXPORT_VAR (grub_file_net_close)) (grub_file_t file); #endif /* ! GRUB_NET_HEADER */