diff --git a/kernel/Makefile b/kernel/Makefile index 73df900..bb1bd93 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -12,7 +12,11 @@ SRCS = kernel.asm \ lock.inc \ gdt.inc \ isr.inc \ - pic.inc + pic.inc \ + idt.inc \ + dev/at/pit.inc \ + dev/at/cga.inc \ + dev/at/kbd.inc .PHONY: all all: $(KERNEL) diff --git a/kernel/dev/at/cga.inc b/kernel/dev/at/cga.inc new file mode 100644 index 0000000..bbbbdfb --- /dev/null +++ b/kernel/dev/at/cga.inc @@ -0,0 +1,131 @@ + ;; File: cga.inc + ;; + ;; Usefull links: + ;; - + ;; - +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 diff --git a/kernel/dev/at/kbd.inc b/kernel/dev/at/kbd.inc new file mode 100644 index 0000000..83e07c2 --- /dev/null +++ b/kernel/dev/at/kbd.inc @@ -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 diff --git a/kernel/dev/at/pit.inc b/kernel/dev/at/pit.inc new file mode 100644 index 0000000..f41d2af --- /dev/null +++ b/kernel/dev/at/pit.inc @@ -0,0 +1,29 @@ + ;; File: pit.inc + ;; + ;; Usefull links: + ;; - + +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 diff --git a/kernel/dev/console.inc b/kernel/dev/console.inc new file mode 100644 index 0000000..3335d2a --- /dev/null +++ b/kernel/dev/console.inc @@ -0,0 +1,2 @@ + ;; File: console.inc + diff --git a/kernel/dev/dev.inc b/kernel/dev/dev.inc new file mode 100644 index 0000000..3a5836b --- /dev/null +++ b/kernel/dev/dev.inc @@ -0,0 +1,9 @@ + ;; File: dev.inc + + + +struc Device { + .write dd ? + .read dd ? +} + diff --git a/kernel/gdt.inc b/kernel/gdt.inc index b68ad50..fe20f62 100644 --- a/kernel/gdt.inc +++ b/kernel/gdt.inc @@ -1,3 +1,4 @@ + ;; File: gdt.inc gdt: ; null descriptor @@ -30,3 +31,7 @@ gdt: pGDT: dw gdt.end - gdt - 1 dd gdt + +gdt_set_tss: + + ret diff --git a/kernel/idt.inc b/kernel/idt.inc index a344cca..88c4fd5 100644 --- a/kernel/idt.inc +++ b/kernel/idt.inc @@ -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 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 -pIdt: - dw ? - dd ? +pIDT: + dw aInterruptGate.end-aInterruptGate-1 + dd aInterruptGate + +; variable: idt_entries +aInterruptGate: + dd 256*2 dup(0) +.end: diff --git a/kernel/irq.inc b/kernel/irq.inc index e69de29..cbbd5b5 100644 --- a/kernel/irq.inc +++ b/kernel/irq.inc @@ -0,0 +1,2 @@ +irq_dummy: + iret diff --git a/kernel/isr.inc b/kernel/isr.inc index 1f1c785..1936ed3 100644 --- a/kernel/isr.inc +++ b/kernel/isr.inc @@ -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: iret diff --git a/kernel/kernel.asm b/kernel/kernel.asm index c487854..1c1f2fa 100644 --- a/kernel/kernel.asm +++ b/kernel/kernel.asm @@ -60,10 +60,15 @@ kmain: lgdt [pGDT] ; I don't think i need to reload segment cuz their value are already correct - call idt_setup - ;int 0x42 + call pit_init + + mov eax, SYSCALL_EXIT + int 0x42 + + mov al, 'X' + call cga_putc .halt: hlt @@ -81,7 +86,11 @@ kmain: include 'gdt.inc' include 'syscall.inc' include 'isr.inc' + include 'idt.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 diff --git a/kernel/klog.inc b/kernel/klog.inc index ff16731..0231086 100644 --- a/kernel/klog.inc +++ b/kernel/klog.inc @@ -34,7 +34,7 @@ klog_print: or al, al jz @f out dx, al - ;call klog_kputc + ;call klog_kput jmp @b @@: ret diff --git a/kernel/sys/coff.inc b/kernel/sys/coff.inc index d9988ec..8c2ffb9 100644 --- a/kernel/sys/coff.inc +++ b/kernel/sys/coff.inc @@ -1,3 +1,6 @@ + ;; File: coff.inc + + ;; Struc: COFFFileHdr struc COFFFileHdr { .f_magic dw ? .f_nscns dw ? diff --git a/lib/crypto/rc4/rc4.asm b/lib/crypto/rc4/rc4.asm index 842fd8b..c71c0b5 100644 --- a/lib/crypto/rc4/rc4.asm +++ b/lib/crypto/rc4/rc4.asm @@ -16,10 +16,21 @@ rc4_init: mov ebp, esp 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: leave ret diff --git a/lib/crypto/sha2/sha256.asm b/lib/crypto/sha2/sha256.asm index cd3670b..bf2b540 100644 --- a/lib/crypto/sha2/sha256.asm +++ b/lib/crypto/sha2/sha256.asm @@ -1,131 +1,131 @@ - ;; File: sha256.asm - ;; SHA-256 cryptographic hash - - ; Implementation is based on - format COFF - use32 - - section '.data' data - ; Constant: K - ; SHA-256 round constants -K: - dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5 - dd 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174 - dd 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da - dd 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967 - dd 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85 - dd 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070 - dd 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3 - dd 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 - -section '.text' code - - ;; Function: sha256_compute_block -sha256_compute_block: - push ebp - mov ebp, esp - - leave - ret - - ;; Function: sha256_internal - ;; - ;; Parameters: - ;; - ;; [esp+8] - state - ;; [esp+12] - input buffer - ;; [esp+16] - size of the input buffer - ;; -sha256_internal: - push ebp - push edi - mov ebp, esp - sub esp, 64+8 - - mov eax, dword [ebp+16] - xor edx, edx - - ; set padlen to (len << 3) - shld edx, eax, 3 ; 64-bit left shit - sal eax, 3 - mov dword [ebp-(64+8)], eax - mov dword [ebp-(64+4)], edx - -.for_block: - cmp dword [ebp+16], 64 - jb .done - push dword [ebp+12] ; push in - push dword [ebp+8] ; push state - call sha256_compute_block - add esp, 8 - sub dword [ebp+16], 64 ; len -= 64 - add dword [ebp+12], 64 ; in += 64 - jmp .for_block -.done: - - ; ensure padding buffer is full of zero - lea edi, [ebp-64] - mov ecx, 16 - xor eax, eax - rep stosd - - - - pop edi - leave - ret - - ;; Function: sha256 - ;; - ;; Parameters: - ;; - ;; [esp+8] - output buffer - ;; [esp+12] - input buffer - ;; [esp+16] - size of the input buffer - ;; - public sha256 -sha256: - push ebp - mov ebp, esp - sub esp, 8*4 ; uint32_t state[8] - - ; setup initial state - mov dword [ebp-8*4], 0x6a09e667 - mov dword [ebp-7*4], 0xbb67ae85 - mov dword [ebp-6*4], 0x3c6ef372 - mov dword [ebp-5*4], 0xa54ff53a - mov dword [ebp-4*4], 0x510e527f - mov dword [ebp-3*4], 0x9b05688c - mov dword [ebp-2*4], 0x1f83d9ab - mov dword [ebp-1*4], 0x5be0cd19 - - ; - - ; copy state to uint8_t *out - mov edx, dword [ebp+8] - mov eax, dword [ebp-8*4] - bswap eax - mov dword [edx], eax ; out[0] = bswap(state[0]) - mov eax, dword [ebp-7*4] - bswap eax - mov dword [edx+4], eax ; out[1] = bswap(state[1]) - mov eax, dword [ebp-6*4] - bswap eax - mov dword [edx+8], eax ; out[2] = bswap(state[2]) - mov eax, dword [ebp-5*4] - bswap eax - mov dword [edx+12], eax ; out[3] = bswap(state[3]) - mov eax, dword [ebp-4*4] - bswap eax - mov dword [edx+16], eax ; out[4] = bswap(state[4]) - mov eax, dword [ebp-3*4] - bswap eax - mov dword [edx+20], eax ; out[5] = bswap(state[5]) - mov eax, dword [ebp-2*4] - bswap eax - mov dword [edx+24], eax ; out[6] = bswap(state[6]) - mov eax, dword [ebp-1*4] - bswap eax - mov dword [edx+28], eax ; out[7] = bswap(state[7]) - leave - ret + ;; File: sha256.asm + ;; SHA-256 cryptographic hash + + ; Implementation is based on + format COFF + use32 + + section '.data' data + ; Constant: K + ; SHA-256 round constants +K: + dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5 + dd 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174 + dd 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da + dd 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967 + dd 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85 + dd 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070 + dd 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3 + dd 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 + +section '.text' code + + ;; Function: sha256_compute_block +sha256_compute_block: + push ebp + mov ebp, esp + + leave + ret + + ;; Function: sha256_internal + ;; + ;; Parameters: + ;; + ;; [esp+8] - state + ;; [esp+12] - input buffer + ;; [esp+16] - size of the input buffer + ;; +sha256_internal: + push ebp + push edi + mov ebp, esp + sub esp, 64+8 + + mov eax, dword [ebp+16] + xor edx, edx + + ; set padlen to (len << 3) + shld edx, eax, 3 ; 64-bit left shit + sal eax, 3 + mov dword [ebp-(64+8)], eax + mov dword [ebp-(64+4)], edx + +.for_block: + cmp dword [ebp+16], 64 + jb .done + push dword [ebp+12] ; push in + push dword [ebp+8] ; push state + call sha256_compute_block + add esp, 8 + sub dword [ebp+16], 64 ; len -= 64 + add dword [ebp+12], 64 ; in += 64 + jmp .for_block +.done: + + ; ensure padding buffer is full of zero + lea edi, [ebp-64] + mov ecx, 16 + xor eax, eax + rep stosd + + + + pop edi + leave + ret + + ;; Function: sha256 + ;; + ;; Parameters: + ;; + ;; [esp+8] - output buffer + ;; [esp+12] - input buffer + ;; [esp+16] - size of the input buffer + ;; + public sha256 +sha256: + push ebp + mov ebp, esp + sub esp, 8*4 ; uint32_t state[8] + + ; setup initial state + mov dword [ebp-8*4], 0x6a09e667 + mov dword [ebp-7*4], 0xbb67ae85 + mov dword [ebp-6*4], 0x3c6ef372 + mov dword [ebp-5*4], 0xa54ff53a + mov dword [ebp-4*4], 0x510e527f + mov dword [ebp-3*4], 0x9b05688c + mov dword [ebp-2*4], 0x1f83d9ab + mov dword [ebp-1*4], 0x5be0cd19 + + ; + + ; copy state to uint8_t *out + mov edx, dword [ebp+8] + mov eax, dword [ebp-8*4] + bswap eax + mov dword [edx], eax ; out[0] = bswap(state[0]) + mov eax, dword [ebp-7*4] + bswap eax + mov dword [edx+4], eax ; out[1] = bswap(state[1]) + mov eax, dword [ebp-6*4] + bswap eax + mov dword [edx+8], eax ; out[2] = bswap(state[2]) + mov eax, dword [ebp-5*4] + bswap eax + mov dword [edx+12], eax ; out[3] = bswap(state[3]) + mov eax, dword [ebp-4*4] + bswap eax + mov dword [edx+16], eax ; out[4] = bswap(state[4]) + mov eax, dword [ebp-3*4] + bswap eax + mov dword [edx+20], eax ; out[5] = bswap(state[5]) + mov eax, dword [ebp-2*4] + bswap eax + mov dword [edx+24], eax ; out[6] = bswap(state[6]) + mov eax, dword [ebp-1*4] + bswap eax + mov dword [edx+28], eax ; out[7] = bswap(state[7]) + leave + ret