load segment headers

This commit is contained in:
georgemoralis 2023-03-30 17:38:37 +03:00
parent 76cbb5eb11
commit be996892a9
3 changed files with 33 additions and 48 deletions

View File

@ -9,6 +9,16 @@ static self_header* load_self(FsFile& f)
return self;
}
static self_segment_header* load_self_segments(FsFile& f, u16 num)
{
auto* segs = new self_segment_header[num];
f.Read(segs, sizeof(self_segment_header) * num);
return segs;
}
void Elf::Open(const std::string& file_name)
{
m_f = new FsFile;
@ -21,7 +31,10 @@ void Elf::Open(const std::string& file_name)
m_self = nullptr;
m_f->Seek(0,fsSeekMode::fsSeekSet); //it is not an self file move to the start of file
}
else
{
m_self_segments = load_self_segments(*m_f, m_self->segment_count);
}
DebugDump();
}
@ -70,11 +83,24 @@ void Elf::DebugDump() {
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(" meta size .....: %u\n", m_self->meta_size);
printf(" file size ..........: %u\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");
printf("SELF segments:\n");
for (int i = 0; i < m_self->segment_count; i++)
{
auto segment_header = m_self_segments[i];
printf(" [%d]\n", i);
printf(" flags ............: 0x%llx\n", segment_header.flags);
printf(" file offset ......: 0x%llx\n", segment_header.file_offset);
printf(" file size ........: %llu\n", segment_header.file_size);
printf(" memory size ......: %llu\n", segment_header.memory_size);
}
printf("\n");
}

View File

@ -27,9 +27,9 @@ struct self_header
struct self_segment_header
{
u64 flags;
u64 offset;
u64 encrypted_compressed_size;
u64 decrypted_decompressed_size;
u64 file_offset;
u64 file_size;
u64 memory_size;
};
class Elf
@ -41,5 +41,6 @@ public:
private:
FsFile* m_f = nullptr;
self_header* m_self = nullptr;
self_segment_header* m_self_segments = nullptr;
};

View File

@ -12,49 +12,7 @@
int main(int argc, char* argv[])
{
const char* const path = argv[1]; //argument 1 is the path of self file to boot
auto handle = fopen(path, "rb");
if (handle == nullptr)
{
printf("Failed to open file.\n");
return 2;
}
self_header header;
if (fread(&header, sizeof(self_header), 1, handle) != 1)
{
printf("Failed to read SELF header.\n");
fclose(handle);
return 3;
}
Elf* elf = new Elf;
elf->Open(path);
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)
{
printf("Failed to read SELF segment headers.\n");
free(segment_headers);
fclose(handle);
return 5;
}
printf("SELF segments:\n");
for (int i = 0; i < header.segment_count; i++)
{
auto segment_header = segment_headers[i];
printf(" [%d]\n", i);
printf(" flags ............: %llx\n", segment_header.flags);
printf(" offset ...........: %llx\n", segment_header.offset);
printf(" compressed size ..: %llx\n", segment_header.encrypted_compressed_size);
printf(" uncompressed size : %llx\n", segment_header.decrypted_decompressed_size);
}
printf("\n");
}