xaizek / tos (License: GPLv3 only) (since 2018-12-07)
This is an alternative version of sources presented as part of Write Your Own OS video tutorial by Viktor Engelmann.
Commit 94ae3c0eeefc0a1baa186b8a74509e024ae3bb8f

Introduce FAT class
Author: xaizek
Author date (UTC): 2018-04-11 20:03
Committer name: xaizek
Committer date (UTC): 2018-04-12 13:51
Parent(s): 4f3df597ebe34811da23608d977b012fbdc09659
Signing key: 99DC5E4DB05F6BE2
Tree: 842ce0b577c01206b619359fb525f2aed8fced5f
File Lines added Lines deleted
src/fs/fat.cpp 11 19
src/fs/fat.hpp 18 1
src/fs/mbr.cpp 2 1
File src/fs/fat.cpp changed (mode: 100644) (index 6b993de..9b2ff57)
5 5
6 6 using namespace fs; using namespace fs;
7 7
8 static void printFileContents(const DirectoryEntryFat32 &ent,
9 drv::ATA &hd, const BiosParameterBlock32 &bpb,
10 std::uint32_t fatStart, std::uint32_t dataStart);
11
12 void
13 fs::readBiosBlock(drv::ATA &hd, std::uint32_t partitionOffset)
8 FAT::FAT(drv::ATA &hd, std::uint32_t partitionOffset) : hd(hd)
14 9 { {
15 BiosParameterBlock32 bpb;
16 10 hd.read28(partitionOffset, reinterpret_cast<std::uint8_t *>(&bpb), hd.read28(partitionOffset, reinterpret_cast<std::uint8_t *>(&bpb),
17 11 sizeof(BiosParameterBlock32)); sizeof(BiosParameterBlock32));
18 12
19 13 kprint("sectors per cluster: ", bpb.sectorsPerCluster, "\n"); kprint("sectors per cluster: ", bpb.sectorsPerCluster, "\n");
20 14
21 std::uint32_t fatStart = partitionOffset + bpb.reservedSectors;
22 std::uint32_t fatSize = bpb.tableSize;
23
24 std::uint32_t dataStart = fatStart + fatSize*bpb.fatCopies;
25
26 std::uint32_t rootStart = dataStart
27 + bpb.sectorsPerCluster*(bpb.rootCluster - 2);
15 fatStart = partitionOffset + bpb.reservedSectors;
16 dataStart = fatStart + bpb.tableSize*bpb.fatCopies;
17 rootStart = dataStart + bpb.sectorsPerCluster*(bpb.rootCluster - 2);
18 }
28 19
20 void
21 FAT::printFiles()
22 {
29 23 DirectoryEntryFat32 dirent[16]; DirectoryEntryFat32 dirent[16];
30 24 hd.read28(rootStart, reinterpret_cast<std::uint8_t *>(&dirent[0]), hd.read28(rootStart, reinterpret_cast<std::uint8_t *>(&dirent[0]),
31 25 16*sizeof(DirectoryEntryFat32)); 16*sizeof(DirectoryEntryFat32));
 
... ... fs::readBiosBlock(drv::ATA &hd, std::uint32_t partitionOffset)
47 41
48 42 // Skip directories. // Skip directories.
49 43 if (!(dirent[i].attributes & 0x10)) { if (!(dirent[i].attributes & 0x10)) {
50 printFileContents(dirent[i], hd, bpb, fatStart, dataStart);
44 printFileContents(dirent[i]);
51 45 } }
52 46 } }
53 47 } }
54 48
55 static void
56 printFileContents(const DirectoryEntryFat32 &ent, drv::ATA &hd,
57 const BiosParameterBlock32 &bpb, std::uint32_t fatStart,
58 std::uint32_t dataStart)
49 void
50 FAT::printFileContents(const DirectoryEntryFat32 &ent) const
59 51 { {
60 52 std::uint32_t firstCluster = ent.firstClusterHi; std::uint32_t firstCluster = ent.firstClusterHi;
61 53 firstCluster = (firstCluster << 16) | ent.firstClusterLow; firstCluster = (firstCluster << 16) | ent.firstClusterLow;
File src/fs/fat.hpp changed (mode: 100644) (index c36d0db..9c37b88)
... ... struct DirectoryEntryFat32
58 58 std::uint32_t size; std::uint32_t size;
59 59 } __attribute__((packed)); } __attribute__((packed));
60 60
61 void readBiosBlock(drv::ATA &hd, std::uint32_t partitionOffset);
61 class FAT
62 {
63 public:
64 FAT(drv::ATA &hd, std::uint32_t partitionOffset);
65
66 public:
67 void printFiles();
68
69 private:
70 void printFileContents(const DirectoryEntryFat32 &ent) const;
71
72 private:
73 drv::ATA &hd;
74 BiosParameterBlock32 bpb;
75 std::uint32_t fatStart;
76 std::uint32_t dataStart;
77 std::uint32_t rootStart;
78 };
62 79
63 80 } }
64 81
File src/fs/mbr.cpp changed (mode: 100644) (index dd4f59c..4b977ad)
... ... MBR::readPartitions(drv::ATA &hd)
31 31 kprint(" Partition ", i, is, " bootable. Type 0x", kprint(" Partition ", i, is, " bootable. Type 0x",
32 32 mbr.primaryPartition[i].partition_id, ".\n"); mbr.primaryPartition[i].partition_id, ".\n");
33 33
34 readBiosBlock(hd, mbr.primaryPartition[i].start_lba);
34 FAT fat(hd, mbr.primaryPartition[i].start_lba);
35 fat.printFiles();
35 36 } }
36 37 } }
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/tos

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/tos

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master