search_wrap.c 6.5 KB

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