]> git.ozlabs.org Git - petitboot/blob - discover/syslinux-parser.c
test/parser: Make parser_scandir() ignore files with path len less than dir
[petitboot] / discover / syslinux-parser.c
1 #if defined(HAVE_CONFIG_H)
2 #include "config.h"
3 #endif
4
5 #include <assert.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <i18n/i18n.h>
9 #include <libgen.h>
10
11 #include "log/log.h"
12 #include "list/list.h"
13 #include "file/file.h"
14 #include "talloc/talloc.h"
15 #include "types/types.h"
16 #include "parser-conf.h"
17 #include "parser-utils.h"
18 #include "resource.h"
19 #include "paths.h"
20
21 struct syslinux_boot_option {
22         char *label;
23         char *image;
24         char *append;
25         char *initrd;
26         struct list_item list;
27 };
28
29 /* by spec 16 is allowed */
30 #define INCLUDE_NEST_LIMIT 16
31
32 struct syslinux_options {
33         struct list processed_options;
34         struct syslinux_boot_option *current_option;
35         int include_nest_level;
36         char *cfg_dir;
37 };
38
39 struct conf_file_stat {
40         char *name;
41         struct stat stat;
42         struct list_item list;
43 };
44
45 static const char *const syslinux_conf_files[] = {
46         "/boot/syslinux/syslinux.cfg",
47         "/syslinux/syslinux.cfg",
48         "/syslinux.cfg",
49         "/BOOT/SYSLINUX/SYSLINUX.CFG",
50         "/SYSLINUX/SYSLINUX.CFG",
51         "/SYSLINUX.CFG",
52         NULL
53 };
54
55 static const char *const syslinux_kernel_unsupported_extensions[] = {
56         ".0", /* eventually support PXE here? */
57         ".bin",
58         ".bs",
59         ".bss",
60         ".c32",
61         ".cbt",
62         ".com",
63         ".img",
64         NULL
65 };
66
67 static const char *const syslinux_ignored_names[] = {
68         "config",
69         "sysapend",
70         "localboot",
71         "ui",
72         "prompt",
73         "noescape",
74         "nocomplete",
75         "allowoptions",
76         "timeout",
77         "totaltimeout",
78         "ontimeout",
79         "onerror",
80         "serial",
81         "nohalt",
82         "console",
83         "font",
84         "kbdmap",
85         "say",
86         "display",
87         "f1",
88         "f2",
89         "f3",
90         "f4",
91         "f5",
92         "f6",
93         "f7",
94         "f8",
95         "f9",
96         "f10",
97         "f11",
98         "f12",
99         NULL
100 };
101
102 static const char *const syslinux_unsupported_boot_names[] = {
103         "boot",
104         "bss",
105         "pxe",
106         "fdimage",
107         "comboot",
108         "com32",
109         NULL
110 };
111
112 static struct conf_global_option syslinux_global_options[] = {
113         { .name = "default" },
114         { .name = "implicit" },
115         { .name = "append" },
116         { .name = NULL }
117 };
118
119
120 static void finish_boot_option(struct syslinux_options *state,
121                                bool free_if_unused)
122 {
123         /*
124          * in the normal this function signals a new image block which means
125          * move the current block to the list of processed items
126          * the special case is a label before an image block which we need to
127          * know whether to keep it for further processing or junk it
128          */
129         if (state->current_option) {
130                 if (state->current_option->image) {
131                         list_add(&state->processed_options,
132                                  &state->current_option->list);
133                         state->current_option = NULL;
134                 } else if (free_if_unused) {
135                         talloc_free(state->current_option);
136                         state->current_option = NULL;
137                 }
138         }
139 }
140
141 static bool start_new_option(struct syslinux_options *state)
142 {
143         bool ret = false;
144
145         finish_boot_option(state, false);
146         if (!state->current_option)
147                 state->current_option = talloc_zero(state, struct syslinux_boot_option);
148
149         if (state->current_option)
150                 ret = true;
151
152         return ret;
153 }
154
155 static void syslinux_process_pair(struct conf_context *conf, const char *name, char *value)
156 {
157         struct syslinux_options *state = conf->parser_info;
158         char *buf, *pos, *path;
159         int len, rc;
160
161         /* ignore bare values */
162         if (!name)
163                 return;
164
165         if (conf_param_in_list(syslinux_ignored_names, name))
166                 return;
167
168         /* a new boot entry needs to terminate any prior one */
169         if (conf_param_in_list(syslinux_unsupported_boot_names, name)) {
170                 finish_boot_option(state, true);
171                 return;
172         }
173
174         if (streq(name, "label")) {
175                 finish_boot_option(state, true);
176                 state->current_option = talloc_zero(state,
177                                             struct syslinux_boot_option);
178                 if (state->current_option)
179                         state->current_option->label = talloc_strdup(state, value);
180                 return;
181         }
182
183         if (streq(name, "linux")) {
184
185                 if (start_new_option(state)) {
186                         state->current_option->image = talloc_strdup(state, value);
187                         if (!state->current_option->image) {
188                                 talloc_free(state->current_option);
189                                 state->current_option = NULL;
190                         }
191                 }
192
193                 return;
194         }
195
196         if (streq(name, "kernel")) {
197
198                 if (start_new_option(state)) {
199                 /*
200                  * by spec a linux image can not have any of these
201                  * extensions, it can have no extension or anything not
202                  * in this list
203                  */
204                         pos = strrchr(value, '.');
205                         if (!pos ||
206                         !conf_param_in_list(syslinux_kernel_unsupported_extensions, pos)) {
207                                 state->current_option->image = talloc_strdup(state, value);
208                                 if (!state->current_option->image) {
209                                         talloc_free(state->current_option);
210                                         state->current_option = NULL;
211                                 }
212                         } else  /* clean up any possible trailing label */
213                                 finish_boot_option(state, true);
214                 }
215                 return;
216         }
217
218
219
220         /* APPEND can be global and/or local so need special handling */
221         if (streq(name, "append")) {
222                 if (state->current_option) {
223                         /* by spec only take last if multiple APPENDs */
224                         if (state->current_option->append)
225                                 talloc_free(state->current_option->append);
226                         state->current_option->append = talloc_strdup(state, value);
227                         if (!state->current_option->append) {
228                                 talloc_free(state->current_option);
229                                 state->current_option = NULL;
230                         }
231                 } else {
232                         finish_boot_option(state, true);
233                         conf_set_global_option(conf, name, value);
234                 }
235                 return;
236         }
237
238         /* now the general globals */
239         if (conf_set_global_option(conf, name, value)) {
240                 finish_boot_option(state, true);
241                 return;
242         }
243
244         if (streq(name, "initrd")) {
245                 if (state->current_option) {
246                         state->current_option->initrd = talloc_strdup(state, value);
247                         if (!state->current_option->initrd) {
248                                 talloc_free(state->current_option);
249                                 state->current_option = NULL;
250                         }
251                 }
252                 return;
253         }
254
255         if (streq(name, "include")) {
256                 if (state->include_nest_level < INCLUDE_NEST_LIMIT) {
257                         state->include_nest_level++;
258
259                         /* if absolute in as-is */
260                         if (value[0] == '/')
261                                 path = talloc_strdup(state, value);
262                         else /* otherwise relative to the root config file */
263                                 path = join_paths(state, state->cfg_dir, value);
264
265                         rc = parser_request_file(conf->dc, conf->dc->device, path, &buf, &len);
266                         if (!rc) {
267                                 conf_parse_buf(conf, buf, len);
268
269                                 device_handler_status_dev_info(conf->dc->handler, conf->dc->device,
270                                 _("Parsed nested syslinux configuration from %s"), value);
271                                 talloc_free(buf);
272                         } else {
273                                 device_handler_status_dev_info(conf->dc->handler, conf->dc->device,
274                                 _("Failed to parse nested syslinux configuration from %s"), value);
275                         }
276
277                         talloc_free(path);
278
279                         state->include_nest_level--;
280                 } else {
281                         device_handler_status_dev_err(conf->dc->handler, conf->dc->device,
282                                 _("Nested syslinux INCLUDE exceeds limit...ignored"));
283                 }
284                 return;
285         }
286
287         pb_debug("%s: unknown name: %s\n", __func__, name);
288 }
289
290 static void syslinux_finalize(struct conf_context *conf)
291 {
292         struct syslinux_options *state = conf->parser_info;
293         struct syslinux_boot_option *syslinux_opt, *tmp;
294         struct discover_context *dc = conf->dc;
295         struct discover_boot_option *d_opt;
296         bool implicit_image = true;
297         char *args_sigfile_default;
298         const char *global_default;
299         const char *global_append;
300         struct boot_option *opt;
301         const char *image;
302         const char *label;
303
304         /* clean up any lingering boot entries */
305         finish_boot_option(state, true);
306
307         global_append  = conf_get_global_option(conf, "append");
308         global_default = conf_get_global_option(conf, "default");
309
310         /*
311          * by spec '0' means disable
312          * note we set the default to '1' (which is by spec) in syslinux_parse
313          */
314         if (conf_get_global_option(conf, "implicit"), "0")
315                 implicit_image = false;
316
317         list_for_each_entry(&state->processed_options, syslinux_opt, list) {
318                 /* need a valid image */
319                 if (!syslinux_opt->image)
320                         continue;
321
322                 image = syslinux_opt->image;
323                 label = syslinux_opt->label;
324
325                 /* if implicit is disabled we must have a label */
326                 if (!label && !implicit_image)
327                         continue;
328
329                 d_opt = discover_boot_option_create(dc, dc->device);
330                 if (!d_opt)
331                         continue;
332                 if (!d_opt->option)
333                         goto fail;
334
335                 opt = d_opt->option;
336
337                 if (syslinux_opt->append) {
338                         /* '-' can signal do not use global APPEND */
339                         if (!strcmp(syslinux_opt->append, "-"))
340                                 opt->boot_args = talloc_strdup(opt, "");
341                         else if (global_append)
342                                 opt->boot_args = talloc_asprintf(opt, "%s %s",
343                                                                  global_append,
344                                                                  syslinux_opt->append);
345                         else
346                                 opt->boot_args = talloc_strdup(opt, syslinux_opt->append);
347                 } else if (global_append)
348                         opt->boot_args = talloc_strdup(opt, global_append);
349
350                 if (!opt->boot_args)
351                         goto fail;
352
353                 opt->id = talloc_asprintf(opt, "%s#%s", dc->device->device->id, image);
354
355                 if (!opt->id)
356                         goto fail;
357
358                 if (label) {
359                         opt->name = talloc_strdup(opt, label);
360                         if (!strcmp(label, global_default))
361                                 opt->is_default = true;
362
363                         opt->description = talloc_asprintf(opt, "(%s) %s", label, image);
364                 } else {
365                         opt->name = talloc_strdup(opt, image);
366                         opt->description = talloc_strdup(opt, image);
367                 }
368
369                 if (!opt->name)
370                         goto fail;
371
372                 d_opt->boot_image = create_devpath_resource(d_opt, dc->device, image);
373
374                 if(!d_opt->boot_image)
375                         goto fail;
376
377                 if (syslinux_opt->initrd) {
378                         d_opt->initrd = create_devpath_resource(d_opt, dc->device,
379                                                                 syslinux_opt->initrd);
380                         opt->description = talloc_asprintf_append(opt->description, 
381                                                                   " initrd=%s",
382                                                                   syslinux_opt->initrd);
383
384                         if (!d_opt->initrd)
385                                 goto fail;
386                 }
387
388                 opt->description = talloc_asprintf_append(opt->description,
389                                                           " args=%s",
390                                                           opt->boot_args);
391
392                 if (!opt->description)
393                         goto fail;
394
395                 args_sigfile_default = talloc_asprintf(d_opt, "%s.cmdline.sig",
396                                                        image);
397
398                 if (!args_sigfile_default)
399                         goto fail;
400
401                 d_opt->args_sig_file = create_devpath_resource(d_opt, dc->device,
402                                                                args_sigfile_default);
403
404                 if (!d_opt->args_sig_file)
405                         goto fail;
406
407                 talloc_free(args_sigfile_default);
408
409                 conf_strip_str(opt->boot_args);
410                 conf_strip_str(opt->description);
411
412                 discover_context_add_boot_option(dc, d_opt);
413                 d_opt = NULL;
414                 continue;
415
416 fail:
417                 talloc_free(d_opt);
418         }
419
420         list_for_each_entry_safe(&state->processed_options, syslinux_opt, tmp, list)
421                 talloc_free(syslinux_opt);
422         list_init(&state->processed_options);
423 }
424
425 static int syslinux_parse(struct discover_context *dc)
426 {
427         struct conf_file_stat *confcmp, *confdat;
428         struct list processed_conf_files;
429         struct syslinux_options *state;
430         const char * const *filename;
431         struct conf_context *conf;
432         struct stat statbuf;
433         char *cfg_dir;
434         int len, rc;
435         char *buf;
436
437         /* Support block device boot only at present */
438         if (dc->event)
439                 return -1;
440
441         conf = talloc_zero(dc, struct conf_context);
442
443         if (!conf)
444                 return -1;
445
446         conf->dc = dc;
447         conf->global_options = syslinux_global_options,
448         conf_init_global_options(conf);
449         conf->get_pair = conf_get_pair_space;
450         conf->process_pair = syslinux_process_pair;
451
452         conf->parser_info = state = talloc_zero(conf, struct syslinux_options);
453         list_init(&state->processed_options);
454
455         list_init(&processed_conf_files);
456
457         /*
458          * set the global defaults
459          * by spec 'default' defaults to 'linux' and
460          * 'implicit' defaults to '1'
461          */
462         conf_set_global_option(conf, "default", "linux");
463         conf_set_global_option(conf, "implicit", "1");
464
465         for (filename = syslinux_conf_files; *filename; filename++) {
466                 /*
467                  * guard against duplicate entries in case-insensitive
468                  * filesystems, mainly vfat boot partitions
469                  */
470                 rc = parser_stat_path(dc, dc->device, *filename, &statbuf);
471                 if (rc)
472                         continue;
473
474                 rc = 0;
475
476                 list_for_each_entry(&processed_conf_files, confcmp, list) {
477                         if (confcmp->stat.st_ino == statbuf.st_ino) {
478                                 pb_log("conf file %s is a path duplicate of %s..skipping\n",
479                                        *filename, confcmp->name);
480                                 rc = 1;
481                                 break;
482                         }
483                 }
484
485                 if (rc)
486                         continue;
487
488                 rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
489                 if (rc)
490                         continue;
491
492                 confdat = talloc_zero(conf, struct conf_file_stat);
493                 confdat->stat = statbuf;
494                 confdat->name = talloc_strdup(confdat, *filename);
495                 list_add(&processed_conf_files, &confdat->list);
496
497                 /*
498                  * save location of root config file for possible
499                  * INCLUDE directives later
500                  *
501                  * dirname can overwrite so need local copy to work on
502                  */
503                 cfg_dir = talloc_strdup(conf, *filename);
504                 state->cfg_dir = talloc_strdup(state, dirname(cfg_dir));
505                 talloc_free(cfg_dir);
506
507                 conf_parse_buf(conf, buf, len);
508                 device_handler_status_dev_info(dc->handler, dc->device,
509                                 _("Parsed syslinux configuration from %s"),
510                                 *filename);
511                 talloc_free(buf);
512
513                 syslinux_finalize(conf);
514         }
515
516         talloc_free(conf);
517         return 0;
518 }
519
520 static struct parser syslinux_parser = {
521         .name                   = "syslinux",
522         .parse                  = syslinux_parse,
523         .resolve_resource       = resolve_devpath_resource,
524 };
525
526 register_parser(syslinux_parser);