diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2009-04-05 23:25:09 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2009-04-05 23:25:09 +0000 |
commit | f4393046ef647504d94b459817193c8f0684531a (patch) | |
tree | 986e8eeb4d1ff9b557dfe5d74bb36cb4615dd082 /modutils/modutils-24.c | |
parent | 913a201bf098a5fbe1da02cb332d7d0787974c4d (diff) | |
download | busybox-w32-f4393046ef647504d94b459817193c8f0684531a.tar.gz busybox-w32-f4393046ef647504d94b459817193c8f0684531a.tar.bz2 busybox-w32-f4393046ef647504d94b459817193c8f0684531a.zip |
modprobe/insmod for 2.4: support compressed modules.
by Guenter (lists AT gknw.net)
function old new delta
bb_init_module_24 4551 4570 +19
obj_load 786 794 +8
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 27/0) Total: 27 bytes
Diffstat (limited to 'modutils/modutils-24.c')
-rw-r--r-- | modutils/modutils-24.c | 57 |
1 files changed, 33 insertions, 24 deletions
diff --git a/modutils/modutils-24.c b/modutils/modutils-24.c index 89bc30fe5..24bc6b446 100644 --- a/modutils/modutils-24.c +++ b/modutils/modutils-24.c | |||
@@ -625,7 +625,11 @@ static unsigned long obj_load_size(struct obj_file *f); | |||
625 | 625 | ||
626 | static int obj_relocate(struct obj_file *f, ElfW(Addr) base); | 626 | static int obj_relocate(struct obj_file *f, ElfW(Addr) base); |
627 | 627 | ||
628 | static struct obj_file *obj_load(int fd, int loadprogbits); | 628 | #if !LOADBITS |
629 | #define obj_load(image, image_size, loadprogbits) \ | ||
630 | obj_load(image, image_size) | ||
631 | #endif | ||
632 | static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits); | ||
629 | 633 | ||
630 | static int obj_create_image(struct obj_file *f, char *image); | 634 | static int obj_create_image(struct obj_file *f, char *image); |
631 | 635 | ||
@@ -3191,7 +3195,7 @@ static int obj_create_image(struct obj_file *f, char *image) | |||
3191 | 3195 | ||
3192 | /*======================================================================*/ | 3196 | /*======================================================================*/ |
3193 | 3197 | ||
3194 | static struct obj_file *obj_load(int fd, int loadprogbits UNUSED_PARAM) | 3198 | static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits) |
3195 | { | 3199 | { |
3196 | #if BB_LITTLE_ENDIAN | 3200 | #if BB_LITTLE_ENDIAN |
3197 | # define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3))))) | 3201 | # define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3))))) |
@@ -3210,8 +3214,9 @@ static struct obj_file *obj_load(int fd, int loadprogbits UNUSED_PARAM) | |||
3210 | f->symbol_hash = obj_elf_hash; | 3214 | f->symbol_hash = obj_elf_hash; |
3211 | f->load_order_search_start = &f->load_order; | 3215 | f->load_order_search_start = &f->load_order; |
3212 | 3216 | ||
3213 | xlseek(fd, 0, SEEK_SET); | 3217 | if (image_size < sizeof(f->header)) |
3214 | xread(fd, &f->header, sizeof(f->header)); | 3218 | bb_error_msg_and_die("error while loading ELF header"); |
3219 | memcpy(&f->header, image, sizeof(f->header)); | ||
3215 | 3220 | ||
3216 | if (*(uint32_t*)(&f->header.e_ident) != ELFMAG_U32) { | 3221 | if (*(uint32_t*)(&f->header.e_ident) != ELFMAG_U32) { |
3217 | bb_error_msg_and_die("not an ELF file"); | 3222 | bb_error_msg_and_die("not an ELF file"); |
@@ -3242,8 +3247,9 @@ static struct obj_file *obj_load(int fd, int loadprogbits UNUSED_PARAM) | |||
3242 | f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4)); | 3247 | f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4)); |
3243 | 3248 | ||
3244 | section_headers = alloca(sizeof(ElfW(Shdr)) * shnum); | 3249 | section_headers = alloca(sizeof(ElfW(Shdr)) * shnum); |
3245 | xlseek(fd, f->header.e_shoff, SEEK_SET); | 3250 | if (image_size < f->header.e_shoff + sizeof(ElfW(Shdr)) * shnum) |
3246 | xread(fd, section_headers, sizeof(ElfW(Shdr)) * shnum); | 3251 | bb_error_msg_and_die("error while loading section headers"); |
3252 | memcpy(section_headers, image + f->header.e_shoff, sizeof(ElfW(Shdr)) * shnum); | ||
3247 | 3253 | ||
3248 | /* Read the section data. */ | 3254 | /* Read the section data. */ |
3249 | 3255 | ||
@@ -3275,8 +3281,9 @@ static struct obj_file *obj_load(int fd, int loadprogbits UNUSED_PARAM) | |||
3275 | sec->contents = NULL; | 3281 | sec->contents = NULL; |
3276 | if (sec->header.sh_size > 0) { | 3282 | if (sec->header.sh_size > 0) { |
3277 | sec->contents = xmalloc(sec->header.sh_size); | 3283 | sec->contents = xmalloc(sec->header.sh_size); |
3278 | xlseek(fd, sec->header.sh_offset, SEEK_SET); | 3284 | if (image_size < (sec->header.sh_offset + sec->header.sh_size)) |
3279 | xread(fd, sec->contents, sec->header.sh_size); | 3285 | bb_error_msg_and_die("error while loading section data"); |
3286 | memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size); | ||
3280 | } | 3287 | } |
3281 | break; | 3288 | break; |
3282 | #if SHT_RELM == SHT_REL | 3289 | #if SHT_RELM == SHT_REL |
@@ -3392,7 +3399,7 @@ static struct obj_file *obj_load(int fd, int loadprogbits UNUSED_PARAM) | |||
3392 | * kernel for the module | 3399 | * kernel for the module |
3393 | */ | 3400 | */ |
3394 | 3401 | ||
3395 | static int obj_load_progbits(int fd, struct obj_file *f, char *imagebase) | 3402 | static int obj_load_progbits(char *image, size_t image_size, struct obj_file *f, char *imagebase) |
3396 | { | 3403 | { |
3397 | ElfW(Addr) base = f->baseaddr; | 3404 | ElfW(Addr) base = f->baseaddr; |
3398 | struct obj_section* sec; | 3405 | struct obj_section* sec; |
@@ -3404,12 +3411,11 @@ static int obj_load_progbits(int fd, struct obj_file *f, char *imagebase) | |||
3404 | if (sec->header.sh_size == 0) | 3411 | if (sec->header.sh_size == 0) |
3405 | continue; | 3412 | continue; |
3406 | sec->contents = imagebase + (sec->header.sh_addr - base); | 3413 | sec->contents = imagebase + (sec->header.sh_addr - base); |
3407 | xlseek(fd, sec->header.sh_offset, SEEK_SET); | 3414 | if (image_size < (sec->header.sh_offset + sec->header.sh_size)) { |
3408 | errno = 0; /* read may be short without errno being set */ | 3415 | bb_error_msg("error reading ELF section data"); |
3409 | if (full_read(fd, sec->contents, sec->header.sh_size) != sec->header.sh_size) { | 3416 | return 0; /* need to delete half-loaded module! */ |
3410 | bb_perror_msg("error reading ELF section data"); | ||
3411 | return 0; | ||
3412 | } | 3417 | } |
3418 | memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size); | ||
3413 | } | 3419 | } |
3414 | return 1; | 3420 | return 1; |
3415 | } | 3421 | } |
@@ -3771,23 +3777,26 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options) | |||
3771 | struct utsname uts; | 3777 | struct utsname uts; |
3772 | int exit_status = EXIT_FAILURE; | 3778 | int exit_status = EXIT_FAILURE; |
3773 | int m_has_modinfo; | 3779 | int m_has_modinfo; |
3774 | char *m_name, *p; | 3780 | char *m_name; |
3775 | #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING | 3781 | #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING |
3776 | char m_strversion[STRVERSIONLEN]; | 3782 | char m_strversion[STRVERSIONLEN]; |
3777 | int m_version, m_crcs; | 3783 | int m_version, m_crcs; |
3778 | #endif | 3784 | #endif |
3779 | int fd; | 3785 | char *image; |
3786 | size_t image_size = 64 * 1024 * 1024; | ||
3780 | 3787 | ||
3781 | uname(&uts); | 3788 | uname(&uts); |
3782 | fd = open_or_warn(m_filename, O_RDONLY); | 3789 | |
3783 | if (fd < 0) | 3790 | /* Load module into memory and unzip if compressed */ |
3791 | image = xmalloc_open_zipped_read_close(m_filename, &image_size); | ||
3792 | if (!image) | ||
3784 | return EXIT_FAILURE; | 3793 | return EXIT_FAILURE; |
3785 | 3794 | ||
3786 | m_name = xstrdup(bb_basename(m_filename)); | 3795 | m_name = xstrdup(bb_basename(m_filename)); |
3787 | p = strrchr(m_name, '.'); | 3796 | /* "module.o[.gz]" -> "module" */ |
3788 | if (p) *p = '\0'; | 3797 | *strchrnul(m_name, '.') = '\0'; |
3789 | 3798 | ||
3790 | f = obj_load(fd, LOADBITS); | 3799 | f = obj_load(image, image_size, LOADBITS); |
3791 | 3800 | ||
3792 | m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL); | 3801 | m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL); |
3793 | 3802 | ||
@@ -3816,7 +3825,7 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options) | |||
3816 | #endif | 3825 | #endif |
3817 | 3826 | ||
3818 | if (query_module(NULL, 0, NULL, 0, NULL)) | 3827 | if (query_module(NULL, 0, NULL, 0, NULL)) |
3819 | bb_error_msg_and_die("not configured to support old kernels"); | 3828 | bb_error_msg_and_die("old (unsupported) kernel"); |
3820 | new_get_kernel_symbols(); | 3829 | new_get_kernel_symbols(); |
3821 | k_crcs = new_is_kernel_checksummed(); | 3830 | k_crcs = new_is_kernel_checksummed(); |
3822 | 3831 | ||
@@ -3869,7 +3878,7 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options) | |||
3869 | * the PROGBITS section was not loaded by the obj_load | 3878 | * the PROGBITS section was not loaded by the obj_load |
3870 | * now we can load them directly into the kernel memory | 3879 | * now we can load them directly into the kernel memory |
3871 | */ | 3880 | */ |
3872 | if (!obj_load_progbits(fd, f, (char*)m_addr)) { | 3881 | if (!obj_load_progbits(image, image_size, f, (char*)m_addr)) { |
3873 | delete_module(m_name, 0); | 3882 | delete_module(m_name, 0); |
3874 | goto out; | 3883 | goto out; |
3875 | } | 3884 | } |
@@ -3891,7 +3900,7 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options) | |||
3891 | exit_status = EXIT_SUCCESS; | 3900 | exit_status = EXIT_SUCCESS; |
3892 | 3901 | ||
3893 | out: | 3902 | out: |
3894 | close(fd); | 3903 | free(image); |
3895 | free(m_name); | 3904 | free(m_name); |
3896 | 3905 | ||
3897 | return exit_status; | 3906 | return exit_status; |