summaryrefslogtreecommitdiffstats
path: root/grub-core/commands/syslinuxcfg.c
blob: 7be28fada90b11278c0aa82017ae3eae9f8bd25b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 *  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_("current directory of syslinux [default is parent directory of input file]."),
     N_("DIR"), ARG_TYPE_STRING},
    {"isolinux",     'i',  0, N_("assume input is an isolinux configuration file."), 0, 0},
    {"pxelinux",     'p',  0, N_("assume input is a pxelinux configuration file."), 0, 0},
    {"syslinux",     's',  0, N_("assume input is a syslinux configuration file."), 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)
	{
	  grub_free (result);
	  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_("Execute syslinux config in same context"),
			    options);
  cmd_configfile
    = grub_register_extcmd ("syslinux_configfile",
			    grub_cmd_syslinux_source, 0,
			    N_("FILE"),
			    N_("Execute syslinux config in new context"),
			    options);
  cmd_source_extract
    = grub_register_extcmd ("extract_syslinux_entries_source",
			    grub_cmd_syslinux_source, 0,
			    N_("FILE"),
			    N_("Execute 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_("Execute 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);
}