1 /* $NetBSD: fileload.c,v 1.1 2006/04/07 14:21:29 cherry Exp $ */
4 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
32 * file/module function dispatcher, support, etc.
35 #include <lib/libsa/stand.h>
36 #include <sys/param.h>
38 #include <sys/queue.h>
40 #include "bootstrap.h"
42 static int file_load(char *filename
, vaddr_t dest
, struct preloaded_file
**result
);
43 static int file_havepath(const char *name
);
44 static void file_insert_tail(struct preloaded_file
*mp
);
46 /* load address should be tweaked by first module loaded (kernel) */
47 static vaddr_t loadaddr
= 0;
49 struct preloaded_file
*preloaded_files
= NULL
;
52 * load a kernel from disk.
54 * kernels are loaded as:
56 * load <path> <options>
60 command_load(int argc
, char *argv
[])
63 int dofile
, dokld
, ch
, error
;
70 command_errmsg
= "no filename specified";
73 while ((ch
= getopt(argc
, argv
, "k:")) != -1) {
80 /* getopt has already reported an error */
88 * Do we have explicit KLD load ?
90 if (dokld
|| file_havepath(argv
[1])) {
91 error
= file_loadkernel(argv
[1], argc
- 2, argv
+ 2);
93 sprintf(command_errbuf
, "warning: KLD '%s' already loaded", argv
[1]);
95 return (error
== 0 ? CMD_OK
: CMD_ERROR
);
100 command_unload(int argc
, char *argv
[])
102 struct preloaded_file
*fp
;
104 while (preloaded_files
!= NULL
) {
105 fp
= preloaded_files
;
106 preloaded_files
= preloaded_files
->f_next
;
110 unsetenv("kernelname");
116 command_lskern(int argc
, char *argv
[])
118 struct preloaded_file
*fp
;
127 for (fp
= preloaded_files
; fp
; fp
= fp
->f_next
) {
128 sprintf(lbuf
, " %p: %s (%s, 0x%lx)\n",
129 (void *) fp
->f_addr
, fp
->f_name
, fp
->f_type
, (long) fp
->f_size
);
131 if (fp
->f_args
!= NULL
) {
132 pager_output(" args: ");
133 pager_output(fp
->f_args
);
142 * File level interface, functions file_*
145 file_load(char *filename
, vaddr_t dest
, struct preloaded_file
**result
)
147 struct preloaded_file
*fp
;
152 for (i
= 0, fp
= NULL
; file_formats
[i
] && fp
== NULL
; i
++) {
153 error
= (file_formats
[i
]->l_load
)(filename
, dest
, &fp
);
155 fp
->f_loader
= i
; /* remember the loader */
160 continue; /* Unknown to this handler? */
162 sprintf(command_errbuf
, "can't load file '%s': %s",
163 filename
, strerror(error
));
171 * Load specified KLD. If path is omitted, then try to locate it via
175 file_loadkernel(char *filename
, int argc
, char *argv
[])
177 struct preloaded_file
*fp
, *last_file
;
181 * Check if KLD already loaded
183 fp
= file_findfile(filename
, NULL
);
185 sprintf(command_errbuf
, "warning: KLD '%s' already loaded", filename
);
189 for (last_file
= preloaded_files
;
190 last_file
!= NULL
&& last_file
->f_next
!= NULL
;
191 last_file
= last_file
->f_next
)
195 err
= file_load(filename
, loadaddr
, &fp
);
198 fp
->f_args
= unargv(argc
, argv
);
199 loadaddr
= fp
->f_addr
+ fp
->f_size
;
200 file_insert_tail(fp
); /* Add to the list of loaded files */
203 sprintf(command_errbuf
, "don't know how to load module '%s'", filename
);
211 * Find a file matching (name) and (type).
212 * NULL may be passed as a wildcard to either.
214 struct preloaded_file
*
215 file_findfile(char *name
, char *type
)
217 struct preloaded_file
*fp
;
219 for (fp
= preloaded_files
; fp
!= NULL
; fp
= fp
->f_next
) {
220 if (((name
== NULL
) || !strcmp(name
, fp
->f_name
)) &&
221 ((type
== NULL
) || !strcmp(type
, fp
->f_type
)))
228 * Check if file name have any qualifiers
231 file_havepath(const char *name
)
235 archsw
.arch_getdev(NULL
, name
, &cp
);
236 return (cp
!= name
|| strchr(name
, '/') != NULL
);
243 file_discard(struct preloaded_file
*fp
)
247 if (fp
->f_name
!= NULL
)
249 if (fp
->f_type
!= NULL
)
251 if (fp
->f_args
!= NULL
)
253 if (fp
->marks
!= NULL
)
259 * Allocate a new file; must be used instead of malloc()
260 * to ensure safe initialisation.
262 struct preloaded_file
*
265 struct preloaded_file
*fp
;
267 if ((fp
= alloc(sizeof(struct preloaded_file
))) != NULL
) {
268 memset(fp
, 0, sizeof(struct preloaded_file
));
270 if (fp
->marks
= alloc(sizeof(u_long
))) {
271 memset(fp
->marks
, 0, sizeof(u_long
));
278 * Add a module to the chain
281 file_insert_tail(struct preloaded_file
*fp
)
283 struct preloaded_file
*cm
;
285 /* Append to list of loaded file */
287 if (preloaded_files
== NULL
) {
288 preloaded_files
= fp
;
290 for (cm
= preloaded_files
; cm
->f_next
!= NULL
; cm
= cm
->f_next
)