diff --git a/src/Makefile b/src/Makefile index 43e5fd7..b4062eb 100644 --- a/src/Makefile +++ b/src/Makefile @@ -14,6 +14,7 @@ OBJS = \ userland.o \ launch_process.o \ paging.o \ + elf.o \ myfile.o \ DEPS = $(OBJS:.o=.d) diff --git a/src/elf.c b/src/elf.c new file mode 100644 index 0000000..ec342d9 --- /dev/null +++ b/src/elf.c @@ -0,0 +1,33 @@ +#include "elf.h" + +#include +#include "serial.h" +#include "debug.h" + +int load_elf(char *elf_data_start) +{ + DEBUG_INFO("LOAD ELF: ", elf_data_start); + + struct elf_header *elf_header = (struct elf_header *) elf_data_start; + + DEBUG_INFO("ELF ENTRY POINT: %d", elf_header->entry_point); + DEBUG_INFO("PROGRAM HEADER ENTRY POINT: %d", elf_header->program_header_table_offset); + DEBUG_INFO("NUMBER PROGRAM HEADER ENTRY: %d", elf_header->number_of_program_header_table_entries); + + for (int i = 0; i < elf_header->number_of_program_header_table_entries; i++) + { + struct elf_header_table *elf_header_table = (struct elf_header_table *) (elf_data_start + elf_header->program_header_table_offset + sizeof(struct elf_header_table) * i); + u32 segment_type = elf_header_table->segment_type; + + if (segment_type == LOAD && elf_header_table->p_vaddr < 0x100000) + { + memcpy(elf_header_table->p_vaddr, elf_data_start + elf_header_table->p_offset, elf_header_table->p_filesz); + + if (elf_header_table->p_filesz < elf_header_table->p_memsz) + { + for (int i = elf_header_table->p_filesz; i < elf_header_table->p_memsz; i++) + elf_header_table->p_vaddr[elf_data_start + elf_header_table->p_offset + i] = 0; + } + } + } +} diff --git a/src/elf.h b/src/elf.h new file mode 100644 index 0000000..7d05f5b --- /dev/null +++ b/src/elf.h @@ -0,0 +1,51 @@ +#ifndef ELF_H +#define ELF_H + +#include + +struct elf_header +{ + u32 magic_number; + u8 bus_size; + u8 endianess; + u8 elf_header_version; + u8 os_abi; + u32 unused[2]; + u16 type; + u16 isa; + u32 elf_version; + u32 entry_point; + u32 program_header_table_offset; + u32 section_header_table_offset; + u32 flags; + u16 header_size; + u16 program_header_table_entry_size; + u16 number_of_program_header_table_entries; + u16 size_of_program_header_table_entries; + u16 number_of_section_header_table_entries; + u16 index_section_header_table_with_section_names; +} __attribute__((packed)); + +struct elf_header_table +{ + u32 segment_type; + u32 p_offset; + u32 p_vaddr; + u32 undefined; + u32 p_filesz; + u32 p_memsz; + u32 flags; + u32 alignment; +} __attribute__((packed)); + +enum elf_segment_type { + NULL = 0, + LOAD = 1, + DYNAMIC = 2, + INTERP = 3, + NOTE = 4 +}; + +int load_elf(char *elf_data_start); + +#endif ELF_H diff --git a/src/kernel.c b/src/kernel.c index 711c1b4..3f77bee 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -7,43 +7,7 @@ #include "debug.h" #include "tss.h" #include "launch_process.h" - -struct elf_header -{ - u32 magic_number; - u8 bus_size; - u8 endianess; - u8 elf_header_version; - u8 os_abi; - u32 unused[2]; - u16 type; - u16 isa; - u32 elf_version; - u32 entry_point; - u32 program_header_table_offset; - u32 section_header_table_offset; - u32 flags; - u16 header_size; - u16 program_header_table_entry_size; - u16 number_of_program_header_table_entries; - u16 size_of_program_header_table_entries; - u16 number_of_section_header_table_entries; - u16 index_section_header_table_with_section_names; -} __attribute__((packed)); - -struct elf_header_table -{ - u32 segment_type; - u32 p_offset; - u32 p_vaddr; - u32 undefined; - u32 p_filesz; - u32 p_memsz; - u32 flags; - u32 alignment; -} __attribute__((packed)); - -int read_cdrom(u16 port, bool slave, u32 lba, u32 sectors, u16 *buffer); +#include "elf.h" extern char _binary_a_out_start[]; extern char _binary_a_out_end[]; @@ -51,95 +15,13 @@ extern char _binary_a_out_size[]; void main(void) { + DEBUG_INFO("Entering Main Function"); + char *data_start = &_binary_a_out_start; char *data_end = _binary_a_out_end; size_t data_size = (size_t)_binary_a_out_size; - DEBUG_INFO("data_start: %d", _binary_a_out_start); - DEBUG_INFO("data_start: %d", _binary_a_out_end); - DEBUG_INFO("data_start: %d", _binary_a_out_size); - - // write_serial("\n"); - // write_serial("mem "); - // write_serial_nb(data_start, false); - // write_serial(": "); - // for (char * j = data_start; j < data_start + 0x1005; j++) - // { - // if ((u32) j % 25 == 0) - // { - // write_serial("\n"); - // write_serial("mem "); - // write_serial_nb(j, false); - // write_serial(": "); - // write_serial_nb((u8) *j, false); - // } - // else - // { - // write_serial(": "); - // write_serial_nb((u8) *j, false); - // } - // } - - struct elf_header *elf_header = (struct elf_header *) data_start; - - write_serial("ELF: "); - char *magic_number = (char *) &elf_header->magic_number; - DEBUG_INFO("ELF: %c%c%c%c", magic_number[0], magic_number[1], magic_number[2], magic_number[3]); - DEBUG_INFO("ELF ENTRY POINT: %d", elf_header->entry_point); - DEBUG_INFO("PROGRAM HEADER ENTRY POINT: %d", elf_header->program_header_table_offset); - DEBUG_INFO("NUMBER PROGRAM HEADER ENTRY: %d", elf_header->number_of_program_header_table_entries); - - for (int i = 0; i < elf_header->number_of_program_header_table_entries; i++) - { - struct elf_header_table *elf_header_table = (struct elf_header_table *) (data_start + elf_header->program_header_table_offset + sizeof(struct elf_header_table) * i); - DEBUG_INFO("ELF HEADER TABLE OFFSET: %d, %d", elf_header_table, (u32) elf_header_table - (u32) data_start); - char *segment_type = (char *) &elf_header_table->segment_type; - DEBUG_INFO("ELF HEADER TYPE: %d%d%d%d", segment_type[0], segment_type[1], segment_type[2], segment_type[3]); - if (segment_type[0] == 1 && elf_header_table->p_vaddr < 0x100000) - { - DEBUG_INFO("-------------------"); - DEBUG_INFO("CODE DATA: %d", elf_header_table->p_offset); - DEBUG_INFO("CODE START: %d", elf_header_table->p_vaddr); - DEBUG_INFO("CODE SIZE: %d", elf_header_table->p_filesz); - DEBUG_INFO("ALLOCATION SIZE: %d", elf_header_table->p_memsz); - DEBUG_INFO("CODE START SECTOR: %d", data_start + elf_header_table->p_offset); - memcpy(elf_header_table->p_vaddr, data_start + elf_header_table->p_offset, elf_header_table->p_filesz); - // for (int j = 0; j < 0x22; j++) - // { - // write_serial_nb(*(data_start + 0x1000 + j), false); - // write_serial(" "); - // } - DEBUG_INFO("CODE: %d", *(data_start + elf_header_table->p_offset)); - if (elf_header_table->p_filesz < elf_header_table->p_memsz) - { - for (int i = elf_header_table->p_filesz; i < elf_header_table->p_memsz; i++) - { - elf_header_table->p_vaddr[data_start + elf_header_table->p_offset + i] = 0; - } - } - DEBUG_INFO("-------------------"); - } - } - - u32 *add = 0x30000; - DEBUG_INFO("data_start: %d", *add); - - - // for (int i = 0; i < 100; i++) - // { - // DEBUG_INFO("bin(%d) %d", i, data_start[i]); - // } - - DEBUG_INFO("Entering Main Function"); - - // for (int i = 0; i < 100000; i++) - // { - // u16 bufffer[2048]; - // int read_cdrom_ret = read_cdrom(0x0172, 0, i, 1, bufffer); - - // // DEBUG_INFO("read_cdrom returned %d", read_cdrom_ret); - // DEBUG_INFO("read_cdrom returned %s", bufffer); - // } + load_elf(data_start); launch_process(0x28, 0x30000, 0x20, 0x18, 0x20);