#include "efiboot.h"
#include "module.h"
#include <sys/queue.h>
int module_enabled = 1;
char module_prefix[128];
struct boot_module {
char *module_name;
TAILQ_ENTRY(boot_module) entries;
};
TAILQ_HEAD(, boot_module) boot_modules = TAILQ_HEAD_INITIALIZER(boot_modules);
static const char *
module_machine(void)
{
return EFIBOOT_MODULE_MACHINE;
}
static void
module_set_prefix(const char *kernel_path)
{
#ifdef KERNEL_DIR
char *ptr = strrchr(kernel_path, '/');
if (ptr)
*ptr = '\0';
snprintf(module_prefix, sizeof(module_prefix), "%s/modules", kernel_path);
if (ptr)
*ptr = '/';
#else
const u_int vmajor = netbsd_version / 100000000;
const u_int vminor = netbsd_version / 1000000 % 100;
const u_int vpatch = netbsd_version / 100 % 10000;
if (vminor == 99) {
snprintf(module_prefix, sizeof(module_prefix),
"/stand/%s/%u.%u.%u/modules", module_machine(),
vmajor, vminor, vpatch);
} else if (vmajor != 0) {
snprintf(module_prefix, sizeof(module_prefix),
"/stand/%s/%u.%u/modules", module_machine(),
vmajor, vminor);
}
#endif
}
void
module_init(const char *kernel_path)
{
module_set_prefix(kernel_path);
}
void
module_foreach(void (*fn)(const char *))
{
struct boot_module *bm;
TAILQ_FOREACH(bm, &boot_modules, entries) {
fn(bm->module_name);
}
}
void
module_add(const char *module_name)
{
struct boot_module *bm;
while (*module_name == ' ' || *module_name == '\t')
++module_name;
TAILQ_FOREACH(bm, &boot_modules, entries) {
if (strcmp(bm->module_name, module_name) == 0)
return;
}
bm = alloc(sizeof(*bm));
const int slen = strlen(module_name) + 1;
bm->module_name = alloc(slen);
memcpy(bm->module_name, module_name, slen);
TAILQ_INSERT_TAIL(&boot_modules, bm, entries);
}
void
module_remove(const char *module_name)
{
struct boot_module *bm;
while (*module_name == ' ' || *module_name == '\t')
++module_name;
TAILQ_FOREACH(bm, &boot_modules, entries) {
if (strcmp(bm->module_name, module_name) == 0) {
TAILQ_REMOVE(&boot_modules, bm, entries);
dealloc(bm->module_name, strlen(bm->module_name) + 1);
dealloc(bm, sizeof(*bm));
return;
}
}
}
void
module_remove_all(void)
{
struct boot_module *bm;
while ((bm = TAILQ_FIRST(&boot_modules)) != NULL) {
TAILQ_REMOVE(&boot_modules, bm, entries);
dealloc(bm->module_name, strlen(bm->module_name) + 1);
dealloc(bm, sizeof(*bm));
}
}
void
module_enable(int onoff)
{
module_enabled = onoff;
}