Skip to content

Commit d75a41d

Browse files
committed
Added variable arguments + drivers keyboard layout[not functional]
1 parent d306cbc commit d75a41d

File tree

8 files changed

+167
-6
lines changed

8 files changed

+167
-6
lines changed

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
BL_SRC_DIR = src/bootloader
22
KERNEL_SRC_DIR = src/kernel
3+
KERNEL_DRIVERS_SRC_DIR = src/kernel/drivers
34
SYSCALLS_SRC_DIR = src/lib/syscalls
45
LIB_SRC_DIR = src/lib
56
UTIL_SRC_DIR = src/lib/util
@@ -28,6 +29,7 @@ app_dashboard = $(BUILD_DIR)/dashboard
2829

2930
# Parameters
3031
BT_STAGE2_SECTOR_COUNT = 19 # In Hex
32+
SOURCE_SNAPSHOT="\"$$(git rev-parse --short HEAD)$$(git diff --quiet || echo '_unstaged')\""
3133

3234
# General Assumptions
3335
## Integer is 4 bytes
@@ -85,10 +87,10 @@ $(kernel_core): $(kernel_core_asm_o) $(kernel_core_c_o)
8587
truncate --size=%512 $@
8688

8789
$(kernel_core_asm_o): $(KERNEL_SRC_DIR)/core.asm
88-
nasm -o $@ -f elf32 -i $(KERNEL_SRC_DIR)/ -i$(SYSCALLS_SRC_DIR)/ $<
90+
nasm -o $@ -f elf32 -i $(KERNEL_SRC_DIR)/ -i $(KERNEL_DRIVERS_SRC_DIR)/ -i$(SYSCALLS_SRC_DIR)/ $<
8991

9092
$(kernel_core_c_o): $(KERNEL_SRC_DIR)/core.c
91-
gcc -m16 -fno-pie -c -Isrc -o $@ $<
93+
gcc -m16 -fno-pie -c -D__SOURCE_SNAPSHOT__=$(SOURCE_SNAPSHOT) -Isrc -o $@ $<
9294

9395
$(app_entry_o): $(LIB_SRC_DIR)/app/entry.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_interface_bios.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm
9496
nasm -o $@ -f elf32 -i $(BL_SRC_DIR)/ -i$(SYSCALLS_SRC_DIR)/ $<

src/kernel/core.asm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
%include "io_interface_protected.asm"
2+
%include "keyboard.asm"
23

34
[BITS 16]
45

56
extern entry_core
7+
global __low_panic_halt
8+
global __low_va_args
69

710
[SECTION .text]
811
jmp entry_core
912

1013
PLUGIN_SYSCALLS_IO_PROTECTED
14+
PLUGIN_KERNEL_DRIVERS_KEYBOARD
1115

16+
__low_panic_halt:
17+
HLT
18+
JMP __low_panic_halt
19+
20+
__low_va_args:
21+
22+
mov esi, [esp + 0x4] ; (index)
23+
shl esi, 2
24+
add esi, 4
25+
mov eax, [ebp + esi]
26+
ret

src/kernel/core.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <kernel/essentials.c>
2+
#include <kernel/drivers/keyboard.c>
13
#include <lib/syscalls/io.h>
24
#include <lib/syscalls/io_interface_protected.c>
35

@@ -27,6 +29,7 @@ void entry_core() {
2729

2830
move_xy(3,3);
2931
print_line(welcome_message);
32+
keyboard_init();
3033
exec(0,0);
3134
while(1);
3235
}

src/kernel/drivers/keyboard.asm

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
%macro PLUGIN_KERNEL_DRIVERS_KEYBOARD 0
2+
3+
global port_write
4+
global port_read
5+
6+
[SECTION .text]
7+
port_write:
8+
push ebp
9+
mov ebp, esp
10+
11+
mov al, [ebp + 0xc] ; value
12+
mov dx, [ebp + 0x8] ; port
13+
out dx, al
14+
15+
mov esp, ebp
16+
pop ebp
17+
ret
18+
19+
port_read:
20+
push ebp
21+
mov ebp, esp
22+
23+
mov dx, [ebp + 0x8] ; port
24+
in al, dx
25+
and eax, 0xFF
26+
27+
mov esp, ebp
28+
pop ebp
29+
ret
30+
31+
%endmacro

src/kernel/drivers/keyboard.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#ifndef __KERNEL_DRIVERS_KEYBOARD
2+
#define __KERNEL_DRIVERS_KEYBOARD
3+
4+
#include <kernel/essentials.c>
5+
6+
#define DRIVERS_KEYBOARD_PORT_DATA 0x60
7+
#define DRIVERS_KEYBOARD_PORT_STATUS 0x64
8+
#define DRIVERS_KEYBOARD_PORT_COMMAND 0x64
9+
10+
// Using USB Legacy Support
11+
extern void port_write(unsigned short port, unsigned char value);
12+
extern unsigned char port_read(unsigned short port);
13+
14+
void keyboard_init() {
15+
kernel_status("Loading Keyboard Drivers...", 0);
16+
unsigned char out;
17+
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0xAD);
18+
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0xA7);
19+
while(1) {
20+
// clear the buffer
21+
out = port_read(DRIVERS_KEYBOARD_PORT_STATUS);
22+
if(!(out&1)) break;
23+
}
24+
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0x20);
25+
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0x60);
26+
out = port_read(DRIVERS_KEYBOARD_PORT_STATUS);
27+
int second_port_exists = 0;
28+
if(out&(0b10000)) {
29+
second_port_exists = 1;
30+
kernel_status("Loading Keyboard Drivers: Second Port Exists", 0);
31+
}
32+
// clear 0, 1 and 6.
33+
// test bit 5
34+
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0xAA);
35+
out = port_read(DRIVERS_KEYBOARD_PORT_STATUS);
36+
if (out != 0x55) {
37+
PANIC(out, "PS2 self test failed.");
38+
}
39+
// enable PS2 first port.
40+
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0xAE);
41+
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0xFF);
42+
out = port_read(DRIVERS_KEYBOARD_PORT_STATUS);
43+
if (out != 0xFA) {
44+
PANIC(out, "PS2 reset failed.");
45+
}
46+
}
47+
48+
#endif

src/kernel/essentials.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef __KERNEL_ESSENTIALS
2+
#define __KERNEL_ESSENTIALS
3+
4+
#include <lib/syscalls/io.h>
5+
6+
// __low_panic_halt defined in core.asm.
7+
extern void __low_panic_halt();
8+
9+
#define PANIC(err, message) panic((err), (message), __FILE__, __LINE__, __SOURCE_SNAPSHOT__)
10+
11+
void panic(int err, const char *message, const char *src_file,
12+
unsigned int line_number, const char *src_snapshot) {
13+
set_color_bg(C_WHITE);
14+
set_color_fg(C_RED);
15+
move_xy(0,0);
16+
print_line(src_file);
17+
print_char(':');
18+
print_int(line_number);
19+
print_char(',');
20+
print_line(src_snapshot);
21+
move_xy(0,1);
22+
print_line("Panic");
23+
if (err>0) {
24+
print_char('[');
25+
print_int(err);
26+
print_char(']');
27+
}
28+
print_line(": ");
29+
print_line(message);
30+
__low_panic_halt();
31+
}
32+
33+
extern unsigned int __low_va_args(unsigned int index);
34+
#define va_args(type, index) ((type)(__low_va_args((index))))
35+
36+
void kernel_status(const char *message, int status) {
37+
set_color_bg(C_WHITE);
38+
set_color_fg(C_BLACK);
39+
move_xy(0, WINDOW_HEIGHT-1);
40+
print_line(message);
41+
print_int(status);
42+
int left = WINDOW_WIDTH - get_cursor_x();
43+
while(left>0) {
44+
print_char(' ');
45+
left--;
46+
}
47+
}
48+
49+
#endif

src/lib/syscalls/io.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,27 @@ void print_char(char c) {
6666
char digit_to_hex[] = "0123456789ABCDEF";
6767

6868
void print_hex_nibble(unsigned char x) {
69-
print_char(digit_to_hex[x]);
69+
if(x<10) {
70+
print_char((char)(x+'0'));
71+
} else {
72+
print_char((char)(x+('A'-10)));
73+
}
7074
}
7175

7276
void print_hex_byte(unsigned char x) {
73-
print_hex_nibble(x>>4);
77+
print_hex_nibble((x>>4)&0xF);
7478
print_hex_nibble(x&0xF);
7579
}
7680

77-
void print_line(char *str) {
81+
82+
void print_line(const char *str) {
7883
while((*str)!='\0') {
7984
print_char(*str);
8085
str++;
8186
}
8287
}
8388

84-
void print_memory_hex(unsigned char *str,unsigned short count) {
89+
void print_memory_hex(const char *str,unsigned short count) {
8590
while(count) {
8691
print_hex_byte(*str);
8792
str++;

src/lib/syscalls/io_interface.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
unsigned char IO_CURRENT_X = 0;
55
unsigned char IO_CURRENT_Y = 0;
66

7+
unsigned char get_cursor_x() {
8+
return IO_CURRENT_X;
9+
}
10+
11+
unsigned char get_cursor_y() {
12+
return IO_CURRENT_Y;
13+
}
14+
715
void io_move_fix_location();
816

917
void io_low_scroll_screen(unsigned char count, unsigned char color,

0 commit comments

Comments
 (0)