course 3 (vga and linker config)
authorMinep <zelong56@gmail.com>
Sun, 6 Feb 2022 19:59:39 +0000 (19:59 +0000)
committerMinep <zelong56@gmail.com>
Sun, 6 Feb 2022 19:59:39 +0000 (19:59 +0000)
19 files changed:
lunaix-os/.gitignore [new file with mode: 0644]
lunaix-os/.vscode/c_cpp_properties.json [new file with mode: 0644]
lunaix-os/GRUB_TEMPLATE [new file with mode: 0644]
lunaix-os/arch/x86/boot.S
lunaix-os/bochs.cfg [new file with mode: 0644]
lunaix-os/build/bin/lunaix.bin [new file with mode: 0755]
lunaix-os/build/iso/boot/grub/grub.cfg [new file with mode: 0644]
lunaix-os/build/iso/boot/lunaix.bin [new file with mode: 0755]
lunaix-os/build/lunaix.iso [new file with mode: 0644]
lunaix-os/build/obj/arch/x86/boot.S.o [new file with mode: 0644]
lunaix-os/build/obj/kernel/kernel.c.o [new file with mode: 0644]
lunaix-os/build/obj/kernel/tty/tty.c.o [new file with mode: 0644]
lunaix-os/config-grub.sh [new file with mode: 0755]
lunaix-os/includes/lunaix/tty/tty.h [new file with mode: 0644]
lunaix-os/kernel/kernel.c [new file with mode: 0644]
lunaix-os/kernel/tty/tty.c [new file with mode: 0644]
lunaix-os/linker.ld [new file with mode: 0644]
lunaix-os/makefile [new file with mode: 0644]
practice-c2/slides.odp [new file with mode: 0644]

diff --git a/lunaix-os/.gitignore b/lunaix-os/.gitignore
new file mode 100644 (file)
index 0000000..995bba6
--- /dev/null
@@ -0,0 +1 @@
+dump
diff --git a/lunaix-os/.vscode/c_cpp_properties.json b/lunaix-os/.vscode/c_cpp_properties.json
new file mode 100644 (file)
index 0000000..10acdd8
--- /dev/null
@@ -0,0 +1,18 @@
+{
+    "configurations": [
+        {
+            "name": "OS-DEV",
+            "includePath": [
+                "${workspaceFolder}/includes/**"
+            ],
+            "compilerArgs": [
+                "-ffreestanding"
+            ],
+            "defines": [],
+            "compilerPath": "${HOME}/opt/cross-compiler/bin/i686-elf-gcc",
+            "cStandard": "gnu99",
+            "intelliSenseMode": "gcc-x86"
+        }
+    ],
+    "version": 4
+}
\ No newline at end of file
diff --git a/lunaix-os/GRUB_TEMPLATE b/lunaix-os/GRUB_TEMPLATE
new file mode 100644 (file)
index 0000000..6652d2c
--- /dev/null
@@ -0,0 +1,3 @@
+menuentry "$_OS_NAME" {
+       multiboot /boot/$_OS_NAME.bin
+}
\ No newline at end of file
index 8e019a56f9f8df812d159bddba3998448ee88ac0..29746eefd052fd4c36f8d51c2f6f58449b35e638 100644 (file)
@@ -13,6 +13,8 @@
     stack_top:
 
 .section .text
+    .global start_
+    .type start_, @function
     start_:
         movl $stack_top, %esp
         /* 
diff --git a/lunaix-os/bochs.cfg b/lunaix-os/bochs.cfg
new file mode 100644 (file)
index 0000000..8114f97
--- /dev/null
@@ -0,0 +1,3 @@
+ata0-master: type=cdrom, path="build/lunaix.iso", status=inserted
+
+boot: cdrom
\ No newline at end of file
diff --git a/lunaix-os/build/bin/lunaix.bin b/lunaix-os/build/bin/lunaix.bin
new file mode 100755 (executable)
index 0000000..45dc739
Binary files /dev/null and b/lunaix-os/build/bin/lunaix.bin differ
diff --git a/lunaix-os/build/iso/boot/grub/grub.cfg b/lunaix-os/build/iso/boot/grub/grub.cfg
new file mode 100644 (file)
index 0000000..9e82c0a
--- /dev/null
@@ -0,0 +1 @@
+menuentry "lunaix" { multiboot /boot/lunaix.bin }
diff --git a/lunaix-os/build/iso/boot/lunaix.bin b/lunaix-os/build/iso/boot/lunaix.bin
new file mode 100755 (executable)
index 0000000..45dc739
Binary files /dev/null and b/lunaix-os/build/iso/boot/lunaix.bin differ
diff --git a/lunaix-os/build/lunaix.iso b/lunaix-os/build/lunaix.iso
new file mode 100644 (file)
index 0000000..4ce0616
Binary files /dev/null and b/lunaix-os/build/lunaix.iso differ
diff --git a/lunaix-os/build/obj/arch/x86/boot.S.o b/lunaix-os/build/obj/arch/x86/boot.S.o
new file mode 100644 (file)
index 0000000..3eafda4
Binary files /dev/null and b/lunaix-os/build/obj/arch/x86/boot.S.o differ
diff --git a/lunaix-os/build/obj/kernel/kernel.c.o b/lunaix-os/build/obj/kernel/kernel.c.o
new file mode 100644 (file)
index 0000000..3bcce39
Binary files /dev/null and b/lunaix-os/build/obj/kernel/kernel.c.o differ
diff --git a/lunaix-os/build/obj/kernel/tty/tty.c.o b/lunaix-os/build/obj/kernel/tty/tty.c.o
new file mode 100644 (file)
index 0000000..f601012
Binary files /dev/null and b/lunaix-os/build/obj/kernel/tty/tty.c.o differ
diff --git a/lunaix-os/config-grub.sh b/lunaix-os/config-grub.sh
new file mode 100755 (executable)
index 0000000..def0ecf
--- /dev/null
@@ -0,0 +1,5 @@
+#!/usr/bin/bash
+
+export _OS_NAME=$1
+
+echo $(cat GRUB_TEMPLATE | envsubst)
\ No newline at end of file
diff --git a/lunaix-os/includes/lunaix/tty/tty.h b/lunaix-os/includes/lunaix/tty/tty.h
new file mode 100644 (file)
index 0000000..b470fdc
--- /dev/null
@@ -0,0 +1,24 @@
+typedef unsigned short vga_atrributes; 
+
+#define VGA_COLOR_BLACK 0
+#define VGA_COLOR_BLUE 1
+#define VGA_COLOR_GREEN 2
+#define VGA_COLOR_CYAN 3
+#define VGA_COLOR_RED 4
+#define VGA_COLOR_MAGENTA 5
+#define VGA_COLOR_BROWN 6
+#define VGA_COLOR_LIGHT_GREY 7
+#define VGA_COLOR_DARK_GREY 8
+#define VGA_COLOR_LIGHT_BLUE 9
+#define VGA_COLOR_LIGHT_GREEN 10
+#define VGA_COLOR_LIGHT_CYAN 11
+#define VGA_COLOR_LIGHT_RED 12
+#define VGA_COLOR_LIGHT_MAGENTA 13
+#define VGA_COLOR_LIGHT_BROWN 14
+#define VGA_COLOR_WHITE 15
+
+void tty_set_theme(vga_atrributes fg, vga_atrributes bg);
+void tty_put_char(char chr);
+void tty_put_str(char* str);
+void tty_scroll_up();
+void tty_clear();
\ No newline at end of file
diff --git a/lunaix-os/kernel/kernel.c b/lunaix-os/kernel/kernel.c
new file mode 100644 (file)
index 0000000..f9c7c4c
--- /dev/null
@@ -0,0 +1,11 @@
+#include <lunaix/tty/tty.h>
+
+void _kernel_init() {
+    // TODO
+}
+
+void _kernel_main(void* info_table) {
+    // TODO
+    tty_set_theme(VGA_COLOR_GREEN, VGA_COLOR_BLACK);
+    tty_put_str("Hello kernel world!\nThis is second line.");
+}
\ No newline at end of file
diff --git a/lunaix-os/kernel/tty/tty.c b/lunaix-os/kernel/tty/tty.c
new file mode 100644 (file)
index 0000000..cb4f1b4
--- /dev/null
@@ -0,0 +1,58 @@
+#include <lunaix/tty/tty.h>
+#include <stdint.h>
+
+#define TTY_WIDTH 80
+#define TTY_HEIGHT 25
+
+vga_atrributes *buffer = 0xB8000;
+
+vga_atrributes theme_color = VGA_COLOR_BLACK;
+
+uint32_t TTY_COLUMN = 0;
+uint16_t TTY_ROW = 0;
+
+void tty_set_theme(vga_atrributes fg, vga_atrributes bg) {
+    theme_color = (bg << 4 | fg) << 8;
+}
+
+void tty_put_char(char chr) {
+    if (chr == '\n') {
+        TTY_COLUMN = 0;
+        TTY_ROW++;
+    }
+    else if (chr == '\r') {
+        TTY_COLUMN = 0;
+    }
+    else {
+        *(buffer + TTY_COLUMN + TTY_ROW * TTY_WIDTH) = (theme_color | chr);
+        TTY_COLUMN++;
+        if (TTY_COLUMN >= TTY_WIDTH) {
+            TTY_COLUMN = 0;
+            TTY_ROW++;
+        }
+    }
+
+    if (TTY_ROW >= TTY_HEIGHT) {
+        tty_scroll_up();
+        TTY_ROW--;
+    } 
+}
+
+void tty_put_str(char* str) {
+    while (*str != '\0') {
+        tty_put_char(*str);
+        str++;
+    }
+}
+
+void tty_scroll_up() {
+    // TODO use memcpy
+}
+
+void tty_clear() {
+    for (uint32_t x = 0; x < TTY_WIDTH; x++) {
+        for (uint32_t y = 0; y < TTY_HEIGHT; y++) {
+            *(buffer + x + y * TTY_WIDTH) = theme_color;
+        }
+    }
+}
\ No newline at end of file
diff --git a/lunaix-os/linker.ld b/lunaix-os/linker.ld
new file mode 100644 (file)
index 0000000..79dd63a
--- /dev/null
@@ -0,0 +1,23 @@
+ENTRY(start_)
+
+SECTIONS {
+    . = 0x100000;
+
+    .text BLOCK(4K) : {
+        * (.multiboot)
+        * (.text)
+    }
+
+    .bss BLOCK(4K) : {
+        * (COMMON)
+        * (.bss)
+    }
+
+    .data BLOCK(4k) : {
+        * (.data)
+    }
+
+    .rodata BLOCK(4K) : {
+        * (.rodata)
+    }
+}
\ No newline at end of file
diff --git a/lunaix-os/makefile b/lunaix-os/makefile
new file mode 100644 (file)
index 0000000..2ffc930
--- /dev/null
@@ -0,0 +1,76 @@
+OS_ARCH := x86
+
+BUILD_DIR := build
+KERNEL_DIR := kernel
+OBJECT_DIR := $(BUILD_DIR)/obj
+BIN_DIR := $(BUILD_DIR)/bin
+ISO_DIR := $(BUILD_DIR)/iso
+ISO_BOOT_DIR := $(ISO_DIR)/boot
+ISO_GRUB_DIR := $(ISO_BOOT_DIR)/grub
+
+INCLUDES_DIR := includes
+INCLUDES := $(patsubst %, -I%, $(INCLUDES_DIR))
+
+OS_NAME = lunaix
+OS_BIN = $(OS_NAME).bin
+OS_ISO = $(OS_NAME).iso
+
+CC := i686-elf-gcc
+AS := i686-elf-as
+
+O := -O3
+W := -Wall -Wextra
+CFLAGS := -std=gnu99 -ffreestanding $(O) $(W)
+LDFLAGS := -ffreestanding $(O) -nostdlib -lgcc
+
+SOURCE_FILES := $(shell find -name "*.[cS]")
+SRC := $(patsubst ./%, $(OBJECT_DIR)/%.o, $(SOURCE_FILES))
+
+$(OBJECT_DIR):
+       @mkdir -p $(OBJECT_DIR)
+
+$(BIN_DIR):
+       @mkdir -p $(BIN_DIR)
+
+$(ISO_DIR):
+       @mkdir -p $(ISO_DIR)
+       @mkdir -p $(ISO_BOOT_DIR)
+       @mkdir -p $(ISO_GRUB_DIR)
+
+$(OBJECT_DIR)/%.S.o: %.S
+       @mkdir -p $(@D)
+       $(CC) -c $< -o $@
+
+$(OBJECT_DIR)/%.c.o: %.c 
+       @mkdir -p $(@D)
+       $(CC) $(INCLUDES) -c $< -o $@ $(CFLAGS)
+
+$(BIN_DIR)/$(OS_BIN): $(OBJECT_DIR) $(BIN_DIR) $(SRC)
+       $(CC) -T linker.ld -o $(BIN_DIR)/$(OS_BIN) $(SRC) $(LDFLAGS)
+
+$(BUILD_DIR)/$(OS_ISO): $(ISO_DIR) $(BIN_DIR)/$(OS_BIN) GRUB_TEMPLATE
+       @./config-grub.sh ${OS_NAME} > $(ISO_GRUB_DIR)/grub.cfg
+       @cp $(BIN_DIR)/$(OS_BIN) $(ISO_BOOT_DIR)
+       @grub-mkrescue -o $(BUILD_DIR)/$(OS_ISO) $(ISO_DIR)
+
+all: clean $(BUILD_DIR)/$(OS_ISO)
+
+all-debug: O := -O0
+all-debug: CFLAGS := -g -std=gnu99 -ffreestanding $(O) $(W) -fomit-frame-pointer
+all-debug: LDFLAGS := -ffreestanding $(O) -nostdlib -lgcc
+all-debug: clean $(BUILD_DIR)/$(OS_ISO)
+       @i686-elf-objdump -D $(BIN_DIR)/$(OS_BIN) > dump
+
+clean:
+       @rm -rf $(BUILD_DIR)
+
+run: $(BUILD_DIR)/$(OS_ISO)
+       @qemu-system-i386 -cdrom $(BUILD_DIR)/$(OS_ISO)
+
+debug-qemu: all-debug
+       @objcopy --only-keep-debug $(BIN_DIR)/$(OS_BIN) $(BUILD_DIR)/kernel.dbg
+       @qemu-system-i386 -s -S -kernel $(BIN_DIR)/$(OS_BIN) &
+       @gdb -s $(BUILD_DIR)/kernel.dbg -ex "target remote localhost:1234"
+
+debug-bochs: all-debug
+       @bochs -q -f bochs.cfg
\ No newline at end of file
diff --git a/practice-c2/slides.odp b/practice-c2/slides.odp
new file mode 100644 (file)
index 0000000..5e7b715
Binary files /dev/null and b/practice-c2/slides.odp differ