]> git.ozlabs.org Git - petitboot/blob - discover/grub2/builtins.c
1ce63cf13103c8f2157029bca3464a5110e0f1f2
[petitboot] / discover / grub2 / builtins.c
1
2 #include <stdio.h>
3 #include <string.h>
4
5 #include <log/log.h>
6 #include <types/types.h>
7 #include <talloc/talloc.h>
8 #include <array-size/array-size.h>
9
10 #include "grub2.h"
11
12
13 static int builtin_set(struct grub2_script *script,
14                 void *data __attribute__((unused)),
15                 int argc, char *argv[])
16 {
17         char *name, *value, *p;
18         int i;
19
20         if (argc < 2)
21                 return -1;
22
23         p = strchr(argv[1], '=');
24         if (!p)
25                 return -1;
26
27         name = talloc_strndup(script, argv[1], p - argv[1]);
28         value = talloc_strdup(script, p+1);
29
30         for (i = 2; i < argc; i++)
31                 value = talloc_asprintf_append(value, " %s", argv[i]);
32
33         script_env_set(script, name, value);
34
35         return 0;
36 }
37
38 static int builtin_linux(struct grub2_script *script,
39                 void *data __attribute__((unused)),
40                 int argc, char *argv[])
41 {
42         struct discover_boot_option *opt = script->opt;
43         const char *root;
44         int i;
45
46         if (!opt) {
47                 pb_log("grub2 syntax error: 'linux' statement outside "
48                                 "a menuentry.\n");
49                 return -1;
50         }
51
52         if (argc < 2) {
53                 pb_log("grub2 syntax error: no filename provided to "
54                                 "linux statement\n");
55                 return -1;
56         }
57
58         root = script_env_get(script, "root");
59
60         opt->boot_image = create_grub2_resource(opt, script->ctx->device,
61                                                 root, argv[1]);
62         opt->option->boot_args = NULL;
63
64         if (argc > 2)
65                 opt->option->boot_args = talloc_strdup(opt, argv[2]);
66
67         for (i = 3; i < argc; i++)
68                 opt->option->boot_args = talloc_asprintf_append(
69                                                 opt->option->boot_args,
70                                                 " %s", argv[i]);
71         return 0;
72 }
73
74 static int builtin_initrd(struct grub2_script *script,
75                 void *data __attribute__((unused)),
76                 int argc, char *argv[])
77 {
78         struct discover_boot_option *opt = script->opt;
79         const char *root;
80
81         if (!opt) {
82                 pb_log("grub2 syntax error: 'initrd' statement outside "
83                                 "a menuentry.\n");
84                 return -1;
85         }
86
87         if (argc < 2) {
88                 pb_log("grub2 syntax error: no filename provided to "
89                                 "initrd statement\n");
90                 return -1;
91         }
92
93         root = script_env_get(script, "root");
94         opt->initrd = create_grub2_resource(opt, script->ctx->device,
95                                                 root, argv[1]);
96
97         return 0;
98 }
99
100 static int builtin_search(struct grub2_script *script,
101                 void *data __attribute__((unused)),
102                 int argc, char *argv[])
103 {
104         const char *env_var, *spec;
105         int i;
106
107         env_var = NULL;
108
109         for (i = 1; i < argc - 1; i++) {
110                 if (!strncmp(argv[i], "--set=", strlen("--set="))) {
111                         env_var = argv[i] + strlen("--set=");
112                         break;
113                 }
114         }
115
116         if (!env_var)
117                 return 0;
118
119         spec = argv[argc - 1];
120
121         script_env_set(script, env_var, spec);
122
123         return 0;
124 }
125
126 static bool builtin_test_op(int argc, char **argv, int *consumed)
127 {
128         char *op;
129
130         if (argc >= 3) {
131                 const char *a1, *a2;
132
133                 a1 = argv[0];
134                 op = argv[1];
135                 a2 = argv[2];
136
137                 if (!strcmp(op, "=") || !strcmp(op, "==")) {
138                         *consumed = 3;
139                         return !strcmp(a1, a2);
140                 }
141
142                 if (!strcmp(op, "!=")) {
143                         *consumed = 3;
144                         return strcmp(a1, a2);
145                 }
146
147                 if (!strcmp(op, "<")) {
148                         *consumed = 3;
149                         return strcmp(a1, a2) < 0;
150                 }
151
152                 if (!strcmp(op, ">")) {
153                         *consumed = 3;
154                         return strcmp(a1, a2) > 0;
155                 }
156         }
157
158         if (argc >= 2) {
159                 const char *a1;
160
161                 op = argv[0];
162                 a1 = argv[1];
163
164                 if (!strcmp(op, "-z")) {
165                         *consumed = 2;
166                         return strlen(a1) == 0;
167                 }
168
169                 if (!strcmp(op, "-n")) {
170                         *consumed = 2;
171                         return strlen(a1) != 0;
172                 }
173
174                 /* todo: implement file checks */
175                 if (!strcmp(op, "-s") || !strcmp(op, "-f")) {
176                         *consumed = 2;
177                         return false;
178                 }
179         }
180
181         *consumed = 1;
182         return strlen(op) > 0;
183 }
184
185 static int builtin_test(struct grub2_script *script __attribute__((unused)),
186                 void *data __attribute__((unused)),
187                 int argc, char *argv[])
188 {
189         int consumed;
190         bool not, rc;
191
192         if (!strcmp(argv[0], "[") && !strcmp(argv[argc - 1], "]"))
193                 argc--;
194
195         /* skip command name */
196         argc--;
197         argv++;
198
199         not = false;
200         rc = false;
201
202         for (consumed = 0; argc > 0; argv += consumed, argc -= consumed) {
203
204                 if (!strcmp(argv[0], "!")) {
205                         not = true;
206                         consumed = 1;
207                         continue;
208                 }
209
210                 if (!strcmp(argv[0], "-a")) {
211                         if (!rc)
212                                 return 1;
213                         consumed = 1;
214                         continue;
215                 }
216
217                 if (!strcmp(argv[0], "-o")) {
218                         if (rc)
219                                 return 0;
220                         consumed = 1;
221                         continue;
222                 }
223
224                 rc = builtin_test_op(argc, argv, &consumed);
225                 if (not) {
226                         rc = !rc;
227                         not = false;
228                 }
229         }
230
231         return rc ? 0 : 1;
232 }
233
234 static int builtin_nop(struct grub2_script *script __attribute__((unused)),
235                 void *data __attribute__((unused)),
236                 int argc __attribute__((unused)),
237                 char *argv[] __attribute__((unused)))
238 {
239         return 0;
240 }
241
242 static struct {
243         const char *name;
244         grub2_function fn;
245 } builtins[] = {
246         {
247                 .name = "set",
248                 .fn = builtin_set,
249         },
250         {
251                 .name = "linux",
252                 .fn = builtin_linux,
253         },
254         {
255                 .name = "initrd",
256                 .fn = builtin_initrd,
257         },
258         {
259                 .name = "search",
260                 .fn = builtin_search,
261         },
262         {
263                 .name = "[",
264                 .fn = builtin_test,
265         },
266         {
267                 .name = "test",
268                 .fn = builtin_test,
269         },
270 };
271
272 static const char *nops[] = {
273         "echo", "export", "insmod", "loadfont", "terminfo",
274 };
275
276 void register_builtins(struct grub2_script *script)
277 {
278         unsigned int i;
279
280         for (i = 0; i < ARRAY_SIZE(builtins); i++)
281                 script_register_function(script, builtins[i].name,
282                                 builtins[i].fn, NULL);
283
284         for (i = 0; i < ARRAY_SIZE(nops); i++)
285                 script_register_function(script, nops[i], builtin_nop, NULL);
286 }