diff --git a/emulator/Loader/Elf.cpp b/emulator/Loader/Elf.cpp index d93b4db0..e6651faf 100644 --- a/emulator/Loader/Elf.cpp +++ b/emulator/Loader/Elf.cpp @@ -15,5 +15,66 @@ void Elf::Open(const std::string& file_name) m_f->Open(file_name, fsOpenMode::fsRead); m_self = load_self(*m_f); + if (!isSelfFile()) + { + delete m_self; + m_self = nullptr; + m_f->Seek(0,fsSeekMode::fsSeekSet); //it is not an self file move to the start of file + } + + DebugDump(); +} + +bool Elf::isSelfFile() const +{ + if (m_f == nullptr) + { + return false; + } + + if (m_self == nullptr) + { + return false;//if we can't load self header return false + } + + if (m_self->magic != self_header::signature) + { + printf("Not a SELF file. Magic file mismatched! current = 0x%08X required = 0x%08X\n", m_self->magic, self_header::signature); + return false; + } + + if (m_self->version != 0x00 || m_self->mode != 0x01 || m_self->endian != 0x01 || m_self->attributes != 0x12) + { + printf("Unknown SELF file\n"); + return false; + } + + if (m_self->category != 0x01 || m_self->program_type != 0x01) + { + printf("Unknown SELF file\n"); + return false; + } + + return true; +} + +void Elf::DebugDump() { + printf("SELF header:\n"); + printf(" magic ..............: 0x%08X\n", m_self->magic); + printf(" version .........: %d\n", m_self->version); + printf(" mode .........: 0x%X\n", m_self->mode); + printf(" endian .........: %d\n", m_self->endian); + printf(" attributes .........: 0x%X\n", m_self->attributes); + printf(" category .........: 0x%X\n", m_self->category); + printf(" program_type........: 0x%X\n", m_self->program_type); + printf(" padding1 ...........: 0x%04X\n", m_self->padding1); + printf(" header size ........: 0x%X\n", m_self->header_size); + printf(" meta size .....: 0x%X\n", m_self->meta_size); + printf(" file size ..........: 0x%X\n", m_self->file_size); + printf(" padding2 ...........: 0x%08X\n", m_self->padding2); + printf(" segment count ......: %u\n", m_self->segment_count); + printf(" unknown 1A .........: 0x%04X\n", m_self->unknown1A); + printf(" padding3 ...........: 0x%04X\n", m_self->padding3); + printf("\n"); } \ No newline at end of file diff --git a/emulator/Loader/Elf.h b/emulator/Loader/Elf.h index 9c8b15a5..82074cd4 100644 --- a/emulator/Loader/Elf.h +++ b/emulator/Loader/Elf.h @@ -36,7 +36,8 @@ class Elf { public: void Open(const std::string & file_name); - + bool isSelfFile() const; + void DebugDump(); private: FsFile* m_f = nullptr; self_header* m_self = nullptr; diff --git a/emulator/main.cpp b/emulator/main.cpp index 098ad2e3..6369605b 100644 --- a/emulator/main.cpp +++ b/emulator/main.cpp @@ -11,7 +11,7 @@ int main(int argc, char* argv[]) { - const char* const path = "eboot.bin";//argv[1]; //argument 1 is the path of self file to boot + const char* const path = argv[1]; //argument 1 is the path of self file to boot auto handle = fopen(path, "rb"); if (handle == nullptr) @@ -28,32 +28,12 @@ int main(int argc, char* argv[]) return 3; } - if (header.magic != self_header::signature) - { - printf("Not a SELF file.\n"); - fclose(handle); - 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); - printf(" mode .........: 0x%X\n", header.mode); - printf(" endian .........: %d\n", header.endian); - printf(" attributes .........: 0x%X\n", header.attributes); - printf(" category .........: 0x%X\n", header.category); - printf(" program_type........: 0x%X\n", header.program_type); - printf(" header size ........: 0x%X\n", header.header_size); - printf(" meta size .....: 0x%X\n", header.meta_size); - printf(" file size ..........: 0x%X\n", header.file_size); - printf(" padding2 ...........: 0x%08X\n", header.padding2); - printf(" segment count ......: %u\n", header.segment_count); - printf(" unknown 1A .........: 0x%04X\n", header.unknown1A); - printf(" padding3 ...........: 0x%04X\n", header.padding3); - printf("\n"); + auto segment_headers = (self_segment_header*)malloc(sizeof(self_segment_header) * header.segment_count); if (fread(segment_headers, sizeof(self_segment_header), header.segment_count, handle) != header.segment_count)