705 lines
18 KiB
C
705 lines
18 KiB
C
/* VasEBoot-editenv.c - tool to edit environment block. */
|
|
/*
|
|
* VAS_EBOOT -- GRand Unified Bootloader
|
|
* Copyright (C) 2008,2009,2010 Free Software Foundation, Inc.
|
|
*
|
|
* VAS_EBOOT 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.
|
|
*
|
|
* VAS_EBOOT 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 VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <config.h>
|
|
#include <VasEBoot/types.h>
|
|
#include <VasEBoot/emu/misc.h>
|
|
#include <VasEBoot/util/misc.h>
|
|
#include <VasEBoot/lib/envblk.h>
|
|
#include <VasEBoot/i18n.h>
|
|
#include <VasEBoot/emu/hostdisk.h>
|
|
#include <VasEBoot/util/install.h>
|
|
#include <VasEBoot/emu/getroot.h>
|
|
#include <VasEBoot/fs.h>
|
|
#include <VasEBoot/crypto.h>
|
|
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
|
|
#pragma GCC diagnostic ignored "-Wmissing-declarations"
|
|
#include <argp.h>
|
|
#pragma GCC diagnostic error "-Wmissing-prototypes"
|
|
#pragma GCC diagnostic error "-Wmissing-declarations"
|
|
|
|
|
|
#include "progname.h"
|
|
|
|
#define DEFAULT_ENVBLK_PATH DEFAULT_DIRECTORY "/" VAS_EBOOT_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},
|
|
/* TRANSLATORS: "set" is a keyword. It's a summary of "set" subcommand. */
|
|
{N_("set [NAME=VALUE ...]"), 0, 0, OPTION_DOC|OPTION_NO_USAGE,
|
|
N_("Set variables."), 0},
|
|
/* TRANSLATORS: "unset" is a keyword. It's a summary of "unset" subcommand. */
|
|
{N_("unset [NAME ...]"), 0, 0, OPTION_DOC|OPTION_NO_USAGE,
|
|
N_("Delete variables."), 0},
|
|
|
|
{0, 0, 0, OPTION_DOC, N_("Options:"), -1},
|
|
{"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
|
|
|
|
{ 0, 0, 0, 0, 0, 0 }
|
|
};
|
|
|
|
/* Print the version information. */
|
|
static void
|
|
print_version (FILE *stream, struct argp_state *state)
|
|
{
|
|
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">";
|
|
|
|
static error_t argp_parser (int key, char *arg, struct argp_state *state)
|
|
{
|
|
switch (key)
|
|
{
|
|
case 'v':
|
|
verbosity++;
|
|
break;
|
|
|
|
case ARGP_KEY_NO_ARGS:
|
|
fprintf (stderr, "%s",
|
|
_("You need to specify at least one command.\n"));
|
|
argp_usage (state);
|
|
break;
|
|
|
|
default:
|
|
return ARGP_ERR_UNKNOWN;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
|
|
|
|
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_ENVBLK_PATH);
|
|
|
|
default:
|
|
return (char *) text;
|
|
}
|
|
}
|
|
|
|
#pragma GCC diagnostic error "-Wformat-nonliteral"
|
|
|
|
struct argp argp = {
|
|
options, argp_parser, N_("FILENAME COMMAND"),
|
|
"\n"N_("\
|
|
Tool to edit environment block.")
|
|
"\v"N_("\
|
|
If FILENAME is `-', the default value %s is used.\n\n\
|
|
There is no `delete' command; if you want to delete the whole environment\n\
|
|
block, use `rm %s'."),
|
|
NULL, help_filter, NULL
|
|
};
|
|
|
|
struct fs_envblk_spec {
|
|
const char *fs_name;
|
|
off_t offset;
|
|
size_t size;
|
|
};
|
|
typedef struct fs_envblk_spec fs_envblk_spec_t;
|
|
|
|
static VasEBoot_envblk_t fs_envblk_open (VasEBoot_envblk_t envblk);
|
|
static void fs_envblk_write (VasEBoot_envblk_t envblk);
|
|
|
|
struct fs_envblk_ops {
|
|
VasEBoot_envblk_t (*open) (VasEBoot_envblk_t);
|
|
void (*write) (VasEBoot_envblk_t);
|
|
};
|
|
typedef struct fs_envblk_ops fs_envblk_ops_t;
|
|
|
|
struct fs_envblk {
|
|
fs_envblk_spec_t *spec;
|
|
fs_envblk_ops_t *ops;
|
|
const char *dev;
|
|
};
|
|
typedef struct fs_envblk *fs_envblk_t;
|
|
|
|
static fs_envblk_ops_t fs_envblk_ops = {
|
|
.open = fs_envblk_open,
|
|
.write = fs_envblk_write
|
|
};
|
|
|
|
/*
|
|
* fs_envblk_spec describes the file-system specific layout of reserved raw
|
|
* blocks used as environment blocks. At present only Btrfs is supported. Other
|
|
* file-systems may be added if they provide a similar facility and avoid the
|
|
* limitation of writing to COW.
|
|
*
|
|
* Note: If this table is modified, also update
|
|
* VasEBoot-core/fs/btrfs.c::btrfs_head, which defines the layout in the Btrfs
|
|
* header and exports VAS_EBOOT_ENV_BTRFS_OFFSET, so that both stay consistent.
|
|
*/
|
|
static fs_envblk_spec_t fs_envblk_spec[] = {
|
|
{ "btrfs", VAS_EBOOT_ENV_BTRFS_OFFSET, VAS_EBOOT_DISK_SECTOR_SIZE },
|
|
{ NULL, 0, 0 }
|
|
};
|
|
|
|
static fs_envblk_t fs_envblk = NULL;
|
|
|
|
static void
|
|
fs_envblk_init (const char *fs_name, const char *dev)
|
|
{
|
|
fs_envblk_spec_t *p;
|
|
|
|
if (fs_name == NULL || dev == NULL)
|
|
return;
|
|
|
|
for (p = fs_envblk_spec; p->fs_name != NULL; p++)
|
|
{
|
|
if (strcmp (fs_name, p->fs_name) == 0)
|
|
{
|
|
if (fs_envblk == NULL)
|
|
fs_envblk = xmalloc (sizeof (*fs_envblk));
|
|
fs_envblk->spec = p;
|
|
fs_envblk->dev = xstrdup (dev);
|
|
fs_envblk->ops = &fs_envblk_ops;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
static int
|
|
read_env_block_var (const char *varname, const char *value, void *hook_data)
|
|
{
|
|
VasEBoot_envblk_t *p_envblk = (VasEBoot_envblk_t *) hook_data;
|
|
off_t off;
|
|
size_t sz;
|
|
char *p, *buf;
|
|
FILE *fp;
|
|
|
|
if (p_envblk == NULL || fs_envblk == NULL)
|
|
return 1;
|
|
|
|
if (strcmp (varname, "env_block") != 0)
|
|
return 0;
|
|
|
|
off = strtol (value, &p, 10);
|
|
if (*p == '+')
|
|
sz = strtol (p + 1, &p, 10);
|
|
else
|
|
return 0;
|
|
|
|
if (*p != '\0' || sz == 0)
|
|
return 0;
|
|
|
|
off <<= VAS_EBOOT_DISK_SECTOR_BITS;
|
|
sz <<= VAS_EBOOT_DISK_SECTOR_BITS;
|
|
|
|
fp = VasEBoot_util_fopen (fs_envblk->dev, "rb");
|
|
if (fp == NULL)
|
|
VasEBoot_util_error (_("cannot open `%s': %s"), fs_envblk->dev, strerror (errno));
|
|
|
|
if (fseek (fp, off, SEEK_SET) < 0)
|
|
VasEBoot_util_error (_("cannot seek `%s': %s"), fs_envblk->dev, strerror (errno));
|
|
|
|
buf = xmalloc (sz);
|
|
if ((fread (buf, 1, sz, fp)) != sz)
|
|
VasEBoot_util_error (_("cannot read `%s': %s"), fs_envblk->dev, strerror (errno));
|
|
|
|
fclose (fp);
|
|
|
|
*p_envblk = VasEBoot_envblk_open (buf, sz);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static void
|
|
create_env_on_block (void)
|
|
{
|
|
FILE *fp;
|
|
char *buf;
|
|
const char *device;
|
|
off_t offset;
|
|
size_t size;
|
|
|
|
if (fs_envblk == NULL)
|
|
return;
|
|
|
|
device = fs_envblk->dev;
|
|
offset = fs_envblk->spec->offset;
|
|
size = fs_envblk->spec->size;
|
|
|
|
fp = VasEBoot_util_fopen (device, "r+b");
|
|
if (fp == NULL)
|
|
VasEBoot_util_error (_("cannot open `%s': %s"), device, strerror (errno));
|
|
|
|
buf = xmalloc (size);
|
|
memcpy (buf, VAS_EBOOT_ENVBLK_SIGNATURE, sizeof (VAS_EBOOT_ENVBLK_SIGNATURE) - 1);
|
|
memset (buf + sizeof (VAS_EBOOT_ENVBLK_SIGNATURE) - 1, '#', size - sizeof (VAS_EBOOT_ENVBLK_SIGNATURE) + 1);
|
|
|
|
if (fseek (fp, offset, SEEK_SET) < 0)
|
|
VasEBoot_util_error (_("cannot seek `%s': %s"), device, strerror (errno));
|
|
|
|
if (fwrite (buf, 1, size, fp) != size)
|
|
VasEBoot_util_error (_("cannot write to `%s': %s"), device, strerror (errno));
|
|
|
|
VasEBoot_util_file_sync (fp);
|
|
free (buf);
|
|
fclose (fp);
|
|
}
|
|
|
|
static VasEBoot_envblk_t
|
|
fs_envblk_open (VasEBoot_envblk_t envblk)
|
|
{
|
|
VasEBoot_envblk_t envblk_on_block = NULL;
|
|
char *val;
|
|
off_t offset;
|
|
size_t size;
|
|
|
|
if (envblk == NULL)
|
|
return NULL;
|
|
|
|
offset = fs_envblk->spec->offset;
|
|
size = fs_envblk->spec->size;
|
|
|
|
VasEBoot_envblk_iterate (envblk, &envblk_on_block, read_env_block_var);
|
|
|
|
if (envblk_on_block != NULL && VasEBoot_envblk_size (envblk_on_block) == size)
|
|
return envblk_on_block;
|
|
|
|
create_env_on_block ();
|
|
|
|
offset = offset >> VAS_EBOOT_DISK_SECTOR_BITS;
|
|
size = (size + VAS_EBOOT_DISK_SECTOR_SIZE - 1) >> VAS_EBOOT_DISK_SECTOR_BITS;
|
|
|
|
val = xasprintf ("%lld+%zu", (long long) offset, size);
|
|
if (VasEBoot_envblk_set (envblk, "env_block", val) == 0)
|
|
VasEBoot_util_error ("%s", _("environment block too small"));
|
|
VasEBoot_envblk_iterate (envblk, &envblk_on_block, read_env_block_var);
|
|
free (val);
|
|
|
|
return envblk_on_block;
|
|
}
|
|
|
|
static VasEBoot_envblk_t
|
|
open_envblk_file (const char *name)
|
|
{
|
|
FILE *fp;
|
|
char *buf;
|
|
long loc;
|
|
size_t size;
|
|
VasEBoot_envblk_t envblk;
|
|
|
|
fp = VasEBoot_util_fopen (name, "rb");
|
|
if (! fp)
|
|
{
|
|
/* Create the file implicitly. */
|
|
VasEBoot_util_create_envblk_file (name);
|
|
fp = VasEBoot_util_fopen (name, "rb");
|
|
if (! fp)
|
|
VasEBoot_util_error (_("cannot open `%s': %s"), name,
|
|
strerror (errno));
|
|
}
|
|
|
|
if (fseek (fp, 0, SEEK_END) < 0)
|
|
VasEBoot_util_error (_("cannot seek `%s': %s"), name,
|
|
strerror (errno));
|
|
|
|
loc = ftell (fp);
|
|
if (loc < 0)
|
|
VasEBoot_util_error (_("cannot get file location `%s': %s"), name,
|
|
strerror (errno));
|
|
|
|
size = (size_t) loc;
|
|
|
|
if (fseek (fp, 0, SEEK_SET) < 0)
|
|
VasEBoot_util_error (_("cannot seek `%s': %s"), name,
|
|
strerror (errno));
|
|
|
|
buf = xmalloc (size);
|
|
|
|
if (fread (buf, 1, size, fp) != size)
|
|
VasEBoot_util_error (_("cannot read `%s': %s"), name,
|
|
strerror (errno));
|
|
|
|
fclose (fp);
|
|
|
|
envblk = VasEBoot_envblk_open (buf, size);
|
|
if (! envblk)
|
|
VasEBoot_util_error ("%s", _("invalid environment block"));
|
|
|
|
return envblk;
|
|
}
|
|
|
|
static int
|
|
print_var (const char *varname, const char *value,
|
|
void *hook_data __attribute__ ((unused)))
|
|
{
|
|
printf ("%s=%s\n", varname, value);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
list_variables (const char *name)
|
|
{
|
|
VasEBoot_envblk_t envblk;
|
|
VasEBoot_envblk_t envblk_on_block = NULL;
|
|
|
|
envblk = open_envblk_file (name);
|
|
VasEBoot_envblk_iterate (envblk, &envblk_on_block, read_env_block_var);
|
|
VasEBoot_envblk_iterate (envblk, NULL, print_var);
|
|
VasEBoot_envblk_close (envblk);
|
|
if (envblk_on_block != NULL)
|
|
{
|
|
VasEBoot_envblk_iterate (envblk_on_block, NULL, print_var);
|
|
VasEBoot_envblk_close (envblk_on_block);
|
|
}
|
|
}
|
|
|
|
static void
|
|
write_envblk (const char *name, VasEBoot_envblk_t envblk)
|
|
{
|
|
FILE *fp;
|
|
|
|
fp = VasEBoot_util_fopen (name, "wb");
|
|
if (! fp)
|
|
VasEBoot_util_error (_("cannot open `%s': %s"), name,
|
|
strerror (errno));
|
|
|
|
if (fwrite (VasEBoot_envblk_buffer (envblk), 1, VasEBoot_envblk_size (envblk), fp)
|
|
!= VasEBoot_envblk_size (envblk))
|
|
VasEBoot_util_error (_("cannot write to `%s': %s"), name,
|
|
strerror (errno));
|
|
|
|
if (VasEBoot_util_file_sync (fp) < 0)
|
|
VasEBoot_util_error (_("cannot sync `%s': %s"), name, strerror (errno));
|
|
fclose (fp);
|
|
}
|
|
|
|
static void
|
|
fs_envblk_write (VasEBoot_envblk_t envblk)
|
|
{
|
|
FILE *fp;
|
|
const char *device;
|
|
off_t offset;
|
|
size_t size;
|
|
|
|
if (envblk == NULL)
|
|
return;
|
|
|
|
device = fs_envblk->dev;
|
|
offset = fs_envblk->spec->offset;
|
|
size = fs_envblk->spec->size;
|
|
|
|
if (VasEBoot_envblk_size (envblk) > size)
|
|
VasEBoot_util_error ("%s", _("environment block too small"));
|
|
|
|
fp = VasEBoot_util_fopen (device, "r+b");
|
|
|
|
if (fp == NULL)
|
|
VasEBoot_util_error (_("cannot open `%s': %s"), device, strerror (errno));
|
|
|
|
if (fseek (fp, offset, SEEK_SET) < 0)
|
|
VasEBoot_util_error (_("cannot seek `%s': %s"), device, strerror (errno));
|
|
|
|
if (fwrite (VasEBoot_envblk_buffer (envblk), 1, VasEBoot_envblk_size (envblk), fp) != VasEBoot_envblk_size (envblk))
|
|
VasEBoot_util_error (_("cannot write to `%s': %s"), device, strerror (errno));
|
|
|
|
VasEBoot_util_file_sync (fp);
|
|
fclose (fp);
|
|
}
|
|
|
|
struct var_lookup_ctx {
|
|
const char *varname;
|
|
bool found;
|
|
};
|
|
typedef struct var_lookup_ctx var_lookup_ctx_t;
|
|
|
|
static int
|
|
var_lookup_iter (const char *varname, const char *value __attribute__ ((unused)), void *hook_data)
|
|
{
|
|
var_lookup_ctx_t *ctx = (var_lookup_ctx_t *) hook_data;
|
|
|
|
if (VasEBoot_strcmp (ctx->varname, varname) == 0)
|
|
{
|
|
ctx->found = true;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
set_variables (const char *name, int argc, char *argv[])
|
|
{
|
|
VasEBoot_envblk_t envblk;
|
|
VasEBoot_envblk_t envblk_on_block = NULL;
|
|
|
|
envblk = open_envblk_file (name);
|
|
if (fs_envblk != NULL)
|
|
envblk_on_block = fs_envblk->ops->open (envblk);
|
|
|
|
while (argc)
|
|
{
|
|
char *p;
|
|
|
|
p = strchr (argv[0], '=');
|
|
if (! p)
|
|
VasEBoot_util_error (_("invalid parameter %s"), argv[0]);
|
|
|
|
*(p++) = 0;
|
|
|
|
if ((strcmp (argv[0], "next_entry") == 0) && envblk_on_block != NULL)
|
|
{
|
|
if (VasEBoot_envblk_set (envblk_on_block, argv[0], p) == 0)
|
|
VasEBoot_util_error ("%s", _("environment block too small"));
|
|
goto next;
|
|
}
|
|
|
|
if (strcmp (argv[0], "env_block") == 0)
|
|
{
|
|
VasEBoot_util_warn (_("can't set env_block as it's read-only"));
|
|
goto next;
|
|
}
|
|
|
|
if (VasEBoot_envblk_set (envblk, argv[0], p) == 0)
|
|
VasEBoot_util_error ("%s", _("environment block too small"));
|
|
|
|
if (envblk_on_block != NULL)
|
|
{
|
|
var_lookup_ctx_t ctx = {
|
|
.varname = argv[0],
|
|
.found = false
|
|
};
|
|
|
|
VasEBoot_envblk_iterate (envblk_on_block, &ctx, var_lookup_iter);
|
|
if (ctx.found == true)
|
|
VasEBoot_envblk_delete (envblk_on_block, argv[0]);
|
|
}
|
|
next:
|
|
argc--;
|
|
argv++;
|
|
}
|
|
|
|
write_envblk (name, envblk);
|
|
VasEBoot_envblk_close (envblk);
|
|
|
|
if (envblk_on_block != NULL)
|
|
{
|
|
fs_envblk->ops->write (envblk_on_block);
|
|
VasEBoot_envblk_close (envblk_on_block);
|
|
}
|
|
}
|
|
|
|
static void
|
|
unset_variables (const char *name, int argc, char *argv[])
|
|
{
|
|
VasEBoot_envblk_t envblk;
|
|
VasEBoot_envblk_t envblk_on_block = NULL;
|
|
|
|
envblk = open_envblk_file (name);
|
|
|
|
if (fs_envblk != NULL)
|
|
envblk_on_block = fs_envblk->ops->open (envblk);
|
|
|
|
while (argc)
|
|
{
|
|
VasEBoot_envblk_delete (envblk, argv[0]);
|
|
|
|
if (envblk_on_block != NULL)
|
|
VasEBoot_envblk_delete (envblk_on_block, argv[0]);
|
|
|
|
argc--;
|
|
argv++;
|
|
}
|
|
|
|
write_envblk (name, envblk);
|
|
VasEBoot_envblk_close (envblk);
|
|
|
|
if (envblk_on_block != NULL)
|
|
{
|
|
fs_envblk->ops->write (envblk_on_block);
|
|
VasEBoot_envblk_close (envblk_on_block);
|
|
}
|
|
}
|
|
|
|
static bool
|
|
is_abstraction (VasEBoot_device_t dev)
|
|
{
|
|
if (dev == NULL || dev->disk == NULL)
|
|
return false;
|
|
|
|
if (dev->disk->dev->id == VAS_EBOOT_DISK_DEVICE_DISKFILTER_ID ||
|
|
dev->disk->dev->id == VAS_EBOOT_DISK_DEVICE_CRYPTODISK_ID)
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
static void
|
|
probe_fs_envblk (fs_envblk_spec_t *spec)
|
|
{
|
|
char **VasEBoot_devices = NULL;
|
|
char **curdev, **curdrive;
|
|
size_t ndev = 0;
|
|
char **VasEBoot_drives = NULL;
|
|
VasEBoot_device_t VasEBoot_dev = NULL;
|
|
VasEBoot_fs_t VasEBoot_fs = NULL;
|
|
bool have_abstraction = false;
|
|
|
|
VasEBoot_util_biosdisk_init (DEFAULT_DEVICE_MAP);
|
|
VasEBoot_init_all ();
|
|
VasEBoot_gcry_init_all ();
|
|
|
|
VasEBoot_lvm_fini ();
|
|
VasEBoot_mdraid09_fini ();
|
|
VasEBoot_mdraid1x_fini ();
|
|
VasEBoot_diskfilter_fini ();
|
|
VasEBoot_diskfilter_init ();
|
|
VasEBoot_mdraid09_init ();
|
|
VasEBoot_mdraid1x_init ();
|
|
VasEBoot_lvm_init ();
|
|
|
|
VasEBoot_devices = VasEBoot_guess_root_devices (DEFAULT_DIRECTORY);
|
|
|
|
if (VasEBoot_devices == NULL || VasEBoot_devices[0] == NULL)
|
|
{
|
|
VasEBoot_util_warn (_("cannot find a device for %s (is /dev mounted?)"), DEFAULT_DIRECTORY);
|
|
goto cleanup;
|
|
}
|
|
|
|
for (curdev = VasEBoot_devices; *curdev != NULL; curdev++, ndev++)
|
|
VasEBoot_util_pull_device (*curdev);
|
|
|
|
VasEBoot_drives = xcalloc ((ndev + 1), sizeof (VasEBoot_drives[0]));
|
|
|
|
for (curdev = VasEBoot_devices, curdrive = VasEBoot_drives; *curdev != NULL; curdev++,
|
|
curdrive++)
|
|
{
|
|
*curdrive = VasEBoot_util_get_VasEBoot_dev (*curdev);
|
|
if (*curdrive == NULL)
|
|
{
|
|
VasEBoot_util_warn (_("cannot find a VAS_EBOOT drive for %s. Check your device.map"),
|
|
*curdev);
|
|
goto cleanup;
|
|
}
|
|
}
|
|
*curdrive = NULL;
|
|
|
|
VasEBoot_dev = VasEBoot_device_open (VasEBoot_drives[0]);
|
|
if (VasEBoot_dev == NULL)
|
|
{
|
|
VasEBoot_util_warn (_("cannot open device %s: %s"), VasEBoot_drives[0], VasEBoot_errmsg);
|
|
VasEBoot_errno = VAS_EBOOT_ERR_NONE;
|
|
goto cleanup;
|
|
}
|
|
|
|
VasEBoot_fs = VasEBoot_fs_probe (VasEBoot_dev);
|
|
if (VasEBoot_fs == NULL)
|
|
{
|
|
VasEBoot_util_warn (_("cannot probe fs for %s: %s"), VasEBoot_drives[0], VasEBoot_errmsg);
|
|
VasEBoot_errno = VAS_EBOOT_ERR_NONE;
|
|
goto cleanup;
|
|
}
|
|
|
|
have_abstraction = is_abstraction (VasEBoot_dev);
|
|
for (curdrive = VasEBoot_drives + 1; *curdrive != NULL && have_abstraction == false; curdrive++)
|
|
{
|
|
VasEBoot_device_t dev = VasEBoot_device_open (*curdrive);
|
|
|
|
if (dev == NULL)
|
|
continue;
|
|
have_abstraction = is_abstraction (dev);
|
|
VasEBoot_device_close (dev);
|
|
}
|
|
|
|
if (have_abstraction == false)
|
|
fs_envblk_init (VasEBoot_fs->name, VasEBoot_devices[0]);
|
|
|
|
cleanup:
|
|
if (VasEBoot_devices != NULL)
|
|
for (curdev = VasEBoot_devices; *curdev != NULL; curdev++)
|
|
free (*curdev);
|
|
free (VasEBoot_devices);
|
|
free (VasEBoot_drives);
|
|
VasEBoot_device_close (VasEBoot_dev);
|
|
VasEBoot_gcry_fini_all ();
|
|
VasEBoot_fini_all ();
|
|
VasEBoot_util_biosdisk_fini ();
|
|
}
|
|
|
|
int
|
|
main (int argc, char *argv[])
|
|
{
|
|
const char *filename;
|
|
char *command;
|
|
int curindex, arg_count;
|
|
|
|
VasEBoot_util_host_init (&argc, &argv);
|
|
|
|
/* Parse our arguments */
|
|
if (argp_parse (&argp, argc, argv, 0, &curindex, 0) != 0)
|
|
{
|
|
fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
|
|
exit(1);
|
|
}
|
|
|
|
arg_count = argc - curindex;
|
|
|
|
if (arg_count == 1)
|
|
{
|
|
filename = DEFAULT_ENVBLK_PATH;
|
|
command = argv[curindex++];
|
|
}
|
|
else
|
|
{
|
|
filename = argv[curindex++];
|
|
if (strcmp (filename, "-") == 0)
|
|
filename = DEFAULT_ENVBLK_PATH;
|
|
command = argv[curindex++];
|
|
}
|
|
|
|
if (strcmp (filename, DEFAULT_ENVBLK_PATH) == 0)
|
|
probe_fs_envblk (fs_envblk_spec);
|
|
|
|
if (strcmp (command, "create") == 0)
|
|
VasEBoot_util_create_envblk_file (filename);
|
|
else if (strcmp (command, "list") == 0)
|
|
list_variables (filename);
|
|
else if (strcmp (command, "set") == 0)
|
|
set_variables (filename, argc - curindex, argv + curindex);
|
|
else if (strcmp (command, "unset") == 0)
|
|
unset_variables (filename, argc - curindex, argv + curindex);
|
|
else
|
|
{
|
|
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);
|
|
}
|
|
|
|
return 0;
|
|
}
|