2009-03-28 19:58:15 +00:00
|
|
|
/* envblk.c - Common functions for environment block. */
|
2008-07-02 07:20:11 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2009-03-28 19:58:15 +00:00
|
|
|
* Copyright (C) 2008,2009 Free Software Foundation, Inc.
|
2008-07-02 07:20:11 +00:00
|
|
|
*
|
|
|
|
* 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 <config.h>
|
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/misc.h>
|
2009-03-28 19:58:15 +00:00
|
|
|
#include <grub/mm.h>
|
2008-07-27 13:51:30 +00:00
|
|
|
#include <grub/lib/envblk.h>
|
2008-07-02 07:20:11 +00:00
|
|
|
|
|
|
|
grub_envblk_t
|
2009-03-28 19:58:15 +00:00
|
|
|
grub_envblk_open (char *buf, grub_size_t size)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2009-03-28 19:58:15 +00:00
|
|
|
grub_envblk_t envblk;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (size < sizeof (GRUB_ENVBLK_SIGNATURE)
|
|
|
|
|| grub_memcmp (buf, GRUB_ENVBLK_SIGNATURE,
|
|
|
|
sizeof (GRUB_ENVBLK_SIGNATURE) - 1))
|
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid environment block");
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
envblk = grub_malloc (sizeof (*envblk));
|
|
|
|
if (envblk)
|
|
|
|
{
|
|
|
|
envblk->buf = buf;
|
|
|
|
envblk->size = size;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
return envblk;
|
|
|
|
}
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
void
|
|
|
|
grub_envblk_close (grub_envblk_t envblk)
|
|
|
|
{
|
|
|
|
grub_free (envblk->buf);
|
|
|
|
grub_free (envblk);
|
|
|
|
}
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
static int
|
|
|
|
escaped_value_len (const char *value)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
char *p;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
for (p = (char *) value; *p; p++)
|
|
|
|
{
|
|
|
|
if (*p == '\\' || *p == '\n')
|
|
|
|
n += 2;
|
|
|
|
else
|
|
|
|
n++;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
return n;
|
|
|
|
}
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
static char *
|
|
|
|
find_next_line (char *p, const char *pend)
|
|
|
|
{
|
|
|
|
while (p < pend)
|
|
|
|
{
|
|
|
|
if (*p == '\\')
|
|
|
|
p += 2;
|
|
|
|
else if (*p == '\n')
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
p++;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
return p + 1;
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-03-28 19:58:15 +00:00
|
|
|
grub_envblk_set (grub_envblk_t envblk, const char *name, const char *value)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
|
|
|
char *p, *pend;
|
2009-03-28 19:58:15 +00:00
|
|
|
char *space;
|
|
|
|
int found = 0;
|
2008-07-02 07:20:11 +00:00
|
|
|
int nl;
|
2009-03-28 19:58:15 +00:00
|
|
|
int vl;
|
|
|
|
int i;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
|
|
|
nl = grub_strlen (name);
|
2009-03-28 19:58:15 +00:00
|
|
|
vl = escaped_value_len (value);
|
|
|
|
p = envblk->buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1;
|
|
|
|
pend = envblk->buf + envblk->size;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
/* First, look at free space. */
|
|
|
|
for (space = pend - 1; *space == '#'; space--)
|
|
|
|
;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (*space != '\n')
|
|
|
|
/* Broken. */
|
|
|
|
return 0;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
space++;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
while (p + nl + 1 < space)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2009-03-28 19:58:15 +00:00
|
|
|
if (grub_memcmp (p, name, nl) == 0 && p[nl] == '=')
|
|
|
|
{
|
|
|
|
int len;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
/* Found the same name. */
|
|
|
|
p += nl + 1;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
/* Check the length of the current value. */
|
|
|
|
len = 0;
|
|
|
|
while (p + len < pend && p[len] != '\n')
|
|
|
|
{
|
|
|
|
if (p[len] == '\\')
|
|
|
|
len += 2;
|
|
|
|
else
|
|
|
|
len++;
|
|
|
|
}
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (p + len >= pend)
|
|
|
|
/* Broken. */
|
|
|
|
return 0;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (pend - space < vl - len)
|
|
|
|
/* No space. */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (vl < len)
|
|
|
|
{
|
|
|
|
/* Move the following characters backward, and fill the new
|
|
|
|
space with harmless characters. */
|
|
|
|
grub_memmove (p + vl, p + len, pend - (p + len));
|
2020-05-12 12:32:25 +00:00
|
|
|
grub_memset (space - (len - vl), '#', len - vl);
|
2009-03-28 19:58:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Move the following characters forward. */
|
|
|
|
grub_memmove (p + vl, p + len, pend - (p + vl));
|
|
|
|
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = find_next_line (p, pend);
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
2009-03-28 19:58:15 +00:00
|
|
|
|
|
|
|
if (! found)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2009-03-28 19:58:15 +00:00
|
|
|
/* Append a new variable. */
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (pend - space < nl + 1 + vl + 1)
|
|
|
|
/* No space. */
|
|
|
|
return 0;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
grub_memcpy (space, name, nl);
|
|
|
|
p = space + nl;
|
|
|
|
*p++ = '=';
|
|
|
|
}
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
/* Write the value. */
|
|
|
|
for (i = 0; value[i]; i++)
|
|
|
|
{
|
|
|
|
if (value[i] == '\\' || value[i] == '\n')
|
|
|
|
*p++ = '\\';
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
*p++ = value[i];
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
*p = '\n';
|
|
|
|
return 1;
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-03-28 19:58:15 +00:00
|
|
|
grub_envblk_delete (grub_envblk_t envblk, const char *name)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
|
|
|
char *p, *pend;
|
|
|
|
int nl;
|
|
|
|
|
|
|
|
nl = grub_strlen (name);
|
2009-03-28 19:58:15 +00:00
|
|
|
p = envblk->buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1;
|
|
|
|
pend = envblk->buf + envblk->size;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
while (p + nl + 1 < pend)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2009-03-28 19:58:15 +00:00
|
|
|
if (grub_memcmp (p, name, nl) == 0 && p[nl] == '=')
|
|
|
|
{
|
|
|
|
/* Found. */
|
|
|
|
int len = nl + 1;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
while (p + len < pend)
|
|
|
|
{
|
|
|
|
if (p[len] == '\n')
|
|
|
|
break;
|
|
|
|
else if (p[len] == '\\')
|
|
|
|
len += 2;
|
|
|
|
else
|
|
|
|
len++;
|
|
|
|
}
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (p + len >= pend)
|
|
|
|
/* Broken. */
|
|
|
|
return;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
len++;
|
|
|
|
grub_memmove (p, p + len, pend - (p + len));
|
|
|
|
grub_memset (pend - len, '#', len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = find_next_line (p, pend);
|
2009-06-10 21:04:23 +00:00
|
|
|
}
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
grub_envblk_iterate (grub_envblk_t envblk,
|
2013-09-27 00:08:32 +00:00
|
|
|
void *hook_data,
|
|
|
|
int hook (const char *name, const char *value, void *hook_data))
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
|
|
|
char *p, *pend;
|
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
p = envblk->buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1;
|
|
|
|
pend = envblk->buf + envblk->size;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
while (p < pend)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2009-03-28 19:58:15 +00:00
|
|
|
if (*p != '#')
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2009-03-28 19:58:15 +00:00
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
char *name_start, *name_end, *value_start;
|
|
|
|
char *q;
|
|
|
|
int ret;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
name_start = p;
|
|
|
|
while (p < pend && *p != '=')
|
|
|
|
p++;
|
|
|
|
if (p == pend)
|
|
|
|
/* Broken. */
|
|
|
|
return;
|
|
|
|
name_end = p;
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
p++;
|
|
|
|
value_start = p;
|
|
|
|
while (p < pend)
|
|
|
|
{
|
|
|
|
if (*p == '\n')
|
|
|
|
break;
|
|
|
|
else if (*p == '\\')
|
|
|
|
p += 2;
|
|
|
|
else
|
|
|
|
p++;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (p >= pend)
|
|
|
|
/* Broken. */
|
|
|
|
return;
|
|
|
|
|
|
|
|
name = grub_malloc (p - name_start + 1);
|
|
|
|
if (! name)
|
|
|
|
/* out of memory. */
|
|
|
|
return;
|
|
|
|
|
|
|
|
value = name + (value_start - name_start);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
grub_memcpy (name, name_start, name_end - name_start);
|
|
|
|
name[name_end - name_start] = '\0';
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
for (p = value_start, q = value; *p != '\n'; ++p)
|
|
|
|
{
|
|
|
|
if (*p == '\\')
|
|
|
|
*q++ = *++p;
|
|
|
|
else
|
|
|
|
*q++ = *p;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-09-27 00:08:32 +00:00
|
|
|
ret = hook (name, value, hook_data);
|
2009-03-28 19:58:15 +00:00
|
|
|
grub_free (name);
|
|
|
|
if (ret)
|
|
|
|
return;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
p = find_next_line (p, pend);
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
}
|