discover/grub2: Fix free in load_env command
[petitboot] / discover / kboot-parser.c
1 #define _GNU_SOURCE
2
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "log/log.h"
8 #include "talloc/talloc.h"
9 #include "types/types.h"
10 #include "parser-conf.h"
11 #include "parser-utils.h"
12 #include "resource.h"
13
14 static void kboot_process_pair(struct conf_context *conf, const char *name,
15                 char *value)
16 {
17         const char *const *ignored_names = conf->parser_info;
18         struct discover_boot_option *d_opt;
19         struct boot_option *opt;
20         char *pos;
21         char *args;
22         const char *initrd;
23         const char *root;
24         const char *dtb;
25
26         /* ignore bare values */
27
28         if (!name)
29                 return;
30
31         if (conf_param_in_list(ignored_names, name))
32                 return;
33
34         if (conf_set_global_option(conf, name, value))
35                 return;
36
37         /* opt must be associated with dc */
38
39         d_opt = talloc_zero(conf->dc, struct discover_boot_option);
40         d_opt->device = conf->dc->device;
41         opt = talloc_zero(d_opt, struct boot_option);
42
43         if (!opt)
44                 return;
45
46         opt->id = talloc_asprintf(opt, "%s#%s", conf->dc->device->device->id,
47                         name);
48         opt->name = talloc_strdup(opt, name);
49         d_opt->option = opt;
50
51         args = talloc_strdup(opt, "");
52         initrd = conf_get_global_option(conf, "initrd");
53         root = conf_get_global_option(conf, "root");
54         dtb = conf_get_global_option(conf, "dtb");
55
56         pos = strchr(value, ' ');
57
58         /* if there's no space, it's only a kernel image with no params */
59
60         if (!pos)
61                 goto out_add;
62         *pos = 0;
63
64         for (pos++; pos;) {
65                 char *cl_name, *cl_value;
66
67                 pos = conf_get_pair_equal(conf, pos, &cl_name, &cl_value, ' ');
68
69                 if (!cl_name) {
70                         args = talloc_asprintf_append(args, "%s ", cl_value);
71                         continue;
72                 }
73
74                 if (streq(cl_name, "initrd")) {
75                         initrd = cl_value;
76                         continue;
77                 }
78
79                 if (streq(cl_name, "root")) {
80                         root = cl_value;
81                         continue;
82                 }
83
84                 if (streq(cl_name, "dtb")) {
85                         dtb = cl_value;
86                         continue;
87                 }
88
89                 args = talloc_asprintf_append(args, "%s=%s ", cl_name,
90                         cl_value);
91         }
92
93 out_add:
94         d_opt->boot_image = create_devpath_resource(d_opt,
95                                 conf->dc->device, value);
96
97         if (root) {
98                 opt->boot_args = talloc_asprintf(opt, "root=%s %s", root, args);
99                 talloc_free(args);
100         } else
101                 opt->boot_args = args;
102
103         opt->description = talloc_asprintf(opt, "%s %s", value,
104                         opt->boot_args);
105
106         if (initrd) {
107                 d_opt->initrd = create_devpath_resource(d_opt,
108                                 conf->dc->device, initrd);
109                 opt->description = talloc_asprintf_append(opt->description,
110                                 " initrd=%s", initrd);
111         }
112
113         if (dtb) {
114                 d_opt->dtb = create_devpath_resource(d_opt,
115                                 conf->dc->device, dtb);
116                 opt->description = talloc_asprintf_append(opt->description,
117                                 " dtb=%s", dtb);
118         }
119
120         conf_strip_str(opt->boot_args);
121         conf_strip_str(opt->description);
122
123         discover_context_add_boot_option(conf->dc, d_opt);
124 }
125
126 static struct conf_global_option kboot_global_options[] = {
127         { .name = "dtb" },
128         { .name = "initrd" },
129         { .name = "root" },
130         { .name = "video" },
131         { .name = NULL }
132 };
133
134 static const char *const kboot_conf_files[] = {
135         "/kboot.conf",
136         "/kboot.cnf",
137         "/etc/kboot.conf",
138         "/etc/kboot.cnf",
139         "/KBOOT.CONF",
140         "/KBOOT.CNF",
141         "/ETC/KBOOT.CONF",
142         "/ETC/KBOOT.CNF",
143         NULL
144 };
145
146 static const char *const kboot_ignored_names[] = {
147         "default",
148         "delay",
149         "message",
150         "timeout",
151         NULL
152 };
153
154 static int kboot_parse(struct discover_context *dc)
155 {
156         struct conf_context *conf;
157         const char * const *filename;
158         char *buf;
159         int len, rc;
160
161         /* Support block device boot only at present */
162         if (dc->event)
163                 return -1;
164
165         conf = talloc_zero(dc, struct conf_context);
166
167         if (!conf)
168                 return -1;
169
170         conf->dc = dc;
171         conf->global_options = kboot_global_options,
172         conf_init_global_options(conf);
173         conf->get_pair = conf_get_pair_equal;
174         conf->process_pair = kboot_process_pair;
175         conf->parser_info = (void *)kboot_ignored_names;
176
177         for (filename = kboot_conf_files; *filename; filename++) {
178                 rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
179                 if (rc)
180                         continue;
181
182                 conf_parse_buf(conf, buf, len);
183                 talloc_free(buf);
184         }
185
186         talloc_free(conf);
187         return 0;
188 }
189
190 static struct parser kboot_parser = {
191         .name                   = "kboot",
192         .parse                  = kboot_parse,
193         .resolve_resource       = resolve_devpath_resource,
194 };
195
196 register_parser(kboot_parser);