aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorbbuerge <b.buerge@proton.me>2026-10-05 12:47:32 +0000
committerbbuerge <b.buerge@proton.me>2026-10-05 12:47:32 +0000
commit007696a98d84ae74ac95dc66c23c8770d6bff065 (patch)
treed84a54fbc0e58194c028ba1071c7b701dedfe836 /kernel
parent4f634ec5bf412954eff23e8410ae8f21264b6793 (diff)
downloadkernel-007696a98d84ae74ac95dc66c23c8770d6bff065.tar.xz
kernel-007696a98d84ae74ac95dc66c23c8770d6bff065.zip
added fat32 img, wip parse bootsector
Diffstat (limited to 'kernel')
-rw-r--r--kernel/CMakeLists.txt1
-rw-r--r--kernel/kernel/filesystems/fat32/boot_sector.hpp18
-rw-r--r--kernel/kernel/filesystems/fat32/driver_state.hpp23
-rw-r--r--kernel/kernel/filesystems/fat32/error.cpp58
-rw-r--r--kernel/kernel/filesystems/fat32/error.hpp33
-rw-r--r--kernel/kernel/filesystems/fat32/filesystem.cpp29
-rw-r--r--kernel/kernel/filesystems/fat32/filesystem.hpp9
-rw-r--r--kernel/kernel/main.cpp17
8 files changed, 185 insertions, 3 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt
index e7464658..aacb5d97 100644
--- a/kernel/CMakeLists.txt
+++ b/kernel/CMakeLists.txt
@@ -86,6 +86,7 @@ target_sources("kernel_lib" PRIVATE
"kernel/filesystems/ext2/write_batch.cpp"
# fat32 Filesystem
+ "kernel/filesystems/fat32/error.cpp"
"kernel/filesystems/fat32/filesystem.cpp"
"kernel/filesystems/fat32/inode.cpp"
"kernel/filesystems/fat32/module.cpp"
diff --git a/kernel/kernel/filesystems/fat32/boot_sector.hpp b/kernel/kernel/filesystems/fat32/boot_sector.hpp
new file mode 100644
index 00000000..c66e2fc5
--- /dev/null
+++ b/kernel/kernel/filesystems/fat32/boot_sector.hpp
@@ -0,0 +1,18 @@
+#ifndef TEACHOS_KERNEL_FILESYSTEMS_FAT32_BOOT_SECTOR_HPP
+#define TEACHOS_KERNEL_FILESYSTEMS_FAT32_BOOT_SECTOR_HPP
+
+#include <kstd/bitfield_enum.hpp>
+
+#include <cstdint>
+
+namespace kernel::filesystems::fat32
+{
+ //! The boot-sector in the fat32 filesystem.
+ struct boot_sector // todo gnu::packed?
+ {
+ std::uint16_t bytes_per_sector;
+ std::uint16_t boot_signature;
+ };
+} // namespace kernel::filesystems::fat32
+
+#endif \ No newline at end of file
diff --git a/kernel/kernel/filesystems/fat32/driver_state.hpp b/kernel/kernel/filesystems/fat32/driver_state.hpp
new file mode 100644
index 00000000..55b90724
--- /dev/null
+++ b/kernel/kernel/filesystems/fat32/driver_state.hpp
@@ -0,0 +1,23 @@
+#ifndef TEACHOS_KERNEL_FILESYSTEMS_FAT32_DRIVER_STATE_HPP
+#define TEACHOS_KERNEL_FILESYSTEMS_FAT32_DRIVER_STATE_HPP
+
+#include <kernel/filesystems/fat32/boot_sector.hpp>
+#include <kernel/vfs/driver_state.hpp>
+#include <kernel/vfs/inode.hpp>
+
+#include <kapi/tracked_mutex.hpp>
+
+#include <kstd/memory.hpp>
+#include <kstd/vector.hpp>
+
+namespace kernel::filesystems::fat32
+{
+
+ struct driver_state : vfs::driver_state
+ {
+ struct boot_sector boot_sector{};
+ };
+
+} // namespace kernel::filesystems::fat32
+
+#endif \ No newline at end of file
diff --git a/kernel/kernel/filesystems/fat32/error.cpp b/kernel/kernel/filesystems/fat32/error.cpp
new file mode 100644
index 00000000..4a18eb9e
--- /dev/null
+++ b/kernel/kernel/filesystems/fat32/error.cpp
@@ -0,0 +1,58 @@
+#include <kernel/filesystems/fat32/error.hpp>
+
+#include <kernel/vfs/error.hpp>
+
+#include <kstd/system_error.hpp>
+
+#include <string_view>
+
+namespace kernel::filesystems::fat32
+{
+
+ namespace
+ {
+ struct category_t final : kstd::error_category
+ {
+ [[nodiscard]] constexpr auto name() const noexcept -> std::string_view override
+ {
+ return "fat32";
+ }
+
+ [[nodiscard]] constexpr auto message(int value) const noexcept -> std::string_view override
+ {
+ switch (static_cast<errc>(value))
+ {
+ case errc::invalid_boot_signature:
+ return "invalid boot signature";
+ default:
+ return "unknown fat32 error";
+ };
+ }
+
+ [[nodiscard]] constexpr auto equivalent(int code, kstd::error_condition const & condition) const noexcept
+ -> bool override
+ {
+ switch (static_cast<errc>(code))
+ {
+ case errc::invalid_boot_signature:
+ if (condition.category() == kernel::vfs::category())
+ {
+ return condition.value() == static_cast<int>(kernel::vfs::errc::invalid_filesystem);
+ }
+ else if (condition.category() == kstd::generic_category())
+ {
+ return condition.value() == static_cast<int>(kstd::errc::invalid_argument);
+ }
+ break;
+ }
+ return kstd::error_category::equivalent(code, condition);
+ }
+ } constexpr inline fat32_category_instance{};
+ } // namespace
+
+ [[nodiscard]] auto category() noexcept -> kstd::error_category const &
+ {
+ return fat32_category_instance;
+ }
+
+} // namespace kernel::filesystems::fat32
diff --git a/kernel/kernel/filesystems/fat32/error.hpp b/kernel/kernel/filesystems/fat32/error.hpp
new file mode 100644
index 00000000..6955be3f
--- /dev/null
+++ b/kernel/kernel/filesystems/fat32/error.hpp
@@ -0,0 +1,33 @@
+#ifndef TEACHOS_KERNEL_FILESYSTEMS_FAT32_ERROR_HPP
+#define TEACHOS_KERNEL_FILESYSTEMS_FAT32_ERROR_HPP
+
+#include <kernel/vfs/error.hpp>
+
+#include <kstd/system_error.hpp>
+
+#include <type_traits>
+
+namespace kernel::filesystems::fat32
+{
+ enum struct errc : int
+ {
+ invalid_boot_signature = 1
+ };
+
+ [[nodiscard]] auto category() noexcept -> kstd::error_category const &;
+
+ [[nodiscard]] constexpr auto inline make_error_code(errc error) noexcept -> kstd::error_code
+ {
+ return {static_cast<int>(error), category()};
+ }
+} // namespace kernel::filesystems::fat32
+
+namespace kstd
+{
+ template<>
+ struct is_error_code_enum<kernel::filesystems::fat32::errc> : std::true_type
+ {
+ };
+} // namespace kstd
+
+#endif \ No newline at end of file
diff --git a/kernel/kernel/filesystems/fat32/filesystem.cpp b/kernel/kernel/filesystems/fat32/filesystem.cpp
index a66a9b25..4899f0bd 100644
--- a/kernel/kernel/filesystems/fat32/filesystem.cpp
+++ b/kernel/kernel/filesystems/fat32/filesystem.cpp
@@ -1,5 +1,7 @@
#include <kernel/filesystems/fat32/filesystem.hpp>
+#include <kernel/filesystems/fat32/boot_sector.hpp>
+#include <kernel/filesystems/fat32/error.hpp>
#include <kernel/filesystems/fat32/inode.hpp>
#include <kernel/vfs/error.hpp>
#include <kernel/vfs/filesystem.hpp>
@@ -7,8 +9,11 @@
#include <kapi/filesystem.hpp>
#include <kstd/memory.hpp>
+#include <kstd/print.hpp>
#include <kstd/result.hpp>
+#include <kstd/span.hpp>
#include <kstd/system_error.hpp>
+#include <kstd/units.hpp>
#include <cstdint>
#include <optional>
@@ -20,11 +25,31 @@ namespace kernel::filesystems::fat32
auto filesystem::probe(inode_ptr const & inode) const -> kstd::result<std::uint32_t>
{
- if (inode)
+ auto boot_sector = fat32::boot_sector{};
+ auto const boot_signature_bytes = kstd::raw_bytes(boot_sector.boot_signature);
+ auto boot_signature_result = inode->read(boot_signature_bytes, kstd::bytes{constants::boot_signature_offset});
+
+ if (!boot_signature_result)
{
- return kstd::failure(vfs::errc::invalid_argument);
+ return kstd::failure(boot_signature_result.error());
}
+ if (boot_sector.boot_signature != constants::boot_signature)
+ {
+ return kstd::failure(errc::invalid_boot_signature);
+ }
+
+ auto const bytes_per_sector_bytes = kstd::raw_bytes(boot_sector.bytes_per_sector);
+ auto bytes_per_sector_result = inode->read(bytes_per_sector_bytes, kstd::bytes{constants::bytes_per_sector_offset});
+
+ if (!bytes_per_sector_result)
+ {
+ return kstd::failure(bytes_per_sector_result.error());
+ }
+
+ kstd::println("[OS:FAT32] bytes_per_sector: {}", boot_sector.bytes_per_sector);
+ kstd::println("[OS:FAT32] boot_signature: {:#06X}", boot_sector.boot_signature);
+
return 0;
}
diff --git a/kernel/kernel/filesystems/fat32/filesystem.hpp b/kernel/kernel/filesystems/fat32/filesystem.hpp
index 8b0b4941..9c516ca9 100644
--- a/kernel/kernel/filesystems/fat32/filesystem.hpp
+++ b/kernel/kernel/filesystems/fat32/filesystem.hpp
@@ -10,6 +10,7 @@
#include <kstd/result.hpp>
#include <kstd/string.hpp>
#include <kstd/system_error.hpp>
+#include <kstd/units.hpp>
#include <kstd/vector.hpp>
#include <cstdint>
@@ -18,6 +19,14 @@
namespace kernel::filesystems::fat32
{
+ //! Constants related to the fat32 filesystem.
+ namespace constants
+ {
+ constexpr kstd::bytes inline bytes_per_sector_offset = kstd::bytes{0x0B};
+ constexpr kstd::bytes inline boot_signature_offset = kstd::bytes{0x1FE};
+ constexpr std::uint16_t inline boot_signature = 0xAA55;
+ } // namespace constants
+
//! The File Allocation Table 32 Filesystem (fat32)
struct filesystem : kernel::vfs::filesystem
{
diff --git a/kernel/kernel/main.cpp b/kernel/kernel/main.cpp
index bc192950..15952547 100644
--- a/kernel/kernel/main.cpp
+++ b/kernel/kernel/main.cpp
@@ -25,6 +25,21 @@
using namespace kstd::units_literals;
+auto run_demo_fat32() -> void
+{
+ kstd::println("[OS:DEM] Running FAT32 Demo");
+
+ kstd::println("[OS:DEM] mount /dev/ram3 to /entrance");
+ if (auto result = kapi::filesystem::mount("/dev/ram3", "/entrance"))
+ {
+ kstd::println("[OS:DEM] mounted /dev/ram3 to /entrance");
+ }
+ else
+ {
+ kapi::system::panic("[OS:DEM] failed to mount /dev/ram3 on /entrance", result.error());
+ }
+}
+
auto run_demo() -> void
{
// 1) open a file
@@ -312,7 +327,7 @@ auto main() -> int
kstd::println("[OS] Virtual filesystem initialized.");
// TODO BA-FS26 remove demo code?
- run_demo();
+ run_demo_fat32();
kapi::system::panic("[OS] Returning from kernel main!");
}