minicmd.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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, N_("filename expected"));
  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_COMMAND_PRIO_MASK,
  75. (p->prio & GRUB_COMMAND_FLAG_ACTIVE) ? '+' : '-',
  76. p->description);
  77. return 0;
  78. }
  79. /* dump ADDRESS [SIZE] */
  80. static grub_err_t
  81. grub_mini_cmd_dump (struct grub_command *cmd __attribute__ ((unused)),
  82. int argc, char *argv[])
  83. {
  84. grub_uint8_t *addr;
  85. grub_size_t size = 4;
  86. if (argc == 0)
  87. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no address specified");
  88. #if GRUB_CPU_SIZEOF_VOID_P == GRUB_CPU_SIZEOF_LONG
  89. #define grub_strtoaddr grub_strtoul
  90. #else
  91. #define grub_strtoaddr grub_strtoull
  92. #endif
  93. addr = (grub_uint8_t *) grub_strtoaddr (argv[0], 0, 0);
  94. if (grub_errno)
  95. return grub_errno;
  96. if (argc > 1)
  97. size = (grub_size_t) grub_strtoaddr (argv[1], 0, 0);
  98. while (size--)
  99. {
  100. grub_printf ("%x%x ", *addr >> 4, *addr & 0xf);
  101. addr++;
  102. }
  103. return 0;
  104. }
  105. /* rmmod MODULE */
  106. static grub_err_t
  107. grub_mini_cmd_rmmod (struct grub_command *cmd __attribute__ ((unused)),
  108. int argc, char *argv[])
  109. {
  110. grub_dl_t mod;
  111. if (argc == 0)
  112. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
  113. mod = grub_dl_get (argv[0]);
  114. if (! mod)
  115. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no such module");
  116. if (grub_dl_unref (mod) <= 0)
  117. grub_dl_unload (mod);
  118. return 0;
  119. }
  120. /* lsmod */
  121. static grub_err_t
  122. grub_mini_cmd_lsmod (struct grub_command *cmd __attribute__ ((unused)),
  123. int argc __attribute__ ((unused)),
  124. char *argv[] __attribute__ ((unused)))
  125. {
  126. grub_dl_t mod;
  127. /* TRANSLATORS: this is module list header. Name
  128. is module name, Ref Count is a reference counter
  129. (how many modules or open descriptors use it).
  130. Dependencies are the other modules it uses.
  131. */
  132. grub_printf_ (N_("Name\tRef Count\tDependencies\n"));
  133. FOR_DL_MODULES (mod)
  134. {
  135. grub_dl_dep_t dep;
  136. grub_printf ("%s\t%d\t\t", mod->name, mod->ref_count);
  137. for (dep = mod->dep; dep; dep = dep->next)
  138. {
  139. if (dep != mod->dep)
  140. grub_xputs (",");
  141. grub_printf ("%s", dep->mod->name);
  142. }
  143. grub_xputs ("\n");
  144. }
  145. return 0;
  146. }
  147. /* exit */
  148. static grub_err_t __attribute__ ((noreturn))
  149. grub_mini_cmd_exit (struct grub_command *cmd __attribute__ ((unused)),
  150. int argc __attribute__ ((unused)),
  151. char *argv[] __attribute__ ((unused)))
  152. {
  153. grub_exit ();
  154. /* Not reached. */
  155. }
  156. static grub_command_t cmd_cat, cmd_help;
  157. static grub_command_t cmd_dump, cmd_rmmod, cmd_lsmod, cmd_exit;
  158. GRUB_MOD_INIT(minicmd)
  159. {
  160. cmd_cat =
  161. grub_register_command ("cat", grub_mini_cmd_cat,
  162. N_("FILE"), N_("Show the contents of a file."));
  163. cmd_help =
  164. grub_register_command ("help", grub_mini_cmd_help,
  165. 0, N_("Show this message."));
  166. cmd_dump =
  167. grub_register_command ("dump", grub_mini_cmd_dump,
  168. N_("ADDR [SIZE]"), N_("Show memory contents."));
  169. cmd_rmmod =
  170. grub_register_command ("rmmod", grub_mini_cmd_rmmod,
  171. N_("MODULE"), N_("Remove a module."));
  172. cmd_lsmod =
  173. grub_register_command ("lsmod", grub_mini_cmd_lsmod,
  174. 0, N_("Show loaded modules."));
  175. cmd_exit =
  176. grub_register_command ("exit", grub_mini_cmd_exit,
  177. 0, N_("Exit from GRUB."));
  178. }
  179. GRUB_MOD_FINI(minicmd)
  180. {
  181. grub_unregister_command (cmd_cat);
  182. grub_unregister_command (cmd_help);
  183. grub_unregister_command (cmd_dump);
  184. grub_unregister_command (cmd_rmmod);
  185. grub_unregister_command (cmd_lsmod);
  186. grub_unregister_command (cmd_exit);
  187. }