search.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/device.h>
  25. #include <grub/file.h>
  26. #include <grub/env.h>
  27. #include <grub/command.h>
  28. #include <grub/search.h>
  29. #include <grub/i18n.h>
  30. #ifdef DO_SEARCH_FILE
  31. GRUB_EXPORT(grub_search_fs_file);
  32. #elif defined (DO_SEARCH_FS_UUID)
  33. GRUB_EXPORT(grub_search_fs_uuid);
  34. #else
  35. GRUB_EXPORT(grub_search_label);
  36. #endif
  37. struct search_fs_closure
  38. {
  39. const char *key;
  40. const char *var;
  41. int no_floppy;
  42. int count;
  43. };
  44. static int
  45. iterate_device (const char *name, void *closure)
  46. {
  47. struct search_fs_closure *c = closure;
  48. int found = 0;
  49. /* Skip floppy drives when requested. */
  50. if (c->no_floppy &&
  51. name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
  52. return 0;
  53. #ifdef DO_SEARCH_FILE
  54. {
  55. char *buf;
  56. grub_file_t file;
  57. buf = grub_xasprintf ("(%s)%s", name, c->key);
  58. if (! buf)
  59. return 1;
  60. file = grub_file_open (buf);
  61. if (file)
  62. {
  63. found = 1;
  64. grub_file_close (file);
  65. }
  66. grub_free (buf);
  67. }
  68. #else
  69. {
  70. /* SEARCH_FS_UUID or SEARCH_LABEL */
  71. grub_device_t dev;
  72. grub_fs_t fs;
  73. char *quid;
  74. dev = grub_device_open (name);
  75. if (dev)
  76. {
  77. fs = grub_fs_probe (dev);
  78. #ifdef DO_SEARCH_FS_UUID
  79. #define compare_fn grub_strcasecmp
  80. #define read_fn uuid
  81. #else
  82. #define compare_fn grub_strcmp
  83. #define read_fn label
  84. #endif
  85. if (fs && fs->read_fn)
  86. {
  87. fs->read_fn (dev, &quid);
  88. if (grub_errno == GRUB_ERR_NONE && quid)
  89. {
  90. if (compare_fn (quid, c->key) == 0)
  91. found = 1;
  92. grub_free (quid);
  93. }
  94. }
  95. grub_device_close (dev);
  96. }
  97. }
  98. #endif
  99. if (found)
  100. {
  101. c->count++;
  102. if (c->var)
  103. grub_env_set (c->var, name);
  104. else
  105. grub_printf (" %s", name);
  106. }
  107. grub_errno = GRUB_ERR_NONE;
  108. return (found && c->var);
  109. }
  110. void
  111. FUNC_NAME (const char *key, const char *var, int no_floppy)
  112. {
  113. struct search_fs_closure c;
  114. c.key = key;
  115. c.var = var;
  116. c.no_floppy = no_floppy;
  117. c.count = 0;
  118. /* First try without autoloading if we're setting variable. */
  119. if (c.var)
  120. {
  121. grub_fs_autoload_hook_t saved_autoload;
  122. saved_autoload = grub_fs_autoload_hook;
  123. grub_fs_autoload_hook = 0;
  124. grub_device_iterate (iterate_device, &c);
  125. /* Restore autoload hook. */
  126. grub_fs_autoload_hook = saved_autoload;
  127. /* Retry with autoload if nothing found. */
  128. if (grub_errno == GRUB_ERR_NONE && c.count == 0)
  129. grub_device_iterate (iterate_device, &c);
  130. }
  131. else
  132. grub_device_iterate (iterate_device, &c);
  133. if (grub_errno == GRUB_ERR_NONE && c.count == 0)
  134. grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
  135. }
  136. static grub_err_t
  137. grub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc,
  138. char **args)
  139. {
  140. if (argc == 0)
  141. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
  142. FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0);
  143. return grub_errno;
  144. }
  145. static grub_command_t cmd;
  146. #ifdef DO_SEARCH_FILE
  147. GRUB_MOD_INIT(search_fs_file)
  148. #elif defined (DO_SEARCH_FS_UUID)
  149. GRUB_MOD_INIT(search_fs_uuid)
  150. #else
  151. GRUB_MOD_INIT(search_label)
  152. #endif
  153. {
  154. cmd =
  155. grub_register_command (COMMAND_NAME, grub_cmd_do_search,
  156. N_("NAME [VARIABLE]"),
  157. HELP_MESSAGE);
  158. }
  159. #ifdef DO_SEARCH_FILE
  160. GRUB_MOD_FINI(search_fs_file)
  161. #elif defined (DO_SEARCH_FS_UUID)
  162. GRUB_MOD_FINI(search_fs_uuid)
  163. #else
  164. GRUB_MOD_FINI(search_label)
  165. #endif
  166. {
  167. grub_unregister_command (cmd);
  168. }