refactor(kernel): ramfs loading

This commit is contained in:
EnderIce2 2025-04-07 05:04:23 +00:00
parent b1a30059ed
commit d4346202ca
Signed by: enderice2
GPG Key ID: FEB6B8A8507BA62E
5 changed files with 95 additions and 38 deletions

View File

@ -139,6 +139,14 @@ namespace vfs
public: public:
vfsInode *FileSystemRoots = nullptr; vfsInode *FileSystemRoots = nullptr;
/**
* Default reserved roots:
*
* 0 - Native
* 1 - Linux
* 2 - Windows
*/
std::unordered_map<ino_t, FileNode *> FileRoots; std::unordered_map<ino_t, FileNode *> FileRoots;
bool PathIsRelative(const char *Path); bool PathIsRelative(const char *Path);

View File

@ -127,11 +127,11 @@ namespace vfs
bool TestArchive(uintptr_t Address); bool TestArchive(uintptr_t Address);
void ReadArchive(uintptr_t Address, size_t Size); void ReadArchive(uintptr_t Address, size_t Size);
USTAR(){}; USTAR() = default;
~USTAR(){}; ~USTAR() = default;
}; };
} }
bool TestAndInitializeUSTAR(uintptr_t Address, size_t Size); bool TestAndInitializeUSTAR(uintptr_t Address, size_t Size, size_t Index);
#endif // !__FENNIX_KERNEL_FILESYSTEM_USTAR_H__ #endif // !__FENNIX_KERNEL_FILESYSTEM_USTAR_H__

View File

@ -31,30 +31,72 @@
#include <vm.hpp> #include <vm.hpp>
#include <vector> #include <vector>
cold int SpawnInit() int SpawnNativeInit()
{
const char *envp[] = {
"PATH=/sys/bin:/usr/bin",
"LD_LIBRARY_PATH=/sys/lib:/usr/lib",
"TERM=tty",
"HOME=/home/root",
"USER=root",
"TZ=UTC",
nullptr};
const char *argv[] = {Config.InitPath, nullptr};
return Execute::Spawn(Config.InitPath, argv, envp, nullptr, false, Tasking::Native, true);
}
int SpawnLinuxInit()
{ {
const char *envp[] = { const char *envp[] = {
"PATH=/bin:/usr/bin", "PATH=/bin:/usr/bin",
"LD_LIBRARY_PATH=/sys/lib:/usr/lib", "LD_LIBRARY_PATH=/lib:/usr/lib",
"TERM=tty", "TERM=tty",
"HOME=/root", "HOME=/root",
"USER=root", "USER=root",
"TZ=UTC", "TZ=UTC",
nullptr}; nullptr};
const char *argv[] = { std::string init = Config.InitPath;
Config.InitPath, std::vector<std::string> fallbackPaths = {
nullptr}; init,
"/bin/init",
"/sbin/init",
"/system/init",
"/usr/bin/init",
"/boot/init",
"/startup/init"};
Tasking::TaskCompatibility compat = Tasking::Native; const char *foundPath = nullptr;
if (Config.LinuxSubsystem) for (const std::string &path : fallbackPaths)
compat = Tasking::Linux; {
if (!fs->PathExists(path.c_str(), fs->GetRoot(1)))
continue;
return Execute::Spawn(Config.InitPath, argv, envp, foundPath = path.c_str();
nullptr, false, compat, true); break;
}
if (!foundPath)
{
error("No valid init found in fallback paths");
return -ENOENT;
}
const char *argv[] = {foundPath, nullptr};
return Execute::Spawn(foundPath, argv, envp, nullptr, false, Tasking::Linux, true);
} }
cold void KernelMainThread() int SpawnInit()
{
if (Config.LinuxSubsystem)
return SpawnLinuxInit();
else
return SpawnNativeInit();
}
void KernelMainThread()
{ {
thisThread->SetPriority(Tasking::Critical); thisThread->SetPriority(Tasking::Critical);
@ -129,7 +171,7 @@ Exit:
} }
NewLock(ShutdownLock); NewLock(ShutdownLock);
cold void __no_stack_protector KernelShutdownThread(bool Reboot) void __no_stack_protector KernelShutdownThread(bool Reboot)
{ {
SmartLock(ShutdownLock); SmartLock(ShutdownLock);
debug("KernelShutdownThread(%s)", Reboot ? "true" : "false"); debug("KernelShutdownThread(%s)", Reboot ? "true" : "false");

View File

@ -26,26 +26,26 @@ void SearchForInitrd()
{ {
for (size_t i = 0; i < MAX_MODULES; i++) for (size_t i = 0; i < MAX_MODULES; i++)
{ {
uintptr_t initrdAddress = (uintptr_t)bInfo.Modules[i].Address; uintptr_t moduleAddress = (uintptr_t)bInfo.Modules[i].Address;
size_t moduleSize = bInfo.Modules[i].Size;
const char *moduleCommand = bInfo.Modules[i].CommandLine;
if (!initrdAddress) if (moduleAddress == 0)
continue; continue;
if (strcmp(bInfo.Modules[i].CommandLine, "rootfs") != 0)
continue;
KPrint("rootfs found at %#lx", initrdAddress);
Memory::Virtual vmm; Memory::Virtual vmm;
if (!vmm.Check((void *)initrdAddress)) if (!vmm.CheckRegion((void *)moduleAddress, moduleSize))
{ {
warn("Initrd is not mapped!"); warn("module entry is not mapped!");
vmm.Map((void *)initrdAddress, (void *)initrdAddress, vmm.Map((void *)moduleAddress, (void *)moduleAddress, moduleSize, Memory::RW);
bInfo.Modules[i].Size, Memory::RW);
} }
if (TestAndInitializeUSTAR(initrdAddress, bInfo.Modules[i].Size)) if (strcmp(moduleCommand, "rootfs") == 0)
continue; /* Maybe add another root? */ {
KPrint("rootfs found at %#lx", moduleAddress);
if (TestAndInitializeUSTAR(moduleAddress, moduleSize, 0))
continue;
}
} }
} }

View File

@ -16,7 +16,6 @@
*/ */
#include <filesystem/ustar.hpp> #include <filesystem/ustar.hpp>
#include <memory.hpp> #include <memory.hpp>
#include <functional> #include <functional>
#include <debug.h> #include <debug.h>
@ -160,10 +159,14 @@ namespace vfs
node->Name.assign(basename, length); node->Name.assign(basename, length);
node->Path.assign(Name, strlen(Name)); node->Path.assign(Name, strlen(Name));
Files.insert(std::make_pair(NextInode, node)); auto &&file = Files.insert(std::make_pair(NextInode, node));
*Result = &Files.at(NextInode)->Node; assert(file.second == true);
*Result = &file.first->second->Node;
if (Parent) if (Parent)
Parent->Children.push_back(Files.at(NextInode)); {
Parent->Children.push_back(file.first->second);
file.first->second->Parent = Parent;
}
NextInode++; NextInode++;
return 0; return 0;
} }
@ -491,6 +494,10 @@ namespace vfs
FileHeader *header = (FileHeader *)Address; FileHeader *header = (FileHeader *)Address;
if (strncmp(header->signature, TMAGIC, TMAGLEN) != 0) if (strncmp(header->signature, TMAGIC, TMAGLEN) != 0)
{ {
/* For some reason if GRUB inflates the archive, the magic is "ustar " */
if (strncmp(header->signature, TMAGIC, TMAGLEN - 1) == 0)
return true;
error("Invalid signature!"); error("Invalid signature!");
return false; return false;
} }
@ -564,7 +571,7 @@ namespace vfs
FileHeader *header = (FileHeader *)Address; FileHeader *header = (FileHeader *)Address;
debug("USTAR signature valid! Name:%s Signature:%s Mode:%d Size:%lu", debug("USTAR signature valid! Name:\"%s\" Signature:\"%s\" Mode:%d Size:%lu",
header->name, header->signature, StringToInt(header->mode), header->size); header->name, header->signature, StringToInt(header->mode), header->size);
Memory::Virtual vmm; Memory::Virtual vmm;
@ -577,7 +584,7 @@ namespace vfs
return; return;
} }
if (strncmp(header->signature, TMAGIC, TMAGLEN) != 0) if (strncmp(header->signature, TMAGIC, TMAGLEN - 1) != 0)
break; break;
// debug("\"%s\"", header->name); // debug("\"%s\"", header->name);
@ -817,13 +824,13 @@ O2 int __ustar_Stat(struct Inode *Node, kstat *Stat)
return ((vfs::USTAR *)Node->PrivateData)->Stat(Node, Stat); return ((vfs::USTAR *)Node->PrivateData)->Stat(Node, Stat);
} }
int __ustar_DestroyInode(FileSystemInfo *Info, Inode *Node) O2 int __ustar_DestroyInode(FileSystemInfo *Info, Inode *Node)
{ {
((vfs::USTAR::USTARInode *)Node)->Deleted = true; ((vfs::USTAR::USTARInode *)Node)->Deleted = true;
return 0; return 0;
} }
int __ustar_Destroy(FileSystemInfo *fsi) O2 int __ustar_Destroy(FileSystemInfo *fsi)
{ {
assert(fsi->PrivateData); assert(fsi->PrivateData);
delete (vfs::USTAR *)fsi->PrivateData; delete (vfs::USTAR *)fsi->PrivateData;
@ -831,7 +838,7 @@ int __ustar_Destroy(FileSystemInfo *fsi)
return 0; return 0;
} }
bool TestAndInitializeUSTAR(uintptr_t Address, size_t Size) bool TestAndInitializeUSTAR(uintptr_t Address, size_t Size, size_t Index)
{ {
vfs::USTAR *ustar = new vfs::USTAR(); vfs::USTAR *ustar = new vfs::USTAR();
if (!ustar->TestArchive(Address)) if (!ustar->TestArchive(Address))
@ -863,6 +870,6 @@ bool TestAndInitializeUSTAR(uintptr_t Address, size_t Size)
fsi->PrivateData = ustar; fsi->PrivateData = ustar;
fs->LateRegisterFileSystem(ustar->DeviceID, fsi, rootfs); fs->LateRegisterFileSystem(ustar->DeviceID, fsi, rootfs);
fs->AddRoot(rootfs); fs->AddRootAt(rootfs, Index);
return true; return true;
} }