From b5ebbd7406e87d19beee3760ef2417e1444a10d2 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 17 Oct 2024 08:16:38 +0200 Subject: FIX/IMP: A set of fixes and new features. (MHR-40, 46 and 47 related) Signed-off-by: Amlal El Mahrouss --- compile_flags.txt | 1 + dev/crt/base_exception.hxx | 7 +++- dev/crt/crt.json | 20 ----------- dev/crt/crtx64.json | 20 +++++++++++ dev/crt/defines.hxx | 1 + dev/crt/src/hal/x86/base_alloc.cxx | 55 +++++++++++++++++++++++++++++ dev/crt/src/hal/x86/exit.cxx | 19 ++++++++++ dev/crt/src/new+delete.cxx | 52 --------------------------- dev/zba/amd64-efi.make | 2 +- tools/common_zka.hxx | 7 ++++ tools/framework.hxx | 13 +++++++ tools/make_framework.json | 13 +++++++ tools/make_zxd.json | 13 +++++++ tools/src/make_framework.cxx | 42 ++++++++++++++++++++++ tools/src/make_zxd.cxx | 72 ++++++++++++++++++++++++++++++++++++++ tools/src/zxdmake.cxx | 72 -------------------------------------- tools/zxd.hxx | 10 ++---- tools/zxdmake.json | 19 ---------- 18 files changed, 265 insertions(+), 173 deletions(-) delete mode 100644 dev/crt/crt.json create mode 100644 dev/crt/crtx64.json create mode 100644 dev/crt/src/hal/x86/base_alloc.cxx create mode 100644 dev/crt/src/hal/x86/exit.cxx delete mode 100644 dev/crt/src/new+delete.cxx create mode 100644 tools/common_zka.hxx create mode 100644 tools/framework.hxx create mode 100644 tools/make_framework.json create mode 100644 tools/make_zxd.json create mode 100644 tools/src/make_framework.cxx create mode 100644 tools/src/make_zxd.cxx delete mode 100644 tools/src/zxdmake.cxx delete mode 100644 tools/zxdmake.json diff --git a/compile_flags.txt b/compile_flags.txt index 0aa5a0d8..4bd4474d 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,6 +1,7 @@ -Idev/zka -Idev/ -Idev/zba/ +-Itools -Idev/zba/BootKit -Idev/base/ -std=c++20 diff --git a/dev/crt/base_exception.hxx b/dev/crt/base_exception.hxx index e73ac11b..e4ee3487 100644 --- a/dev/crt/base_exception.hxx +++ b/dev/crt/base_exception.hxx @@ -9,7 +9,6 @@ #include #include -/// @brief Standard C++ namespace namespace std { inline void __throw_general(void) @@ -23,6 +22,12 @@ namespace std __builtin_unreachable(); // prevent from continuing. } + inline void __throw_bad_alloc(void) + { + __throw_general(); + __builtin_unreachable(); // prevent from continuing. + } + inline void __throw_bad_array_new_length(void) { __throw_general(); diff --git a/dev/crt/crt.json b/dev/crt/crt.json deleted file mode 100644 index fbd54bf8..00000000 --- a/dev/crt/crt.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "compiler_path": "x86_64-w64-mingw32-g++", - "compiler_std": "c++20", - "headers_path": ["../", "./"], - "sources_path": ["src/*.cxx"], - "output_name": "crt.dll", - "compiler_flags": [ - "-ffreestanding", - "-shared", - "-fno-rtti", - "-fno-exceptions", - "-Wl,--subsystem=17" - ], - "cpp_macros": [ - "__CRT_AMD64__", - "cCRTVersion=0x0100", - "cEFSVersionHighest=0x0100", - "cEFSVersionLowest=0x0100" - ] -} diff --git a/dev/crt/crtx64.json b/dev/crt/crtx64.json new file mode 100644 index 00000000..3e321e5d --- /dev/null +++ b/dev/crt/crtx64.json @@ -0,0 +1,20 @@ +{ + "compiler_path": "x86_64-w64-mingw32-g++", + "compiler_std": "c++20", + "headers_path": ["../", "./"], + "sources_path": ["src/*.cxx", "src/hal/x86/*.cxx"], + "output_name": "crtx64.dll", + "compiler_flags": [ + "-ffreestanding", + "-shared", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17" + ], + "cpp_macros": [ + "__CRT_AMD64__", + "cCRTVersion=0x0100", + "cEFSVersionHighest=0x0100", + "cEFSVersionLowest=0x0100" + ] +} diff --git a/dev/crt/defines.hxx b/dev/crt/defines.hxx index 5636c337..73422165 100644 --- a/dev/crt/defines.hxx +++ b/dev/crt/defines.hxx @@ -79,6 +79,7 @@ typedef union double_cast { #endif // ifdef __STD_CXX__ +/// @brief Standard C++ namespace. namespace std { /// @brief Forward object. diff --git a/dev/crt/src/hal/x86/base_alloc.cxx b/dev/crt/src/hal/x86/base_alloc.cxx new file mode 100644 index 00000000..effb6c94 --- /dev/null +++ b/dev/crt/src/hal/x86/base_alloc.cxx @@ -0,0 +1,55 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include + +#define kAllocSyscallId "mov $10, %%r9\n\t" +#define kFreeSyscallId "mov $11, %%r9\n\t" + +void* operator new(size_t length) +{ + void* ptr = nullptr; + + // do syscall according to PEF convention. + asm("mov %0, %%r8\n\t" kAllocSyscallId + "syscall" + : "=r"(ptr)); + + if (!ptr) + std::__throw_bad_alloc(); + + return ptr; +} + +void* operator new[](size_t length) +{ + ptr_type ptr = nullptr; + + // do syscall according to PEF convention. + asm("mov %0, %%r8\n\t" kAllocSyscallId + "syscall" + : "=r"(ptr)); + + if (!ptr) + std::__throw_bad_alloc(); + + return ptr; +} + +void operator delete(void* ptr) noexcept +{ + asm("mov %0, %%r8\n\t" kFreeSyscallId + "syscall" + : "=r"(ptr)); +} + +void operator delete[](void* ptr) noexcept +{ + asm("mov %0, %%r8\n\t" kFreeSyscallId + "syscall" + : "=r"(ptr)); +} diff --git a/dev/crt/src/hal/x86/exit.cxx b/dev/crt/src/hal/x86/exit.cxx new file mode 100644 index 00000000..d593adf9 --- /dev/null +++ b/dev/crt/src/hal/x86/exit.cxx @@ -0,0 +1,19 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include + +#define kAllocSyscallId "mov $12, %%r9\n\t" + +/// @note Just here for building. +extern "C" int exit(int code) +{ + asm("mov 0, %%r8\n\t" kAllocSyscallId + "syscall" + : "=r"(code)); + + return 1; +} diff --git a/dev/crt/src/new+delete.cxx b/dev/crt/src/new+delete.cxx deleted file mode 100644 index 1241bf66..00000000 --- a/dev/crt/src/new+delete.cxx +++ /dev/null @@ -1,52 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include - -#define kAllocSyscallId "mov $10, %%r9\n\t" -#define kFreeSyscallId "mov $11, %%r9\n\t" - -void* operator new(size_t length) -{ - void* ptr = nullptr; - - // do syscall according to PEF convention. - asm ("mov %0, %%r8\n\t" - kAllocSyscallId - "syscall" - : "=r" (ptr)); - - return ptr; -} - -void* operator new[](size_t length) -{ - ptr_type ptr = nullptr; - - // do syscall according to PEF convention. - asm ("mov %0, %%r8\n\t" - kAllocSyscallId - "syscall" - : "=r" (ptr)); - - return ptr; -} - -void operator delete(void* ptr) noexcept -{ - asm ("mov %0, %%r8\n\t" - kFreeSyscallId - "syscall" - : "=r" (ptr)); -} - -void operator delete[](void* ptr) noexcept -{ - asm ("mov %0, %%r8\n\t" - kFreeSyscallId - "syscall" - : "=r" (ptr)); -} diff --git a/dev/zba/amd64-efi.make b/dev/zba/amd64-efi.make index 7bf3ceaa..e1a212ef 100644 --- a/dev/zba/amd64-efi.make +++ b/dev/zba/amd64-efi.make @@ -55,7 +55,7 @@ BOOT_LOADER=newosldr.exe KERNEL=minoskrnl.exe DDK=ddk.dll SCI=sci.dll -CRT=crt.dll +CRT=crtx64.dll SYS_CHK=syschk.sys STARTUP=startup.sys diff --git a/tools/common_zka.hxx b/tools/common_zka.hxx new file mode 100644 index 00000000..3b7a1b19 --- /dev/null +++ b/tools/common_zka.hxx @@ -0,0 +1,7 @@ +#include +#include +#include +#include +#include +#include +#include diff --git a/tools/framework.hxx b/tools/framework.hxx new file mode 100644 index 00000000..555f7aae --- /dev/null +++ b/tools/framework.hxx @@ -0,0 +1,13 @@ +/** + Thu Oct 17 07:57:43 CEST 2024 + (C) ZKA Technologies. +*/ + +#pragma once + +#include + +#define kFKDLLDirectory "ZKA/DLL/" +#define kFKManifestDirectory "ZKA/Manifests/" +#define kFKRootDirectory "ZKA/" +#define kFKExtension ".framework" diff --git a/tools/make_framework.json b/tools/make_framework.json new file mode 100644 index 00000000..c597e5d6 --- /dev/null +++ b/tools/make_framework.json @@ -0,0 +1,13 @@ +{ + "compiler_path": "g++", + "compiler_std": "c++20", + "headers_path": ["./"], + "sources_path": ["src/make_framework.cxx"], + "output_name": "make_framework.exe", + "cpp_macros": [ + "__MKF_AMD64__", + "cMKFVersion=0x0100", + "cMKFVersionHighest=0x0100", + "cMKFVersionLowest=0x0100" + ] +} diff --git a/tools/make_zxd.json b/tools/make_zxd.json new file mode 100644 index 00000000..1c25c1a2 --- /dev/null +++ b/tools/make_zxd.json @@ -0,0 +1,13 @@ +{ + "compiler_path": "g++", + "compiler_std": "c++20", + "headers_path": ["./"], + "sources_path": ["src/make_zxd.cxx"], + "output_name": "make_zxd.exe", + "cpp_macros": [ + "__DRVSIGN_AMD64__", + "cDSVersion=0x0100", + "cDSVersionHighest=0x0100", + "cDSVersionLowest=0x0100" + ] +} diff --git a/tools/src/make_framework.cxx b/tools/src/make_framework.cxx new file mode 100644 index 00000000..3d719529 --- /dev/null +++ b/tools/src/make_framework.cxx @@ -0,0 +1,42 @@ +/* + * Created on Thu Oct 17 08:00:42 CEST 2024 + * + * Copyright (c) 2024 ZKA Technologies + */ + +#include +#include + +/// @brief This program converts a PE32+ driver, into a custom format, the ZXD. +/// @note ZXD is a format for ZKA signed drivers. +int main(int argc, char* argv[]) +{ + for (size_t i = 1ul; i < argc; ++i) + { + if (strcmp(argv[i], "/?") == 0) + { + std::cout << "make_framework: Framework Tool.\n"; + std::cout << "make_framework: © ZKA Technologies, all rights reserved.\n"; + + return 0; + } + } + + auto path = std::string(argv[1]); + + if (!path.ends_with(kFKExtension)) + return 1; + + std::filesystem::path path_arg = path; + + if (std::filesystem::create_directory(path_arg)) + { + std::filesystem::create_directory(path_arg / kFKRootDirectory); + std::filesystem::create_directory(path_arg / kFKManifestDirectory); + std::filesystem::create_directory(path_arg / kFKDLLDirectory); + + return 0; + } + + return 1; +} diff --git a/tools/src/make_zxd.cxx b/tools/src/make_zxd.cxx new file mode 100644 index 00000000..2a456e28 --- /dev/null +++ b/tools/src/make_zxd.cxx @@ -0,0 +1,72 @@ +/* + * Created on Thu Aug 22 09:29:13 CEST 2024 + * + * Copyright (c) 2024 ZKA Technologies + */ + +#include + +/// @brief This program converts a PE32+ driver, into a custom format, the ZXD. +/// @note ZXD is a format for ZKA signed drivers. +int main(int argc, char* argv[]) +{ + for (size_t i = 1ul; i < argc; ++i) + { + if (strcmp(argv[i], "/?") == 0) + { + std::cout << "make_zxd: ZXD Tool.\n"; + std::cout << "make_zxd: © ZKA Technologies, all rights reserved.\n"; + + return 0; + } + } + + if (!std::filesystem::exists(argv[1]) || + !std::string(argv[1]).ends_with(kDriverExt)) + return -1; + + ZXD::ZXD_HEADER zxd_hdr{0}; + + zxd_hdr.d_binary_version = 1; + + memcpy(zxd_hdr.d_binary_magic, kSignedDriverMagic, strlen(kSignedDriverMagic)); + memcpy(zxd_hdr.d_binary_name, argv[1], strlen(argv[1])); + + zxd_hdr.d_binary_size = std::filesystem::file_size(argv[1]); + + memset(zxd_hdr.d_binary_padding, 0x00, 512); + + zxd_hdr.d_binary_checksum = 0; + + std::string signed_path = argv[1]; + signed_path.erase(signed_path.find(kDriverExt), strlen(kDriverExt)); + signed_path += kDriverSignedExt; + + std::ofstream of_drv(signed_path, std::ios::binary); + std::ifstream if_drv(argv[1], std::ios::binary); + + std::stringstream ss; + ss << if_drv.rdbuf(); + + if (!ZXD::zxd_check_for_mz(ss.str())) + { + std::filesystem::remove(signed_path); + std::cout << "zxdmake: Couldn't sign current driver, Input driver isn't a valid executable.\n"; + + return 1; + } + + for (auto ch : ss.str()) + { + zxd_hdr.d_binary_checksum |= ch; + } + + zxd_hdr.d_binary_checksum ^= zxd_hdr.d_binary_size; + + of_drv.write((char*)&zxd_hdr, sizeof(ZXD::ZXD_HEADER)); + of_drv.write(ss.str().c_str(), ss.str().size()); + + std::cout << "zxdmake: Signing is done, quiting, Checksum: " << zxd_hdr.d_binary_checksum << ".\n"; + + return 0; +} diff --git a/tools/src/zxdmake.cxx b/tools/src/zxdmake.cxx deleted file mode 100644 index 8a3276ff..00000000 --- a/tools/src/zxdmake.cxx +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Created on Thu Aug 22 09:29:13 CEST 2024 - * - * Copyright (c) 2024 ZKA Technologies - */ - -#include - -/// @brief This program converts a PE32+ driver, into a custom format, the ZXD. -/// @note ZXD is a format for ZKA signed drivers. -int main(int argc, char* argv[]) -{ - for (size_t i = 1ul; i < argc; ++i) - { - if (strcmp(argv[i], "/?") == 0) - { - std::cout << "zxdmake: ZKA ZXD Driver Tool.\n"; - std::cout << "zxdmake: © ZKA Technologies, all rights reserved.\n"; - - return 0; - } - } - - if (!std::filesystem::exists(argv[1]) || - !std::string(argv[1]).ends_with(kDriverExt)) - return -1; - - ZXD::ZXD_HEADER zxd_hdr{0}; - - zxd_hdr.d_binary_version = 1; - - memcpy(zxd_hdr.d_binary_magic, kSignedDriverMagic, strlen(kSignedDriverMagic)); - memcpy(zxd_hdr.d_binary_name, argv[1], strlen(argv[1])); - - zxd_hdr.d_binary_size = std::filesystem::file_size(argv[1]); - - memset(zxd_hdr.d_binary_padding, 0x00, 512); - - zxd_hdr.d_binary_checksum = 0; - - std::string signed_path = argv[1]; - signed_path.erase(signed_path.find(kDriverExt), strlen(kDriverExt)); - signed_path += kDriverSignedExt; - - std::ofstream of_drv(signed_path, std::ios::binary); - std::ifstream if_drv(argv[1], std::ios::binary); - - std::stringstream ss; - ss << if_drv.rdbuf(); - - if (!ZXD::zxd_check_for_mz(ss.str())) - { - std::filesystem::remove(signed_path); - std::cout << "zxdmake: Couldn't sign current driver, Input driver isn't a valid executable.\n"; - - return 1; - } - - for (auto ch : ss.str()) - { - zxd_hdr.d_binary_checksum |= ch; - } - - zxd_hdr.d_binary_checksum ^= zxd_hdr.d_binary_size; - - of_drv.write((char*)&zxd_hdr, sizeof(ZXD::ZXD_HEADER)); - of_drv.write(ss.str().c_str(), ss.str().size()); - - std::cout << "zxdmake: Signing is done, quiting, Checksum: " << zxd_hdr.d_binary_checksum << ".\n"; - - return 0; -} diff --git a/tools/zxd.hxx b/tools/zxd.hxx index 123c9124..bf942241 100644 --- a/tools/zxd.hxx +++ b/tools/zxd.hxx @@ -6,13 +6,7 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include +#include #define kDriverSignedExt ".zxd" #define kDriverExt ".sys" @@ -53,4 +47,4 @@ namespace ZXD return mz_blob[0] == 'M' && mz_blob[1] == 'Z'; } -} // namespace ZXD \ No newline at end of file +} // namespace ZXD diff --git a/tools/zxdmake.json b/tools/zxdmake.json deleted file mode 100644 index d24a5de9..00000000 --- a/tools/zxdmake.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "compiler_path": "x86_64-w64-mingw32-g++.exe", - "compiler_std": "c++20", - "headers_path": [ "../", "./", "../../" ], - "sources_path": [ "src/zxdmake.cxx" ], - "output_name": "zxdmake.exe", - "compiler_flags": [ - "-ffreestanding", - "-fno-rtti", - "-fno-exceptions", - "-Wl,--subsystem=17" - ], - "cpp_macros": [ - "__DRVSIGN_AMD64__", - "cDSVersion=0x0100", - "cDSVersionHighest=0x0100", - "cDSVersionLowest=0x0100" - ] -} -- cgit v1.2.3