search_wrap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. GRUB_MOD_LICENSE ("GPLv3+");
  29. static const struct grub_arg_option options[] =
  30. {
  31. {"file", 'f', 0, N_("Search devices by a file."), 0, 0},
  32. {"label", 'l', 0, N_("Search devices by a filesystem label."),
  33. 0, 0},
  34. {"fs-uuid", 'u', 0, N_("Search devices by a filesystem UUID."),
  35. 0, 0},
  36. {"set", 's', GRUB_ARG_OPTION_OPTIONAL,
  37. N_("Set a variable to the first device found."), "VAR", ARG_TYPE_STRING},
  38. {"no-floppy", 'n', 0, N_("Do not probe any floppy drive."), 0, 0},
  39. {"hint", 'h', GRUB_ARG_OPTION_REPEATABLE,
  40. N_("First try the device HINT. If HINT ends in comma, "
  41. "also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
  42. {0, 0, 0, 0, 0, 0}
  43. };
  44. enum options
  45. {
  46. SEARCH_FILE,
  47. SEARCH_LABEL,
  48. SEARCH_FS_UUID,
  49. SEARCH_SET,
  50. SEARCH_NO_FLOPPY,
  51. SEARCH_HINT
  52. };
  53. static grub_err_t
  54. grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
  55. {
  56. struct grub_arg_list *state = ctxt->state;
  57. const char *var = 0;
  58. int nhints = 0;
  59. if (state[SEARCH_HINT].set)
  60. while (state[SEARCH_HINT].args[nhints])
  61. nhints++;
  62. if (argc == 0)
  63. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
  64. if (state[SEARCH_SET].set)
  65. var = state[SEARCH_SET].arg ? state[SEARCH_SET].arg : "root";
  66. if (state[SEARCH_LABEL].set)
  67. grub_search_label (args[0], var, state[SEARCH_NO_FLOPPY].set,
  68. state[SEARCH_HINT].args, nhints);
  69. else if (state[SEARCH_FS_UUID].set)
  70. grub_search_fs_uuid (args[0], var, state[SEARCH_NO_FLOPPY].set,
  71. state[SEARCH_HINT].args, nhints);
  72. else if (state[SEARCH_FILE].set)
  73. grub_search_fs_file (args[0], var, state[SEARCH_NO_FLOPPY].set,
  74. state[SEARCH_HINT].args, nhints);
  75. else
  76. return grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
  77. return grub_errno;
  78. }
  79. static grub_extcmd_t cmd;
  80. GRUB_MOD_INIT(search)
  81. {
  82. cmd =
  83. grub_register_extcmd ("search", grub_cmd_search, GRUB_COMMAND_FLAG_EXTRACTOR,
  84. N_("[-f|-l|-u|-s|-n] [--hint HINT [--hint HINT] ...]"
  85. " NAME"),
  86. N_("Search devices by file, filesystem label"
  87. " or filesystem UUID."
  88. " If --set is specified, the first device found is"
  89. " set to a variable. If no variable name is"
  90. " specified, \"root\" is used."),
  91. options);
  92. }
  93. GRUB_MOD_FINI(search)
  94. {
  95. grub_unregister_extcmd (cmd);
  96. }