fileload.c revision 1.1.6.2 1 1.1.6.2 simonb /* $NetBSD: fileload.c,v 1.1.6.2 2006/04/22 11:37:38 simonb Exp $ */
2 1.1.6.2 simonb
3 1.1.6.2 simonb /*-
4 1.1.6.2 simonb * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
5 1.1.6.2 simonb * All rights reserved.
6 1.1.6.2 simonb *
7 1.1.6.2 simonb * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 simonb * modification, are permitted provided that the following conditions
9 1.1.6.2 simonb * are met:
10 1.1.6.2 simonb * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 simonb * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 simonb * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 simonb * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 simonb * documentation and/or other materials provided with the distribution.
15 1.1.6.2 simonb *
16 1.1.6.2 simonb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1.6.2 simonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.6.2 simonb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.6.2 simonb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1.6.2 simonb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.6.2 simonb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.6.2 simonb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.6.2 simonb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.6.2 simonb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.6.2 simonb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.6.2 simonb * SUCH DAMAGE.
27 1.1.6.2 simonb */
28 1.1.6.2 simonb
29 1.1.6.2 simonb #include <sys/cdefs.h>
30 1.1.6.2 simonb
31 1.1.6.2 simonb /*
32 1.1.6.2 simonb * file/module function dispatcher, support, etc.
33 1.1.6.2 simonb */
34 1.1.6.2 simonb
35 1.1.6.2 simonb #include <lib/libsa/stand.h>
36 1.1.6.2 simonb #include <sys/param.h>
37 1.1.6.2 simonb #include <sys/lkm.h>
38 1.1.6.2 simonb #include <sys/queue.h>
39 1.1.6.2 simonb
40 1.1.6.2 simonb #include "bootstrap.h"
41 1.1.6.2 simonb
42 1.1.6.2 simonb static int file_load(char *filename, vaddr_t dest, struct preloaded_file **result);
43 1.1.6.2 simonb static int file_havepath(const char *name);
44 1.1.6.2 simonb static void file_insert_tail(struct preloaded_file *mp);
45 1.1.6.2 simonb
46 1.1.6.2 simonb /* load address should be tweaked by first module loaded (kernel) */
47 1.1.6.2 simonb static vaddr_t loadaddr = 0;
48 1.1.6.2 simonb
49 1.1.6.2 simonb struct preloaded_file *preloaded_files = NULL;
50 1.1.6.2 simonb
51 1.1.6.2 simonb /*
52 1.1.6.2 simonb * load a kernel from disk.
53 1.1.6.2 simonb *
54 1.1.6.2 simonb * kernels are loaded as:
55 1.1.6.2 simonb *
56 1.1.6.2 simonb * load <path> <options>
57 1.1.6.2 simonb */
58 1.1.6.2 simonb
59 1.1.6.2 simonb int
60 1.1.6.2 simonb command_load(int argc, char *argv[])
61 1.1.6.2 simonb {
62 1.1.6.2 simonb char *typestr;
63 1.1.6.2 simonb int dofile, dokld, ch, error;
64 1.1.6.2 simonb
65 1.1.6.2 simonb dokld = dofile = 0;
66 1.1.6.2 simonb optind = 1;
67 1.1.6.2 simonb optreset = 1;
68 1.1.6.2 simonb typestr = NULL;
69 1.1.6.2 simonb if (argc == 1) {
70 1.1.6.2 simonb command_errmsg = "no filename specified";
71 1.1.6.2 simonb return(CMD_ERROR);
72 1.1.6.2 simonb }
73 1.1.6.2 simonb while ((ch = getopt(argc, argv, "k:")) != -1) {
74 1.1.6.2 simonb switch(ch) {
75 1.1.6.2 simonb case 'k':
76 1.1.6.2 simonb dokld = 1;
77 1.1.6.2 simonb break;
78 1.1.6.2 simonb case '?':
79 1.1.6.2 simonb default:
80 1.1.6.2 simonb /* getopt has already reported an error */
81 1.1.6.2 simonb return(CMD_OK);
82 1.1.6.2 simonb }
83 1.1.6.2 simonb }
84 1.1.6.2 simonb argv += (optind - 1);
85 1.1.6.2 simonb argc -= (optind - 1);
86 1.1.6.2 simonb
87 1.1.6.2 simonb /*
88 1.1.6.2 simonb * Do we have explicit KLD load ?
89 1.1.6.2 simonb */
90 1.1.6.2 simonb if (dokld || file_havepath(argv[1])) {
91 1.1.6.2 simonb error = file_loadkernel(argv[1], argc - 2, argv + 2);
92 1.1.6.2 simonb if (error == EEXIST)
93 1.1.6.2 simonb sprintf(command_errbuf, "warning: KLD '%s' already loaded", argv[1]);
94 1.1.6.2 simonb }
95 1.1.6.2 simonb return (error == 0 ? CMD_OK : CMD_ERROR);
96 1.1.6.2 simonb }
97 1.1.6.2 simonb
98 1.1.6.2 simonb
99 1.1.6.2 simonb int
100 1.1.6.2 simonb command_unload(int argc, char *argv[])
101 1.1.6.2 simonb {
102 1.1.6.2 simonb struct preloaded_file *fp;
103 1.1.6.2 simonb
104 1.1.6.2 simonb while (preloaded_files != NULL) {
105 1.1.6.2 simonb fp = preloaded_files;
106 1.1.6.2 simonb preloaded_files = preloaded_files->f_next;
107 1.1.6.2 simonb file_discard(fp);
108 1.1.6.2 simonb }
109 1.1.6.2 simonb loadaddr = 0;
110 1.1.6.2 simonb unsetenv("kernelname");
111 1.1.6.2 simonb return(CMD_OK);
112 1.1.6.2 simonb }
113 1.1.6.2 simonb
114 1.1.6.2 simonb
115 1.1.6.2 simonb int
116 1.1.6.2 simonb command_lskern(int argc, char *argv[])
117 1.1.6.2 simonb {
118 1.1.6.2 simonb struct preloaded_file *fp;
119 1.1.6.2 simonb char lbuf[80];
120 1.1.6.2 simonb int ch, verbose;
121 1.1.6.2 simonb
122 1.1.6.2 simonb verbose = 0;
123 1.1.6.2 simonb optind = 1;
124 1.1.6.2 simonb optreset = 1;
125 1.1.6.2 simonb
126 1.1.6.2 simonb pager_open();
127 1.1.6.2 simonb for (fp = preloaded_files; fp; fp = fp->f_next) {
128 1.1.6.2 simonb sprintf(lbuf, " %p: %s (%s, 0x%lx)\n",
129 1.1.6.2 simonb (void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
130 1.1.6.2 simonb pager_output(lbuf);
131 1.1.6.2 simonb if (fp->f_args != NULL) {
132 1.1.6.2 simonb pager_output(" args: ");
133 1.1.6.2 simonb pager_output(fp->f_args);
134 1.1.6.2 simonb pager_output("\n");
135 1.1.6.2 simonb }
136 1.1.6.2 simonb }
137 1.1.6.2 simonb pager_close();
138 1.1.6.2 simonb return(CMD_OK);
139 1.1.6.2 simonb }
140 1.1.6.2 simonb
141 1.1.6.2 simonb /*
142 1.1.6.2 simonb * File level interface, functions file_*
143 1.1.6.2 simonb */
144 1.1.6.2 simonb int
145 1.1.6.2 simonb file_load(char *filename, vaddr_t dest, struct preloaded_file **result)
146 1.1.6.2 simonb {
147 1.1.6.2 simonb struct preloaded_file *fp;
148 1.1.6.2 simonb int error;
149 1.1.6.2 simonb int i;
150 1.1.6.2 simonb
151 1.1.6.2 simonb error = EFTYPE;
152 1.1.6.2 simonb for (i = 0, fp = NULL; file_formats[i] && fp == NULL; i++) {
153 1.1.6.2 simonb error = (file_formats[i]->l_load)(filename, dest, &fp);
154 1.1.6.2 simonb if (error == 0) {
155 1.1.6.2 simonb fp->f_loader = i; /* remember the loader */
156 1.1.6.2 simonb *result = fp;
157 1.1.6.2 simonb break;
158 1.1.6.2 simonb }
159 1.1.6.2 simonb if (error == EFTYPE)
160 1.1.6.2 simonb continue; /* Unknown to this handler? */
161 1.1.6.2 simonb if (error) {
162 1.1.6.2 simonb sprintf(command_errbuf, "can't load file '%s': %s",
163 1.1.6.2 simonb filename, strerror(error));
164 1.1.6.2 simonb break;
165 1.1.6.2 simonb }
166 1.1.6.2 simonb }
167 1.1.6.2 simonb return (error);
168 1.1.6.2 simonb }
169 1.1.6.2 simonb
170 1.1.6.2 simonb /*
171 1.1.6.2 simonb * Load specified KLD. If path is omitted, then try to locate it via
172 1.1.6.2 simonb * search path.
173 1.1.6.2 simonb */
174 1.1.6.2 simonb int
175 1.1.6.2 simonb file_loadkernel(char *filename, int argc, char *argv[])
176 1.1.6.2 simonb {
177 1.1.6.2 simonb struct preloaded_file *fp, *last_file;
178 1.1.6.2 simonb int err;
179 1.1.6.2 simonb
180 1.1.6.2 simonb /*
181 1.1.6.2 simonb * Check if KLD already loaded
182 1.1.6.2 simonb */
183 1.1.6.2 simonb fp = file_findfile(filename, NULL);
184 1.1.6.2 simonb if (fp) {
185 1.1.6.2 simonb sprintf(command_errbuf, "warning: KLD '%s' already loaded", filename);
186 1.1.6.2 simonb free(filename);
187 1.1.6.2 simonb return (0);
188 1.1.6.2 simonb }
189 1.1.6.2 simonb for (last_file = preloaded_files;
190 1.1.6.2 simonb last_file != NULL && last_file->f_next != NULL;
191 1.1.6.2 simonb last_file = last_file->f_next)
192 1.1.6.2 simonb ;
193 1.1.6.2 simonb
194 1.1.6.2 simonb do {
195 1.1.6.2 simonb err = file_load(filename, loadaddr, &fp);
196 1.1.6.2 simonb if (err)
197 1.1.6.2 simonb break;
198 1.1.6.2 simonb fp->f_args = unargv(argc, argv);
199 1.1.6.2 simonb loadaddr = fp->f_addr + fp->f_size;
200 1.1.6.2 simonb file_insert_tail(fp); /* Add to the list of loaded files */
201 1.1.6.2 simonb } while(0);
202 1.1.6.2 simonb if (err == EFTYPE)
203 1.1.6.2 simonb sprintf(command_errbuf, "don't know how to load module '%s'", filename);
204 1.1.6.2 simonb if (err && fp)
205 1.1.6.2 simonb file_discard(fp);
206 1.1.6.2 simonb free(filename);
207 1.1.6.2 simonb return (err);
208 1.1.6.2 simonb }
209 1.1.6.2 simonb
210 1.1.6.2 simonb /*
211 1.1.6.2 simonb * Find a file matching (name) and (type).
212 1.1.6.2 simonb * NULL may be passed as a wildcard to either.
213 1.1.6.2 simonb */
214 1.1.6.2 simonb struct preloaded_file *
215 1.1.6.2 simonb file_findfile(char *name, char *type)
216 1.1.6.2 simonb {
217 1.1.6.2 simonb struct preloaded_file *fp;
218 1.1.6.2 simonb
219 1.1.6.2 simonb for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
220 1.1.6.2 simonb if (((name == NULL) || !strcmp(name, fp->f_name)) &&
221 1.1.6.2 simonb ((type == NULL) || !strcmp(type, fp->f_type)))
222 1.1.6.2 simonb break;
223 1.1.6.2 simonb }
224 1.1.6.2 simonb return (fp);
225 1.1.6.2 simonb }
226 1.1.6.2 simonb
227 1.1.6.2 simonb /*
228 1.1.6.2 simonb * Check if file name have any qualifiers
229 1.1.6.2 simonb */
230 1.1.6.2 simonb static int
231 1.1.6.2 simonb file_havepath(const char *name)
232 1.1.6.2 simonb {
233 1.1.6.2 simonb const char *cp;
234 1.1.6.2 simonb
235 1.1.6.2 simonb archsw.arch_getdev(NULL, name, &cp);
236 1.1.6.2 simonb return (cp != name || strchr(name, '/') != NULL);
237 1.1.6.2 simonb }
238 1.1.6.2 simonb
239 1.1.6.2 simonb /*
240 1.1.6.2 simonb * Throw a file away
241 1.1.6.2 simonb */
242 1.1.6.2 simonb void
243 1.1.6.2 simonb file_discard(struct preloaded_file *fp)
244 1.1.6.2 simonb {
245 1.1.6.2 simonb if (fp == NULL)
246 1.1.6.2 simonb return;
247 1.1.6.2 simonb if (fp->f_name != NULL)
248 1.1.6.2 simonb free(fp->f_name);
249 1.1.6.2 simonb if (fp->f_type != NULL)
250 1.1.6.2 simonb free(fp->f_type);
251 1.1.6.2 simonb if (fp->f_args != NULL)
252 1.1.6.2 simonb free(fp->f_args);
253 1.1.6.2 simonb if (fp->marks != NULL)
254 1.1.6.2 simonb free(fp->marks);
255 1.1.6.2 simonb free(fp);
256 1.1.6.2 simonb }
257 1.1.6.2 simonb
258 1.1.6.2 simonb /*
259 1.1.6.2 simonb * Allocate a new file; must be used instead of malloc()
260 1.1.6.2 simonb * to ensure safe initialisation.
261 1.1.6.2 simonb */
262 1.1.6.2 simonb struct preloaded_file *
263 1.1.6.2 simonb file_alloc(void)
264 1.1.6.2 simonb {
265 1.1.6.2 simonb struct preloaded_file *fp;
266 1.1.6.2 simonb
267 1.1.6.2 simonb if ((fp = alloc(sizeof(struct preloaded_file))) != NULL) {
268 1.1.6.2 simonb bzero(fp, sizeof(struct preloaded_file));
269 1.1.6.2 simonb
270 1.1.6.2 simonb if (fp->marks = alloc(sizeof(u_long))) {
271 1.1.6.2 simonb bzero(fp->marks, sizeof(u_long));
272 1.1.6.2 simonb }
273 1.1.6.2 simonb }
274 1.1.6.2 simonb return (fp);
275 1.1.6.2 simonb }
276 1.1.6.2 simonb
277 1.1.6.2 simonb /*
278 1.1.6.2 simonb * Add a module to the chain
279 1.1.6.2 simonb */
280 1.1.6.2 simonb static void
281 1.1.6.2 simonb file_insert_tail(struct preloaded_file *fp)
282 1.1.6.2 simonb {
283 1.1.6.2 simonb struct preloaded_file *cm;
284 1.1.6.2 simonb
285 1.1.6.2 simonb /* Append to list of loaded file */
286 1.1.6.2 simonb fp->f_next = NULL;
287 1.1.6.2 simonb if (preloaded_files == NULL) {
288 1.1.6.2 simonb preloaded_files = fp;
289 1.1.6.2 simonb } else {
290 1.1.6.2 simonb for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
291 1.1.6.2 simonb ;
292 1.1.6.2 simonb cm->f_next = fp;
293 1.1.6.2 simonb }
294 1.1.6.2 simonb }
295 1.1.6.2 simonb
296