2010-06-10 15:20:49 +00:00
|
|
|
/* menuentry.c - menuentry command */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2010 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/types.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/extcmd.h>
|
|
|
|
#include <grub/i18n.h>
|
|
|
|
#include <grub/normal.h>
|
|
|
|
|
2010-07-22 13:31:40 +00:00
|
|
|
static const struct grub_arg_option options[] =
|
|
|
|
{
|
|
|
|
{"class", 1, GRUB_ARG_OPTION_REPEATABLE,
|
2011-11-11 23:34:14 +00:00
|
|
|
N_("Menu entry type."), N_("STRING"), ARG_TYPE_STRING},
|
2010-07-22 13:31:40 +00:00
|
|
|
{"users", 2, 0,
|
2012-02-08 18:26:01 +00:00
|
|
|
N_("List of users allowed to boot this entry."), N_("USERNAME[,USERNAME]"),
|
|
|
|
ARG_TYPE_STRING},
|
2010-07-22 13:31:40 +00:00
|
|
|
{"hotkey", 3, 0,
|
2012-02-05 10:23:47 +00:00
|
|
|
N_("Keyboard key to quickly boot this entry."), N_("KEYBOARD_KEY"), ARG_TYPE_STRING},
|
2010-09-04 02:44:50 +00:00
|
|
|
{"source", 4, 0,
|
2012-02-08 18:26:01 +00:00
|
|
|
N_("Use STRING as menu entry body."), N_("STRING"), ARG_TYPE_STRING},
|
2012-03-05 00:00:09 +00:00
|
|
|
{"id", 0, 0, N_("Menu entry identifier."), N_("STRING"), ARG_TYPE_STRING},
|
2012-05-03 21:43:59 +00:00
|
|
|
/* TRANSLATORS: menu entry can either be bootable by anyone or only by
|
|
|
|
handful of users. By default when security is active only superusers can
|
2012-05-03 23:20:38 +00:00
|
|
|
boot a given menu entry. With --unrestricted (this option)
|
2012-05-03 21:43:59 +00:00
|
|
|
anyone can boot it. */
|
2012-05-02 08:26:09 +00:00
|
|
|
{"unrestricted", 0, 0, N_("This entry can be booted by any user."),
|
|
|
|
0, ARG_TYPE_NONE},
|
2010-07-22 13:31:40 +00:00
|
|
|
{0, 0, 0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
2011-11-30 15:20:13 +00:00
|
|
|
const char *name;
|
2010-07-22 13:31:40 +00:00
|
|
|
int key;
|
|
|
|
} hotkey_aliases[] =
|
|
|
|
{
|
2017-08-07 15:20:30 +00:00
|
|
|
{"backspace", GRUB_TERM_BACKSPACE},
|
|
|
|
{"tab", GRUB_TERM_TAB},
|
2010-09-18 23:01:35 +00:00
|
|
|
{"delete", GRUB_TERM_KEY_DC},
|
|
|
|
{"insert", GRUB_TERM_KEY_INSERT},
|
|
|
|
{"f1", GRUB_TERM_KEY_F1},
|
|
|
|
{"f2", GRUB_TERM_KEY_F2},
|
|
|
|
{"f3", GRUB_TERM_KEY_F3},
|
|
|
|
{"f4", GRUB_TERM_KEY_F4},
|
|
|
|
{"f5", GRUB_TERM_KEY_F5},
|
|
|
|
{"f6", GRUB_TERM_KEY_F6},
|
|
|
|
{"f7", GRUB_TERM_KEY_F7},
|
|
|
|
{"f8", GRUB_TERM_KEY_F8},
|
|
|
|
{"f9", GRUB_TERM_KEY_F9},
|
|
|
|
{"f10", GRUB_TERM_KEY_F10},
|
|
|
|
{"f11", GRUB_TERM_KEY_F11},
|
|
|
|
{"f12", GRUB_TERM_KEY_F12},
|
2010-07-22 13:31:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Add a menu entry to the current menu context (as given by the environment
|
|
|
|
variable data slot `menu'). As the configuration file is read, the script
|
|
|
|
parser calls this when a menu entry is to be created. */
|
2010-09-11 20:39:55 +00:00
|
|
|
grub_err_t
|
2012-03-04 13:55:13 +00:00
|
|
|
grub_normal_add_menu_entry (int argc, const char **args,
|
|
|
|
char **classes, const char *id,
|
2010-09-11 20:39:55 +00:00
|
|
|
const char *users, const char *hotkey,
|
2010-09-20 22:47:49 +00:00
|
|
|
const char *prefix, const char *sourcecode,
|
|
|
|
int submenu)
|
2010-07-22 13:31:40 +00:00
|
|
|
{
|
|
|
|
int menu_hotkey = 0;
|
2010-07-22 15:45:14 +00:00
|
|
|
char **menu_args = NULL;
|
2010-07-22 13:31:40 +00:00
|
|
|
char *menu_users = NULL;
|
|
|
|
char *menu_title = NULL;
|
|
|
|
char *menu_sourcecode = NULL;
|
2012-03-04 13:55:13 +00:00
|
|
|
char *menu_id = NULL;
|
2010-07-22 13:31:40 +00:00
|
|
|
struct grub_menu_entry_class *menu_classes = NULL;
|
|
|
|
|
|
|
|
grub_menu_t menu;
|
|
|
|
grub_menu_entry_t *last;
|
|
|
|
|
|
|
|
menu = grub_env_get_menu ();
|
|
|
|
if (! menu)
|
|
|
|
return grub_error (GRUB_ERR_MENU, "no menu context");
|
|
|
|
|
|
|
|
last = &menu->entry_list;
|
|
|
|
|
2010-09-03 16:58:16 +00:00
|
|
|
menu_sourcecode = grub_xasprintf ("%s%s", prefix ?: "", sourcecode);
|
2010-07-22 13:31:40 +00:00
|
|
|
if (! menu_sourcecode)
|
|
|
|
return grub_errno;
|
|
|
|
|
2011-05-14 10:14:00 +00:00
|
|
|
if (classes && classes[0])
|
2010-07-22 13:31:40 +00:00
|
|
|
{
|
2010-09-20 22:47:49 +00:00
|
|
|
int i;
|
2010-07-22 13:31:40 +00:00
|
|
|
for (i = 0; classes[i]; i++); /* count # of menuentry classes */
|
2012-03-19 09:56:43 +00:00
|
|
|
menu_classes = grub_zalloc (sizeof (struct grub_menu_entry_class)
|
|
|
|
* (i + 1));
|
2010-07-22 13:31:40 +00:00
|
|
|
if (! menu_classes)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
for (i = 0; classes[i]; i++)
|
|
|
|
{
|
|
|
|
menu_classes[i].name = grub_strdup (classes[i]);
|
|
|
|
if (! menu_classes[i].name)
|
|
|
|
goto fail;
|
|
|
|
menu_classes[i].next = classes[i + 1] ? &menu_classes[i + 1] : NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (users)
|
|
|
|
{
|
|
|
|
menu_users = grub_strdup (users);
|
|
|
|
if (! menu_users)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hotkey)
|
|
|
|
{
|
2010-09-20 22:47:49 +00:00
|
|
|
unsigned i;
|
2010-07-22 13:31:40 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE (hotkey_aliases); i++)
|
|
|
|
if (grub_strcmp (hotkey, hotkey_aliases[i].name) == 0)
|
|
|
|
{
|
|
|
|
menu_hotkey = hotkey_aliases[i].key;
|
|
|
|
break;
|
|
|
|
}
|
2010-09-03 15:23:38 +00:00
|
|
|
if (i == ARRAY_SIZE (hotkey_aliases))
|
|
|
|
menu_hotkey = hotkey[0];
|
2010-07-22 13:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! argc)
|
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_MENU, "menuentry is missing title");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
menu_title = grub_strdup (args[0]);
|
|
|
|
if (! menu_title)
|
|
|
|
goto fail;
|
|
|
|
|
2012-03-04 13:55:13 +00:00
|
|
|
menu_id = grub_strdup (id ? : menu_title);
|
|
|
|
if (! menu_id)
|
|
|
|
goto fail;
|
|
|
|
|
2010-07-22 15:45:14 +00:00
|
|
|
/* Save argc, args to pass as parameters to block arg later. */
|
2020-06-15 16:26:01 +00:00
|
|
|
menu_args = grub_calloc (argc + 1, sizeof (char *));
|
2010-07-22 15:45:14 +00:00
|
|
|
if (! menu_args)
|
|
|
|
goto fail;
|
|
|
|
|
2010-09-20 22:47:49 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
menu_args[i] = grub_strdup (args[i]);
|
|
|
|
if (! menu_args[i])
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
menu_args[argc] = NULL;
|
|
|
|
}
|
2010-07-22 13:31:40 +00:00
|
|
|
|
|
|
|
/* Add the menu entry at the end of the list. */
|
|
|
|
while (*last)
|
|
|
|
last = &(*last)->next;
|
|
|
|
|
|
|
|
*last = grub_zalloc (sizeof (**last));
|
|
|
|
if (! *last)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
(*last)->title = menu_title;
|
2012-03-04 13:55:13 +00:00
|
|
|
(*last)->id = menu_id;
|
2010-07-22 13:31:40 +00:00
|
|
|
(*last)->hotkey = menu_hotkey;
|
|
|
|
(*last)->classes = menu_classes;
|
|
|
|
if (menu_users)
|
|
|
|
(*last)->restricted = 1;
|
|
|
|
(*last)->users = menu_users;
|
2010-07-22 15:45:14 +00:00
|
|
|
(*last)->argc = argc;
|
|
|
|
(*last)->args = menu_args;
|
2010-07-22 13:31:40 +00:00
|
|
|
(*last)->sourcecode = menu_sourcecode;
|
2010-09-20 22:47:49 +00:00
|
|
|
(*last)->submenu = submenu;
|
2010-07-22 13:31:40 +00:00
|
|
|
|
|
|
|
menu->size++;
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
grub_free (menu_sourcecode);
|
2010-09-20 22:47:49 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; menu_classes && menu_classes[i].name; i++)
|
|
|
|
grub_free (menu_classes[i].name);
|
|
|
|
grub_free (menu_classes);
|
|
|
|
}
|
2010-07-22 15:45:14 +00:00
|
|
|
|
2010-09-20 22:47:49 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; menu_args && menu_args[i]; i++)
|
|
|
|
grub_free (menu_args[i]);
|
|
|
|
grub_free (menu_args);
|
|
|
|
}
|
2010-07-22 15:45:14 +00:00
|
|
|
|
2010-07-22 13:31:40 +00:00
|
|
|
grub_free (menu_users);
|
|
|
|
grub_free (menu_title);
|
2012-03-04 13:55:13 +00:00
|
|
|
grub_free (menu_id);
|
2010-07-22 13:31:40 +00:00
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
2010-09-03 16:58:16 +00:00
|
|
|
static char *
|
|
|
|
setparams_prefix (int argc, char **args)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
char *p;
|
|
|
|
char *result;
|
|
|
|
grub_size_t len = 10;
|
|
|
|
|
|
|
|
/* Count resulting string length */
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
len += 3; /* 3 = 1 space + 2 quotes */
|
|
|
|
p = args[i];
|
|
|
|
while (*p)
|
2010-11-19 04:45:25 +00:00
|
|
|
len += (*p++ == '\'' ? 3 : 1);
|
2010-09-03 16:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result = grub_malloc (len + 2);
|
|
|
|
if (! result)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
grub_strcpy (result, "setparams");
|
2010-11-19 13:38:44 +00:00
|
|
|
p = result + 9;
|
2010-09-03 16:58:16 +00:00
|
|
|
|
|
|
|
for (j = 0; j < argc; j++)
|
|
|
|
{
|
2010-11-19 13:38:44 +00:00
|
|
|
*p++ = ' ';
|
|
|
|
*p++ = '\'';
|
2010-11-25 13:26:20 +00:00
|
|
|
p = grub_strchrsub (p, args[j], '\'', "'\\''");
|
2010-11-19 13:38:44 +00:00
|
|
|
*p++ = '\'';
|
2010-09-03 16:58:16 +00:00
|
|
|
}
|
2010-11-19 13:38:44 +00:00
|
|
|
*p++ = '\n';
|
|
|
|
*p = '\0';
|
2010-09-03 16:58:16 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-06-10 15:20:49 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_menuentry (grub_extcmd_context_t ctxt, int argc, char **args)
|
|
|
|
{
|
2010-07-22 13:31:40 +00:00
|
|
|
char ch;
|
2010-06-10 15:20:49 +00:00
|
|
|
char *src;
|
2010-09-03 16:58:16 +00:00
|
|
|
char *prefix;
|
2010-07-19 21:03:57 +00:00
|
|
|
unsigned len;
|
2010-06-10 15:20:49 +00:00
|
|
|
grub_err_t r;
|
2012-05-02 08:26:09 +00:00
|
|
|
const char *users;
|
2010-06-10 15:20:49 +00:00
|
|
|
|
2010-09-04 02:44:50 +00:00
|
|
|
if (! argc)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "missing arguments");
|
|
|
|
|
|
|
|
if (ctxt->state[3].set && ctxt->script)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "multiple menuentry definitions");
|
|
|
|
|
|
|
|
if (! ctxt->state[3].set && ! ctxt->script)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no menuentry definition");
|
|
|
|
|
2012-05-02 08:26:09 +00:00
|
|
|
if (ctxt->state[1].set)
|
|
|
|
users = ctxt->state[1].arg;
|
|
|
|
else if (ctxt->state[5].set)
|
|
|
|
users = NULL;
|
|
|
|
else
|
|
|
|
users = "";
|
|
|
|
|
2010-09-04 02:44:50 +00:00
|
|
|
if (! ctxt->script)
|
2010-09-11 20:39:55 +00:00
|
|
|
return grub_normal_add_menu_entry (argc, (const char **) args,
|
2011-05-14 10:14:00 +00:00
|
|
|
(ctxt->state[0].set ? ctxt->state[0].args
|
2012-03-04 13:55:13 +00:00
|
|
|
: NULL),
|
|
|
|
ctxt->state[4].arg,
|
2012-05-02 08:26:09 +00:00
|
|
|
users,
|
2010-09-11 20:39:55 +00:00
|
|
|
ctxt->state[2].arg, 0,
|
2010-09-20 22:47:49 +00:00
|
|
|
ctxt->state[3].arg,
|
|
|
|
ctxt->extcmd->cmd->name[0] == 's');
|
2010-06-10 15:20:49 +00:00
|
|
|
|
|
|
|
src = args[argc - 1];
|
2010-07-22 13:31:40 +00:00
|
|
|
args[argc - 1] = NULL;
|
|
|
|
|
2010-07-19 21:03:57 +00:00
|
|
|
len = grub_strlen(src);
|
2010-07-22 13:31:40 +00:00
|
|
|
ch = src[len - 1];
|
2010-07-19 21:03:57 +00:00
|
|
|
src[len - 1] = '\0';
|
2010-06-10 15:20:49 +00:00
|
|
|
|
2010-09-03 16:58:16 +00:00
|
|
|
prefix = setparams_prefix (argc - 1, args);
|
|
|
|
if (! prefix)
|
|
|
|
return grub_errno;
|
|
|
|
|
2010-09-11 20:39:55 +00:00
|
|
|
r = grub_normal_add_menu_entry (argc - 1, (const char **) args,
|
2012-03-04 13:55:13 +00:00
|
|
|
ctxt->state[0].args, ctxt->state[4].arg,
|
2012-05-02 08:26:09 +00:00
|
|
|
users,
|
2010-09-20 22:47:49 +00:00
|
|
|
ctxt->state[2].arg, prefix, src + 1,
|
|
|
|
ctxt->extcmd->cmd->name[0] == 's');
|
2010-06-10 15:20:49 +00:00
|
|
|
|
2010-07-22 13:31:40 +00:00
|
|
|
src[len - 1] = ch;
|
2010-06-10 15:20:49 +00:00
|
|
|
args[argc - 1] = src;
|
2010-09-03 16:58:16 +00:00
|
|
|
grub_free (prefix);
|
2010-06-10 15:20:49 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2010-09-20 22:47:49 +00:00
|
|
|
static grub_extcmd_t cmd, cmd_sub;
|
2010-06-10 15:20:49 +00:00
|
|
|
|
2010-09-03 15:38:12 +00:00
|
|
|
void
|
|
|
|
grub_menu_init (void)
|
2010-06-10 15:20:49 +00:00
|
|
|
{
|
|
|
|
cmd = grub_register_extcmd ("menuentry", grub_cmd_menuentry,
|
2010-09-16 21:48:32 +00:00
|
|
|
GRUB_COMMAND_FLAG_BLOCKS
|
2012-03-04 13:55:13 +00:00
|
|
|
| GRUB_COMMAND_ACCEPT_DASH
|
2010-09-20 21:01:34 +00:00
|
|
|
| GRUB_COMMAND_FLAG_EXTRACTOR,
|
2012-02-26 16:28:05 +00:00
|
|
|
N_("BLOCK"), N_("Define a menu entry."), options);
|
2010-09-20 22:47:49 +00:00
|
|
|
cmd_sub = grub_register_extcmd ("submenu", grub_cmd_menuentry,
|
|
|
|
GRUB_COMMAND_FLAG_BLOCKS
|
2012-03-04 13:55:13 +00:00
|
|
|
| GRUB_COMMAND_ACCEPT_DASH
|
2010-09-20 22:47:49 +00:00
|
|
|
| GRUB_COMMAND_FLAG_EXTRACTOR,
|
|
|
|
N_("BLOCK"), N_("Define a submenu."),
|
|
|
|
options);
|
2010-06-10 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
2010-09-03 15:38:12 +00:00
|
|
|
void
|
|
|
|
grub_menu_fini (void)
|
2010-06-10 15:20:49 +00:00
|
|
|
{
|
|
|
|
grub_unregister_extcmd (cmd);
|
2010-09-20 22:47:49 +00:00
|
|
|
grub_unregister_extcmd (cmd_sub);
|
2010-06-10 15:20:49 +00:00
|
|
|
}
|