2008-07-02 07:20:11 +00:00
|
|
|
/* grub-editenv.c - tool to edit environment block. */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-01-01 20:32:30 +00:00
|
|
|
* Copyright (C) 2008,2009,2010 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>
|
2010-05-06 06:04:04 +00:00
|
|
|
#include <grub/emu/misc.h>
|
2008-07-02 07:20:11 +00:00
|
|
|
#include <grub/util/misc.h>
|
2008-07-27 13:51:30 +00:00
|
|
|
#include <grub/lib/envblk.h>
|
2009-11-25 23:10:02 +00:00
|
|
|
#include <grub/i18n.h>
|
2008-07-02 07:20:11 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2010-09-20 10:39:28 +00:00
|
|
|
#include <argp.h>
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-11-25 23:10:02 +00:00
|
|
|
#include "progname.h"
|
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
#define DEFAULT_ENVBLK_SIZE 1024
|
2010-09-20 10:39:28 +00:00
|
|
|
#define DEFAULT_ENVBLK_PATH DEFAULT_DIRECTORY "/" GRUB_ENVBLK_DEFCFG
|
|
|
|
|
|
|
|
static struct argp_option options[] = {
|
|
|
|
{0, 0, 0, OPTION_DOC, N_("Commands:"), 1},
|
|
|
|
{"create", 0, 0, OPTION_DOC|OPTION_NO_USAGE,
|
|
|
|
N_("Create a blank environment block file."), 0},
|
|
|
|
{"list", 0, 0, OPTION_DOC|OPTION_NO_USAGE,
|
|
|
|
N_("List the current variables."), 0},
|
2012-03-10 12:12:45 +00:00
|
|
|
/* TRANSLATORS: "set" is a keyword. It's a summary of "set" subcommand. */
|
|
|
|
{N_("set [NAME=VALUE ...]"), 0, 0, OPTION_DOC|OPTION_NO_USAGE,
|
2010-09-20 10:39:28 +00:00
|
|
|
N_("Set variables."), 0},
|
2012-03-10 12:12:45 +00:00
|
|
|
/* TRANSLATORS: "unset" is a keyword. It's a summary of "unset" subcommand. */
|
|
|
|
{N_("unset [NAME ...]"), 0, 0, OPTION_DOC|OPTION_NO_USAGE,
|
2010-09-20 10:39:28 +00:00
|
|
|
N_("Delete variables."), 0},
|
|
|
|
|
|
|
|
{0, 0, 0, OPTION_DOC, N_("Options:"), -1},
|
2012-02-05 10:23:47 +00:00
|
|
|
{"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
|
2010-09-20 10:39:28 +00:00
|
|
|
|
|
|
|
{ 0, 0, 0, 0, 0, 0 }
|
2008-07-02 07:20:11 +00:00
|
|
|
};
|
|
|
|
|
2010-09-20 10:39:28 +00:00
|
|
|
/* Print the version information. */
|
2008-07-02 07:20:11 +00:00
|
|
|
static void
|
2010-09-20 10:39:28 +00:00
|
|
|
print_version (FILE *stream, struct argp_state *state)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2010-09-20 10:39:28 +00:00
|
|
|
fprintf (stream, "%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
|
|
|
|
}
|
|
|
|
void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
|
|
|
|
|
|
|
|
/* Set the bug report address */
|
|
|
|
const char *argp_program_bug_address = "<"PACKAGE_BUGREPORT">";
|
|
|
|
|
2012-02-10 12:26:19 +00:00
|
|
|
static error_t argp_parser (int key, char *arg, struct argp_state *state)
|
2010-09-20 10:39:28 +00:00
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case 'v':
|
|
|
|
verbosity++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARGP_KEY_NO_ARGS:
|
2010-09-20 16:56:14 +00:00
|
|
|
fprintf (stderr, "%s",
|
|
|
|
_("You need to specify at least one command.\n"));
|
2010-09-20 10:39:28 +00:00
|
|
|
argp_usage (state);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ARGP_ERR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
help_filter (int key, const char *text, void *input __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case ARGP_KEY_HELP_POST_DOC:
|
|
|
|
return xasprintf(text, DEFAULT_ENVBLK_PATH);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (char *) text;
|
|
|
|
}
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
2010-09-20 10:39:28 +00:00
|
|
|
struct argp argp = {
|
|
|
|
options, argp_parser, N_("FILENAME COMMAND"),
|
2010-09-21 09:17:54 +00:00
|
|
|
"\n"N_("\
|
|
|
|
Tool to edit environment block.")
|
|
|
|
"\v"N_("\
|
2012-03-03 12:05:08 +00:00
|
|
|
If FILENAME is `-', the default value %s is used."),
|
2010-09-20 10:39:28 +00:00
|
|
|
NULL, help_filter, NULL
|
|
|
|
};
|
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
static void
|
|
|
|
create_envblk_file (const char *name)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2009-03-28 19:58:15 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *buf;
|
2009-10-01 18:22:43 +00:00
|
|
|
char *namenew;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2012-02-10 12:26:19 +00:00
|
|
|
buf = xmalloc (DEFAULT_ENVBLK_SIZE);
|
2009-03-28 19:58:15 +00:00
|
|
|
|
2009-12-07 16:46:24 +00:00
|
|
|
namenew = xasprintf ("%s.new", name);
|
2009-10-01 18:22:43 +00:00
|
|
|
fp = fopen (namenew, "wb");
|
2009-03-28 19:58:15 +00:00
|
|
|
if (! fp)
|
2012-02-05 10:07:33 +00:00
|
|
|
grub_util_error (_("cannot open `%s': %s"), namenew,
|
|
|
|
strerror (errno));
|
2009-03-28 19:58:15 +00:00
|
|
|
|
|
|
|
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);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE)
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_util_error (_("cannot write to `%s': %s"), namenew,
|
2012-02-05 10:07:33 +00:00
|
|
|
strerror (errno));
|
2009-03-28 19:58:15 +00:00
|
|
|
|
|
|
|
fsync (fileno (fp));
|
|
|
|
free (buf);
|
|
|
|
fclose (fp);
|
2009-10-01 18:22:43 +00:00
|
|
|
|
|
|
|
if (rename (namenew, name) < 0)
|
2011-11-11 23:34:14 +00:00
|
|
|
grub_util_error (_("cannot rename the file %s to %s"), namenew, name);
|
2009-10-01 18:22:43 +00:00
|
|
|
free (namenew);
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
static grub_envblk_t
|
|
|
|
open_envblk_file (const char *name)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2009-03-28 19:58:15 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *buf;
|
|
|
|
size_t size;
|
|
|
|
grub_envblk_t envblk;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
fp = fopen (name, "rb");
|
|
|
|
if (! fp)
|
|
|
|
{
|
|
|
|
/* Create the file implicitly. */
|
|
|
|
create_envblk_file (name);
|
|
|
|
fp = fopen (name, "rb");
|
|
|
|
if (! fp)
|
2012-02-05 10:07:33 +00:00
|
|
|
grub_util_error (_("cannot open `%s': %s"), name,
|
|
|
|
strerror (errno));
|
2009-03-28 19:58:15 +00:00
|
|
|
}
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (fseek (fp, 0, SEEK_END) < 0)
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_util_error (_("cannot seek `%s': %s"), name,
|
2012-02-05 10:07:33 +00:00
|
|
|
strerror (errno));
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
size = (size_t) ftell (fp);
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (fseek (fp, 0, SEEK_SET) < 0)
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_util_error (_("cannot seek `%s': %s"), name,
|
2012-02-05 10:07:33 +00:00
|
|
|
strerror (errno));
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2012-02-10 12:26:19 +00:00
|
|
|
buf = xmalloc (size);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (fread (buf, 1, size, fp) != size)
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_util_error (_("cannot read `%s': %s"), name,
|
2012-02-05 10:07:33 +00:00
|
|
|
strerror (errno));
|
2008-07-02 07:20:11 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
fclose (fp);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
envblk = grub_envblk_open (buf, size);
|
|
|
|
if (! envblk)
|
2012-02-10 12:26:19 +00:00
|
|
|
grub_util_error ("%s", _("invalid environment block"));
|
2009-03-28 19:58:15 +00:00
|
|
|
|
|
|
|
return envblk;
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:52:05 +00:00
|
|
|
static int
|
2013-09-27 00:08:32 +00:00
|
|
|
print_var (const char *varname, const char *value,
|
|
|
|
void *hook_data __attribute__ ((unused)))
|
2013-03-01 12:52:05 +00:00
|
|
|
{
|
|
|
|
printf ("%s=%s\n", varname, value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-02 07:20:11 +00:00
|
|
|
static void
|
2009-03-28 19:58:15 +00:00
|
|
|
list_variables (const char *name)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2009-03-28 19:58:15 +00:00
|
|
|
grub_envblk_t envblk;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
envblk = open_envblk_file (name);
|
2013-09-27 00:08:32 +00:00
|
|
|
grub_envblk_iterate (envblk, NULL, print_var);
|
2009-03-28 19:58:15 +00:00
|
|
|
grub_envblk_close (envblk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
write_envblk (const char *name, grub_envblk_t envblk)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
fp = fopen (name, "wb");
|
|
|
|
if (! fp)
|
2012-02-05 10:07:33 +00:00
|
|
|
grub_util_error (_("cannot open `%s': %s"), name,
|
|
|
|
strerror (errno));
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (fwrite (grub_envblk_buffer (envblk), 1, grub_envblk_size (envblk), fp)
|
|
|
|
!= grub_envblk_size (envblk))
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_util_error (_("cannot write to `%s': %s"), name,
|
2012-02-05 10:07:33 +00:00
|
|
|
strerror (errno));
|
2009-03-28 19:58:15 +00:00
|
|
|
|
|
|
|
fsync (fileno (fp));
|
|
|
|
fclose (fp);
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-03-28 19:58:15 +00:00
|
|
|
set_variables (const char *name, int argc, char *argv[])
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2009-03-28 19:58:15 +00:00
|
|
|
grub_envblk_t envblk;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
envblk = open_envblk_file (name);
|
2008-07-02 07:20:11 +00:00
|
|
|
while (argc)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = strchr (argv[0], '=');
|
|
|
|
if (! p)
|
2011-11-11 23:34:14 +00:00
|
|
|
grub_util_error (_("invalid parameter %s"), argv[0]);
|
2008-07-02 07:20:11 +00:00
|
|
|
|
|
|
|
*(p++) = 0;
|
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
if (! grub_envblk_set (envblk, argv[0], p))
|
2012-02-10 12:26:19 +00:00
|
|
|
grub_util_error ("%s", _("environment block too small"));
|
2008-07-02 07:20:11 +00:00
|
|
|
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
2009-03-28 19:58:15 +00:00
|
|
|
|
|
|
|
write_envblk (name, envblk);
|
|
|
|
grub_envblk_close (envblk);
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-03-28 19:58:15 +00:00
|
|
|
unset_variables (const char *name, int argc, char *argv[])
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2009-03-28 19:58:15 +00:00
|
|
|
grub_envblk_t envblk;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-03-28 19:58:15 +00:00
|
|
|
envblk = open_envblk_file (name);
|
|
|
|
while (argc)
|
|
|
|
{
|
|
|
|
grub_envblk_delete (envblk, argv[0]);
|
|
|
|
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_envblk (name, envblk);
|
|
|
|
grub_envblk_close (envblk);
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2012-02-10 11:28:34 +00:00
|
|
|
const char *filename;
|
2009-03-28 19:58:15 +00:00
|
|
|
char *command;
|
2012-02-10 11:28:34 +00:00
|
|
|
int curindex, arg_count;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-11-25 23:10:02 +00:00
|
|
|
set_program_name (argv[0]);
|
2010-01-01 20:32:30 +00:00
|
|
|
|
|
|
|
grub_util_init_nls ();
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-09-20 10:39:28 +00:00
|
|
|
/* Parse our arguments */
|
2012-02-10 11:28:34 +00:00
|
|
|
if (argp_parse (&argp, argc, argv, 0, &curindex, 0) != 0)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2010-09-20 16:56:14 +00:00
|
|
|
fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
|
2010-09-20 10:39:28 +00:00
|
|
|
exit(1);
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
2012-02-10 11:28:34 +00:00
|
|
|
arg_count = argc - curindex;
|
2008-07-02 20:51:46 +00:00
|
|
|
|
2010-09-20 10:39:28 +00:00
|
|
|
if (arg_count == 1)
|
2008-07-02 07:20:11 +00:00
|
|
|
{
|
2010-09-20 10:39:28 +00:00
|
|
|
filename = DEFAULT_ENVBLK_PATH;
|
2012-02-10 11:28:34 +00:00
|
|
|
command = argv[curindex++];
|
2009-10-26 16:33:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-10 11:28:34 +00:00
|
|
|
filename = argv[curindex++];
|
2010-09-20 10:39:28 +00:00
|
|
|
if (strcmp (filename, "-") == 0)
|
|
|
|
filename = DEFAULT_ENVBLK_PATH;
|
2012-02-10 11:28:34 +00:00
|
|
|
command = argv[curindex++];
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
2009-03-28 19:58:15 +00:00
|
|
|
|
|
|
|
if (strcmp (command, "create") == 0)
|
|
|
|
create_envblk_file (filename);
|
|
|
|
else if (strcmp (command, "list") == 0)
|
|
|
|
list_variables (filename);
|
|
|
|
else if (strcmp (command, "set") == 0)
|
2012-02-10 11:28:34 +00:00
|
|
|
set_variables (filename, argc - curindex, argv + curindex);
|
2009-03-28 19:58:15 +00:00
|
|
|
else if (strcmp (command, "unset") == 0)
|
2012-02-10 11:28:34 +00:00
|
|
|
unset_variables (filename, argc - curindex, argv + curindex);
|
2008-07-02 07:20:11 +00:00
|
|
|
else
|
|
|
|
{
|
2010-09-20 10:39:28 +00:00
|
|
|
char *program = xstrdup(program_name);
|
|
|
|
fprintf (stderr, _("Unknown command `%s'.\n"), command);
|
|
|
|
argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
|
|
|
|
free(program);
|
|
|
|
exit(1);
|
2008-07-02 07:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|