summaryrefslogtreecommitdiffhomepage
path: root/Private/Source
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-04-21 08:53:36 +0200
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-04-21 08:53:36 +0200
commitebadabf4b6ab4a08c47b098affa0de6d681b5982 (patch)
tree477087431fd2bbee9217390ab370dff30f0a2bf0 /Private/Source
parentbc4f32c10a34c6bded98f378004f7435deede43b (diff)
MHR-8: IMP: Implement an allocator for catalog and forks.
- Implement ke_find_free_catalog and ke_find_free_fork. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Private/Source')
-rw-r--r--Private/Source/CxxAbi.cxx2
-rw-r--r--Private/Source/FS/NewFS.cxx148
-rw-r--r--Private/Source/IndexableProperty.cxx2
-rw-r--r--Private/Source/KernelHeap.cxx4
-rw-r--r--Private/Source/ThreadLocalStorage.cxx6
5 files changed, 137 insertions, 25 deletions
diff --git a/Private/Source/CxxAbi.cxx b/Private/Source/CxxAbi.cxx
index d79d688c..452c4383 100644
--- a/Private/Source/CxxAbi.cxx
+++ b/Private/Source/CxxAbi.cxx
@@ -13,7 +13,7 @@ atexit_func_entry_t __atexit_funcs[kDSOMaxObjects];
uarch_t __atexit_func_count;
extern "C" void __cxa_pure_virtual() {
- NewOS::kcout << "NewOS: C++ placeholder method.\n";
+ NewOS::kcout << "New OS: C++ placeholder method.\n";
}
extern "C" void ___chkstk_ms() {
diff --git a/Private/Source/FS/NewFS.cxx b/Private/Source/FS/NewFS.cxx
index 216db7c6..7eb79d10 100644
--- a/Private/Source/FS/NewFS.cxx
+++ b/Private/Source/FS/NewFS.cxx
@@ -7,13 +7,16 @@
#ifdef __FSKIT_NEWFS__
#include <FSKit/NewFS.hxx>
+#include <KernelKit/HError.hpp>
+#include <NewKit/Crc32.hpp>
#include <NewKit/Utils.hpp>
using namespace NewOS;
-STATIC Lba ke_find_free_fork(SizeT sz);
-STATIC Lba ke_find_free_catalog(SizeT sz);
-STATIC Lba ke_find_free_data(SizeT sz);
+/// forward decl.
+
+STATIC Lba ke_find_free_fork(SizeT sz, Int32 drv, NewCatalog* catalog);
+STATIC Lba ke_find_free_catalog(SizeT kind, Int32 drv);
STATIC MountpointInterface sMountpointInterface;
@@ -26,7 +29,8 @@ _Output NewFork* NewFSParser::CreateFork(_Input NewCatalog* catalog,
if (catalog && theFork.Name[0] != 0 && theFork.DataSize > 0) {
Lba whereFork = 0;
- theFork.DataOffset = ke_find_free_fork(theFork.DataSize);
+ theFork.DataOffset =
+ ke_find_free_fork(theFork.DataSize, this->fDriveIndex, catalog);
theFork.Flags |= kNewFSFlagCreated;
if (catalog->FirstFork == 0) {
@@ -172,8 +176,10 @@ bool NewFSParser::Format(_Input _Output DriveTrait* drive) {
/// disk isnt faulty and data has been fetched.
if (drive->fPacket.fPacketGood) {
NewPartitionBlock* partBlock = (NewPartitionBlock*)sectorBuf;
+
+ /// check for an empty partition here.
if (partBlock->PartitionName[0] == 0 &&
- rt_string_cmp(partBlock->Ident, kNewFSIdent, kNewFSIdentLen) == 0) {
+ rt_string_cmp(partBlock->Ident, kNewFSIdent, kNewFSIdentLen)) {
/// partition is free and valid.
rt_copy_memory((VoidPtr)kNewFSIdent, (VoidPtr)partBlock->Ident,
@@ -276,23 +282,129 @@ bool NewFSParser::Seek(_Input _Output NewCatalog* catalog, SizeT off) {
/// @return
SizeT NewFSParser::Tell(_Input _Output NewCatalog* catalog) { return 0; }
-/// @brief
-/// @param sz
-/// @return
-STATIC Lba ke_find_free_fork(SizeT sz) { return 0; }
+/// @brief Find a free fork inside the filesystem.
+/// @param sz the size of the fork to set.
+/// @return the valid lba.
+STATIC Lba ke_find_free_fork(SizeT sz, Int32 drv, NewCatalog* catalog) {
+ auto drive = *sMountpointInterface.GetAddressOf(drv);
-/// @brief
-/// @param sz
-/// @return
-STATIC Lba ke_find_free_catalog(SizeT sz) { return 0; }
+ if (drive) {
+ /// prepare packet.
+ bool done = false;
+ bool error = false;
-/// @brief
-/// @param sz
-/// @return
-STATIC Lba ke_find_free_data(SizeT sz) { return 0; }
+ Lba lba = catalog->LastFork;
+
+ while (!done) {
+ Char sectorBuf[kNewFSMinimumSectorSz] = {0};
+
+ drive->fPacket.fPacketContent = sectorBuf;
+ drive->fPacket.fPacketSize = kNewFSMinimumSectorSz;
+ drive->fPacket.fLba = lba;
+
+ drive->fInput(&drive->fPacket);
+
+ if (!drive->fPacket.fPacketGood) {
+ ///! not a lot of choices, disk has become unreliable.
+ if (ke_calculate_crc32(sectorBuf, kNewFSMinimumSectorSz) !=
+ drive->fPacket.fPacketCRC32) {
+ DbgLastError() = kErrorDiskIsCorrupted;
+ }
+
+ error = true;
+ break;
+ }
+
+ NewFork* fork = (NewFork*)sectorBuf;
+
+ if (fork->DataSize == 0 && fork->Name[0] == 0 &&
+ (fork->Flags == kNewFSFlagDeleted ||
+ fork->Flags == kNewFSFlagUnallocated)) {
+ fork->DataSize = sz;
+ fork->Flags |= kNewFSFlagCreated;
+
+ drive->fOutput(&drive->fPacket);
+
+ /// here it's either a read-only filesystem or something bad happened.'
+ if (!drive->fPacket.fPacketGood) {
+ DbgLastError() = kErrorDiskReadOnly;
+
+ return 0;
+ }
+
+ return lba;
+ }
+
+ lba += sizeof(NewFork);
+ }
+
+ if (error) {
+ DbgLastError() = kErrorDisk;
+ return 0;
+ }
+ }
+
+ return 0;
+}
+
+/// @brief find a free catalog.
+/// @param kind the catalog kind.
+/// @return the valid lba.
+STATIC Lba ke_find_free_catalog(SizeT kind, Int32 drv) {
+ auto drive = *sMountpointInterface.GetAddressOf(drv);
+
+ if (drive) {
+ Char sectorBuf[kNewFSMinimumSectorSz] = {0};
+
+ /// prepare packet.
+
+ drive->fPacket.fPacketContent = sectorBuf;
+ drive->fPacket.fPacketSize = kNewFSMinimumSectorSz;
+ drive->fPacket.fLba = kNewFSAddressAsLba;
+
+ drive->fInput(&drive->fPacket);
+
+ NewPartitionBlock* partBlock = (NewPartitionBlock*)sectorBuf;
+
+ /// check for a valid partition.
+ if (partBlock->PartitionName[0] != 0 &&
+ rt_string_cmp(partBlock->Ident, kNewFSIdent, kNewFSIdentLen) == 0) {
+ auto startLba = partBlock->FreeCatalog;
+
+ if (startLba == 0) {
+ DbgLastError() = kErrorDiskIsFull;
+ return 1;
+ } else {
+ while (startLba != 0) {
+ drive->fPacket.fPacketContent = sectorBuf;
+ drive->fPacket.fPacketSize = kNewFSMinimumSectorSz;
+ drive->fPacket.fLba = startLba;
+
+ drive->fInput(&drive->fPacket);
+
+ NewCatalog* catalog = (NewCatalog*)sectorBuf;
+
+ if (catalog->Flags == kNewFSFlagUnallocated ||
+ catalog->Flags == kNewFSFlagDeleted) {
+ catalog->Flags |= kNewFSFlagCreated;
+ catalog->Kind |= kind;
+
+ return startLba;
+ }
+
+ startLba = catalog->NextSibling;
+ }
+
+ return 0;
+ }
+ }
+ }
+
+ return 0;
+}
namespace NewOS::Detail {
-Boolean fs_init_newfs(Void) noexcept { return false; }
+Boolean fs_init_newfs(Void) noexcept { return true; }
} // namespace NewOS::Detail
#endif // ifdef __FSKIT_NEWFS__
diff --git a/Private/Source/IndexableProperty.cxx b/Private/Source/IndexableProperty.cxx
index c3978fe9..dba56c74 100644
--- a/Private/Source/IndexableProperty.cxx
+++ b/Private/Source/IndexableProperty.cxx
@@ -34,7 +34,7 @@ Void fs_index_file(const Char* filename, SizeT filenameLen, IndexableProperty& i
indexer.AddFlag(kIndexerClaimed);
rt_copy_memory((VoidPtr)indexer.LeakProperty().Path, (VoidPtr)filename, filenameLen);
- kcout << "NewOS: FSKit: index new file: " << filename << endl;
+ kcout << "New OS: FSKit: index new file: " << filename << endl;
}
}
} // namespace Indexer
diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx
index 72d291a8..3b77ebb6 100644
--- a/Private/Source/KernelHeap.cxx
+++ b/Private/Source/KernelHeap.cxx
@@ -97,7 +97,7 @@ Int32 ke_delete_ke_heap(VoidPtr heapPtr) {
PTEWrapper pageWrapper(false, false, false, (UIntPtr)virtualAddress);
Ref<PTEWrapper*> pteAddress{ &pageWrapper };
-
+
kHeapPageManager.Free(pteAddress);
--kHeapCount;
@@ -126,7 +126,7 @@ Boolean ke_is_valid_heap(VoidPtr heapPtr) {
return false;
}
-/// @brief Protect the heap pointer with a CRC32.
+/// @brief Protect the heap with a CRC value.
/// @param heapPtr HIB pointer.
/// @return if it valid: point has crc now., otherwise fail.
Boolean ke_protect_ke_heap(VoidPtr heapPtr) {
diff --git a/Private/Source/ThreadLocalStorage.cxx b/Private/Source/ThreadLocalStorage.cxx
index d3e31a1c..8d3281e3 100644
--- a/Private/Source/ThreadLocalStorage.cxx
+++ b/Private/Source/ThreadLocalStorage.cxx
@@ -31,7 +31,7 @@ Boolean tls_check_tib(ThreadInformationBlock* tib) {
Encoder encoder;
const char* tibAsBytes = encoder.AsBytes(tib);
- kcout << "NewOS: Checking for a valid cookie...\r\n";
+ kcout << "New OS: Checking for a valid cookie...\r\n";
return tibAsBytes[0] == kCookieMag0 && tibAsBytes[1] == kCookieMag1 &&
tibAsBytes[2] == kCookieMag2;
@@ -46,9 +46,9 @@ EXTERN_C Void tls_check_syscall_impl(NewOS::HAL::StackFramePtr stackPtr) noexcep
ThreadInformationBlock* tib = (ThreadInformationBlock*)stackPtr->Gs;
if (!tls_check_tib(tib)) {
- kcout << "NewOS: Verification failed, Crashing...\r\n";
+ kcout << "New OS: Verification failed, Crashing...\r\n";
ProcessScheduler::Shared().Leak().GetCurrent().Leak().Crash();
}
- kcout << "NewOS: Verification succeeded! Keeping on...\r\n";
+ kcout << "New OS: Verification succeeded! Keeping on...\r\n";
}