Read all ELF program headers
This commit is contained in:
parent
a0883e95d0
commit
3abf49f881
2 changed files with 47 additions and 34 deletions
|
@ -228,37 +228,42 @@ mov esp, 0x090000
|
|||
|
||||
; parse elf file at ELF_START to KERNEL_START
|
||||
|
||||
; verify ELF header
|
||||
; verify ELF header (scrapped for memory)
|
||||
mov esi, ELF_START
|
||||
cmp dword [esi], 464C457Fh ; ELF magic
|
||||
jne .invalid_elf
|
||||
cmp word [esi+4], 0101h ; lsb 32 bit, little endian
|
||||
jne .invalid_elf
|
||||
cmp word [esi+18], 03 ; x86 architecture
|
||||
jne .invalid_elf
|
||||
; cmp dword [esi], 464C457Fh ; ELF magic
|
||||
; jne .invalid_elf
|
||||
; cmp word [esi+4], 0101h ; lsb 32 bit, little endian
|
||||
; jne .invalid_elf
|
||||
; cmp word [esi+18], 03 ; x86 architecture
|
||||
; jne .invalid_elf
|
||||
|
||||
; read the entrypoint and store it
|
||||
mov eax, dword [esi+0x18] ; program entry position
|
||||
mov dword [.entrypoint], eax
|
||||
|
||||
mov cx, word [esi+0x2C] ; read phnum (number of program headers)
|
||||
mov eax, dword [esi+0x1C] ; read phoff (offset of program header)
|
||||
mov ax, word [esi+0x2C] ; read phnum (number of program headers)
|
||||
|
||||
; ebx is now ELF_START, esi jumps to the start of the program header
|
||||
mov ebx, esi
|
||||
add esi, eax
|
||||
; move esi to the start of the program header
|
||||
add esi, dword [esi+0x1C]
|
||||
|
||||
; set up for loop
|
||||
sub esi, 0x20
|
||||
inc cx
|
||||
inc ax
|
||||
mov dword [.edi_backup], esi
|
||||
|
||||
.elf_ph_loop:
|
||||
mov ebx, ELF_START
|
||||
mov esi, dword [.edi_backup]
|
||||
add esi, 0x20
|
||||
dec cx
|
||||
jz .invalid_elf ; there is no valid code block
|
||||
mov dword [.edi_backup], esi
|
||||
dec ax
|
||||
jz .start_kernel ; there is no valid code block
|
||||
cmp word [esi], 1 ; check if p_type is loadable
|
||||
jne .elf_ph_loop
|
||||
|
||||
; set destination
|
||||
mov edi, dword [esi+0x08]
|
||||
|
||||
; add offset to ebx (ebx = pointer to code)
|
||||
add ebx, dword [esi+0x04]
|
||||
|
||||
|
@ -276,15 +281,20 @@ jz .invalid_elf
|
|||
; set source
|
||||
mov esi, ebx
|
||||
|
||||
; set destination
|
||||
mov edi, KERNEL_START
|
||||
|
||||
; repeat ecx/4 times (because it moves 4 bytes at a time)
|
||||
shr ecx, 2
|
||||
|
||||
; copy
|
||||
repnz movsd
|
||||
|
||||
or ax, ax
|
||||
jnz .elf_ph_loop
|
||||
|
||||
.start_kernel
|
||||
|
||||
cmp edi, KERNEL_START
|
||||
je .invalid_elf
|
||||
|
||||
; jump to start of kernel
|
||||
jmp [.entrypoint]
|
||||
|
||||
|
@ -334,6 +344,7 @@ dw .gdt_end - .gdt - 1
|
|||
dd .gdt
|
||||
|
||||
.entrypoint: dd 0
|
||||
.edi_backup: dd 0
|
||||
|
||||
; magic string
|
||||
dw 0xAA55
|
||||
|
|
|
@ -92,23 +92,25 @@ int ree(char* unused) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int run_command(char* buffer) {
|
||||
// TODO If I make these 2 global, it breaks...
|
||||
// TODO This is ugly, fix this
|
||||
const char* shell_commands_strings[] = {
|
||||
"echo",
|
||||
"hello",
|
||||
"cls",
|
||||
"ree",
|
||||
NULL
|
||||
};
|
||||
// TODO This is ugly, fix this
|
||||
const char* shell_commands_strings[] = {
|
||||
"echo",
|
||||
"hello",
|
||||
"cls",
|
||||
"ree",
|
||||
"getgdt",
|
||||
NULL
|
||||
};
|
||||
|
||||
int (*shell_commands_functions[]) (char*) = {
|
||||
echo,
|
||||
hello,
|
||||
cls,
|
||||
ree
|
||||
};
|
||||
int (*shell_commands_functions[]) (char*) = {
|
||||
echo,
|
||||
hello,
|
||||
cls,
|
||||
ree,
|
||||
get_gdt
|
||||
};
|
||||
|
||||
int run_command(char* buffer) {
|
||||
|
||||
if(buffer[0] == 0) {
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue