2009-12-20 01:52:39 +00:00
|
|
|
/* env.c - Environment variables */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2003,2005,2006,2007,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
|
|
|
|
* 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 <grub/env.h>
|
|
|
|
#include <grub/env_private.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/command.h>
|
2009-12-29 16:53:10 +00:00
|
|
|
#include <grub/normal.h>
|
2010-05-01 18:28:07 +00:00
|
|
|
#include <grub/i18n.h>
|
2009-12-20 01:52:39 +00:00
|
|
|
|
|
|
|
struct menu_pointer
|
|
|
|
{
|
|
|
|
grub_menu_t menu;
|
|
|
|
struct menu_pointer *prev;
|
|
|
|
};
|
|
|
|
|
2011-03-23 11:05:13 +00:00
|
|
|
static struct menu_pointer initial_menu;
|
|
|
|
static struct menu_pointer *current_menu = &initial_menu;
|
2009-12-20 01:52:39 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
grub_env_unset_menu (void)
|
|
|
|
{
|
|
|
|
current_menu->menu = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_menu_t
|
|
|
|
grub_env_get_menu (void)
|
|
|
|
{
|
|
|
|
return current_menu->menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
grub_env_set_menu (grub_menu_t nmenu)
|
|
|
|
{
|
|
|
|
current_menu->menu = nmenu;
|
|
|
|
}
|
|
|
|
|
2010-09-16 21:48:32 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_env_new_context (int export_all)
|
2009-12-20 01:52:39 +00:00
|
|
|
{
|
|
|
|
struct grub_env_context *context;
|
|
|
|
int i;
|
|
|
|
struct menu_pointer *menu;
|
|
|
|
|
|
|
|
context = grub_zalloc (sizeof (*context));
|
|
|
|
if (! context)
|
|
|
|
return grub_errno;
|
|
|
|
menu = grub_zalloc (sizeof (*menu));
|
|
|
|
if (! menu)
|
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
context->prev = grub_current_context;
|
|
|
|
grub_current_context = context;
|
|
|
|
|
|
|
|
menu->prev = current_menu;
|
|
|
|
current_menu = menu;
|
|
|
|
|
|
|
|
/* Copy exported variables. */
|
|
|
|
for (i = 0; i < HASHSZ; i++)
|
|
|
|
{
|
|
|
|
struct grub_env_var *var;
|
|
|
|
|
|
|
|
for (var = context->prev->vars[i]; var; var = var->next)
|
2010-09-16 21:48:32 +00:00
|
|
|
if (var->global || export_all)
|
|
|
|
{
|
|
|
|
if (grub_env_set (var->name, var->value) != GRUB_ERR_NONE)
|
|
|
|
{
|
|
|
|
grub_env_context_close ();
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
grub_env_export (var->name);
|
|
|
|
grub_register_variable_hook (var->name, var->read_hook, var->write_hook);
|
|
|
|
}
|
2009-12-20 01:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-09-16 21:48:32 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_env_context_open (void)
|
|
|
|
{
|
|
|
|
return grub_env_new_context (0);
|
|
|
|
}
|
|
|
|
|
2010-09-20 21:01:34 +00:00
|
|
|
int grub_extractor_level = 0;
|
2010-09-16 21:48:32 +00:00
|
|
|
|
|
|
|
grub_err_t
|
2010-09-20 21:01:34 +00:00
|
|
|
grub_env_extractor_open (int source)
|
2010-09-16 21:48:32 +00:00
|
|
|
{
|
2010-09-20 21:01:34 +00:00
|
|
|
grub_extractor_level++;
|
2010-09-16 21:48:32 +00:00
|
|
|
return grub_env_new_context (source);
|
|
|
|
}
|
|
|
|
|
2009-12-20 01:52:39 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_env_context_close (void)
|
|
|
|
{
|
|
|
|
struct grub_env_context *context;
|
|
|
|
int i;
|
|
|
|
struct menu_pointer *menu;
|
|
|
|
|
|
|
|
if (! grub_current_context->prev)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
|
|
"cannot close the initial context");
|
|
|
|
|
|
|
|
/* Free the variables associated with this context. */
|
|
|
|
for (i = 0; i < HASHSZ; i++)
|
|
|
|
{
|
|
|
|
struct grub_env_var *p, *q;
|
|
|
|
|
|
|
|
for (p = grub_current_context->vars[i]; p; p = q)
|
|
|
|
{
|
|
|
|
q = p->next;
|
|
|
|
grub_free (p->name);
|
|
|
|
grub_free (p->value);
|
|
|
|
grub_free (p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore the previous context. */
|
|
|
|
context = grub_current_context->prev;
|
|
|
|
grub_free (grub_current_context);
|
|
|
|
grub_current_context = context;
|
|
|
|
|
|
|
|
menu = current_menu->prev;
|
|
|
|
grub_free (current_menu);
|
|
|
|
current_menu = menu;
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-09-16 21:48:32 +00:00
|
|
|
grub_err_t
|
2010-09-20 21:01:34 +00:00
|
|
|
grub_env_extractor_close (int source)
|
2010-09-16 21:48:32 +00:00
|
|
|
{
|
2011-01-12 10:52:51 +00:00
|
|
|
grub_menu_t menu = NULL;
|
2010-09-16 21:48:32 +00:00
|
|
|
grub_menu_entry_t *last;
|
|
|
|
grub_err_t err;
|
|
|
|
|
|
|
|
if (source)
|
|
|
|
{
|
|
|
|
menu = grub_env_get_menu ();
|
|
|
|
grub_env_unset_menu ();
|
|
|
|
}
|
|
|
|
err = grub_env_context_close ();
|
|
|
|
|
2012-02-01 14:35:30 +00:00
|
|
|
if (source && menu)
|
2010-09-16 21:48:32 +00:00
|
|
|
{
|
2011-01-12 10:52:51 +00:00
|
|
|
grub_menu_t menu2;
|
2010-09-16 21:48:32 +00:00
|
|
|
menu2 = grub_env_get_menu ();
|
|
|
|
|
|
|
|
last = &menu2->entry_list;
|
|
|
|
while (*last)
|
|
|
|
last = &(*last)->next;
|
|
|
|
|
|
|
|
*last = menu->entry_list;
|
|
|
|
menu2->size += menu->size;
|
|
|
|
}
|
|
|
|
|
2010-09-20 21:01:34 +00:00
|
|
|
grub_extractor_level--;
|
2010-09-16 21:48:32 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-12-20 01:52:39 +00:00
|
|
|
static grub_command_t export_cmd;
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_export (struct grub_command *cmd __attribute__ ((unused)),
|
|
|
|
int argc, char **args)
|
|
|
|
{
|
2010-08-16 09:34:30 +00:00
|
|
|
int i;
|
|
|
|
|
2009-12-20 01:52:39 +00:00
|
|
|
if (argc < 1)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
|
|
"no environment variable specified");
|
|
|
|
|
2010-08-16 09:34:30 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
grub_env_export (args[i]);
|
|
|
|
|
2009-12-20 01:52:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
grub_context_init (void)
|
|
|
|
{
|
|
|
|
export_cmd = grub_register_command ("export", grub_cmd_export,
|
2010-08-25 13:09:00 +00:00
|
|
|
N_("ENVVAR [ENVVAR] ..."),
|
2010-08-16 09:34:30 +00:00
|
|
|
N_("Export variables."));
|
2009-12-20 01:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
grub_context_fini (void)
|
|
|
|
{
|
|
|
|
grub_unregister_command (export_cmd);
|
|
|
|
}
|