]> git.proxmox.com Git - grub2.git/blame - grub-core/commands/minicmd.c
bump version to 2.06-13+pmx2
[grub2.git] / grub-core / commands / minicmd.c
CommitLineData
b1b797cb 1/* minicmd.c - commands for the rescue mode */
2/*
3 * GRUB -- GRand Unified Bootloader
0d5d5653 4 * Copyright (C) 2003,2005,2006,2007,2009 Free Software Foundation, Inc.
b1b797cb 5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <grub/dl.h>
21#include <grub/mm.h>
22#include <grub/err.h>
23#include <grub/env.h>
24#include <grub/misc.h>
25#include <grub/file.h>
26#include <grub/disk.h>
27#include <grub/term.h>
28#include <grub/loader.h>
29#include <grub/command.h>
77a79592 30#include <grub/i18n.h>
b1b797cb 31
e745cf0c
VS
32GRUB_MOD_LICENSE ("GPLv3+");
33
b1b797cb 34/* cat FILE */
35static grub_err_t
36grub_mini_cmd_cat (struct grub_command *cmd __attribute__ ((unused)),
37 int argc, char *argv[])
38{
39 grub_file_t file;
40 char buf[GRUB_DISK_SECTOR_SIZE];
41 grub_ssize_t size;
42
43 if (argc < 1)
9c4b5c13 44 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
b1b797cb 45
ca0a4f68 46 file = grub_file_open (argv[0], GRUB_FILE_TYPE_CAT);
b1b797cb 47 if (! file)
48 return grub_errno;
49
50 while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
51 {
52 int i;
53
54 for (i = 0; i < size; i++)
55 {
56 unsigned char c = buf[i];
57
58 if ((grub_isprint (c) || grub_isspace (c)) && c != '\r')
dfed5c6b 59 grub_printf ("%c", c);
b1b797cb 60 else
61 {
62 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
63 grub_printf ("<%x>", (int) c);
64 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
65 }
66 }
67 }
68
dfed5c6b 69 grub_xputs ("\n");
b1b797cb 70 grub_refresh ();
71 grub_file_close (file);
72
73 return 0;
74}
75
76/* help */
77static grub_err_t
78grub_mini_cmd_help (struct grub_command *cmd __attribute__ ((unused)),
79 int argc __attribute__ ((unused)),
80 char *argv[] __attribute__ ((unused)))
81{
82 grub_command_t p;
83
84 for (p = grub_command_list; p; p = p->next)
85 grub_printf ("%s (%d%c)\t%s\n", p->name,
ead2a882
VS
86 p->prio & GRUB_COMMAND_PRIO_MASK,
87 (p->prio & GRUB_COMMAND_FLAG_ACTIVE) ? '+' : '-',
b1b797cb 88 p->description);
89
90 return 0;
91}
92
b1b797cb 93/* dump ADDRESS [SIZE] */
94static grub_err_t
95grub_mini_cmd_dump (struct grub_command *cmd __attribute__ ((unused)),
96 int argc, char *argv[])
97{
98 grub_uint8_t *addr;
99 grub_size_t size = 4;
100
101 if (argc == 0)
102 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no address specified");
103
66a07ce2
VS
104#if GRUB_CPU_SIZEOF_VOID_P == GRUB_CPU_SIZEOF_LONG
105#define grub_strtoaddr grub_strtoul
106#else
107#define grub_strtoaddr grub_strtoull
108#endif
109
110 addr = (grub_uint8_t *) grub_strtoaddr (argv[0], 0, 0);
b1b797cb 111 if (grub_errno)
112 return grub_errno;
113
114 if (argc > 1)
66a07ce2 115 size = (grub_size_t) grub_strtoaddr (argv[1], 0, 0);
b1b797cb 116
117 while (size--)
118 {
119 grub_printf ("%x%x ", *addr >> 4, *addr & 0xf);
120 addr++;
121 }
122
123 return 0;
124}
125
126/* rmmod MODULE */
127static grub_err_t
128grub_mini_cmd_rmmod (struct grub_command *cmd __attribute__ ((unused)),
129 int argc, char *argv[])
130{
131 grub_dl_t mod;
132
133 if (argc == 0)
134 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
135
136 mod = grub_dl_get (argv[0]);
137 if (! mod)
138 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no such module");
139
ee7808e2
DK
140 if (grub_dl_is_persistent (mod))
141 return grub_error (GRUB_ERR_BAD_ARGUMENT, "cannot unload persistent module");
142
9e6b789f
JMC
143 if (grub_dl_ref_count (mod) > 1)
144 return grub_error (GRUB_ERR_BAD_ARGUMENT, "cannot unload referenced module");
145
146 grub_dl_unref (mod);
147 grub_dl_unload (mod);
b1b797cb 148
149 return 0;
150}
151
152/* lsmod */
153static grub_err_t
154grub_mini_cmd_lsmod (struct grub_command *cmd __attribute__ ((unused)),
155 int argc __attribute__ ((unused)),
156 char *argv[] __attribute__ ((unused)))
157{
fcaae9ec 158 grub_dl_t mod;
b1b797cb 159
e7d2559b
VS
160 /* TRANSLATORS: this is module list header. Name
161 is module name, Ref Count is a reference counter
162 (how many modules or open descriptors use it).
163 Dependencies are the other modules it uses.
164 */
6e0632e2 165 grub_printf_ (N_("Name\tRef Count\tDependencies\n"));
fcaae9ec
VS
166 FOR_DL_MODULES (mod)
167 {
168 grub_dl_dep_t dep;
169
170 grub_printf ("%s\t%d\t\t", mod->name, mod->ref_count);
171 for (dep = mod->dep; dep; dep = dep->next)
172 {
173 if (dep != mod->dep)
e8d0a8f8 174 grub_xputs (",");
fcaae9ec
VS
175
176 grub_printf ("%s", dep->mod->name);
177 }
e8d0a8f8 178 grub_xputs ("\n");
fcaae9ec 179 }
b1b797cb 180
181 return 0;
182}
183
184/* exit */
02a2bf83 185static grub_err_t __attribute__ ((noreturn))
b1b797cb 186grub_mini_cmd_exit (struct grub_command *cmd __attribute__ ((unused)),
187 int argc __attribute__ ((unused)),
188 char *argv[] __attribute__ ((unused)))
189{
190 grub_exit ();
02a2bf83 191 /* Not reached. */
b1b797cb 192}
193
545b752f 194static grub_command_t cmd_cat, cmd_help;
b1b797cb 195static grub_command_t cmd_dump, cmd_rmmod, cmd_lsmod, cmd_exit;
196
197GRUB_MOD_INIT(minicmd)
198{
b1b797cb 199 cmd_cat =
200 grub_register_command ("cat", grub_mini_cmd_cat,
77a79592 201 N_("FILE"), N_("Show the contents of a file."));
b1b797cb 202 cmd_help =
203 grub_register_command ("help", grub_mini_cmd_help,
77a79592 204 0, N_("Show this message."));
b1b797cb 205 cmd_dump =
206 grub_register_command ("dump", grub_mini_cmd_dump,
9c4b5c13 207 N_("ADDR [SIZE]"), N_("Show memory contents."));
b1b797cb 208 cmd_rmmod =
209 grub_register_command ("rmmod", grub_mini_cmd_rmmod,
77a79592 210 N_("MODULE"), N_("Remove a module."));
b1b797cb 211 cmd_lsmod =
212 grub_register_command ("lsmod", grub_mini_cmd_lsmod,
77a79592 213 0, N_("Show loaded modules."));
b1b797cb 214 cmd_exit =
215 grub_register_command ("exit", grub_mini_cmd_exit,
77a79592 216 0, N_("Exit from GRUB."));
b1b797cb 217}
218
219GRUB_MOD_FINI(minicmd)
220{
b1b797cb 221 grub_unregister_command (cmd_cat);
222 grub_unregister_command (cmd_help);
b1b797cb 223 grub_unregister_command (cmd_dump);
224 grub_unregister_command (cmd_rmmod);
225 grub_unregister_command (cmd_lsmod);
226 grub_unregister_command (cmd_exit);
227}