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