/* search.c - search devices based on a file or a filesystem label */ /* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2005,2007,2008,2009 Free Software Foundation, Inc. * * VAS_EBOOT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VAS_EBOOT is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VAS_EBOOT. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include #include #include VAS_EBOOT_MOD_LICENSE ("GPLv3+"); struct cache_entry { struct cache_entry *next; char *key; char *value; }; static struct cache_entry *cache; /* Context for FUNC_NAME. */ struct search_ctx { const char *key; const char *var; enum search_flags flags; char **hints; unsigned nhints; int count; int is_cache; }; static bool is_unencrypted_disk (VasEBoot_disk_t disk) { VasEBoot_command_t cmd; char *disk_str; int disk_str_len; int res; if (disk->dev->id == VAS_EBOOT_DISK_DEVICE_CRYPTODISK_ID) return false; /* This is (crypto) disk. */ if (disk->dev->id == VAS_EBOOT_DISK_DEVICE_DISKFILTER_ID) { char opt[] = "--quiet"; char *args[2]; cmd = VasEBoot_command_find ("cryptocheck"); if (cmd == NULL) /* No diskfilter module loaded for some reason. */ return true; disk_str_len = VasEBoot_strlen (disk->name) + 2 + 1; disk_str = VasEBoot_malloc (disk_str_len); if (disk_str == NULL) /* Something is wrong, better report as unencrypted. */ return true; VasEBoot_snprintf (disk_str, disk_str_len, "(%s)", disk->name); args[0] = opt; args[1] = disk_str; res = cmd->func (cmd, 2, args); VasEBoot_free (disk_str); return (res != VAS_EBOOT_ERR_NONE) ? true : false; /* VAS_EBOOT_ERR_NONE for encrypted. */ } return true; } /* Helper for FUNC_NAME. */ static int iterate_device (const char *name, void *data) { struct search_ctx *ctx = data; int found = 0; /* Skip floppy drives when requested. */ if (ctx->flags & SEARCH_FLAGS_NO_FLOPPY && name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9') return 0; /* Limit to EFI disks when requested. */ if (ctx->flags & SEARCH_FLAGS_EFIDISK_ONLY) { VasEBoot_device_t dev; dev = VasEBoot_device_open (name); if (dev == NULL) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; return 0; } if (dev->disk == NULL || dev->disk->dev->id != VAS_EBOOT_DISK_DEVICE_EFIDISK_ID) { VasEBoot_device_close (dev); VasEBoot_errno = VAS_EBOOT_ERR_NONE; return 0; } VasEBoot_device_close (dev); } /* Limit to encrypted disks when requested. */ if (ctx->flags & SEARCH_FLAGS_CRYPTODISK_ONLY) { VasEBoot_device_t dev; dev = VasEBoot_device_open (name); if (dev == NULL) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; return 0; } if (dev->disk == NULL || is_unencrypted_disk (dev->disk) == true) { VasEBoot_device_close (dev); VasEBoot_errno = VAS_EBOOT_ERR_NONE; return 0; } VasEBoot_device_close (dev); } #ifdef DO_SEARCH_FS_UUID #define compare_fn VasEBoot_strcasecmp #else #define compare_fn VasEBoot_strcmp #endif #ifdef DO_SEARCH_FILE { char *buf; VasEBoot_file_t file; buf = VasEBoot_xasprintf ("(%s)%s", name, ctx->key); if (! buf) return 1; file = VasEBoot_file_open (buf, VAS_EBOOT_FILE_TYPE_FS_SEARCH | VAS_EBOOT_FILE_TYPE_NO_DECOMPRESS); if (file) { found = 1; VasEBoot_file_close (file); } VasEBoot_free (buf); } #else { /* SEARCH_FS_UUID or SEARCH_LABEL */ VasEBoot_device_t dev; VasEBoot_fs_t fs; char *quid; dev = VasEBoot_device_open (name); if (dev) { fs = VasEBoot_fs_probe (dev); #ifdef DO_SEARCH_FS_UUID #define read_fn fs_uuid #else #define read_fn fs_label #endif if (fs && fs->read_fn) { fs->read_fn (dev, &quid); if (VasEBoot_errno == VAS_EBOOT_ERR_NONE && quid) { if (compare_fn (quid, ctx->key) == 0) found = 1; VasEBoot_free (quid); } } VasEBoot_device_close (dev); } } #endif if (!ctx->is_cache && found && ctx->count == 0) { struct cache_entry *cache_ent; cache_ent = VasEBoot_malloc (sizeof (*cache_ent)); if (cache_ent) { cache_ent->key = VasEBoot_strdup (ctx->key); cache_ent->value = VasEBoot_strdup (name); if (cache_ent->value && cache_ent->key) { cache_ent->next = cache; cache = cache_ent; } else { VasEBoot_free (cache_ent->value); VasEBoot_free (cache_ent->key); VasEBoot_free (cache_ent); VasEBoot_errno = VAS_EBOOT_ERR_NONE; } } else VasEBoot_errno = VAS_EBOOT_ERR_NONE; } if (found) { ctx->count++; if (ctx->var) VasEBoot_env_set (ctx->var, name); else VasEBoot_printf (" %s", name); } VasEBoot_errno = VAS_EBOOT_ERR_NONE; return (found && ctx->var); } /* Helper for FUNC_NAME. */ static int part_hook (VasEBoot_disk_t disk, const VasEBoot_partition_t partition, void *data) { struct search_ctx *ctx = data; char *partition_name, *devname; int ret; partition_name = VasEBoot_partition_get_name (partition); if (! partition_name) return 1; devname = VasEBoot_xasprintf ("%s,%s", disk->name, partition_name); VasEBoot_free (partition_name); if (!devname) return 1; ret = iterate_device (devname, ctx); VasEBoot_free (devname); return ret; } /* Helper for FUNC_NAME. */ static void try (struct search_ctx *ctx) { unsigned i; struct cache_entry **prev; struct cache_entry *cache_ent; for (prev = &cache, cache_ent = *prev; cache_ent; prev = &cache_ent->next, cache_ent = *prev) if (compare_fn (cache_ent->key, ctx->key) == 0) break; if (cache_ent) { ctx->is_cache = 1; if (iterate_device (cache_ent->value, ctx)) { ctx->is_cache = 0; return; } ctx->is_cache = 0; /* Cache entry was outdated. Remove it. */ if (!ctx->count) { *prev = cache_ent->next; VasEBoot_free (cache_ent->key); VasEBoot_free (cache_ent->value); VasEBoot_free (cache_ent); } } for (i = 0; i < ctx->nhints; i++) { char *end; if (!ctx->hints[i][0]) continue; end = ctx->hints[i] + VasEBoot_strlen (ctx->hints[i]) - 1; if (*end == ',') *end = 0; if (iterate_device (ctx->hints[i], ctx)) { if (!*end) *end = ','; return; } if (!*end) { VasEBoot_device_t dev; int ret; dev = VasEBoot_device_open (ctx->hints[i]); if (!dev) { if (!*end) *end = ','; continue; } if (!dev->disk) { VasEBoot_device_close (dev); if (!*end) *end = ','; continue; } ret = VasEBoot_partition_iterate (dev->disk, part_hook, ctx); if (!*end) *end = ','; VasEBoot_device_close (dev); if (ret) return; } } VasEBoot_device_iterate (iterate_device, ctx); } void FUNC_NAME (const char *key, const char *var, enum search_flags flags, char **hints, unsigned nhints) { struct search_ctx ctx = { .key = key, .var = var, .flags = flags, .hints = hints, .nhints = nhints, .count = 0, .is_cache = 0 }; VasEBoot_fs_autoload_hook_t saved_autoload; /* First try without autoloading if we're setting variable. */ if (var) { saved_autoload = VasEBoot_fs_autoload_hook; VasEBoot_fs_autoload_hook = 0; try (&ctx); /* Restore autoload hook. */ VasEBoot_fs_autoload_hook = saved_autoload; /* Retry with autoload if nothing found. */ if (VasEBoot_errno == VAS_EBOOT_ERR_NONE && ctx.count == 0) try (&ctx); } else try (&ctx); if (VasEBoot_errno == VAS_EBOOT_ERR_NONE && ctx.count == 0) VasEBoot_error (VAS_EBOOT_ERR_FILE_NOT_FOUND, "no such device: %s", key); } static VasEBoot_err_t VasEBoot_cmd_do_search (VasEBoot_command_t cmd __attribute__ ((unused)), int argc, char **args) { if (argc == 0) return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("one argument expected")); FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0, (args + 2), argc > 2 ? argc - 2 : 0); return VasEBoot_errno; } static VasEBoot_command_t cmd; #ifdef DO_SEARCH_FILE VAS_EBOOT_MOD_INIT(search_fs_file) #elif defined (DO_SEARCH_FS_UUID) VAS_EBOOT_MOD_INIT(search_fs_uuid) #else VAS_EBOOT_MOD_INIT(search_label) #endif { cmd = VasEBoot_register_command (COMMAND_NAME, VasEBoot_cmd_do_search, N_("NAME [VARIABLE] [HINTS]"), HELP_MESSAGE); } #ifdef DO_SEARCH_FILE VAS_EBOOT_MOD_FINI(search_fs_file) #elif defined (DO_SEARCH_FS_UUID) VAS_EBOOT_MOD_FINI(search_fs_uuid) #else VAS_EBOOT_MOD_FINI(search_label) #endif { VasEBoot_unregister_command (cmd); }