Implement syslinux parser.

This commit is contained in:
Vladimir Serbinenko 2013-12-18 05:28:05 +01:00
parent 8ff35d0a1b
commit 8f5add13ff
10 changed files with 2116 additions and 58 deletions

View File

@ -1,3 +1,7 @@
2013-12-17 Vladimir Serbinenko <phcoder@gmail.com>
Implement syslinux parser.
2013-12-17 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/commands/legacycfg.c: Use 32-bit Linux protocol on non-BIOS.

View File

@ -1235,6 +1235,24 @@ program = {
ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
};
program = {
name = grub-syslinux2cfg;
mansection = 1;
common = util/grub-syslinux2cfg.c;
common = grub-core/lib/syslinux_parse.c;
common = grub-core/lib/getline.c;
common = grub-core/osdep/init.c;
common = grub-core/kern/emu/hostfs.c;
common = grub-core/disk/host.c;
common = grub-core/kern/emu/argp_common.c;
ldadd = libgrubmods.a;
ldadd = libgrubgcry.a;
ldadd = libgrubkern.a;
ldadd = grub-core/gnulib/libgnu.a;
ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
};
program = {
name = grub-glue-efi;
mansection = 1;

View File

@ -0,0 +1,4 @@
[NAME]
grub-syslinux2cfg \- transform syslinux config into grub.cfg
[SEE ALSO]
.BR grub-menulst2cfg (8)

View File

@ -1761,6 +1761,7 @@ module = {
common = normal/term.c;
common = normal/context.c;
common = normal/charset.c;
common = lib/getline.c;
common = script/main.c;
common = script/script.c;
@ -2142,6 +2143,12 @@ module = {
enable = xen;
};
module = {
name = syslinuxcfg;
common = lib/syslinux_parse.c;
common = commands/syslinuxcfg.c;
};
module = {
name = test_blockarg;
common = tests/test_blockarg.c;

View File

@ -0,0 +1,214 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2013 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/extcmd.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/file.h>
#include <grub/normal.h>
#include <grub/script_sh.h>
#include <grub/i18n.h>
#include <grub/term.h>
#include <grub/syslinux_parse.h>
#include <grub/crypto.h>
#include <grub/auth.h>
#include <grub/disk.h>
#include <grub/partition.h>
GRUB_MOD_LICENSE ("GPLv3+");
/* Helper for syslinux_file. */
static grub_err_t
syslinux_file_getline (char **line, int cont __attribute__ ((unused)),
void *data __attribute__ ((unused)))
{
*line = 0;
return GRUB_ERR_NONE;
}
static const struct grub_arg_option options[] =
{
{"root", 'r', 0,
N_("root directory of the syslinux disk (default /)."),
N_("DIR"), ARG_TYPE_STRING},
{"cwd", 'c', 0,
N_("home directory of the syslinux config (default directory of configfile)."),
N_("DIR"), ARG_TYPE_STRING},
{"isolinux", 'i', 0, N_("assume isolinux."), 0, 0},
{"pxelinux", 'p', 0, N_("assume pxelinux."), 0, 0},
{"syslinux", 's', 0, N_("assume syslinux."), 0, 0},
{0, 0, 0, 0, 0, 0}
};
enum
{
OPTION_ROOT,
OPTION_CWD,
OPTION_ISOLINUX,
OPTION_PXELINUX,
OPTION_SYSLINUX
};
static grub_err_t
syslinux_file (grub_extcmd_context_t ctxt, const char *filename)
{
char *result;
const char *root = ctxt->state[OPTION_ROOT].set ? ctxt->state[OPTION_ROOT].arg : "/";
const char *cwd = ctxt->state[OPTION_CWD].set ? ctxt->state[OPTION_CWD].arg : NULL;
grub_syslinux_flavour_t flav = GRUB_SYSLINUX_UNKNOWN;
char *cwdf = NULL;
grub_menu_t menu;
if (ctxt->state[OPTION_ISOLINUX].set)
flav = GRUB_SYSLINUX_ISOLINUX;
if (ctxt->state[OPTION_PXELINUX].set)
flav = GRUB_SYSLINUX_PXELINUX;
if (ctxt->state[OPTION_SYSLINUX].set)
flav = GRUB_SYSLINUX_SYSLINUX;
if (!cwd)
{
char *p;
cwdf = grub_strdup (filename);
if (!cwdf)
return grub_errno;
p = grub_strrchr (cwdf, '/');
if (!p)
{
grub_free (cwdf);
cwd = "/";
cwdf = NULL;
}
else
{
*p = '\0';
cwd = cwdf;
}
}
grub_dprintf ("syslinux",
"transforming syslinux config %s, root = %s, cwd = %s\n",
filename, root, cwd);
result = grub_syslinux_config_file (root, root, cwd, cwd, filename, flav);
if (!result)
return grub_errno;
grub_dprintf ("syslinux", "syslinux config transformed\n");
menu = grub_env_get_menu ();
if (! menu)
{
menu = grub_zalloc (sizeof (*menu));
if (! menu)
return grub_errno;
grub_env_set_menu (menu);
}
grub_normal_parse_line (result, syslinux_file_getline, NULL);
grub_print_error ();
grub_free (result);
grub_free (cwdf);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_cmd_syslinux_source (grub_extcmd_context_t ctxt,
int argc, char **args)
{
int new_env, extractor;
grub_err_t ret;
if (argc != 1)
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
extractor = (ctxt->extcmd->cmd->name[0] == 'e');
new_env = (ctxt->extcmd->cmd->name[extractor ? (sizeof ("extract_syslinux_entries_") - 1)
: (sizeof ("syslinux_") - 1)] == 'c');
if (new_env)
grub_cls ();
if (new_env && !extractor)
grub_env_context_open ();
if (extractor)
grub_env_extractor_open (!new_env);
ret = syslinux_file (ctxt, args[0]);
if (new_env)
{
grub_menu_t menu;
menu = grub_env_get_menu ();
if (menu && menu->size)
grub_show_menu (menu, 1, 0);
if (!extractor)
grub_env_context_close ();
}
if (extractor)
grub_env_extractor_close (!new_env);
return ret;
}
static grub_extcmd_t cmd_source, cmd_configfile;
static grub_extcmd_t cmd_source_extract, cmd_configfile_extract;
GRUB_MOD_INIT(syslinuxcfg)
{
cmd_source
= grub_register_extcmd ("syslinux_source",
grub_cmd_syslinux_source, 0,
N_("FILE"),
/* TRANSLATORS: "syslinux config" means
"config as used by syslinux". */
N_("Parse syslinux config in same context"),
options);
cmd_configfile
= grub_register_extcmd ("syslinux_configfile",
grub_cmd_syslinux_source, 0,
N_("FILE"),
N_("Parse syslinux config in new context"),
options);
cmd_source_extract
= grub_register_extcmd ("extract_syslinux_entries_source",
grub_cmd_syslinux_source, 0,
N_("FILE"),
N_("Parse syslinux config in same context taking only menu entries"),
options);
cmd_configfile_extract
= grub_register_extcmd ("extract_syslinux_entries_configfile",
grub_cmd_syslinux_source, 0,
N_("FILE"),
N_("Parse syslinux config in new context taking only menu entries"),
options);
}
GRUB_MOD_FINI(syslinuxcfg)
{
grub_unregister_extcmd (cmd_source);
grub_unregister_extcmd (cmd_configfile);
grub_unregister_extcmd (cmd_source_extract);
grub_unregister_extcmd (cmd_configfile_extract);
}

92
grub-core/lib/getline.c Normal file
View File

@ -0,0 +1,92 @@
/* main.c - the normal mode main routine */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2000,2001,2002,2003,2005,2006,2007,2008,2009,2013 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/kernel.h>
#include <grub/normal.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/file.h>
#include <grub/mm.h>
#include <grub/term.h>
#include <grub/env.h>
#include <grub/parser.h>
#include <grub/reader.h>
#include <grub/menu_viewer.h>
#include <grub/auth.h>
#include <grub/i18n.h>
#include <grub/charset.h>
#include <grub/script_sh.h>
/* Read a line from the file FILE. */
char *
grub_file_getline (grub_file_t file)
{
char c;
grub_size_t pos = 0;
char *cmdline;
int have_newline = 0;
grub_size_t max_len = 64;
/* Initially locate some space. */
cmdline = grub_malloc (max_len);
if (! cmdline)
return 0;
while (1)
{
if (grub_file_read (file, &c, 1) != 1)
break;
/* Skip all carriage returns. */
if (c == '\r')
continue;
if (pos + 1 >= max_len)
{
char *old_cmdline = cmdline;
max_len = max_len * 2;
cmdline = grub_realloc (cmdline, max_len);
if (! cmdline)
{
grub_free (old_cmdline);
return 0;
}
}
if (c == '\n')
{
have_newline = 1;
break;
}
cmdline[pos++] = c;
}
cmdline[pos] = '\0';
/* If the buffer is empty, don't return anything at all. */
if (pos == 0 && !have_newline)
{
grub_free (cmdline);
cmdline = 0;
}
return cmdline;
}

File diff suppressed because it is too large Load Diff

View File

@ -40,64 +40,6 @@ GRUB_MOD_LICENSE ("GPLv3+");
static int nested_level = 0;
int grub_normal_exit_level = 0;
/* Read a line from the file FILE. */
char *
grub_file_getline (grub_file_t file)
{
char c;
grub_size_t pos = 0;
char *cmdline;
int have_newline = 0;
grub_size_t max_len = 64;
/* Initially locate some space. */
cmdline = grub_malloc (max_len);
if (! cmdline)
return 0;
while (1)
{
if (grub_file_read (file, &c, 1) != 1)
break;
/* Skip all carriage returns. */
if (c == '\r')
continue;
if (pos + 1 >= max_len)
{
char *old_cmdline = cmdline;
max_len = max_len * 2;
cmdline = grub_realloc (cmdline, max_len);
if (! cmdline)
{
grub_free (old_cmdline);
return 0;
}
}
if (c == '\n')
{
have_newline = 1;
break;
}
cmdline[pos++] = c;
}
cmdline[pos] = '\0';
/* If the buffer is empty, don't return anything at all. */
if (pos == 0 && !have_newline)
{
grub_free (cmdline);
cmdline = 0;
}
return cmdline;
}
void
grub_normal_free_menu (grub_menu_t menu)
{

View File

@ -0,0 +1,37 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2013 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/>.
*/
#ifndef GRUB_SYSLINUX_PARSE_HEADER
#define GRUB_SYSLINUX_PARSE_HEADER 1
#include <grub/types.h>
typedef enum
{
GRUB_SYSLINUX_UNKNOWN,
GRUB_SYSLINUX_ISOLINUX,
GRUB_SYSLINUX_PXELINUX,
GRUB_SYSLINUX_SYSLINUX,
} grub_syslinux_flavour_t;
char *
grub_syslinux_config_file (const char *root, const char *target_root,
const char *cwd, const char *target_cwd,
const char *fname, grub_syslinux_flavour_t flav);
#endif

239
util/grub-syslinux2cfg.c Normal file
View File

@ -0,0 +1,239 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2010,2012,2013 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 <config.h>
#include <grub/util/misc.h>
#include <grub/i18n.h>
#include <grub/term.h>
#include <grub/font.h>
#include <grub/emu/hostdisk.h>
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <argp.h>
#include <unistd.h>
#include <errno.h>
#include <grub/err.h>
#include <grub/types.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/syslinux_parse.h>
#include "progname.h"
struct arguments
{
char *input;
char *root;
char *target_root;
char *cwd;
char *target_cwd;
char *output;
int verbosity;
grub_syslinux_flavour_t flav;
};
static struct argp_option options[] = {
{"target-root", 't', N_("DIR"), 0,
N_("root directory as it will be seen on runtime (default /)."), 0},
{"root", 'r', N_("DIR"), 0,
N_("root directory of the syslinux disk (default /)."), 0},
{"target-cwd", 'T', N_("DIR"), 0,
N_("current directory as it will be seen on runtime (default $pwd)."), 0},
{"cwd", 'c', N_("DIR"), 0,
N_("current directory of the syslinux disk (default $pwd)."), 0},
{"output", 'o', N_("FILE"), 0, N_("write output to FILE (default stdout)."), 0},
{"isolinux", 'i', 0, 0, N_("assume isolinux."), 0},
{"pxelinux", 'p', 0, 0, N_("assume pxelinux."), 0},
{"syslinux", 's', 0, 0, N_("assume syslinux."), 0},
{"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
{ 0, 0, 0, 0, 0, 0 }
};
static error_t
argp_parser (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
switch (key)
{
case 't':
free (arguments->target_root);
arguments->target_root = xstrdup (arg);
break;
case 'T':
free (arguments->target_cwd);
arguments->target_cwd = xstrdup (arg);
break;
case 'c':
free (arguments->cwd);
arguments->cwd = xstrdup (arg);
break;
case 'o':
free (arguments->output);
arguments->output = xstrdup (arg);
break;
case ARGP_KEY_ARG:
if (!arguments->input)
{
arguments->input = xstrdup (arg);
return 0;
}
return ARGP_ERR_UNKNOWN;
case 'r':
free (arguments->root);
arguments->root = xstrdup (arg);
return 0;
case 'i':
arguments->flav = GRUB_SYSLINUX_ISOLINUX;
break;
case 's':
arguments->flav = GRUB_SYSLINUX_SYSLINUX;
break;
case 'p':
arguments->flav = GRUB_SYSLINUX_PXELINUX;
break;
case 'v':
arguments->verbosity++;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {
options, argp_parser, N_("[OPTIONS] FILE ROOT"),
N_("Transform syslinux config into GRUB one."),
NULL, NULL, NULL
};
int
main (int argc, char *argv[])
{
struct arguments arguments;
grub_util_host_init (&argc, &argv);
/* Check for options. */
memset (&arguments, 0, sizeof (struct arguments));
if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
{
fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
exit(1);
}
if (!arguments.input)
{
fprintf (stderr, "%s", _("Missing arguments\n"));
exit(1);
}
grub_init_all ();
grub_hostfs_init ();
grub_host_init ();
char *t, *inpfull, *rootfull, *res;
t = canonicalize_file_name (arguments.input);
if (!t)
{
grub_util_error (_("cannot open `%s': %s"), arguments.input,
strerror (errno));
}
inpfull = xasprintf ("(host)/%s", t);
free (t);
t = canonicalize_file_name (arguments.root ? : "/");
if (!t)
{
grub_util_error (_("cannot open `%s': %s"), arguments.root,
strerror (errno));
}
rootfull = xasprintf ("(host)/%s", t);
free (t);
char *cwd = xstrdup (arguments.input);
char *p = strrchr (cwd, '/');
char *cwdfull;
if (p)
*p = '\0';
else
{
free (cwd);
cwd = xstrdup (".");
}
t = canonicalize_file_name (arguments.cwd ? : cwd);
if (!t)
{
grub_util_error (_("cannot open `%s': %s"), arguments.root,
strerror (errno));
}
cwdfull = xasprintf ("(host)/%s", t);
free (t);
res = grub_syslinux_config_file (rootfull, arguments.target_root ? : "/",
cwdfull, arguments.target_cwd ? : cwd,
inpfull, arguments.flav);
if (!res)
grub_util_error ("%s", grub_errmsg);
if (arguments.output)
{
FILE *f = grub_util_fopen (arguments.output, "wb");
if (!f)
grub_util_error (_("cannot open `%s': %s"), arguments.output,
strerror (errno));
fwrite (res, 1, strlen (res), f);
fclose (f);
}
else
printf ("%s\n", res);
free (res);
free (rootfull);
free (inpfull);
free (arguments.root);
free (arguments.output);
free (arguments.target_root);
free (arguments.input);
free (cwdfull);
free (cwd);
return 0;
}