- Added the simplest examples of drivers (fasm and kos32-gcc).

- Added a program for testing them.

git-svn-id: svn://kolibrios.org@8703 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
turbocat 2021-04-29 18:49:57 +00:00
parent caa49e901b
commit 14b216f2e8
7 changed files with 268 additions and 0 deletions

View File

@ -0,0 +1,9 @@
FASM = fasm
KPACK = kpack
all:
$(FASM) asm_drv.asm
$(KPACK) asm_drv.sys
clean:
rm *.sys

View File

@ -0,0 +1,61 @@
format PE DLL native
entry START
__DEBUG__ = 1
__DEBUG_LEVEL__ = 2
section '.flat' readable writable executable
include '../../proc32.inc'
include '../../struct.inc'
include '../../macros.inc'
include '../../fdo.inc'
proc START c, reason:dword
DEBUGF 2,"Loading 'asm_drv' driver\n"
cmp [reason], DRV_ENTRY
jne .fail
invoke RegService, my_service, service_proc
ret
.fail:
xor eax, eax
ret
endp
proc service_proc stdcall, ioctl:dword
mov ebx, [ioctl]
mov eax, [ebx + IOCTL.io_code]
cmp eax, 0 ; FUNCTION ADD(a+b);
jne .fail
mov eax, [ebx + IOCTL.input]
mov ecx, eax
add ecx, 4
mov eax, [eax]
add eax, [ecx]
mov ecx, [ebx + IOCTL.output]
mov [ecx], eax
xor eax, eax
ret
.fail:
or eax, -1
ret
endp
data fixups
end data
include '../../peimport.inc'
my_service db 'asm_drv',0 ; max 16 chars include zero
include_debug_strings

View File

@ -0,0 +1,42 @@
CC = kos32-gcc
LD = kos32-ld
KPACK = kpack
DDK_TOPDIR = ../../ddk
DRV_INCLUDES = ../../include
INCLUDES = -I$(DRV_INCLUDES) \
-I$(DRV_INCLUDES)/asm \
-I$(DRV_INCLUDES)/uapi \
-I$(DRV_INCLUDES)/drm
NAME=c_drv
DEFINES = -DKOLIBRI -D__KERNEL__ -DCONFIG_X86_32 -DCONFIG_DMI -DCONFIG_TINY_RCU
DEFINES+= -DCONFIG_X86_L1_CACHE_SHIFT=6 -DCONFIG_ARCH_HAS_CACHE_LINE_SIZE
DEFINES+= -DCONFIG_PRINTK
CFLAGS= -c -O2 $(DEFINES) $(INCLUDES) -march=i686 -fno-ident -msse2 -fomit-frame-pointer -fno-builtin-printf
CFLAGS+= -mno-stack-arg-probe -mpreferred-stack-boundary=2 -mincoming-stack-boundary=2 -mno-ms-bitfields
LIBPATH = -L $(DDK_TOPDIR)
LIBPATH+= -L ../../../contrib/sdk/lib
LIBS:= -lddk -lcore -lgcc
PE_FLAGS = --major-os-version 0 --minor-os-version 7 --major-subsystem-version 0 \
--minor-subsystem-version 5 --subsystem native
LDFLAGS = -nostdlib -shared -s $(PE_FLAGS) --image-base 0\
--file-alignment 512 --section-alignment 4096
all: $(NAME).dll
$(NAME).dll:
$(CC) $(CFLAGS) c_drv.c
$(LD) $(LIBPATH) $(LDFLAGS) -T drv.lds c_drv.o -o $@ $(NAME_OBJS) $(LIBS)
$(KPACK) $(NAME).dll
clean:
rm -rf *.o *.dll

View File

@ -0,0 +1,21 @@
#include "linux/kernel.h"
#include <ddk.h>
#include <syscall.h>
static int __stdcall service_proc(ioctl_t *my_ctl){
int c = 0;
int a = *(int*)(my_ctl->input);
int b = *(int*)(my_ctl->input+4);
if(my_ctl->io_code==0){
c = a + b;
}else{
return -1;
}
*(int*)(my_ctl->output) = c;
return 0;
}
unsigned drvEntry(int action, char *cmdline){
printk("Driver c_drv.dll loaded!\n");
return RegService("c_drv", service_proc);
}

View File

@ -0,0 +1,57 @@
OUTPUT_FORMAT(pei-i386)
ENTRY("_drvEntry")
SECTIONS
{
. = SIZEOF_HEADERS;
. = ALIGN(__section_alignment__);
.text __image_base__ + ( __section_alignment__ < 0x1000 ? . : __section_alignment__ ) :
{
*(.text) *(.rdata)
}
.data ALIGN(__section_alignment__) :
{
*(.data)
}
.bss ALIGN(__section_alignment__):
{
*(.bss)
*(COMMON)
}
/DISCARD/ :
{
*(.debug$S)
*(.debug$T)
*(.debug$F)
*(.drectve)
*(.edata)
*(.eh_frame)
}
.idata ALIGN(__section_alignment__):
{
SORT(*)(.idata$2)
SORT(*)(.idata$3)
/* These zeroes mark the end of the import list. */
LONG (0); LONG (0); LONG (0); LONG (0); LONG (0);
SORT(*)(.idata$4)
SORT(*)(.idata$5)
SORT(*)(.idata$6)
SORT(*)(.idata$7)
}
.reloc ALIGN(__section_alignment__) :
{
*(.reloc)
}
}

View File

@ -0,0 +1,12 @@
KTCC=../../../programs/develop/ktcc/trunk/bin/kos32-tcc
KPACK = kpack
KLIBC = ../../../programs/develop/libraries/kolibri-libc
CFLAGS = -I $(KLIBC)/include
LDFLAGS = -nostdlib -L$(KLIBC)/lib $(KLIBC)/lib/crt0.o
all:
$(KTCC) $(CFLAGS) $(LDFLAGS) drv_test.c -o drv_test -lc.obj
$(KPACK) --nologo drv_test
clean:
rm *.kex

View File

@ -0,0 +1,66 @@
#include <sys/ksys.h>
#include <stdio.h>
#include <stdlib.h>
char* drv_name = NULL;
struct{
int a;
int b;
}add_struct;
int sum=0;
ksys_drv_hand_t drv = 0;
#define DRV_ADD_FUNC 0
int main(int argc, char** argv){
puts("Which driver?");
puts("1 - asm_drv.sys\n2 - c_drv.dll");
int num_drv = (char)getchar()-'0';
switch (num_drv) {
case 1 :
drv_name = "asm_drv";
drv = _ksys_load_driver(drv_name);
break;
case 2 :
drv_name = "/rd/1/drivers/c_drv.dll";
drv = _ksys_load_pe_driver(drv_name, NULL);
break;
default:
printf("No driver selected!\n");
exit(0);
}
if(!drv){
printf("'%s' driver not load!\n", drv_name);
exit(0);
}else{
printf("'%s' driver is load!\n", drv_name);
}
add_struct.a = 43;
add_struct.b = 532;
ksys_drv_ctl_t ioctl;
ioctl.func_num = DRV_ADD_FUNC;
ioctl.handler = drv;
ioctl.in_data_ptr = &add_struct;
ioctl.in_data_size = sizeof(add_struct);
ioctl.out_data_ptr = &sum;
ioctl.out_data_size = sizeof(sum);
unsigned status =_ksys_work_driver(&ioctl);
if(status==-1){
puts("Error!");
}else {
printf("%d + %d = %d\n", add_struct.a, add_struct.b, sum);
if(sum == add_struct.a + add_struct.b){
puts("True!");
}else{
puts("False!");
}
}
exit(0);
}