search.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. struct cache_entry
  34. {
  35. struct cache_entry *next;
  36. char *key;
  37. char *value;
  38. };
  39. static struct cache_entry *cache;
  40. /* Context for FUNC_NAME. */
  41. struct search_ctx
  42. {
  43. const char *key;
  44. const char *var;
  45. int no_floppy;
  46. char **hints;
  47. unsigned nhints;
  48. int count;
  49. int is_cache;
  50. };
  51. /* Helper for FUNC_NAME. */
  52. static int
  53. iterate_device (const char *name, void *data)
  54. {
  55. struct search_ctx *ctx = data;
  56. int found = 0;
  57. /* Skip floppy drives when requested. */
  58. if (ctx->no_floppy &&
  59. name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
  60. return 1;
  61. #ifdef DO_SEARCH_FS_UUID
  62. #define compare_fn grub_strcasecmp
  63. #else
  64. #define compare_fn grub_strcmp
  65. #endif
  66. #ifdef DO_SEARCH_FILE
  67. {
  68. char *buf;
  69. grub_file_t file;
  70. buf = grub_xasprintf ("(%s)%s", name, ctx->key);
  71. if (! buf)
  72. return 1;
  73. grub_file_filter_disable_compression ();
  74. file = grub_file_open (buf);
  75. if (file)
  76. {
  77. found = 1;
  78. grub_file_close (file);
  79. }
  80. grub_free (buf);
  81. }
  82. #else
  83. {
  84. /* SEARCH_FS_UUID or SEARCH_LABEL */
  85. grub_device_t dev;
  86. grub_fs_t fs;
  87. char *quid;
  88. dev = grub_device_open (name);
  89. if (dev)
  90. {
  91. fs = grub_fs_probe (dev);
  92. #ifdef DO_SEARCH_FS_UUID
  93. #define read_fn uuid
  94. #else
  95. #define read_fn label
  96. #endif
  97. if (fs && fs->read_fn)
  98. {
  99. fs->read_fn (dev, &quid);
  100. if (grub_errno == GRUB_ERR_NONE && quid)
  101. {
  102. if (compare_fn (quid, ctx->key) == 0)
  103. found = 1;
  104. grub_free (quid);
  105. }
  106. }
  107. grub_device_close (dev);
  108. }
  109. }
  110. #endif
  111. if (!ctx->is_cache && found && ctx->count == 0)
  112. {
  113. struct cache_entry *cache_ent;
  114. cache_ent = grub_malloc (sizeof (*cache_ent));
  115. if (cache_ent)
  116. {
  117. cache_ent->key = grub_strdup (ctx->key);
  118. cache_ent->value = grub_strdup (name);
  119. if (cache_ent->value && cache_ent->key)
  120. {
  121. cache_ent->next = cache;
  122. cache = cache_ent;
  123. }
  124. else
  125. {
  126. grub_free (cache_ent->value);
  127. grub_free (cache_ent->key);
  128. grub_free (cache_ent);
  129. grub_errno = GRUB_ERR_NONE;
  130. }
  131. }
  132. else
  133. grub_errno = GRUB_ERR_NONE;
  134. }
  135. if (found)
  136. {
  137. ctx->count++;
  138. if (ctx->var)
  139. grub_env_set (ctx->var, name);
  140. else
  141. grub_printf (" %s", name);
  142. }
  143. grub_errno = GRUB_ERR_NONE;
  144. return (found && ctx->var);
  145. }
  146. /* Helper for FUNC_NAME. */
  147. static int
  148. part_hook (grub_disk_t disk, const grub_partition_t partition, void *data)
  149. {
  150. struct search_ctx *ctx = data;
  151. char *partition_name, *devname;
  152. int ret;
  153. partition_name = grub_partition_get_name (partition);
  154. if (! partition_name)
  155. return 1;
  156. devname = grub_xasprintf ("%s,%s", disk->name, partition_name);
  157. grub_free (partition_name);
  158. if (!devname)
  159. return 1;
  160. ret = iterate_device (devname, ctx);
  161. grub_free (devname);
  162. return ret;
  163. }
  164. /* Helper for FUNC_NAME. */
  165. static void
  166. try (struct search_ctx *ctx)
  167. {
  168. unsigned i;
  169. struct cache_entry **prev;
  170. struct cache_entry *cache_ent;
  171. for (prev = &cache, cache_ent = *prev; cache_ent;
  172. prev = &cache_ent->next, cache_ent = *prev)
  173. if (compare_fn (cache_ent->key, ctx->key) == 0)
  174. break;
  175. if (cache_ent)
  176. {
  177. ctx->is_cache = 1;
  178. if (iterate_device (cache_ent->value, ctx))
  179. {
  180. ctx->is_cache = 0;
  181. return;
  182. }
  183. ctx->is_cache = 0;
  184. /* Cache entry was outdated. Remove it. */
  185. if (!ctx->count)
  186. {
  187. *prev = cache_ent->next;
  188. grub_free (cache_ent->key);
  189. grub_free (cache_ent->value);
  190. grub_free (cache_ent);
  191. }
  192. }
  193. for (i = 0; i < ctx->nhints; i++)
  194. {
  195. char *end;
  196. if (!ctx->hints[i][0])
  197. continue;
  198. end = ctx->hints[i] + grub_strlen (ctx->hints[i]) - 1;
  199. if (*end == ',')
  200. *end = 0;
  201. if (iterate_device (ctx->hints[i], ctx))
  202. {
  203. if (!*end)
  204. *end = ',';
  205. return;
  206. }
  207. if (!*end)
  208. {
  209. grub_device_t dev;
  210. int ret;
  211. dev = grub_device_open (ctx->hints[i]);
  212. if (!dev)
  213. {
  214. if (!*end)
  215. *end = ',';
  216. continue;
  217. }
  218. if (!dev->disk)
  219. {
  220. grub_device_close (dev);
  221. if (!*end)
  222. *end = ',';
  223. continue;
  224. }
  225. ret = grub_partition_iterate (dev->disk, part_hook, ctx);
  226. if (!*end)
  227. *end = ',';
  228. grub_device_close (dev);
  229. if (ret)
  230. return;
  231. }
  232. }
  233. grub_device_iterate (iterate_device, ctx);
  234. }
  235. void
  236. FUNC_NAME (const char *key, const char *var, int no_floppy,
  237. char **hints, unsigned nhints)
  238. {
  239. struct search_ctx ctx = {
  240. .key = key,
  241. .var = var,
  242. .no_floppy = no_floppy,
  243. .hints = hints,
  244. .nhints = nhints,
  245. .count = 0,
  246. .is_cache = 0
  247. };
  248. grub_fs_autoload_hook_t saved_autoload;
  249. /* First try without autoloading if we're setting variable. */
  250. if (var)
  251. {
  252. saved_autoload = grub_fs_autoload_hook;
  253. grub_fs_autoload_hook = 0;
  254. try (&ctx);
  255. /* Restore autoload hook. */
  256. grub_fs_autoload_hook = saved_autoload;
  257. /* Retry with autoload if nothing found. */
  258. if (grub_errno == GRUB_ERR_NONE && ctx.count == 0)
  259. try (&ctx);
  260. }
  261. else
  262. try (&ctx);
  263. if (grub_errno == GRUB_ERR_NONE && ctx.count == 0)
  264. grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
  265. }
  266. static grub_err_t
  267. grub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc,
  268. char **args)
  269. {
  270. if (argc == 0)
  271. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
  272. FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0, (args + 2),
  273. argc > 2 ? argc - 2 : 0);
  274. return grub_errno;
  275. }
  276. static grub_command_t cmd;
  277. #ifdef DO_SEARCH_FILE
  278. GRUB_MOD_INIT(search_fs_file)
  279. #elif defined (DO_SEARCH_FS_UUID)
  280. GRUB_MOD_INIT(search_fs_uuid)
  281. #else
  282. GRUB_MOD_INIT(search_label)
  283. #endif
  284. {
  285. cmd =
  286. grub_register_command (COMMAND_NAME, grub_cmd_do_search,
  287. N_("NAME [VARIABLE] [HINTS]"),
  288. HELP_MESSAGE);
  289. }
  290. #ifdef DO_SEARCH_FILE
  291. GRUB_MOD_FINI(search_fs_file)
  292. #elif defined (DO_SEARCH_FS_UUID)
  293. GRUB_MOD_FINI(search_fs_uuid)
  294. #else
  295. GRUB_MOD_FINI(search_label)
  296. #endif
  297. {
  298. grub_unregister_command (cmd);
  299. }