2009-03-29 Yoshinori K. Okuji <okuji@enbug.org>

Make the format of Environment Block plain text. The boot loader
    part is not tested well yet.
    
    * util/grub-editenv.c (DEFAULT_ENVBLK_SIZE): New macro.
    (buffer): Removed.
    (envblk): Likewise.
    (usage): Remove "info" and "clear". Add "unset". Update the
    description of "set", as this does not delete variables any
    longer.
    (create_envblk_file): Complete rewrite.
    (open_envblk_file): Likewise.
    (cmd_info): Removed.
    (cmd_list): Likewise.
    (cmd_set): Likewise.
    (cmd_clear): Likewise.
    (list_variables): New function.
    (write_envblk): Likewise.
    (set_variables): Likewise.
    (unset_variables): Likewise.
    (main): Complete rewrite.

    * commands/loadenv.c (buffer): Removed.
    (envblk): Likewise.
    (open_envblk_file): New function.
    (read_envblk_file): Complete rewrite.
    (grub_cmd_load_env): Likewise.
    (grub_cmd_list_env): Likewise.
    (struct blocklist): New struct.
    (free_blocklists): New function.
    (check_blocklists): Likewise.
    (write_blocklists): Likewise.
    (grub_cmd_save_env): Complete rewrite.

    * include/grub/lib/envblk.h (GRUB_ENVBLK_SIGNATURE): Replaced with
    a plain text signature.
    (GRUB_ENVBLK_MAXLEN): Removed.
    (struct grub_envblk): Complete rewrite.
    (grub_envblk_find): Removed.
    (grub_envblk_insert): Likewise.
    (grub_envblk_open): New prototype.
    (grub_envblk_set): Likewise.
    (grub_envblk_delete): Put const to VALUE.
    (grub_envblk_iterate): Put const to NAME and VALUE.
    (grub_envblk_close): New prototype.
    (grub_envblk_buffer): New inline function.
    (grub_envblk_size): Likewise.

    * lib/envblk.c: Include grub/mm.h.
    (grub_env_find): Removed.
    (grub_envblk_open): New function.
    (grub_envblk_close): Likewise.
    (escaped_value_len): Likewise.
    (find_next_line): Likewise.
    (grub_envblk_insert): Removed.
    (grub_envblk_set): New function.
    (grub_envblk_delete): Complete rewrite.
    (grub_envblk_iterate): Likewise.
This commit is contained in:
okuji 2009-03-28 19:58:15 +00:00
parent a9368fd30c
commit 5709cfc4d1
7 changed files with 3685 additions and 2644 deletions

View File

@ -1,3 +1,63 @@
2009-03-29 Yoshinori K. Okuji <okuji@enbug.org>
Make the format of Environment Block plain text. The boot loader
part is not tested well yet.
* util/grub-editenv.c (DEFAULT_ENVBLK_SIZE): New macro.
(buffer): Removed.
(envblk): Likewise.
(usage): Remove "info" and "clear". Add "unset". Update the
description of "set", as this does not delete variables any
longer.
(create_envblk_file): Complete rewrite.
(open_envblk_file): Likewise.
(cmd_info): Removed.
(cmd_list): Likewise.
(cmd_set): Likewise.
(cmd_clear): Likewise.
(list_variables): New function.
(write_envblk): Likewise.
(set_variables): Likewise.
(unset_variables): Likewise.
(main): Complete rewrite.
* commands/loadenv.c (buffer): Removed.
(envblk): Likewise.
(open_envblk_file): New function.
(read_envblk_file): Complete rewrite.
(grub_cmd_load_env): Likewise.
(grub_cmd_list_env): Likewise.
(struct blocklist): New struct.
(free_blocklists): New function.
(check_blocklists): Likewise.
(write_blocklists): Likewise.
(grub_cmd_save_env): Complete rewrite.
* include/grub/lib/envblk.h (GRUB_ENVBLK_SIGNATURE): Replaced with
a plain text signature.
(GRUB_ENVBLK_MAXLEN): Removed.
(struct grub_envblk): Complete rewrite.
(grub_envblk_find): Removed.
(grub_envblk_insert): Likewise.
(grub_envblk_open): New prototype.
(grub_envblk_set): Likewise.
(grub_envblk_delete): Put const to VALUE.
(grub_envblk_iterate): Put const to NAME and VALUE.
(grub_envblk_close): New prototype.
(grub_envblk_buffer): New inline function.
(grub_envblk_size): Likewise.
* lib/envblk.c: Include grub/mm.h.
(grub_env_find): Removed.
(grub_envblk_open): New function.
(grub_envblk_close): Likewise.
(escaped_value_len): Likewise.
(find_next_line): Likewise.
(grub_envblk_insert): Removed.
(grub_envblk_set): New function.
(grub_envblk_delete): Complete rewrite.
(grub_envblk_iterate): Likewise.
2009-03-28 Robert Millan <rmh@aybabtu.com>
* conf/i386-pc.rmk (pkglib_MODULES): Add `linux16.mod'.

View File

@ -1,7 +1,7 @@
/* loadenv.c - command to load/save environment variable. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2008 Free Software Foundation, Inc.
* Copyright (C) 2008,2009 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
@ -33,13 +33,9 @@ static const struct grub_arg_option options[] =
{0, 0, 0, 0, 0, 0}
};
char buffer[GRUB_ENVBLK_MAXLEN];
grub_envblk_t envblk;
static grub_file_t
read_envblk_file (char *filename, void NESTED_FUNC_ATTR read_hook (grub_disk_addr_t sector, unsigned offset, unsigned length))
open_envblk_file (char *filename)
{
char *buf = 0;
grub_file_t file;
if (! filename)
@ -52,11 +48,16 @@ read_envblk_file (char *filename, void NESTED_FUNC_ATTR read_hook (grub_disk_add
int len;
len = grub_strlen (prefix);
buf = grub_malloc (len + 1 + sizeof (GRUB_ENVBLK_DEFCFG));
grub_strcpy (buf, prefix);
buf[len] = '/';
grub_strcpy (buf + len + 1, GRUB_ENVBLK_DEFCFG);
filename = buf;
filename = grub_malloc (len + 1 + sizeof (GRUB_ENVBLK_DEFCFG));
if (! filename)
return 0;
grub_strcpy (filename, prefix);
filename[len] = '/';
grub_strcpy (filename + len + 1, GRUB_ENVBLK_DEFCFG);
file = grub_file_open (filename);
grub_free (filename);
return file;
}
else
{
@ -65,66 +66,78 @@ read_envblk_file (char *filename, void NESTED_FUNC_ATTR read_hook (grub_disk_add
}
}
file = grub_file_open (filename);
grub_free (buf);
if (! file)
return 0;
return grub_file_open (filename);
}
if (read_hook)
static grub_envblk_t
read_envblk_file (grub_file_t file)
{
grub_off_t offset = 0;
char *buf;
grub_size_t size = grub_file_size (file);
grub_envblk_t envblk;
buf = grub_malloc (size);
if (! buf)
return 0;
while (size > 0)
{
if (! file->device->disk)
grub_ssize_t ret;
ret = grub_file_read (file, buf + offset, size);
if (ret <= 0)
{
grub_file_close (file);
grub_error (GRUB_ERR_BAD_DEVICE,
"this command is available only for disk devices.");
if (grub_errno == GRUB_ERR_NONE)
grub_error (GRUB_ERR_FILE_READ_ERROR, "cannot read");
grub_free (buf);
return 0;
}
file->read_hook = read_hook;
size -= ret;
offset += ret;
}
if (grub_file_read (file, buffer, GRUB_ENVBLK_MAXLEN) != GRUB_ENVBLK_MAXLEN)
{
grub_file_close (file);
grub_error (GRUB_ERR_BAD_FILE_TYPE, "file too short");
return 0;
}
envblk = grub_envblk_find (buffer);
envblk = grub_envblk_open (buf, offset);
if (! envblk)
{
grub_file_close (file);
grub_error (GRUB_ERR_BAD_FILE_TYPE, "environment block not found");
grub_free (buf);
grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid environment block");
return 0;
}
return file;
return envblk;
}
static grub_err_t
grub_cmd_load_env (grub_extcmd_t cmd,
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
struct grub_arg_list *state = cmd->state;
grub_file_t file;
grub_envblk_t envblk;
auto int hook (char *name, char *value);
int hook (char *name, char *value)
{
grub_env_set (name, value);
return 0;
}
file = read_envblk_file ((state[0].set) ? state[0].arg : 0, 0);
auto int set_var (const char *name, const char *value);
int set_var (const char *name, const char *value)
{
grub_env_set (name, value);
return 0;
}
file = open_envblk_file ((state[0].set) ? state[0].arg : 0);
if (! file)
return grub_errno;
envblk = read_envblk_file (file);
if (! envblk)
goto fail;
grub_envblk_iterate (envblk, set_var);
grub_envblk_close (envblk);
fail:
grub_file_close (file);
grub_envblk_iterate (envblk, hook);
return grub_errno;
}
@ -135,83 +148,207 @@ grub_cmd_list_env (grub_extcmd_t cmd,
{
struct grub_arg_list *state = cmd->state;
grub_file_t file;
grub_envblk_t envblk;
auto int hook (char *name, char *value);
int hook (char *name, char *value)
/* Print all variables in current context. */
auto int print_var (const char *name, const char *value);
int print_var (const char *name, const char *value)
{
grub_printf ("%s=%s\n", name, value);
return 0;
}
file = read_envblk_file ((state[0].set) ? state[0].arg : 0, 0);
file = open_envblk_file ((state[0].set) ? state[0].arg : 0);
if (! file)
return grub_errno;
envblk = read_envblk_file (file);
if (! envblk)
goto fail;
grub_envblk_iterate (envblk, print_var);
grub_envblk_close (envblk);
fail:
grub_file_close (file);
grub_envblk_iterate (envblk, hook);
return grub_errno;
}
/* Used to maintain a variable length of blocklists internally. */
struct blocklist
{
grub_disk_addr_t sector;
unsigned offset;
unsigned length;
struct blocklist *next;
};
static void
free_blocklists (struct blocklist *p)
{
struct blocklist *q;
for (; p; p = q)
{
q = p->next;
grub_free (p);
}
}
static int
check_blocklists (grub_envblk_t envblk, struct blocklist *blocklists,
grub_file_t file)
{
grub_size_t total_length;
grub_size_t index;
grub_disk_t disk;
grub_disk_addr_t part_start;
struct blocklist *p;
char *buf;
/* Sanity checks. */
total_length = 0;
for (p = blocklists; p; p = p->next)
{
struct blocklist *q;
for (q = p->next; q; q = q->next)
{
/* Check if any pair of blocks overlap. */
if (p->sector == q->sector)
{
/* This might be actually valid, but it is unbelievable that
any filesystem makes such a silly allocation. */
grub_error (GRUB_ERR_BAD_FS, "malformed file");
return 0;
}
}
total_length += p->length;
}
if (total_length != grub_file_size (file))
{
/* Maybe sparse, unallocated sectors. No way in GRUB. */
grub_error (GRUB_ERR_BAD_FILE_TYPE, "sparse file not allowed");
return 0;
}
/* One more sanity check. Re-read all sectors by blocklists, and compare
those with the data read via a file. */
disk = file->device->disk;
if (disk->partition)
part_start = grub_partition_get_start (disk->partition);
else
part_start = 0;
buf = grub_envblk_buffer (envblk);
for (p = blocklists, index = 0; p; p = p->next, index += p->length)
{
char blockbuf[GRUB_DISK_SECTOR_SIZE];
if (grub_disk_read (disk, p->sector - part_start,
p->offset, p->length, blockbuf))
return 0;
if (grub_memcmp (buf + index, blockbuf, p->length) != 0)
{
grub_error (GRUB_ERR_FILE_READ_ERROR, "invalid blocklist");
return 0;
}
}
return 1;
}
static int
write_blocklists (grub_envblk_t envblk, struct blocklist *blocklists,
grub_file_t file)
{
char *buf;
grub_disk_t disk;
grub_disk_addr_t part_start;
struct blocklist *p;
grub_size_t index;
buf = grub_envblk_buffer (envblk);
disk = file->device->disk;
if (disk->partition)
part_start = grub_partition_get_start (disk->partition);
else
part_start = 0;
index = 0;
for (p = blocklists; p; p = p->next, index += p->length)
{
if (grub_disk_write (disk, p->sector - part_start,
p->offset, p->length, buf + index))
return 0;
}
return 1;
}
static grub_err_t
grub_cmd_save_env (grub_extcmd_t cmd, int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
grub_file_t file;
grub_disk_t disk;
grub_disk_addr_t addr[GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS];
char buf[GRUB_DISK_SECTOR_SIZE];
grub_disk_addr_t part_start = 0;
int num = 0;
auto void NESTED_FUNC_ATTR hook (grub_disk_addr_t sector, unsigned offset,
unsigned length);
void NESTED_FUNC_ATTR hook (grub_disk_addr_t sector,
unsigned offset, unsigned length)
grub_envblk_t envblk;
struct blocklist *head = 0;
struct blocklist *tail = 0;
/* Store blocklists in a linked list. */
auto void NESTED_FUNC_ATTR read_hook (grub_disk_addr_t sector,
unsigned offset,
unsigned length);
void NESTED_FUNC_ATTR read_hook (grub_disk_addr_t sector,
unsigned offset, unsigned length)
{
if ((offset != 0) || (length != GRUB_DISK_SECTOR_SIZE))
struct blocklist *block;
if (offset + length > GRUB_DISK_SECTOR_SIZE)
/* Seemingly a bug. */
return;
block = grub_malloc (sizeof (*block));
if (! block)
return;
if (num < (GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS))
addr[num++] = sector;
block->sector = sector;
block->offset = offset;
block->length = length;
/* Slightly complicated, because the list should be FIFO. */
block->next = 0;
if (tail)
tail->next = block;
tail = block;
if (! head)
head = block;
}
if (! argc)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "No variable is specified");
file = read_envblk_file ((state[0].set) ? state[0].arg : 0, hook);
file = open_envblk_file ((state[0].set) ? state[0].arg : 0);
if (! file)
return grub_errno;
if (! file->device->disk)
{
grub_file_close (file);
return grub_error (GRUB_ERR_BAD_DEVICE, "disk device required");
}
file->read_hook = read_hook;
envblk = read_envblk_file (file);
file->read_hook = 0;
if (! envblk)
goto fail;
if (num != GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS)
{
grub_error (GRUB_ERR_BAD_DEVICE, "invalid blocklist");
goto quit;
}
disk = file->device->disk;
if (disk->partition)
part_start = grub_partition_get_start (disk->partition);
for (num = 0; num < (GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS); num++)
{
if (grub_disk_read (disk, addr[num] - part_start, 0,
GRUB_DISK_SECTOR_SIZE, buf))
goto quit;
if (grub_memcmp (&buffer[num << GRUB_DISK_SECTOR_BITS], buf,
GRUB_DISK_SECTOR_SIZE))
{
grub_error (GRUB_ERR_BAD_DEVICE, "invalid blocklist");
goto quit;
}
}
if (! check_blocklists (envblk, head, file))
goto fail;
while (argc)
{
char *value;
@ -219,10 +356,10 @@ grub_cmd_save_env (grub_extcmd_t cmd, int argc, char **args)
value = grub_env_get (args[0]);
if (value)
{
if (grub_envblk_insert (envblk, args[0], value))
if (! grub_envblk_set (envblk, args[0], value))
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "environment block too small");
goto quit;
goto fail;
}
}
@ -230,15 +367,13 @@ grub_cmd_save_env (grub_extcmd_t cmd, int argc, char **args)
args++;
}
for (num = 0; num < (GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS); num++)
if (grub_disk_write (disk, addr[num] - part_start, 0,
GRUB_DISK_SECTOR_SIZE,
&buffer[num << GRUB_DISK_SECTOR_BITS]))
goto quit;
quit:
write_blocklists (envblk, head, file);
fail:
if (envblk)
grub_envblk_close (envblk);
free_blocklists (head);
grub_file_close (file);
return grub_errno;
}

View File

@ -4,6 +4,9 @@
prefixed with an asterisk */
#undef ABSOLUTE_WITHOUT_ASTERISK
/* Define if building universal (internal helper macro) */
#undef AC_APPLE_UNIVERSAL_BUILD
/* Define it to \"addr32\" or \"addr32;\" to make GAS happy */
#undef ADDR32
@ -112,17 +115,52 @@
/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS
/* Define to 1 if your processor stores words with the most significant byte
first (like Motorola and SPARC, unlike Intel and VAX). */
#undef WORDS_BIGENDIAN
/* Number of bits in a file offset, on hosts where this is settable. */
#undef _FILE_OFFSET_BITS
/* Enable extensions on AIX 3, Interix. */
#ifndef _ALL_SOURCE
# undef _ALL_SOURCE
#endif
/* Enable GNU extensions on systems that have them. */
#ifndef _GNU_SOURCE
# undef _GNU_SOURCE
#endif
/* Enable threading extensions on Solaris. */
#ifndef _POSIX_PTHREAD_SEMANTICS
# undef _POSIX_PTHREAD_SEMANTICS
#endif
/* Enable extensions on HP NonStop. */
#ifndef _TANDEM_SOURCE
# undef _TANDEM_SOURCE
#endif
/* Enable general extensions on Solaris. */
#ifndef __EXTENSIONS__
# undef __EXTENSIONS__
#endif
/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
significant byte first (like Motorola and SPARC, unlike Intel). */
#if defined AC_APPLE_UNIVERSAL_BUILD
# if defined __BIG_ENDIAN__
# define WORDS_BIGENDIAN 1
# endif
#else
# ifndef WORDS_BIGENDIAN
# undef WORDS_BIGENDIAN
# endif
#endif
/* Number of bits in a file offset, on hosts where this is settable. */
#undef _FILE_OFFSET_BITS
/* Define for large files, on AIX-style hosts. */
#undef _LARGE_FILES
/* Define to 1 if on MINIX. */
#undef _MINIX
/* Define to 2 if the system does not provide POSIX.1 features except with
this defined. */
#undef _POSIX_1_SOURCE
/* Define to 1 if you need to in order for `stat' and other things to work. */
#undef _POSIX_SOURCE

5280
configure vendored

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2008 Free Software Foundation, Inc.
* Copyright (C) 2008,2009 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
@ -19,27 +19,37 @@
#ifndef GRUB_ENVBLK_HEADER
#define GRUB_ENVBLK_HEADER 1
#define GRUB_ENVBLK_SIGNATURE 0x764e6547 /* GeNv */
#define GRUB_ENVBLK_MAXLEN 8192
#define GRUB_ENVBLK_SIGNATURE "# GRUB Environment Block\n"
#define GRUB_ENVBLK_DEFCFG "grubenv"
#ifndef ASM_FILE
struct grub_envblk
{
grub_uint32_t signature;
grub_uint16_t length;
char data[0];
} __attribute__ ((packed));
char *buf;
grub_size_t size;
};
typedef struct grub_envblk *grub_envblk_t;
grub_envblk_t grub_envblk_find (char *buf);
int grub_envblk_insert (grub_envblk_t envblk, char *name, char *value);
void grub_envblk_delete (grub_envblk_t envblk, char *name);
void grub_envblk_iterate (grub_envblk_t envblk, int hook (char *name, char *value));
grub_envblk_t grub_envblk_open (char *buf, grub_size_t size);
int grub_envblk_set (grub_envblk_t envblk, const char *name, const char *value);
void grub_envblk_delete (grub_envblk_t envblk, const char *name);
void grub_envblk_iterate (grub_envblk_t envblk,
int hook (const char *name, const char *value));
void grub_envblk_close (grub_envblk_t envblk);
#endif
static inline char *
grub_envblk_buffer (const grub_envblk_t envblk)
{
return envblk->buf;
}
static inline grub_size_t
grub_envblk_size (const grub_envblk_t envblk)
{
return envblk->size;
}
#endif /* ! ASM_FILE */
#endif /* ! GRUB_ENVBLK_HEADER */

View File

@ -1,7 +1,7 @@
/* envblk.c - Common function for environment block. */
/* envblk.c - Common functions for environment block. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2008 Free Software Foundation, Inc.
* Copyright (C) 2008,2009 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
@ -20,137 +20,277 @@
#include <config.h>
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/lib/envblk.h>
grub_envblk_t
grub_envblk_find (char *buf)
grub_envblk_open (char *buf, grub_size_t size)
{
grub_uint32_t *pd;
int len;
grub_envblk_t envblk;
pd = (grub_uint32_t *) buf;
for (len = GRUB_ENVBLK_MAXLEN - 6; len > 0; len -= 4, pd++)
if (*pd == GRUB_ENVBLK_SIGNATURE)
{
grub_envblk_t p;
p = (grub_envblk_t) pd;
if (p->length <= len)
return p;
}
return 0;
}
int
grub_envblk_insert (grub_envblk_t envblk, char *name, char *value)
{
char *p, *pend;
char *found = 0;
int nl;
nl = grub_strlen (name);
p = envblk->data;
pend = p + envblk->length;
while (*p)
if (size < sizeof (GRUB_ENVBLK_SIGNATURE)
|| grub_memcmp (buf, GRUB_ENVBLK_SIGNATURE,
sizeof (GRUB_ENVBLK_SIGNATURE) - 1))
{
if ((! found) && (! grub_memcmp (name, p, nl)) && (p[nl] == '='))
found = p + nl + 1;
p += grub_strlen (p) + 1;
if (p >= pend)
return 1;
grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid environment block");
return 0;
}
if (found)
envblk = grub_malloc (sizeof (*envblk));
if (envblk)
{
int len1, len2;
len1 = grub_strlen (found);
len2 = grub_strlen (value);
if ((p - envblk->data) + 1 - len1 + len2 > envblk->length)
return 1;
grub_memcpy (found + len2 + 1, found + len1 + 1, (p - found) - len1);
grub_strcpy (found, value);
envblk->buf = buf;
envblk->size = size;
}
else
{
int len2 = grub_strlen (value);
if ((p - envblk->data) + nl + 1 + len2 + 2 > envblk->length)
return 1;
grub_strcpy (p, name);
p[nl] = '=';
grub_strcpy (p + nl + 1, value);
p[nl + 1 + len2 + 1] = 0;
}
return 0;
return envblk;
}
void
grub_envblk_delete (grub_envblk_t envblk, char *name)
grub_envblk_close (grub_envblk_t envblk)
{
grub_free (envblk->buf);
grub_free (envblk);
}
static int
escaped_value_len (const char *value)
{
int n = 0;
char *p;
for (p = (char *) value; *p; p++)
{
if (*p == '\\' || *p == '\n')
n += 2;
else
n++;
}
return n;
}
static char *
find_next_line (char *p, const char *pend)
{
while (p < pend)
{
if (*p == '\\')
p += 2;
else if (*p == '\n')
break;
else
p++;
}
return p + 1;
}
int
grub_envblk_set (grub_envblk_t envblk, const char *name, const char *value)
{
char *p, *pend;
char *space;
int found = 0;
int nl;
int vl;
int i;
nl = grub_strlen (name);
vl = escaped_value_len (value);
p = envblk->buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1;
pend = envblk->buf + envblk->size;
/* First, look at free space. */
for (space = pend - 1; *space == '#'; space--)
;
if (*space != '\n')
/* Broken. */
return 0;
space++;
while (p + nl + 1 < space)
{
if (grub_memcmp (p, name, nl) == 0 && p[nl] == '=')
{
int len;
/* Found the same name. */
p += nl + 1;
/* Check the length of the current value. */
len = 0;
while (p + len < pend && p[len] != '\n')
{
if (p[len] == '\\')
len += 2;
else
len++;
}
if (p + len >= pend)
/* Broken. */
return 0;
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));
grub_memset (space + len - vl, '#', len - vl);
}
else
/* Move the following characters forward. */
grub_memmove (p + vl, p + len, pend - (p + vl));
found = 1;
break;
}
p = find_next_line (p, pend);
}
if (! found)
{
/* Append a new variable. */
if (pend - space < nl + 1 + vl + 1)
/* No space. */
return 0;
grub_memcpy (space, name, nl);
p = space + nl;
*p++ = '=';
}
/* Write the value. */
for (i = 0; value[i]; i++)
{
if (value[i] == '\\' || value[i] == '\n')
*p++ = '\\';
*p++ = value[i];
}
*p = '\n';
return 1;
}
void
grub_envblk_delete (grub_envblk_t envblk, const char *name)
{
char *p, *pend;
char *found = 0;
int nl;
nl = grub_strlen (name);
p = envblk->data;
pend = p + envblk->length;
p = envblk->buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1;
pend = envblk->buf + envblk->size;
while (*p)
while (p + nl + 1 < pend)
{
if ((! found) && (! grub_memcmp (name, p, nl)) && (p[nl] == '='))
found = p;
if (grub_memcmp (p, name, nl) == 0 && p[nl] == '=')
{
/* Found. */
int len = nl + 1;
p += grub_strlen (p) + 1;
if (p >= pend)
return;
}
while (p + len < pend)
{
if (p[len] == '\n')
break;
else if (p[len] == '\\')
len += 2;
else
len++;
}
if (found)
{
int len;
if (p + len >= pend)
/* Broken. */
return;
len = grub_strlen (found);
grub_memcpy (found, found + len + 1, (p - found) - len);
}
len++;
grub_memmove (p, p + len, pend - (p + len));
grub_memset (pend - len, '#', len);
break;
}
p = find_next_line (p, pend);
}
}
void
grub_envblk_iterate (grub_envblk_t envblk,
int hook (char *name, char *value))
int hook (const char *name, const char *value))
{
char *p, *pend;
p = envblk->data;
pend = p + envblk->length;
p = envblk->buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1;
pend = envblk->buf + envblk->size;
while (*p)
while (p < pend)
{
char *v;
int r;
v = grub_strchr (p, '=');
if (v)
if (*p != '#')
{
*v = 0;
r = hook (p, v + 1);
*v = '=';
char *name;
char *value;
char *name_start, *name_end, *value_start;
char *q;
int ret;
name_start = p;
while (p < pend && *p != '=')
p++;
if (p == pend)
/* Broken. */
return;
name_end = p;
p++;
value_start = p;
while (p < pend)
{
if (*p == '\n')
break;
else if (*p == '\\')
p += 2;
else
p++;
}
if (p >= pend)
/* Broken. */
return;
name = grub_malloc (p - name_start + 1);
if (! name)
/* out of memory. */
return;
value = name + (value_start - name_start);
grub_memcpy (name, name_start, name_end - name_start);
name[name_end - name_start] = '\0';
for (p = value_start, q = value; *p != '\n'; ++p)
{
if (*p == '\\')
*q++ = *++p;
else
*q++ = *p;
}
*q = '\0';
ret = hook (name, value);
grub_free (name);
if (ret)
return;
}
else
r = hook (p, "");
if (r)
break;
p += grub_strlen (p) + 1;
if (p >= pend)
break;
p = find_next_line (p, pend);
}
}

View File

@ -1,7 +1,7 @@
/* grub-editenv.c - tool to edit environment block. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2008 Free Software Foundation, Inc.
* Copyright (C) 2008,2009 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
@ -29,6 +29,8 @@
#include <stdlib.h>
#include <getopt.h>
#define DEFAULT_ENVBLK_SIZE 1024
void
grub_putchar (int c)
{
@ -63,9 +65,6 @@ static struct option options[] = {
{0, 0, 0, 0}
};
char buffer[GRUB_ENVBLK_MAXLEN];
grub_envblk_t envblk;
static void
usage (int status)
{
@ -78,10 +77,9 @@ Usage: grub-editenv [OPTIONS] FILENAME COMMAND\n\
Tool to edit environment block.\n\
\nCommands:\n\
create create a blank environment block file\n\
info show information about the environment block\n\
list list the current variables\n\
set [name=value] ... change/delete variables\n\
clear delete all variables\n\
set [name=value ...] set variables\n\
unset [name ....] delete variables\n\
\nOptions:\n\
-h, --help display this message and exit\n\
-V, --version print version information and exit\n\
@ -92,108 +90,161 @@ Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
exit (status);
}
int
create_envblk_file (char *name)
static void
create_envblk_file (const char *name)
{
FILE *f;
grub_envblk_t p;
FILE *fp;
char *buf;
buf = malloc (DEFAULT_ENVBLK_SIZE);
if (! buf)
grub_util_error ("out of memory");
f = fopen (name, "wb");
if (! f)
return 1;
fp = fopen (name, "wb");
if (! fp)
grub_util_error ("cannot open the file %s", name);
/* Just in case OS don't save 0s. */
memset (buffer, -1, sizeof (buffer));
memcpy (buf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
memset (buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1, '#',
DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) + 1);
if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE)
grub_util_error ("cannot write to the file %s", name);
p = (grub_envblk_t) &buffer[0];
p->signature = GRUB_ENVBLK_SIGNATURE;
p->length = sizeof (buffer) - sizeof (struct grub_envblk);
p->data[0] = p->data[1] = 0;
fwrite (buffer, sizeof (buffer), 1, f);
fclose (f);
return 0;
fsync (fileno (fp));
free (buf);
fclose (fp);
}
FILE *
open_envblk_file (char *name)
static grub_envblk_t
open_envblk_file (const char *name)
{
FILE *f;
FILE *fp;
char *buf;
size_t size;
grub_envblk_t envblk;
fp = fopen (name, "rb");
if (! fp)
{
/* Create the file implicitly. */
create_envblk_file (name);
fp = fopen (name, "rb");
if (! fp)
grub_util_error ("cannot open the file %s", name);
}
f = fopen (name, "r+b");
if (! f)
grub_util_error ("Can\'t open file %s", name);
if (fseek (fp, 0, SEEK_END) < 0)
grub_util_error ("cannot seek the file %s", name);
if (fread (buffer, 1, sizeof (buffer), f) != sizeof (buffer))
grub_util_error ("The envblk file is too short");
size = (size_t) ftell (fp);
envblk = grub_envblk_find (buffer);
if (fseek (fp, 0, SEEK_SET) < 0)
grub_util_error ("cannot seek the file %s", name);
buf = malloc (size);
if (! buf)
grub_util_error ("out of memory");
if (fread (buf, 1, size, fp) != size)
grub_util_error ("cannot read the file %s", name);
fclose (fp);
envblk = grub_envblk_open (buf, size);
if (! envblk)
grub_util_error ("Can\'t find environment block");
grub_util_error ("invalid environment block");
return f;
return envblk;
}
static void
cmd_info (void)
list_variables (const char *name)
{
printf ("Envblk offset: %ld\n", (long) (envblk->data - buffer));
printf ("Envblk length: %d\n", envblk->length);
}
static void
cmd_list (void)
{
auto int hook (char *name, char *value);
int hook (char *name, char *value)
grub_envblk_t envblk;
auto int print_var (const char *name, const char *value);
int print_var (const char *name, const char *value)
{
printf ("%s=%s\n", name, value);
return 0;
}
grub_envblk_iterate (envblk, hook);
envblk = open_envblk_file (name);
grub_envblk_iterate (envblk, print_var);
grub_envblk_close (envblk);
}
static void
cmd_set (int argc, char *argv[])
write_envblk (const char *name, grub_envblk_t envblk)
{
FILE *fp;
fp = fopen (name, "wb");
if (! fp)
grub_util_error ("cannot open the file %s", name);
if (fwrite (grub_envblk_buffer (envblk), 1, grub_envblk_size (envblk), fp)
!= grub_envblk_size (envblk))
grub_util_error ("cannot write to the file %s", name);
fsync (fileno (fp));
fclose (fp);
}
static void
set_variables (const char *name, int argc, char *argv[])
{
grub_envblk_t envblk;
envblk = open_envblk_file (name);
while (argc)
{
char *p;
p = strchr (argv[0], '=');
if (! p)
grub_util_error ("Invalid parameter");
grub_util_error ("invalid parameter %s", argv[0]);
*(p++) = 0;
if (*p)
{
if (grub_envblk_insert (envblk, argv[0], p))
grub_util_error ("Environment block too small");
}
else
grub_envblk_delete (envblk, argv[0]);
if (! grub_envblk_set (envblk, argv[0], p))
grub_util_error ("environment block too small");
argc--;
argv++;
}
write_envblk (name, envblk);
grub_envblk_close (envblk);
}
static void
cmd_clear (void)
unset_variables (const char *name, int argc, char *argv[])
{
envblk->data[0] = envblk->data[1] = 0;
grub_envblk_t envblk;
envblk = open_envblk_file (name);
while (argc)
{
grub_envblk_delete (envblk, argv[0]);
argc--;
argv++;
}
write_envblk (name, envblk);
grub_envblk_close (envblk);
}
int
main (int argc, char *argv[])
{
FILE *f;
char *filename;
char *command;
progname = "grub-editenv";
/* Check for options. */
while (1)
{
@ -222,40 +273,35 @@ main (int argc, char *argv[])
}
}
/* Obtain PATH. */
/* Obtain the filename. */
if (optind >= argc)
{
fprintf (stderr, "Filename not specified.\n");
fprintf (stderr, "no filename specified\n");
usage (1);
}
if (optind + 1 >= argc)
{
fprintf (stderr, "Command not specified.\n");
fprintf (stderr, "no command specified\n");
usage (1);
}
filename = argv[optind];
command = argv[optind + 1];
if (strcmp (command, "create") == 0)
create_envblk_file (filename);
else if (strcmp (command, "list") == 0)
list_variables (filename);
else if (strcmp (command, "set") == 0)
set_variables (filename, argc - optind - 2, argv + optind + 2);
else if (strcmp (command, "unset") == 0)
unset_variables (filename, argc - optind - 2, argv + optind + 2);
else
{
fprintf (stderr, "unknown command %s\n", command);
usage (1);
}
if (! strcmp (argv[optind + 1], "create"))
return create_envblk_file (argv[optind]);
f = open_envblk_file (argv[optind]);
optind++;
if (! strcmp (argv[optind], "info"))
cmd_info ();
else if (! strcmp (argv[optind], "list"))
cmd_list ();
else
{
if (! strcmp (argv[optind], "set"))
cmd_set (argc - optind - 1, argv + optind + 1);
else if (! strcmp (argv[optind], "clear"))
cmd_clear ();
fseeko (f, 0, SEEK_SET);
fwrite (buffer, sizeof (buffer), 1, f);
}
fclose (f);
return 0;
}