Serenity Operating System
1/*
2 * Copyright (c) 2022, Undefine <undefine@undefine.pl>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/OwnPtr.h>
10#include <AK/RefPtr.h>
11#include <AK/Types.h>
12#include <Kernel/FileSystem/BlockBasedFileSystem.h>
13#include <Kernel/FileSystem/FATFS/Definitions.h>
14#include <Kernel/FileSystem/Inode.h>
15#include <Kernel/Forward.h>
16#include <Kernel/KBuffer.h>
17
18namespace Kernel {
19
20class FATFS final : public BlockBasedFileSystem {
21 friend FATInode;
22
23public:
24 static ErrorOr<NonnullLockRefPtr<FileSystem>> try_create(OpenFileDescription&);
25
26 virtual ~FATFS() override = default;
27 virtual StringView class_name() const override { return "FATFS"sv; }
28 virtual Inode& root_inode() override;
29
30private:
31 virtual ErrorOr<void> initialize_while_locked() override;
32 virtual bool is_initialized_while_locked() override;
33 // FIXME: This is not a proper way to clear last mount of a FAT filesystem,
34 // but for now we simply have no other way to properly do it.
35 virtual ErrorOr<void> prepare_to_clear_last_mount() override { return {}; }
36
37 FATFS(OpenFileDescription&);
38
39 static constexpr u8 signature_1 = 0x28;
40 static constexpr u8 signature_2 = 0x29;
41
42 static constexpr u32 first_data_cluster = 2;
43
44 FAT32BootRecord const* boot_record() const { return reinterpret_cast<FAT32BootRecord const*>(m_boot_record->data()); };
45
46 BlockBasedFileSystem::BlockIndex first_block_of_cluster(u32 cluster) const;
47
48 OwnPtr<KBuffer> m_boot_record {};
49 RefPtr<FATInode> m_root_inode;
50 u32 m_first_data_sector { 0 };
51};
52
53}