search.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #include <grub/disk.h>
  31. #include <grub/partition.h>
  32. GRUB_MOD_LICENSE ("GPLv3+");
  33. void
  34. FUNC_NAME (const char *key, const char *var, int no_floppy,
  35. char **hints, unsigned nhints)
  36. {
  37. int count = 0;
  38. grub_fs_autoload_hook_t saved_autoload;
  39. auto int iterate_device (const char *name);
  40. int iterate_device (const char *name)
  41. {
  42. int found = 0;
  43. /* Skip floppy drives when requested. */
  44. if (no_floppy &&
  45. name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
  46. return 0;
  47. #ifdef DO_SEARCH_FILE
  48. {
  49. char *buf;
  50. grub_file_t file;
  51. buf = grub_xasprintf ("(%s)%s", name, key);
  52. if (! buf)
  53. return 1;
  54. grub_file_filter_disable_compression ();
  55. file = grub_file_open (buf);
  56. if (file)
  57. {
  58. found = 1;
  59. grub_file_close (file);
  60. }
  61. grub_free (buf);
  62. }
  63. #else
  64. {
  65. /* SEARCH_FS_UUID or SEARCH_LABEL */
  66. grub_device_t dev;
  67. grub_fs_t fs;
  68. char *quid;
  69. dev = grub_device_open (name);
  70. if (dev)
  71. {
  72. fs = grub_fs_probe (dev);
  73. #ifdef DO_SEARCH_FS_UUID
  74. #define compare_fn grub_strcasecmp
  75. #define read_fn uuid
  76. #else
  77. #define compare_fn grub_strcmp
  78. #define read_fn label
  79. #endif
  80. if (fs && fs->read_fn)
  81. {
  82. fs->read_fn (dev, &quid);
  83. if (grub_errno == GRUB_ERR_NONE && quid)
  84. {
  85. if (compare_fn (quid, key) == 0)
  86. found = 1;
  87. grub_free (quid);
  88. }
  89. }
  90. grub_device_close (dev);
  91. }
  92. }
  93. #endif
  94. if (found)
  95. {
  96. count++;
  97. if (var)
  98. grub_env_set (var, name);
  99. else
  100. grub_printf (" %s", name);
  101. }
  102. grub_errno = GRUB_ERR_NONE;
  103. return (found && var);
  104. }
  105. auto int part_hook (grub_disk_t disk, const grub_partition_t partition);
  106. int part_hook (grub_disk_t disk, const grub_partition_t partition)
  107. {
  108. char *partition_name, *devname;
  109. int ret;
  110. partition_name = grub_partition_get_name (partition);
  111. if (! partition_name)
  112. return 1;
  113. devname = grub_xasprintf ("%s,%s", disk->name, partition_name);
  114. grub_free (partition_name);
  115. if (!devname)
  116. return 1;
  117. ret = iterate_device (devname);
  118. grub_free (devname);
  119. return ret;
  120. }
  121. auto void try (void);
  122. void try (void)
  123. {
  124. unsigned i;
  125. for (i = 0; i < nhints; i++)
  126. {
  127. char *end;
  128. if (!hints[i][0])
  129. continue;
  130. end = hints[i] + grub_strlen (hints[i]) - 1;
  131. if (*end == ',')
  132. *end = 0;
  133. if (iterate_device (hints[i]))
  134. {
  135. if (!*end)
  136. *end = ',';
  137. return;
  138. }
  139. if (!*end)
  140. {
  141. grub_device_t dev;
  142. int ret;
  143. dev = grub_device_open (hints[i]);
  144. if (!dev)
  145. {
  146. *end = ',';
  147. continue;
  148. }
  149. if (!dev->disk)
  150. {
  151. grub_device_close (dev);
  152. *end = ',';
  153. continue;
  154. }
  155. ret = grub_partition_iterate (dev->disk, part_hook);
  156. *end = ',';
  157. grub_device_close (dev);
  158. if (ret)
  159. return;
  160. }
  161. }
  162. grub_device_iterate (iterate_device);
  163. }
  164. /* First try without autoloading if we're setting variable. */
  165. if (var)
  166. {
  167. saved_autoload = grub_fs_autoload_hook;
  168. grub_fs_autoload_hook = 0;
  169. try ();
  170. /* Restore autoload hook. */
  171. grub_fs_autoload_hook = saved_autoload;
  172. /* Retry with autoload if nothing found. */
  173. if (grub_errno == GRUB_ERR_NONE && count == 0)
  174. try ();
  175. }
  176. else
  177. try ();
  178. if (grub_errno == GRUB_ERR_NONE && count == 0)
  179. grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
  180. }
  181. static grub_err_t
  182. grub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc,
  183. char **args)
  184. {
  185. if (argc == 0)
  186. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
  187. FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0, (args + 2),
  188. argc > 2 ? argc - 2 : 0);
  189. return grub_errno;
  190. }
  191. static grub_command_t cmd;
  192. #ifdef DO_SEARCH_FILE
  193. GRUB_MOD_INIT(search_fs_file)
  194. #elif defined (DO_SEARCH_FS_UUID)
  195. GRUB_MOD_INIT(search_fs_uuid)
  196. #else
  197. GRUB_MOD_INIT(search_label)
  198. #endif
  199. {
  200. cmd =
  201. grub_register_command (COMMAND_NAME, grub_cmd_do_search,
  202. N_("NAME [VARIABLE] [HINTS]"),
  203. HELP_MESSAGE);
  204. }
  205. #ifdef DO_SEARCH_FILE
  206. GRUB_MOD_FINI(search_fs_file)
  207. #elif defined (DO_SEARCH_FS_UUID)
  208. GRUB_MOD_FINI(search_fs_uuid)
  209. #else
  210. GRUB_MOD_FINI(search_label)
  211. #endif
  212. {
  213. grub_unregister_command (cmd);
  214. }