search.c 7.5 KB

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