started work on elf class

This commit is contained in:
georgemoralis 2023-03-28 17:36:19 +03:00
parent 1290756b3d
commit 9c3e9b3bef
7 changed files with 243 additions and 28 deletions

90
emulator/Core/FsFile.cpp Normal file
View file

@ -0,0 +1,90 @@
#include "FsFile.h"
FsFile::FsFile()
{
m_file = nullptr;
}
FsFile::FsFile(const std::string& path, fsOpenMode mode)
{
Open(path, mode);
}
bool FsFile::Open(const std::string& path, fsOpenMode mode)
{
Close();
fopen_s(&m_file, path.c_str(), getOpenMode(mode));
return IsOpen();
}
bool FsFile::Close()
{
if (!IsOpen() || std::fclose(m_file) != 0) {
m_file = nullptr;
return false;
}
m_file = nullptr;
return true;
}
FsFile::~FsFile()
{
Close();
}
bool FsFile::Write(const void* src, u64 size)
{
if (!IsOpen() || std::fwrite(src, 1, size, m_file) != size) {
return false;
}
return true;
}
bool FsFile::Read(void* dst, u64 size)
{
if (!IsOpen() || std::fread(dst, 1, size, m_file) != size) {
return false;
}
return true;
}
u32 FsFile::ReadBytes(void* dst, u64 size)
{
return std::fread(dst, 1, size, m_file);
}
bool FsFile::Seek(s64 offset, fsSeekMode mode)
{
if (!IsOpen() || _fseeki64(m_file, offset, getSeekMode(mode)) != 0) {
return false;
}
return true;
}
u64 FsFile::Tell() const
{
if (IsOpen()) {
return _ftelli64(m_file);
}
else {
return -1;
}
}
u64 FsFile::getFileSize()
{
u64 pos = _ftelli64(m_file);
if (_fseeki64(m_file, 0, SEEK_END) != 0) {
return 0;
}
u64 size = _ftelli64(m_file);
if (_fseeki64(m_file, pos, SEEK_SET) != 0) {
return 0;
}
return size;
}
bool FsFile::IsOpen() const
{
return m_file != nullptr;
}

63
emulator/Core/FsFile.h Normal file
View file

@ -0,0 +1,63 @@
#pragma once
#include <cstdio>
#include <string>
#include "../Types.h"
enum fsOpenMode
{
fsRead = 0x1,
fsWrite = 0x2,
fsReadWrite = fsRead | fsWrite
};
enum fsSeekMode
{
fsSeekSet,
fsSeekCur,
fsSeekEnd,
};
class FsFile
{
std::FILE* m_file;
public:
FsFile();
FsFile(const std::string& path, fsOpenMode mode = fsRead);
bool Open(const std::string& path, fsOpenMode mode = fsRead);
bool IsOpen() const;
bool Close();
bool Read(void* dst, u64 size);
u32 ReadBytes(void* dst, u64 size);
bool Write(const void* src, u64 size);
bool Seek(s64 offset, fsSeekMode mode);
u64 getFileSize();
u64 Tell() const;
~FsFile();
const char* getOpenMode(fsOpenMode mode)
{
switch (mode) {
case fsRead: return "rb";
case fsWrite: return "wb";
case fsReadWrite: return "r+b";
}
return "r";
}
const int getSeekMode(fsSeekMode mode)
{
switch (mode) {
case fsSeekSet: return SEEK_SET;
case fsSeekCur: return SEEK_CUR;
case fsSeekEnd: return SEEK_END;
}
return SEEK_SET;
}
std::FILE* fileDescr()
{
return m_file;
}
};

19
emulator/Loader/Elf.cpp Normal file
View file

@ -0,0 +1,19 @@
#include "Elf.h"
static self_header* load_self(FsFile& f)
{
//read self header
auto* self = new self_header;
f.Read(self, sizeof(self_header));
return self;
}
void Elf::Open(const std::string& file_name)
{
m_f = new FsFile;
m_f->Open(file_name, fsOpenMode::fsRead);
m_self = load_self(*m_f);
}

44
emulator/Loader/Elf.h Normal file
View file

@ -0,0 +1,44 @@
#pragma once
#include <string>
#include "../types.h"
#include "../Core/FsFile.h"
struct self_header
{
static const u32 signature = 0x1D3D154Fu;
u32 magic;
u08 version;
u08 mode;
u08 endian;// 1 is little endian
u08 attributes;
u08 category;
u08 program_type;
u16 padding1;
u16 header_size;
u16 meta_size;
u32 file_size;
u32 padding2;
u16 segment_count;
u16 unknown1A; //always 0x22
u32 padding3;
};
struct self_segment_header
{
u64 flags;
u64 offset;
u64 encrypted_compressed_size;
u64 decrypted_decompressed_size;
};
class Elf
{
public:
void Open(const std::string & file_name);
private:
FsFile* m_f = nullptr;
self_header* m_self = nullptr;
};

View file

@ -133,9 +133,13 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Core\FsFile.cpp" />
<ClCompile Include="Loader\Elf.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Core\FsFile.h" />
<ClInclude Include="Loader\Elf.h" />
<ClInclude Include="types.h" /> <ClInclude Include="types.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View file

@ -13,15 +13,33 @@
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter> </Filter>
<Filter Include="FileFormat">
<UniqueIdentifier>{6c59bbae-9b2e-454e-bfcf-44d9344c23cd}</UniqueIdentifier>
</Filter>
<Filter Include="Core">
<UniqueIdentifier>{9c8991bf-e512-441a-97d7-04da356ec70e}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Loader\Elf.cpp">
<Filter>FileFormat</Filter>
</ClCompile>
<ClCompile Include="Core\FsFile.cpp">
<Filter>Core</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="types.h"> <ClInclude Include="types.h">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Loader\Elf.h">
<Filter>FileFormat</Filter>
</ClInclude>
<ClInclude Include="Core\FsFile.h">
<Filter>Core</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -1,43 +1,17 @@
#include "types.h" #include "types.h"
#include <stdio.h> #include <stdio.h>
#include <corecrt_malloc.h> #include <corecrt_malloc.h>
#include "Loader/Elf.h"
#pragma warning(disable:4996) #pragma warning(disable:4996)
struct self_header
{
static const u32 signature = 0x1D3D154Fu;
u32 magic;
u08 version;
u08 mode;
u08 endian;// 1 is little endian
u08 attributes;
u08 category;
u08 program_type;
u16 padding1;
u16 header_size;
u16 meta_size;
u32 file_size;
u32 padding2;
u16 segment_count;
u16 unknown1A; //always 0x22
u32 padding3;
};
struct self_segment_header
{
u64 flags;
u64 offset;
u64 encrypted_compressed_size;
u64 decrypted_decompressed_size;
};
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
const char* const path = argv[1]; //argument 1 is the path of self file to boot const char* const path = "eboot.bin";//argv[1]; //argument 1 is the path of self file to boot
auto handle = fopen(path, "rb"); auto handle = fopen(path, "rb");
if (handle == nullptr) if (handle == nullptr)
@ -61,6 +35,9 @@ int main(int argc, char* argv[])
return 4; return 4;
} }
Elf* elf = new Elf;
elf->Open(path);
printf("SELF header:\n"); printf("SELF header:\n");
printf(" magic ..............: 0x%08X\n", header.magic); printf(" magic ..............: 0x%08X\n", header.magic);
printf(" version .........: %d\n", header.version); printf(" version .........: %d\n", header.version);