]> git.ozlabs.org Git - petitboot/blob - discover/event-parser.c
discover/event-parser: correctly populate boot option resources
[petitboot] / discover / event-parser.c
1 #define _GNU_SOURCE
2
3 #include <assert.h>
4
5 #include "log/log.h"
6 #include "talloc/talloc.h"
7 #include "url/url.h"
8
9 #include "resource.h"
10 #include "event.h"
11 #include "parser-utils.h"
12 #include "device-handler.h"
13
14 static struct resource *user_event_resource(struct discover_boot_option *opt,
15                 struct event *event, const char *param_name)
16 {
17         struct resource *res;
18         struct pb_url *url;
19         const char *val;
20
21         val = event_get_param(event, param_name);
22         if (!val)
23                 return NULL;
24
25         url = pb_url_parse(opt, val);
26         if (!url)
27                 return NULL;
28
29         res = create_url_resource(opt, url);
30         if (!res) {
31                 talloc_free(url);
32                 return NULL;
33         }
34
35         return res;
36 }
37
38 /**
39  * parse_user_event - Parse a user event.
40  *
41  * Understands params: name, image, args.
42  */
43
44 int parse_user_event(struct discover_context *ctx, struct event *event)
45 {
46         struct discover_boot_option *d_opt;
47         struct boot_option *opt;
48         struct device *dev;
49         const char *p;
50
51         dev = ctx->device->device;
52
53         d_opt = discover_boot_option_create(ctx, ctx->device);
54         opt = d_opt->option;
55
56         if (!d_opt)
57                 goto fail;
58
59         p = event_get_param(event, "name");
60
61         if (!p) {
62                 pb_log("%s: no name found\n", __func__);
63                 goto fail;
64         }
65
66         opt->id = talloc_asprintf(opt, "%s#%s", dev->id, p);
67         opt->device_id = talloc_strdup(opt, dev->id);
68         opt->name = talloc_strdup(opt, p);
69
70         d_opt->boot_image = user_event_resource(d_opt, event, "image");
71         if (!d_opt->boot_image) {
72                 pb_log("%s: no boot image found for %s!\n", __func__,
73                                 opt->name);
74                 goto fail;
75         }
76
77         d_opt->initrd = user_event_resource(d_opt, event, "initrd");
78
79         p = event_get_param(event, "args");
80
81         if (p)
82                 opt->boot_args = talloc_strdup(opt, p);
83
84         opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
85                 opt->boot_args ? : "");
86
87         discover_context_add_boot_option(ctx, d_opt);
88
89         return 0;
90
91 fail:
92         talloc_free(d_opt);
93         return -1;
94 }