From 179ca598e91a7adf9c0a915e97b20864b35b24c4 Mon Sep 17 00:00:00 2001 From: bbuerge Date: Sun, 15 Nov 2026 12:25:50 +0000 Subject: kernel/filesystems: cleanup boot-sector --- kernel/kernel/filesystems/fat32/boot_sector.cpp | 38 ++++++-------- kernel/kernel/filesystems/fat32/boot_sector.hpp | 58 +++++++++++----------- .../filesystems/fat32/file_allocation_table.cpp | 2 +- .../filesystems/fat32/file_allocation_table.hpp | 3 +- kernel/kernel/filesystems/fat32/filesystem.hpp | 18 ------- 5 files changed, 49 insertions(+), 70 deletions(-) diff --git a/kernel/kernel/filesystems/fat32/boot_sector.cpp b/kernel/kernel/filesystems/fat32/boot_sector.cpp index 5937ba6f..91733fdf 100644 --- a/kernel/kernel/filesystems/fat32/boot_sector.cpp +++ b/kernel/kernel/filesystems/fat32/boot_sector.cpp @@ -1,15 +1,16 @@ #include +#include #include -#include -#include -#include #include #include #include #include +#include + +using namespace kstd::units_literals; namespace kernel::filesystems::fat32 { @@ -17,34 +18,27 @@ namespace kernel::filesystems::fat32 : m_data(data) {} - auto boot_sector::is_fat32() const -> bool + auto boot_sector::cluster_count() const -> std::uint64_t { // is zero for FAT32 because root directory is stored in data-sector - auto const root_dir_size_fat16 = root_entries_count_16() * constants::fat_entry_size; // todo maybe constant + auto const root_dir_size_fat16 = root_entries_count_16() * constants::directory_entry_size; // ceiling divide - auto const root_dir_sectors_fat16 = (root_dir_size_fat16 + kstd::bytes{bytes_per_sector()} - kstd::bytes{1}) / - kstd::bytes{bytes_per_sector()}; // todo - - auto const fat_region_sectors = fats_count() * fat_size_in_sectors(); + auto const root_dir_sectors_fat16 = (root_dir_size_fat16 + sector_size() - 1_B) / sector_size(); - auto const data_region_sectors = - total_size_in_sectors() - (reserved_region_size_in_sectors() + fat_region_sectors + root_dir_sectors_fat16); + auto const data_region_sectors = total_size_in_sectors() - (reserved_region_size_in_sectors() + + fat_region_size_in_sectors() + root_dir_sectors_fat16); - auto const data_region_clusters = data_region_sectors / sectors_per_cluster(); - - return data_region_clusters > constants::max_clusters_fat16; + return data_region_sectors / sectors_per_cluster(); } auto boot_sector::validate() const -> kstd::result { - auto const fat_region_sectors = fats_count() * fat_size_in_sectors(); - - auto is_valid = bytes_per_sector() >= constants::min_bytes_per_sector && - bytes_per_sector() <= constants::max_bytes_per_sector && reserved_region_size_in_sectors() > 0 && + auto is_valid = sector_size() >= constants::min_bytes_per_sector && + sector_size() <= constants::max_bytes_per_sector && reserved_region_size_in_sectors() > 0 && fats_count() > 0 && - fat_region_sectors + reserved_region_size_in_sectors() < total_size_in_sectors() && - std::has_single_bit(bytes_per_sector()) && std::has_single_bit(sectors_per_cluster()); + fat_region_size_in_sectors() + reserved_region_size_in_sectors() < total_size_in_sectors() && + std::has_single_bit(sector_size().value) && std::has_single_bit(sectors_per_cluster()); if (boot_signature() != constants::boot_signature) { @@ -55,7 +49,7 @@ namespace kernel::filesystems::fat32 return kstd::failure(errc::invalid_boot_sector); } - if (!is_fat32()) + if (cluster_count() < constants::min_clusters_fat32) { return kstd::failure(errc::unsupported_fat_fs); } @@ -63,7 +57,7 @@ namespace kernel::filesystems::fat32 return kstd::success(); } - auto boot_sector::create(kernel::vfs::filesystem::inode_ptr const & backing_inode) -> kstd::result + auto boot_sector::create(inode_ptr const & backing_inode) -> kstd::result { auto data = fat32::boot_sector_data{}; diff --git a/kernel/kernel/filesystems/fat32/boot_sector.hpp b/kernel/kernel/filesystems/fat32/boot_sector.hpp index 3f0cb398..1753f3e9 100644 --- a/kernel/kernel/filesystems/fat32/boot_sector.hpp +++ b/kernel/kernel/filesystems/fat32/boot_sector.hpp @@ -11,12 +11,17 @@ namespace kernel::filesystems::fat32 { - //! The boot sector in the fat32 filesystem. - //! - //! The layout follows the "Microsoft Extensible Firmware Initiative FAT32 File System Specification" (fatgen103). - //! The sector consists of the BIOS Parameter Block (BPB) common to all FAT variants, followed by the FAT32 specific - //! extension. - // cspell:ignore fatgen + namespace constants + { + using namespace kstd::units_literals; + + constexpr kstd::bytes inline boot_sector_offset = 0_B; + constexpr std::uint16_t inline boot_signature = 0xAA55; + constexpr kstd::bytes inline min_bytes_per_sector = 512_B; + constexpr kstd::bytes inline max_bytes_per_sector = 4096_B; + constexpr std::uint16_t inline min_clusters_fat32 = 65525; + } // namespace constants + struct [[gnu::packed]] boot_sector_data { // Common BIOS Parameter Block @@ -52,21 +57,26 @@ namespace kernel::filesystems::fat32 std::array boot_code; // NOLINT(readability-magic-numbers) std::uint16_t boot_signature; }; + static_assert(sizeof(boot_sector_data) == 512, "The fat32 boot sector must be exactly 512 bytes in size."); struct boot_sector { + using inode_ptr = kernel::vfs::filesystem::inode_ptr; + explicit boot_sector(boot_sector_data const & data); - auto static create(kernel::vfs::filesystem::inode_ptr const & backing_inode) -> kstd::result; + auto static create(inode_ptr const & backing_inode) -> kstd::result; + + [[nodiscard]] auto cluster_count() const -> std::uint64_t; [[nodiscard]] auto root_directory_cluster_index() const -> std::uint64_t { return m_data.root_directory_cluster_index; } - [[nodiscard]] auto fat_size() const -> std::uint64_t + [[nodiscard]] auto fat_size() const -> kstd::bytes { - return fat_size_in_sectors() * bytes_per_sector(); + return fat_size_in_sectors() * sector_size(); } [[nodiscard]] auto fats_count() const -> std::uint64_t @@ -76,39 +86,28 @@ namespace kernel::filesystems::fat32 [[nodiscard]] auto fs_info_offset() const -> kstd::bytes { - return kstd::bytes{m_data.fs_info_offset_in_sectors} * bytes_per_sector(); + return m_data.fs_info_offset_in_sectors * sector_size(); } [[nodiscard]] auto fat1_offset() const -> kstd::bytes { - return kstd::bytes{reserved_region_size_in_sectors()} * bytes_per_sector(); + return reserved_region_size_in_sectors() * sector_size(); } [[nodiscard]] auto cluster_size() const -> kstd::bytes { - return kstd::bytes{bytes_per_sector() * sectors_per_cluster()}; - } - - [[nodiscard]] auto cluster_count() const -> std::uint64_t - { - auto const fat_region_size_in_sectors = fats_count() * fat_size_in_sectors(); - auto const data_region_size_in_sectors = - total_size_in_sectors() - (reserved_region_size_in_sectors() + fat_region_size_in_sectors); - - return data_region_size_in_sectors / sectors_per_cluster(); + return sector_size() * sectors_per_cluster(); } [[nodiscard]] auto data_region_offset() const -> kstd::bytes { - auto const fat_region_size_in_sectors = fats_count() * fat_size_in_sectors(); - return kstd::bytes{(fat_region_size_in_sectors + reserved_region_size_in_sectors()) * bytes_per_sector()}; + return (fat_region_size_in_sectors() + reserved_region_size_in_sectors()) * sector_size(); } private: boot_sector_data m_data{}; [[nodiscard]] auto validate() const -> kstd::result; - [[nodiscard]] auto is_fat32() const -> bool; [[nodiscard]] auto fat_size_in_sectors() const noexcept -> std::uint64_t // todo rename { @@ -120,9 +119,9 @@ namespace kernel::filesystems::fat32 return m_data.total_size_in_sectors_16 != 0 ? m_data.total_size_in_sectors_16 : m_data.total_size_in_sectors_32; } - [[nodiscard]] auto bytes_per_sector() const -> std::uint64_t + [[nodiscard]] auto sector_size() const -> kstd::bytes { - return m_data.bytes_per_sector; + return kstd::bytes{m_data.bytes_per_sector}; } [[nodiscard]] auto sectors_per_cluster() const -> std::uint64_t @@ -135,6 +134,11 @@ namespace kernel::filesystems::fat32 return m_data.reserved_region_size_in_sectors; } + [[nodiscard]] auto fat_region_size_in_sectors() const -> std::uint64_t + { + return fats_count() * fat_size_in_sectors(); + } + [[nodiscard]] auto root_entries_count_16() const -> std::uint64_t { return m_data.root_entries_count_16; @@ -145,8 +149,6 @@ namespace kernel::filesystems::fat32 return m_data.boot_signature; } }; - - static_assert(sizeof(boot_sector_data) == 512, "The fat32 boot sector must be exactly 512 bytes in size."); } // namespace kernel::filesystems::fat32 #endif diff --git a/kernel/kernel/filesystems/fat32/file_allocation_table.cpp b/kernel/kernel/filesystems/fat32/file_allocation_table.cpp index 09648dc7..d3d4cf14 100644 --- a/kernel/kernel/filesystems/fat32/file_allocation_table.cpp +++ b/kernel/kernel/filesystems/fat32/file_allocation_table.cpp @@ -34,7 +34,7 @@ namespace kernel::filesystems::fat32 [[nodiscard]] auto fat_entry_offset(std::uint32_t cluster, std::uint32_t fat_index, driver_state const & state) -> kstd::bytes { - auto const fat_offset = state.boot_sector.fat1_offset() + (kstd::bytes{fat_index} * state.boot_sector.fat_size()); + auto const fat_offset = state.boot_sector.fat1_offset() + (fat_index * state.boot_sector.fat_size()); return fat_offset + cluster * constants::fat_entry_size; } diff --git a/kernel/kernel/filesystems/fat32/file_allocation_table.hpp b/kernel/kernel/filesystems/fat32/file_allocation_table.hpp index 43cf2de5..048939bf 100644 --- a/kernel/kernel/filesystems/fat32/file_allocation_table.hpp +++ b/kernel/kernel/filesystems/fat32/file_allocation_table.hpp @@ -14,6 +14,7 @@ namespace kernel::filesystems::fat32 { + using namespace kstd::units_literals; namespace constants { constexpr std::uint32_t inline first_data_cluster = 2; @@ -22,7 +23,7 @@ namespace kernel::filesystems::fat32 constexpr std::uint32_t inline reserved_cluster = 0x0000'0001; constexpr std::uint32_t inline bad_cluster = 0x0FFF'FFF7; constexpr std::uint32_t inline end_of_chain = 0x0FFF'FFF8; - constexpr kstd::bytes inline fat_entry_size = kstd::bytes{sizeof(std::uint32_t)}; + constexpr kstd::bytes inline fat_entry_size = 4_B; } // namespace constants enum struct fat_entry_type : std::uint8_t diff --git a/kernel/kernel/filesystems/fat32/filesystem.hpp b/kernel/kernel/filesystems/fat32/filesystem.hpp index ffb4e3c3..69f3ade5 100644 --- a/kernel/kernel/filesystems/fat32/filesystem.hpp +++ b/kernel/kernel/filesystems/fat32/filesystem.hpp @@ -2,16 +2,10 @@ #define TEACHOS_KERNEL_FILESYSTEMS_FAT32_FILESYSTEM_HPP #include -#include #include -#include #include -#include -#include -#include -#include #include #include @@ -19,18 +13,6 @@ namespace kernel::filesystems::fat32 { - //! Constants related to the fat32 filesystem. - namespace constants - { - constexpr kstd::bytes inline boot_sector_offset = kstd::bytes{0}; - constexpr std::uint16_t inline boot_signature = 0xAA55; - constexpr std::uint16_t inline min_bytes_per_sector = 512; - constexpr std::uint16_t inline max_bytes_per_sector = 4096; - - constexpr std::uint16_t inline max_clusters_fat12 = 4084; - constexpr std::uint16_t inline max_clusters_fat16 = 65524; - } // namespace constants - //! The File Allocation Table 32 Filesystem (fat32) struct filesystem : kernel::vfs::filesystem { -- cgit v1.2.3