diff options
Diffstat (limited to 'kernel')
9 files changed, 72 insertions, 66 deletions
diff --git a/kernel/kernel/filesystems/fat32/boot_sector.hpp b/kernel/kernel/filesystems/fat32/boot_sector.hpp index b5789b92..7a100ac2 100644 --- a/kernel/kernel/filesystems/fat32/boot_sector.hpp +++ b/kernel/kernel/filesystems/fat32/boot_sector.hpp @@ -19,24 +19,24 @@ namespace kernel::filesystems::fat32 std::array<char, 8> oem_name; std::uint16_t bytes_per_sector; std::uint8_t sectors_per_cluster; - std::uint16_t reserved_sector_count; - std::uint8_t number_of_fats; - std::uint16_t root_entry_count; - std::uint16_t total_sectors_16; + std::uint16_t reserved_region_size_in_sectors; + std::uint8_t fats_count; + std::uint16_t root_entries_count_16; + std::uint16_t total_size_in_sectors_16; std::uint8_t media; - std::uint16_t fat_size_16; + std::uint16_t fat_size_in_sectors_16; std::uint16_t sectors_per_track; - std::uint16_t number_of_heads; - std::uint32_t hidden_sectors; - std::uint32_t total_sectors_32; + std::uint16_t heads_count; + std::uint32_t hidden_region_size_in_sectors; + std::uint32_t total_size_in_sectors_32; // FAT32 Extended BIOS Parameter Block - std::uint32_t fat_size_32; + std::uint32_t fat_size_in_sectors_32; std::uint16_t extended_flags; std::uint16_t filesystem_version; - std::uint32_t root_cluster; - std::uint16_t filesystem_info_sector; - std::uint16_t backup_boot_sector; + std::uint32_t root_directory_cluster_index; + std::uint16_t fs_info_offset_in_sectors; + std::uint16_t backup_boot_sector_offset_in_sectors; std::array<std::uint8_t, 12> reserved; // NOLINT(readability-magic-numbers) std::uint8_t drive_number; std::uint8_t reserved1; diff --git a/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp b/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp index c83cf315..215cc3be 100644 --- a/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp +++ b/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp @@ -126,7 +126,7 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 director auto driver_state = static_pointer_cast<kernel::filesystems::fat32::driver_state>(mount->second); REQUIRE(driver_state); - auto const root_cluster = driver_state->boot_sector.root_cluster; + auto const root_cluster = driver_state->boot_sector.root_directory_cluster_index; THEN("the volume label is skipped and only HELLO.TXT is found") { diff --git a/kernel/kernel/filesystems/fat32/file_allocation_table.cpp b/kernel/kernel/filesystems/fat32/file_allocation_table.cpp index 2390c22a..43fc4caa 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_start_sector = state.fat_start_sector + fat_index * state.boot_sector.fat_size_32; + auto const fat_start_sector = state.fat_start_sector + fat_index * state.boot_sector.fat_size_in_sectors_32; return fat_start_sector * state.sector_size + cluster * constants::fat_entry_size; } @@ -50,7 +50,7 @@ namespace kernel::filesystems::fat32 } auto const hints = std::array{state.free_cluster_count, state.next_free_cluster}; - auto const hints_offset = *state.fs_info_offset + kstd::bytes{offsetof(fs_info, free_cluster_count)}; + auto const hints_offset = *state.fs_info_offset + kstd::bytes{offsetof(fs_info, free_clusters_count)}; if (auto const write_result = state.backing_inode->write(kstd::raw_bytes(hints), hints_offset); !write_result) { return kstd::failure(write_result.error()); @@ -236,7 +236,7 @@ namespace kernel::filesystems::fat32 return kstd::failure(errc::invalid_cluster); } - for (auto fat_index = std::uint32_t{}; fat_index < state.boot_sector.number_of_fats; ++fat_index) + for (auto fat_index = std::uint32_t{}; fat_index < state.boot_sector.fats_count; ++fat_index) { auto const entry_offset = fat_entry_offset(cluster, fat_index, state); diff --git a/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp b/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp index e6f56728..ab765d49 100644 --- a/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp +++ b/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp @@ -53,8 +53,8 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 cluster THEN("the root directory occupies a single cluster") { - auto const chain = - kernel::filesystems::fat32::cluster_chain(driver_state->boot_sector.root_cluster, *driver_state); + auto const chain = kernel::filesystems::fat32::cluster_chain( + driver_state->boot_sector.root_directory_cluster_index, *driver_state); REQUIRE(chain); REQUIRE(*chain == kstd::vector<std::uint32_t>{2}); } @@ -86,7 +86,8 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 cluster auto entries = std::array<directory_entry, 3>{}; auto const buffer = std::as_writable_bytes(std::span{entries}); - auto const read_result = read_cluster_chain(driver_state->boot_sector.root_cluster, buffer, 0_B, *driver_state); + auto const read_result = + read_cluster_chain(driver_state->boot_sector.root_directory_cluster_index, buffer, 0_B, *driver_state); REQUIRE(read_result); REQUIRE(*read_result == kstd::bytes{buffer.size()}); diff --git a/kernel/kernel/filesystems/fat32/filesystem.cpp b/kernel/kernel/filesystems/fat32/filesystem.cpp index be104f20..175db9d0 100644 --- a/kernel/kernel/filesystems/fat32/filesystem.cpp +++ b/kernel/kernel/filesystems/fat32/filesystem.cpp @@ -54,22 +54,22 @@ namespace kernel::filesystems::fat32 kstd::println("[OS:FAT32] oem_name: '{}'", to_string_view(boot_sector.oem_name)); kstd::println("[OS:FAT32] bytes_per_sector: {}", boot_sector.bytes_per_sector); kstd::println("[OS:FAT32] sectors_per_cluster: {}", boot_sector.sectors_per_cluster); - kstd::println("[OS:FAT32] reserved_sector_count: {}", boot_sector.reserved_sector_count); - kstd::println("[OS:FAT32] number_of_fats: {}", boot_sector.number_of_fats); - kstd::println("[OS:FAT32] root_entry_count: {}", boot_sector.root_entry_count); - kstd::println("[OS:FAT32] total_sectors_16: {}", boot_sector.total_sectors_16); + kstd::println("[OS:FAT32] reserved_sector_count: {}", boot_sector.reserved_region_size_in_sectors); + kstd::println("[OS:FAT32] number_of_fats: {}", boot_sector.fats_count); + kstd::println("[OS:FAT32] root_entry_count: {}", boot_sector.root_entries_count_16); + kstd::println("[OS:FAT32] total_sectors_16: {}", boot_sector.total_size_in_sectors_16); kstd::println("[OS:FAT32] media: {:#04X}", boot_sector.media); - kstd::println("[OS:FAT32] fat_size_16: {}", boot_sector.fat_size_16); + kstd::println("[OS:FAT32] fat_size_16: {}", boot_sector.fat_size_in_sectors_16); kstd::println("[OS:FAT32] sectors_per_track: {}", boot_sector.sectors_per_track); - kstd::println("[OS:FAT32] number_of_heads: {}", boot_sector.number_of_heads); - kstd::println("[OS:FAT32] hidden_sectors: {}", boot_sector.hidden_sectors); - kstd::println("[OS:FAT32] total_sectors_32: {}", boot_sector.total_sectors_32); - kstd::println("[OS:FAT32] fat_size_32: {}", boot_sector.fat_size_32); + kstd::println("[OS:FAT32] number_of_heads: {}", boot_sector.heads_count); + kstd::println("[OS:FAT32] hidden_sectors: {}", boot_sector.hidden_region_size_in_sectors); + kstd::println("[OS:FAT32] total_sectors_32: {}", boot_sector.total_size_in_sectors_32); + kstd::println("[OS:FAT32] fat_size_32: {}", boot_sector.fat_size_in_sectors_32); kstd::println("[OS:FAT32] extended_flags: {:#06X}", boot_sector.extended_flags); kstd::println("[OS:FAT32] filesystem_version: {:#06X}", boot_sector.filesystem_version); - kstd::println("[OS:FAT32] root_cluster: {}", boot_sector.root_cluster); - kstd::println("[OS:FAT32] filesystem_info_sector: {}", boot_sector.filesystem_info_sector); - kstd::println("[OS:FAT32] backup_boot_sector: {}", boot_sector.backup_boot_sector); + kstd::println("[OS:FAT32] root_cluster: {}", boot_sector.root_directory_cluster_index); + kstd::println("[OS:FAT32] filesystem_info_sector: {}", boot_sector.fs_info_offset_in_sectors); + kstd::println("[OS:FAT32] backup_boot_sector: {}", boot_sector.backup_boot_sector_offset_in_sectors); kstd::println("[OS:FAT32] drive_number: {:#04X}", boot_sector.drive_number); kstd::println("[OS:FAT32] extended_boot_signature: {:#04X}", boot_sector.extended_boot_signature); kstd::println("[OS:FAT32] volume_id: {:#010X}", boot_sector.volume_id); @@ -103,14 +103,16 @@ namespace kernel::filesystems::fat32 //! @return true on valid boot-sector, else false auto validate_boot_sector(boot_sector const & bs) -> bool { - auto const fat_sectors = bs.fat_size_16 != 0 ? std::uint64_t{bs.fat_size_16} : bs.fat_size_32; - auto const total_sectors = bs.total_sectors_16 != 0 ? std::uint64_t{bs.total_sectors_16} : bs.total_sectors_32; + auto const fat_sectors = + bs.fat_size_in_sectors_16 != 0 ? std::uint64_t{bs.fat_size_in_sectors_16} : bs.fat_size_in_sectors_32; + auto const total_sectors = + bs.total_size_in_sectors_16 != 0 ? std::uint64_t{bs.total_size_in_sectors_16} : bs.total_size_in_sectors_32; - auto const fat_region_sectors = std::uint64_t{bs.number_of_fats} * fat_sectors; + auto const fat_region_sectors = std::uint64_t{bs.fats_count} * fat_sectors; return bs.bytes_per_sector >= constants::min_bytes_per_sector && - bs.bytes_per_sector <= constants::max_bytes_per_sector && bs.reserved_sector_count > 0 && - bs.number_of_fats > 0 && fat_region_sectors + bs.reserved_sector_count < total_sectors && + bs.bytes_per_sector <= constants::max_bytes_per_sector && bs.reserved_region_size_in_sectors > 0 && + bs.fats_count > 0 && fat_region_sectors + bs.reserved_region_size_in_sectors < total_sectors && std::has_single_bit(bs.bytes_per_sector) && std::has_single_bit(bs.sectors_per_cluster); } @@ -123,18 +125,20 @@ namespace kernel::filesystems::fat32 auto get_fat_type(boot_sector const & bs) -> fat_type { // is zero for FAT32 because root directory is stored in data-sector - auto const root_dir_size_fat16 = std::uint64_t{bs.root_entry_count} * 32; // todo maybe constant + auto const root_dir_size_fat16 = std::uint64_t{bs.root_entries_count_16} * 32; // todo maybe constant // ceiling divide auto const root_dir_sectors_fat16 = (root_dir_size_fat16 + bs.bytes_per_sector - 1) / bs.bytes_per_sector; - auto const fat_sectors = bs.fat_size_16 != 0 ? std::uint64_t{bs.fat_size_16} : bs.fat_size_32; - auto const total_sectors = bs.total_sectors_16 != 0 ? std::uint64_t{bs.total_sectors_16} : bs.total_sectors_32; + auto const fat_sectors = + bs.fat_size_in_sectors_16 != 0 ? std::uint64_t{bs.fat_size_in_sectors_16} : bs.fat_size_in_sectors_32; + auto const total_sectors = + bs.total_size_in_sectors_16 != 0 ? std::uint64_t{bs.total_size_in_sectors_16} : bs.total_size_in_sectors_32; - auto const fat_region_sectors = bs.number_of_fats * fat_sectors; + auto const fat_region_sectors = bs.fats_count * fat_sectors; auto const data_region_sectors = - total_sectors - (bs.reserved_sector_count + fat_region_sectors + root_dir_sectors_fat16); + total_sectors - (bs.reserved_region_size_in_sectors + fat_region_sectors + root_dir_sectors_fat16); auto const data_region_clusters = data_region_sectors / bs.sectors_per_cluster; @@ -188,15 +192,16 @@ namespace kernel::filesystems::fat32 auto const bytes_per_sector = boot_sector.bytes_per_sector; auto const sectors_per_cluster = boot_sector.sectors_per_cluster; - auto const total_sectors = boot_sector.total_sectors_32 != 0 ? std::uint64_t{boot_sector.total_sectors_32} - : std::uint64_t{boot_sector.total_sectors_16}; - auto const fat_sectors = std::uint64_t{boot_sector.number_of_fats} * boot_sector.fat_size_32; - auto const data_start_sector = boot_sector.reserved_sector_count + fat_sectors; + auto const total_sectors = boot_sector.total_size_in_sectors_32 != 0 + ? std::uint64_t{boot_sector.total_size_in_sectors_32} + : std::uint64_t{boot_sector.total_size_in_sectors_16}; + auto const fat_sectors = std::uint64_t{boot_sector.fats_count} * boot_sector.fat_size_in_sectors_32; + auto const data_start_sector = boot_sector.reserved_region_size_in_sectors + fat_sectors; auto const cluster_count = (total_sectors - data_start_sector) / sectors_per_cluster; state.sector_size = kstd::bytes{bytes_per_sector}; state.cluster_size = state.sector_size * sectors_per_cluster; - state.fat_start_sector = boot_sector.reserved_sector_count; + state.fat_start_sector = boot_sector.reserved_region_size_in_sectors; state.data_start_sector = static_cast<std::uint32_t>(data_start_sector); state.cluster_count = static_cast<std::uint32_t>(cluster_count); } @@ -209,8 +214,8 @@ namespace kernel::filesystems::fat32 //! @return Nothing on success, an error if the sector could not be read. [[nodiscard]] auto read_fs_info(driver_state & state) -> kstd::result<void> { - auto const fs_info_sector = state.boot_sector.filesystem_info_sector; - if (fs_info_sector == 0 || fs_info_sector >= state.boot_sector.reserved_sector_count) + auto const fs_info_sector = state.boot_sector.fs_info_offset_in_sectors; + if (fs_info_sector == 0 || fs_info_sector >= state.boot_sector.reserved_region_size_in_sectors) { return {}; } @@ -229,7 +234,7 @@ namespace kernel::filesystems::fat32 state.fs_info_offset = fs_info_offset; state.free_cluster_count = - fs_info.free_cluster_count <= state.cluster_count ? fs_info.free_cluster_count : constants::fs_info_unknown; + fs_info.free_clusters_count <= state.cluster_count ? fs_info.free_clusters_count : constants::fs_info_unknown; state.next_free_cluster = fs_info.next_free_cluster; return {}; } @@ -272,7 +277,7 @@ namespace kernel::filesystems::fat32 return kstd::failure(fs_info_result.error()); } - auto const root_cluster = std::uint32_t{driver_state->boot_sector.root_cluster}; + auto const root_cluster = std::uint32_t{driver_state->boot_sector.root_directory_cluster_index}; if (!is_valid_cluster(root_cluster, *driver_state)) { return kstd::failure(errc::invalid_cluster); @@ -317,8 +322,8 @@ namespace kernel::filesystems::fat32 // A ".." entry pointing to the root directory stores cluster 0 instead of the root cluster. if (it->entry.first_cluster() == 0 && (it->entry.attributes & constants::attribute_directory) != 0) { - return kstd::make_shared<inode>(std::uint32_t{state->boot_sector.root_cluster}, constants::attribute_directory, - kstd::bytes{}, constants::root_inode_number); + return kstd::make_shared<inode>(std::uint32_t{state->boot_sector.root_directory_cluster_index}, + constants::attribute_directory, kstd::bytes{}, constants::root_inode_number); } return kstd::make_shared<inode>(*it); diff --git a/kernel/kernel/filesystems/fat32/filesystem.tests.cpp b/kernel/kernel/filesystems/fat32/filesystem.tests.cpp index a727b921..589c57cc 100644 --- a/kernel/kernel/filesystems/fat32/filesystem.tests.cpp +++ b/kernel/kernel/filesystems/fat32/filesystem.tests.cpp @@ -114,14 +114,14 @@ SCENARIO("Fat32 filesystem mount validates the boot sector", "[filesystem][fat32 THEN("a missing file allocation table is rejected") { - boot_sector.number_of_fats = 0; + boot_sector.fats_count = 0; kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, boot_sector); REQUIRE(fs->mount(dev_inode).error() == kernel::filesystems::fat32::errc::invalid_boot_sector); } THEN("a data region that does not fit into the volume is rejected") { - boot_sector.total_sectors_32 = 2; + boot_sector.total_size_in_sectors_32 = 2; kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, boot_sector); REQUIRE(fs->mount(dev_inode).error() == kernel::filesystems::fat32::errc::invalid_boot_sector); } diff --git a/kernel/kernel/filesystems/fat32/fs_info.hpp b/kernel/kernel/filesystems/fat32/fs_info.hpp index 754f06b1..95fbc30a 100644 --- a/kernel/kernel/filesystems/fat32/fs_info.hpp +++ b/kernel/kernel/filesystems/fat32/fs_info.hpp @@ -36,7 +36,7 @@ namespace kernel::filesystems::fat32 std::uint32_t lead_signature; std::array<std::uint8_t, 480> reserved; // NOLINT(readability-magic-numbers) std::uint32_t struct_signature; - std::uint32_t free_cluster_count; + std::uint32_t free_clusters_count; std::uint32_t next_free_cluster; std::array<std::uint8_t, 12> reserved1; // NOLINT(readability-magic-numbers) std::uint32_t trail_signature; diff --git a/kernel/kernel/filesystems/fat32/inode.tests.cpp b/kernel/kernel/filesystems/fat32/inode.tests.cpp index 2f5822a0..be84b3d7 100644 --- a/kernel/kernel/filesystems/fat32/inode.tests.cpp +++ b/kernel/kernel/filesystems/fat32/inode.tests.cpp @@ -846,8 +846,8 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 inode wr REQUIRE(second); REQUIRE(*second); - auto const fat_size = state->boot_sector.fat_size_32 * state->sector_size; - for (auto fat_index = 0uz; fat_index < state->boot_sector.number_of_fats; ++fat_index) + auto const fat_size = state->boot_sector.fat_size_in_sectors_32 * state->sector_size; + for (auto fat_index = 0uz; fat_index < state->boot_sector.fats_count; ++fat_index) { auto raw_entry = std::uint32_t{}; auto const entry_offset = state->fat_start_sector * state->sector_size + fat_index * fat_size + @@ -862,7 +862,7 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 inode wr auto fs_info = kernel::filesystems::fat32::fs_info{}; REQUIRE(dev_inode->read(kstd::raw_bytes(fs_info), *state->fs_info_offset)); REQUIRE(fs_info.is_valid()); - REQUIRE(fs_info.free_cluster_count == free_clusters_before - 1); + REQUIRE(fs_info.free_clusters_count == free_clusters_before - 1); REQUIRE(fs_info.next_free_cluster == *next_cluster(file->first_cluster(), *state).value()); } } diff --git a/kernel/kernel/test_support/filesystems/fat32.cpp b/kernel/kernel/test_support/filesystems/fat32.cpp index c320eb3e..03abed15 100644 --- a/kernel/kernel/test_support/filesystems/fat32.cpp +++ b/kernel/kernel/test_support/filesystems/fat32.cpp @@ -23,7 +23,7 @@ namespace kernel::tests::filesystems::fat32 namespace { constexpr auto sector_size = 512_B; - constexpr std::uint16_t reserved_sector_count = 1; + constexpr std::uint16_t reserved_region_size_in_sectors = 1; constexpr std::uint32_t fat_sectors = 1; constexpr std::uint32_t data_sectors = 69632; @@ -45,11 +45,11 @@ namespace kernel::tests::filesystems::fat32 auto boot_sector = kernel::filesystems::fat32::boot_sector{}; boot_sector.bytes_per_sector = static_cast<std::uint16_t>(sector_size.value); boot_sector.sectors_per_cluster = 1; - boot_sector.reserved_sector_count = reserved_sector_count; - boot_sector.number_of_fats = 1; - boot_sector.total_sectors_32 = reserved_sector_count + fat_sectors + data_sectors; - boot_sector.fat_size_32 = fat_sectors; - boot_sector.root_cluster = kernel::filesystems::fat32::constants::first_data_cluster; + boot_sector.reserved_region_size_in_sectors = reserved_region_size_in_sectors; + boot_sector.fats_count = 1; + boot_sector.total_size_in_sectors_32 = reserved_region_size_in_sectors + fat_sectors + data_sectors; + boot_sector.fat_size_in_sectors_32 = fat_sectors; + boot_sector.root_directory_cluster_index = kernel::filesystems::fat32::constants::first_data_cluster; boot_sector.boot_signature = kernel::filesystems::fat32::constants::boot_signature; return boot_sector; } @@ -74,14 +74,14 @@ namespace kernel::tests::filesystems::fat32 -> void { auto const offset = - reserved_sector_count * sector_size + cluster * kernel::filesystems::fat32::constants::fat_entry_size; + reserved_region_size_in_sectors * sector_size + cluster * kernel::filesystems::fat32::constants::fat_entry_size; write_bytes(device, offset, &value, kstd::size_of(value)); } auto write_cluster_data(kernel::tests::devices::block_device & device, std::uint32_t cluster, std::span<std::byte const> data) -> void { - auto const data_start = (reserved_sector_count + fat_sectors) * sector_size; + auto const data_start = (reserved_region_size_in_sectors + fat_sectors) * sector_size; auto const offset = data_start + (cluster - kernel::filesystems::fat32::constants::first_data_cluster) * sector_size; write_bytes(device, offset, data.data(), kstd::bytes{data.size()}); |
