feat(kernel): cga text mode driver

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-07-14 15:11:28 +02:00
parent baaeda937f
commit 7fcb200475
15 changed files with 405 additions and 189 deletions

View file

@ -12,7 +12,11 @@ SRCS = kernel.asm \
lock.inc \ lock.inc \
gdt.inc \ gdt.inc \
isr.inc \ isr.inc \
pic.inc pic.inc \
idt.inc \
dev/at/pit.inc \
dev/at/cga.inc \
dev/at/kbd.inc
.PHONY: all .PHONY: all
all: $(KERNEL) all: $(KERNEL)

131
kernel/dev/at/cga.inc Normal file
View file

@ -0,0 +1,131 @@
;; File: cga.inc
;;
;; Usefull links:
;; - <http://www.techhelpmanual.com/901-color_graphics_adapter_i_o_ports.html>
;; - <http://www.osdever.net/FreeVGA/vga/crtcreg.htm>
CGA_MEMORY = 0xC00B8000
CGA_COMMAND = 0x3d4
CGA_DATA = 0x3d5
CGA_CURSOR_HIGH = 0xE
CGA_CURSOR_LOW = 0xF
CGA_COLUMNS = 80
CGA_LINES = 24
;; Function: cga_getpos
;;
;; Out:
;; AX - cursor position
cga_getpos:
; read cursor position
xor ax, ax
xchg bx, bx
; get high
mov dx, CGA_COMMAND
mov al, CGA_CURSOR_HIGH
out dx, al
mov dx, CGA_DATA
in al, dx
xchg al, ah
; get low
mov dx, CGA_COMMAND
mov al, CGA_CURSOR_LOW
out dx, al
mov dx, CGA_DATA
in al, dx
ret
;; Function: cga_setpos
;;
;; In:
;; AX - cursor position
cga_setpos:
xchg bx, bx
mov dx, CGA_COMMAND
push ax
mov al, CGA_CURSOR_HIGH
out dx, al
pop ax
xchg al, ah
mov dx, CGA_DATA
out dx, al
mov dx, CGA_COMMAND
mov al, CGA_CURSOR_LOW
out dx, al
xchg al, ah
mov dx, CGA_DATA
out dx, al
ret
;; Function: cga_putc
;;
;; In:
;; AL - charactere to print
cga_putc:
push ebp
mov ebp, esp
sub esp, 0x2
push eax
call cga_getpos
mov [ebp-0x2], ax
pop eax
cmp al, LF
jne @f
mov ax, [ebp-0x2]
mov dl, CGA_COLUMNS
div dl
mov al, CGA_COLUMNS
sub al, ah
xor ah, ah
mov cx, [ebp-0x2]
add cx, ax
jmp .end
@@:
mov edx, CGA_MEMORY
xor ecx, ecx
mov cx, [ebp-0x2]
shl cx, 1
add edx, ecx
mov ah, 0x07
mov word [edx], ax
mov cx, [ebp-0x2]
inc cx
mov ax, cx
mov dl, CGA_COLUMNS
div dl
cmp al, CGA_LINES
jbe .end
mov [ebp-0x2], cx
; scroll up
push esi
push edi
mov ecx, CGA_COLUMNS*(CGA_LINES-1)
mov edi, CGA_MEMORY
mov esi, CGA_MEMORY+80*2
rep movsw
mov ecx, CGA_COLUMNS
xor ax, ax
mov edi, CGA_MEMORY+(CGA_COLUMNS*(CGA_LINES-1)*2)
rep stosw
pop esi
pop edi
mov cx, CGA_COLUMNS*(CGA_LINES-1)
.end:
mov ax, cx
call cga_setpos
leave
ret

18
kernel/dev/at/kbd.inc Normal file
View file

@ -0,0 +1,18 @@
;; File: kbd.inc
kbd_getc:
ret
kbd_irq:
pusha
mov esi, szMsgKbdIRQ
call klog
mov al, PIC_EOI
out PIC1_COMMAND, al
popa
iret
szMsgKbdIRQ db "KBD: IRQ", 0

29
kernel/dev/at/pit.inc Normal file
View file

@ -0,0 +1,29 @@
;; File: pit.inc
;;
;; Usefull links:
;; - <http://www.osdever.net/bkerndev/Docs/pit.htm>
PIT_CHANNEL0 = 0x40
PIT_CHANNEL1 = 0x40
PIT_CHANNEL2 = 0x42
PIT_COMMAND = 0x43
;; Function: pit_init
pit_init:
ret
;; Function: pit_irq
pit_irq:
pusha
;mov esi, szMsgPitIRQ
;call klog
mov al, PIC_EOI
out PIC1_COMMAND, al
popa
iret
szMsgPitIRQ db "PIT: IRQ", 0

2
kernel/dev/console.inc Normal file
View file

@ -0,0 +1,2 @@
;; File: console.inc

9
kernel/dev/dev.inc Normal file
View file

@ -0,0 +1,9 @@
;; File: dev.inc
struc Device {
.write dd ?
.read dd ?
}

View file

@ -1,3 +1,4 @@
;; File: gdt.inc
gdt: gdt:
; null descriptor ; null descriptor
@ -30,3 +31,7 @@ gdt:
pGDT: pGDT:
dw gdt.end - gdt - 1 dw gdt.end - gdt - 1
dd gdt dd gdt
gdt_set_tss:
ret

View file

@ -1,10 +1,51 @@
;; File: idt.inc
idt_set_table: idt_set_gate:
mov [aInterruptGate + (ecx * 8)], ax
mov [aInterruptGate + (ecx * 8) + 2], word 0x08
mov [aInterruptGate + (ecx * 8) + 5], word 0x8E
shr eax, 16
mov [aInterruptGate + (ecx * 8) + 6], ax
ret ret
idt_setup: idt_setup:
xor ecx, ecx
@@:
mov eax, [aISRTable + (ecx * 4)]
call idt_set_gate
inc ecx
cmp ecx, 32
jb @b
mov eax, pit_irq
call idt_set_gate
inc ecx
mov eax, kbd_irq
call idt_set_gate
inc ecx
@@:
mov eax, irq_dummy
call idt_set_gate
inc ecx
cmp ecx, 0x30
jb @b
mov ecx, 0x42
mov eax, isr_syscall
call idt_set_gate
lidt [pIDT]
sti
ret ret
pIdt: pIDT:
dw ? dw aInterruptGate.end-aInterruptGate-1
dd ? dd aInterruptGate
; variable: idt_entries
aInterruptGate:
dd 256*2 dup(0)
.end:

View file

@ -0,0 +1,2 @@
irq_dummy:
iret

View file

@ -18,54 +18,6 @@ forward
} }
idt_setup:
xor ecx, ecx
@@:
mov eax, [aISRTable + (ecx * 4)]
mov [aInterruptGate + (ecx * 8)], ax
mov [aInterruptGate + (ecx * 8) + 2], word 0x08
mov [aInterruptGate + (ecx * 8) + 5], word 0x8E
shr eax, 16
mov [aInterruptGate + (ecx * 8) + 6], ax
inc ecx
cmp ecx, 32
jb @b
@@:
mov eax, irq_dummy
mov [aInterruptGate + (ecx * 8)], ax
mov [aInterruptGate + (ecx * 8) + 2], word 0x08
mov [aInterruptGate + (ecx * 8) + 5], word 0x8E
shr eax, 16
mov [aInterruptGate + (ecx * 8) + 6], ax
inc ecx
cmp ecx, 0x30
jb @b
mov ecx, 0x42
mov eax, isr_syscall
mov [aInterruptGate + (ecx * 8)], ax
mov [aInterruptGate + (ecx * 8) + 2], word 0x08
mov [aInterruptGate + (ecx * 8) + 5], word 0x8E
shr eax, 16
mov [aInterruptGate + (ecx * 8) + 6], ax
lidt [pIDT]
sti
ret
pIDT:
dw aInterruptGate.end-aInterruptGate-1
dd aInterruptGate
; variable: idt_entries
aInterruptGate:
dd 256*2 dup(0)
.end:
irq_dummy: irq_dummy:
iret iret

View file

@ -60,10 +60,15 @@ kmain:
lgdt [pGDT] lgdt [pGDT]
; I don't think i need to reload segment cuz their value are already correct ; I don't think i need to reload segment cuz their value are already correct
call idt_setup call idt_setup
;int 0x42 call pit_init
mov eax, SYSCALL_EXIT
int 0x42
mov al, 'X'
call cga_putc
.halt: .halt:
hlt hlt
@ -81,7 +86,11 @@ kmain:
include 'gdt.inc' include 'gdt.inc'
include 'syscall.inc' include 'syscall.inc'
include 'isr.inc' include 'isr.inc'
include 'idt.inc'
include 'pic.inc' include 'pic.inc'
include 'dev/at/pit.inc'
include 'dev/at/kbd.inc'
include 'dev/at/cga.inc'
szMsgKernelAlive db "Kernel (", VERSION_FULL , ") is alive", 0 szMsgKernelAlive db "Kernel (", VERSION_FULL , ") is alive", 0

View file

@ -34,7 +34,7 @@ klog_print:
or al, al or al, al
jz @f jz @f
out dx, al out dx, al
;call klog_kputc ;call klog_kput
jmp @b jmp @b
@@: @@:
ret ret

View file

@ -1,3 +1,6 @@
;; File: coff.inc
;; Struc: COFFFileHdr
struc COFFFileHdr { struc COFFFileHdr {
.f_magic dw ? .f_magic dw ?
.f_nscns dw ? .f_nscns dw ?

View file

@ -16,10 +16,21 @@ rc4_init:
mov ebp, esp mov ebp, esp
mov eax, [ebp+8] mov eax, [ebp+8]
xor ecx, ecx xor cx, cx
.loop: @@:
mov byte [eax], byte cx
inc cx
inc eax
cmp cx, 255
jne @b
xor cx, cx
mov eax, [ebp+8]
@@:
.end: .end:
leave leave
ret ret

View file

@ -1,131 +1,131 @@
;; File: sha256.asm ;; File: sha256.asm
;; SHA-256 cryptographic hash ;; SHA-256 cryptographic hash
; Implementation is based on <Wikipedia's pseudocode at https://en.wikipedia.org/wiki/SHA-2#Pseudocode> ; Implementation is based on <Wikipedia's pseudocode at https://en.wikipedia.org/wiki/SHA-2#Pseudocode>
format COFF format COFF
use32 use32
section '.data' data section '.data' data
; Constant: K ; Constant: K
; SHA-256 round constants ; SHA-256 round constants
K: K:
dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5 dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5
dd 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174 dd 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174
dd 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da dd 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da
dd 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967 dd 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967
dd 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85 dd 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85
dd 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070 dd 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070
dd 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3 dd 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3
dd 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 dd 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
section '.text' code section '.text' code
;; Function: sha256_compute_block ;; Function: sha256_compute_block
sha256_compute_block: sha256_compute_block:
push ebp push ebp
mov ebp, esp mov ebp, esp
leave leave
ret ret
;; Function: sha256_internal ;; Function: sha256_internal
;; ;;
;; Parameters: ;; Parameters:
;; ;;
;; [esp+8] - state ;; [esp+8] - state
;; [esp+12] - input buffer ;; [esp+12] - input buffer
;; [esp+16] - size of the input buffer ;; [esp+16] - size of the input buffer
;; ;;
sha256_internal: sha256_internal:
push ebp push ebp
push edi push edi
mov ebp, esp mov ebp, esp
sub esp, 64+8 sub esp, 64+8
mov eax, dword [ebp+16] mov eax, dword [ebp+16]
xor edx, edx xor edx, edx
; set padlen to (len << 3) ; set padlen to (len << 3)
shld edx, eax, 3 ; 64-bit left shit shld edx, eax, 3 ; 64-bit left shit
sal eax, 3 sal eax, 3
mov dword [ebp-(64+8)], eax mov dword [ebp-(64+8)], eax
mov dword [ebp-(64+4)], edx mov dword [ebp-(64+4)], edx
.for_block: .for_block:
cmp dword [ebp+16], 64 cmp dword [ebp+16], 64
jb .done jb .done
push dword [ebp+12] ; push in push dword [ebp+12] ; push in
push dword [ebp+8] ; push state push dword [ebp+8] ; push state
call sha256_compute_block call sha256_compute_block
add esp, 8 add esp, 8
sub dword [ebp+16], 64 ; len -= 64 sub dword [ebp+16], 64 ; len -= 64
add dword [ebp+12], 64 ; in += 64 add dword [ebp+12], 64 ; in += 64
jmp .for_block jmp .for_block
.done: .done:
; ensure padding buffer is full of zero ; ensure padding buffer is full of zero
lea edi, [ebp-64] lea edi, [ebp-64]
mov ecx, 16 mov ecx, 16
xor eax, eax xor eax, eax
rep stosd rep stosd
pop edi pop edi
leave leave
ret ret
;; Function: sha256 ;; Function: sha256
;; ;;
;; Parameters: ;; Parameters:
;; ;;
;; [esp+8] - output buffer ;; [esp+8] - output buffer
;; [esp+12] - input buffer ;; [esp+12] - input buffer
;; [esp+16] - size of the input buffer ;; [esp+16] - size of the input buffer
;; ;;
public sha256 public sha256
sha256: sha256:
push ebp push ebp
mov ebp, esp mov ebp, esp
sub esp, 8*4 ; uint32_t state[8] sub esp, 8*4 ; uint32_t state[8]
; setup initial state ; setup initial state
mov dword [ebp-8*4], 0x6a09e667 mov dword [ebp-8*4], 0x6a09e667
mov dword [ebp-7*4], 0xbb67ae85 mov dword [ebp-7*4], 0xbb67ae85
mov dword [ebp-6*4], 0x3c6ef372 mov dword [ebp-6*4], 0x3c6ef372
mov dword [ebp-5*4], 0xa54ff53a mov dword [ebp-5*4], 0xa54ff53a
mov dword [ebp-4*4], 0x510e527f mov dword [ebp-4*4], 0x510e527f
mov dword [ebp-3*4], 0x9b05688c mov dword [ebp-3*4], 0x9b05688c
mov dword [ebp-2*4], 0x1f83d9ab mov dword [ebp-2*4], 0x1f83d9ab
mov dword [ebp-1*4], 0x5be0cd19 mov dword [ebp-1*4], 0x5be0cd19
; ;
; copy state to uint8_t *out ; copy state to uint8_t *out
mov edx, dword [ebp+8] mov edx, dword [ebp+8]
mov eax, dword [ebp-8*4] mov eax, dword [ebp-8*4]
bswap eax bswap eax
mov dword [edx], eax ; out[0] = bswap(state[0]) mov dword [edx], eax ; out[0] = bswap(state[0])
mov eax, dword [ebp-7*4] mov eax, dword [ebp-7*4]
bswap eax bswap eax
mov dword [edx+4], eax ; out[1] = bswap(state[1]) mov dword [edx+4], eax ; out[1] = bswap(state[1])
mov eax, dword [ebp-6*4] mov eax, dword [ebp-6*4]
bswap eax bswap eax
mov dword [edx+8], eax ; out[2] = bswap(state[2]) mov dword [edx+8], eax ; out[2] = bswap(state[2])
mov eax, dword [ebp-5*4] mov eax, dword [ebp-5*4]
bswap eax bswap eax
mov dword [edx+12], eax ; out[3] = bswap(state[3]) mov dword [edx+12], eax ; out[3] = bswap(state[3])
mov eax, dword [ebp-4*4] mov eax, dword [ebp-4*4]
bswap eax bswap eax
mov dword [edx+16], eax ; out[4] = bswap(state[4]) mov dword [edx+16], eax ; out[4] = bswap(state[4])
mov eax, dword [ebp-3*4] mov eax, dword [ebp-3*4]
bswap eax bswap eax
mov dword [edx+20], eax ; out[5] = bswap(state[5]) mov dword [edx+20], eax ; out[5] = bswap(state[5])
mov eax, dword [ebp-2*4] mov eax, dword [ebp-2*4]
bswap eax bswap eax
mov dword [edx+24], eax ; out[6] = bswap(state[6]) mov dword [edx+24], eax ; out[6] = bswap(state[6])
mov eax, dword [ebp-1*4] mov eax, dword [ebp-1*4]
bswap eax bswap eax
mov dword [edx+28], eax ; out[7] = bswap(state[7]) mov dword [edx+28], eax ; out[7] = bswap(state[7])
leave leave
ret ret