discover: pxe: Avoid dereferencing null pointer
[petitboot] / discover / kboot-parser.c
index b28603e3895107299b4a27b4a5e1260f97b6c0cc..bf9c5eb2fe98f5f59056cad85f47e018cb31a674 100644 (file)
@@ -1,26 +1,30 @@
-#define _GNU_SOURCE
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
 
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
+#include <i18n/i18n.h>
 
 #include "log/log.h"
 #include "talloc/talloc.h"
 #include "types/types.h"
 #include "parser-conf.h"
 #include "parser-utils.h"
-#include "paths.h"
+#include "resource.h"
 
 static void kboot_process_pair(struct conf_context *conf, const char *name,
                char *value)
 {
        const char *const *ignored_names = conf->parser_info;
-       struct device *dev;
+       struct discover_boot_option *d_opt;
+       struct boot_option *opt;
        char *pos;
        char *args;
        const char *initrd;
        const char *root;
-       struct boot_option *opt;
+       const char *dtb;
 
        /* ignore bare values */
 
@@ -35,18 +39,22 @@ static void kboot_process_pair(struct conf_context *conf, const char *name,
 
        /* opt must be associated with dc */
 
-       dev = conf->dc->device->device;
-       opt = talloc_zero(dev, struct boot_option);
+       d_opt = talloc_zero(conf->dc, struct discover_boot_option);
+       d_opt->device = conf->dc->device;
+       opt = talloc_zero(d_opt, struct boot_option);
 
        if (!opt)
                return;
 
-       opt->id = talloc_asprintf(opt, "%s#%s", dev->id, name);
+       opt->id = talloc_asprintf(opt, "%s#%s", conf->dc->device->device->id,
+                       name);
        opt->name = talloc_strdup(opt, name);
+       d_opt->option = opt;
 
        args = talloc_strdup(opt, "");
        initrd = conf_get_global_option(conf, "initrd");
        root = conf_get_global_option(conf, "root");
+       dtb = conf_get_global_option(conf, "dtb");
 
        pos = strchr(value, ' ');
 
@@ -76,13 +84,24 @@ static void kboot_process_pair(struct conf_context *conf, const char *name,
                        continue;
                }
 
+               if (streq(cl_name, "dtb")) {
+                       dtb = cl_value;
+                       continue;
+               }
+
                args = talloc_asprintf_append(args, "%s=%s ", cl_name,
                        cl_value);
        }
 
 out_add:
-       opt->boot_image_file = resolve_path(opt, value,
-                       conf->dc->device->device_path);
+       d_opt->boot_image = create_devpath_resource(d_opt,
+                               conf->dc->device, value);
+
+       char* args_sigfile_default = talloc_asprintf(d_opt,
+               "%s.cmdline.sig", value);
+       d_opt->args_sig_file = create_devpath_resource(d_opt,
+                               conf->dc->device, args_sigfile_default);
+       talloc_free(args_sigfile_default);
 
        if (root) {
                opt->boot_args = talloc_asprintf(opt, "root=%s %s", root, args);
@@ -90,23 +109,31 @@ out_add:
        } else
                opt->boot_args = args;
 
+       opt->description = talloc_asprintf(opt, "%s %s", value,
+                       opt->boot_args);
+
        if (initrd) {
-               opt->initrd_file = resolve_path(opt, initrd,
-                               conf->dc->device->device_path);
+               d_opt->initrd = create_devpath_resource(d_opt,
+                               conf->dc->device, initrd);
+               opt->description = talloc_asprintf_append(opt->description,
+                               " initrd=%s", initrd);
+       }
 
-               opt->description = talloc_asprintf(opt, "%s initrd=%s %s",
-                       value, initrd, opt->boot_args);
-       } else
-               opt->description = talloc_asprintf(opt, "%s %s", value,
-                       opt->boot_args);
+       if (dtb) {
+               d_opt->dtb = create_devpath_resource(d_opt,
+                               conf->dc->device, dtb);
+               opt->description = talloc_asprintf_append(opt->description,
+                               " dtb=%s", dtb);
+       }
 
        conf_strip_str(opt->boot_args);
        conf_strip_str(opt->description);
 
-       discover_context_add_boot_option(conf->dc, opt);
+       discover_context_add_boot_option(conf->dc, d_opt);
 }
 
 static struct conf_global_option kboot_global_options[] = {
+       { .name = "dtb" },
        { .name = "initrd" },
        { .name = "root" },
        { .name = "video" },
@@ -133,30 +160,49 @@ static const char *const kboot_ignored_names[] = {
        NULL
 };
 
-static int kboot_parse(struct discover_context *dc, char *buf, int len)
+static int kboot_parse(struct discover_context *dc)
 {
        struct conf_context *conf;
+       const char * const *filename;
+       char *buf;
+       int len, rc;
+
+       /* Support block device boot only at present */
+       if (dc->event)
+               return -1;
 
        conf = talloc_zero(dc, struct conf_context);
 
        if (!conf)
-               return 0;
+               return -1;
 
        conf->dc = dc;
        conf->global_options = kboot_global_options,
        conf_init_global_options(conf);
        conf->get_pair = conf_get_pair_equal;
        conf->process_pair = kboot_process_pair;
-       conf->parser_info = (void *)kboot_ignored_names,
+       conf->parser_info = (void *)kboot_ignored_names;
 
-       conf_parse_buf(conf, buf, len);
+       for (filename = kboot_conf_files; *filename; filename++) {
+               rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
+               if (rc)
+                       continue;
+
+               conf_parse_buf(conf, buf, len);
+               device_handler_status_dev_info(dc->handler, dc->device,
+                               _("Parsed kboot configuration from %s"),
+                               *filename);
+               talloc_free(buf);
+       }
 
        talloc_free(conf);
-       return 1;
+       return 0;
 }
 
-struct parser __kboot_parser = {
-       .name           = "kboot",
-       .parse          = kboot_parse,
-       .filenames      = kboot_conf_files,
+static struct parser kboot_parser = {
+       .name                   = "kboot",
+       .parse                  = kboot_parse,
+       .resolve_resource       = resolve_devpath_resource,
 };
+
+register_parser(kboot_parser);