1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
/*-
* 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.
*
* $FreeBSD$
*/
/*
* file/module function dispatcher, support, etc.
*/
#include <stand.h>
#include <string.h>
#include <sys/param.h>
#include <sys/linker.h>
#include "bootstrap.h"
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);
static char *mod_searchmodule(char *name);
static void file_insert_tail(struct preloaded_file *mp);
struct file_metadata* metadata_next(struct file_metadata *base_mp, int type);
/* load address should be tweaked by first module loaded (kernel) */
static vm_offset_t loadaddr = 0;
static const char *default_searchpath ="/;/boot;/modules";
struct preloaded_file *preloaded_files = NULL;
/*
* 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>
*/
COMMAND_SET(load, "load", "load a kernel or module", command_load);
static int
command_load(int argc, char *argv[])
{
char *typestr;
int dofile, ch, error;
dofile = 0;
optind = 1;
optreset = 1;
typestr = NULL;
if (argc == 1) {
command_errmsg = "no filename specified";
return(CMD_ERROR);
}
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);
}
return(file_loadraw(typestr, argv[1]));
}
/*
* Looks like a request for a module.
*/
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);
}
COMMAND_SET(unload, "unload", "unload all modules", command_unload);
static int
command_unload(int argc, char *argv[])
{
struct preloaded_file *fp;
while (preloaded_files != NULL) {
fp = preloaded_files;
preloaded_files = preloaded_files->f_next;
file_discard(fp);
}
loadaddr = 0;
return(CMD_OK);
}
COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
static int
command_lsmod(int argc, char *argv[])
{
struct preloaded_file *fp;
struct kernel_module *mp;
struct file_metadata *md;
char lbuf[80];
int ch, verbose;
verbose = 0;
optind = 1;
optreset = 1;
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);
}
}
pager_open();
for (fp = preloaded_files; fp; fp = fp->f_next) {
sprintf(lbuf, " %p: %s (%s, 0x%lx)\n",
(void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
pager_output(lbuf);
if (fp->f_args != NULL) {
pager_output(" args: ");
pager_output(fp->f_args);
pager_output("\n");
}
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) {
/* XXX could add some formatting smarts here to display some better */
for (md = fp->f_metadata; md != NULL; md = md->md_next) {
sprintf(lbuf, " 0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
pager_output(lbuf);
}
}
}
pager_close();
return(CMD_OK);
}
/*
* File level interface, functions file_*
*/
int
file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
{
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);
}
static int
file_load_dependancies(struct preloaded_file *base_file) {
struct file_metadata *md;
struct preloaded_file *fp;
char *dmodname;
int error;
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);
if (!error)
return (0);
/* Load failed; discard everything */
while (base_file != NULL) {
fp = base_file;
base_file = base_file->f_next;
file_discard(fp);
}
return (error);
}
/*
* We've been asked to load (name) as (type), so just suck it in,
* no arguments or anything.
*/
int
file_loadraw(char *type, char *name)
{
struct preloaded_file *fp;
char *cp;
int fd, got;
vm_offset_t laddr;
/* We can't load first */
if ((file_findfile(NULL, NULL)) == NULL) {
command_errmsg = "can't load file before kernel";
return(CMD_ERROR);
}
/* locate the file on the load path */
cp = file_search(name);
if (cp == NULL) {
sprintf(command_errbuf, "can't find '%s'", name);
return(CMD_ERROR);
}
name = cp;
if ((fd = open(name, O_RDONLY)) < 0) {
sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
free(name);
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));
free(name);
close(fd);
return(CMD_ERROR);
}
laddr += got;
}
/* Looks OK so far; create & populate control structure */
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;
/* recognise space consumption */
loadaddr = laddr;
/* Add to the list of loaded files */
file_insert_tail(fp);
close(fd);
return(CMD_OK);
}
/*
* 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.
*/
int
mod_load(char *modname, int argc, char *argv[])
{
struct preloaded_file *fp, *last_file;
struct kernel_module *mp;
int err;
char *filename;
/* see if module is already loaded */
mp = file_findmodule(NULL, modname);
if (mp) {
#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);
}
/* 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)
;
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;
break;
}
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);
if (err == EFTYPE)
sprintf(command_errbuf, "don't know how to load module '%s'", filename);
if (err && fp)
file_discard(fp);
free(filename);
return (err);
}
/*
* 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)
{
struct preloaded_file *fp;
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);
}
/*
* Find a module matching (name) inside of given file.
* NULL may be passed as a wildcard.
*/
struct kernel_module *
file_findmodule(struct preloaded_file *fp, char *modname)
{
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);
}
return (NULL);
}
/*
* Make a copy of (size) bytes of data from (p), and associate them as
* metadata of (type) to the module (mp).
*/
void
file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
{
struct file_metadata *md;
md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
md->md_size = size;
md->md_type = type;
bcopy(p, md->md_data, size);
md->md_next = fp->f_metadata;
fp->f_metadata = md;
}
/*
* Find a metadata object of (type) associated with the file (fp)
*/
struct file_metadata *
file_findmetadata(struct preloaded_file *fp, int type)
{
struct file_metadata *md;
for (md = fp->f_metadata; md != NULL; md = md->md_next)
if (md->md_type == type)
break;
return(md);
}
struct file_metadata *
metadata_next(struct file_metadata *md, int type)
{
if (md == NULL)
return (NULL);
while((md = md->md_next) != NULL)
if (md->md_type == type)
break;
return (md);
}
/*
* Attempt to find the file (name) on the module searchpath.
* 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 *
file_search(char *name)
{
char *result;
char *path, *sp;
const char *cp;
struct stat sb;
/* Don't look for nothing */
if (name == NULL)
return(name);
if (*name == 0)
return(strdup(name));
/*
* 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)
return(strdup(name));
return(NULL);
}
/*
* Get the module path
*/
if ((cp = getenv("module_path")) == NULL)
cp = default_searchpath;
sp = path = strdup(cp);
/*
* Traverse the path, splitting off ';'-delimited components.
*/
result = NULL;
while((cp = strsep(&path, ";")) != NULL) {
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))
break;
free(result);
result = NULL;
}
free(sp);
return(result);
}
/*
* Attempt to locate the file containing the module (name)
*/
static char *
mod_searchmodule(char *name)
{
char *tn, *result;
/* Look for (name).ko */
tn = malloc(strlen(name) + 3 + 1);
strcpy(tn, name);
strcat(tn, ".ko");
result = file_search(tn);
free(tn);
/* Look for just (name) (useful for finding kernels) */
if (result == NULL)
result = file_search(name);
return(result);
}
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);
}
/*
* Throw a file away
*/
void
file_discard(struct preloaded_file *fp)
{
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);
}
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);
}
/*
* Allocate a new file; must be used instead of malloc()
* to ensure safe initialisation.
*/
struct preloaded_file *
file_alloc(void)
{
struct preloaded_file *fp;
if ((fp = malloc(sizeof(struct preloaded_file))) != NULL) {
bzero(fp, sizeof(struct preloaded_file));
}
return (fp);
}
/*
* Add a module to the chain
*/
static void
file_insert_tail(struct preloaded_file *fp)
{
struct preloaded_file *cm;
/* Append to list of loaded file */
fp->f_next = NULL;
if (preloaded_files == NULL) {
preloaded_files = fp;
} else {
for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
;
cm->f_next = fp;
}
}
|