summaryrefslogtreecommitdiffstats
path: root/discover/syslinux-parser.c
blob: 8aef9c3b5ea878fefce296bb6e628f1edf2db830 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <i18n/i18n.h>
#include <libgen.h>

#include "log/log.h"
#include "list/list.h"
#include "file/file.h"
#include "talloc/talloc.h"
#include "types/types.h"
#include "parser-conf.h"
#include "parser-utils.h"
#include "resource.h"
#include "paths.h"

struct syslinux_boot_option {
	char *label;
	char *image;
	char *append;
	char *initrd;
	struct list_item list;
};

/* by spec 16 is allowed */
#define INCLUDE_NEST_LIMIT 16

struct syslinux_options {
	struct list processed_options;
	struct syslinux_boot_option *current_option;
	int include_nest_level;
	char *cfg_dir;
};


static const char *const syslinux_conf_files[] = {
	"/boot/syslinux/syslinux.cfg",
	"/syslinux/syslinux.cfg",
	"/syslinux.cfg",
	"/BOOT/SYSLINUX/SYSLINUX.CFG",
	"/SYSLINUX/SYSLINUX.CFG",
	"/SYSLINUX.CFG",
	NULL
};

static const char *const syslinux_kernel_unsupported_extensions[] = {
	".0", /* eventually support PXE here? */
	".bin",
	".bs",
	".bss",
	".c32",
	".cbt",
	".com",
	".img",
	NULL
};

static const char *const syslinux_ignored_names[] = {
	"config",
	"sysapend",
	"localboot",
	"ui",
	"prompt",
	"noescape",
	"nocomplete",
	"allowoptions",
	"timeout",
	"totaltimeout",
	"ontimeout",
	"onerror",
	"serial",
	"nohalt",
	"console",
	"font",
	"kbdmap",
	"say",
	"display",
	"f1",
	"f2",
	"f3",
	"f4",
	"f5"
	"f6",
	"f7",
	"f8",
	"f9",
	"f10",
	"f11",
	"f12",
	NULL
};

static const char *const syslinux_unsupported_boot_names[] = {
	"boot",
	"bss",
	"pxe",
	"fdimage",
	"comboot",
	"com32",
	NULL
};

static struct conf_global_option syslinux_global_options[] = {
	{ .name = "default" },
	{ .name = "implicit" },
	{ .name = "append" },
	{ .name = NULL }
};


static void finish_boot_option(struct syslinux_options *state,
			       bool free_if_unused)
{
	/*
	 * in the normal this function signals a new image block which means
	 * move the current block to the list of processed items
	 * the special case is a label before an image block which we need to
	 * know whether to keep it for further processing or junk it
	 */
	if (state->current_option) {
		if (state->current_option->image) {
			list_add(&state->processed_options,
				 &state->current_option->list);
			state->current_option = NULL;
		} else if (free_if_unused) {
			talloc_free(state->current_option);
			state->current_option = NULL;
		}
	}
}

static bool start_new_option(struct syslinux_options *state)
{
	bool ret = false;

	finish_boot_option(state, false);
	if (!state->current_option)
		state->current_option = talloc_zero(state, struct syslinux_boot_option);

	if (state->current_option)
		ret = true;

	return ret;
}

static void syslinux_process_pair(struct conf_context *conf, const char *name, char *value)
{
	struct syslinux_options *state = conf->parser_info;
	char *buf, *pos, *path;
	int len, rc;

	/* ignore bare values */
	if (!name)
		return;

	if (conf_param_in_list(syslinux_ignored_names, name))
		return;

	/* a new boot entry needs to terminate any prior one */
	if (conf_param_in_list(syslinux_unsupported_boot_names, name)) {
		finish_boot_option(state, true);
		return;
	}

	if (streq(name, "label")) {
		finish_boot_option(state, true);
		state->current_option = talloc_zero(state,
					    struct syslinux_boot_option);
		if (state->current_option)
			state->current_option->label = talloc_strdup(state, value);
		return;
	}

	if (streq(name, "linux")) {

		if (start_new_option(state)) {
			state->current_option->image = talloc_strdup(state, value);
			if (!state->current_option->image) {
				talloc_free(state->current_option);
				state->current_option = NULL;
			}
		}

		return;
	}

	if (streq(name, "kernel")) {

		if (start_new_option(state)) {
		/*
		 * by spec a linux image can not have any of these
		 * extensions, it can have no extension or anything not
		 * in this list
		 */
			pos = strrchr(value, '.');
			if (!pos ||
			!conf_param_in_list(syslinux_kernel_unsupported_extensions, pos)) {
				state->current_option->image = talloc_strdup(state, value);
				if (!state->current_option->image) {
					talloc_free(state->current_option);
					state->current_option = NULL;
				}
			} else	/* clean up any possible trailing label */
				finish_boot_option(state, true);
		}
		return;
	}



	/* APPEND can be global and/or local so need special handling */
	if (streq(name, "append")) {
		if (state->current_option) {
			/* by spec only take last if multiple APPENDs */
			if (state->current_option->append)
				talloc_free(state->current_option->append);
			state->current_option->append = talloc_strdup(state, value);
			if (!state->current_option->append) {
				talloc_free(state->current_option);
				state->current_option = NULL;
			}
		} else {
			finish_boot_option(state, true);
			conf_set_global_option(conf, name, value);
		}
		return;
	}

	/* now the general globals */
	if (conf_set_global_option(conf, name, value)) {
		finish_boot_option(state, true);
		return;
	}

	if (streq(name, "initrd")) {
		if (state->current_option) {
			state->current_option->initrd = talloc_strdup(state, value);
			if (!state->current_option->initrd) {
				talloc_free(state->current_option);
				state->current_option = NULL;
			}
		}
		return;
	}

	if (streq(name, "include")) {
		if (state->include_nest_level < INCLUDE_NEST_LIMIT) {
			state->include_nest_level++;

			/* if absolute in as-is */
			if (value[0] == '/')
				path = talloc_strdup(state, value);
			else /* otherwise relative to the root config file */
				path = join_paths(state, state->cfg_dir, value);

			rc = parser_request_file(conf->dc, conf->dc->device, path, &buf, &len);
			if (!rc) {
				conf_parse_buf(conf, buf, len);

				device_handler_status_dev_info(conf->dc->handler, conf->dc->device,
				_("Parsed nested syslinux configuration from %s"), value);
				talloc_free(buf);
			} else {
				device_handler_status_dev_info(conf->dc->handler, conf->dc->device,
				_("Failed to parse nested syslinux configuration from %s"), value);
			}

			talloc_free(path);

			state->include_nest_level--;
		} else {
			device_handler_status_dev_err(conf->dc->handler, conf->dc->device,
				_("Nested syslinux INCLUDE exceeds limit...ignored"));
		}
		return;
	}

	pb_debug("%s: unknown name: %s\n", __func__, name);
}

static void syslinux_finalize(struct conf_context *conf)
{
	struct syslinux_options *state = conf->parser_info;
	struct syslinux_boot_option *syslinux_opt;
	struct discover_context *dc = conf->dc;
	struct discover_boot_option *d_opt;
	bool implicit_image = true;
	char *args_sigfile_default;
	const char *global_default;
	const char *global_append;
	struct boot_option *opt;
	const char *image;
	const char *label;

	/* clean up any lingering boot entries */
	finish_boot_option(state, true);

	global_append  = conf_get_global_option(conf, "append");
	global_default = conf_get_global_option(conf, "default");

	/*
	 * by spec '0' means disable
	 * note we set the default to '1' (which is by spec) in syslinux_parse
	 */
	if (conf_get_global_option(conf, "implicit"), "0")
		implicit_image = false;

	list_for_each_entry(&state->processed_options, syslinux_opt, list) {
		/* need a valid image */
		if (!syslinux_opt->image)
			continue;

		image = syslinux_opt->image;
		label = syslinux_opt->label;

		/* if implicit is disabled we must have a label */
		if (!label && !implicit_image)
			continue;

		d_opt = discover_boot_option_create(dc, dc->device);
		if (!d_opt)
			continue;
		if (!d_opt->option)
			goto fail;

		opt = d_opt->option;

		if (syslinux_opt->append) {
			/* '-' can signal do not use global APPEND */
			if (!strcmp(syslinux_opt->append, "-"))
				opt->boot_args = talloc_strdup(opt, "");
			else
				opt->boot_args = talloc_asprintf(opt, "%s %s",
								 global_append,
								 syslinux_opt->append);
		} else
			opt->boot_args = talloc_strdup(opt, global_append);

		if (!opt->boot_args)
			goto fail;

		opt->id = talloc_asprintf(opt, "%s#%s", dc->device->device->id, image);

		if (!opt->id)
			goto fail;

		if (label) {
			opt->name = talloc_strdup(opt, label);
			if (!strcmp(label, global_default))
				opt->is_default = true;

			opt->description = talloc_asprintf(opt, "(%s) %s", label, image);
		} else {
			opt->name = talloc_strdup(opt, image);
			opt->description = talloc_strdup(opt, image);
		}

		if (!opt->name)
			goto fail;

		d_opt->boot_image = create_devpath_resource(d_opt, dc->device, image);

		if(!d_opt->boot_image)
			goto fail;

		if (syslinux_opt->initrd) {
			d_opt->initrd = create_devpath_resource(d_opt, dc->device,
								syslinux_opt->initrd);
			opt->description = talloc_asprintf_append(opt->description, 
								  " initrd=%s",
								  syslinux_opt->initrd);

			if (!d_opt->initrd)
				goto fail;
		}

		opt->description = talloc_asprintf_append(opt->description,
							  " args=%s",
							  opt->boot_args);

		if (!opt->description)
			goto fail;

		args_sigfile_default = talloc_asprintf(d_opt, "%s.cmdline.sig",
						       image);

		if (!args_sigfile_default)
			goto fail;

		d_opt->args_sig_file = create_devpath_resource(d_opt, dc->device,
							       args_sigfile_default);

		if (!d_opt->args_sig_file)
			goto fail;

		talloc_free(args_sigfile_default);

		conf_strip_str(opt->boot_args);
		conf_strip_str(opt->description);

		discover_context_add_boot_option(dc, d_opt);
		continue;
fail:
		talloc_free(d_opt);
	}
}

static int syslinux_parse(struct discover_context *dc)
{
	struct syslinux_options *state;
	const char * const *filename;
	struct conf_context *conf;
	char *cfg_dir;
	int len, rc;
	char *buf;

	/* Support block device boot only at present */
	if (dc->event)
		return -1;

	conf = talloc_zero(dc, struct conf_context);

	if (!conf)
		return -1;

	conf->dc = dc;
	conf->global_options = syslinux_global_options,
	conf_init_global_options(conf);
	conf->get_pair = conf_get_pair_space;
	conf->process_pair = syslinux_process_pair;

	conf->parser_info = state = talloc_zero(conf, struct syslinux_options);
	list_init(&state->processed_options);

	/*
	 * set the global defaults
	 * by spec 'default' defaults to 'linux' and
	 * 'implicit' defaults to '1', we also just set
	 * and empty string in 'append' to make it easier
	 * in syslinux_finish
	 */
	conf_set_global_option(conf, "default", "linux");
	conf_set_global_option(conf, "implicit", "1");
	conf_set_global_option(conf, "append", "");

	for (filename = syslinux_conf_files; *filename; filename++) {
		rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
		if (rc)
			continue;

		/*
		 * save location of root config file for possible
		 * INCLUDE directives later
		 *
		 * dirname can overwrite so need local copy to work on
		 */
		cfg_dir = talloc_strdup(conf, *filename);
		state->cfg_dir = talloc_strdup(state, dirname(cfg_dir));
		talloc_free(cfg_dir);

		conf_parse_buf(conf, buf, len);
		device_handler_status_dev_info(dc->handler, dc->device,
				_("Parsed syslinux configuration from %s"),
				*filename);
		talloc_free(buf);

		syslinux_finalize(conf);
	}

	talloc_free(conf);
	return 0;
}

static struct parser syslinux_parser = {
	.name			= "syslinux",
	.parse			= syslinux_parse,
	.resolve_resource	= resolve_devpath_resource,
};

register_parser(syslinux_parser);
OpenPOWER on IntegriCloud