2010-09-16 19:57:31 +00:00
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/net/tftp.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-05-12 18:40:54 +00:00
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
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-06-08 00:59:53 +00:00
|
|
|
char 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-05-19 13:39:34 +00:00
|
|
|
|
2011-06-24 18:35:25 +00:00
|
|
|
data = grub_malloc (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
|
|
|
file->device->net->socket->data = (void *) data;
|
|
|
|
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-06-18 23:20:53 +00:00
|
|
|
if ((err = grub_netbuff_push (&nb, sizeof (*tftph))) != GRUB_ERR_NONE)
|
|
|
|
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
|
|
|
|
2010-09-22 22:45:39 +00:00
|
|
|
tftph->opcode = grub_cpu_to_be16 (TFTP_RRQ);
|
2010-09-16 19:57:31 +00:00
|
|
|
grub_strcpy (rrq, filename);
|
|
|
|
rrqlen += grub_strlen (filename) + 1;
|
2011-06-08 00:59:53 +00:00
|
|
|
rrq += grub_strlen (filename) + 1;
|
|
|
|
|
|
|
|
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-06-18 23:20:53 +00:00
|
|
|
if ((err = grub_netbuff_unput (&nb, nb.tail - (nb.data + hdrlen))) != GRUB_ERR_NONE)
|
|
|
|
return err;
|
2011-04-01 08:42:34 +00:00
|
|
|
file->device->net->socket->out_port = TFTP_SERVER_PORT;
|
2010-09-22 22:45:39 +00:00
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
err = grub_net_send_udp_packet (file->device->net->socket, &nb);
|
2010-09-22 22:45:39 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
/* Receive OACK packet. */
|
2011-06-08 00:59:53 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
2010-09-22 22:45:39 +00:00
|
|
|
{
|
2011-05-19 13:39:34 +00:00
|
|
|
grub_net_poll_cards (100);
|
2011-04-01 08:42:34 +00:00
|
|
|
if (grub_errno)
|
|
|
|
return grub_errno;
|
|
|
|
if (file->device->net->socket->status != 0)
|
|
|
|
break;
|
|
|
|
/* Retry. */
|
2011-06-07 14:47:31 +00:00
|
|
|
/*err = grub_net_send_udp_packet (file->device->net->socket, &nb);
|
2011-06-08 00:59:53 +00:00
|
|
|
if (err)
|
2011-06-24 18:35:25 +00:00
|
|
|
return err; */
|
2010-09-22 22:45:39 +00:00
|
|
|
}
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
if (file->device->net->socket->status == 0)
|
2011-06-08 00:59:53 +00:00
|
|
|
return grub_error (GRUB_ERR_TIMEOUT, "Time out opening tftp.");
|
2011-04-01 08:42:34 +00:00
|
|
|
file->size = data->file_size;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2010-09-22 22:45:39 +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-04-01 08:42:34 +00:00
|
|
|
tftp_receive (grub_net_socket_t sock, struct grub_net_buff *nb)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
|
|
|
struct tftphdr *tftph;
|
2011-06-08 00:59:53 +00:00
|
|
|
char nbdata[128];
|
2011-04-01 08:42:34 +00:00
|
|
|
tftp_data_t data = sock->data;
|
2010-09-16 19:57:31 +00:00
|
|
|
grub_err_t err;
|
2011-04-01 08:42:34 +00:00
|
|
|
char *ptr;
|
|
|
|
struct grub_net_buff nb_ack;
|
|
|
|
|
|
|
|
nb_ack.head = nbdata;
|
|
|
|
nb_ack.end = nbdata + sizeof (nbdata);
|
2010-09-16 19:57:31 +00:00
|
|
|
|
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
tftph = (struct tftphdr *) nb->data;
|
2010-09-22 22:45:39 +00:00
|
|
|
switch (grub_be_to_cpu16 (tftph->opcode))
|
2011-06-08 00:59:53 +00:00
|
|
|
{
|
2011-04-01 08:42:34 +00:00
|
|
|
case TFTP_OACK:
|
2011-06-08 00:59:53 +00:00
|
|
|
for (ptr = nb->data + sizeof (tftph->opcode); ptr < nb->tail;)
|
2011-04-01 08:42:34 +00:00
|
|
|
{
|
|
|
|
if (grub_memcmp (ptr, "tsize\0", sizeof ("tsize\0") - 1) == 0)
|
2011-05-19 13:39:34 +00:00
|
|
|
{
|
|
|
|
data->file_size = grub_strtoul (ptr + sizeof ("tsize\0") - 1,
|
|
|
|
0, 0);
|
|
|
|
}
|
2011-04-01 08:42:34 +00:00
|
|
|
while (ptr < nb->tail && *ptr)
|
|
|
|
ptr++;
|
|
|
|
ptr++;
|
|
|
|
}
|
2011-06-08 00:59:53 +00:00
|
|
|
sock->status = 1;
|
2011-04-01 08:42:34 +00:00
|
|
|
data->block = 0;
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_netbuff_clear (nb);
|
|
|
|
break;
|
2010-09-16 19:57:31 +00:00
|
|
|
case TFTP_DATA:
|
2011-06-26 15:18:53 +00:00
|
|
|
err = grub_netbuff_pull (nb, sizeof (tftph->opcode) +
|
|
|
|
sizeof (tftph->u.data.block));
|
|
|
|
if (err)
|
2011-06-18 23:20:53 +00:00
|
|
|
return err;
|
2011-04-01 08:42:34 +00:00
|
|
|
if (grub_be_to_cpu16 (tftph->u.data.block) == data->block + 1)
|
|
|
|
{
|
|
|
|
data->block++;
|
2011-05-10 17:07:20 +00:00
|
|
|
unsigned size = nb->tail - nb->data;
|
|
|
|
if (size < 1024)
|
2011-04-01 08:42:34 +00:00
|
|
|
sock->status = 2;
|
2011-05-10 17:07:20 +00:00
|
|
|
/* Prevent garbage in broken cards. */
|
|
|
|
if (size > 1024)
|
2011-06-26 15:18:53 +00:00
|
|
|
{
|
|
|
|
err = grub_netbuff_unput (nb, size - 1024);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2011-04-01 08:42:34 +00:00
|
|
|
}
|
|
|
|
else
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_netbuff_clear (nb);
|
|
|
|
|
|
|
|
break;
|
2010-09-16 19:57:31 +00:00
|
|
|
case TFTP_ERROR:
|
2011-04-01 08:42:34 +00:00
|
|
|
grub_netbuff_clear (nb);
|
2011-06-08 00:59:53 +00:00
|
|
|
return grub_error (GRUB_ERR_IO, (char *) tftph->u.err.errmsg);
|
|
|
|
break;
|
|
|
|
}
|
2011-04-01 08:42:34 +00:00
|
|
|
grub_netbuff_clear (&nb_ack);
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_netbuff_reserve (&nb_ack, 128);
|
2011-06-24 19:51:57 +00:00
|
|
|
err = grub_netbuff_push (&nb_ack, sizeof (tftph->opcode)
|
|
|
|
+ sizeof (tftph->u.ack.block));
|
|
|
|
if (err)
|
2011-06-18 23:20:53 +00:00
|
|
|
return err;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
tftph = (struct tftphdr *) nb_ack.data;
|
2010-09-22 22:45:39 +00:00
|
|
|
tftph->opcode = grub_cpu_to_be16 (TFTP_ACK);
|
2011-05-19 13:39:34 +00:00
|
|
|
tftph->u.ack.block = grub_cpu_to_be16 (data->block);
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
err = grub_net_send_udp_packet (sock, &nb_ack);
|
|
|
|
return err;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
static grub_err_t
|
2010-09-16 19:57:31 +00:00
|
|
|
tftp_close (struct grub_file *file __attribute__ ((unused)))
|
|
|
|
{
|
2011-05-10 17:07:54 +00:00
|
|
|
grub_free (file->device->net->socket->data);
|
|
|
|
return GRUB_ERR_NONE;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
.read = tftp_receive,
|
|
|
|
.close = tftp_close
|
|
|
|
};
|
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);
|
|
|
|
}
|