]> git.ozlabs.org Git - petitboot/blob - discover/grub2/builtins.c
ui/ncurses: Use pmenu_item destrutor to free ncurses ITEMs
[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 <util/util.h>
9
10 #include "discover/parser.h"
11 #include "grub2.h"
12
13
14 static int builtin_set(struct grub2_script *script,
15                 void *data __attribute__((unused)),
16                 int argc, char *argv[])
17 {
18         char *name, *value, *p;
19         int i;
20
21         if (argc < 2)
22                 return -1;
23
24         p = strchr(argv[1], '=');
25         if (!p)
26                 return -1;
27
28         name = talloc_strndup(script, argv[1], p - argv[1]);
29         value = talloc_strdup(script, p+1);
30
31         for (i = 2; i < argc; i++)
32                 value = talloc_asprintf_append(value, " %s", argv[i]);
33
34         script_env_set(script, name, value);
35
36         return 0;
37 }
38
39 static int builtin_linux(struct grub2_script *script,
40                 void *data __attribute__((unused)),
41                 int argc, char *argv[])
42 {
43         struct discover_boot_option *opt = script->opt;
44         const char *root;
45         int i;
46
47         if (!opt) {
48                 pb_log("grub2 syntax error: 'linux' statement outside "
49                                 "a menuentry.\n");
50                 return -1;
51         }
52
53         if (argc < 2) {
54                 pb_log("grub2 syntax error: no filename provided to "
55                                 "linux statement\n");
56                 return -1;
57         }
58
59         root = script_env_get(script, "root");
60
61         opt->boot_image = create_grub2_resource(opt, script->ctx->device,
62                                                 root, argv[1]);
63         opt->option->boot_args = NULL;
64
65         if (argc > 2)
66                 opt->option->boot_args = talloc_strdup(opt, argv[2]);
67
68         for (i = 3; i < argc; i++)
69                 opt->option->boot_args = talloc_asprintf_append(
70                                                 opt->option->boot_args,
71                                                 " %s", argv[i]);
72         return 0;
73 }
74
75 static int builtin_initrd(struct grub2_script *script,
76                 void *data __attribute__((unused)),
77                 int argc, char *argv[])
78 {
79         struct discover_boot_option *opt = script->opt;
80         const char *root;
81
82         if (!opt) {
83                 pb_log("grub2 syntax error: 'initrd' statement outside "
84                                 "a menuentry.\n");
85                 return -1;
86         }
87
88         if (argc < 2) {
89                 pb_log("grub2 syntax error: no filename provided to "
90                                 "initrd statement\n");
91                 return -1;
92         }
93
94         root = script_env_get(script, "root");
95         opt->initrd = create_grub2_resource(opt, script->ctx->device,
96                                                 root, argv[1]);
97
98         return 0;
99 }
100
101 static int builtin_search(struct grub2_script *script,
102                 void *data __attribute__((unused)),
103                 int argc, char *argv[])
104 {
105         const char *env_var, *spec;
106         int i;
107
108         env_var = NULL;
109
110         for (i = 1; i < argc - 1; i++) {
111                 if (!strncmp(argv[i], "--set=", strlen("--set="))) {
112                         env_var = argv[i] + strlen("--set=");
113                         break;
114                 }
115         }
116
117         if (!env_var)
118                 return 0;
119
120         spec = argv[argc - 1];
121
122         script_env_set(script, env_var, spec);
123
124         return 0;
125 }
126
127 static bool builtin_test_op_file(struct grub2_script *script, char op,
128                 const char *file)
129 {
130         bool result;
131         int len, rc;
132         char *buf;
133
134         rc = parser_request_file(script->ctx, script->ctx->device,
135                         file, &buf, &len);
136         if (rc)
137                 return false;
138
139         switch (op) {
140         case 's':
141                 /* -s: return true if file exists and has non-zero size */
142                 result = len > 0;
143                 break;
144         case 'f':
145                 /* -f: return true if file exists */
146                 result = true;
147                 break;
148         default:
149                 result = false;
150
151         }
152
153         talloc_free(buf);
154         return result;
155 }
156
157 static bool builtin_test_op_dir(struct grub2_script *script, char op,
158                 const char *dir)
159 {
160         if (op != 'd')
161                 return false;
162
163         return parser_check_dir(script->ctx, script->ctx->device, dir) == 0;
164 }
165
166 static bool builtin_test_op(struct grub2_script *script,
167                 int argc, char **argv, int *consumed)
168 {
169         char *op;
170
171         if (argc >= 3) {
172                 const char *a1, *a2;
173
174                 a1 = argv[0];
175                 op = argv[1];
176                 a2 = argv[2];
177
178                 if (!strcmp(op, "=") || !strcmp(op, "==")) {
179                         *consumed = 3;
180                         return !strcmp(a1, a2);
181                 }
182
183                 if (!strcmp(op, "!=")) {
184                         *consumed = 3;
185                         return strcmp(a1, a2);
186                 }
187
188                 if (!strcmp(op, "<")) {
189                         *consumed = 3;
190                         return strcmp(a1, a2) < 0;
191                 }
192
193                 if (!strcmp(op, ">")) {
194                         *consumed = 3;
195                         return strcmp(a1, a2) > 0;
196                 }
197         }
198
199         if (argc >= 2) {
200                 const char *a1;
201
202                 op = argv[0];
203                 a1 = argv[1];
204
205                 if (!strcmp(op, "-z")) {
206                         *consumed = 2;
207                         return strlen(a1) == 0;
208                 }
209
210                 if (!strcmp(op, "-n")) {
211                         *consumed = 2;
212                         return strlen(a1) != 0;
213                 }
214
215                 if (!strcmp(op, "-s") || !strcmp(op, "-f")) {
216                         *consumed = 2;
217                         return builtin_test_op_file(script, op[1], a1);
218                 }
219
220                 if (!strcmp(op, "-d")) {
221                         *consumed = 2;
222                         return builtin_test_op_dir(script, op[1], a1);
223                 }
224         }
225
226         op = argv[0];
227         *consumed = 1;
228         return strlen(op) > 0;
229 }
230
231 static int builtin_test(struct grub2_script *script,
232                 void *data __attribute__((unused)),
233                 int argc, char *argv[])
234 {
235         int consumed;
236         bool not, rc;
237
238         if (!strcmp(argv[0], "[") && !strcmp(argv[argc - 1], "]"))
239                 argc--;
240
241         /* skip command name */
242         argc--;
243         argv++;
244
245         not = false;
246         rc = false;
247
248         for (consumed = 0; argc > 0; argv += consumed, argc -= consumed) {
249
250                 if (!strcmp(argv[0], "!")) {
251                         not = true;
252                         consumed = 1;
253                         continue;
254                 }
255
256                 if (!strcmp(argv[0], "-a")) {
257                         if (!rc)
258                                 return 1;
259                         consumed = 1;
260                         continue;
261                 }
262
263                 if (!strcmp(argv[0], "-o")) {
264                         if (rc)
265                                 return 0;
266                         consumed = 1;
267                         continue;
268                 }
269
270                 rc = builtin_test_op(script, argc, argv, &consumed);
271                 if (not) {
272                         rc = !rc;
273                         not = false;
274                 }
275         }
276
277         return rc ? 0 : 1;
278 }
279
280 static int builtin_true(struct grub2_script *script __attribute__((unused)),
281                 void *data __attribute__((unused)),
282                 int argc __attribute__((unused)),
283                 char *argv[] __attribute__((unused)))
284 {
285         return 0;
286 }
287
288 static int builtin_false(struct grub2_script *script __attribute__((unused)),
289                 void *data __attribute__((unused)),
290                 int argc __attribute__((unused)),
291                 char *argv[] __attribute__((unused)))
292 {
293         return 1;
294 }
295
296 static int builtin_nop(struct grub2_script *script __attribute__((unused)),
297                 void *data __attribute__((unused)),
298                 int argc __attribute__((unused)),
299                 char *argv[] __attribute__((unused)))
300 {
301         return 0;
302 }
303
304 extern int builtin_load_env(struct grub2_script *script,
305                 void *data __attribute__((unused)),
306                 int argc, char *argv[]);
307 int builtin_save_env(struct grub2_script *script,
308                 void *data __attribute__((unused)),
309                 int argc, char *argv[]);
310
311
312 static struct {
313         const char *name;
314         grub2_function fn;
315 } builtins[] = {
316         {
317                 .name = "set",
318                 .fn = builtin_set,
319         },
320         {
321                 .name = "linux",
322                 .fn = builtin_linux,
323         },
324         {
325                 .name = "linux16",
326                 .fn = builtin_linux,
327         },
328         {
329                 .name = "initrd",
330                 .fn = builtin_initrd,
331         },
332         {
333                 .name = "search",
334                 .fn = builtin_search,
335         },
336         {
337                 .name = "[",
338                 .fn = builtin_test,
339         },
340         {
341                 .name = "test",
342                 .fn = builtin_test,
343         },
344         {
345                 .name = "true",
346                 .fn = builtin_true,
347         },
348         {
349                 .name = "false",
350                 .fn = builtin_false,
351         },
352         {
353                 .name = "load_env",
354                 .fn = builtin_load_env,
355         },
356         {
357                 .name = "save_env",
358                 .fn = builtin_save_env,
359         },
360 };
361
362 static const char *nops[] = {
363         "echo", "export", "insmod", "loadfont", "terminfo",
364 };
365
366 void register_builtins(struct grub2_script *script)
367 {
368         unsigned int i;
369
370         for (i = 0; i < ARRAY_SIZE(builtins); i++)
371                 script_register_function(script, builtins[i].name,
372                                 builtins[i].fn, NULL);
373
374         for (i = 0; i < ARRAY_SIZE(nops); i++)
375                 script_register_function(script, nops[i], builtin_nop, NULL);
376 }