minicmd.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. /* cat FILE */
  31. static grub_err_t
  32. grub_mini_cmd_cat (struct grub_command *cmd __attribute__ ((unused)),
  33. int argc, char *argv[])
  34. {
  35. grub_file_t file;
  36. char buf[GRUB_DISK_SECTOR_SIZE];
  37. grub_ssize_t size;
  38. if (argc < 1)
  39. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
  40. file = grub_file_open (argv[0]);
  41. if (! file)
  42. return grub_errno;
  43. while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
  44. {
  45. int i;
  46. for (i = 0; i < size; i++)
  47. {
  48. unsigned char c = buf[i];
  49. if ((grub_isprint (c) || grub_isspace (c)) && c != '\r')
  50. grub_putchar (c);
  51. else
  52. {
  53. grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
  54. grub_printf ("<%x>", (int) c);
  55. grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
  56. }
  57. }
  58. }
  59. grub_putchar ('\n');
  60. grub_refresh ();
  61. grub_file_close (file);
  62. return 0;
  63. }
  64. /* help */
  65. static grub_err_t
  66. grub_mini_cmd_help (struct grub_command *cmd __attribute__ ((unused)),
  67. int argc __attribute__ ((unused)),
  68. char *argv[] __attribute__ ((unused)))
  69. {
  70. grub_command_t p;
  71. for (p = grub_command_list; p; p = p->next)
  72. grub_printf ("%s (%d%c)\t%s\n", p->name,
  73. p->prio & GRUB_PRIO_LIST_PRIO_MASK,
  74. (p->prio & GRUB_PRIO_LIST_FLAG_ACTIVE) ? '+' : '-',
  75. p->description);
  76. return 0;
  77. }
  78. #if 0
  79. static void
  80. grub_rescue_cmd_info (void)
  81. {
  82. extern void grub_disk_cache_get_performance (unsigned long *,
  83. unsigned long *);
  84. unsigned long hits, misses;
  85. grub_disk_cache_get_performance (&hits, &misses);
  86. grub_printf ("Disk cache: hits = %u, misses = %u ", hits, misses);
  87. if (hits + misses)
  88. {
  89. unsigned long ratio = hits * 10000 / (hits + misses);
  90. grub_printf ("(%u.%u%%)\n", ratio / 100, ratio % 100);
  91. }
  92. else
  93. grub_printf ("(N/A)\n");
  94. }
  95. #endif
  96. /* root [DEVICE] */
  97. static grub_err_t
  98. grub_mini_cmd_root (struct grub_command *cmd __attribute__ ((unused)),
  99. int argc, char *argv[])
  100. {
  101. grub_device_t dev;
  102. grub_fs_t fs;
  103. if (argc > 0)
  104. {
  105. char *device_name = grub_file_get_device_name (argv[0]);
  106. if (! device_name)
  107. return grub_errno;
  108. grub_env_set ("root", device_name);
  109. grub_free (device_name);
  110. }
  111. dev = grub_device_open (0);
  112. if (! dev)
  113. return grub_errno;
  114. fs = grub_fs_probe (dev);
  115. if (grub_errno == GRUB_ERR_UNKNOWN_FS)
  116. grub_errno = GRUB_ERR_NONE;
  117. grub_printf ("(%s): Filesystem is %s.\n",
  118. grub_env_get ("root"), fs ? fs->name : "unknown");
  119. grub_device_close (dev);
  120. return 0;
  121. }
  122. #if 0
  123. static void
  124. grub_rescue_cmd_testload (int argc, char *argv[])
  125. {
  126. grub_file_t file;
  127. char *buf;
  128. grub_ssize_t size;
  129. grub_ssize_t pos;
  130. auto void read_func (unsigned long sector, unsigned offset, unsigned len);
  131. void read_func (unsigned long sector __attribute__ ((unused)),
  132. unsigned offset __attribute__ ((unused)),
  133. unsigned len __attribute__ ((unused)))
  134. {
  135. grub_putchar ('.');
  136. grub_refresh ();
  137. }
  138. if (argc < 1)
  139. {
  140. grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
  141. return;
  142. }
  143. file = grub_file_open (argv[0]);
  144. if (! file)
  145. return;
  146. size = grub_file_size (file) & ~(GRUB_DISK_SECTOR_SIZE - 1);
  147. if (size == 0)
  148. {
  149. grub_file_close (file);
  150. return;
  151. }
  152. buf = grub_malloc (size);
  153. if (! buf)
  154. goto fail;
  155. grub_printf ("Reading %s sequentially", argv[0]);
  156. file->read_hook = read_func;
  157. if (grub_file_read (file, buf, size) != size)
  158. goto fail;
  159. grub_printf (" Done.\n");
  160. /* Read sequentially again. */
  161. grub_printf ("Reading %s sequentially again", argv[0]);
  162. if (grub_file_seek (file, 0) < 0)
  163. goto fail;
  164. for (pos = 0; pos < size; pos += GRUB_DISK_SECTOR_SIZE)
  165. {
  166. char sector[GRUB_DISK_SECTOR_SIZE];
  167. if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
  168. != GRUB_DISK_SECTOR_SIZE)
  169. goto fail;
  170. if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
  171. {
  172. grub_printf ("\nDiffers in %d\n", pos);
  173. goto fail;
  174. }
  175. }
  176. grub_printf (" Done.\n");
  177. /* Read backwards and compare. */
  178. grub_printf ("Reading %s backwards", argv[0]);
  179. pos = size;
  180. while (pos > 0)
  181. {
  182. char sector[GRUB_DISK_SECTOR_SIZE];
  183. pos -= GRUB_DISK_SECTOR_SIZE;
  184. if (grub_file_seek (file, pos) < 0)
  185. goto fail;
  186. if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
  187. != GRUB_DISK_SECTOR_SIZE)
  188. goto fail;
  189. if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
  190. {
  191. int i;
  192. grub_printf ("\nDiffers in %d\n", pos);
  193. for (i = 0; i < GRUB_DISK_SECTOR_SIZE; i++)
  194. grub_putchar (buf[pos + i]);
  195. if (i)
  196. grub_refresh ();
  197. goto fail;
  198. }
  199. }
  200. grub_printf (" Done.\n");
  201. fail:
  202. grub_file_close (file);
  203. grub_free (buf);
  204. }
  205. #endif
  206. /* dump ADDRESS [SIZE] */
  207. static grub_err_t
  208. grub_mini_cmd_dump (struct grub_command *cmd __attribute__ ((unused)),
  209. int argc, char *argv[])
  210. {
  211. grub_uint8_t *addr;
  212. grub_size_t size = 4;
  213. if (argc == 0)
  214. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no address specified");
  215. addr = (grub_uint8_t *) grub_strtoul (argv[0], 0, 0);
  216. if (grub_errno)
  217. return grub_errno;
  218. if (argc > 1)
  219. size = (grub_size_t) grub_strtoul (argv[1], 0, 0);
  220. while (size--)
  221. {
  222. grub_printf ("%x%x ", *addr >> 4, *addr & 0xf);
  223. addr++;
  224. }
  225. return 0;
  226. }
  227. /* rmmod MODULE */
  228. static grub_err_t
  229. grub_mini_cmd_rmmod (struct grub_command *cmd __attribute__ ((unused)),
  230. int argc, char *argv[])
  231. {
  232. grub_dl_t mod;
  233. if (argc == 0)
  234. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
  235. mod = grub_dl_get (argv[0]);
  236. if (! mod)
  237. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no such module");
  238. if (grub_dl_unref (mod) <= 0)
  239. grub_dl_unload (mod);
  240. return 0;
  241. }
  242. static int
  243. print_module (grub_dl_t mod)
  244. {
  245. grub_dl_dep_t dep;
  246. grub_printf ("%s\t%d\t\t", mod->name, mod->ref_count);
  247. for (dep = mod->dep; dep; dep = dep->next)
  248. {
  249. if (dep != mod->dep)
  250. grub_putchar (',');
  251. grub_printf ("%s", dep->mod->name);
  252. }
  253. grub_putchar ('\n');
  254. return 0;
  255. }
  256. /* lsmod */
  257. static grub_err_t
  258. grub_mini_cmd_lsmod (struct grub_command *cmd __attribute__ ((unused)),
  259. int argc __attribute__ ((unused)),
  260. char *argv[] __attribute__ ((unused)))
  261. {
  262. grub_printf ("Name\tRef Count\tDependencies\n");
  263. grub_dl_iterate (print_module);
  264. return 0;
  265. }
  266. /* exit */
  267. static grub_err_t
  268. grub_mini_cmd_exit (struct grub_command *cmd __attribute__ ((unused)),
  269. int argc __attribute__ ((unused)),
  270. char *argv[] __attribute__ ((unused)))
  271. {
  272. grub_exit ();
  273. return 0;
  274. }
  275. /* clear */
  276. static grub_err_t
  277. grub_mini_cmd_clear (struct grub_command *cmd __attribute__ ((unused)),
  278. int argc __attribute__ ((unused)),
  279. char *argv[] __attribute__ ((unused)))
  280. {
  281. grub_cls ();
  282. return 0;
  283. }
  284. static grub_command_t cmd_cat, cmd_help, cmd_root;
  285. static grub_command_t cmd_dump, cmd_rmmod, cmd_lsmod, cmd_exit;
  286. static grub_command_t cmd_clear;
  287. GRUB_MOD_INIT(minicmd)
  288. {
  289. cmd_cat =
  290. grub_register_command ("cat", grub_mini_cmd_cat,
  291. N_("FILE"), N_("Show the contents of a file."));
  292. cmd_help =
  293. grub_register_command ("help", grub_mini_cmd_help,
  294. 0, N_("Show this message."));
  295. cmd_root =
  296. grub_register_command ("root", grub_mini_cmd_root,
  297. N_("[DEVICE]"), N_("Set the root device."));
  298. cmd_dump =
  299. grub_register_command ("dump", grub_mini_cmd_dump,
  300. N_("ADDR"), N_("Dump memory."));
  301. cmd_rmmod =
  302. grub_register_command ("rmmod", grub_mini_cmd_rmmod,
  303. N_("MODULE"), N_("Remove a module."));
  304. cmd_lsmod =
  305. grub_register_command ("lsmod", grub_mini_cmd_lsmod,
  306. 0, N_("Show loaded modules."));
  307. cmd_exit =
  308. grub_register_command ("exit", grub_mini_cmd_exit,
  309. 0, N_("Exit from GRUB."));
  310. cmd_clear =
  311. grub_register_command ("clear", grub_mini_cmd_clear,
  312. 0, N_("Clear the screen."));
  313. }
  314. GRUB_MOD_FINI(minicmd)
  315. {
  316. grub_unregister_command (cmd_cat);
  317. grub_unregister_command (cmd_help);
  318. grub_unregister_command (cmd_root);
  319. grub_unregister_command (cmd_dump);
  320. grub_unregister_command (cmd_rmmod);
  321. grub_unregister_command (cmd_lsmod);
  322. grub_unregister_command (cmd_exit);
  323. grub_unregister_command (cmd_clear);
  324. }