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,27 +909,18 @@
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 struct arch_file *ifile = (struct arch_file *) f;
202 enum obj_reloc ret = obj_reloc_ok;
203 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
204 -#if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
205 - || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
206 - || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
207 - || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
208 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
210 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
211 struct arch_symbol *isym = (struct arch_symbol *) sym;
213 -#if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
214 - || defined(__sh__) || defined(__s390__)
215 +#if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
216 #if defined(USE_GOT_ENTRIES)
217 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
236 #elif defined(__microblaze__)
237 @@ -1758,7 +1825,7 @@
239 #if defined(USE_SINGLE)
241 -static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
242 +static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
243 int offset, int size)
245 if (single->allocated == 0) {
246 @@ -1906,7 +1973,7 @@
247 #if defined(USE_GOT_ENTRIES)
249 got_offset += arch_single_init(
250 - /*rel,*/ &intsym->gotent,
251 + rel, &intsym->gotent,
252 got_offset, GOT_ENTRY_SIZE);
255 @@ -1920,7 +1987,7 @@
256 plt_offset, PLT_ENTRY_SIZE);
258 plt_offset += arch_single_init(
259 - /*rel,*/ &intsym->pltent,
260 + rel, &intsym->pltent,
261 plt_offset, PLT_ENTRY_SIZE);
264 @@ -1958,8 +2025,7 @@
268 - g = (h & 0xf0000000);
270 + if ((g = (h & 0xf0000000)) != 0) {
274 @@ -2038,7 +2104,7 @@
275 int n_type = ELF_ST_TYPE(info);
276 int n_binding = ELF_ST_BIND(info);
278 - for (sym = f->symtab[hash]; sym; sym = sym->next) {
279 + for (sym = f->symtab[hash]; sym; sym = sym->next)
280 if (f->symbol_cmp(sym->name, name) == 0) {
281 int o_secidx = sym->secidx;
282 int o_info = sym->info;
283 @@ -2097,14 +2163,14 @@
289 /* Completely new symbol. */
290 sym = arch_new_symbol();
291 sym->next = f->symtab[hash];
292 f->symtab[hash] = sym;
294 - if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
296 + if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
297 if (symidx >= f->local_symtab_size)
298 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
299 name, (long) symidx, (long) f->local_symtab_size);
300 @@ -3227,7 +3293,7 @@
303 switch (arch_apply_relocation
304 - (f, targsec, /*symsec,*/ intsym, rel, value)
305 + (f, targsec, symsec, intsym, rel, value)
309 @@ -3306,11 +3372,11 @@
311 /*======================================================================*/
313 -static struct obj_file *obj_load(FILE * fp, int loadprogbits ATTRIBUTE_UNUSED)
314 +static struct obj_file *obj_load(FILE * fp, int loadprogbits)
317 ElfW(Shdr) * section_headers;
322 /* Read the file header. */
323 @@ -3582,7 +3648,7 @@
324 while (ptr < endptr) {
325 value = strchr(ptr, '=');
326 if (value && strncmp(ptr, "license", value-ptr) == 0) {
331 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
332 @@ -3686,9 +3752,6 @@
333 * start of some sections. this info is used by ksymoops to do better
336 -#if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
337 -#define get_module_version(f, str) get_module_version(str)
340 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
342 @@ -3721,8 +3784,7 @@
343 struct obj_symbol *sym;
344 char *name, *absolute_filename;
345 char str[STRVERSIONLEN];
347 - int l, lm_name, lfilename, use_ksymtab, version;
348 + int i, l, lm_name, lfilename, use_ksymtab, version;
351 /* WARNING: was using realpath, but replaced by readlink to stop using
352 @@ -3909,145 +3971,57 @@
353 void print_load_map(struct obj_file *f);
356 -int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
357 -int insmod_main(int argc, char **argv)
358 +int insmod_main_24( int argc, char **argv)
364 unsigned long m_size;
368 - char *m_name = NULL;
369 - int exit_status = EXIT_FAILURE;
370 + char *tmp = NULL, *m_name = NULL;
373 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
374 struct utsname uts_info;
375 char m_strversion[STRVERSIONLEN];
376 int m_version, m_crcs;
378 -#if ENABLE_FEATURE_CLEAN_UP
385 struct utsname myuname;
387 + /* check the kernel version */
388 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
391 + k_version = myuname.release[2] - '0';
395 /* Parse any options */
396 getopt32(argv, OPTION_STR, &opt_o);
398 if (option_mask32 & OPT_o) { // -o /* name the output module */
400 m_name = xstrdup(opt_o);
403 - if (arg1 == NULL) {
408 - /* Grab the module name */
409 - tmp1 = xstrdup(arg1);
410 - tmp = basename(tmp1);
413 - if (uname(&myuname) == 0) {
414 - if (myuname.release[0] == '2') {
415 - k_version = myuname.release[2] - '0';
419 -#if ENABLE_FEATURE_2_6_MODULES
420 - if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
421 - && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
427 - if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
433 -#if ENABLE_FEATURE_2_6_MODULES
435 - m_fullName = xasprintf("%s.ko", tmp);
438 - m_fullName = xasprintf("%s.o", tmp);
439 + ret = find_module(arg1);
447 - tmp1 = NULL; /* flag for free(m_name) before exit() */
450 - /* Get a filedesc for the module. Check that we have a complete path */
451 - if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
452 - || (fp = fopen(arg1, "r")) == NULL
454 - /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
455 - * but do not error out yet if we fail to find it... */
456 - if (k_version) { /* uname succeedd */
460 - tmdn = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, myuname.release);
461 - /* Jump through hoops in case /lib/modules/`uname -r`
462 - * is a symlink. We do not want recursive_action to
463 - * follow symlinks, but we do want to follow the
464 - * /lib/modules/`uname -r` dir, So resolve it ourselves
465 - * if it is a link... */
466 - module_dir = xmalloc_readlink(tmdn);
468 - module_dir = xstrdup(tmdn);
469 - recursive_action(module_dir, ACTION_RECURSE,
470 - check_module_name_match, NULL, m_fullName, 0);
475 - /* Check if we have found anything yet */
476 - if (!m_filename || ((fp = fopen(m_filename, "r")) == NULL)) {
482 - module_dir = xmalloc_readlink(CONFIG_DEFAULT_MODULES_DIR);
484 - module_dir = xstrdup(CONFIG_DEFAULT_MODULES_DIR);
485 - /* No module found under /lib/modules/`uname -r`, this
486 - * time cast the net a bit wider. Search /lib/modules/ */
487 - r = recursive_action(module_dir, ACTION_RECURSE,
488 - check_module_name_match, NULL, m_fullName, 0);
490 - bb_error_msg_and_die("%s: module not found", m_fullName);
492 - if (m_filename == NULL
493 - || ((fp = fopen(m_filename, "r")) == NULL)
495 - bb_error_msg_and_die("%s: module not found", m_fullName);
497 + tmp = xstrdup(arg1);
498 + m_name = basename(tmp);
501 - m_filename = xstrdup(arg1);
504 - printf("Using %s\n", m_filename);
506 -#if ENABLE_FEATURE_2_6_MODULES
507 - if (k_version > 4) {
508 - argv[optind] = m_filename;
510 - return insmod_ng_main(argc - optind, argv + optind);
511 + fp = fopen(g_filename, "r");
518 f = obj_load(fp, LOADBITS);
520 @@ -4074,7 +4048,7 @@
521 "\t%s was compiled for kernel version %s\n"
522 "\twhile this kernel is version %s",
523 flag_force_load ? "warning: " : "",
524 - m_filename, m_strversion, uts_info.release);
525 + g_filename, m_strversion, uts_info.release);
526 if (!flag_force_load)
529 @@ -4116,7 +4090,7 @@
530 hide_special_symbols(f);
532 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
533 - add_ksymoops_symbols(f, m_filename, m_name);
534 + add_ksymoops_symbols(f, g_filename, m_name);
535 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
537 new_create_module_ksymtab(f);
538 @@ -4125,7 +4099,7 @@
539 m_size = obj_load_size(f);
541 m_addr = create_module(m_name, m_size);
542 - if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
543 + if (m_addr == -1) switch (errno) {
545 bb_error_msg_and_die("a module named %s already exists", m_name);
547 @@ -4141,36 +4115,37 @@
548 * now we can load them directly into the kernel memory
550 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
551 - delete_module(m_name, 0);
552 + delete_module(m_name);
557 if (!obj_relocate(f, m_addr)) {
558 - delete_module(m_name, 0);
559 + delete_module(m_name);
563 if (!new_init_module(m_name, f, m_size)) {
564 - delete_module(m_name, 0);
565 + delete_module(m_name);
569 if (flag_print_load_map)
572 - exit_status = EXIT_SUCCESS;
573 + ret = EXIT_SUCCESS;
576 #if ENABLE_FEATURE_CLEAN_UP
588 - return exit_status;
592 #endif /* ENABLE_FEATURE_2_4_MODULES */
593 @@ -4182,15 +4157,8 @@
594 #if ENABLE_FEATURE_2_6_MODULES
596 #include <sys/mman.h>
598 -#if defined __UCLIBC__ && !ENABLE_FEATURE_2_4_MODULES
599 -/* big time suckage. The old prototype above renders our nice fwd-decl wrong */
600 -extern int init_module(void *module, unsigned long len, const char *options);
602 #include <asm/unistd.h>
603 #include <sys/syscall.h>
604 -#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
607 /* We use error numbers in a loose translation... */
608 static const char *moderror(int err)
609 @@ -4209,22 +4177,32 @@
613 -#if !ENABLE_FEATURE_2_4_MODULES
614 -int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
615 -int insmod_main(int argc ATTRIBUTE_UNUSED, char **argv)
617 -static int insmod_ng_main(int argc ATTRIBUTE_UNUSED, char **argv)
619 +int insmod_main_26(int argc, char **argv)
622 + char *filename, *options;
623 + struct utsname myuname;
628 - char *filename, *options;
631 + /* check the kernel version */
632 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
635 + k_version = myuname.release[2] - '0';
636 + if (k_version <= 4)
643 + ret = find_module(filename);
644 + if (ret || (g_filename == NULL))
647 /* Rest is options */
648 options = xzalloc(1);
650 @@ -4234,41 +4212,47 @@
651 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
655 - /* Any special reason why mmap? It isn't performance critical. -vda */
656 - /* Yes, xmalloc'ing can use *alot* of RAM. Don't forget that there are
657 - * modules out there that are half a megabyte! mmap()ing is way nicer
658 - * for small mem boxes, i guess. */
659 - /* But after load, these modules will take up that 0.5mb in kernel
660 - * anyway. Using malloc here causes only a transient spike to 1mb,
661 - * after module is loaded, we go back to normal 0.5mb usage
662 - * (in kernel). Also, mmap isn't magic - when we touch mapped data,
663 - * we use memory. -vda */
667 - fd = xopen(filename, O_RDONLY);
670 - map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
671 - if (map == MAP_FAILED) {
672 - bb_perror_msg_and_die("cannot mmap '%s'", filename);
675 - /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
677 - map = xmalloc(len);
678 - xread(fd, map, len);
681 len = MAXINT(ssize_t);
682 - map = xmalloc_open_read_close(filename, &len);
684 + map = xmalloc_open_read_close(g_filename, &len);
685 + ret = syscall(__NR_init_module, map, len, options);
687 + bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
688 + g_filename, moderror(errno), ret);
691 + if (g_filename && (g_filename != filename))
694 - if (init_module(map, len, options) != 0)
695 - bb_error_msg_and_die("cannot insert '%s': %s",
696 - filename, moderror(errno));
703 +int insmod_main(int argc, char **argv)
708 +#if ENABLE_FEATURE_2_6_MODULES
709 + ret = insmod_main_26(argc, argv);
710 + if (ret != ENOTSUP)
714 +#if ENABLE_FEATURE_2_4_MODULES
715 + ret = insmod_main_24(argc, argv);
716 + if (ret != ENOTSUP)
720 + fprintf(stderr, "Error: Kernel version not supported\n");
726 + bb_perror_msg("Loading module failed");