From 9c3e9b3beffde9a71844836b14275d49e8548f6d Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Tue, 28 Mar 2023 17:36:19 +0300 Subject: [PATCH] started work on elf class --- emulator/Core/FsFile.cpp | 90 +++++++++++++++++++++++++++++++ emulator/Core/FsFile.h | 63 ++++++++++++++++++++++ emulator/Loader/Elf.cpp | 19 +++++++ emulator/Loader/Elf.h | 44 +++++++++++++++ emulator/emulator.vcxproj | 4 ++ emulator/emulator.vcxproj.filters | 18 +++++++ emulator/main.cpp | 33 ++---------- 7 files changed, 243 insertions(+), 28 deletions(-) create mode 100644 emulator/Core/FsFile.cpp create mode 100644 emulator/Core/FsFile.h create mode 100644 emulator/Loader/Elf.cpp create mode 100644 emulator/Loader/Elf.h diff --git a/emulator/Core/FsFile.cpp b/emulator/Core/FsFile.cpp new file mode 100644 index 00000000..2ea882f1 --- /dev/null +++ b/emulator/Core/FsFile.cpp @@ -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; +} + diff --git a/emulator/Core/FsFile.h b/emulator/Core/FsFile.h new file mode 100644 index 00000000..dd505df8 --- /dev/null +++ b/emulator/Core/FsFile.h @@ -0,0 +1,63 @@ +#pragma once +#include +#include +#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; + } +}; + diff --git a/emulator/Loader/Elf.cpp b/emulator/Loader/Elf.cpp new file mode 100644 index 00000000..d93b4db0 --- /dev/null +++ b/emulator/Loader/Elf.cpp @@ -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); + +} \ No newline at end of file diff --git a/emulator/Loader/Elf.h b/emulator/Loader/Elf.h new file mode 100644 index 00000000..9c8b15a5 --- /dev/null +++ b/emulator/Loader/Elf.h @@ -0,0 +1,44 @@ +#pragma once +#include +#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; +}; + diff --git a/emulator/emulator.vcxproj b/emulator/emulator.vcxproj index 6806016b..435b1c86 100644 --- a/emulator/emulator.vcxproj +++ b/emulator/emulator.vcxproj @@ -133,9 +133,13 @@ + + + + diff --git a/emulator/emulator.vcxproj.filters b/emulator/emulator.vcxproj.filters index bcb8ec23..52d61484 100644 --- a/emulator/emulator.vcxproj.filters +++ b/emulator/emulator.vcxproj.filters @@ -13,15 +13,33 @@ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + {6c59bbae-9b2e-454e-bfcf-44d9344c23cd} + + + {9c8991bf-e512-441a-97d7-04da356ec70e} + Source Files + + FileFormat + + + Core + Source Files + + FileFormat + + + Core + \ No newline at end of file diff --git a/emulator/main.cpp b/emulator/main.cpp index d7955b4d..098ad2e3 100644 --- a/emulator/main.cpp +++ b/emulator/main.cpp @@ -1,43 +1,17 @@ #include "types.h" #include #include +#include "Loader/Elf.h" #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[]) { - 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"); if (handle == nullptr) @@ -61,6 +35,9 @@ int main(int argc, char* argv[]) return 4; } + Elf* elf = new Elf; + elf->Open(path); + printf("SELF header:\n"); printf(" magic ..............: 0x%08X\n", header.magic); printf(" version .........: %d\n", header.version);