diff options
-rwxr-xr-x | run | 39 | ||||
-rw-r--r-- | src/bootloader/cd.asm | 443 | ||||
-rw-r--r-- | src/bootloader/grub.cfg | 6 | ||||
-rw-r--r-- | src/bootloader/hdd1.asm | 118 | ||||
-rw-r--r-- | src/bootloader/hdd2.asm | 388 | ||||
-rw-r--r-- | src/kernel/acpi/acpi.c | 8 | ||||
-rw-r--r-- | src/kernel/boot.asm | 16 | ||||
-rw-r--r-- | src/kernel/fs/load.c | 8 | ||||
-rw-r--r-- | src/kernel/fs/load.h | 1 | ||||
-rw-r--r-- | src/kernel/kernel.c | 16 | ||||
-rw-r--r-- | src/kernel/lib/lib.h | 5 | ||||
-rw-r--r-- | src/kernel/lib/memory.c | 38 | ||||
-rw-r--r-- | src/kernel/linker.ld | 1 | ||||
-rw-r--r-- | src/kernel/memory/paging.c | 2 | ||||
-rw-r--r-- | src/kernel/multiboot.h | 216 | ||||
-rw-r--r-- | src/kernel/system.c | 1 | ||||
-rw-r--r-- | src/kernel/system.h | 6 | ||||
-rwxr-xr-x | src/resources/conv.sh | 2 | ||||
-rw-r--r-- | src/resources/font.c | 2 |
19 files changed, 310 insertions, 1006 deletions
@@ -111,43 +111,42 @@ make_build() { compile_with_flags -O2 -c ./"${line}" -I ./src/userspace -o ./build/userspace/"${stripped}" done <./build/tmp rm ./build/tmp - compile_with_flags -emain -O2 ./build/userspace/*.o -T ./src/userspace/linker.ld -I ./src/userspace -o ./build/user.bin + compile_with_flags -emain -O2 ./build/userspace/*.o -I ./src/userspace -o ./build/user.bin # Create ISO - mkdir -p ./iso/boot/ - mv ./build/melvix.bin ./iso/boot/kernel.bin - nasm ./src/bootloader/cd.asm -f bin -o ./iso/boot/cd.bin || exit 1 - nasm ./src/bootloader/hdd1.asm -f bin -o ./iso/boot/hdd1.bin || exit 1 - nasm ./src/bootloader/hdd2.asm -f bin -o ./iso/boot/hdd2.bin || exit 1 - cp ./build/user.bin ./iso/user.bin || exit 1 - cp ./build/font.bin ./iso/font.bin || exit 1 - genisoimage -quiet -input-charset utf-8 -no-emul-boot -b boot/cd.bin -o ./build/melvix.iso ./iso + mkdir -p ./iso/boot/grub/ + cp ./build/melvix.bin ./iso/boot/kernel.bin + cp ./src/bootloader/grub.cfg ./iso/boot/grub/ + cp ./build/user.bin ./iso/user.bin + cp ./build/font.bin ./iso/font.bin + grub-mkrescue -o ./build/melvix.iso ./iso/ head -c 10485760 /dev/zero >./build/hdd10M.img printf "Build finshed successfully!\n\n" } make_test() { - qemu_with_flags -cdrom ./build/melvix.iso -drive file=./build/hdd10M.img,format=raw + qemu_with_flags -cdrom ./build/melvix.iso #-drive file=./build/hdd10M.img,format=raw } make_debug() { - qemu_with_flags -s -cdrom ./build/melvix.iso -drive file=./build/hdd10M.img,format=raw + qemu_with_flags -s -cdrom ./build/melvix.iso #-drive file=./build/hdd10M.img,format=raw } make_image_debug() { - qemu_with_flags -cdrom ./build/melvix.iso -drive file=./build/hdd10M.img,format=raw - qemu_with_flags -drive file=./build/hdd10M.img,format=raw + qemu_with_flags -cdrom ./build/melvix.iso #-drive file=./build/hdd10M.img,format=raw + #qemu_with_flags -drive file=./build/hdd10M.img,format=raw } make_image() { - start=$(date +%s.%N) - qemu_with_flags -nographic -cdrom ./build/melvix.iso -drive file=./build/hdd10M.img,format=raw >install.log - end=$(date +%s.%N) - cat install.log - printf "[LOG END]\n\n" - tail install.log | grep -q "Installation successful!" && echo Booted and installed within "$(echo "$end - $start" | bc -l)" seconds || echo Installation failed! && exit 1 - rm install.log + #start=$(date +%s.%N) + #qemu_with_flags -nographic -cdrom ./build/melvix.iso -drive file=./build/hdd10M.img,format=raw >install.log + #end=$(date +%s.%N) + #cat install.log + #printf "[LOG END]\n\n" + #tail install.log | grep -q "Installation successful!" && echo Booted and installed within "$(echo "$end - $start" | bc -l)" seconds || echo Installation failed! && exit 1 + #rm install.log + echo "This test is currently disabled" } make_sync() { diff --git a/src/bootloader/cd.asm b/src/bootloader/cd.asm deleted file mode 100644 index 1661df1..0000000 --- a/src/bootloader/cd.asm +++ /dev/null @@ -1,443 +0,0 @@ -BITS 16 -ORG 0x7C00 - -; Save the boot drive id -mov [bootDriveID], dl - -jmp start - -print: - mov ah, 0x0E - xor bh, bh - .print_L: - lodsb - test al, al - jz .print_end - int 0x10 - jmp .print_L - .print_end: - ret - -bootDriveID: db 0 - -dapack: - dapack_size: db 0x10 - dapack_null: db 0x00 - dapack_blkcount: dw 0x0001 - dapack_boffset: dw 0x9000 - dapack_bsegment: dw 0x0000 - dapack_start: dd 0x00000000 - dapack_upper_lba_bits: dd 0x00000000 - -readsector: - mov dword [dapack_start], eax - mov ah, 0x42 - mov dl, [bootDriveID] - xor bx, bx - mov ds, bx - mov si, dapack - int 0x13 - ret - -filenameLength dw 1 -filename dw 1 - -findfile: - mov bx, [esp+2] - mov ax, [esp+4] - mov [filenameLength], ax - mov ax, [esp+6] - mov [filename], ax - - .findfile_L: - mov al, [bx] - - test al, al - jz .findfile_notfound - - mov ah, [bx+32] - cmp ah, byte [filenameLength] - jnz .findfile_keep - - call findfile_check - test ax, ax - jz findfile_found - - .findfile_keep: - mov al, [bx] - xor ah, ah - add bx, ax - jmp .findfile_L - - .findfile_notfound: - xor ax, ax - inc ax - ret - - findfile_check: - pusha - add bx, 33 - mov ax, bx - - xor cx, cx - .findfile_check_L: - mov bx, [filename] - add bx, cx - mov dh, [bx] - mov bx, ax - add bx, cx - mov dl, [bx] - cmp dh, dl - jnz .findfile_check_fail - - inc cx - cmp cx, word [filenameLength] - jz .findfile_check_success - jmp .findfile_check_L - - .findfile_check_fail: - popa - xor ax, ax - inc ax - ret - .findfile_check_success: - popa - xor ax, ax - ret - - findfile_found: - xor ax, ax - ret - -LOAD_PVD: - mov eax, 0x10 - PVD_L: - call readsector - mov bx, [dapack_boffset] - mov bl, [bx] - cmp bl, 0x01 - jz PVD_FOUND - inc eax - jmp PVD_L - PVD_FOUND: - ret - -checkA20: - ; Stolen from https://wiki.osdev.org/A20_Line - pushf - push ds - push es - push di - push si - cli - xor ax, ax - mov es, ax - not ax - mov ds, ax - mov di, 0x0500 - mov si, 0x0510 - mov al, byte [es:di] - push ax - mov al, byte [ds:si] - push ax - mov byte [es:di], 0x00 - mov byte [ds:si], 0xFF - cmp byte [es:di], 0xFF - pop ax - mov byte [ds:si], al - pop ax - mov byte [es:di], al - mov ax, 0 - jz checkA20_exit - mov ax, 1 - checkA20_exit: - pop si - pop di - pop es - pop ds - popf - ret - -welcome db "Melvix", 0x0A, 0x0D, 0x00 -nolba db "BIOS lacks support for lba addressing.", 0x00 -noboot db "Boot directory could not be found.", 0x00 -noa20 db "A20 could not be enabled.", 0 -loading db "Loading kernel...", 0x0A, 0x0D, 0x00 -nokernel db "kernel.bin could not be found!", 0 -booting db "Booting...", 0x0A, 0x0D, 0x00 -nomem db "BIOS does not support memory detection!", 0 -memno20 db "BIOS returns memory detection with 24 bytes. This has never been seen!", 0 - -start: - ; Check if lba is supported by the BIOS. - mov ah, 0x41 - mov bx, 0x55AA - int 0x13 - jc lba_not_supported - cmp bx, 0xAA55 - jnz lba_not_supported - - call checkA20 - test ax, ax - jnz A20_ENABLED - - in al, 0x92 - or al, 2 - out 0x92, al - - call checkA20 - test ax, ax - jnz A20_ENABLED - - mov si, noa20 - call print - jmp $ - -A20_ENABLED: - ; Inspired by https://wiki.osdev.org/Unreal_Mode - cli - push ds - lgdt [gdtinfo] - - mov eax, cr0 - or al, 1 - mov cr0, eax - - jmp $+2 - - mov bx, 0x08 - mov ds, bx - - and al, 0xFE - mov cr0, eax - pop ds - - mov si, loading - call print - - call LOAD_PVD - - mov bx, 0x9000 - add bx, 156 - add bx, 2 - mov eax, dword [bx] - call readsector - - push boot - push boot_len - push 0x9000 - call findfile - add esp, 6 - test ax, ax - jz continue_BOOT - - mov si, noboot - call print - jmp $ - -continue_BOOT: - mov word [dapack_blkcount], 0x0001 - add bx, 2 - mov eax, [bx] - call readsector - - push kernelbin - push kernelbin_len - push 0x9000 - call findfile - add esp, 6 - test ax, ax - jz continue_KERNEL - - mov si, nokernel - call print - jmp $ - -continue_KERNEL: - mov word [dapack_blkcount], 1 - mov word [dapack_boffset], 0x9000 - - push bx - add bx, 10 - mov eax, [bx] - xor edx, edx - mov ebx, 2048 - div ebx - inc eax - pop bx - push eax - - add bx, 2 - mov eax, [bx] - push eax - - ; Offsetless count. - xor ecx, ecx -LOAD_KERNEL: - mov eax, [esp] - add eax, ecx - call readsector - - mov eax, ecx - shl eax, 11 - add eax, 0x200000 - - push ecx - xor ecx, ecx - .LOAD_KERNEL_L: - mov ebx, ecx - add ebx, 0x9000 - mov ebx, dword [ebx] - - mov edx, ebx - mov ebx, eax - add ebx, ecx - mov dword [ebx], edx - - add ecx, 4 - cmp ecx, 2048 - jl .LOAD_KERNEL_L - pop ecx - - inc ecx - cmp ecx, dword [esp+4] - jl LOAD_KERNEL - -mov si, booting -call print - -mov dl, [bootDriveID] -mov byte [0x9000], dl - -mov di, 0xA000 -mov eax, 0xE820 -xor ebx, ebx -mov ecx, 24 -mov edx, 0x534D4150 -int 0x15 - -jc BIOS_NO_MEM -cmp eax, 0x534D4150 -jnz BIOS_NO_MEM -cmp cl, 20 -jnz BIOS_MEM_NO20 - -MEM_L: - test ebx, ebx - jz MEM_FINISHED - - mov ax, di - xor ch, ch - add ax, cx - mov di, ax - - mov eax, 0xE820 - mov ecx, 24 - int 0x15 - jmp MEM_L - -MEM_FINISHED: - mov eax, cr0 - or al, 1 - mov cr0, eax - - mov ax, 0x08 - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov ss, ax - -; Infomation: https://wiki.osdev.org/ELF -mov ebx, 0x20002C -mov dl, byte [ebx] -push dx - -mov ebx, 0x20001C -mov ebx, dword [ebx] -push ebx - -xor dh, dh -PHT: - xor eax, eax - mov al, dh - shl eax, 5 - add eax, dword [esp] - add eax, 0x200000 - mov ebx, eax - - mov eax, dword [ebx] - cmp eax, 1 - jnz .PHT_ignore - - add ebx, 4 - mov eax, dword [ebx] - add eax, 0x200000 - push eax - - add ebx, 4 - mov eax, dword [ebx] - push eax - - add ebx, 8 - mov eax, dword [ebx] - push eax - - push dx - xor ecx, ecx - .MOVE_KERNEL_L: - mov ebx, [esp+10] - add ebx, ecx - mov ebx, dword [ebx] - - mov eax, ebx - mov ebx, dword [esp+6] - add ebx, ecx - mov dword [ebx], eax - - add ecx, 4 - cmp ecx, dword [esp+2] - jl .MOVE_KERNEL_L - pop dx - add esp, 12 - - .PHT_ignore: - inc dh - cmp dh, dl - jl PHT - -jmp (codedesc - gdt):protectedMode - -protectedMode: - BITS 32 - mov ebx, 0x100000 - mov eax, dword [ebx] - jmp eax - -BITS 16 -BIOS_NO_MEM: - mov si, nomem - call print - jmp $ -BIOS_MEM_NO20: - mov si, memno20 - call print - jmp $ - -gdtinfo: - dw gdt_end - gdt - 1 - dd gdt - gdt dd 0, 0 - flatdesc db 0xff, 0xff, 0, 0, 0, 10010010b, 11001111b, 0 - codedesc db 0xff, 0xff, 0, 0, 0, 10011010b, 11001111b, 0 - gdt_end: - -lba_not_supported: - mov si, nolba - call print - jmp $ - -boot db "BOOT" -boot_len equ ($ - boot) -kernelbin db "KERNEL.BIN", 0x3B, "1" -kernelbin_len equ ($ - kernelbin)
\ No newline at end of file diff --git a/src/bootloader/grub.cfg b/src/bootloader/grub.cfg new file mode 100644 index 0000000..227030e --- /dev/null +++ b/src/bootloader/grub.cfg @@ -0,0 +1,6 @@ +set timeout=0 +set default=0 +menuentry "Melvix" { + multiboot /boot/kernel.bin + boot +}
\ No newline at end of file diff --git a/src/bootloader/hdd1.asm b/src/bootloader/hdd1.asm deleted file mode 100644 index 514deca..0000000 --- a/src/bootloader/hdd1.asm +++ /dev/null @@ -1,118 +0,0 @@ -BITS 16 -ORG 0x7C00 - -%define INODE_ADDRESS 0xF000 -%define INODE_NBLOCKS 20 -%define INODE_DBP0 24 - -; Save the boot drive id -mov [bootDriveID], dl - -jmp start - -print: - mov ah, 0x0E - xor bh, bh - .print_L: - lodsb - test al, al - jz .print_end - int 0x10 - jmp .print_L - .print_end: - ret - -bootDriveID: db 0 - -dapack: - dapack_size: db 0x10 - dapack_null: db 0x00 - dapack_blkcount: dw 0x0001 - dapack_boffset: dw 0x8000 - dapack_bsegment: dw 0x0000 - dapack_start: dd 0x00000000 - dapack_upper_lba_bits: dd 0x00000000 - -readsector: - mov dword [dapack_start], eax - mov ah, 0x42 - mov dl, [bootDriveID] - xor bx, bx - mov ds, bx - mov si, dapack - int 0x13 - ret - -; 5K max -readwholefile: - mov bx, word [dapack_boffset] - push bx - mov bx, INODE_ADDRESS - mov word [dapack_boffset], bx - call readsector - pop bx - mov word [dapack_boffset], bx - - mov edx, [INODE_ADDRESS+INODE_NBLOCKS] - - xor ecx, ecx - .readwholefile_L: - lea bx, [INODE_ADDRESS + INODE_DBP0 + (ecx*4)] - mov eax, dword [bx] - - push dx - call readsector - pop dx - - add dword [dapack_boffset], 512 - - inc cx - cmp cx, dx - jl .readwholefile_L - ret - -welcome db "Melvix", 0x0A, 0x0D, 0x00 -nolba db "BIOS lacks support for lba addressing.", 0x00 -signaturebad db "Bad disk signature.", 0x00 - -start: - mov ax, 0x0003 - int 0x10 - - mov si, welcome - call print - - mov ah, 0x41 - mov bx, 0x55AA - int 0x13 - jc lba_not_supported - cmp bx, 0xAA55 - jnz lba_not_supported - - mov eax, 1 - call readsector - - mov eax, [0x8000+0] - cmp eax, 0x34B59645 - jnz signature_BAD - mov eax, [0x8000+4] - cmp eax, 0x1083B99F - jnz signature_BAD - - mov eax, 2 - call readwholefile - - jmp 0x8000 - -lba_not_supported: - mov si, nolba - call print - jmp $ - -signature_BAD: - mov si, signaturebad - call print - jmp $ - -times 510-($-$$) db 0 -dw 0xAA55
\ No newline at end of file diff --git a/src/bootloader/hdd2.asm b/src/bootloader/hdd2.asm deleted file mode 100644 index db5aa38..0000000 --- a/src/bootloader/hdd2.asm +++ /dev/null @@ -1,388 +0,0 @@ -BITS 16 -ORG 0x8000 - -%define SECTOR_BUFFER 0x9000 -%define INODE_NBLOCKS 20 -%define INODE_DBP0 24 -%define INODE_SIBP 64 -%define INODE_DIBP 68 -%define INODE_TIBP 72 -%define INODE_QIBP 76 - -mov esp, 0xFFFF -jmp start - -checkA20: - ; Stolen from https://wiki.osdev.org/A20_Line - pushf - push ds - push es - push di - push si - cli - xor ax, ax - mov es, ax - not ax - mov ds, ax - mov di, 0x0500 - mov si, 0x0510 - mov al, byte [es:di] - push ax - mov al, byte [ds:si] - push ax - mov byte [es:di], 0x00 - mov byte [ds:si], 0xFF - cmp byte [es:di], 0xFF - pop ax - mov byte [ds:si], al - pop ax - mov byte [es:di], al - mov ax, 0 - jz checkA20_exit - mov ax, 1 - checkA20_exit: - pop si - pop di - pop es - pop ds - popf - ret - -last_max_level db 0 -getrecursive: - push edi - push esi - push ecx - - mov eax, edi - cmp al, byte [last_max_level] - jna .last_max_level_updated - mov byte [last_max_level], al - .last_max_level_updated: - - mov eax, esi - call 0x7c26 - - mov eax, dword [esp] - sub eax, 10 - - cmp byte [last_max_level], 1 - jna .check1 - mov ebx, 1 - shl ebx, 7 - sub eax, ebx - .check1: - cmp byte [last_max_level], 2 - jna .check2 - mov ebx, 1 - shl ebx, 14 - sub eax, ebx - .check2: - cmp byte [last_max_level], 3 - jna .check3 - mov ebx, 1 - shl ebx, 21 - sub eax, ebx - .check3: - mov ecx, dword [esp+8] - dec ecx - push ecx - shl ecx, 3 - sub ecx, dword [esp] - add esp, 4 - shr eax, cl - - mov eax, dword [SECTOR_BUFFER+(4*eax)] - - cmp dword [esp+8], 1 - jna .finish_recursion - mov edi, [esp+8] - dec edi - mov esi, eax - mov ecx, [esp] - call getrecursive - .finish_recursion: - mov byte [last_max_level], 0 - - pop ecx - pop esi - pop edi - ret - -get_block: - push ecx - push ebx - - xor ebx, ebx - inc ebx - shl ebx, 21 - add ebx, 9 - cmp ecx, ebx - jna .gtb1 - - mov edi, 4 - mov esi, dword [SECTOR_BUFFER+INODE_QIBP] - call getrecursive - jmp .get_block_end - - .gtb1: - xor ebx, ebx - inc ebx - shl ebx, 14 - add ebx, 9 - cmp ecx, ebx - jna .gtb2 - - mov edi, 3 - mov esi, dword [SECTOR_BUFFER+INODE_TIBP] - call getrecursive - jmp .get_block_end - - .gtb2: - xor ebx, ebx - inc ebx - shl ebx, 7 - add ebx, 9 - cmp ecx, ebx - jna .gtb3 - - mov edi, 2 - mov esi, dword [SECTOR_BUFFER+INODE_DIBP] - call getrecursive - jmp .get_block_end - - .gtb3: - cmp ecx, 9 - jna .gtb4 - - mov edi, 1 - mov esi, dword [SECTOR_BUFFER+INODE_SIBP] - call getrecursive - jmp .get_block_end - - .gtb4: - mov eax, SECTOR_BUFFER+INODE_DBP0 - mov eax, dword [eax+(4*ecx)] - - .get_block_end: - pop ebx - pop ecx - ret - -noa20 db "A20 could not be enabled.", 0 -loading db "Loading kernel...", 0x0A, 0x0D, 0x00 -booting db "Booting...", 0x0A, 0x0D, 0x00 -nomem db "BIOS does not support memory detection!", 0 -memno20 db "BIOS returns memory detection with 24 bytes. This has never been seen!", 0 - -start: - call checkA20 - test ax, ax - jnz A20_ENABLED - - in al, 0x92 - or al, 2 - out 0x92, al - - call checkA20 - test ax, ax - jnz A20_ENABLED - - mov si, noa20 - call 0x7c07 - jmp $ - -A20_ENABLED: - ; Inspired by https://wiki.osdev.org/Unreal_Mode - cli - push ds - lgdt [gdtinfo] - - mov eax, cr0 - or al, 1 - mov cr0, eax - - jmp $+2 - - mov bx, 0x08 - mov ds, bx - - and al, 0xFE - mov cr0, eax - pop ds - - mov si, loading - call 0x7c07 - - mov word [0x7c1a], SECTOR_BUFFER - - mov eax, 3 - call 0x7c26 - mov edx, dword [SECTOR_BUFFER + INODE_NBLOCKS] - - xor ecx, ecx -LOAD_KERNEL: - push edx - - mov eax, 3 - call 0x7c26 - call get_block - call 0x7c26 - - mov eax, ecx - shl eax, 9 - add eax, 0x200000 - - push ecx - xor ecx, ecx - .LOAD_KERNEL_L: - mov ebx, ecx - add ebx, SECTOR_BUFFER - mov ebx, dword [ebx] - - mov edx, ebx - mov ebx, eax - add ebx, ecx - mov dword [ebx], edx - - add ecx, 4 - cmp ecx, 2048 - jl .LOAD_KERNEL_L - pop ecx - - inc ecx - pop edx - cmp ecx, edx - jl LOAD_KERNEL - -mov si, booting -call 0x7c07 - -mov dl, [0x7c15] -mov byte [0x9000], dl - -mov di, 0xA000 -mov eax, 0xE820 -xor ebx, ebx -mov ecx, 24 -mov edx, 0x534D4150 -int 0x15 - -jc BIOS_NO_MEM -cmp eax, 0x534D4150 -jnz BIOS_NO_MEM -cmp cl, 20 -jnz BIOS_MEM_NO20 - -MEM_L: - test ebx, ebx - jz MEM_FINISHED - - mov ax, di - xor ch, ch - add ax, cx - mov di, ax - - mov eax, 0xE820 - mov ecx, 24 - int 0x15 - jmp MEM_L - -MEM_FINISHED: - mov eax, cr0 - or al, 1 - mov cr0, eax - - mov ax, 0x08 - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov ss, ax - -; Infomation: https://wiki.osdev.org/ELF -mov ebx, 0x20002C -mov dl, byte [ebx] -push dx - -mov ebx, 0x20001C -mov ebx, dword [ebx] -push ebx - -xor dh, dh -PHT: - xor eax, eax - mov al, dh - shl eax, 5 - add eax, dword [esp] - add eax, 0x200000 - mov ebx, eax - - mov eax, dword [ebx] - cmp eax, 1 - jnz .PHT_ignore - - add ebx, 4 - mov eax, dword [ebx] - add eax, 0x200000 - push eax - - add ebx, 4 - mov eax, dword [ebx] - push eax - - add ebx, 8 - mov eax, dword [ebx] - push eax - - push dx - xor ecx, ecx - .MOVE_KERNEL_L: - mov ebx, [esp+10] - add ebx, ecx - mov ebx, dword [ebx] - - mov eax, ebx - mov ebx, dword [esp+6] - add ebx, ecx - mov dword [ebx], eax - - add ecx, 4 - cmp ecx, dword [esp+2] - jl .MOVE_KERNEL_L - pop dx - add esp, 12 - - .PHT_ignore: - inc dh - cmp dh, dl - jl PHT - -jmp (codedesc - gdt):protectedMode - -protectedMode: - BITS 32 - mov ebx, 0x100000 - mov eax, dword [ebx] - jmp eax - -BITS 16 -BIOS_NO_MEM: - mov si, nomem - call 0x7c07 - jmp $ -BIOS_MEM_NO20: - mov si, memno20 - call 0x7c07 - jmp $ - -gdtinfo: - dw gdt_end - gdt - 1 - dd gdt - gdt dd 0, 0 - flatdesc db 0xff, 0xff, 0, 0, 0, 10010010b, 11001111b, 0 - codedesc db 0xff, 0xff, 0, 0, 0, 10011010b, 11001111b, 0 - gdt_end: - -kernelbin db "KERNEL.BIN", 0x3B, "1" -kernelbin_len equ ($ - kernelbin)
\ No newline at end of file diff --git a/src/kernel/acpi/acpi.c b/src/kernel/acpi/acpi.c index 57ecc58..e129a95 100644 --- a/src/kernel/acpi/acpi.c +++ b/src/kernel/acpi/acpi.c @@ -166,10 +166,10 @@ int acpi_install() } if (memcmp((unsigned int *)*ptr, "HPET", 4) == 0) { hpet = (struct HPET *)*ptr; - serial_printf("%c%c%c%c", hpet->signature[0], hpet->signature[1], - hpet->signature[2], hpet->signature[3]); - serial_printf("%d", hpet->legacy_replacement); - serial_printf("%d", hpet->address.address); + //serial_printf("%c%c%c%c", hpet->signature[0], hpet->signature[1], + //hpet->signature[2], hpet->signature[3]); + //serial_printf("%d", hpet->legacy_replacement); + //serial_printf("%d", hpet->address.address); } ptr++; } // Else: no valid FADT present diff --git a/src/kernel/boot.asm b/src/kernel/boot.asm index 06411ea..968a824 100644 --- a/src/kernel/boot.asm +++ b/src/kernel/boot.asm @@ -1,3 +1,15 @@ +%define ALIGN 1 ; Align loaded modules on page boundaries +%define MEMINFO 2 ; Provide memory map +%define FLAGS 3 ; Flags (ALIGN | MEMINFO) +%define MAGIC 0x1BADB002 +%define CHECKSUM -(MAGIC + FLAGS) + +section .multiboot + align 4 + dd MAGIC + dd FLAGS + dd CHECKSUM + section .start_section dd _start @@ -15,7 +27,9 @@ section .text global _start extern kernel_main _start: - push esp + mov esp, STACK_TOP + push ebx + push eax cli call kernel_main cli diff --git a/src/kernel/fs/load.c b/src/kernel/fs/load.c index b717e4f..49d31e1 100644 --- a/src/kernel/fs/load.c +++ b/src/kernel/fs/load.c @@ -13,8 +13,7 @@ void load_binaries() userspace = (uint32_t)kmalloc(10000); font = (struct font *)kmalloc(100000); // High quality shit - uint8_t boot_drive_id = (uint8_t)(*((uint8_t *)0x9000)); - if (boot_drive_id != 0xE0) { + if (multiboot_header->boot_device != 0xE0FFFFFF) { struct ata_interface *primary_master = new_ata(1, 0x1F0); marfs_init(primary_master); marfs_read_whole_file(4, (uint8_t *)userspace); @@ -33,6 +32,11 @@ void load_binaries() panic("Userspace binary not found!"); ATAPI_granular_read(1 + (user_e->length / 2048), user_e->lba, (uint8_t *)userspace); kfree(user_e); + + if (font->magic != 0xf0f0f0f0) { + serial_printf("0x%x: WRONG FONT MAGIC!", font->magic); + halt_loop(); + } } vga_log("Successfully loaded binaries"); }
\ No newline at end of file diff --git a/src/kernel/fs/load.h b/src/kernel/fs/load.h index 3730b0b..d4833ce 100644 --- a/src/kernel/fs/load.h +++ b/src/kernel/fs/load.h @@ -12,6 +12,7 @@ struct font { uint16_t font_24[758][24]; uint8_t font_16[758][16]; uint16_t cursor[19]; + uint32_t magic; }; void load_binaries(); diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 80439bc..d06ea4d 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,3 +1,4 @@ +#include <kernel/multiboot.h> #include <kernel/graphics/vesa.h> #include <kernel/gdt/gdt.h> #include <kernel/interrupts/interrupts.h> @@ -15,20 +16,24 @@ #include <kernel/fs/elf.h> #include <kernel/lib/stdio.h> -void kernel_main(uint32_t initial_stack) +void kernel_main(uint32_t magic, multiboot_info_t *grub_header) { - initial_esp = initial_stack; + if (magic != MULTIBOOT_BOOTLOADER_MAGIC) { + vga_log("Invalid boot magic!"); + halt_loop(); + } vga_log("Installing basic features of Melvix..."); // Install features - memory_init(); gdt_install(); init_serial(); acpi_install(); idt_install(); isrs_install(); irq_install(); + memory_init(grub_header); paging_install(); + memory_print(); load_binaries(); set_optimal_resolution(); @@ -49,6 +54,7 @@ void kernel_main(uint32_t initial_stack) // Print total memory info("Total memory found: %dMiB", (memory_get_all() >> 10) + 1); + serial_printf("Total memory found: %dMiB", (memory_get_all() >> 10) + 1); #ifdef INSTALL_MELVIX #include <kernel/fs/install.h> @@ -57,12 +63,12 @@ void kernel_main(uint32_t initial_stack) install_melvix(); #endif - load_elf((char *)userspace); + // load_elf((char *)userspace); // syscalls_install(); // exec(userspace); - panic("This should NOT happen!"); + // panic("This should NOT happen!"); // asm ("div %0" :: "r"(0)); // Exception testing x/0 }
\ No newline at end of file diff --git a/src/kernel/lib/lib.h b/src/kernel/lib/lib.h index b2b7056..ae8ee9c 100644 --- a/src/kernel/lib/lib.h +++ b/src/kernel/lib/lib.h @@ -3,6 +3,7 @@ #include <stddef.h> #include <stdint.h> +#include <kernel/multiboot.h> /** * Copy n data from src to dest @@ -31,7 +32,9 @@ void *memset(void *dest, char val, size_t count); */ int memcmp(const void *a_ptr, const void *b_ptr, size_t size); -void memory_init(); +void memory_init(multiboot_info_t *grub_header); + +void memory_print(); uint32_t memory_get_all(); diff --git a/src/kernel/lib/memory.c b/src/kernel/lib/memory.c index f9e0054..4763ca0 100644 --- a/src/kernel/lib/memory.c +++ b/src/kernel/lib/memory.c @@ -1,5 +1,9 @@ #include <stddef.h> #include <stdint.h> +#include <kernel/system.h> +#include <kernel/lib/stdio.h> +#include <kernel/memory/paging.h> +#include <kernel/multiboot.h> void *memcpy(void *dest, const void *src, size_t count) { @@ -31,39 +35,29 @@ int memcmp(const void *a_ptr, const void *b_ptr, size_t size) return 0; } -uint32_t total_memory; +multiboot_info_t *multiboot_header; -struct memory_entry { - uint64_t base; - uint64_t length; - uint32_t type; -} __attribute__((packed)); - -struct memory_entry *memory_get_entries() +void memory_print() { - return (struct memory_entry *)0xA000; + if (multiboot_header->flags & MULTIBOOT_INFO_MEMORY) { + serial_printf("Mem lower: 0x%x", multiboot_header->mem_lower); + serial_printf("Mem upper: 0x%x", multiboot_header->mem_upper); + } else { + serial_printf("No memory information available!"); + } } -void memory_init() +void memory_init(multiboot_info_t *grub_header) { - uint64_t max_base = 0; - uint64_t max_length = 0; - for (struct memory_entry *i = memory_get_entries(); i->type; i++) { - if (i->type == 1 && i->base > max_base) { - max_base = i->base; - max_length = i->length; - } - } - total_memory = (uint32_t)(max_base + max_length); - total_memory /= 1024; + multiboot_header = grub_header; } uint32_t memory_get_free() { - return total_memory; // - paging_get_used_pages() * 4; + return multiboot_header->mem_upper - paging_get_used_pages() * 4; } uint32_t memory_get_all() { - return total_memory; + return multiboot_header->mem_upper; }
\ No newline at end of file diff --git a/src/kernel/linker.ld b/src/kernel/linker.ld index 50edc46..ff04c17 100644 --- a/src/kernel/linker.ld +++ b/src/kernel/linker.ld @@ -4,6 +4,7 @@ SECTIONS { . = 1M; .text BLOCK(4K) : ALIGN(4K) { + *(.multiboot) *(.start_section) *(.text) } diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c index a29d7db..8b5de59 100644 --- a/src/kernel/memory/paging.c +++ b/src/kernel/memory/paging.c @@ -2,6 +2,7 @@ #include <kernel/memory/paging.h> #include <kernel/system.h> #include <kernel/lib/lib.h> +#include <kernel/io/io.h> int paging_enabled = 0; @@ -40,7 +41,6 @@ void paging_install() paging_set_used(0, ((uint32_t)ASM_KERNEL_END >> 12) + 1); // /4096 paging_enable(); - vga_log("Installed paging"); } diff --git a/src/kernel/multiboot.h b/src/kernel/multiboot.h new file mode 100644 index 0000000..5a98a00 --- /dev/null +++ b/src/kernel/multiboot.h @@ -0,0 +1,216 @@ +/* multiboot.h - Multiboot header file. */ +/* Copyright (C) 1999,2003,2007,2008,2009 Free Software Foundation, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY + * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR + * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef MULTIBOOT_HEADER +#define MULTIBOOT_HEADER 1 + +/* How many bytes from the start of the file we search for the header. */ +#define MULTIBOOT_SEARCH 8192 + +/* The magic field should contain this. */ +#define MULTIBOOT_HEADER_MAGIC 0x1BADB002 + +/* This should be in %eax. */ +#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002 + +/* The bits in the required part of flags field we don't support. */ +#define MULTIBOOT_UNSUPPORTED 0x0000fffc + +/* Alignment of multiboot modules. */ +#define MULTIBOOT_MOD_ALIGN 0x00001000 + +/* Alignment of the multiboot info structure. */ +#define MULTIBOOT_INFO_ALIGN 0x00000004 + +/* Flags set in the 'flags' member of the multiboot header. */ + +/* Align all boot modules on i386 page (4KB) boundaries. */ +#define MULTIBOOT_PAGE_ALIGN 0x00000001 + +/* Must pass memory information to OS. */ +#define MULTIBOOT_MEMORY_INFO 0x00000002 + +/* Must pass video information to OS. */ +#define MULTIBOOT_VIDEO_MODE 0x00000004 + +/* This flag indicates the use of the address fields in the header. */ +#define MULTIBOOT_AOUT_KLUDGE 0x00010000 + +/* Flags to be set in the 'flags' member of the multiboot info structure. */ + +/* is there basic lower/upper memory information? */ +#define MULTIBOOT_INFO_MEMORY 0x00000001 +/* is there a boot device set? */ +#define MULTIBOOT_INFO_BOOTDEV 0x00000002 +/* is the command-line defined? */ +#define MULTIBOOT_INFO_CMDLINE 0x00000004 +/* are there modules to do something with? */ +#define MULTIBOOT_INFO_MODS 0x00000008 + +/* These next two are mutually exclusive */ + +/* is there a symbol table loaded? */ +#define MULTIBOOT_INFO_AOUT_SYMS 0x00000010 +/* is there an ELF section header table? */ +#define MULTIBOOT_INFO_ELF_SHDR 0X00000020 + +/* is there a full memory map? */ +#define MULTIBOOT_INFO_MEM_MAP 0x00000040 + +/* Is there drive info? */ +#define MULTIBOOT_INFO_DRIVE_INFO 0x00000080 + +/* Is there a config table? */ +#define MULTIBOOT_INFO_CONFIG_TABLE 0x00000100 + +/* Is there a boot loader name? */ +#define MULTIBOOT_INFO_BOOT_LOADER_NAME 0x00000200 + +/* Is there a APM table? */ +#define MULTIBOOT_INFO_APM_TABLE 0x00000400 + +/* Is there video information? */ +#define MULTIBOOT_INFO_VIDEO_INFO 0x00000800 + +#ifndef ASM_FILE + +typedef unsigned short multiboot_uint16_t; +typedef unsigned int multiboot_uint32_t; +typedef unsigned long long multiboot_uint64_t; + +struct multiboot_header { + /* Must be MULTIBOOT_MAGIC - see above. */ + multiboot_uint32_t magic; + + /* Feature flags. */ + multiboot_uint32_t flags; + + /* The above fields plus this one must equal 0 mod 2^32. */ + multiboot_uint32_t checksum; + + /* These are only valid if MULTIBOOT_AOUT_KLUDGE is set. */ + multiboot_uint32_t header_addr; + multiboot_uint32_t load_addr; + multiboot_uint32_t load_end_addr; + multiboot_uint32_t bss_end_addr; + multiboot_uint32_t entry_addr; + + /* These are only valid if MULTIBOOT_VIDEO_MODE is set. */ + multiboot_uint32_t mode_type; + multiboot_uint32_t width; + multiboot_uint32_t height; + multiboot_uint32_t depth; +}; + +/* The symbol table for a.out. */ +struct multiboot_aout_symbol_table { + multiboot_uint32_t tabsize; + multiboot_uint32_t strsize; + multiboot_uint32_t addr; + multiboot_uint32_t reserved; +}; +typedef struct multiboot_aout_symbol_table multiboot_aout_symbol_table_t; + +/* The section header table for ELF. */ +struct multiboot_elf_section_header_table { + multiboot_uint32_t num; + multiboot_uint32_t size; + multiboot_uint32_t addr; + multiboot_uint32_t shndx; +}; +typedef struct multiboot_elf_section_header_table multiboot_elf_section_header_table_t; + +struct multiboot_info { + /* Multiboot info version number */ + multiboot_uint32_t flags; + + /* Available memory from BIOS */ + multiboot_uint32_t mem_lower; + multiboot_uint32_t mem_upper; + + /* "root" partition */ + multiboot_uint32_t boot_device; + + /* Kernel command line */ + multiboot_uint32_t cmdline; + + /* Boot-Module list */ + multiboot_uint32_t mods_count; + multiboot_uint32_t mods_addr; + + union { + multiboot_aout_symbol_table_t aout_sym; + multiboot_elf_section_header_table_t elf_sec; + } u; + + /* Memory Mapping buffer */ + multiboot_uint32_t mmap_length; + multiboot_uint32_t mmap_addr; + + /* Drive Info buffer */ + multiboot_uint32_t drives_length; + multiboot_uint32_t drives_addr; + + /* ROM configuration table */ + multiboot_uint32_t config_table; + + /* Boot Loader Name */ + multiboot_uint32_t boot_loader_name; + + /* APM table */ + multiboot_uint32_t apm_table; + + /* Video */ + multiboot_uint32_t vbe_control_info; + multiboot_uint32_t vbe_mode_info; + multiboot_uint16_t vbe_mode; + multiboot_uint16_t vbe_interface_seg; + multiboot_uint16_t vbe_interface_off; + multiboot_uint16_t vbe_interface_len; +}; +typedef struct multiboot_info multiboot_info_t; + +struct multiboot_mmap_entry { + multiboot_uint32_t size; + multiboot_uint64_t addr; + multiboot_uint64_t len; +#define MULTIBOOT_MEMORY_AVAILABLE 1 +#define MULTIBOOT_MEMORY_RESERVED 2 + multiboot_uint32_t type; +} __attribute__((packed)); +typedef struct multiboot_mmap_entry multiboot_memory_map_t; + +struct multiboot_mod_list { + /* the memory used goes from bytes 'mod_start' to 'mod_end-1' inclusive */ + multiboot_uint32_t mod_start; + multiboot_uint32_t mod_end; + + /* Module command line */ + multiboot_uint32_t cmdline; + + /* padding to take it to 16 bytes (must be zero) */ + multiboot_uint32_t pad; +}; +typedef struct multiboot_mod_list multiboot_module_t; + +#endif /* ! ASM_FILE */ + +#endif /* ! MULTIBOOT_HEADER */
\ No newline at end of file diff --git a/src/kernel/system.c b/src/kernel/system.c index b58472b..c58949e 100644 --- a/src/kernel/system.c +++ b/src/kernel/system.c @@ -28,6 +28,7 @@ void vga_log(char *msg) uint16_t *terminal_buffer = (uint16_t *)0xB8000; for (size_t i = 0; i < strlen(msg); i++) terminal_buffer[line * 80 + i] = (uint16_t)msg[i] | (uint16_t)0x700; + serial_printf(msg); char string[80]; strcpy(string, "["); strcat(string, itoa((int)get_time())); diff --git a/src/kernel/system.h b/src/kernel/system.h index 687649f..90535f6 100644 --- a/src/kernel/system.h +++ b/src/kernel/system.h @@ -3,6 +3,7 @@ #include <stdint.h> #include <stddef.h> +#include <kernel/multiboot.h> /** * The kernel end @@ -15,6 +16,11 @@ extern void ASM_KERNEL_END(); uint32_t initial_esp; /** + * Multiboot structure pointer by grub + */ +multiboot_info_t *multiboot_header; + +/** * Initialize the basic features of the OS */ void init(); diff --git a/src/resources/conv.sh b/src/resources/conv.sh index 36a17d0..8b38016 100755 --- a/src/resources/conv.sh +++ b/src/resources/conv.sh @@ -4,7 +4,7 @@ printf "// Generated using the Spleen font and the bdf2c converter (modified using the conv.sh script)\n// Spleen font: (c) 2018-2019, Frederic Cambus, License: MIT\n // bdf2c: (c) 2009-2010 Lutz Sammer, License: AGPLv3\n\n" >font.c -printf "#include <stdint.h>\n\nuint16_t cursor[19]={0b100000000000,0b110000000000,0b111000000000,0b111100000000,0b111110000000,0b111111000000,0b111111100000,0b111111110000,0b111111111000,0b111111111100,0b111111111110,0b111111111111,0b111111111111,0b111111110000,0b111101111000,0b111001111000,0b110000111100,0b000000111100,0b000000011000};\n" >>font.c +printf "#include <stdint.h>\n\nuint32_t magic=0xf0f0f0f0;\n\nuint16_t cursor[19]={0b100000000000,0b110000000000,0b111000000000,0b111100000000,0b111110000000,0b111111000000,0b111111100000,0b111111110000,0b111111111000,0b111111111100,0b111111111110,0b111111111111,0b111111111111,0b111111110000,0b111101111000,0b111001111000,0b110000111100,0b000000111100,0b000000011000};\n" >>font.c generate() { font_url="https://raw.githubusercontent.com/fcambus/spleen/master/spleen-$1x$2.bdf" diff --git a/src/resources/font.c b/src/resources/font.c index de5dda1..4269b35 100644 --- a/src/resources/font.c +++ b/src/resources/font.c @@ -4,6 +4,8 @@ #include <stdint.h> +uint32_t magic = 0xf0f0f0f0; + uint16_t cursor[19] = { 0b100000000000, 0b110000000000, 0b111000000000, 0b111100000000, 0b111110000000, 0b111111000000, 0b111111100000, 0b111111110000, 0b111111111000, 0b111111111100, 0b111111111110, 0b111111111111, |