vaseboot/VasEBoot-core/normal/crypto.c

164 lines
3.5 KiB
C

/* crypto.c - support crypto autoload */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2009 Free Software Foundation, Inc.
*
* VasEBoot 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.
*
* VasEBoot 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 VasEBoot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/dl.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/env.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/crypto.h>
#include <VasEBoot/normal.h>
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 = VasEBoot_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 = VasEBoot_ERR_NONE;
return;
}
filename = VasEBoot_xasprintf ("%s/" VasEBoot_TARGET_CPU "-" VasEBoot_PLATFORM
"/crypto.lst", prefix);
if (!filename)
{
VasEBoot_errno = VasEBoot_ERR_NONE;
return;
}
file = VasEBoot_file_open (filename);
VasEBoot_free (filename);
if (!file)
{
VasEBoot_errno = VasEBoot_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 = VasEBoot_ERR_NONE;
continue;
}
cur->name = VasEBoot_strdup (name);
if (! cur->name)
{
VasEBoot_errno = VasEBoot_ERR_NONE;
VasEBoot_free (cur);
continue;
}
cur->modname = VasEBoot_strdup (p);
if (! cur->modname)
{
VasEBoot_errno = VasEBoot_ERR_NONE;
VasEBoot_free (cur->name);
VasEBoot_free (cur);
continue;
}
cur->next = crypto_specs;
crypto_specs = cur;
}
VasEBoot_file_close (file);
VasEBoot_errno = VasEBoot_ERR_NONE;
VasEBoot_crypto_autoload_hook = VasEBoot_crypto_autoload;
}