From 35ba6d85629b272a508f58e0d4a2aba07359c439 Mon Sep 17 00:00:00 2001 From: turbocat Date: Wed, 28 Apr 2021 00:18:12 +0000 Subject: [PATCH] kolibri-libc: - [KSYS] added functions for working with drivers. - [SAMPLES] added an example of working with the tmpdisk.sys driver - Misc: fixed Makefiles git-svn-id: svn://kolibrios.org@8699 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../libraries/kolibri-libc/include/sys/ksys.h | 47 +++++++++++++--- .../libraries/kolibri-libc/samples/Makefile | 3 +- .../kolibri-libc/samples/tmpdisk_work.c | 56 +++++++++++++++++++ .../libraries/kolibri-libc/source/Makefile | 10 +++- 4 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 programs/develop/libraries/kolibri-libc/samples/tmpdisk_work.c diff --git a/programs/develop/libraries/kolibri-libc/include/sys/ksys.h b/programs/develop/libraries/kolibri-libc/include/sys/ksys.h index 88f23db9a3..7a9293cbbd 100644 --- a/programs/develop/libraries/kolibri-libc/include/sys/ksys.h +++ b/programs/develop/libraries/kolibri-libc/include/sys/ksys.h @@ -48,15 +48,6 @@ typedef union ksys_oskey_t{ }; }ksys_oskey_t; -typedef struct{ - unsigned handle; - unsigned io_code; - unsigned *input; - int inp_size; - void *output; - int out_size; -}ksys_ioctl_t; - typedef struct{ void *data; size_t size; @@ -152,6 +143,17 @@ typedef struct { void* func_ptr; }ksys_coff_etable_t; +typedef void* ksys_drv_hand_t; + +typedef struct{ + ksys_drv_hand_t handler; + unsigned func_num; + void* in_data_ptr; + unsigned in_data_size; + void* out_data_ptr; + unsigned out_data_size; +}ksys_drv_ctl_t; + #pragma pack(pop) enum KSYS_EVENTS { @@ -999,6 +1001,33 @@ void _ksys_shm_close(char *shm_name) ); } +/* Driver functions */ + +static inline +ksys_drv_hand_t _ksys_load_driver(char *driver_name) +{ + ksys_drv_hand_t driver_h; + asm_inline( + "int $0x40" + :"=a"(driver_h) + :"a"(68), "b"(16), "c"(driver_name) + ); + return driver_h; +} + +static inline +unsigned _ksys_work_driver(ksys_drv_ctl_t *ioctl) +{ + unsigned status; + asm_inline( + "int $0x40" + :"=a"(status) + :"a"(68), "b"(17), "c"(ioctl) + :"memory" + ); + return status; +} + #ifdef __cplusplus } #endif diff --git a/programs/develop/libraries/kolibri-libc/samples/Makefile b/programs/develop/libraries/kolibri-libc/samples/Makefile index 29243a4b1c..ebf5b7b2b3 100644 --- a/programs/develop/libraries/kolibri-libc/samples/Makefile +++ b/programs/develop/libraries/kolibri-libc/samples/Makefile @@ -1,5 +1,5 @@ -KTCC=../linuxtools/kos32-tcc +KTCC=../../../ktcc/trunk/bin/kos32-tcc KPACK = kpack CFLAGS = -I../include LDFLAGS = -nostdlib -L../lib ../lib/crt0.o @@ -11,6 +11,7 @@ math_test.kex \ string_test.kex \ whois.kex \ file_io.kex \ +tmpdisk_work.kex all: $(BIN) diff --git a/programs/develop/libraries/kolibri-libc/samples/tmpdisk_work.c b/programs/develop/libraries/kolibri-libc/samples/tmpdisk_work.c new file mode 100644 index 0000000000..511c6598e1 --- /dev/null +++ b/programs/develop/libraries/kolibri-libc/samples/tmpdisk_work.c @@ -0,0 +1,56 @@ +#include "stddef.h" +#include +#include +#include + +#define DEV_ADD_DISK 1 + +#define TMPDISK_SIZE 10 //Mb + +#pragma pack(push, 1) +struct{ + unsigned disk_size; + unsigned char disk_id; +}tmpdisk_add; +#pragma pack(pop) + +char *tmpdisk_res_text[]={ + "TmpDisk operation completed successfully", + "Unknown IOCTL code, wrong input/output size...", + "DiskId must be from 0 to 9", + "DiskSize is too large", + "DiskSize is too small, might be too little free RAM", + "Memory allocation failed", + "Unknown error O_o", + 0}; + + +int main(){ + ksys_drv_hand_t tmpdisk_drv = _ksys_load_driver("tmpdisk"); + if(!tmpdisk_drv){ + puts("tmpdisk.sys driver not load!"); + exit(0); + }else{ + puts("tmpdisk.sys driver is load!"); + } + + tmpdisk_add.disk_size = TMPDISK_SIZE*1024*1024/512; + tmpdisk_add.disk_id = 5; + + ksys_drv_ctl_t ioctl; + ioctl.func_num = DEV_ADD_DISK; + ioctl.handler = tmpdisk_drv; + ioctl.in_data_ptr = &tmpdisk_add; + ioctl.in_data_size = sizeof(tmpdisk_add); + ioctl.out_data_ptr = NULL; + ioctl.out_data_size = 0; + + printf("Create '/tmp%u/' disk a %u Mb size...\n", tmpdisk_add.disk_id, TMPDISK_SIZE); + unsigned status =_ksys_work_driver(&ioctl); + if(status<7){ + puts(tmpdisk_res_text[status]); + }else{ + puts(tmpdisk_res_text[6]); + } + exit(0); +} \ No newline at end of file diff --git a/programs/develop/libraries/kolibri-libc/source/Makefile b/programs/develop/libraries/kolibri-libc/source/Makefile index 9d89014f70..f5953f1d50 100644 --- a/programs/develop/libraries/kolibri-libc/source/Makefile +++ b/programs/develop/libraries/kolibri-libc/source/Makefile @@ -1,5 +1,6 @@ - -CC=kos32-gcc +ifndef GCC + GCC=kos32-gcc +endif KPACK=kpack FASM=fasm @@ -14,8 +15,11 @@ all: mkdir -p exports ../lib ../linuxtools/ExportGen symbols.txt exports/exports.c $(FASM) crt/crt0.asm ../lib/crt0.o - $(CC) $(CFLAGS) $(SRC) -o $(LIB) + $(GCC) $(CFLAGS) $(SRC) -o $(LIB) $(KPACK) $(LIB) + ../linuxtools/LoaderGen symbols.txt ../loader + ../linuxtools/LoaderBuild ../loader + rm -rf exports install: cp -f ../lib/libc.obj ~/.kex/root/RD/1/LIB