search_wrap.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* search.c - search devices based on a file or a filesystem label */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2007,2008,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/types.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/err.h>
  23. #include <grub/dl.h>
  24. #include <grub/env.h>
  25. #include <grub/extcmd.h>
  26. #include <grub/search.h>
  27. #include <grub/i18n.h>
  28. static const struct grub_arg_option options[] =
  29. {
  30. {"file", 'f', 0, N_("Search devices by a file."), 0, 0},
  31. {"label", 'l', 0, N_("Search devices by a filesystem label."),
  32. 0, 0},
  33. {"fs-uuid", 'u', 0, N_("Search devices by a filesystem UUID."),
  34. 0, 0},
  35. {"set", 's', GRUB_ARG_OPTION_OPTIONAL,
  36. N_("Set a variable to the first device found."), "VAR", ARG_TYPE_STRING},
  37. {"no-floppy", 'n', 0, N_("Do not probe any floppy drive."), 0, 0},
  38. {0, 0, 0, 0, 0, 0}
  39. };
  40. enum options
  41. {
  42. SEARCH_FILE,
  43. SEARCH_LABEL,
  44. SEARCH_FS_UUID,
  45. SEARCH_SET,
  46. SEARCH_NO_FLOPPY,
  47. };
  48. static grub_err_t
  49. grub_cmd_search (grub_extcmd_t cmd, int argc, char **args)
  50. {
  51. struct grub_arg_list *state = cmd->state;
  52. const char *var = 0;
  53. if (argc == 0)
  54. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
  55. if (state[SEARCH_SET].set)
  56. var = state[SEARCH_SET].arg ? state[SEARCH_SET].arg : "root";
  57. if (state[SEARCH_LABEL].set)
  58. grub_search_label (args[0], var, state[SEARCH_NO_FLOPPY].set);
  59. else if (state[SEARCH_FS_UUID].set)
  60. grub_search_fs_uuid (args[0], var, state[SEARCH_NO_FLOPPY].set);
  61. else if (state[SEARCH_FILE].set)
  62. grub_search_fs_file (args[0], var, state[SEARCH_NO_FLOPPY].set);
  63. else
  64. return grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
  65. return grub_errno;
  66. }
  67. static grub_extcmd_t cmd;
  68. GRUB_MOD_INIT(search)
  69. {
  70. cmd =
  71. grub_register_extcmd ("search", grub_cmd_search,
  72. GRUB_COMMAND_FLAG_BOTH,
  73. N_("search [-f|-l|-u|-s|-n] NAME"),
  74. N_("Search devices by file, filesystem label"
  75. " or filesystem UUID."
  76. " If --set is specified, the first device found is"
  77. " set to a variable. If no variable name is"
  78. " specified, \"root\" is used."),
  79. options);
  80. }
  81. GRUB_MOD_FINI(search)
  82. {
  83. grub_unregister_extcmd (cmd);
  84. }