minicmd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.
  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. #include <grub/dl.h>
  20. #include <grub/mm.h>
  21. #include <grub/err.h>
  22. #include <grub/env.h>
  23. #include <grub/misc.h>
  24. #include <grub/file.h>
  25. #include <grub/disk.h>
  26. #include <grub/term.h>
  27. #include <grub/loader.h>
  28. #include <grub/command.h>
  29. #include <grub/i18n.h>
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. /* cat FILE */
  32. static grub_err_t
  33. grub_mini_cmd_cat (struct grub_command *cmd __attribute__ ((unused)),
  34. int argc, char *argv[])
  35. {
  36. grub_file_t file;
  37. char buf[GRUB_DISK_SECTOR_SIZE];
  38. grub_ssize_t size;
  39. if (argc < 1)
  40. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
  41. file = grub_file_open (argv[0]);
  42. if (! file)
  43. return grub_errno;
  44. while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
  45. {
  46. int i;
  47. for (i = 0; i < size; i++)
  48. {
  49. unsigned char c = buf[i];
  50. if ((grub_isprint (c) || grub_isspace (c)) && c != '\r')
  51. grub_printf ("%c", c);
  52. else
  53. {
  54. grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
  55. grub_printf ("<%x>", (int) c);
  56. grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
  57. }
  58. }
  59. }
  60. grub_xputs ("\n");
  61. grub_refresh ();
  62. grub_file_close (file);
  63. return 0;
  64. }
  65. /* help */
  66. static grub_err_t
  67. grub_mini_cmd_help (struct grub_command *cmd __attribute__ ((unused)),
  68. int argc __attribute__ ((unused)),
  69. char *argv[] __attribute__ ((unused)))
  70. {
  71. grub_command_t p;
  72. for (p = grub_command_list; p; p = p->next)
  73. grub_printf ("%s (%d%c)\t%s\n", p->name,
  74. p->prio & GRUB_PRIO_LIST_PRIO_MASK,
  75. (p->prio & GRUB_PRIO_LIST_FLAG_ACTIVE) ? '+' : '-',
  76. p->description);
  77. return 0;
  78. }
  79. #if 0
  80. static void
  81. grub_rescue_cmd_info (void)
  82. {
  83. extern void grub_disk_cache_get_performance (unsigned long *,
  84. unsigned long *);
  85. unsigned long hits, misses;
  86. grub_disk_cache_get_performance (&hits, &misses);
  87. grub_printf ("Disk cache: hits = %u, misses = %u ", hits, misses);
  88. if (hits + misses)
  89. {
  90. unsigned long ratio = hits * 10000 / (hits + misses);
  91. grub_printf ("(%u.%u%%)\n", ratio / 100, ratio % 100);
  92. }
  93. else
  94. grub_printf ("(N/A)\n");
  95. }
  96. #endif
  97. /* dump ADDRESS [SIZE] */
  98. static grub_err_t
  99. grub_mini_cmd_dump (struct grub_command *cmd __attribute__ ((unused)),
  100. int argc, char *argv[])
  101. {
  102. grub_uint8_t *addr;
  103. grub_size_t size = 4;
  104. if (argc == 0)
  105. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no address specified");
  106. addr = (grub_uint8_t *) grub_strtoul (argv[0], 0, 0);
  107. if (grub_errno)
  108. return grub_errno;
  109. if (argc > 1)
  110. size = (grub_size_t) grub_strtoul (argv[1], 0, 0);
  111. while (size--)
  112. {
  113. grub_printf ("%x%x ", *addr >> 4, *addr & 0xf);
  114. addr++;
  115. }
  116. return 0;
  117. }
  118. /* rmmod MODULE */
  119. static grub_err_t
  120. grub_mini_cmd_rmmod (struct grub_command *cmd __attribute__ ((unused)),
  121. int argc, char *argv[])
  122. {
  123. grub_dl_t mod;
  124. if (argc == 0)
  125. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
  126. mod = grub_dl_get (argv[0]);
  127. if (! mod)
  128. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no such module");
  129. if (grub_dl_unref (mod) <= 0)
  130. grub_dl_unload (mod);
  131. return 0;
  132. }
  133. /* lsmod */
  134. static grub_err_t
  135. grub_mini_cmd_lsmod (struct grub_command *cmd __attribute__ ((unused)),
  136. int argc __attribute__ ((unused)),
  137. char *argv[] __attribute__ ((unused)))
  138. {
  139. grub_dl_t mod;
  140. grub_printf ("Name\tRef Count\tDependencies\n");
  141. FOR_DL_MODULES (mod)
  142. {
  143. grub_dl_dep_t dep;
  144. grub_printf ("%s\t%d\t\t", mod->name, mod->ref_count);
  145. for (dep = mod->dep; dep; dep = dep->next)
  146. {
  147. if (dep != mod->dep)
  148. grub_xputs (",");
  149. grub_printf ("%s", dep->mod->name);
  150. }
  151. grub_xputs ("\n");
  152. }
  153. return 0;
  154. }
  155. /* exit */
  156. static grub_err_t
  157. grub_mini_cmd_exit (struct grub_command *cmd __attribute__ ((unused)),
  158. int argc __attribute__ ((unused)),
  159. char *argv[] __attribute__ ((unused)))
  160. {
  161. grub_exit ();
  162. return 0;
  163. }
  164. static grub_command_t cmd_cat, cmd_help;
  165. static grub_command_t cmd_dump, cmd_rmmod, cmd_lsmod, cmd_exit;
  166. GRUB_MOD_INIT(minicmd)
  167. {
  168. cmd_cat =
  169. grub_register_command ("cat", grub_mini_cmd_cat,
  170. N_("FILE"), N_("Show the contents of a file."));
  171. cmd_help =
  172. grub_register_command ("help", grub_mini_cmd_help,
  173. 0, N_("Show this message."));
  174. cmd_dump =
  175. grub_register_command ("dump", grub_mini_cmd_dump,
  176. N_("ADDR"), N_("Dump memory."));
  177. cmd_rmmod =
  178. grub_register_command ("rmmod", grub_mini_cmd_rmmod,
  179. N_("MODULE"), N_("Remove a module."));
  180. cmd_lsmod =
  181. grub_register_command ("lsmod", grub_mini_cmd_lsmod,
  182. 0, N_("Show loaded modules."));
  183. cmd_exit =
  184. grub_register_command ("exit", grub_mini_cmd_exit,
  185. 0, N_("Exit from GRUB."));
  186. }
  187. GRUB_MOD_FINI(minicmd)
  188. {
  189. grub_unregister_command (cmd_cat);
  190. grub_unregister_command (cmd_help);
  191. grub_unregister_command (cmd_dump);
  192. grub_unregister_command (cmd_rmmod);
  193. grub_unregister_command (cmd_lsmod);
  194. grub_unregister_command (cmd_exit);
  195. }