changed zfs files naming to (hdX,Y)/filesystem@@file
[grub2/phcoder.git] / commands / minicmd.c
blobb31438844a6844715dc2f6321ad0d46d5c54a950
1 /* minicmd.c - commands for the rescue mode */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2006,2007,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/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>
31 /* cat FILE */
32 static grub_err_t
33 grub_mini_cmd_cat (struct grub_command *cmd __attribute__ ((unused)),
34 int argc, char *argv[])
36 grub_file_t file;
37 char buf[GRUB_DISK_SECTOR_SIZE];
38 grub_ssize_t size;
40 if (argc < 1)
41 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
43 file = grub_file_open (argv[0]);
44 if (! file)
45 return grub_errno;
47 while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
49 int i;
51 for (i = 0; i < size; i++)
53 unsigned char c = buf[i];
55 if ((grub_isprint (c) || grub_isspace (c)) && c != '\r')
56 grub_putchar (c);
57 else
59 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
60 grub_printf ("<%x>", (int) c);
61 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
66 grub_putchar ('\n');
67 grub_refresh ();
68 grub_file_close (file);
70 return 0;
73 /* help */
74 static grub_err_t
75 grub_mini_cmd_help (struct grub_command *cmd __attribute__ ((unused)),
76 int argc __attribute__ ((unused)),
77 char *argv[] __attribute__ ((unused)))
79 grub_command_t p;
81 for (p = grub_command_list; p; p = p->next)
82 grub_printf ("%s (%d%c)\t%s\n", p->name,
83 p->prio & GRUB_PRIO_LIST_PRIO_MASK,
84 (p->prio & GRUB_PRIO_LIST_FLAG_ACTIVE) ? '+' : '-',
85 p->description);
87 return 0;
90 #if 0
91 static void
92 grub_rescue_cmd_info (void)
94 extern void grub_disk_cache_get_performance (unsigned long *,
95 unsigned long *);
96 unsigned long hits, misses;
98 grub_disk_cache_get_performance (&hits, &misses);
99 grub_printf ("Disk cache: hits = %u, misses = %u ", hits, misses);
100 if (hits + misses)
102 unsigned long ratio = hits * 10000 / (hits + misses);
103 grub_printf ("(%u.%u%%)\n", ratio / 100, ratio % 100);
105 else
106 grub_printf ("(N/A)\n");
108 #endif
110 /* root [DEVICE] */
111 static grub_err_t
112 grub_mini_cmd_root (struct grub_command *cmd __attribute__ ((unused)),
113 int argc, char *argv[])
115 grub_device_t dev;
116 grub_fs_t fs;
118 if (argc > 0)
120 char *device_name = grub_file_get_device_name (argv[0]);
121 if (! device_name)
122 return grub_errno;
124 grub_env_set ("root", device_name);
125 grub_free (device_name);
128 dev = grub_device_open (0);
129 if (! dev)
130 return grub_errno;
132 fs = grub_fs_probe (dev);
133 if (grub_errno == GRUB_ERR_UNKNOWN_FS)
134 grub_errno = GRUB_ERR_NONE;
136 grub_printf ("(%s): Filesystem is %s.\n",
137 grub_env_get ("root"), fs ? fs->name : "unknown");
139 grub_device_close (dev);
141 return 0;
144 #if 0
145 static void
146 grub_rescue_cmd_testload (int argc, char *argv[])
148 grub_file_t file;
149 char *buf;
150 grub_ssize_t size;
151 grub_ssize_t pos;
152 auto void read_func (unsigned long sector, unsigned offset, unsigned len);
154 void read_func (unsigned long sector __attribute__ ((unused)),
155 unsigned offset __attribute__ ((unused)),
156 unsigned len __attribute__ ((unused)))
158 grub_putchar ('.');
159 grub_refresh ();
162 if (argc < 1)
164 grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
165 return;
168 file = grub_file_open (argv[0]);
169 if (! file)
170 return;
172 size = grub_file_size (file) & ~(GRUB_DISK_SECTOR_SIZE - 1);
173 if (size == 0)
175 grub_file_close (file);
176 return;
179 buf = grub_malloc (size);
180 if (! buf)
181 goto fail;
183 grub_printf ("Reading %s sequentially", argv[0]);
184 file->read_hook = read_func;
185 if (grub_file_read (file, buf, size) != size)
186 goto fail;
187 grub_printf (" Done.\n");
189 /* Read sequentially again. */
190 grub_printf ("Reading %s sequentially again", argv[0]);
191 if (grub_file_seek (file, 0) < 0)
192 goto fail;
194 for (pos = 0; pos < size; pos += GRUB_DISK_SECTOR_SIZE)
196 char sector[GRUB_DISK_SECTOR_SIZE];
198 if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
199 != GRUB_DISK_SECTOR_SIZE)
200 goto fail;
202 if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
204 grub_printf ("\nDiffers in %d\n", pos);
205 goto fail;
208 grub_printf (" Done.\n");
210 /* Read backwards and compare. */
211 grub_printf ("Reading %s backwards", argv[0]);
212 pos = size;
213 while (pos > 0)
215 char sector[GRUB_DISK_SECTOR_SIZE];
217 pos -= GRUB_DISK_SECTOR_SIZE;
219 if (grub_file_seek (file, pos) < 0)
220 goto fail;
222 if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
223 != GRUB_DISK_SECTOR_SIZE)
224 goto fail;
226 if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
228 int i;
230 grub_printf ("\nDiffers in %d\n", pos);
232 for (i = 0; i < GRUB_DISK_SECTOR_SIZE; i++)
233 grub_putchar (buf[pos + i]);
235 if (i)
236 grub_refresh ();
238 goto fail;
241 grub_printf (" Done.\n");
243 fail:
245 grub_file_close (file);
246 grub_free (buf);
248 #endif
250 /* dump ADDRESS [SIZE] */
251 static grub_err_t
252 grub_mini_cmd_dump (struct grub_command *cmd __attribute__ ((unused)),
253 int argc, char *argv[])
255 grub_uint8_t *addr;
256 grub_size_t size = 4;
258 if (argc == 0)
259 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no address specified");
261 addr = (grub_uint8_t *) grub_strtoul (argv[0], 0, 0);
262 if (grub_errno)
263 return grub_errno;
265 if (argc > 1)
266 size = (grub_size_t) grub_strtoul (argv[1], 0, 0);
268 while (size--)
270 grub_printf ("%x%x ", *addr >> 4, *addr & 0xf);
271 addr++;
274 return 0;
277 /* rmmod MODULE */
278 static grub_err_t
279 grub_mini_cmd_rmmod (struct grub_command *cmd __attribute__ ((unused)),
280 int argc, char *argv[])
282 grub_dl_t mod;
284 if (argc == 0)
285 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
287 mod = grub_dl_get (argv[0]);
288 if (! mod)
289 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no such module");
291 if (grub_dl_unref (mod) <= 0)
292 grub_dl_unload (mod);
294 return 0;
297 /* lsmod */
298 static grub_err_t
299 grub_mini_cmd_lsmod (struct grub_command *cmd __attribute__ ((unused)),
300 int argc __attribute__ ((unused)),
301 char *argv[] __attribute__ ((unused)))
303 auto int print_module (grub_dl_t mod);
305 int print_module (grub_dl_t mod)
307 grub_dl_dep_t dep;
309 grub_printf ("%s\t%d\t\t", mod->name, mod->ref_count);
310 for (dep = mod->dep; dep; dep = dep->next)
312 if (dep != mod->dep)
313 grub_putchar (',');
315 grub_printf ("%s", dep->mod->name);
317 grub_putchar ('\n');
318 grub_refresh ();
320 return 0;
323 grub_printf ("Name\tRef Count\tDependencies\n");
324 grub_dl_iterate (print_module);
326 return 0;
329 /* exit */
330 static grub_err_t
331 grub_mini_cmd_exit (struct grub_command *cmd __attribute__ ((unused)),
332 int argc __attribute__ ((unused)),
333 char *argv[] __attribute__ ((unused)))
335 grub_exit ();
336 return 0;
339 static grub_command_t cmd_cat, cmd_help, cmd_root;
340 static grub_command_t cmd_dump, cmd_rmmod, cmd_lsmod, cmd_exit;
342 GRUB_MOD_INIT(minicmd)
344 cmd_cat =
345 grub_register_command ("cat", grub_mini_cmd_cat,
346 "cat FILE", "show the contents of a file");
347 cmd_help =
348 grub_register_command ("help", grub_mini_cmd_help,
349 0, "show this message");
350 cmd_root =
351 grub_register_command ("root", grub_mini_cmd_root,
352 "root [DEVICE]", "set the root device");
353 cmd_dump =
354 grub_register_command ("dump", grub_mini_cmd_dump,
355 "dump ADDR", "dump memory");
356 cmd_rmmod =
357 grub_register_command ("rmmod", grub_mini_cmd_rmmod,
358 "rmmod MODULE", "remove a module");
359 cmd_lsmod =
360 grub_register_command ("lsmod", grub_mini_cmd_lsmod,
361 0, "show loaded modules");
362 cmd_exit =
363 grub_register_command ("exit", grub_mini_cmd_exit,
364 0, "exit from GRUB");
367 GRUB_MOD_FINI(minicmd)
369 grub_unregister_command (cmd_cat);
370 grub_unregister_command (cmd_help);
371 grub_unregister_command (cmd_root);
372 grub_unregister_command (cmd_dump);
373 grub_unregister_command (cmd_rmmod);
374 grub_unregister_command (cmd_lsmod);
375 grub_unregister_command (cmd_exit);