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,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;
}