Textausgabe will nicht so recht klappen

16/09/2012 - 22:48 von Jens Kallup | Report spam
Hallo Gemeinde,

hier wieder mal ein fast gleiches Assembler Kunstwerk ;-)
Es wird ein driver file geöffnet, versucht zu lesen und wieder geschlossen-
Leider will die Textausgabe nicht klappen.
mach ich da was falsch?

Gruß
Jens

BITS 32
org 0x08048000

ehdr: ; Elf32_Ehdr
db 0x7F, "ELF", 1, 1, 1, 0 ; e_ident
times 8 db 0
dw 2 ; e_type
dw 3 ; e_machine
dd 1 ; e_version
dd _start ; e_entry
dd phdr - $$ ; e_phoff
dd 0 ; e_shoff
dd 0 ; e_flags
dw ehdrsize ; e_ehsize
dw phdrsize ; e_phentsize
dw 1 ; e_phnum
dw 0 ; e_shentsize
dw 0 ; e_shnum
dw 0 ; e_shstrndx

ehdrsize equ $ - ehdr

phdr: ; Elf32_Phdr
dd 1 ; p_type
dd 0 ; p_offset
dd $$ ; p_vaddr
dd $$ ; p_paddr
dd filesize ; p_filesz
dd filesize ; p_memsz
dd 5 ; p_flags
dd 0x1000 ; p_align

phdrsize equ $ - phdr

LINUX_SYSCALL equ 0x80

ST_SIZE_RESERVE equ 8
ST_FD_IN equ 0
ST_ARGC equ 8
ST_ARGC_0 equ 12
ST_ARGV_1 equ 16

DBASE_FUNC_1 equ (2 << 30) | (240 << 8) | (1 << 0) | (200 << 16)

_start:

push ebp
mov ebp, esp
sub esp, 16
open_file:
mov eax, 5
mov ebx, driver_file_name
xor ecx, ecx
int LINUX_SYSCALL

store_fd_in:
cmp eax, -1
jle no_driver_loaded
mov [ebp], eax

call_ioctl_func:
mov eax, 54
mov ebx, [ebp]
mov ecx, DBASE_FUNC_1
mov edx, [message]
int LINUX_SYSCALL

write_text:
mov [ebp + 4], eax
mov eax, 4
mov ebx, 1
mov ecx, [ebp + 4]
mov edx, 8
int LINUX_SYSCALL

close_fd_in:
mov eax, 6
mov ebx, [ebp]
int LINUX_SYSCALL
jmp no_error

no_driver_loaded:
mov eax, 4
mov ebx, 1
mov ecx, err_driver_load
mov edx, err_driver_size-1
int LINUX_SYSCALL

no_error:
mov eax, 1
mov ebx, 42
int LINUX_SYSCALL
leave
ret

filesize equ $ - $$

;--

#ifndef CHARDEV_H
#define CHARDEV_H

#include <linux/ioctl.h>

#define MAJOR_NUM 240
#define DBASE_IOCTL_TEST _IOR(MAJOR_NUM, 1, char*)
#define DBASE_IOCTL_TEST2 _IOR(MAJOR_NUM, 2, char*)

/*
* The name of the device file
*/
#define DEVICE_FILE_NAME "dBaseHelper"
#define DEVICE_NAME DEVICE_FILE_NAME

#endif


;-
#include "chardev.h"

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> /* open */
#include <unistd.h> /* exit */
#include <sys/ioctl.h> /* ioctl */

#include "chardev.h"

void test1(int file_desc)
{
int ret_val;
char message[200];

ret_val = ioctl(file_desc, DBASE_IOCTL_TEST2, message);
if (ret_val < 0) {
printf("ioctl_set_msg failed:%d", ret_val);
exit(-1);
}

printf("-> %s", message);
}

main()
{
int file_desc, ret_val;;

file_desc = open(DEVICE_FILE_NAME, O_RDONLY);
if (file_desc < 0) {
printf("Can't open device file: %s", DEVICE_FILE_NAME);
exit(-1);
}

test1(file_desc);
close(file_desc);
}


#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/fs.h>
#include <asm/uaccess.h> /* for get_user and put_user */

#include "chardev.h"

#define SUCCESS 0
#define BUF_LEN 80

static int major;
static char msg[200];

/*
* This function is called whenever a process which has already opened the
* device file attempts to read from it.
*/
static ssize_t device_read(struct file *file, /* see include/linux/fs.h */
char __user * buffer, /* buffer to be
* filled with data */
size_t length, /* length of the buffer */
loff_t * offset)
{
return simple_read_from_buffer(buffer, length, offset, msg, 200);
}

/*
* This function is called when somebody tries to
* write into our device file.
*/
static ssize_t
device_write(struct file *file,
const char __user * buffer, size_t len, loff_t * offset)
{
if (len > 299)
return -EINVAL;
copy_from_user(msg, buffer, len);
msg[len] = '\0';
return len;
}

char buf[200];
int device_ioctl(struct inode *inode, /* see include/linux/fs.h */
struct file *file, /* ditto */
unsigned int ioctl_num, /* number and param for ioctl */
unsigned long arg)
{
switch (ioctl_num) {
case DBASE_IOCTL_TEST:
strcpy(buf,"Jens79");
copy_to_user((char *)arg, buf, 200);
break;
case DBASE_IOCTL_TEST2:
strcpy(buf,"lupsolo");
copy_to_user((char *)arg, buf, 200);
break;
default:
return -ENOTTY;
}

return SUCCESS;
}

/* Module Declarations */

/*
* This structure will hold the functions to be called
* when a process does something to the device we
* created. Since a pointer to this structure is kept in
* the devices table, it can't be local to
* init_module. NULL is for unimplemented functions.
*/
struct file_operations fops = {
.read = device_read,
.write = device_write,
.ioctl = device_ioctl,
};

/*
* Initialize the module - Register the character device
*/
int __init my_init_module()
{
major = register_chrdev(240, DEVICE_NAME, &fops);
if (major < 0) {
printk ("Registering the character device failed with %d", major);
return major;
}
printk("cdeDEVICE_NAMEv example: assigned major: %d", major);
printk("create node with mknod /dev/cdev_example c %d 0", major);
return 0;
}

/*
* Cleanup - unregister the appropriate file from /proc
*/
void __exit my_cleanup_module()
{
unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
}

module_init(my_init_module);
module_exit(my_cleanup_module);
MODULE_LICENSE("GPL");
 

Lesen sie die antworten

#1 Jens Kallup
16/09/2012 - 22:50 | Warnen spam
segment .data
driver_file_name: db "./driver/dBaseHelper", 0
err_driver_load : db "kann Treiber nicht laden!",10,0
err_driver_size equ $ - err_driver_load
driver_file_desc: dd 0

;segment .bss
;message: resb 200

message: db "kaluppke",0

Ähnliche fragen