SECURITY: Add SECURITY file
[grub.git] / grub-core / kern / corecmd.c
blobfc54f43f2b827012cc8757a3171dffb5223ee265
1 /* corecmd.c - critical commands which are registered in kernel */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/mm.h>
21 #include <grub/dl.h>
22 #include <grub/err.h>
23 #include <grub/env.h>
24 #include <grub/misc.h>
25 #include <grub/term.h>
26 #include <grub/file.h>
27 #include <grub/device.h>
28 #include <grub/command.h>
29 #include <grub/i18n.h>
31 /* set ENVVAR=VALUE */
32 static grub_err_t
33 grub_core_cmd_set (struct grub_command *cmd __attribute__ ((unused)),
34 int argc, char *argv[])
36 char *var;
37 char *val;
39 if (argc < 1)
41 struct grub_env_var *env;
42 FOR_SORTED_ENV (env)
43 grub_printf ("%s=%s\n", env->name, grub_env_get (env->name));
44 return 0;
47 var = argv[0];
48 val = grub_strchr (var, '=');
49 if (! val)
50 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not an assignment");
52 val[0] = 0;
53 grub_env_set (var, val + 1);
54 val[0] = '=';
56 return 0;
59 static grub_err_t
60 grub_core_cmd_unset (struct grub_command *cmd __attribute__ ((unused)),
61 int argc, char *argv[])
63 if (argc < 1)
64 return grub_error (GRUB_ERR_BAD_ARGUMENT,
65 N_("one argument expected"));
67 grub_env_unset (argv[0]);
68 return 0;
71 /* insmod MODULE */
72 static grub_err_t
73 grub_core_cmd_insmod (struct grub_command *cmd __attribute__ ((unused)),
74 int argc, char *argv[])
76 grub_dl_t mod;
78 if (argc == 0)
79 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
81 if (argv[0][0] == '/' || argv[0][0] == '(' || argv[0][0] == '+')
82 mod = grub_dl_load_file (argv[0]);
83 else
84 mod = grub_dl_load (argv[0]);
86 if (mod)
87 grub_dl_ref (mod);
89 return 0;
92 static int
93 grub_mini_print_devices (const char *name, void *data __attribute__ ((unused)))
95 grub_printf ("(%s) ", name);
97 return 0;
100 static int
101 grub_mini_print_files (const char *filename,
102 const struct grub_dirhook_info *info,
103 void *data __attribute__ ((unused)))
105 grub_printf ("%s%s ", filename, info->dir ? "/" : "");
107 return 0;
110 /* ls [ARG] */
111 static grub_err_t
112 grub_core_cmd_ls (struct grub_command *cmd __attribute__ ((unused)),
113 int argc, char *argv[])
115 if (argc < 1)
117 grub_device_iterate (grub_mini_print_devices, NULL);
118 grub_xputs ("\n");
119 grub_refresh ();
121 else
123 char *device_name;
124 grub_device_t dev = 0;
125 grub_fs_t fs;
126 char *path;
128 device_name = grub_file_get_device_name (argv[0]);
129 if (grub_errno)
130 goto fail;
131 dev = grub_device_open (device_name);
132 if (! dev)
133 goto fail;
135 fs = grub_fs_probe (dev);
136 path = grub_strchr (argv[0], ')');
137 if (! path)
138 path = argv[0];
139 else
140 path++;
142 if (! *path && ! device_name)
144 grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
145 goto fail;
148 if (! *path)
150 if (grub_errno == GRUB_ERR_UNKNOWN_FS)
151 grub_errno = GRUB_ERR_NONE;
153 grub_printf ("(%s): Filesystem is %s.\n",
154 device_name, fs ? fs->name : "unknown");
156 else if (fs)
158 (fs->fs_dir) (dev, path, grub_mini_print_files, NULL);
159 grub_xputs ("\n");
160 grub_refresh ();
163 fail:
164 if (dev)
165 grub_device_close (dev);
167 grub_free (device_name);
170 return grub_errno;
173 void
174 grub_register_core_commands (void)
176 grub_command_t cmd;
177 cmd = grub_register_command ("set", grub_core_cmd_set,
178 N_("[ENVVAR=VALUE]"),
179 N_("Set an environment variable."));
180 if (cmd)
181 cmd->flags |= GRUB_COMMAND_FLAG_EXTRACTOR;
182 grub_register_command ("unset", grub_core_cmd_unset,
183 N_("ENVVAR"),
184 N_("Remove an environment variable."));
185 grub_register_command ("ls", grub_core_cmd_ls,
186 N_("[ARG]"), N_("List devices or files."));
187 grub_register_command ("insmod", grub_core_cmd_insmod,
188 N_("MODULE"), N_("Insert a module."));