Read all ELF program headers

This commit is contained in:
Robbe Van Herck 2020-01-10 09:28:31 +01:00
parent a0883e95d0
commit 3abf49f881
No known key found for this signature in database
GPG key ID: A66F76F7B81BD784
2 changed files with 47 additions and 34 deletions

View file

@ -228,37 +228,42 @@ mov esp, 0x090000
; parse elf file at ELF_START to KERNEL_START ; parse elf file at ELF_START to KERNEL_START
; verify ELF header ; verify ELF header (scrapped for memory)
mov esi, ELF_START mov esi, ELF_START
cmp dword [esi], 464C457Fh ; ELF magic ; cmp dword [esi], 464C457Fh ; ELF magic
jne .invalid_elf ; jne .invalid_elf
cmp word [esi+4], 0101h ; lsb 32 bit, little endian ; cmp word [esi+4], 0101h ; lsb 32 bit, little endian
jne .invalid_elf ; jne .invalid_elf
cmp word [esi+18], 03 ; x86 architecture ; cmp word [esi+18], 03 ; x86 architecture
jne .invalid_elf ; jne .invalid_elf
; read the entrypoint and store it ; read the entrypoint and store it
mov eax, dword [esi+0x18] ; program entry position mov eax, dword [esi+0x18] ; program entry position
mov dword [.entrypoint], eax mov dword [.entrypoint], eax
mov cx, word [esi+0x2C] ; read phnum (number of program headers) mov ax, word [esi+0x2C] ; read phnum (number of program headers)
mov eax, dword [esi+0x1C] ; read phoff (offset of program header)
; ebx is now ELF_START, esi jumps to the start of the program header ; move esi to the start of the program header
mov ebx, esi add esi, dword [esi+0x1C]
add esi, eax
; set up for loop ; set up for loop
sub esi, 0x20 sub esi, 0x20
inc cx inc ax
mov dword [.edi_backup], esi
.elf_ph_loop: .elf_ph_loop:
mov ebx, ELF_START
mov esi, dword [.edi_backup]
add esi, 0x20 add esi, 0x20
dec cx mov dword [.edi_backup], esi
jz .invalid_elf ; there is no valid code block dec ax
jz .start_kernel ; there is no valid code block
cmp word [esi], 1 ; check if p_type is loadable cmp word [esi], 1 ; check if p_type is loadable
jne .elf_ph_loop jne .elf_ph_loop
; set destination
mov edi, dword [esi+0x08]
; add offset to ebx (ebx = pointer to code) ; add offset to ebx (ebx = pointer to code)
add ebx, dword [esi+0x04] add ebx, dword [esi+0x04]
@ -276,15 +281,20 @@ jz .invalid_elf
; set source ; set source
mov esi, ebx mov esi, ebx
; set destination
mov edi, KERNEL_START
; repeat ecx/4 times (because it moves 4 bytes at a time) ; repeat ecx/4 times (because it moves 4 bytes at a time)
shr ecx, 2 shr ecx, 2
; copy ; copy
repnz movsd repnz movsd
or ax, ax
jnz .elf_ph_loop
.start_kernel
cmp edi, KERNEL_START
je .invalid_elf
; jump to start of kernel ; jump to start of kernel
jmp [.entrypoint] jmp [.entrypoint]
@ -334,6 +344,7 @@ dw .gdt_end - .gdt - 1
dd .gdt dd .gdt
.entrypoint: dd 0 .entrypoint: dd 0
.edi_backup: dd 0
; magic string ; magic string
dw 0xAA55 dw 0xAA55

View file

@ -92,14 +92,13 @@ int ree(char* unused) {
return 0; return 0;
} }
int run_command(char* buffer) {
// TODO If I make these 2 global, it breaks...
// TODO This is ugly, fix this // TODO This is ugly, fix this
const char* shell_commands_strings[] = { const char* shell_commands_strings[] = {
"echo", "echo",
"hello", "hello",
"cls", "cls",
"ree", "ree",
"getgdt",
NULL NULL
}; };
@ -107,9 +106,12 @@ int run_command(char* buffer) {
echo, echo,
hello, hello,
cls, cls,
ree ree,
get_gdt
}; };
int run_command(char* buffer) {
if(buffer[0] == 0) { if(buffer[0] == 0) {
return 0; return 0;
} }