2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/boot/common/module.c,v 1.13.2.4 2002/12/19 12:41:10 thomas Exp $
30 * module function dispatcher, support, etc.
35 #include <sys/param.h>
36 #include <sys/linker.h>
38 #include "bootstrap.h"
40 static int mod_loadmodule(char *name, int argc, char *argv[], struct loaded_module **mpp);
41 static int file_load_dependancies(struct loaded_module *base_file);
42 static char *mod_searchfile(char *name);
43 static char *mod_searchmodule(char *name);
44 static void mod_append(struct loaded_module *mp);
45 static struct module_metadata *metadata_next(struct module_metadata *md, int type);
47 /* load address should be tweaked by first module loaded (kernel) */
48 static vm_offset_t loadaddr = 0;
50 static const char *default_searchpath ="/;/boot;/modules";
52 struct loaded_module *loaded_modules = NULL;
55 * load an object, either a disk file or code module.
57 * To load a file, the syntax is:
59 * load -t <type> <path>
61 * code modules are loaded as:
63 * load <path> <options>
66 COMMAND_SET(load, "load", "load a kernel or module", command_load);
69 command_load(int argc, char *argv[])
72 int dofile, ch, error;
79 command_errmsg = "no filename specified";
82 while ((ch = getopt(argc, argv, "t:")) != -1) {
90 /* getopt has already reported an error */
98 * Request to load a raw file?
101 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
102 command_errmsg = "invalid load type";
105 return(mod_loadobj(typestr, argv[1]));
109 * Looks like a request for a module.
111 error = mod_load(argv[1], argc - 2, argv + 2);
113 sprintf(command_errbuf, "warning: module '%s' already loaded", argv[1]);
114 return (error == 0 ? CMD_OK : CMD_ERROR);
117 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
120 command_unload(int argc, char *argv[])
122 struct loaded_module *mp;
124 while (loaded_modules != NULL) {
126 loaded_modules = loaded_modules->m_next;
133 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
136 command_lsmod(int argc, char *argv[])
138 struct loaded_module *am;
139 struct module_metadata *md;
146 while ((ch = getopt(argc, argv, "v")) != -1) {
153 /* getopt has already reported an error */
159 for (am = loaded_modules; (am != NULL); am = am->m_next) {
160 sprintf(lbuf, " %p: %s (%s, 0x%lx)\n",
161 (void *) am->m_addr, am->m_name, am->m_type, (long) am->m_size);
163 if (am->m_args != NULL) {
164 pager_output(" args: ");
165 pager_output(am->m_args);
169 /* XXX could add some formatting smarts here to display some better */
170 for (md = am->m_metadata; md != NULL; md = md->md_next) {
171 sprintf(lbuf, " 0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
180 * We've been asked to load (name) and give it (argc),(argv).
181 * Start by trying to load it, and then attempt to load all of its
182 * dependancies. If we fail at any point, throw them all away and
183 * fail the entire load.
185 * XXX if a depended-on module requires arguments, it must be loaded
189 mod_load(char *name, int argc, char *argv[])
191 struct loaded_module *last_mod, *base_mod, *mp;
194 /* remember previous last module on chain */
195 for (last_mod = loaded_modules;
196 (last_mod != NULL) && (last_mod->m_next != NULL);
197 last_mod = last_mod->m_next)
201 * Load the first module; note that it's the only one that gets
202 * arguments explicitly.
204 error = mod_loadmodule(name, argc, argv, &base_mod);
208 error = file_load_dependancies(base_mod);
212 /* Load failed; discard everything */
213 last_mod->m_next = NULL;
214 loadaddr = last_mod->m_addr + last_mod->m_size;
215 while (base_mod != NULL) {
217 base_mod = base_mod->m_next;
224 * We've been asked to load (name) as (type), so just suck it in,
225 * no arguments or anything.
228 mod_loadobj(char *type, char *name)
230 struct loaded_module *mp;
235 /* We can't load first */
236 if ((mod_findmodule(NULL, NULL)) == NULL) {
237 command_errmsg = "can't load file before kernel";
241 /* locate the file on the load path */
242 cp = mod_searchfile(name);
244 sprintf(command_errbuf, "can't find '%s'", name);
249 if ((fd = open(name, O_RDONLY)) < 0) {
250 sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
257 /* read in 4k chunks; size is not really important */
258 got = archsw.arch_readin(fd, laddr, 4096);
259 if (got == 0) /* end of file */
261 if (got < 0) { /* error */
262 sprintf(command_errbuf, "error reading '%s': %s", name, strerror(errno));
270 /* Looks OK so far; create & populate control structure */
271 mp = malloc(sizeof(struct loaded_module));
273 mp->m_type = strdup(type);
275 mp->m_metadata = NULL;
277 mp->m_addr = loadaddr;
278 mp->m_size = laddr - loadaddr;
280 /* recognise space consumption */
283 /* Add to the list of loaded modules */
290 * Load the module (name), pass it (argc),(argv).
291 * Don't do any dependancy checking.
294 mod_loadmodule(char *name, int argc, char *argv[], struct loaded_module **mpp)
296 struct loaded_module *mp;
300 /* locate the module on the search path */
301 cp = mod_searchmodule(name);
303 sprintf(command_errbuf, "can't find '%s'", name);
308 cp = strrchr(name, '/');
313 /* see if module is already loaded */
314 mp = mod_findmodule(cp, NULL);
321 for (i = 0, mp = NULL; (module_formats[i] != NULL) && (mp == NULL); i++) {
322 if ((err = (module_formats[i]->l_load)(name, loadaddr, &mp)) != 0) {
324 /* Unknown to this handler? */
329 sprintf(command_errbuf, "can't load module '%s': %s", name, strerror(err));
334 /* Load was OK, set args */
335 mp->m_args = unargv(argc, argv);
337 /* where can we put the next one? */
338 loadaddr = mp->m_addr + mp->m_size;
340 /* remember the loader */
343 /* Add to the list of loaded modules */
351 sprintf(command_errbuf, "don't know how to load module '%s'", name);
357 file_load_dependancies(struct loaded_module *base_file)
359 struct module_metadata *md;
363 md = mod_findmetadata(base_file, MODINFOMD_DEPLIST);
368 dmodname = (char *)md->md_data;
369 if (mod_findmodule(NULL, dmodname) == NULL) {
370 printf("loading required module '%s'\n", dmodname);
371 error = mod_load(dmodname, 0, NULL);
377 md = metadata_next(md, MODINFOMD_DEPLIST);
383 * Find a module matching (name) and (type).
384 * NULL may be passed as a wildcard to either.
386 struct loaded_module *
387 mod_findmodule(char *name, char *type)
389 struct loaded_module *mp;
391 for (mp = loaded_modules; mp != NULL; mp = mp->m_next) {
392 if (((name == NULL) || !strcmp(name, mp->m_name)) &&
393 ((type == NULL) || !strcmp(type, mp->m_type)))
400 * Make a copy of (size) bytes of data from (p), and associate them as
401 * metadata of (type) to the module (mp).
404 mod_addmetadata(struct loaded_module *mp, int type, size_t size, void *p)
406 struct module_metadata *md;
408 md = malloc(sizeof(struct module_metadata) + size);
411 bcopy(p, md->md_data, size);
412 md->md_next = mp->m_metadata;
417 * Find a metadata object of (type) associated with the module
420 struct module_metadata *
421 mod_findmetadata(struct loaded_module *mp, int type)
423 struct module_metadata *md;
425 for (md = mp->m_metadata; md != NULL; md = md->md_next)
426 if (md->md_type == type)
431 struct module_metadata *
432 metadata_next(struct module_metadata *md, int type)
436 while((md = md->md_next) != NULL)
437 if (md->md_type == type)
443 * Attempt to find the file (name) on the module searchpath.
444 * If (name) is qualified in any way, we simply check it and
445 * return it or NULL. If it is not qualified, then we attempt
446 * to construct a path using entries in the environment variable
449 * The path we return a pointer to need never be freed, as we manage
453 mod_searchfile(char *name)
460 /* Don't look for nothing */
465 return(strdup(name));
468 * See if there's a device on the front, or a directory name.
470 archsw.arch_getdev(NULL, name, &cp);
471 if ((cp != name) || (strchr(name, '/') != NULL)) {
472 /* Qualified, so just see if it exists */
473 if (stat(name, &sb) == 0)
474 return(strdup(name));
479 * Get the module path
481 if ((cp = getenv("module_path")) == NULL)
482 cp = default_searchpath;
483 sp = path = strdup(cp);
486 * Traverse the path, splitting off ';'-delimited components.
489 while((cp = strsep(&path, ";")) != NULL) {
490 result = malloc(strlen(cp) + strlen(name) + 5);
492 if (cp[strlen(cp) - 1] != '/')
494 strcat(result, name);
495 /* printf("search '%s'\n", result); */
496 if ((stat(result, &sb) == 0) &&
507 * Attempt to locate the file containing the module (name)
510 mod_searchmodule(char *name)
514 /* Look for (name).ko */
515 tn = malloc(strlen(name) + 3 + 1);
518 result = mod_searchfile(tn);
520 /* Look for just (name) (useful for finding kernels) */
522 result = mod_searchfile(name);
529 * Throw a module away
532 mod_discard(struct loaded_module *mp)
534 struct module_metadata *md;
537 while (mp->m_metadata != NULL) {
539 mp->m_metadata = mp->m_metadata->md_next;
542 if (mp->m_name != NULL)
544 if (mp->m_type != NULL)
546 if (mp->m_args != NULL)
553 * Allocate a new module; must be used instead of malloc()
554 * to ensure safe initialisation.
556 struct loaded_module *
557 mod_allocmodule(void)
559 struct loaded_module *mp;
561 if ((mp = malloc(sizeof(struct loaded_module))) != NULL) {
562 bzero(mp, sizeof(struct loaded_module));
569 * Add a module to the chain
572 mod_append(struct loaded_module *mp)
574 struct loaded_module *cm;
576 /* Append to list of loaded modules */
578 if (loaded_modules == NULL) {
581 for (cm = loaded_modules; cm->m_next != NULL; cm = cm->m_next)