diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-04-20 16:47:35 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-04-20 16:47:35 +0200 |
| commit | bc97b2b790eb06d8f9adb8ef8fa41874d7d19770 (patch) | |
| tree | a6929db78467c2b07877abb9d9aad579d77139bb | |
| parent | 699e395505a602f4a731c481646233d87f87a85c (diff) | |
kernel, dev: python script to make an ESP from files.
- Started implementing the wrt_* APIs (Wide string functions)
- Make user.sys link now, need to implement more syscalls.
- Tiny HEFS refactors too.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | compile_flags.txt | 4 | ||||
| -rwxr-xr-x | debug_ahci_x64.sh | 3 | ||||
| -rwxr-xr-x | debug_ata_x64.sh | 3 | ||||
| -rw-r--r-- | dev/kernel/FSKit/HeFS.h | 12 | ||||
| -rw-r--r-- | dev/kernel/NewKit/Utils.h | 1 | ||||
| -rw-r--r-- | dev/kernel/amd64-desktop.make | 2 | ||||
| -rw-r--r-- | dev/kernel/src/FS/HeFS.cc | 21 | ||||
| -rw-r--r-- | dev/kernel/src/KString.cc | 2 | ||||
| -rw-r--r-- | dev/kernel/src/Utils.cc | 10 | ||||
| -rw-r--r-- | dev/user/Opts.h | 12 | ||||
| -rw-r--r-- | dev/user/src/SystemCalls+IO.asm | 2 | ||||
| -rw-r--r-- | dev/user/src/SystemCalls.cc | 34 | ||||
| -rw-r--r-- | dev/user/user.json | 1 | ||||
| -rwxr-xr-x | tooling/copy_to_fat32.py | 44 |
14 files changed, 125 insertions, 26 deletions
diff --git a/compile_flags.txt b/compile_flags.txt index 59862b28..001d0cfc 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -12,7 +12,6 @@ -std=c++20 -D__NE_AMD64__ -D__NEOSKRNL__ --D__FSKIT_INCLUDES_NEFS__ -D__NE_ED__ -xc++ -D__AHCI__ @@ -23,5 +22,6 @@ -Wpedantic -Wextra -DBOOTZ_GPT_SUPPORT --D__FSKIT_INCLUDES_HeFS__ +-D__FSKIT_INCLUDES_HEFS__ +-D__FSKIT_INCLUDES_NEFS__ -D__FSKIT_INCLUDES_EXT2__
\ No newline at end of file diff --git a/debug_ahci_x64.sh b/debug_ahci_x64.sh index 8c750a15..24b80246 100755 --- a/debug_ahci_x64.sh +++ b/debug_ahci_x64.sh @@ -9,4 +9,7 @@ cd dev/kernel make -f amd64-desktop.make all cd ../boot make -f amd64-desktop.make all +cd ../../ +./tooling/copy_to_fat32.py ./dev/boot/src/nekernel-esp.img ./dev/boot/src/root +cd dev/boot make -f amd64-desktop.make run-efi-amd64-ahci
\ No newline at end of file diff --git a/debug_ata_x64.sh b/debug_ata_x64.sh index b91f7d47..767e53f9 100755 --- a/debug_ata_x64.sh +++ b/debug_ata_x64.sh @@ -9,4 +9,7 @@ cd dev/kernel make -f amd64-desktop.make all cd ../boot make -f amd64-desktop.make all +cd ../../ +./tooling/copy_to_fat32.py ./dev/boot/src/nekernel-esp.img ./dev/boot/src/root +cd dev/boot make -f amd64-desktop.make run-efi-amd64-ata-pio diff --git a/dev/kernel/FSKit/HeFS.h b/dev/kernel/FSKit/HeFS.h index 58f51ae3..d0311a38 100644 --- a/dev/kernel/FSKit/HeFS.h +++ b/dev/kernel/FSKit/HeFS.h @@ -26,9 +26,9 @@ #define kHeFSMinimumDiskSize (gib_cast(4))
-struct HeFS_BOOT_NODE;
-struct HeFS_INDEX_NODE;
-struct HeFS_INDEX_NODE_DIRECTORY;
+struct HEFS_BOOT_NODE;
+struct HEFS_INDEX_NODE;
+struct HEFS_INDEX_NODE_DIRECTORY;
enum
{
@@ -84,7 +84,7 @@ inline constexpr UInt16 kHeFSInvalidVID = 0xFFFF; /// @brief HeFS Boot node.
/// @details Acts like a superblock, it contains the information about the filesystem.
/// @note The boot node is the first block of the filesystem.
-struct PACKED HeFS_BOOT_NODE final
+struct PACKED HEFS_BOOT_NODE final
{
Kernel::Char fMagic[kHeFSMagicLen]; /// @brief Magic number of the filesystem.
Kernel::Utf16Char fVolName[kHeFSPartNameLen]; /// @brief Volume name.
@@ -115,7 +115,7 @@ inline constexpr ATime kHeFSTimeMax = 0xFFFFFFFFFFFFFFFF; /// @details This structure is used to store the file information of a file.
/// @note The index node is a special type of INode that contains the file information.
/// @note The index node is used to store the file information of a file.
-struct PACKED HeFS_INDEX_NODE final
+struct PACKED HEFS_INDEX_NODE final
{
Kernel::Utf16Char fName[kHeFSFileNameLen]; /// @brief File name.
Kernel::UInt32 fFlags; /// @brief File flags.
@@ -147,7 +147,7 @@ enum /// @brief HeFS directory node.
/// @details This structure is used to store the directory information of a file.
/// @note The directory node is a special type of INode that contains the directory entries.
-struct PACKED HeFS_INDEX_NODE_DIRECTORY final
+struct PACKED HEFS_INDEX_NODE_DIRECTORY final
{
Kernel::Utf16Char fName[kHeFSFileNameLen]; /// @brief Directory name.
diff --git a/dev/kernel/NewKit/Utils.h b/dev/kernel/NewKit/Utils.h index 5fc77366..7340631b 100644 --- a/dev/kernel/NewKit/Utils.h +++ b/dev/kernel/NewKit/Utils.h @@ -18,6 +18,7 @@ namespace Kernel Int rt_string_cmp(const Char* src, const Char* cmp, Size len); const Char* rt_alloc_string(const Char* text); Size rt_string_len(const Char* str); + Size wrt_string_len(const Utf16Char* str); Size rt_string_len(const Char* str, SizeT _len); Boolean rt_to_string(Char* str_out, UInt64 base, Int32 limit); Boolean rt_is_newln(Char chr); diff --git a/dev/kernel/amd64-desktop.make b/dev/kernel/amd64-desktop.make index 714b045d..ca31d130 100644 --- a/dev/kernel/amd64-desktop.make +++ b/dev/kernel/amd64-desktop.make @@ -5,7 +5,7 @@ CXX = x86_64-w64-mingw32-g++ LD = x86_64-w64-mingw32-ld -CCFLAGS = -fshort-wchar -c -D__NE_AMD64__ -Wall -Wpedantic -Wextra -mno-red-zone -fno-rtti -fno-exceptions -std=c++20 -D__FSKIT_INCLUDES_HeFS__ -D__NE_SUPPORT_NX__ -O0 -I../vendor -D__FSKIT_INCLUDES_NEFS__ -D__NEOSKRNL__ -D__HAVE_NE_APIS__ -D__FREESTANDING__ -D__NE_VIRTUAL_MEMORY_SUPPORT__ -D__NE_AUTO_FORMAT__ -D__NE__ -I./ -I../ -I../boot +CCFLAGS = -fshort-wchar -c -D__NE_AMD64__ -Wall -Wpedantic -Wextra -mno-red-zone -fno-rtti -fno-exceptions -std=c++20 -D__FSKIT_INCLUDES_HEFS__ -D__NE_SUPPORT_NX__ -O0 -I../vendor -D__FSKIT_INCLUDES_NEFS__ -D__NEOSKRNL__ -D__HAVE_NE_APIS__ -D__FREESTANDING__ -D__NE_VIRTUAL_MEMORY_SUPPORT__ -D__NE_AUTO_FORMAT__ -D__NE__ -I./ -I../ -I../boot ASM = nasm diff --git a/dev/kernel/src/FS/HeFS.cc b/dev/kernel/src/FS/HeFS.cc index 0344309e..142ddbea 100644 --- a/dev/kernel/src/FS/HeFS.cc +++ b/dev/kernel/src/FS/HeFS.cc @@ -4,7 +4,7 @@ ------------------------------------------- */ -#ifdef __FSKIT_INCLUDES_HeFS__ +#ifdef __FSKIT_INCLUDES_HEFS__ #include <modules/AHCI/AHCI.h> #include <modules/ATA/ATA.h> @@ -22,12 +22,12 @@ namespace Kernel { namespace Detail { - STATIC HeFS_INDEX_NODE* hefs_get_index_node(HeFS_BOOT_NODE* root, DriveTrait* mnt, const Utf16Char* dir_name, const Utf16Char* file_name, UInt8 kind) noexcept + STATIC HEFS_INDEX_NODE* hefs_get_index_node(HEFS_BOOT_NODE* root, DriveTrait* mnt, const Utf16Char* dir_name, const Utf16Char* file_name, UInt8 kind) noexcept { if (root) { - HeFS_INDEX_NODE* node = new HeFS_INDEX_NODE(); - HeFS_INDEX_NODE_DIRECTORY* dir = new HeFS_INDEX_NODE_DIRECTORY(); + HEFS_INDEX_NODE* node = new HEFS_INDEX_NODE(); + HEFS_INDEX_NODE_DIRECTORY* dir = new HEFS_INDEX_NODE_DIRECTORY(); if (!node) { @@ -45,7 +45,7 @@ namespace Kernel if (hop_watch++ > 100) { kout << "Error: Hop watch exceeded.\r"; - + break; } @@ -56,7 +56,7 @@ namespace Kernel } mnt->fPacket.fPacketLba = start; - mnt->fPacket.fPacketSize = sizeof(HeFS_INDEX_NODE_DIRECTORY); + mnt->fPacket.fPacketSize = sizeof(HEFS_INDEX_NODE_DIRECTORY); mnt->fPacket.fPacketContent = dir; mnt->fInput(mnt->fPacket); @@ -81,7 +81,7 @@ namespace Kernel if (dir->fIndexNodeStart[inode_index] != 0) { mnt->fPacket.fPacketLba = dir->fIndexNodeStart[inode_index]; - mnt->fPacket.fPacketSize = sizeof(HeFS_INDEX_NODE); + mnt->fPacket.fPacketSize = sizeof(HEFS_INDEX_NODE); mnt->fPacket.fPacketContent = node; mnt->fInput(mnt->fPacket); @@ -103,7 +103,7 @@ namespace Kernel else if (dir->fIndexNodeEnd[inode_index] != 0) { mnt->fPacket.fPacketLba = dir->fIndexNodeEnd[inode_index]; - mnt->fPacket.fPacketSize = sizeof(HeFS_INDEX_NODE); + mnt->fPacket.fPacketSize = sizeof(HEFS_INDEX_NODE); mnt->fPacket.fPacketContent = node; mnt->fInput(mnt->fPacket); @@ -151,4 +151,7 @@ namespace Kernel } // namespace Detail } // namespace Kernel -#endif // ifdef __FSKIT_INCLUDES_HeFS__ +/// @note HeFS will allocate inodes and ind in advance, to avoid having to allocate them in real-time. +/// @note This is certainly take longer to format a disk with it, but worth-it in the long run. + +#endif // ifdef __FSKIT_INCLUDES_HEFS__ diff --git a/dev/kernel/src/KString.cc b/dev/kernel/src/KString.cc index 2f81ad3d..cb21e003 100644 --- a/dev/kernel/src/KString.cc +++ b/dev/kernel/src/KString.cc @@ -146,7 +146,7 @@ namespace Kernel /// @note This is unsafe!!! bool KStringBuilder::Equals(const Utf16Char* lhs, const Utf16Char* rhs) { - for (Size index = 0; index[rhs] != 0; ++index) + for (Size index = 0; index < wrt_string_len(rhs); ++index) { if (rhs[index] != lhs[index]) return false; diff --git a/dev/kernel/src/Utils.cc b/dev/kernel/src/Utils.cc index f5e61ddf..10c5d714 100644 --- a/dev/kernel/src/Utils.cc +++ b/dev/kernel/src/Utils.cc @@ -42,6 +42,16 @@ namespace Kernel return len; } + + Size wrt_string_len(const Utf16Char* str) + { + SizeT len{0}; + + while (str[len] != 0) + ++len; + + return len; + } Size rt_string_len(const Char* ptr) { diff --git a/dev/user/Opts.h b/dev/user/Opts.h new file mode 100644 index 00000000..09a77570 --- /dev/null +++ b/dev/user/Opts.h @@ -0,0 +1,12 @@ +/* ------------------------------------------- + + Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include <cstdarg> + +IMPORT_C VoidPtr sci_syscall_arg_1(SizeT id); +IMPORT_C VoidPtr sci_syscall_arg_2(SizeT id, VoidPtr arg1); +IMPORT_C VoidPtr sci_syscall_arg_3(SizeT id, VoidPtr arg1, VoidPtr arg3); +IMPORT_C VoidPtr sci_syscall_arg_4(SizeT id, VoidPtr arg1, VoidPtr arg3, VoidPtr arg4); diff --git a/dev/user/src/SystemCalls+IO.asm b/dev/user/src/SystemCalls+IO.asm index 58ea79e7..77e22b59 100644 --- a/dev/user/src/SystemCalls+IO.asm +++ b/dev/user/src/SystemCalls+IO.asm @@ -1,7 +1,7 @@ ;; /* ;; * ======================================================== ;; * -;; * libsci +;; * user/src/SystemCalls+IO.asm ;; * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ;; * ;; * ======================================================== diff --git a/dev/user/src/SystemCalls.cc b/dev/user/src/SystemCalls.cc index 7ad0fe6f..20eaa0e0 100644 --- a/dev/user/src/SystemCalls.cc +++ b/dev/user/src/SystemCalls.cc @@ -5,14 +5,10 @@ ------------------------------------------- */
#include <user/SystemCalls.h>
+#include <user/Opts.h>
/// @file SystemCalls.cc
-/// @brief Source file for the memory functions of the libsci.
-
-IMPORT_C VoidPtr sci_syscall_arg_1(SizeT id);
-IMPORT_C VoidPtr sci_syscall_arg_2(SizeT id, VoidPtr arg1);
-IMPORT_C VoidPtr sci_syscall_arg_3(SizeT id, VoidPtr arg1, VoidPtr arg3);
-IMPORT_C VoidPtr sci_syscall_arg_4(SizeT id, VoidPtr arg1, VoidPtr arg3, VoidPtr arg4);
+/// @brief Source file for the memory functions of the user.sys.
/// @brief Copy memory region.
IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len)
@@ -32,6 +28,7 @@ IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input Si return dest;
}
+/// @brief Get string length.
IMPORT_C SInt64 MmStrLen(const Char* in)
{
if (!in)
@@ -89,3 +86,28 @@ IMPORT_C UInt64 IoTellFile(_Input Ref desc) auto ret = (UInt64*)sci_syscall_arg_2(4, reinterpret_cast<VoidPtr>(desc));
return *ret;
}
+
+/// @brief Print to the file descriptor.
+/// @param desc the file descriptor.
+IMPORT_C SInt32 PrintOut(_Input IORef desc, const char* fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+
+ auto ret = (UInt64*)sci_syscall_arg_4(5, reinterpret_cast<VoidPtr>(desc),
+ reinterpret_cast<VoidPtr>(const_cast<Char*>(fmt)), args);
+
+ va_end(args);
+
+ return *ret;
+}
+
+IMPORT_C Void _rtl_assert(Bool expr, const Char* origin)
+{
+ if (!expr)
+ {
+ PrintOut(nullptr, "Assertion failed: %s\r", origin);
+ PrintOut(nullptr, "Origin: %s\r", origin);
+ }
+}
\ No newline at end of file diff --git a/dev/user/user.json b/dev/user/user.json index 9581d4d4..05b90abf 100644 --- a/dev/user/user.json +++ b/dev/user/user.json @@ -7,6 +7,7 @@ "compiler_flags": [ "-ffreestanding", "-shared", + "-fPIC", "-fno-rtti", "-fno-exceptions", "-Wl,--subsystem=17" diff --git a/tooling/copy_to_fat32.py b/tooling/copy_to_fat32.py new file mode 100755 index 00000000..ea0cb1b9 --- /dev/null +++ b/tooling/copy_to_fat32.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import os +import sys +import subprocess +import glob + +def copy_to_fat(image_path, source_dir): + if not os.path.isfile(image_path): + print(f"Error: FAT32 image {image_path} does not exist.") + sys.exit(1) + + if not os.path.isdir(source_dir): + print(f"Error: {source_dir} is not a valid directory.") + sys.exit(1) + + try: + files_to_copy = glob.glob(os.path.join(source_dir, "*")) + + # Now build the command + command = ["mcopy", "-spom", "-i", image_path] + files_to_copy + ["::"] + subprocess.run(command, check=True) + + print(f"Successfully copied contents of '{source_dir}' into '{image_path}'") + except FileNotFoundError: + print("Error: mcopy is not installed. Please install mtools.") + sys.exit(1) + except subprocess.CalledProcessError as e: + print(f"Error: mcopy failed with error code {e.returncode}.") + sys.exit(1) + except Exception as e: + print(f"mcopy failed: {e}") + sys.exit(1) + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("Usage: python3 copy_to_fat32.py <fat32_image> <source_directory>") + sys.exit(1) + + image_path = sys.argv[1] + source_dir = sys.argv[2] + + copy_to_fat(image_path, source_dir)
\ No newline at end of file |
