2011-07-06 10:53:37 +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/misc.h>
|
|
|
|
#include <grub/net/udp.h>
|
|
|
|
#include <grub/net/ip.h>
|
|
|
|
#include <grub/net/ethernet.h>
|
|
|
|
#include <grub/net/netbuff.h>
|
|
|
|
#include <grub/net.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/file.h>
|
2011-07-08 23:28:47 +00:00
|
|
|
#include <grub/priority_queue.h>
|
2012-02-08 18:26:01 +00:00
|
|
|
#include <grub/i18n.h>
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-05-12 18:40:54 +00:00
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
2011-07-06 10:53:37 +00:00
|
|
|
/* IP port for the MTFTP server used for Intel's PXE */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
MTFTP_SERVER_PORT = 75,
|
|
|
|
MTFTP_CLIENT_PORT = 76,
|
|
|
|
/* IP port for the TFTP server */
|
|
|
|
TFTP_SERVER_PORT = 69
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
TFTP_DEFAULTSIZE_PACKET = 512,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
TFTP_CODE_EOF = 1,
|
|
|
|
TFTP_CODE_MORE = 2,
|
|
|
|
TFTP_CODE_ERROR = 3,
|
|
|
|
TFTP_CODE_BOOT = 4,
|
|
|
|
TFTP_CODE_CFG = 5
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
TFTP_RRQ = 1,
|
|
|
|
TFTP_WRQ = 2,
|
|
|
|
TFTP_DATA = 3,
|
|
|
|
TFTP_ACK = 4,
|
|
|
|
TFTP_ERROR = 5,
|
|
|
|
TFTP_OACK = 6
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
TFTP_EUNDEF = 0, /* not defined */
|
|
|
|
TFTP_ENOTFOUND = 1, /* file not found */
|
|
|
|
TFTP_EACCESS = 2, /* access violation */
|
|
|
|
TFTP_ENOSPACE = 3, /* disk full or allocation exceeded */
|
|
|
|
TFTP_EBADOP = 4, /* illegal TFTP operation */
|
|
|
|
TFTP_EBADID = 5, /* unknown transfer ID */
|
|
|
|
TFTP_EEXISTS = 6, /* file already exists */
|
|
|
|
TFTP_ENOUSER = 7 /* no such user */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tftphdr {
|
|
|
|
grub_uint16_t opcode;
|
|
|
|
union {
|
|
|
|
grub_int8_t rrq[TFTP_DEFAULTSIZE_PACKET];
|
|
|
|
struct {
|
|
|
|
grub_uint16_t block;
|
2012-01-14 10:30:43 +00:00
|
|
|
grub_int8_t download[0];
|
2011-07-06 10:53:37 +00:00
|
|
|
} data;
|
|
|
|
struct {
|
|
|
|
grub_uint16_t block;
|
|
|
|
} ack;
|
|
|
|
struct {
|
|
|
|
grub_uint16_t errcode;
|
|
|
|
grub_int8_t errmsg[TFTP_DEFAULTSIZE_PACKET];
|
|
|
|
} err;
|
|
|
|
struct {
|
|
|
|
grub_int8_t data[TFTP_DEFAULTSIZE_PACKET+2];
|
|
|
|
} oack;
|
|
|
|
} u;
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED ;
|
2011-07-06 10:53:37 +00:00
|
|
|
|
|
|
|
|
2011-07-02 15:58:23 +00:00
|
|
|
typedef struct tftp_data
|
|
|
|
{
|
|
|
|
grub_uint64_t file_size;
|
|
|
|
grub_uint64_t block;
|
|
|
|
grub_uint32_t block_size;
|
2013-10-26 10:48:49 +00:00
|
|
|
grub_uint64_t ack_sent;
|
2011-07-02 20:13:33 +00:00
|
|
|
int have_oack;
|
2012-02-05 09:24:53 +00:00
|
|
|
struct grub_error_saved save_err;
|
2011-07-08 12:41:52 +00:00
|
|
|
grub_net_udp_socket_t sock;
|
2011-07-08 23:28:47 +00:00
|
|
|
grub_priority_queue_t pq;
|
2011-07-02 15:58:23 +00:00
|
|
|
} *tftp_data_t;
|
|
|
|
|
2013-10-26 10:48:49 +00:00
|
|
|
static int
|
|
|
|
cmp_block (grub_uint16_t a, grub_uint16_t b)
|
|
|
|
{
|
|
|
|
grub_int16_t i = (grub_int16_t) (a - b);
|
|
|
|
if (i > 0)
|
|
|
|
return +1;
|
|
|
|
if (i < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-08 23:28:47 +00:00
|
|
|
static int
|
|
|
|
cmp (const void *a__, const void *b__)
|
|
|
|
{
|
|
|
|
struct grub_net_buff *a_ = *(struct grub_net_buff **) a__;
|
|
|
|
struct grub_net_buff *b_ = *(struct grub_net_buff **) b__;
|
|
|
|
struct tftphdr *a = (struct tftphdr *) a_->data;
|
|
|
|
struct tftphdr *b = (struct tftphdr *) b_->data;
|
|
|
|
/* We want the first elements to be on top. */
|
2013-10-26 10:48:49 +00:00
|
|
|
return -cmp_block (grub_be_to_cpu16 (a->u.data.block), grub_be_to_cpu16 (b->u.data.block));
|
2011-07-08 23:28:47 +00:00
|
|
|
}
|
|
|
|
|
2011-07-02 20:13:33 +00:00
|
|
|
static grub_err_t
|
2013-10-26 10:48:49 +00:00
|
|
|
ack (tftp_data_t data, grub_uint64_t block)
|
2011-07-08 23:28:47 +00:00
|
|
|
{
|
|
|
|
struct tftphdr *tftph_ack;
|
|
|
|
grub_uint8_t nbdata[512];
|
|
|
|
struct grub_net_buff nb_ack;
|
|
|
|
grub_err_t err;
|
|
|
|
|
|
|
|
nb_ack.head = nbdata;
|
|
|
|
nb_ack.end = nbdata + sizeof (nbdata);
|
|
|
|
grub_netbuff_clear (&nb_ack);
|
|
|
|
grub_netbuff_reserve (&nb_ack, 512);
|
|
|
|
err = grub_netbuff_push (&nb_ack, sizeof (tftph_ack->opcode)
|
|
|
|
+ sizeof (tftph_ack->u.ack.block));
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
tftph_ack = (struct tftphdr *) nb_ack.data;
|
2014-09-22 16:47:10 +00:00
|
|
|
tftph_ack->opcode = grub_cpu_to_be16_compile_time (TFTP_ACK);
|
2012-07-02 09:22:50 +00:00
|
|
|
tftph_ack->u.ack.block = grub_cpu_to_be16 (block);
|
2011-07-08 23:28:47 +00:00
|
|
|
|
2012-06-22 12:17:46 +00:00
|
|
|
err = grub_net_send_udp_packet (data->sock, &nb_ack);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
data->ack_sent = block;
|
|
|
|
return GRUB_ERR_NONE;
|
2011-07-08 23:28:47 +00:00
|
|
|
}
|
|
|
|
|
2011-07-02 20:13:33 +00:00
|
|
|
static grub_err_t
|
2011-07-08 12:41:52 +00:00
|
|
|
tftp_receive (grub_net_udp_socket_t sock __attribute__ ((unused)),
|
2011-07-02 20:13:33 +00:00
|
|
|
struct grub_net_buff *nb,
|
|
|
|
void *f)
|
|
|
|
{
|
|
|
|
grub_file_t file = f;
|
|
|
|
struct tftphdr *tftph = (void *) nb->data;
|
|
|
|
tftp_data_t data = file->data;
|
|
|
|
grub_err_t err;
|
2011-07-08 22:27:27 +00:00
|
|
|
grub_uint8_t *ptr;
|
2011-07-02 20:13:33 +00:00
|
|
|
|
2011-07-08 23:28:47 +00:00
|
|
|
if (nb->tail - nb->data < (grub_ssize_t) sizeof (tftph->opcode))
|
2012-02-12 18:11:06 +00:00
|
|
|
{
|
|
|
|
grub_dprintf ("tftp", "TFTP packet too small\n");
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
2011-07-02 20:13:33 +00:00
|
|
|
|
|
|
|
tftph = (struct tftphdr *) nb->data;
|
|
|
|
switch (grub_be_to_cpu16 (tftph->opcode))
|
|
|
|
{
|
|
|
|
case TFTP_OACK:
|
2011-07-06 10:53:37 +00:00
|
|
|
data->block_size = TFTP_DEFAULTSIZE_PACKET;
|
2011-07-02 20:13:33 +00:00
|
|
|
data->have_oack = 1;
|
|
|
|
for (ptr = nb->data + sizeof (tftph->opcode); ptr < nb->tail;)
|
|
|
|
{
|
|
|
|
if (grub_memcmp (ptr, "tsize\0", sizeof ("tsize\0") - 1) == 0)
|
2011-07-08 22:27:27 +00:00
|
|
|
data->file_size = grub_strtoul ((char *) ptr + sizeof ("tsize\0")
|
|
|
|
- 1, 0, 0);
|
2011-07-02 20:13:33 +00:00
|
|
|
if (grub_memcmp (ptr, "blksize\0", sizeof ("blksize\0") - 1) == 0)
|
2011-07-08 22:27:27 +00:00
|
|
|
data->block_size = grub_strtoul ((char *) ptr + sizeof ("blksize\0")
|
|
|
|
- 1, 0, 0);
|
2011-07-02 20:13:33 +00:00
|
|
|
while (ptr < nb->tail && *ptr)
|
|
|
|
ptr++;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
data->block = 0;
|
|
|
|
grub_netbuff_free (nb);
|
2012-06-22 12:17:46 +00:00
|
|
|
err = ack (data, 0);
|
2012-02-05 09:24:53 +00:00
|
|
|
grub_error_save (&data->save_err);
|
2011-07-08 23:28:47 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2011-07-02 20:13:33 +00:00
|
|
|
case TFTP_DATA:
|
2011-07-08 23:28:47 +00:00
|
|
|
if (nb->tail - nb->data < (grub_ssize_t) (sizeof (tftph->opcode)
|
|
|
|
+ sizeof (tftph->u.data.block)))
|
2012-02-12 18:11:06 +00:00
|
|
|
{
|
|
|
|
grub_dprintf ("tftp", "TFTP packet too small\n");
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
2011-07-08 23:28:47 +00:00
|
|
|
|
|
|
|
err = grub_priority_queue_push (data->pq, &nb);
|
2011-07-02 20:13:33 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
2011-07-08 23:28:47 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
struct grub_net_buff **nb_top_p, *nb_top;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
nb_top_p = grub_priority_queue_top (data->pq);
|
|
|
|
if (!nb_top_p)
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
nb_top = *nb_top_p;
|
|
|
|
tftph = (struct tftphdr *) nb_top->data;
|
2013-10-26 10:48:49 +00:00
|
|
|
if (cmp_block (grub_be_to_cpu16 (tftph->u.data.block), data->block + 1) >= 0)
|
2011-07-08 23:28:47 +00:00
|
|
|
break;
|
2013-10-26 10:48:49 +00:00
|
|
|
ack (data, grub_be_to_cpu16 (tftph->u.data.block));
|
2012-05-08 14:03:02 +00:00
|
|
|
grub_netbuff_free (nb_top);
|
2011-07-08 23:28:47 +00:00
|
|
|
grub_priority_queue_pop (data->pq);
|
|
|
|
}
|
2013-10-26 10:48:49 +00:00
|
|
|
while (cmp_block (grub_be_to_cpu16 (tftph->u.data.block), data->block + 1) == 0)
|
2011-07-08 23:28:47 +00:00
|
|
|
{
|
|
|
|
unsigned size;
|
|
|
|
|
|
|
|
grub_priority_queue_pop (data->pq);
|
|
|
|
|
2012-06-22 20:04:16 +00:00
|
|
|
if (file->device->net->packs.count < 50)
|
2012-07-02 09:22:50 +00:00
|
|
|
err = ack (data, data->block + 1);
|
2012-06-22 12:17:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
file->device->net->stall = 1;
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2011-07-08 23:28:47 +00:00
|
|
|
err = grub_netbuff_pull (nb_top, sizeof (tftph->opcode) +
|
|
|
|
sizeof (tftph->u.data.block));
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
size = nb_top->tail - nb_top->data;
|
|
|
|
|
|
|
|
data->block++;
|
|
|
|
if (size < data->block_size)
|
|
|
|
{
|
2013-08-14 23:32:42 +00:00
|
|
|
if (data->ack_sent < data->block)
|
|
|
|
ack (data, data->block);
|
2011-07-08 23:28:47 +00:00
|
|
|
file->device->net->eof = 1;
|
2012-06-22 12:17:46 +00:00
|
|
|
file->device->net->stall = 1;
|
2011-07-08 23:28:47 +00:00
|
|
|
grub_net_udp_close (data->sock);
|
|
|
|
data->sock = NULL;
|
|
|
|
}
|
|
|
|
/* Prevent garbage in broken cards. Is it still necessary
|
|
|
|
given that IP implementation has been fixed?
|
|
|
|
*/
|
|
|
|
if (size > data->block_size)
|
|
|
|
{
|
|
|
|
err = grub_netbuff_unput (nb_top, size - data->block_size);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
/* If there is data, puts packet in socket list. */
|
|
|
|
if ((nb_top->tail - nb_top->data) > 0)
|
|
|
|
grub_net_put_packet (&file->device->net->packs, nb_top);
|
|
|
|
else
|
2012-05-08 14:03:02 +00:00
|
|
|
grub_netbuff_free (nb_top);
|
2011-07-08 23:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return GRUB_ERR_NONE;
|
2011-07-02 20:13:33 +00:00
|
|
|
case TFTP_ERROR:
|
2012-02-05 09:24:53 +00:00
|
|
|
data->have_oack = 1;
|
2011-07-02 20:13:33 +00:00
|
|
|
grub_netbuff_free (nb);
|
2012-02-05 09:24:53 +00:00
|
|
|
grub_error (GRUB_ERR_IO, (char *) tftph->u.err.errmsg);
|
|
|
|
grub_error_save (&data->save_err);
|
|
|
|
return GRUB_ERR_NONE;
|
2011-07-08 23:28:47 +00:00
|
|
|
default:
|
|
|
|
grub_netbuff_free (nb);
|
|
|
|
return GRUB_ERR_NONE;
|
2011-07-02 20:13:33 +00:00
|
|
|
}
|
2011-07-08 23:28:47 +00:00
|
|
|
}
|
2011-07-02 20:13:33 +00:00
|
|
|
|
2011-07-08 23:28:47 +00:00
|
|
|
static void
|
|
|
|
destroy_pq (tftp_data_t data)
|
|
|
|
{
|
|
|
|
struct grub_net_buff **nb_p;
|
|
|
|
while ((nb_p = grub_priority_queue_top (data->pq)))
|
2012-05-08 14:03:02 +00:00
|
|
|
{
|
|
|
|
grub_netbuff_free (*nb_p);
|
|
|
|
grub_priority_queue_pop (data->pq);
|
|
|
|
}
|
2011-07-02 20:13:33 +00:00
|
|
|
|
2011-07-08 23:28:47 +00:00
|
|
|
grub_priority_queue_destroy (data->pq);
|
2011-07-02 20:13:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 10:33:39 +00:00
|
|
|
/*
|
|
|
|
* Create a normalized copy of the filename. Compress any string of consecutive
|
|
|
|
* forward slashes to a single forward slash.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
grub_normalize_filename (char *normalized, const char *filename)
|
|
|
|
{
|
|
|
|
char *dest = normalized;
|
|
|
|
const char *src = filename;
|
|
|
|
|
|
|
|
while (*src != '\0')
|
|
|
|
{
|
|
|
|
if (src[0] == '/' && src[1] == '/')
|
|
|
|
src++;
|
|
|
|
else
|
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
*dest = '\0';
|
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
static grub_err_t
|
2010-09-16 19:57:31 +00:00
|
|
|
tftp_open (struct grub_file *file, const char *filename)
|
|
|
|
{
|
2011-06-08 00:59:53 +00:00
|
|
|
struct tftphdr *tftph;
|
2010-09-16 19:57:31 +00:00
|
|
|
char *rrq;
|
2011-04-01 08:42:34 +00:00
|
|
|
int i;
|
2010-09-16 19:57:31 +00:00
|
|
|
int rrqlen;
|
2011-04-01 08:42:34 +00:00
|
|
|
int hdrlen;
|
2011-07-08 22:27:27 +00:00
|
|
|
grub_uint8_t open_data[1500];
|
2011-04-01 08:42:34 +00:00
|
|
|
struct grub_net_buff nb;
|
2011-05-19 13:39:34 +00:00
|
|
|
tftp_data_t data;
|
2010-09-16 19:57:31 +00:00
|
|
|
grub_err_t err;
|
2011-07-10 06:46:48 +00:00
|
|
|
grub_uint8_t *nbd;
|
2011-10-14 17:21:59 +00:00
|
|
|
grub_net_network_level_address_t addr;
|
2011-05-19 13:39:34 +00:00
|
|
|
|
2011-07-02 20:13:33 +00:00
|
|
|
data = grub_zalloc (sizeof (*data));
|
2011-05-19 13:39:34 +00:00
|
|
|
if (!data)
|
|
|
|
return grub_errno;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
nb.head = open_data;
|
|
|
|
nb.end = open_data + sizeof (open_data);
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_netbuff_clear (&nb);
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_netbuff_reserve (&nb, 1500);
|
2011-07-02 20:13:33 +00:00
|
|
|
err = grub_netbuff_push (&nb, sizeof (*tftph));
|
|
|
|
if (err)
|
2016-01-16 18:27:57 +00:00
|
|
|
{
|
|
|
|
grub_free (data);
|
|
|
|
return err;
|
|
|
|
}
|
2011-06-08 00:59:53 +00:00
|
|
|
|
|
|
|
tftph = (struct tftphdr *) nb.data;
|
2010-09-22 22:45:39 +00:00
|
|
|
|
2010-09-16 19:57:31 +00:00
|
|
|
rrq = (char *) tftph->u.rrq;
|
|
|
|
rrqlen = 0;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2014-09-22 16:47:10 +00:00
|
|
|
tftph->opcode = grub_cpu_to_be16_compile_time (TFTP_RRQ);
|
2019-10-31 10:33:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy and normalize the filename to work-around issues on some TFTP
|
|
|
|
* servers when file names are being matched for remapping.
|
|
|
|
*/
|
|
|
|
grub_normalize_filename (rrq, filename);
|
|
|
|
rrqlen += grub_strlen (rrq) + 1;
|
|
|
|
rrq += grub_strlen (rrq) + 1;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
|
|
|
grub_strcpy (rrq, "octet");
|
2010-09-16 19:57:31 +00:00
|
|
|
rrqlen += grub_strlen ("octet") + 1;
|
2011-06-08 00:59:53 +00:00
|
|
|
rrq += grub_strlen ("octet") + 1;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_strcpy (rrq, "blksize");
|
|
|
|
rrqlen += grub_strlen ("blksize") + 1;
|
|
|
|
rrq += grub_strlen ("blksize") + 1;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_strcpy (rrq, "1024");
|
2010-09-16 19:57:31 +00:00
|
|
|
rrqlen += grub_strlen ("1024") + 1;
|
2011-06-08 00:59:53 +00:00
|
|
|
rrq += grub_strlen ("1024") + 1;
|
|
|
|
|
|
|
|
grub_strcpy (rrq, "tsize");
|
2010-09-16 19:57:31 +00:00
|
|
|
rrqlen += grub_strlen ("tsize") + 1;
|
2011-06-08 00:59:53 +00:00
|
|
|
rrq += grub_strlen ("tsize") + 1;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_strcpy (rrq, "0");
|
2010-09-16 19:57:31 +00:00
|
|
|
rrqlen += grub_strlen ("0") + 1;
|
2010-09-22 22:45:39 +00:00
|
|
|
rrq += grub_strlen ("0") + 1;
|
2010-09-16 19:57:31 +00:00
|
|
|
hdrlen = sizeof (tftph->opcode) + rrqlen;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2011-07-02 16:05:40 +00:00
|
|
|
err = grub_netbuff_unput (&nb, nb.tail - (nb.data + hdrlen));
|
|
|
|
if (err)
|
2016-01-16 18:27:57 +00:00
|
|
|
{
|
|
|
|
grub_free (data);
|
|
|
|
return err;
|
|
|
|
}
|
2010-09-22 22:45:39 +00:00
|
|
|
|
2011-07-02 20:13:33 +00:00
|
|
|
file->not_easily_seekable = 1;
|
|
|
|
file->data = data;
|
2011-07-08 23:28:47 +00:00
|
|
|
|
|
|
|
data->pq = grub_priority_queue_new (sizeof (struct grub_net_buff *), cmp);
|
|
|
|
if (!data->pq)
|
2016-01-16 18:27:57 +00:00
|
|
|
{
|
|
|
|
grub_free (data);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
2011-07-02 20:13:33 +00:00
|
|
|
|
2011-10-14 17:21:59 +00:00
|
|
|
err = grub_net_resolve_address (file->device->net->server, &addr);
|
2010-09-22 22:45:39 +00:00
|
|
|
if (err)
|
2011-07-02 20:13:33 +00:00
|
|
|
{
|
2011-10-14 17:21:59 +00:00
|
|
|
destroy_pq (data);
|
2016-01-16 18:27:57 +00:00
|
|
|
grub_free (data);
|
2011-07-02 20:13:33 +00:00
|
|
|
return err;
|
|
|
|
}
|
2010-09-22 22:45:39 +00:00
|
|
|
|
2011-10-14 17:21:59 +00:00
|
|
|
data->sock = grub_net_udp_open (addr,
|
2011-07-02 20:13:33 +00:00
|
|
|
TFTP_SERVER_PORT, tftp_receive,
|
|
|
|
file);
|
|
|
|
if (!data->sock)
|
2011-07-08 23:28:47 +00:00
|
|
|
{
|
|
|
|
destroy_pq (data);
|
2016-01-16 18:27:57 +00:00
|
|
|
grub_free (data);
|
2011-07-08 23:28:47 +00:00
|
|
|
return grub_errno;
|
|
|
|
}
|
2011-07-02 20:13:33 +00:00
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
/* Receive OACK packet. */
|
2011-07-10 06:46:48 +00:00
|
|
|
nbd = nb.data;
|
2011-10-05 20:15:30 +00:00
|
|
|
for (i = 0; i < GRUB_NET_TRIES; i++)
|
2010-09-22 22:45:39 +00:00
|
|
|
{
|
2011-07-10 06:46:48 +00:00
|
|
|
nb.data = nbd;
|
2011-07-02 20:13:33 +00:00
|
|
|
err = grub_net_send_udp_packet (data->sock, &nb);
|
2011-06-26 15:18:53 +00:00
|
|
|
if (err)
|
2011-04-01 08:42:34 +00:00
|
|
|
{
|
2011-07-02 20:13:33 +00:00
|
|
|
grub_net_udp_close (data->sock);
|
2011-07-08 23:28:47 +00:00
|
|
|
destroy_pq (data);
|
2016-01-16 18:27:57 +00:00
|
|
|
grub_free (data);
|
2011-07-02 20:13:33 +00:00
|
|
|
return err;
|
2011-04-01 08:42:34 +00:00
|
|
|
}
|
2014-01-21 13:03:51 +00:00
|
|
|
grub_net_poll_cards (GRUB_NET_INTERVAL + (i * GRUB_NET_INTERVAL_ADDITION),
|
|
|
|
&data->have_oack);
|
2011-07-10 06:46:48 +00:00
|
|
|
if (data->have_oack)
|
|
|
|
break;
|
2011-06-08 00:59:53 +00:00
|
|
|
}
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-07-02 20:13:33 +00:00
|
|
|
if (!data->have_oack)
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_error (GRUB_ERR_TIMEOUT, N_("time out opening `%s'"), filename);
|
2012-02-05 09:24:53 +00:00
|
|
|
else
|
|
|
|
grub_error_load (&data->save_err);
|
|
|
|
if (grub_errno)
|
2011-07-02 20:13:33 +00:00
|
|
|
{
|
|
|
|
grub_net_udp_close (data->sock);
|
2011-07-08 23:28:47 +00:00
|
|
|
destroy_pq (data);
|
2016-01-16 18:27:57 +00:00
|
|
|
grub_free (data);
|
2012-02-05 09:24:53 +00:00
|
|
|
return grub_errno;
|
2011-07-02 20:13:33 +00:00
|
|
|
}
|
2012-02-05 09:24:53 +00:00
|
|
|
|
2011-07-02 20:13:33 +00:00
|
|
|
file->size = data->file_size;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-07-02 20:13:33 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
static grub_err_t
|
2011-07-02 20:13:33 +00:00
|
|
|
tftp_close (struct grub_file *file)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-07-02 20:13:33 +00:00
|
|
|
tftp_data_t data = file->data;
|
2011-07-06 10:53:37 +00:00
|
|
|
|
2011-07-02 20:13:33 +00:00
|
|
|
if (data->sock)
|
2011-07-06 10:53:37 +00:00
|
|
|
{
|
2011-07-08 22:27:27 +00:00
|
|
|
grub_uint8_t nbdata[512];
|
2011-07-06 10:53:37 +00:00
|
|
|
grub_err_t err;
|
|
|
|
struct grub_net_buff nb_err;
|
|
|
|
struct tftphdr *tftph;
|
|
|
|
|
|
|
|
nb_err.head = nbdata;
|
|
|
|
nb_err.end = nbdata + sizeof (nbdata);
|
|
|
|
|
|
|
|
grub_netbuff_clear (&nb_err);
|
|
|
|
grub_netbuff_reserve (&nb_err, 512);
|
|
|
|
err = grub_netbuff_push (&nb_err, sizeof (tftph->opcode)
|
|
|
|
+ sizeof (tftph->u.err.errcode)
|
|
|
|
+ sizeof ("closed"));
|
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
tftph = (struct tftphdr *) nb_err.data;
|
2014-09-22 16:47:10 +00:00
|
|
|
tftph->opcode = grub_cpu_to_be16_compile_time (TFTP_ERROR);
|
|
|
|
tftph->u.err.errcode = grub_cpu_to_be16_compile_time (TFTP_EUNDEF);
|
2011-07-06 10:53:37 +00:00
|
|
|
grub_memcpy (tftph->u.err.errmsg, "closed", sizeof ("closed"));
|
|
|
|
|
|
|
|
err = grub_net_send_udp_packet (data->sock, &nb_err);
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
grub_print_error ();
|
|
|
|
grub_net_udp_close (data->sock);
|
|
|
|
}
|
2011-07-08 23:28:47 +00:00
|
|
|
destroy_pq (data);
|
2011-07-02 20:13:33 +00:00
|
|
|
grub_free (data);
|
2011-05-10 17:07:54 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2012-06-22 12:17:46 +00:00
|
|
|
static grub_err_t
|
|
|
|
tftp_packets_pulled (struct grub_file *file)
|
|
|
|
{
|
|
|
|
tftp_data_t data = file->data;
|
2012-06-22 20:04:16 +00:00
|
|
|
if (file->device->net->packs.count >= 50)
|
2012-06-22 12:17:46 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!file->device->net->eof)
|
|
|
|
file->device->net->stall = 0;
|
|
|
|
if (data->ack_sent >= data->block)
|
|
|
|
return 0;
|
|
|
|
return ack (data, data->block);
|
|
|
|
}
|
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
static struct grub_net_app_protocol grub_tftp_protocol =
|
2011-06-08 00:59:53 +00:00
|
|
|
{
|
|
|
|
.name = "tftp",
|
|
|
|
.open = tftp_open,
|
2012-06-22 12:17:46 +00:00
|
|
|
.close = tftp_close,
|
|
|
|
.packets_pulled = tftp_packets_pulled
|
2011-06-08 00:59:53 +00:00
|
|
|
};
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-24 18:35:25 +00:00
|
|
|
GRUB_MOD_INIT (tftp)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
|
|
|
grub_net_app_level_register (&grub_tftp_protocol);
|
|
|
|
}
|
|
|
|
|
2011-06-24 18:35:25 +00:00
|
|
|
GRUB_MOD_FINI (tftp)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
|
|
|
grub_net_app_level_unregister (&grub_tftp_protocol);
|
|
|
|
}
|