2010-04-27 21:05:35 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2010 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/net/netbuff.h>
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_netbuff_put (struct grub_net_buff *nb, grub_size_t len)
|
2010-04-27 21:05:35 +00:00
|
|
|
{
|
2011-04-01 08:27:06 +00:00
|
|
|
nb->tail += len;
|
|
|
|
if (nb->tail > nb->end)
|
2010-06-21 22:15:45 +00:00
|
|
|
return grub_error (GRUB_ERR_OUT_OF_RANGE, "put out of the packet range.");
|
2011-06-08 00:59:53 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-04-27 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_netbuff_unput (struct grub_net_buff *nb, grub_size_t len)
|
2010-04-27 21:05:35 +00:00
|
|
|
{
|
2011-04-01 08:27:06 +00:00
|
|
|
nb->tail -= len;
|
|
|
|
if (nb->tail < nb->head)
|
2011-06-08 00:59:53 +00:00
|
|
|
return grub_error (GRUB_ERR_OUT_OF_RANGE,
|
|
|
|
"unput out of the packet range.");
|
|
|
|
return GRUB_ERR_NONE;
|
2010-04-27 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_netbuff_push (struct grub_net_buff *nb, grub_size_t len)
|
2010-04-27 21:05:35 +00:00
|
|
|
{
|
2011-04-01 08:27:06 +00:00
|
|
|
nb->data -= len;
|
|
|
|
if (nb->data < nb->head)
|
2011-06-08 00:59:53 +00:00
|
|
|
return grub_error (GRUB_ERR_OUT_OF_RANGE,
|
|
|
|
"push out of the packet range.");
|
|
|
|
return GRUB_ERR_NONE;
|
2010-04-27 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_netbuff_pull (struct grub_net_buff *nb, grub_size_t len)
|
2010-04-27 21:05:35 +00:00
|
|
|
{
|
2011-04-01 08:27:06 +00:00
|
|
|
nb->data += len;
|
|
|
|
if (nb->data > nb->end)
|
2011-06-08 00:59:53 +00:00
|
|
|
return grub_error (GRUB_ERR_OUT_OF_RANGE,
|
|
|
|
"pull out of the packet range.");
|
|
|
|
return GRUB_ERR_NONE;
|
2010-04-27 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_netbuff_reserve (struct grub_net_buff *nb, grub_size_t len)
|
2010-04-27 21:05:35 +00:00
|
|
|
{
|
2011-04-01 08:27:06 +00:00
|
|
|
nb->data += len;
|
|
|
|
nb->tail += len;
|
|
|
|
if ((nb->tail > nb->end) || (nb->data > nb->end))
|
2011-06-08 00:59:53 +00:00
|
|
|
return grub_error (GRUB_ERR_OUT_OF_RANGE,
|
|
|
|
"reserve out of the packet range.");
|
|
|
|
return GRUB_ERR_NONE;
|
2010-04-27 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
struct grub_net_buff *
|
|
|
|
grub_netbuff_alloc (grub_size_t len)
|
2010-04-27 21:05:35 +00:00
|
|
|
{
|
2010-06-21 22:15:45 +00:00
|
|
|
struct grub_net_buff *nb;
|
|
|
|
void *data;
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
if (len < NETBUFFMINLEN)
|
2010-04-27 21:05:35 +00:00
|
|
|
len = NETBUFFMINLEN;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
|
|
|
len = ALIGN_UP (len, NETBUFF_ALIGN);
|
2010-08-13 17:42:16 +00:00
|
|
|
data = grub_memalign (NETBUFF_ALIGN, len + sizeof (*nb));
|
2011-04-01 08:27:06 +00:00
|
|
|
if (!data)
|
|
|
|
return NULL;
|
2011-06-08 00:59:53 +00:00
|
|
|
nb = (struct grub_net_buff *) ((grub_uint8_t *) data + len);
|
2010-06-21 22:15:45 +00:00
|
|
|
nb->head = nb->data = nb->tail = data;
|
2011-07-08 22:27:27 +00:00
|
|
|
nb->end = (grub_uint8_t *) nb;
|
2011-06-08 00:59:53 +00:00
|
|
|
return nb;
|
2010-06-21 22:15:45 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_netbuff_free (struct grub_net_buff *nb)
|
2010-06-21 22:15:45 +00:00
|
|
|
{
|
2011-04-01 08:27:06 +00:00
|
|
|
grub_free (nb->head);
|
2011-06-18 23:20:53 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-06-21 22:15:45 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_netbuff_clear (struct grub_net_buff *nb)
|
2010-06-21 22:15:45 +00:00
|
|
|
{
|
2011-04-01 08:27:06 +00:00
|
|
|
nb->data = nb->tail = nb->head;
|
2011-06-18 23:20:53 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-04-27 21:05:35 +00:00
|
|
|
}
|