From d6b3ab11863ef3413c7b21f064fc02e5a6e60c0d Mon Sep 17 00:00:00 2001 From: Aren Date: Fri, 13 Oct 2023 17:44:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B1=D0=BE=D1=80=D0=BA=D0=B0=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B4=D1=83=D0=BB=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ build.py | 5 ++-- build.sh | 0 configs/limine.cfg | 11 +++---- deploy_to_sdc.sh | 0 include/version.h | 2 +- kernel/mod.cpp | 58 ++++++++++++++++++++++++++++++++++++- modules/helloworld/build.sh | 6 ++++ modules/helloworld/main.cpp | 5 ++++ run.sh | 2 +- 10 files changed, 79 insertions(+), 12 deletions(-) mode change 100644 => 100755 build.sh mode change 100644 => 100755 deploy_to_sdc.sh create mode 100755 modules/helloworld/build.sh mode change 100644 => 100755 run.sh diff --git a/.gitignore b/.gitignore index f11f210..701de54 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ ovmf/ iso_root/ modules/helloworld/helloworld.elf modules/com/com.elf +modules/helloworld/hello.o +modules/helloworld/hello.so diff --git a/build.py b/build.py index 3b26349..64c5a11 100644 --- a/build.py +++ b/build.py @@ -115,7 +115,7 @@ def create_hdd(IMAGE_NAME): subprocess.run(["mcopy", "-i", IMAGE_NAME+".hdd@@1M", "kernel.elf", "configs/limine.cfg", "limine/limine-bios.sys", "::/"]) subprocess.run(["mcopy", "-i", IMAGE_NAME+".hdd@@1M", - "modules/com/com.elf", "modules/helloworld/helloworld.elf", "::/mod"]) + "modules/com/com.elf", "modules/helloworld/hello.so", "::/mod"]) subprocess.run(["mcopy", "-i", IMAGE_NAME+".hdd@@1M", "limine/BOOTX64.EFI", "limine/BOOTIA32.EFI", "::/EFI/BOOT"]) subprocess.run(["mcopy", "-i", IMAGE_NAME+".hdd@@1M", @@ -134,7 +134,7 @@ def create_iso(IMAGE_NAME): "iso_root/"]) subprocess.run(["mkdir", "-p", "iso_root/EFI/BOOT"]) subprocess.run(["mkdir", "-p", "iso_root/mod"]) - subprocess.run(["cp", "-v", "modules/helloworld/helloworld.elf", "iso_root/mod/"]) + subprocess.run(["cp", "-v", "modules/helloworld/hello.so", "iso_root/mod/"]) subprocess.run(["cp", "-v", "modules/com/com.elf", "iso_root/mod/"]) subprocess.run(["cp", "-v", "limine/BOOTX64.EFI", "iso_root/EFI/BOOT/"]) subprocess.run(["cp", "-v", "limine/BOOTIA32.EFI", "iso_root/EFI/BOOT/"]) @@ -162,5 +162,6 @@ def create_iso(IMAGE_NAME): compile_all() create_iso("bmosp") create_hdd("bmosp") + os.system("cd modules/helloworld/ && ./build.sh") print(f"Не забудьте сохранить изменения! Номер сборки: {major}.{minor}, {build}") \ No newline at end of file diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 diff --git a/configs/limine.cfg b/configs/limine.cfg index ac1fec1..7ce6906 100644 --- a/configs/limine.cfg +++ b/configs/limine.cfg @@ -13,11 +13,8 @@ BACKGROUND_PATH=boot:///boot.png KASLR=no KERNEL_PATH=boot:///kernel.elf - MODULE_PATH=boot:///mod/helloworld.elf - MODULE_CMDLINE=helloworld + MODULE_PATH=boot:///mod/hello.so + MODULE_CMDLINE=[MOD]hello.so - MODULE_PATH=boot:///mod/com.elf - MODULE_CMDLINE=com - - MODULE_PATH=boot:///boot.png - MODULE_CMDLINE=bootimage \ No newline at end of file + #MODULE_PATH=boot:///boot.png + #MODULE_CMDLINE=bootimage \ No newline at end of file diff --git a/deploy_to_sdc.sh b/deploy_to_sdc.sh old mode 100644 new mode 100755 diff --git a/include/version.h b/include/version.h index aeb37da..2511017 100644 --- a/include/version.h +++ b/include/version.h @@ -1,3 +1,3 @@ #define VERSION_MAJOR 0 #define VERSION_MINOR 1 -#define VERSION_BUILD 17 +#define VERSION_BUILD 22 diff --git a/kernel/mod.cpp b/kernel/mod.cpp index c067aaa..aea4e0f 100644 --- a/kernel/mod.cpp +++ b/kernel/mod.cpp @@ -2,6 +2,59 @@ #include #include +// Структуры соответствующие ELF заголовкам +typedef struct { + unsigned char e_ident[16]; + uint16_t e_type; + uint16_t e_machine; + uint32_t e_version; + uint64_t e_entry; + uint64_t e_phoff; + uint64_t e_shoff; + uint32_t e_flags; + uint16_t e_ehsize; + uint16_t e_phentsize; + uint16_t e_phnum; + uint16_t e_shentsize; + uint16_t e_shnum; + uint16_t e_shstrndx; +} elf64_header_t; + +void *elf_entry(void *module, uint64_t size) { + // Приводим заголовок ELF файла к типу elf64_header_t + elf64_header_t *elf_header = (elf64_header_t *)module; + + // Выводим данные о заголовке ELF файла + fb::printf("ELF Header:\n"); + fb::printf(" Magic number: "); + for (int i = 0; i < 16; i++) { + fb::printf("0x%x ", elf_header->e_ident[i]); + } + fb::printf("\n"); + fb::printf(" Class: ELF64\n"); + fb::printf(" Version: %u\n", elf_header->e_ident[6]); + fb::printf(" OS/ABI: %u\n", elf_header->e_ident[7]); + fb::printf(" Type: %u\n", elf_header->e_type); + fb::printf(" Machine: %u\n", elf_header->e_machine); + fb::printf(" Version: %u\n", elf_header->e_version); + fb::printf(" Entry point: 0x%x\n", elf_header->e_entry); + fb::printf(" Program header offset: %u\n", elf_header->e_phoff); + fb::printf(" Section header offset: %u\n", elf_header->e_shoff); + fb::printf(" Flags: %u\n", elf_header->e_flags); + fb::printf(" ELF header size: %u (bytes)\n", + elf_header->e_ehsize); + fb::printf(" Program header entry size: %u (bytes)\n", + elf_header->e_phentsize); + fb::printf(" Program header count: %u\n", elf_header->e_phnum); + fb::printf(" Section header entry size: %u (bytes)\n", + elf_header->e_shentsize); + fb::printf(" Section header count: %u\n", elf_header->e_shnum); + fb::printf(" Section header string index: %u\n", elf_header->e_shstrndx); + + // Возвращаем указатель на точку входа + return (void *)elf_header->e_entry; +} + namespace mod { static volatile struct limine_module_request module_request = { .id = LIMINE_MODULE_REQUEST, @@ -24,9 +77,12 @@ void init( ) { fb::printf("->Size: %u, media_type: %u, partition_index: %u\n", module_ptr->size, module_ptr->media_type, module_ptr->partition_index); - fb::printf("->mbr_disk_id: %u, tftp_ip: %u, tftp_port: %u\n\n", + fb::printf("->mbr_disk_id: %u, tftp_ip: %u, tftp_port: %u\n", module_ptr->mbr_disk_id, module_ptr->tftp_ip, module_ptr->tftp_port); + + fb::printf("\t->Entry point: 0x%x\n\n", + elf_entry(module_ptr->address, module_ptr->size)); } } } // namespace mod \ No newline at end of file diff --git a/modules/helloworld/build.sh b/modules/helloworld/build.sh new file mode 100755 index 0000000..97bbafd --- /dev/null +++ b/modules/helloworld/build.sh @@ -0,0 +1,6 @@ +#/bin/sh +echo "Название: Hello world" +echo "Лицензия: Публичное достояние" +g++ -c -fPIC -nostdlib main.cpp -o hello.o +gcc -shared -nostdlib hello.o -o hello.so -Wl,--entry=_start +echo "Сборка завершена, файл: hello.so" diff --git a/modules/helloworld/main.cpp b/modules/helloworld/main.cpp index e69de29..010d457 100644 --- a/modules/helloworld/main.cpp +++ b/modules/helloworld/main.cpp @@ -0,0 +1,5 @@ + + +extern "C" int _start( ) { + return 80; +} \ No newline at end of file diff --git a/run.sh b/run.sh old mode 100644 new mode 100755 index 44db64b..f9e1eff --- a/run.sh +++ b/run.sh @@ -1,2 +1,2 @@ #!/bin/sh -qemu-system-x86_64 -cpu max -m 2G -smp 1 -bios ovmf/OVMF.fd -hda mseos.hdd \ No newline at end of file +qemu-system-x86_64 -cpu max -m 2G -smp 1 -bios ovmf/OVMF.fd -hda bmosp.hdd \ No newline at end of file