search_wrap.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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."), N_("VARNAME"),
  38. ARG_TYPE_STRING},
  39. {"no-floppy", 'n', 0, N_("Do not probe any floppy drive."), 0, 0},
  40. {"efidisk-only", 0, 0, N_("Only probe EFI disks."), 0, 0},
  41. {"hint", 'h', GRUB_ARG_OPTION_REPEATABLE,
  42. N_("First try the device HINT. If HINT ends in comma, "
  43. "also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
  44. {"hint-ieee1275", 0, GRUB_ARG_OPTION_REPEATABLE,
  45. N_("First try the device HINT if currently running on IEEE1275. "
  46. "If HINT ends in comma, also try subpartitions"),
  47. N_("HINT"), ARG_TYPE_STRING},
  48. {"hint-bios", 0, GRUB_ARG_OPTION_REPEATABLE,
  49. N_("First try the device HINT if currently running on BIOS. "
  50. "If HINT ends in comma, also try subpartitions"),
  51. N_("HINT"), ARG_TYPE_STRING},
  52. {"hint-baremetal", 0, GRUB_ARG_OPTION_REPEATABLE,
  53. N_("First try the device HINT if direct hardware access is supported. "
  54. "If HINT ends in comma, also try subpartitions"),
  55. N_("HINT"), ARG_TYPE_STRING},
  56. {"hint-efi", 0, GRUB_ARG_OPTION_REPEATABLE,
  57. N_("First try the device HINT if currently running on EFI. "
  58. "If HINT ends in comma, also try subpartitions"),
  59. N_("HINT"), ARG_TYPE_STRING},
  60. {"hint-arc", 0, GRUB_ARG_OPTION_REPEATABLE,
  61. N_("First try the device HINT if currently running on ARC."
  62. " If HINT ends in comma, also try subpartitions"),
  63. N_("HINT"), ARG_TYPE_STRING},
  64. {0, 0, 0, 0, 0, 0}
  65. };
  66. enum options
  67. {
  68. SEARCH_FILE,
  69. SEARCH_LABEL,
  70. SEARCH_FS_UUID,
  71. SEARCH_SET,
  72. SEARCH_NO_FLOPPY,
  73. SEARCH_EFIDISK_ONLY,
  74. SEARCH_HINT,
  75. SEARCH_HINT_IEEE1275,
  76. SEARCH_HINT_BIOS,
  77. SEARCH_HINT_BAREMETAL,
  78. SEARCH_HINT_EFI,
  79. SEARCH_HINT_ARC,
  80. };
  81. static grub_err_t
  82. grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
  83. {
  84. struct grub_arg_list *state = ctxt->state;
  85. const char *var = 0;
  86. const char *id = 0;
  87. int i = 0, j = 0, nhints = 0;
  88. char **hints = NULL;
  89. enum search_flags flags = SEARCH_FLAGS_NONE;
  90. if (state[SEARCH_HINT].set)
  91. for (i = 0; state[SEARCH_HINT].args[i]; i++)
  92. nhints++;
  93. #ifdef GRUB_MACHINE_IEEE1275
  94. if (state[SEARCH_HINT_IEEE1275].set)
  95. for (i = 0; state[SEARCH_HINT_IEEE1275].args[i]; i++)
  96. nhints++;
  97. #endif
  98. #ifdef GRUB_MACHINE_EFI
  99. if (state[SEARCH_HINT_EFI].set)
  100. for (i = 0; state[SEARCH_HINT_EFI].args[i]; i++)
  101. nhints++;
  102. #endif
  103. #ifdef GRUB_MACHINE_PCBIOS
  104. if (state[SEARCH_HINT_BIOS].set)
  105. for (i = 0; state[SEARCH_HINT_BIOS].args[i]; i++)
  106. nhints++;
  107. #endif
  108. #ifdef GRUB_MACHINE_ARC
  109. if (state[SEARCH_HINT_ARC].set)
  110. for (i = 0; state[SEARCH_HINT_ARC].args[i]; i++)
  111. nhints++;
  112. #endif
  113. if (state[SEARCH_HINT_BAREMETAL].set)
  114. for (i = 0; state[SEARCH_HINT_BAREMETAL].args[i]; i++)
  115. nhints++;
  116. hints = grub_calloc (nhints, sizeof (hints[0]));
  117. if (!hints)
  118. return grub_errno;
  119. j = 0;
  120. if (state[SEARCH_HINT].set)
  121. for (i = 0; state[SEARCH_HINT].args[i]; i++)
  122. hints[j++] = state[SEARCH_HINT].args[i];
  123. #ifdef GRUB_MACHINE_IEEE1275
  124. if (state[SEARCH_HINT_IEEE1275].set)
  125. for (i = 0; state[SEARCH_HINT_IEEE1275].args[i]; i++)
  126. hints[j++] = state[SEARCH_HINT_IEEE1275].args[i];
  127. #endif
  128. #ifdef GRUB_MACHINE_EFI
  129. if (state[SEARCH_HINT_EFI].set)
  130. for (i = 0; state[SEARCH_HINT_EFI].args[i]; i++)
  131. hints[j++] = state[SEARCH_HINT_EFI].args[i];
  132. #endif
  133. #ifdef GRUB_MACHINE_ARC
  134. if (state[SEARCH_HINT_ARC].set)
  135. for (i = 0; state[SEARCH_HINT_ARC].args[i]; i++)
  136. hints[j++] = state[SEARCH_HINT_ARC].args[i];
  137. #endif
  138. #ifdef GRUB_MACHINE_PCBIOS
  139. if (state[SEARCH_HINT_BIOS].set)
  140. for (i = 0; state[SEARCH_HINT_BIOS].args[i]; i++)
  141. hints[j++] = state[SEARCH_HINT_BIOS].args[i];
  142. #endif
  143. if (state[SEARCH_HINT_BAREMETAL].set)
  144. for (i = 0; state[SEARCH_HINT_BAREMETAL].args[i]; i++)
  145. hints[j++] = state[SEARCH_HINT_BAREMETAL].args[i];
  146. /* Skip hints for future platforms. */
  147. for (j = 0; j < argc; j++)
  148. if (grub_memcmp (args[j], "--hint-", sizeof ("--hint-") - 1) != 0)
  149. break;
  150. if (state[SEARCH_SET].set)
  151. var = state[SEARCH_SET].arg ? state[SEARCH_SET].arg : "root";
  152. if (argc != j)
  153. id = args[j];
  154. else if (state[SEARCH_SET].set && state[SEARCH_SET].arg)
  155. {
  156. id = state[SEARCH_SET].arg;
  157. var = "root";
  158. }
  159. else
  160. {
  161. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
  162. goto out;
  163. }
  164. if (state[SEARCH_NO_FLOPPY].set)
  165. flags |= SEARCH_FLAGS_NO_FLOPPY;
  166. if (state[SEARCH_EFIDISK_ONLY].set)
  167. flags |= SEARCH_FLAGS_EFIDISK_ONLY;
  168. if (state[SEARCH_LABEL].set)
  169. grub_search_label (id, var, flags, hints, nhints);
  170. else if (state[SEARCH_FS_UUID].set)
  171. grub_search_fs_uuid (id, var, flags, hints, nhints);
  172. else if (state[SEARCH_FILE].set)
  173. grub_search_fs_file (id, var, flags, hints, nhints);
  174. else
  175. grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
  176. out:
  177. grub_free (hints);
  178. return grub_errno;
  179. }
  180. static grub_extcmd_t cmd;
  181. GRUB_MOD_INIT(search)
  182. {
  183. cmd =
  184. grub_register_extcmd ("search", grub_cmd_search,
  185. GRUB_COMMAND_FLAG_EXTRACTOR | GRUB_COMMAND_ACCEPT_DASH,
  186. N_("[-f|-l|-u|-s|-n] [--hint HINT [--hint HINT] ...]"
  187. " NAME"),
  188. N_("Search devices by file, filesystem label"
  189. " or filesystem UUID."
  190. " If --set is specified, the first device found is"
  191. " set to a variable. If no variable name is"
  192. " specified, `root' is used."),
  193. options);
  194. }
  195. GRUB_MOD_FINI(search)
  196. {
  197. grub_unregister_extcmd (cmd);
  198. }