/* crypto.c - support crypto autoload */ /* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 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 struct load_spec { struct load_spec *next; char *name; char *modname; }; static struct load_spec *crypto_specs = NULL; static void VasEBoot_crypto_autoload (const char *name) { struct load_spec *cur; VasEBoot_dl_t mod; static int depth = 0; /* Some bufio of filesystems may want some crypto modules. It may result in infinite recursion. Hence this check. */ if (depth) return; depth++; for (cur = crypto_specs; cur; cur = cur->next) if (VasEBoot_strcasecmp (name, cur->name) == 0) { mod = VasEBoot_dl_load (cur->modname); if (mod) VasEBoot_dl_ref (mod); VasEBoot_errno = VAS_EBOOT_ERR_NONE; } depth--; } static void VasEBoot_crypto_spec_free (void) { struct load_spec *cur, *next; for (cur = crypto_specs; cur; cur = next) { next = cur->next; VasEBoot_free (cur->name); VasEBoot_free (cur->modname); VasEBoot_free (cur); } crypto_specs = NULL; } /* Read the file crypto.lst for auto-loading. */ void read_crypto_list (const char *prefix) { char *filename; VasEBoot_file_t file; char *buf = NULL; if (!prefix) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; return; } filename = VasEBoot_xasprintf ("%s/" VAS_EBOOT_TARGET_CPU "-" VAS_EBOOT_PLATFORM "/crypto.lst", prefix); if (!filename) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; return; } file = VasEBoot_file_open (filename, VAS_EBOOT_FILE_TYPE_VAS_EBOOT_MODULE_LIST); VasEBoot_free (filename); if (!file) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; return; } /* Override previous crypto.lst. */ VasEBoot_crypto_spec_free (); for (;; VasEBoot_free (buf)) { char *p, *name; struct load_spec *cur; buf = VasEBoot_file_getline (file); if (! buf) break; name = buf; while (VasEBoot_isspace (name[0])) name++; p = VasEBoot_strchr (name, ':'); if (! p) continue; *p = '\0'; p++; while (*p == ' ' || *p == '\t') p++; cur = VasEBoot_malloc (sizeof (*cur)); if (!cur) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; continue; } cur->name = VasEBoot_strdup (name); if (! cur->name) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; VasEBoot_free (cur); continue; } cur->modname = VasEBoot_strdup (p); if (! cur->modname) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; VasEBoot_free (cur->name); VasEBoot_free (cur); continue; } cur->next = crypto_specs; crypto_specs = cur; } VasEBoot_file_close (file); VasEBoot_errno = VAS_EBOOT_ERR_NONE; VasEBoot_crypto_autoload_hook = VasEBoot_crypto_autoload; }