corecmd.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* corecmd.c - critical commands which are registered in kernel */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 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/mm.h>
  20. #include <grub/dl.h>
  21. #include <grub/err.h>
  22. #include <grub/env.h>
  23. #include <grub/misc.h>
  24. #include <grub/term.h>
  25. #include <grub/file.h>
  26. #include <grub/device.h>
  27. #include <grub/command.h>
  28. #include <grub/i18n.h>
  29. static int
  30. print_env (struct grub_env_var *env, void *closure __attribute__ ((unused)))
  31. {
  32. grub_printf ("%s=%s\n", env->name, env->value);
  33. return 0;
  34. }
  35. /* set ENVVAR=VALUE */
  36. static grub_err_t
  37. grub_core_cmd_set (struct grub_command *cmd __attribute__ ((unused)),
  38. int argc, char *argv[])
  39. {
  40. char *var;
  41. char *val;
  42. if (argc < 1)
  43. {
  44. grub_env_iterate (print_env, 0);
  45. return 0;
  46. }
  47. var = argv[0];
  48. val = grub_strchr (var, '=');
  49. if (! val)
  50. return grub_error (GRUB_ERR_BAD_ARGUMENT, "not an assignment");
  51. val[0] = 0;
  52. grub_env_set (var, val + 1);
  53. val[0] = '=';
  54. return 0;
  55. }
  56. static grub_err_t
  57. grub_core_cmd_unset (struct grub_command *cmd __attribute__ ((unused)),
  58. int argc, char *argv[])
  59. {
  60. if (argc < 1)
  61. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  62. "no environment variable specified");
  63. grub_env_unset (argv[0]);
  64. return 0;
  65. }
  66. /* insmod MODULE */
  67. static grub_err_t
  68. grub_core_cmd_insmod (struct grub_command *cmd __attribute__ ((unused)),
  69. int argc, char *argv[])
  70. {
  71. char *p;
  72. grub_dl_t mod;
  73. if (argc == 0)
  74. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
  75. p = grub_strchr (argv[0], '/');
  76. if (! p)
  77. mod = grub_dl_load (argv[0]);
  78. else
  79. mod = grub_dl_load_file (argv[0]);
  80. if (mod)
  81. grub_dl_ref (mod);
  82. return 0;
  83. }
  84. static int
  85. grub_mini_print_devices (const char *name,
  86. void *closure __attribute__ ((unused)))
  87. {
  88. grub_printf ("(%s) ", name);
  89. return 0;
  90. }
  91. static int
  92. grub_mini_print_files (const char *filename,
  93. const struct grub_dirhook_info *info,
  94. void *closure __attribute__ ((unused)))
  95. {
  96. grub_printf ("%s%s ", filename, info->dir ? "/" : "");
  97. return 0;
  98. }
  99. /* ls [ARG] */
  100. static grub_err_t
  101. grub_core_cmd_ls (struct grub_command *cmd __attribute__ ((unused)),
  102. int argc, char *argv[])
  103. {
  104. if (argc < 1)
  105. {
  106. grub_device_iterate (grub_mini_print_devices, 0);
  107. grub_putchar ('\n');
  108. grub_refresh ();
  109. }
  110. else
  111. {
  112. char *device_name;
  113. grub_device_t dev;
  114. grub_fs_t fs;
  115. char *path;
  116. device_name = grub_file_get_device_name (argv[0]);
  117. dev = grub_device_open (device_name);
  118. if (! dev)
  119. goto fail;
  120. fs = grub_fs_probe (dev);
  121. path = grub_strchr (argv[0], ')');
  122. if (! path)
  123. path = argv[0];
  124. else
  125. path++;
  126. if (! path && ! device_name)
  127. {
  128. grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
  129. goto fail;
  130. }
  131. if (! path)
  132. {
  133. if (grub_errno == GRUB_ERR_UNKNOWN_FS)
  134. grub_errno = GRUB_ERR_NONE;
  135. grub_printf ("(%s): Filesystem is %s.\n",
  136. device_name, fs ? fs->name : "unknown");
  137. }
  138. else if (fs)
  139. {
  140. (fs->dir) (dev, path, grub_mini_print_files, 0);
  141. grub_putchar ('\n');
  142. grub_refresh ();
  143. }
  144. fail:
  145. if (dev)
  146. grub_device_close (dev);
  147. grub_free (device_name);
  148. }
  149. return grub_errno;
  150. }
  151. void
  152. grub_register_core_commands (void)
  153. {
  154. grub_register_command ("set", grub_core_cmd_set,
  155. N_("[ENVVAR=VALUE]"),
  156. N_("Set an environment variable."));
  157. grub_register_command ("unset", grub_core_cmd_unset,
  158. N_("ENVVAR"),
  159. N_("Remove an environment variable."));
  160. grub_register_command ("ls", grub_core_cmd_ls,
  161. N_("[ARG]"), N_("List devices or files."));
  162. grub_register_command ("insmod", grub_core_cmd_insmod,
  163. N_("MODULE"), N_("Insert a module."));
  164. }