1998-08-21 03:17:42 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1998-08-21 03:17:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2000-05-01 17:41:25 +00:00
|
|
|
* file/module function dispatcher, support, etc.
|
1998-08-21 03:17:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stand.h>
|
|
|
|
#include <string.h>
|
1998-10-09 23:12:34 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/linker.h>
|
1998-08-21 03:17:42 +00:00
|
|
|
|
|
|
|
#include "bootstrap.h"
|
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
static int file_load(char *filename, vm_offset_t dest, struct preloaded_file **result);
|
|
|
|
static int file_loadraw(char *type, char *name);
|
|
|
|
static int file_load_dependancies(struct preloaded_file *base_mod);
|
|
|
|
static char * file_search(char *name);
|
|
|
|
struct kernel_module * file_findmodule(struct preloaded_file *fp, char *modname);
|
1998-09-03 02:10:09 +00:00
|
|
|
static char *mod_searchmodule(char *name);
|
2000-05-01 17:41:25 +00:00
|
|
|
static void file_insert_tail(struct preloaded_file *mp);
|
|
|
|
struct file_metadata* metadata_next(struct file_metadata *base_mp, int type);
|
1998-09-03 02:10:09 +00:00
|
|
|
|
1998-09-14 18:27:06 +00:00
|
|
|
/* load address should be tweaked by first module loaded (kernel) */
|
1998-08-21 03:17:42 +00:00
|
|
|
static vm_offset_t loadaddr = 0;
|
|
|
|
|
2000-09-05 22:37:46 +00:00
|
|
|
static const char *default_searchpath ="/boot/modules;/modules;/boot/kernel";
|
1998-09-14 18:27:06 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *preloaded_files = NULL;
|
1998-08-21 03:17:42 +00:00
|
|
|
|
1998-09-03 02:10:09 +00:00
|
|
|
/*
|
|
|
|
* load an object, either a disk file or code module.
|
|
|
|
*
|
|
|
|
* To load a file, the syntax is:
|
|
|
|
*
|
|
|
|
* load -t <type> <path>
|
|
|
|
*
|
|
|
|
* code modules are loaded as:
|
|
|
|
*
|
|
|
|
* load <path> <options>
|
|
|
|
*/
|
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
COMMAND_SET(load, "load", "load a kernel or module", command_load);
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_load(int argc, char *argv[])
|
|
|
|
{
|
1998-09-03 02:10:09 +00:00
|
|
|
char *typestr;
|
2000-02-25 05:10:44 +00:00
|
|
|
int dofile, ch, error;
|
1998-09-03 02:10:09 +00:00
|
|
|
|
|
|
|
dofile = 0;
|
|
|
|
optind = 1;
|
1999-01-11 06:41:32 +00:00
|
|
|
optreset = 1;
|
1998-09-03 02:10:09 +00:00
|
|
|
typestr = NULL;
|
1999-12-01 18:30:26 +00:00
|
|
|
if (argc == 1) {
|
|
|
|
command_errmsg = "no filename specified";
|
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
1998-09-03 02:10:09 +00:00
|
|
|
while ((ch = getopt(argc, argv, "t:")) != -1) {
|
|
|
|
switch(ch) {
|
|
|
|
case 't':
|
|
|
|
typestr = optarg;
|
|
|
|
dofile = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
/* getopt has already reported an error */
|
|
|
|
return(CMD_OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argv += (optind - 1);
|
|
|
|
argc -= (optind - 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request to load a raw file?
|
|
|
|
*/
|
|
|
|
if (dofile) {
|
|
|
|
if ((typestr == NULL) || (*typestr == 0)) {
|
|
|
|
command_errmsg = "invalid load type";
|
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
return(file_loadraw(typestr, argv[1]));
|
1998-09-03 02:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Looks like a request for a module.
|
|
|
|
*/
|
2000-02-25 05:10:44 +00:00
|
|
|
error = mod_load(argv[1], argc - 2, argv + 2);
|
|
|
|
if (error == EEXIST)
|
|
|
|
sprintf(command_errbuf, "warning: module '%s' already loaded", argv[1]);
|
|
|
|
return (error == 0 ? CMD_OK : CMD_ERROR);
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
|
|
|
|
1998-08-31 21:10:43 +00:00
|
|
|
COMMAND_SET(unload, "unload", "unload all modules", command_unload);
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_unload(int argc, char *argv[])
|
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *fp;
|
1998-08-31 21:10:43 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
while (preloaded_files != NULL) {
|
|
|
|
fp = preloaded_files;
|
|
|
|
preloaded_files = preloaded_files->f_next;
|
|
|
|
file_discard(fp);
|
1998-08-31 21:10:43 +00:00
|
|
|
}
|
|
|
|
loadaddr = 0;
|
2000-09-08 16:47:05 +00:00
|
|
|
unsetenv("kernelname");
|
1998-08-31 21:10:43 +00:00
|
|
|
return(CMD_OK);
|
|
|
|
}
|
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_lsmod(int argc, char *argv[])
|
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *fp;
|
|
|
|
struct kernel_module *mp;
|
|
|
|
struct file_metadata *md;
|
1998-08-21 03:17:42 +00:00
|
|
|
char lbuf[80];
|
1998-09-03 02:10:09 +00:00
|
|
|
int ch, verbose;
|
|
|
|
|
|
|
|
verbose = 0;
|
|
|
|
optind = 1;
|
1999-01-11 06:41:32 +00:00
|
|
|
optreset = 1;
|
1998-09-03 02:10:09 +00:00
|
|
|
while ((ch = getopt(argc, argv, "v")) != -1) {
|
|
|
|
switch(ch) {
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
/* getopt has already reported an error */
|
|
|
|
return(CMD_OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
pager_open();
|
2000-05-01 17:41:25 +00:00
|
|
|
for (fp = preloaded_files; fp; fp = fp->f_next) {
|
1998-09-26 10:51:38 +00:00
|
|
|
sprintf(lbuf, " %p: %s (%s, 0x%lx)\n",
|
2000-05-01 17:41:25 +00:00
|
|
|
(void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
|
1998-08-21 03:17:42 +00:00
|
|
|
pager_output(lbuf);
|
2000-05-01 17:41:25 +00:00
|
|
|
if (fp->f_args != NULL) {
|
1998-08-21 03:17:42 +00:00
|
|
|
pager_output(" args: ");
|
2000-05-01 17:41:25 +00:00
|
|
|
pager_output(fp->f_args);
|
1998-08-21 03:17:42 +00:00
|
|
|
pager_output("\n");
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
if (fp->f_modules) {
|
|
|
|
pager_output(" modules: ");
|
|
|
|
for (mp = fp->f_modules; mp; mp = mp->m_next) {
|
|
|
|
sprintf(lbuf, "%s ", mp->m_name);
|
|
|
|
pager_output(lbuf);
|
|
|
|
}
|
|
|
|
pager_output("\n");
|
|
|
|
}
|
|
|
|
if (verbose) {
|
1998-09-03 02:10:09 +00:00
|
|
|
/* XXX could add some formatting smarts here to display some better */
|
2000-05-01 17:41:25 +00:00
|
|
|
for (md = fp->f_metadata; md != NULL; md = md->md_next) {
|
1998-09-26 10:51:38 +00:00
|
|
|
sprintf(lbuf, " 0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
|
1998-09-03 02:10:09 +00:00
|
|
|
pager_output(lbuf);
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
}
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
|
|
|
pager_close();
|
|
|
|
return(CMD_OK);
|
|
|
|
}
|
|
|
|
|
1998-09-03 02:10:09 +00:00
|
|
|
/*
|
2000-05-01 17:41:25 +00:00
|
|
|
* File level interface, functions file_*
|
1998-09-03 02:10:09 +00:00
|
|
|
*/
|
1998-08-21 03:17:42 +00:00
|
|
|
int
|
2000-05-01 17:41:25 +00:00
|
|
|
file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
|
1998-08-21 03:17:42 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *fp;
|
|
|
|
int error;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
error = EFTYPE;
|
|
|
|
for (i = 0, fp = NULL; file_formats[i] && fp == NULL; i++) {
|
|
|
|
error = (file_formats[i]->l_load)(filename, loadaddr, &fp);
|
|
|
|
if (error == 0) {
|
|
|
|
fp->f_loader = i; /* remember the loader */
|
|
|
|
*result = fp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (error == EFTYPE)
|
|
|
|
continue; /* Unknown to this handler? */
|
|
|
|
if (error) {
|
|
|
|
sprintf(command_errbuf, "can't load file '%s': %s",
|
|
|
|
filename, strerror(error));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
1998-09-03 02:10:09 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
static int
|
|
|
|
file_load_dependancies(struct preloaded_file *base_file) {
|
|
|
|
struct file_metadata *md;
|
|
|
|
struct preloaded_file *fp;
|
|
|
|
char *dmodname;
|
|
|
|
int error;
|
2000-02-25 05:10:44 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
|
|
|
|
if (md == NULL)
|
|
|
|
return (0);
|
|
|
|
error = 0;
|
|
|
|
do {
|
|
|
|
dmodname = (char *)md->md_data;
|
|
|
|
if (file_findmodule(NULL, dmodname) == NULL) {
|
|
|
|
printf("loading required module '%s'\n", dmodname);
|
|
|
|
error = mod_load(dmodname, 0, NULL);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
md = metadata_next(md, MODINFOMD_DEPLIST);
|
|
|
|
} while (md);
|
2000-02-25 05:10:44 +00:00
|
|
|
if (!error)
|
|
|
|
return (0);
|
|
|
|
/* Load failed; discard everything */
|
2000-05-01 17:41:25 +00:00
|
|
|
while (base_file != NULL) {
|
|
|
|
fp = base_file;
|
|
|
|
base_file = base_file->f_next;
|
|
|
|
file_discard(fp);
|
1998-09-03 02:10:09 +00:00
|
|
|
}
|
2000-02-25 05:10:44 +00:00
|
|
|
return (error);
|
1998-09-03 02:10:09 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We've been asked to load (name) as (type), so just suck it in,
|
|
|
|
* no arguments or anything.
|
|
|
|
*/
|
|
|
|
int
|
2000-05-01 17:41:25 +00:00
|
|
|
file_loadraw(char *type, char *name)
|
1998-09-03 02:10:09 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *fp;
|
1998-09-03 02:10:09 +00:00
|
|
|
char *cp;
|
|
|
|
int fd, got;
|
|
|
|
vm_offset_t laddr;
|
|
|
|
|
|
|
|
/* We can't load first */
|
2000-05-01 17:41:25 +00:00
|
|
|
if ((file_findfile(NULL, NULL)) == NULL) {
|
1998-09-03 02:10:09 +00:00
|
|
|
command_errmsg = "can't load file before kernel";
|
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
|
|
|
|
1998-09-14 18:27:06 +00:00
|
|
|
/* locate the file on the load path */
|
2000-05-01 17:41:25 +00:00
|
|
|
cp = file_search(name);
|
1998-09-14 18:27:06 +00:00
|
|
|
if (cp == NULL) {
|
|
|
|
sprintf(command_errbuf, "can't find '%s'", name);
|
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
|
|
|
name = cp;
|
1998-09-03 02:10:09 +00:00
|
|
|
|
|
|
|
if ((fd = open(name, O_RDONLY)) < 0) {
|
|
|
|
sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
|
1999-03-08 10:32:39 +00:00
|
|
|
free(name);
|
1998-09-03 02:10:09 +00:00
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
laddr = loadaddr;
|
|
|
|
for (;;) {
|
|
|
|
/* read in 4k chunks; size is not really important */
|
|
|
|
got = archsw.arch_readin(fd, laddr, 4096);
|
|
|
|
if (got == 0) /* end of file */
|
|
|
|
break;
|
|
|
|
if (got < 0) { /* error */
|
|
|
|
sprintf(command_errbuf, "error reading '%s': %s", name, strerror(errno));
|
1999-02-22 13:12:37 +00:00
|
|
|
free(name);
|
2000-02-17 02:19:19 +00:00
|
|
|
close(fd);
|
1998-09-03 02:10:09 +00:00
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
|
|
|
laddr += got;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Looks OK so far; create & populate control structure */
|
2000-05-01 17:41:25 +00:00
|
|
|
fp = file_alloc();
|
|
|
|
fp->f_name = name;
|
|
|
|
fp->f_type = strdup(type);
|
|
|
|
fp->f_args = NULL;
|
|
|
|
fp->f_metadata = NULL;
|
|
|
|
fp->f_loader = -1;
|
|
|
|
fp->f_addr = loadaddr;
|
|
|
|
fp->f_size = laddr - loadaddr;
|
1998-09-03 02:10:09 +00:00
|
|
|
|
|
|
|
/* recognise space consumption */
|
|
|
|
loadaddr = laddr;
|
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
/* Add to the list of loaded files */
|
|
|
|
file_insert_tail(fp);
|
2000-02-17 02:19:19 +00:00
|
|
|
close(fd);
|
1998-09-03 02:10:09 +00:00
|
|
|
return(CMD_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-05-01 17:41:25 +00:00
|
|
|
* Load the module (name), pass it (argc),(argv), add container file
|
|
|
|
* to the list of loaded files.
|
|
|
|
* If module is already loaded just assign new argc/argv.
|
1998-09-03 02:10:09 +00:00
|
|
|
*/
|
2000-05-01 17:41:25 +00:00
|
|
|
int
|
|
|
|
mod_load(char *modname, int argc, char *argv[])
|
1998-09-03 02:10:09 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *fp, *last_file;
|
|
|
|
struct kernel_module *mp;
|
|
|
|
int err;
|
|
|
|
char *filename;
|
1998-09-03 02:10:09 +00:00
|
|
|
|
2000-02-25 05:10:44 +00:00
|
|
|
/* see if module is already loaded */
|
2000-05-01 17:41:25 +00:00
|
|
|
mp = file_findmodule(NULL, modname);
|
2000-02-25 05:10:44 +00:00
|
|
|
if (mp) {
|
2000-05-01 17:41:25 +00:00
|
|
|
#ifdef moduleargs
|
|
|
|
if (mp->m_args)
|
|
|
|
free(mp->m_args);
|
|
|
|
mp->m_args = unargv(argc, argv);
|
|
|
|
#endif
|
|
|
|
sprintf(command_errbuf, "warning: module '%s' already loaded", mp->m_name);
|
|
|
|
return (0);
|
2000-02-25 05:10:44 +00:00
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
/* locate file with the module on the search path */
|
|
|
|
filename = mod_searchmodule(modname);
|
|
|
|
if (filename == NULL) {
|
|
|
|
sprintf(command_errbuf, "can't find '%s'", modname);
|
|
|
|
return (ENOENT);
|
|
|
|
}
|
|
|
|
for (last_file = preloaded_files;
|
|
|
|
last_file != NULL && last_file->f_next != NULL;
|
|
|
|
last_file = last_file->f_next)
|
|
|
|
;
|
2000-02-25 05:10:44 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
fp = NULL;
|
|
|
|
do {
|
|
|
|
err = file_load(filename, loadaddr, &fp);
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
#ifdef moduleargs
|
|
|
|
mp = file_findmodule(fp, modname);
|
|
|
|
if (mp == NULL) {
|
|
|
|
sprintf(command_errbuf, "module '%s' not found in the file '%s': %s",
|
|
|
|
modname, filename, strerror(err));
|
|
|
|
err = ENOENT;
|
1998-09-03 02:10:09 +00:00
|
|
|
break;
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
mp->m_args = unargv(argc, argv);
|
|
|
|
#else
|
|
|
|
fp->f_args = unargv(argc, argv);
|
|
|
|
#endif
|
|
|
|
loadaddr = fp->f_addr + fp->f_size;
|
|
|
|
file_insert_tail(fp); /* Add to the list of loaded files */
|
|
|
|
if (file_load_dependancies(fp) != 0) {
|
|
|
|
err = ENOENT;
|
|
|
|
last_file->f_next = NULL;
|
|
|
|
loadaddr = last_file->f_addr + last_file->f_size;
|
|
|
|
fp = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(0);
|
1998-09-03 02:10:09 +00:00
|
|
|
if (err == EFTYPE)
|
2000-05-01 17:41:25 +00:00
|
|
|
sprintf(command_errbuf, "don't know how to load module '%s'", filename);
|
|
|
|
if (err && fp)
|
|
|
|
file_discard(fp);
|
|
|
|
free(filename);
|
2000-02-25 05:10:44 +00:00
|
|
|
return (err);
|
1998-09-03 02:10:09 +00:00
|
|
|
}
|
1998-08-21 03:17:42 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
/*
|
|
|
|
* Find a file matching (name) and (type).
|
|
|
|
* NULL may be passed as a wildcard to either.
|
|
|
|
*/
|
|
|
|
struct preloaded_file *
|
|
|
|
file_findfile(char *name, char *type)
|
1998-09-03 02:10:09 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *fp;
|
2000-02-25 05:10:44 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
|
|
|
|
if (((name == NULL) || !strcmp(name, fp->f_name)) &&
|
|
|
|
((type == NULL) || !strcmp(type, fp->f_type)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (fp);
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
|
|
|
|
1998-09-03 02:10:09 +00:00
|
|
|
/*
|
2000-05-01 17:41:25 +00:00
|
|
|
* Find a module matching (name) inside of given file.
|
|
|
|
* NULL may be passed as a wildcard.
|
1998-09-03 02:10:09 +00:00
|
|
|
*/
|
2000-05-01 17:41:25 +00:00
|
|
|
struct kernel_module *
|
|
|
|
file_findmodule(struct preloaded_file *fp, char *modname)
|
1998-08-21 03:17:42 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct kernel_module *mp;
|
|
|
|
|
|
|
|
if (fp == NULL) {
|
|
|
|
for (fp = preloaded_files; fp; fp = fp->f_next) {
|
|
|
|
for (mp = fp->f_modules; mp; mp = mp->m_next) {
|
|
|
|
if (strcmp(modname, mp->m_name) == 0)
|
|
|
|
return (mp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
for (mp = fp->f_modules; mp; mp = mp->m_next) {
|
|
|
|
if (strcmp(modname, mp->m_name) == 0)
|
|
|
|
return (mp);
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
return (NULL);
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
1998-09-03 02:10:09 +00:00
|
|
|
/*
|
|
|
|
* Make a copy of (size) bytes of data from (p), and associate them as
|
|
|
|
* metadata of (type) to the module (mp).
|
|
|
|
*/
|
1998-08-31 21:10:43 +00:00
|
|
|
void
|
2000-05-01 17:41:25 +00:00
|
|
|
file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
|
1998-08-31 21:10:43 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct file_metadata *md;
|
1998-08-31 21:10:43 +00:00
|
|
|
|
2000-08-03 09:14:02 +00:00
|
|
|
md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
|
1998-08-31 21:10:43 +00:00
|
|
|
md->md_size = size;
|
|
|
|
md->md_type = type;
|
|
|
|
bcopy(p, md->md_data, size);
|
2000-05-01 17:41:25 +00:00
|
|
|
md->md_next = fp->f_metadata;
|
|
|
|
fp->f_metadata = md;
|
1998-08-31 21:10:43 +00:00
|
|
|
}
|
|
|
|
|
1998-09-03 02:10:09 +00:00
|
|
|
/*
|
2000-05-01 17:41:25 +00:00
|
|
|
* Find a metadata object of (type) associated with the file (fp)
|
1998-09-03 02:10:09 +00:00
|
|
|
*/
|
2000-05-01 17:41:25 +00:00
|
|
|
struct file_metadata *
|
|
|
|
file_findmetadata(struct preloaded_file *fp, int type)
|
1998-08-31 21:10:43 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct file_metadata *md;
|
1998-08-31 21:10:43 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
for (md = fp->f_metadata; md != NULL; md = md->md_next)
|
1998-08-31 21:10:43 +00:00
|
|
|
if (md->md_type == type)
|
|
|
|
break;
|
|
|
|
return(md);
|
|
|
|
}
|
1998-09-03 02:10:09 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
struct file_metadata *
|
|
|
|
metadata_next(struct file_metadata *md, int type)
|
2000-02-25 05:10:44 +00:00
|
|
|
{
|
|
|
|
if (md == NULL)
|
|
|
|
return (NULL);
|
|
|
|
while((md = md->md_next) != NULL)
|
|
|
|
if (md->md_type == type)
|
|
|
|
break;
|
|
|
|
return (md);
|
|
|
|
}
|
|
|
|
|
1998-09-03 02:10:09 +00:00
|
|
|
/*
|
1998-09-14 18:27:06 +00:00
|
|
|
* Attempt to find the file (name) on the module searchpath.
|
1998-09-03 02:10:09 +00:00
|
|
|
* If (name) is qualified in any way, we simply check it and
|
|
|
|
* return it or NULL. If it is not qualified, then we attempt
|
|
|
|
* to construct a path using entries in the environment variable
|
|
|
|
* module_path.
|
|
|
|
*
|
|
|
|
* The path we return a pointer to need never be freed, as we manage
|
|
|
|
* it internally.
|
|
|
|
*/
|
|
|
|
static char *
|
2000-05-01 17:41:25 +00:00
|
|
|
file_search(char *name)
|
1998-09-03 02:10:09 +00:00
|
|
|
{
|
1999-03-08 10:32:39 +00:00
|
|
|
char *result;
|
1998-09-26 10:51:38 +00:00
|
|
|
char *path, *sp;
|
|
|
|
const char *cp;
|
1998-09-03 02:10:09 +00:00
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
/* Don't look for nothing */
|
1999-02-22 13:12:37 +00:00
|
|
|
if (name == NULL)
|
1998-09-03 02:10:09 +00:00
|
|
|
return(name);
|
|
|
|
|
1999-02-22 13:12:37 +00:00
|
|
|
if (*name == 0)
|
|
|
|
return(strdup(name));
|
|
|
|
|
1998-09-03 02:10:09 +00:00
|
|
|
/*
|
|
|
|
* See if there's a device on the front, or a directory name.
|
|
|
|
*/
|
|
|
|
archsw.arch_getdev(NULL, name, &cp);
|
|
|
|
if ((cp != name) || (strchr(name, '/') != NULL)) {
|
|
|
|
/* Qualified, so just see if it exists */
|
|
|
|
if (stat(name, &sb) == 0)
|
1999-02-22 13:12:37 +00:00
|
|
|
return(strdup(name));
|
1998-09-03 02:10:09 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the module path
|
|
|
|
*/
|
|
|
|
if ((cp = getenv("module_path")) == NULL)
|
1998-09-14 18:27:06 +00:00
|
|
|
cp = default_searchpath;
|
1998-09-03 02:10:09 +00:00
|
|
|
sp = path = strdup(cp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Traverse the path, splitting off ';'-delimited components.
|
|
|
|
*/
|
1999-03-08 10:32:39 +00:00
|
|
|
result = NULL;
|
1998-09-03 02:10:09 +00:00
|
|
|
while((cp = strsep(&path, ";")) != NULL) {
|
1998-09-14 18:27:06 +00:00
|
|
|
result = malloc(strlen(cp) + strlen(name) + 5);
|
|
|
|
strcpy(result, cp);
|
|
|
|
if (cp[strlen(cp) - 1] != '/')
|
|
|
|
strcat(result, "/");
|
|
|
|
strcat(result, name);
|
|
|
|
if ((stat(result, &sb) == 0) &&
|
|
|
|
S_ISREG(sb.st_mode))
|
1998-09-03 02:10:09 +00:00
|
|
|
break;
|
|
|
|
free(result);
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
free(sp);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
1998-09-14 18:27:06 +00:00
|
|
|
/*
|
|
|
|
* Attempt to locate the file containing the module (name)
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
mod_searchmodule(char *name)
|
|
|
|
{
|
|
|
|
char *tn, *result;
|
|
|
|
|
|
|
|
/* Look for (name).ko */
|
1998-09-26 10:51:38 +00:00
|
|
|
tn = malloc(strlen(name) + 3 + 1);
|
1998-09-14 18:27:06 +00:00
|
|
|
strcpy(tn, name);
|
|
|
|
strcat(tn, ".ko");
|
2000-05-01 17:41:25 +00:00
|
|
|
result = file_search(tn);
|
1998-09-14 18:27:06 +00:00
|
|
|
free(tn);
|
|
|
|
/* Look for just (name) (useful for finding kernels) */
|
|
|
|
if (result == NULL)
|
2000-05-01 17:41:25 +00:00
|
|
|
result = file_search(name);
|
1998-09-14 18:27:06 +00:00
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
int
|
|
|
|
file_addmodule(struct preloaded_file *fp, char *modname,
|
|
|
|
struct kernel_module **newmp)
|
|
|
|
{
|
|
|
|
struct kernel_module *mp;
|
|
|
|
|
|
|
|
mp = file_findmodule(fp, modname);
|
|
|
|
if (mp)
|
|
|
|
return (EEXIST);
|
|
|
|
mp = malloc(sizeof(struct kernel_module));
|
|
|
|
if (mp == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
bzero(mp, sizeof(struct kernel_module));
|
|
|
|
mp->m_name = strdup(modname);
|
|
|
|
mp->m_fp = fp;
|
|
|
|
mp->m_next = fp->f_modules;
|
|
|
|
fp->f_modules = mp;
|
|
|
|
if (newmp)
|
|
|
|
*newmp = mp;
|
|
|
|
return (0);
|
|
|
|
}
|
1998-09-14 18:27:06 +00:00
|
|
|
|
1998-09-03 02:10:09 +00:00
|
|
|
/*
|
2000-05-01 17:41:25 +00:00
|
|
|
* Throw a file away
|
1998-09-03 02:10:09 +00:00
|
|
|
*/
|
|
|
|
void
|
2000-05-01 17:41:25 +00:00
|
|
|
file_discard(struct preloaded_file *fp)
|
1998-09-03 02:10:09 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct file_metadata *md, *md1;
|
|
|
|
struct kernel_module *mp, *mp1;
|
|
|
|
if (fp == NULL)
|
|
|
|
return;
|
|
|
|
md = fp->f_metadata;
|
|
|
|
while (md) {
|
|
|
|
md1 = md;
|
|
|
|
md = md->md_next;
|
|
|
|
free(md1);
|
1998-09-14 18:27:06 +00:00
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
mp = fp->f_modules;
|
|
|
|
while (mp) {
|
|
|
|
if (mp->m_name)
|
|
|
|
free(mp->m_name);
|
|
|
|
mp1 = mp;
|
|
|
|
mp = mp->m_next;
|
|
|
|
free(mp1);
|
|
|
|
}
|
|
|
|
if (fp->f_name != NULL)
|
|
|
|
free(fp->f_name);
|
|
|
|
if (fp->f_type != NULL)
|
|
|
|
free(fp->f_type);
|
|
|
|
if (fp->f_args != NULL)
|
|
|
|
free(fp->f_args);
|
|
|
|
free(fp);
|
1998-09-03 02:10:09 +00:00
|
|
|
}
|
|
|
|
|
1998-09-14 18:27:06 +00:00
|
|
|
/*
|
2000-05-01 17:41:25 +00:00
|
|
|
* Allocate a new file; must be used instead of malloc()
|
1998-09-14 18:27:06 +00:00
|
|
|
* to ensure safe initialisation.
|
|
|
|
*/
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *
|
|
|
|
file_alloc(void)
|
1998-09-14 18:27:06 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *fp;
|
1998-09-14 18:27:06 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
if ((fp = malloc(sizeof(struct preloaded_file))) != NULL) {
|
|
|
|
bzero(fp, sizeof(struct preloaded_file));
|
1998-09-14 18:27:06 +00:00
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
return (fp);
|
1998-09-14 18:27:06 +00:00
|
|
|
}
|
|
|
|
|
1998-09-03 02:10:09 +00:00
|
|
|
/*
|
|
|
|
* Add a module to the chain
|
|
|
|
*/
|
|
|
|
static void
|
2000-05-01 17:41:25 +00:00
|
|
|
file_insert_tail(struct preloaded_file *fp)
|
1998-09-03 02:10:09 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *cm;
|
1998-09-03 02:10:09 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
/* Append to list of loaded file */
|
|
|
|
fp->f_next = NULL;
|
|
|
|
if (preloaded_files == NULL) {
|
|
|
|
preloaded_files = fp;
|
1998-09-03 02:10:09 +00:00
|
|
|
} else {
|
2000-05-01 17:41:25 +00:00
|
|
|
for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
|
1998-09-03 02:10:09 +00:00
|
|
|
;
|
2000-05-01 17:41:25 +00:00
|
|
|
cm->f_next = fp;
|
1998-09-03 02:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
|