1 --- a/modutils/insmod.c
2 +++ b/modutils/insmod.c
6 #include <sys/utsname.h>
7 +#if ENABLE_FEATURE_2_6_MODULES
9 +#include <asm/unistd.h>
10 +#include <sys/syscall.h>
13 #if !ENABLE_FEATURE_2_4_MODULES && !ENABLE_FEATURE_2_6_MODULES
14 #undef ENABLE_FEATURE_2_4_MODULES
15 #define ENABLE_FEATURE_2_4_MODULES 1
19 - * Big piece of 2.4-specific code
21 #if ENABLE_FEATURE_2_4_MODULES
23 +int insmod_main_24(int argc, char **argv);
25 #if ENABLE_FEATURE_2_6_MODULES
26 -static int insmod_ng_main(int argc, char **argv);
27 +int insmod_main_26(int argc, char **argv);
29 +int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31 +static char *g_filename = NULL;
32 +#define _PATH_MODULES "/lib/modules"
34 +static int check_module_name_match(const char *filename, struct stat *statbuf,
35 + void *userdata, int depth)
37 + char *fullname = (char *) userdata;
40 + if (fullname[0] == '\0')
43 + tmp = bb_get_last_path_component_nostrip(filename);
44 + if (strcmp(tmp, fullname) == 0) {
45 + /* Stop searching if we find a match */
46 + g_filename = xstrdup(filename);
53 +static int find_module(char *filename)
55 + char *module_dir, real_module_dir[FILENAME_MAX];
56 + int len, slen, ret = ENOENT, k_version;
57 + struct utsname myuname;
61 + /* check the kernel version */
62 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
65 + k_version = myuname.release[2] - '0';
66 +#if ENABLE_FEATURE_2_4_MODULES
73 + len = strlen(filename);
74 + slen = strlen(suffix);
76 + /* check for suffix and absolute path first */
77 + if ((len < slen + 2) || (strcmp(filename + len - slen, suffix) != 0)) {
78 + filename = xasprintf("%s%s", filename, suffix);
80 + filename = strdup(filename);
81 + if ((stat(filename, &st) == 0) && S_ISREG(st.st_mode)) {
82 + g_filename = filename;
89 + /* next: scan /lib/modules/<release> */
90 + /* Jump through hoops in case /lib/modules/`uname -r`
91 + * is a symlink. We do not want recursive_action to
92 + * follow symlinks, but we do want to follow the
93 + * /lib/modules/`uname -r` dir, So resolve it ourselves
94 + * if it is a link... */
95 + module_dir = concat_path_file(_PATH_MODULES, myuname.release);
96 + if (realpath(module_dir, real_module_dir) != NULL) {
98 + module_dir = real_module_dir;
101 + recursive_action(module_dir, ACTION_RECURSE,
102 + check_module_name_match, 0, filename, 0);
104 + /* Check if we have a complete path */
105 + if (g_filename == NULL)
108 + if ((stat(g_filename, &st) == 0) && S_ISREG(st.st_mode))
120 + * Big piece of 2.4-specific code
122 +#if ENABLE_FEATURE_2_4_MODULES
123 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
128 #if defined(__microblaze__)
130 -#include <linux/elf-em.h>
131 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
132 #define SHT_RELM SHT_RELA
133 #define Elf32_RelM Elf32_Rela
135 /* The system calls unchanged between 2.0 and 2.1. */
137 unsigned long create_module(const char *, size_t);
138 -int delete_module(const char *module, unsigned int flags);
139 +int delete_module(const char *);
142 #endif /* module.h */
145 static enum obj_reloc arch_apply_relocation(struct obj_file *f,
146 struct obj_section *targsec,
147 - /*struct obj_section *symsec,*/
148 + struct obj_section *symsec,
149 struct obj_symbol *sym,
150 ElfW(RelM) *rel, ElfW(Addr) value);
157 enum { STRVERSIONLEN = 64 };
159 /*======================================================================*/
161 static char *m_fullName;
164 -/*======================================================================*/
167 -static int check_module_name_match(const char *filename,
168 - struct stat *statbuf ATTRIBUTE_UNUSED,
169 - void *userdata, int depth ATTRIBUTE_UNUSED)
171 - char *fullname = (char *) userdata;
174 - if (fullname[0] == '\0')
177 - tmp = bb_get_last_path_component_nostrip(filename);
178 - if (strcmp(tmp, fullname) == 0) {
179 - /* Stop searching if we find a match */
180 - m_filename = xstrdup(filename);
187 /*======================================================================*/
189 @@ -835,32 +909,20 @@
190 static enum obj_reloc
191 arch_apply_relocation(struct obj_file *f,
192 struct obj_section *targsec,
193 - /*struct obj_section *symsec,*/
194 + struct obj_section *symsec,
195 struct obj_symbol *sym,
196 ElfW(RelM) *rel, ElfW(Addr) v)
198 -#if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
199 - || defined(__sh__) || defined(__s390__) || defined(__x86_64__) \
200 - || defined(__powerpc__) || defined(__mips__)
201 struct arch_file *ifile = (struct arch_file *) f;
203 enum obj_reloc ret = obj_reloc_ok;
204 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
205 -#if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
206 - || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
207 - || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
208 - || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
209 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
211 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
212 struct arch_symbol *isym = (struct arch_symbol *) sym;
214 -#if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
215 - || defined(__sh__) || defined(__s390__)
216 #if defined(USE_GOT_ENTRIES)
217 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
220 #if defined(USE_PLT_ENTRIES)
221 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
240 #elif defined(__microblaze__)
241 @@ -1759,7 +1823,7 @@
243 #if defined(USE_SINGLE)
245 -static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
246 +static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
247 int offset, int size)
249 if (single->allocated == 0) {
250 @@ -1907,7 +1971,7 @@
251 #if defined(USE_GOT_ENTRIES)
253 got_offset += arch_single_init(
254 - /*rel,*/ &intsym->gotent,
255 + rel, &intsym->gotent,
256 got_offset, GOT_ENTRY_SIZE);
259 @@ -1921,7 +1985,7 @@
260 plt_offset, PLT_ENTRY_SIZE);
262 plt_offset += arch_single_init(
263 - /*rel,*/ &intsym->pltent,
264 + rel, &intsym->pltent,
265 plt_offset, PLT_ENTRY_SIZE);
268 @@ -1959,8 +2023,7 @@
272 - g = (h & 0xf0000000);
274 + if ((g = (h & 0xf0000000)) != 0) {
278 @@ -2039,7 +2102,7 @@
279 int n_type = ELF_ST_TYPE(info);
280 int n_binding = ELF_ST_BIND(info);
282 - for (sym = f->symtab[hash]; sym; sym = sym->next) {
283 + for (sym = f->symtab[hash]; sym; sym = sym->next)
284 if (f->symbol_cmp(sym->name, name) == 0) {
285 int o_secidx = sym->secidx;
286 int o_info = sym->info;
287 @@ -2098,14 +2161,14 @@
293 /* Completely new symbol. */
294 sym = arch_new_symbol();
295 sym->next = f->symtab[hash];
296 f->symtab[hash] = sym;
298 - if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
300 + if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
301 if (symidx >= f->local_symtab_size)
302 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
303 name, (long) symidx, (long) f->local_symtab_size);
304 @@ -3228,7 +3291,7 @@
307 switch (arch_apply_relocation
308 - (f, targsec, /*symsec,*/ intsym, rel, value)
309 + (f, targsec, symsec, intsym, rel, value)
313 @@ -3307,11 +3370,11 @@
315 /*======================================================================*/
317 -static struct obj_file *obj_load(FILE * fp, int loadprogbits ATTRIBUTE_UNUSED)
318 +static struct obj_file *obj_load(FILE * fp, int loadprogbits)
321 ElfW(Shdr) * section_headers;
326 /* Read the file header. */
327 @@ -3583,7 +3646,7 @@
328 while (ptr < endptr) {
329 value = strchr(ptr, '=');
330 if (value && strncmp(ptr, "license", value-ptr) == 0) {
335 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
336 @@ -3687,9 +3750,6 @@
337 * start of some sections. this info is used by ksymoops to do better
340 -#if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
341 -#define get_module_version(f, str) get_module_version(str)
344 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
346 @@ -3722,8 +3782,7 @@
347 struct obj_symbol *sym;
348 char *name, *absolute_filename;
349 char str[STRVERSIONLEN];
351 - int l, lm_name, lfilename, use_ksymtab, version;
352 + int i, l, lm_name, lfilename, use_ksymtab, version;
355 /* WARNING: was using realpath, but replaced by readlink to stop using
356 @@ -3910,145 +3969,57 @@
357 void print_load_map(struct obj_file *f);
360 -int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
361 -int insmod_main(int argc, char **argv)
362 +int insmod_main_24( int argc, char **argv)
368 unsigned long m_size;
372 - char *m_name = NULL;
373 - int exit_status = EXIT_FAILURE;
374 + char *tmp = NULL, *m_name = NULL;
377 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
378 struct utsname uts_info;
379 char m_strversion[STRVERSIONLEN];
380 int m_version, m_crcs;
382 -#if ENABLE_FEATURE_CLEAN_UP
389 struct utsname myuname;
391 + /* check the kernel version */
392 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
395 + k_version = myuname.release[2] - '0';
399 /* Parse any options */
400 getopt32(argv, OPTION_STR, &opt_o);
402 if (option_mask32 & OPT_o) { // -o /* name the output module */
404 m_name = xstrdup(opt_o);
407 - if (arg1 == NULL) {
412 - /* Grab the module name */
413 - tmp1 = xstrdup(arg1);
414 - tmp = basename(tmp1);
417 - if (uname(&myuname) == 0) {
418 - if (myuname.release[0] == '2') {
419 - k_version = myuname.release[2] - '0';
423 -#if ENABLE_FEATURE_2_6_MODULES
424 - if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
425 - && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
431 - if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
437 -#if ENABLE_FEATURE_2_6_MODULES
439 - m_fullName = xasprintf("%s.ko", tmp);
442 - m_fullName = xasprintf("%s.o", tmp);
443 + ret = find_module(arg1);
451 - tmp1 = NULL; /* flag for free(m_name) before exit() */
454 - /* Get a filedesc for the module. Check that we have a complete path */
455 - if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
456 - || (fp = fopen(arg1, "r")) == NULL
458 - /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
459 - * but do not error out yet if we fail to find it... */
460 - if (k_version) { /* uname succeedd */
464 - tmdn = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, myuname.release);
465 - /* Jump through hoops in case /lib/modules/`uname -r`
466 - * is a symlink. We do not want recursive_action to
467 - * follow symlinks, but we do want to follow the
468 - * /lib/modules/`uname -r` dir, So resolve it ourselves
469 - * if it is a link... */
470 - module_dir = xmalloc_readlink(tmdn);
472 - module_dir = xstrdup(tmdn);
473 - recursive_action(module_dir, ACTION_RECURSE,
474 - check_module_name_match, NULL, m_fullName, 0);
479 - /* Check if we have found anything yet */
480 - if (!m_filename || ((fp = fopen(m_filename, "r")) == NULL)) {
486 - module_dir = xmalloc_readlink(CONFIG_DEFAULT_MODULES_DIR);
488 - module_dir = xstrdup(CONFIG_DEFAULT_MODULES_DIR);
489 - /* No module found under /lib/modules/`uname -r`, this
490 - * time cast the net a bit wider. Search /lib/modules/ */
491 - r = recursive_action(module_dir, ACTION_RECURSE,
492 - check_module_name_match, NULL, m_fullName, 0);
494 - bb_error_msg_and_die("%s: module not found", m_fullName);
496 - if (m_filename == NULL
497 - || ((fp = fopen(m_filename, "r")) == NULL)
499 - bb_error_msg_and_die("%s: module not found", m_fullName);
501 + tmp = xstrdup(arg1);
502 + m_name = basename(tmp);
505 - m_filename = xstrdup(arg1);
508 - printf("Using %s\n", m_filename);
510 -#if ENABLE_FEATURE_2_6_MODULES
511 - if (k_version > 4) {
512 - argv[optind] = m_filename;
514 - return insmod_ng_main(argc - optind, argv + optind);
515 + fp = fopen(g_filename, "r");
522 f = obj_load(fp, LOADBITS);
524 @@ -4075,7 +4046,7 @@
525 "\t%s was compiled for kernel version %s\n"
526 "\twhile this kernel is version %s",
527 flag_force_load ? "warning: " : "",
528 - m_filename, m_strversion, uts_info.release);
529 + g_filename, m_strversion, uts_info.release);
530 if (!flag_force_load)
533 @@ -4117,7 +4088,7 @@
534 hide_special_symbols(f);
536 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
537 - add_ksymoops_symbols(f, m_filename, m_name);
538 + add_ksymoops_symbols(f, g_filename, m_name);
539 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
541 new_create_module_ksymtab(f);
542 @@ -4126,7 +4097,7 @@
543 m_size = obj_load_size(f);
545 m_addr = create_module(m_name, m_size);
546 - if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
547 + if (m_addr == -1) switch (errno) {
549 bb_error_msg_and_die("a module named %s already exists", m_name);
551 @@ -4142,36 +4113,37 @@
552 * now we can load them directly into the kernel memory
554 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
555 - delete_module(m_name, 0);
556 + delete_module(m_name);
561 if (!obj_relocate(f, m_addr)) {
562 - delete_module(m_name, 0);
563 + delete_module(m_name);
567 if (!new_init_module(m_name, f, m_size)) {
568 - delete_module(m_name, 0);
569 + delete_module(m_name);
573 if (flag_print_load_map)
576 - exit_status = EXIT_SUCCESS;
577 + ret = EXIT_SUCCESS;
580 #if ENABLE_FEATURE_CLEAN_UP
592 - return exit_status;
596 #endif /* ENABLE_FEATURE_2_4_MODULES */
597 @@ -4183,15 +4155,8 @@
598 #if ENABLE_FEATURE_2_6_MODULES
600 #include <sys/mman.h>
602 -#if defined __UCLIBC__ && !ENABLE_FEATURE_2_4_MODULES
603 -/* big time suckage. The old prototype above renders our nice fwd-decl wrong */
604 -extern int init_module(void *module, unsigned long len, const char *options);
606 #include <asm/unistd.h>
607 #include <sys/syscall.h>
608 -#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
611 /* We use error numbers in a loose translation... */
612 static const char *moderror(int err)
613 @@ -4210,22 +4175,32 @@
617 -#if !ENABLE_FEATURE_2_4_MODULES
618 -int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
619 -int insmod_main(int argc ATTRIBUTE_UNUSED, char **argv)
621 -static int insmod_ng_main(int argc ATTRIBUTE_UNUSED, char **argv)
623 +int insmod_main_26(int argc, char **argv)
626 + char *filename, *options;
627 + struct utsname myuname;
632 - char *filename, *options;
635 + /* check the kernel version */
636 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
639 + k_version = myuname.release[2] - '0';
640 + if (k_version <= 4)
647 + ret = find_module(filename);
648 + if (ret || (g_filename == NULL))
651 /* Rest is options */
652 options = xzalloc(1);
654 @@ -4235,41 +4210,47 @@
655 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
659 - /* Any special reason why mmap? It isn't performance critical. -vda */
660 - /* Yes, xmalloc'ing can use *alot* of RAM. Don't forget that there are
661 - * modules out there that are half a megabyte! mmap()ing is way nicer
662 - * for small mem boxes, i guess. */
663 - /* But after load, these modules will take up that 0.5mb in kernel
664 - * anyway. Using malloc here causes only a transient spike to 1mb,
665 - * after module is loaded, we go back to normal 0.5mb usage
666 - * (in kernel). Also, mmap isn't magic - when we touch mapped data,
667 - * we use memory. -vda */
671 - fd = xopen(filename, O_RDONLY);
674 - map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
675 - if (map == MAP_FAILED) {
676 - bb_perror_msg_and_die("cannot mmap '%s'", filename);
679 - /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
681 - map = xmalloc(len);
682 - xread(fd, map, len);
685 len = MAXINT(ssize_t);
686 - map = xmalloc_open_read_close(filename, &len);
688 + map = xmalloc_open_read_close(g_filename, &len);
689 + ret = syscall(__NR_init_module, map, len, options);
691 + bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
692 + g_filename, moderror(errno), ret);
695 + if (g_filename && (g_filename != filename))
698 - if (init_module(map, len, options) != 0)
699 - bb_error_msg_and_die("cannot insert '%s': %s",
700 - filename, moderror(errno));
707 +int insmod_main(int argc, char **argv)
712 +#if ENABLE_FEATURE_2_6_MODULES
713 + ret = insmod_main_26(argc, argv);
714 + if (ret != ENOTSUP)
718 +#if ENABLE_FEATURE_2_4_MODULES
719 + ret = insmod_main_24(argc, argv);
720 + if (ret != ENOTSUP)
724 + fprintf(stderr, "Error: Kernel version not supported\n");
730 + bb_perror_msg("Loading module failed");