From 86c0c87425f2a482d5f21a1cb6878806650760d6 Mon Sep 17 00:00:00 2001 From: Nocturn9x Date: Tue, 15 Nov 2022 15:47:33 +0100 Subject: [PATCH] Initial work on the bootloader --- .gitignore | 4 ++++ README.md | 9 +++++++- src/bootloader/mbr.s | 41 ++++++++++++++++++++++++++++++++++++ src/bootloader/util/tty.s | 44 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/bootloader/mbr.s create mode 100644 src/bootloader/util/tty.s diff --git a/.gitignore b/.gitignore index 65d6b54..9227b93 100644 --- a/.gitignore +++ b/.gitignore @@ -65,3 +65,7 @@ compile_commands.json CTestTestfile.cmake _deps +# TSOS stuff + +*.bin +dist/ \ No newline at end of file diff --git a/README.md b/README.md index befd716..dd5e9cb 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ # TSOS -The Simple Operating System \ No newline at end of file +A toy project to implement: +- A BIOS-compatible bootloader written in x86 Assembly +- A 32-bit kernel and associated operating system written in C +- Drivers (keyboard, VGA, etc.) +- Dynamic memory management +- A shell +- Multitasking +- More? \ No newline at end of file diff --git a/src/bootloader/mbr.s b/src/bootloader/mbr.s new file mode 100644 index 0000000..8db254b --- /dev/null +++ b/src/bootloader/mbr.s @@ -0,0 +1,41 @@ +; Definition of the MBR (Master Boot Record). This is basically our bootloader and +; is located in the first 512 bytes of the drive we're booting from. From here, we +; do some basic setup and then call into the kernel + +[org 0x7c00] ; Address where the code expects to be loaded in. The BIOS always loads us here +[bits 16] ; All x86 CPUs start in 16 bit (aka "real") mode, so we tell nasm to emit 16-bit code + +; We print a simple startup message using the ISRs +; from the BIOS +mov si, startup_msg +call print + + +; Now we setup the stack by setting the +; base pointer to address 0x8000. The +; address itself doesn't matter as long +; as it's far enough away from memory already +; in use by the BIOS +mov bp, 0x8000 +mov sp, bp ; The stack starts out empty, so sp == bp + + +jmp $ ; Keeps jumping at the current address (loops forever) + +; These two lines never execute (for now), but they might once +; we jump into the kernel, so we disable interrupts and halt the +; CPU, just in case +cli +hlt + +; Now we include our "function definitions" (after the halting, so +; they're never executed unless explicitly called) +%include "src/bootloader/util/tty.s" + +; Here we define our variables: They need to be defined after the +; halting because otherwise they will be executed as code! +startup_msg: db "TSOS - Bootloader: Starting up", 0xA, 0xD, 0x0 + +; padding and magic number +times 510 - ($-$$) db 0 +dw 0xaa55 \ No newline at end of file diff --git a/src/bootloader/util/tty.s b/src/bootloader/util/tty.s new file mode 100644 index 0000000..ce0ca79 --- /dev/null +++ b/src/bootloader/util/tty.s @@ -0,0 +1,44 @@ +; Some utilities to deal with the TTY using the BIOS +; during real mode + +next_line: + ; Points the TTY cursor to the next + ; line + pusha + ; Source: http://www.techhelpmanual.com/118-int_10h_03h__query_cursor_position_and_size.html + mov ah, 0x3 ; Get cursor position + mov bh, 0x0 ; Page 0 + int 0x10 + ; Go to the next row + ; Source: http://www.techhelpmanual.com/117-int_10h_02h__set_cursor_position.html + mov ah, 0x2 ; Set cursor position + mov bh, 0x0 ; Page 0 + xor dl, dl ; Goes to column 0 (i.e. start of the line) + inc dh ; Goes to the next row + int 0x10 + popa + ret + + +print: + ; Prints a null-terminated string whose address + ; is located in the si register + pusha + mov ah, 0xe ; Set the screen in TTY mode + print_loop: + mov al, [si] + int 0x10 ; Writes the content of al to the screen + inc si + cmp byte [si], 0x0 ; If we got to the null byte, we're done + jne print_loop ; Otherwise, we run this again + popa + ret + + +println: + ; Prints a null-terminated string whose address + ; is located in the si register and sets the TTY + ; cursor to the next line + call print + call next_line + ret \ No newline at end of file