6 #include <types/types.h>
7 #include <talloc/talloc.h>
10 #include "discover/parser.h"
14 static int builtin_set(struct grub2_script *script,
15 void *data __attribute__((unused)),
16 int argc, char *argv[])
18 char *name, *value, *p;
24 p = strchr(argv[1], '=');
28 name = talloc_strndup(script, argv[1], p - argv[1]);
29 value = talloc_strdup(script, p+1);
31 for (i = 2; i < argc; i++)
32 value = talloc_asprintf_append(value, " %s", argv[i]);
34 script_env_set(script, name, value);
39 static int builtin_linux(struct grub2_script *script,
40 void *data __attribute__((unused)),
41 int argc, char *argv[])
43 struct discover_boot_option *opt = script->opt;
48 pb_log("grub2 syntax error: 'linux' statement outside "
54 pb_log("grub2 syntax error: no filename provided to "
59 root = script_env_get(script, "root");
61 opt->boot_image = create_grub2_resource(opt, script->ctx->device,
63 opt->option->boot_args = NULL;
66 opt->option->boot_args = talloc_strdup(opt, argv[2]);
68 for (i = 3; i < argc; i++)
69 opt->option->boot_args = talloc_asprintf_append(
70 opt->option->boot_args,
75 static int builtin_initrd(struct grub2_script *script,
76 void *data __attribute__((unused)),
77 int argc, char *argv[])
79 struct discover_boot_option *opt = script->opt;
83 pb_log("grub2 syntax error: 'initrd' statement outside "
89 pb_log("grub2 syntax error: no filename provided to "
90 "initrd statement\n");
94 root = script_env_get(script, "root");
95 opt->initrd = create_grub2_resource(opt, script->ctx->device,
101 static int builtin_search(struct grub2_script *script,
102 void *data __attribute__((unused)),
103 int argc, char *argv[])
105 const char *env_var, *spec;
110 for (i = 1; i < argc - 1; i++) {
111 if (!strncmp(argv[i], "--set=", strlen("--set="))) {
112 env_var = argv[i] + strlen("--set=");
120 spec = argv[argc - 1];
122 script_env_set(script, env_var, spec);
127 static bool builtin_test_op_file(struct grub2_script *script, char op,
134 rc = parser_request_file(script->ctx, script->ctx->device,
141 /* -s: return true if file exists and has non-zero size */
145 /* -f: return true if file exists */
157 static bool builtin_test_op_dir(struct grub2_script *script, char op,
163 return parser_check_dir(script->ctx, script->ctx->device, dir) == 0;
166 static bool builtin_test_op(struct grub2_script *script,
167 int argc, char **argv, int *consumed)
178 if (!strcmp(op, "=") || !strcmp(op, "==")) {
180 return !strcmp(a1, a2);
183 if (!strcmp(op, "!=")) {
185 return strcmp(a1, a2);
188 if (!strcmp(op, "<")) {
190 return strcmp(a1, a2) < 0;
193 if (!strcmp(op, ">")) {
195 return strcmp(a1, a2) > 0;
205 if (!strcmp(op, "-z")) {
207 return strlen(a1) == 0;
210 if (!strcmp(op, "-n")) {
212 return strlen(a1) != 0;
215 if (!strcmp(op, "-s") || !strcmp(op, "-f")) {
217 return builtin_test_op_file(script, op[1], a1);
220 if (!strcmp(op, "-d")) {
222 return builtin_test_op_dir(script, op[1], a1);
228 return strlen(op) > 0;
231 static int builtin_test(struct grub2_script *script,
232 void *data __attribute__((unused)),
233 int argc, char *argv[])
238 if (!strcmp(argv[0], "[") && !strcmp(argv[argc - 1], "]"))
241 /* skip command name */
248 for (consumed = 0; argc > 0; argv += consumed, argc -= consumed) {
250 if (!strcmp(argv[0], "!")) {
256 if (!strcmp(argv[0], "-a")) {
263 if (!strcmp(argv[0], "-o")) {
270 rc = builtin_test_op(script, argc, argv, &consumed);
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)))
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)))
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)))
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[]);
330 .fn = builtin_initrd,
334 .fn = builtin_search,
354 .fn = builtin_load_env,
358 .fn = builtin_save_env,
362 static const char *nops[] = {
363 "echo", "export", "insmod", "loadfont", "terminfo",
366 void register_builtins(struct grub2_script *script)
370 for (i = 0; i < ARRAY_SIZE(builtins); i++)
371 script_register_function(script, builtins[i].name,
372 builtins[i].fn, NULL);
374 for (i = 0; i < ARRAY_SIZE(nops); i++)
375 script_register_function(script, nops[i], builtin_nop, NULL);