Remove redundant file operation functions

This commit is contained in:
EnderIce2 2024-04-01 04:36:11 +03:00
parent bbb67b6a88
commit a49e5e9913
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
15 changed files with 153 additions and 320 deletions

View File

@ -381,14 +381,14 @@ namespace Driver
break;
}
int fd = fopen(rDrv->node->FullPath, "r");
vfs::RefNode *fd = fs->Open(rDrv->node->FullPath);
std::vector<Elf64_Dyn> SymTab = Execute::ELFGetDynamicTag_x86_64(fd, DT_SYMTAB);
std::vector<Elf64_Dyn> StrTab = Execute::ELFGetDynamicTag_x86_64(fd, DT_STRTAB);
Elf64_Sym *_SymTab = (Elf64_Sym *)((uintptr_t)BaseAddress + SymTab[0].d_un.d_ptr);
char *DynStr = (char *)((uintptr_t)BaseAddress + StrTab[0].d_un.d_ptr);
UNUSED(DynStr);
fclose(fd);
delete fd;
Elf64_Rela *Rela = (Elf64_Rela *)(BaseAddress + Dynamic->d_un.d_ptr);
for (size_t i = 0; i < (PltRelSize->d_un.d_val / sizeof(Elf64_Rela)); i++)
@ -431,14 +431,14 @@ namespace Driver
{
fixme("DT_SYMTAB");
break;
int fd = fopen(rDrv->node->FullPath, "r");
vfs::RefNode *fd = fs->Open(rDrv->node->FullPath);
std::vector<Elf64_Dyn> SymTab = Execute::ELFGetDynamicTag_x86_64(fd, DT_SYMTAB);
std::vector<Elf64_Dyn> StrTab = Execute::ELFGetDynamicTag_x86_64(fd, DT_STRTAB);
Elf64_Sym *_SymTab = (Elf64_Sym *)((uintptr_t)BaseAddress + SymTab[0].d_un.d_ptr);
char *DynStr = (char *)((uintptr_t)BaseAddress + StrTab[0].d_un.d_ptr);
UNUSED(DynStr);
fclose(fd);
delete fd;
size_t symtabEntrySize = 0;
Elf64_Dyn *entrySizeDyn = Dynamic;

View File

@ -19,6 +19,8 @@
#include <msexec.h>
#include "../kernel.h"
namespace Execute
{
BinaryType GetBinaryType(const char *Path)
@ -26,18 +28,17 @@ namespace Execute
debug("Checking binary type of %s(ptr: %#lx)",
Path, Path);
BinaryType Type;
int fd = fopen(Path, "r");
vfs::RefNode *fd = fs->Open(Path);
if (fd < 0)
if (fd == nullptr)
{
debug("Failed to open file %s: %s",
Path, strerror(fd));
return (BinaryType)fd;
debug("Failed to open file %s", Path);
return (BinaryType)-ENOENT;
}
debug("File opened: %s, descriptor %d", Path, fd);
Memory::SmartHeap sh = Memory::SmartHeap(1024);
fread(fd, sh, 128);
fd->read(sh, 128);
Elf32_Ehdr *ELFHeader = (Elf32_Ehdr *)sh.Get();
IMAGE_DOS_HEADER *MZHeader = (IMAGE_DOS_HEADER *)sh.Get();
@ -56,8 +57,8 @@ namespace Execute
/* Check MZ header. */
else if (MZHeader->e_magic == IMAGE_DOS_SIGNATURE)
{
lseek(fd, MZHeader->e_lfanew, SEEK_SET);
fread(fd, sh, 512);
fd->seek(MZHeader->e_lfanew, SEEK_SET);
fd->read(sh, 512);
IMAGE_NT_HEADERS *PEHeader =
(IMAGE_NT_HEADERS *)(((char *)sh.Get()) +
MZHeader->e_lfanew);
@ -91,7 +92,7 @@ namespace Execute
Type = BinaryType::BinTypeUnknown;
Success:
fclose(fd);
delete fd;
return Type;
}
}

View File

@ -33,7 +33,7 @@ using namespace vfs;
namespace Execute
{
void ELFObject::GenerateAuxiliaryVector_x86_32(Memory::VirtualMemoryArea *vma,
int fd,
vfs::RefNode *fd,
Elf32_Ehdr ELFHeader,
uint32_t EntryPoint,
uint32_t BaseAddress)
@ -41,7 +41,7 @@ namespace Execute
}
void ELFObject::GenerateAuxiliaryVector_x86_64(Memory::VirtualMemoryArea *vma,
int fd,
vfs::RefNode *fd,
Elf64_Ehdr ELFHeader,
uint64_t EntryPoint,
uint64_t BaseAddress)
@ -50,10 +50,8 @@ namespace Execute
char *aux_platform = (char *)vma->RequestPages(1, true); /* TODO: 4KiB is too much for this */
strcpy(aux_platform, "x86_64");
const char *execfn = thisProcess->FileDescriptors->GetAbsolutePath(fd);
void *execfn_str = vma->RequestPages(TO_PAGES(strlen(execfn) + 1), true);
strcpy((char *)execfn_str, execfn);
delete[] execfn;
void *execfn_str = vma->RequestPages(TO_PAGES(strlen(fd->node->FullPath) + 1), true);
strcpy((char *)execfn_str, fd->node->FullPath);
void *at_random = vma->RequestPages(1, true);
*(uint64_t *)at_random = Random::rand16();
@ -95,25 +93,23 @@ namespace Execute
#endif
}
void ELFObject::LoadExec_x86_32(int fd, PCB *TargetProcess)
void ELFObject::LoadExec_x86_32(vfs::RefNode *, PCB *)
{
stub;
UNUSED(fd);
UNUSED(TargetProcess);
}
void ELFObject::LoadExec_x86_64(int fd, PCB *TargetProcess)
void ELFObject::LoadExec_x86_64(vfs::RefNode *fd, PCB *TargetProcess)
{
#if defined(a64)
std::vector<Elf64_Phdr> PhdrINTERP = ELFGetSymbolType_x86_64(fd, PT_INTERP);
foreach (auto Interp in PhdrINTERP)
{
Memory::SmartHeap InterpreterPath = Memory::SmartHeap(256);
lseek(fd, Interp.p_offset, SEEK_SET);
fread(fd, InterpreterPath, 256);
fd->seek(Interp.p_offset, SEEK_SET);
fd->read(InterpreterPath, 256);
int ifd = fopen((const char *)InterpreterPath.Get(), "r");
if (ifd < 0)
vfs::RefNode *ifd = fs->Open((const char *)InterpreterPath.Get());
if (ifd == nullptr)
{
warn("Failed to open interpreter file: %s",
(const char *)InterpreterPath.Get());
@ -125,7 +121,7 @@ namespace Execute
{
warn("Interpreter %s is not an ELF file",
(const char *)InterpreterPath.Get());
fclose(ifd);
delete ifd;
continue;
}
@ -134,15 +130,15 @@ namespace Execute
/* FIXME: specify argv[1] as the location for the interpreter */
debug("Interpreter loaded successfully");
fclose(ifd);
delete ifd;
return;
}
}
}
Elf64_Ehdr ELFHeader;
lseek(fd, 0, SEEK_SET);
fread(fd, (uint8_t *)&ELFHeader, sizeof(Elf64_Ehdr));
fd->seek(0, SEEK_SET);
fd->read((uint8_t *)&ELFHeader, sizeof(Elf64_Ehdr));
uintptr_t EntryPoint = ELFHeader.e_entry;
debug("Entry point is %#lx", EntryPoint);
@ -156,8 +152,8 @@ namespace Execute
Elf64_Phdr ProgramHeader;
for (Elf64_Half i = 0; i < ELFHeader.e_phnum; i++)
{
lseek(fd, ELFHeader.e_phoff + (i * sizeof(Elf64_Phdr)), SEEK_SET);
fread(fd, (uint8_t *)&ProgramHeader, sizeof(Elf64_Phdr));
fd->seek(ELFHeader.e_phoff + (i * sizeof(Elf64_Phdr)), SEEK_SET);
fd->read((uint8_t *)&ProgramHeader, sizeof(Elf64_Phdr));
switch (ProgramHeader.p_type)
{
case PT_LOAD:
@ -188,8 +184,8 @@ namespace Execute
{
debug("%d %#lx %d", ProgramHeader.p_offset,
(uint8_t *)pAddr + SegDestOffset, ProgramHeader.p_filesz);
lseek(fd, ProgramHeader.p_offset, SEEK_SET);
fread(fd, (uint8_t *)pAddr + SegDestOffset, ProgramHeader.p_filesz);
fd->seek(ProgramHeader.p_offset, SEEK_SET);
fd->read((uint8_t *)pAddr + SegDestOffset, ProgramHeader.p_filesz);
}
if (ProgramHeader.p_memsz - ProgramHeader.p_filesz > 0)
@ -207,40 +203,40 @@ namespace Execute
case PT_NOTE:
{
Elf64_Nhdr NoteHeader;
lseek(fd, ProgramHeader.p_offset, SEEK_SET);
fread(fd, (uint8_t *)&NoteHeader, sizeof(Elf64_Nhdr));
fd->seek(ProgramHeader.p_offset, SEEK_SET);
fd->read((uint8_t *)&NoteHeader, sizeof(Elf64_Nhdr));
switch (NoteHeader.n_type)
{
case NT_PRSTATUS:
{
Elf64_Prstatus prstatus;
lseek(fd, ProgramHeader.p_offset + sizeof(Elf64_Nhdr), SEEK_SET);
fread(fd, (uint8_t *)&prstatus, sizeof(Elf64_Prstatus));
fd->seek(ProgramHeader.p_offset + sizeof(Elf64_Nhdr), SEEK_SET);
fd->read((uint8_t *)&prstatus, sizeof(Elf64_Prstatus));
debug("PRSTATUS: %#lx", prstatus.pr_reg[0]);
break;
}
case NT_PRPSINFO:
{
Elf64_Prpsinfo prpsinfo;
lseek(fd, ProgramHeader.p_offset + sizeof(Elf64_Nhdr), SEEK_SET);
fread(fd, (uint8_t *)&prpsinfo, sizeof(Elf64_Prpsinfo));
fd->seek(ProgramHeader.p_offset + sizeof(Elf64_Nhdr), SEEK_SET);
fd->read((uint8_t *)&prpsinfo, sizeof(Elf64_Prpsinfo));
debug("PRPSINFO: %s", prpsinfo.pr_fname);
break;
}
case NT_PLATFORM:
{
char platform[256];
lseek(fd, ProgramHeader.p_offset + sizeof(Elf64_Nhdr), SEEK_SET);
fread(fd, (uint8_t *)&platform, 256);
fd->seek(ProgramHeader.p_offset + sizeof(Elf64_Nhdr), SEEK_SET);
fd->read((uint8_t *)&platform, 256);
debug("PLATFORM: %s", platform);
break;
}
case NT_AUXV:
{
Elf64_auxv_t auxv;
lseek(fd, ProgramHeader.p_offset + sizeof(Elf64_Nhdr), SEEK_SET);
fread(fd, (uint8_t *)&auxv, sizeof(Elf64_auxv_t));
fd->seek(ProgramHeader.p_offset + sizeof(Elf64_Nhdr), SEEK_SET);
fd->read((uint8_t *)&auxv, sizeof(Elf64_auxv_t));
debug("AUXV: %#lx", auxv.a_un.a_val);
break;
}
@ -258,8 +254,8 @@ namespace Execute
debug("TLS Size: %ld (%ld pages)",
tlsSize, TO_PAGES(tlsSize));
void *tlsMemory = vma->RequestPages(TO_PAGES(tlsSize));
lseek(fd, ProgramHeader.p_offset, SEEK_SET);
fread(fd, (uint8_t *)tlsMemory, tlsSize);
fd->seek(ProgramHeader.p_offset, SEEK_SET);
fd->read((uint8_t *)tlsMemory, tlsSize);
TargetProcess->TLS = {
.pBase = uintptr_t(tlsMemory),
.vBase = ProgramHeader.p_vaddr,
@ -327,26 +323,24 @@ namespace Execute
#endif
}
void ELFObject::LoadDyn_x86_32(int fd, PCB *TargetProcess)
void ELFObject::LoadDyn_x86_32(vfs::RefNode *, PCB *)
{
stub;
UNUSED(fd);
UNUSED(TargetProcess);
}
void ELFObject::LoadDyn_x86_64(int fd, PCB *TargetProcess)
void ELFObject::LoadDyn_x86_64(vfs::RefNode *fd, PCB *TargetProcess)
{
#if defined(a64)
std::vector<Elf64_Phdr> PhdrINTERP = ELFGetSymbolType_x86_64(fd, PT_INTERP);
foreach (auto Interp in PhdrINTERP)
{
Memory::SmartHeap InterpreterPath = Memory::SmartHeap(256);
lseek(fd, Interp.p_offset, SEEK_SET);
fread(fd, InterpreterPath, 256);
fd->seek(Interp.p_offset, SEEK_SET);
fd->read(InterpreterPath, 256);
InterpreterPath = InterpreterPath;
int ifd = fopen((const char *)InterpreterPath.Get(), "r");
if (ifd < 0)
vfs::RefNode *ifd = fs->Open((const char *)InterpreterPath.Get());
if (ifd == nullptr)
{
warn("Failed to open interpreter file: %s",
(const char *)InterpreterPath.Get());
@ -358,22 +352,22 @@ namespace Execute
{
warn("Interpreter %s is not an ELF file",
(const char *)InterpreterPath.Get());
fclose(ifd);
delete ifd;
continue;
}
if (LoadInterpreter(ifd, TargetProcess))
{
debug("Interpreter loaded successfully");
fclose(ifd);
delete ifd;
return;
}
}
}
Elf64_Ehdr ELFHeader;
lseek(fd, 0, SEEK_SET);
fread(fd, (uint8_t *)&ELFHeader, sizeof(Elf64_Ehdr));
fd->seek(0, SEEK_SET);
fd->read((uint8_t *)&ELFHeader, sizeof(Elf64_Ehdr));
uintptr_t EntryPoint = ELFHeader.e_entry;
debug("Entry point is %#lx", EntryPoint);
@ -389,8 +383,8 @@ namespace Execute
size_t SegmentsSize = 0;
for (Elf64_Half i = 0; i < ELFHeader.e_phnum; i++)
{
lseek(fd, ELFHeader.e_phoff + (i * sizeof(Elf64_Phdr)), SEEK_SET);
fread(fd, (uint8_t *)&ProgramHeader, sizeof(Elf64_Phdr));
fd->seek(ELFHeader.e_phoff + (i * sizeof(Elf64_Phdr)), SEEK_SET);
fd->read((uint8_t *)&ProgramHeader, sizeof(Elf64_Phdr));
if (ProgramHeader.p_type == PT_LOAD ||
ProgramHeader.p_type == PT_DYNAMIC)
@ -414,8 +408,8 @@ namespace Execute
for (Elf64_Half i = 0; i < ELFHeader.e_phnum; i++)
{
lseek(fd, ELFHeader.e_phoff + (i * sizeof(Elf64_Phdr)), SEEK_SET);
fread(fd, (uint8_t *)&ProgramHeader, sizeof(Elf64_Phdr));
fd->seek(ELFHeader.e_phoff + (i * sizeof(Elf64_Phdr)), SEEK_SET);
fd->read((uint8_t *)&ProgramHeader, sizeof(Elf64_Phdr));
switch (ProgramHeader.p_type)
{
@ -434,8 +428,8 @@ namespace Execute
if (ProgramHeader.p_filesz > 0)
{
lseek(fd, ProgramHeader.p_offset, SEEK_SET);
fread(fd, (uint8_t *)SegmentDestination, ProgramHeader.p_filesz);
fd->seek(ProgramHeader.p_offset, SEEK_SET);
fd->read((uint8_t *)SegmentDestination, ProgramHeader.p_filesz);
}
if (ProgramHeader.p_memsz - ProgramHeader.p_filesz > 0)
@ -461,8 +455,8 @@ namespace Execute
if (ProgramHeader.p_filesz > 0)
{
lseek(fd, ProgramHeader.p_offset, SEEK_SET);
fread(fd, (uint8_t *)DynamicSegmentDestination, ProgramHeader.p_filesz);
fd->seek(ProgramHeader.p_offset, SEEK_SET);
fd->read((uint8_t *)DynamicSegmentDestination, ProgramHeader.p_filesz);
}
if (ProgramHeader.p_memsz - ProgramHeader.p_filesz > 0)
@ -565,14 +559,14 @@ namespace Execute
// Elf64_Shdr shdr;
// for (Elf64_Half i = 0; i < ELFHeader.e_shnum; i++)
// {
// lseek(fd, ELFHeader.e_shoff + i * sizeof(Elf64_Shdr), SEEK_SET);
// fread(fd, (uint8_t *)&shdr, sizeof(Elf64_Shdr));
// fd->seek(ELFHeader.e_shoff + i * sizeof(Elf64_Shdr), SEEK_SET);
// fd->read((uint8_t *)&shdr, sizeof(Elf64_Shdr));
// char sectionName[32];
// Elf64_Shdr n_shdr;
// lseek(fd, ELFHeader.e_shoff + ELFHeader.e_shstrndx * sizeof(Elf64_Shdr), SEEK_SET);
// fread(fd, (uint8_t *)&n_shdr, sizeof(Elf64_Shdr));
// lseek(fd, n_shdr.sh_offset + shdr.sh_name, SEEK_SET);
// fread(fd, (uint8_t *)sectionName, 32);
// fd->seek(ELFHeader.e_shoff + ELFHeader.e_shstrndx * sizeof(Elf64_Shdr), SEEK_SET);
// fd->read((uint8_t *)&n_shdr, sizeof(Elf64_Shdr));
// fd->seek(n_shdr.sh_offset + shdr.sh_name, SEEK_SET);
// fd->read((uint8_t *)sectionName, 32);
// debug("shdr: %s", sectionName);
// if (strcmp(sectionName, ".rela.plt") == 0)
// {
@ -698,8 +692,8 @@ namespace Execute
// // STT_OBJECT
// Elf64_Xword numEntries = shdr.sh_size / shdr.sh_entsize;
// Elf64_Sym *SymArray = new Elf64_Sym[numEntries];
// lseek(fd, shdr.sh_offset, SEEK_SET);
// fread(fd, (uint8_t *)SymArray, shdr.sh_size);
// fd->seek(shdr.sh_offset, SEEK_SET);
// fd->read((uint8_t *)SymArray, shdr.sh_size);
// debug("start %#lx (off %#lx), entries %ld",
// SymArray, shdr.sh_addr, numEntries);
// for (Elf64_Xword j = 0; j < numEntries; j++)
@ -740,10 +734,10 @@ namespace Execute
#endif
}
bool ELFObject::LoadInterpreter(int fd, PCB *TargetProcess)
bool ELFObject::LoadInterpreter(vfs::RefNode *fd, PCB *TargetProcess)
{
Elf32_Ehdr ELFHeader;
fread(fd, &ELFHeader, sizeof(Elf32_Ehdr));
fd->read((uint8_t *)&ELFHeader, sizeof(Elf32_Ehdr));
switch (ELFHeader.e_type)
{
@ -822,8 +816,8 @@ namespace Execute
return;
}
int fd = fopen(AbsolutePath, "r");
if (fd < 0)
vfs::RefNode *fd = fs->Open(AbsolutePath);
if (fd == nullptr)
{
error("Failed to open %s, errno: %d", AbsolutePath, fd);
return;
@ -839,17 +833,17 @@ namespace Execute
envc++;
Elf32_Ehdr ELFHeader;
fread(fd, &ELFHeader, sizeof(Elf32_Ehdr));
fd->read((uint8_t *)&ELFHeader, sizeof(Elf32_Ehdr));
std::vector<Elf64_Phdr> PhdrINTERP = ELFGetSymbolType_x86_64(fd, PT_INTERP);
const char *ElfInterpPath = nullptr;
if (!PhdrINTERP.empty() && ELFHeader.e_type == ET_DYN)
{
lseek(fd, PhdrINTERP.front().p_offset, SEEK_SET);
fd->seek(PhdrINTERP.front().p_offset, SEEK_SET);
ElfInterpPath = new char[256];
fread(fd, (void *)ElfInterpPath, 256);
fd->read((uint8_t *)ElfInterpPath, 256);
debug("Interpreter: %s", ElfInterpPath);
lseek(fd, 0, SEEK_SET);
fd->seek(0, SEEK_SET);
argc++;
}
@ -952,7 +946,7 @@ namespace Execute
}
}
fclose(fd);
delete fd;
}
ELFObject::~ELFObject()

View File

@ -92,14 +92,14 @@ namespace Execute
return nullptr;
}
Elf64_Sym ELFLookupSymbol(int fd, const char *Name)
Elf64_Sym ELFLookupSymbol(vfs::RefNode *fd, const char *Name)
{
#if defined(a64)
off_t OldOffset = lseek(fd, 0, SEEK_CUR);
off_t OldOffset = fd->seek(0, SEEK_CUR);
Elf64_Ehdr Header;
lseek(fd, 0, SEEK_SET);
fread(fd, (uint8_t *)&Header, sizeof(Elf64_Ehdr));
fd->seek(0, SEEK_SET);
fd->read((uint8_t *)&Header, sizeof(Elf64_Ehdr));
Elf64_Shdr SymbolTable;
Elf64_Shdr StringTable;
@ -107,15 +107,15 @@ namespace Execute
for (Elf64_Half i = 0; i < Header.e_shnum; i++)
{
Elf64_Shdr shdr;
lseek(fd, Header.e_shoff + (i * sizeof(Elf64_Shdr)), SEEK_SET);
fread(fd, (uint8_t *)&shdr, sizeof(Elf64_Shdr));
fd->seek(Header.e_shoff + (i * sizeof(Elf64_Shdr)), SEEK_SET);
fd->read((uint8_t *)&shdr, sizeof(Elf64_Shdr));
switch (shdr.sh_type)
{
case SHT_SYMTAB:
SymbolTable = shdr;
lseek(fd, Header.e_shoff + (shdr.sh_link * sizeof(Elf64_Shdr)), SEEK_SET);
fread(fd, (uint8_t *)&StringTable, sizeof(Elf64_Shdr));
fd->seek(Header.e_shoff + (shdr.sh_link * sizeof(Elf64_Shdr)), SEEK_SET);
fd->read((uint8_t *)&StringTable, sizeof(Elf64_Shdr));
break;
default:
{
@ -128,7 +128,7 @@ namespace Execute
StringTable.sh_name == 0)
{
error("Symbol table not found.");
lseek(fd, OldOffset, SEEK_SET);
fd->seek(OldOffset, SEEK_SET);
return {};
}
@ -136,22 +136,22 @@ namespace Execute
{
// Elf64_Sym *Symbol = (Elf64_Sym *)((uintptr_t)Header + SymbolTable->sh_offset + (i * sizeof(Elf64_Sym)));
Elf64_Sym Symbol;
lseek(fd, SymbolTable.sh_offset + (i * sizeof(Elf64_Sym)), SEEK_SET);
fread(fd, (uint8_t *)&Symbol, sizeof(Elf64_Sym));
fd->seek(SymbolTable.sh_offset + (i * sizeof(Elf64_Sym)), SEEK_SET);
fd->read((uint8_t *)&Symbol, sizeof(Elf64_Sym));
// char *String = (char *)((uintptr_t)Header + StringTable->sh_offset + Symbol->st_name);
char String[256];
lseek(fd, StringTable.sh_offset + Symbol.st_name, SEEK_SET);
fread(fd, (uint8_t *)&String, 256);
fd->seek(StringTable.sh_offset + Symbol.st_name, SEEK_SET);
fd->read((uint8_t *)&String, 256);
if (strcmp(String, Name) == 0)
{
lseek(fd, OldOffset, SEEK_SET);
fd->seek(OldOffset, SEEK_SET);
return Symbol;
}
}
error("Symbol not found.");
lseek(fd, OldOffset, SEEK_SET);
fd->seek(OldOffset, SEEK_SET);
#endif
return {};
}

View File

@ -21,16 +21,16 @@
namespace Execute
{
std::vector<Elf64_Dyn> ELFGetDynamicTag_x86_64(int fd,
std::vector<Elf64_Dyn> ELFGetDynamicTag_x86_64(vfs::RefNode *fd,
DynamicArrayTags Tag)
{
#if defined(a64) || defined(aa64)
off_t OldOffset = lseek(fd, 0, SEEK_CUR);
off_t OldOffset = fd->seek(0, SEEK_CUR);
std::vector<Elf64_Dyn> Ret;
Elf64_Ehdr ELFHeader;
lseek(fd, 0, SEEK_SET);
fread(fd, (uint8_t *)&ELFHeader, sizeof(Elf64_Ehdr));
fd->seek(0, SEEK_SET);
fd->read((uint8_t *)&ELFHeader, sizeof(Elf64_Ehdr));
std::vector<Elf64_Phdr> DYNAMICPhdrs = ELFGetSymbolType_x86_64(fd, PT_DYNAMIC);
@ -45,8 +45,8 @@ namespace Execute
Elf64_Dyn Dynamic;
for (size_t i = 0; i < Phdr.p_filesz / sizeof(Elf64_Dyn); i++)
{
lseek(fd, Phdr.p_offset + (i * sizeof(Elf64_Dyn)), SEEK_SET);
fread(fd, (uint8_t *)&Dynamic, sizeof(Elf64_Dyn));
fd->seek(Phdr.p_offset + (i * sizeof(Elf64_Dyn)), SEEK_SET);
fd->read((uint8_t *)&Dynamic, sizeof(Elf64_Dyn));
if (Dynamic.d_tag != Tag)
continue;
@ -57,7 +57,7 @@ namespace Execute
}
}
lseek(fd, OldOffset, SEEK_SET);
fd->seek(OldOffset, SEEK_SET);
return Ret;
#elif defined(a32)
return {};

View File

@ -21,24 +21,24 @@
namespace Execute
{
std::vector<Elf64_Shdr> ELFGetSections_x86_64(int fd,
std::vector<Elf64_Shdr> ELFGetSections_x86_64(vfs::RefNode *fd,
const char *SectionName)
{
#if defined(a64) || defined(aa64)
off_t OldOffset = lseek(fd, 0, SEEK_CUR);
off_t OldOffset = fd->seek(0, SEEK_CUR);
std::vector<Elf64_Shdr> Ret;
Elf64_Ehdr ELFHeader;
lseek(fd, 0, SEEK_SET);
fread(fd, (uint8_t *)&ELFHeader, sizeof(Elf64_Ehdr));
fd->seek(0, SEEK_SET);
fd->read((uint8_t *)&ELFHeader, sizeof(Elf64_Ehdr));
Elf64_Shdr *SectionHeaders = new Elf64_Shdr[ELFHeader.e_shnum];
lseek(fd, ELFHeader.e_shoff, SEEK_SET);
fread(fd, (uint8_t *)SectionHeaders, sizeof(Elf64_Shdr) * ELFHeader.e_shnum);
fd->seek(ELFHeader.e_shoff, SEEK_SET);
fd->read((uint8_t *)SectionHeaders, sizeof(Elf64_Shdr) * ELFHeader.e_shnum);
char *SectionNames = new char[SectionHeaders[ELFHeader.e_shstrndx].sh_size];
lseek(fd, SectionHeaders[ELFHeader.e_shstrndx].sh_offset, SEEK_SET);
fread(fd, (uint8_t *)SectionNames, SectionHeaders[ELFHeader.e_shstrndx].sh_size);
fd->seek(SectionHeaders[ELFHeader.e_shstrndx].sh_offset, SEEK_SET);
fd->read((uint8_t *)SectionNames, SectionHeaders[ELFHeader.e_shstrndx].sh_size);
for (Elf64_Half i = 0; i < ELFHeader.e_shnum; ++i)
{
@ -47,7 +47,7 @@ namespace Execute
Ret.push_back(SectionHeaders[i]);
}
lseek(fd, OldOffset, SEEK_SET);
fd->seek(OldOffset, SEEK_SET);
delete[] SectionHeaders;
delete[] SectionNames;
return Ret;

View File

@ -21,31 +21,31 @@
namespace Execute
{
std::vector<Elf64_Phdr> ELFGetSymbolType_x86_64(int fd,
std::vector<Elf64_Phdr> ELFGetSymbolType_x86_64(vfs::RefNode *fd,
SegmentTypes Tag)
{
#if defined(a64) || defined(aa64)
off_t OldOffset = lseek(fd, 0, SEEK_CUR);
off_t OldOffset = fd->seek(0, SEEK_CUR);
std::vector<Elf64_Phdr> Ret;
Elf64_Ehdr ELFHeader;
lseek(fd, 0, SEEK_SET);
fread(fd, (uint8_t *)&ELFHeader, sizeof(Elf64_Ehdr));
fd->seek(0, SEEK_SET);
fd->read((uint8_t *)&ELFHeader, sizeof(Elf64_Ehdr));
Elf64_Phdr ProgramHeaders;
lseek(fd, ELFHeader.e_phoff, SEEK_SET);
fread(fd, (uint8_t *)&ProgramHeaders, sizeof(Elf64_Phdr));
fd->seek(ELFHeader.e_phoff, SEEK_SET);
fd->read((uint8_t *)&ProgramHeaders, sizeof(Elf64_Phdr));
for (Elf64_Half i = 0; i < ELFHeader.e_phnum; i++)
{
if (ProgramHeaders.p_type == Tag)
Ret.push_back(ProgramHeaders);
lseek(fd, sizeof(Elf64_Phdr), SEEK_CUR);
fread(fd, (uint8_t *)&ProgramHeaders, sizeof(Elf64_Phdr));
fd->seek(sizeof(Elf64_Phdr), SEEK_CUR);
fd->read((uint8_t *)&ProgramHeaders, sizeof(Elf64_Phdr));
}
lseek(fd, OldOffset, SEEK_SET);
fd->seek(OldOffset, SEEK_SET);
return Ret;
#elif defined(a32)
return {};

View File

@ -35,15 +35,13 @@ namespace Execute
Tasking::TaskCompatibility Compatibility,
bool Critical)
{
int fd = fopen(Path, "r");
if (fd < 0)
return fd;
vfs::RefNode *fd = fs->Open(Path);
if (fd == nullptr)
return -ENOENT;
struct stat statbuf;
fstat(fd, &statbuf);
if (!S_ISREG(statbuf.st_mode))
if (fd->node->Type == vfs::NodeType::DIRECTORY)
{
fclose(fd);
delete fd;
return -EISDIR;
}
@ -55,7 +53,7 @@ namespace Execute
const char *BaseName;
cwk_path_get_basename(Path, &BaseName, nullptr);
Elf32_Ehdr ELFHeader;
fread(fd, (uint8_t *)&ELFHeader, sizeof(Elf32_Ehdr));
fd->read((uint8_t *)&ELFHeader, sizeof(Elf32_Ehdr));
switch (ELFHeader.e_machine)
{
@ -136,7 +134,7 @@ namespace Execute
if (!obj->IsValid)
{
error("Failed to load ELF object");
fclose(fd);
delete fd;
delete Process;
return -ENOEXEC;
}
@ -186,19 +184,19 @@ namespace Execute
Compatibility);
Thread->SetCritical(Critical);
}
fclose(fd);
delete fd;
return Thread->ID;
}
default:
{
debug("Unknown binary type: %d",
GetBinaryType(Path));
fclose(fd);
delete fd;
return -ENOEXEC;
}
}
fclose(fd);
delete fd;
return -ENOEXEC;
}
}

View File

@ -67,31 +67,20 @@ namespace Execute
void *ELFProgramHeaders;
void GenerateAuxiliaryVector_x86_32(Memory::VirtualMemoryArea *vma,
int fd,
Elf32_Ehdr ELFHeader,
vfs::RefNode *fd, Elf32_Ehdr ELFHeader,
uint32_t EntryPoint,
uint32_t BaseAddress);
void GenerateAuxiliaryVector_x86_64(Memory::VirtualMemoryArea *vma,
int fd,
Elf64_Ehdr ELFHeader,
vfs::RefNode *fd, Elf64_Ehdr ELFHeader,
uint64_t EntryPoint,
uint64_t BaseAddress);
void LoadExec_x86_32(int fd,
Tasking::PCB *TargetProcess);
void LoadExec_x86_64(int fd,
Tasking::PCB *TargetProcess);
void LoadDyn_x86_32(int fd,
Tasking::PCB *TargetProcess);
void LoadDyn_x86_64(int fd,
Tasking::PCB *TargetProcess);
bool LoadInterpreter(int fd,
Tasking::PCB *TargetProcess);
void LoadExec_x86_32(vfs::RefNode *fd, Tasking::PCB *TargetProcess);
void LoadExec_x86_64(vfs::RefNode *fd, Tasking::PCB *TargetProcess);
void LoadDyn_x86_32(vfs::RefNode *fd, Tasking::PCB *TargetProcess);
void LoadDyn_x86_64(vfs::RefNode *fd, Tasking::PCB *TargetProcess);
bool LoadInterpreter(vfs::RefNode *fd, Tasking::PCB *TargetProcess);
public:
decltype(IsElfValid) &IsValid = IsElfValid;
@ -111,7 +100,7 @@ namespace Execute
int Spawn(char *Path, const char **argv, const char **envp,
Tasking::PCB *Parent = nullptr, bool Fork = false,
Tasking::TaskCompatibility Compatibility = Tasking::TaskCompatibility::Native,
Tasking::TaskCompatibility Compatibility = Tasking::Native,
bool Critical = false);
bool ELFIs64(void *Header);
@ -120,17 +109,17 @@ namespace Execute
char *GetELFStringTable(Elf64_Ehdr *Header);
char *ELFLookupString(Elf64_Ehdr *Header, uintptr_t Offset);
Elf64_Sym *ELFLookupSymbol(Elf64_Ehdr *Header, const char *Name);
Elf64_Sym ELFLookupSymbol(int fd, const char *Name);
Elf64_Sym ELFLookupSymbol(vfs::RefNode *fd, const char *Name);
uintptr_t ELFGetSymbolValue(Elf64_Ehdr *Header, uint64_t Table, uint64_t Index);
std::vector<Elf64_Phdr> ELFGetSymbolType_x86_64(int fd, SegmentTypes Tag);
std::vector<Elf32_Phdr> ELFGetSymbolType_x86_32(int fd, SegmentTypes Tag);
std::vector<Elf64_Phdr> ELFGetSymbolType_x86_64(vfs::RefNode *fd, SegmentTypes Tag);
std::vector<Elf32_Phdr> ELFGetSymbolType_x86_32(vfs::RefNode *fd, SegmentTypes Tag);
std::vector<Elf64_Shdr> ELFGetSections_x86_64(int fd, const char *SectionName);
std::vector<Elf32_Shdr> ELFGetSections_x86_32(int fd, const char *SectionName);
std::vector<Elf64_Shdr> ELFGetSections_x86_64(vfs::RefNode *fd, const char *SectionName);
std::vector<Elf32_Shdr> ELFGetSections_x86_32(vfs::RefNode *fd, const char *SectionName);
std::vector<Elf64_Dyn> ELFGetDynamicTag_x86_64(int fd, DynamicArrayTags Tag);
std::vector<Elf32_Dyn> ELFGetDynamicTag_x86_32(int fd, DynamicArrayTags Tag);
std::vector<Elf64_Dyn> ELFGetDynamicTag_x86_64(vfs::RefNode *fd, DynamicArrayTags Tag);
std::vector<Elf32_Dyn> ELFGetDynamicTag_x86_32(vfs::RefNode *fd, DynamicArrayTags Tag);
}
#endif // !__FENNIX_KERNEL_FILE_EXECUTE_H__

View File

@ -436,14 +436,4 @@ namespace vfs
};
}
int fopen(const char *pathname, const char *mode);
int creat(const char *pathname, mode_t mode);
ssize_t fread(int fd, void *buf, size_t count);
ssize_t fwrite(int fd, const void *buf, size_t count);
int fclose(int fd);
off_t lseek(int fd, off_t offset, int whence);
int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);
#endif // !__FENNIX_KERNEL_FILESYSTEM_H__

View File

@ -42,16 +42,14 @@ void cmd_cat(const char *args)
return;
}
int fd = fopen(thisNode->FullPath, "r");
struct stat st;
fstat(fd, &st);
vfs::RefNode *fd = fs->Open(thisNode->FullPath);
char *buffer = new char[st.st_size + 1];
ssize_t rBytes = fread(fd, buffer, st.st_size);
uint8_t *buffer = new uint8_t[fd->Size + 1];
ssize_t rBytes = fd->read(buffer, fd->Size);
if (rBytes > 0)
printf("%s\n", buffer);
else
printf("cat: %s: Could not read file\n", args);
delete[] buffer;
fclose(fd);
delete fd;
}

View File

@ -167,11 +167,10 @@ void StartKernelShell()
bool upperCase = false;
bool tabDblPress = false;
int kfd = fopen("/dev/key", "r");
if (kfd < 0)
vfs::RefNode *kfd = fs->Open("/dev/key");
if (kfd == nullptr)
{
KPrint("Failed to open keyboard device! %s",
strerror(kfd));
KPrint("Failed to open keyboard device!");
return;
}
@ -216,7 +215,7 @@ void StartKernelShell()
CurY.store(__cy);
CurHalt.store(false);
nBytes = fread(kfd, scBuf, 2);
nBytes = kfd->read(scBuf, 2);
if (nBytes == 0)
continue;
if (nBytes < 0)

View File

@ -22,12 +22,6 @@
- **user <=> ref_node.cpp**
- Manages the file descriptor table for user processes
<br>
- `kernel_io.cpp`
- **kernel <=> file_descriptor.cpp**
- Performs a similar role as `file_descriptor.cpp` but for kernel processes
### /storage/fs
This directory contains the implementations of various file systems, such as `fat32.cpp` and `ustar.cpp`.

View File

@ -1,130 +0,0 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <filesystem.hpp>
#include <errno.h>
#include "../kernel.h"
using Tasking::PCB;
using vfs::FileDescriptorTable;
static bool CheckForScheduler()
{
if (TaskManager == nullptr)
return false;
return true;
}
int fopen(const char *pathname, const char *mode)
{
if (!CheckForScheduler())
return -ENOSYS;
PCB *pcb = thisProcess;
FileDescriptorTable *fdt = pcb->FileDescriptors;
int fd = fdt->_open(pathname, ConvertFileFlags(mode), 0666);
return fd;
}
int creat(const char *pathname, mode_t mode)
{
if (!CheckForScheduler())
return -ENOSYS;
PCB *pcb = thisProcess;
FileDescriptorTable *fdt = pcb->FileDescriptors;
int fd = fdt->_creat(pathname, mode);
return fd;
}
ssize_t fread(int fd, void *buf, size_t count)
{
if (!CheckForScheduler())
return -ENOSYS;
PCB *pcb = thisProcess;
FileDescriptorTable *fdt = pcb->FileDescriptors;
ssize_t ret = fdt->_read(fd, buf, count);
return ret;
}
ssize_t fwrite(int fd, const void *buf, size_t count)
{
if (!CheckForScheduler())
return -ENOSYS;
PCB *pcb = thisProcess;
FileDescriptorTable *fdt = pcb->FileDescriptors;
ssize_t ret = fdt->_write(fd, buf, count);
return ret;
}
int fclose(int fd)
{
if (!CheckForScheduler())
return -ENOSYS;
PCB *pcb = thisProcess;
FileDescriptorTable *fdt = pcb->FileDescriptors;
int ret = fdt->_close(fd);
return ret;
}
off_t lseek(int fd, off_t offset, int whence)
{
if (!CheckForScheduler())
return -ENOSYS;
PCB *pcb = thisProcess;
FileDescriptorTable *fdt = pcb->FileDescriptors;
off_t ret = fdt->_lseek(fd, offset, whence);
return ret;
}
int stat(const char *pathname, struct stat *statbuf)
{
if (!CheckForScheduler())
return -ENOSYS;
PCB *pcb = thisProcess;
FileDescriptorTable *fdt = pcb->FileDescriptors;
int ret = fdt->_stat(pathname, statbuf);
return ret;
}
int fstat(int fd, struct stat *statbuf)
{
if (!CheckForScheduler())
return -ENOSYS;
PCB *pcb = thisProcess;
FileDescriptorTable *fdt = pcb->FileDescriptors;
int ret = fdt->_fstat(fd, statbuf);
return ret;
}
int lstat(const char *pathname, struct stat *statbuf)
{
if (!CheckForScheduler())
return -ENOSYS;
PCB *pcb = thisProcess;
FileDescriptorTable *fdt = pcb->FileDescriptors;
int ret = fdt->_lstat(pathname, statbuf);
return ret;
}

View File

@ -37,7 +37,7 @@ void lsof()
thisThread->SetPriority(Tasking::High);
fs->Create("/dummy_lsof_file", NodeType::FILE);
fopen("/dummy_lsof_file", "r");
fs->Open("/dummy_lsof_file");
while (true)
{