commit eaa8b0914df5708dcded6397ae450f3bdcac10ef Author: Kieron Morris Date: Tue Sep 15 21:13:23 2015 +0000 Initial Commit. git-svn-id: https://spexeah.com:8443/svn/Asuro@1 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c diff --git a/Compile.bat b/Compile.bat new file mode 100644 index 00000000..9530db2b --- /dev/null +++ b/Compile.bat @@ -0,0 +1,52 @@ +@echo off +set /a ERRCOUNT=0 +echo ======================= +echo == ASURO COMPILATION == +echo ======================= +echo. +echo Compiling ASM Stub... +echo. +nasm -f elf src\stub.asm -o lib\stub.o +if %ERRORLEVEL% GEQ 1 ( + echo Failed to compile stub! + set /a ERRCOUNT=%ERRCOUNT%+1 +) ELSE ( + echo Success. +) +echo. +echo ======================= +echo. +echo Compiling FPC Sources... +echo. +fpc -Aelf -n -O3 -Op3 -Si -Sc -Sg -Xd -CX -XXs -Rintel -Tlinux -FElib\ src\kernel.pas +if %ERRORLEVEL% GEQ 1 ( + echo Failed to compile FPC Sources! + set /a ERRCOUNT=%ERRCOUNT%+1 +) ELSE ( + echo Success. +) +echo. +echo ======================= +echo. +echo Linking... +echo. +ld -A elf32-i386 --gc-sections -s -Tlinker.script -o bin\kernel.obj lib\stub.o lib\kernel.o lib\multiboot.o lib\system.o lib\console.o +if %ERRORLEVEL% GEQ 1 ( + echo Failed linking! + set /a ERRCOUNT=%ERRCOUNT%+1 +) ELSE ( + echo Success. +) +echo. +echo ======================= +echo. +if %ERRCOUNT% GEQ 1 ( + echo %ERRCOUNT% Errors Occurred, please review. +) ELSE ( + echo No errors. +) +echo. +echo ======================= +echo. +echo Press any key to exit... +pause > nul \ No newline at end of file diff --git a/bin/kernel.obj b/bin/kernel.obj new file mode 100644 index 00000000..25ce963a Binary files /dev/null and b/bin/kernel.obj differ diff --git a/lib/console.o b/lib/console.o new file mode 100644 index 00000000..696fc280 Binary files /dev/null and b/lib/console.o differ diff --git a/lib/console.ppu b/lib/console.ppu new file mode 100644 index 00000000..d5db41b4 Binary files /dev/null and b/lib/console.ppu differ diff --git a/lib/kernel.o b/lib/kernel.o new file mode 100644 index 00000000..869ae73b Binary files /dev/null and b/lib/kernel.o differ diff --git a/lib/kernel.ppu b/lib/kernel.ppu new file mode 100644 index 00000000..dcbe3367 Binary files /dev/null and b/lib/kernel.ppu differ diff --git a/lib/libpconsole.a b/lib/libpconsole.a new file mode 100644 index 00000000..e91a8bd0 Binary files /dev/null and b/lib/libpconsole.a differ diff --git a/lib/libpkernel.a b/lib/libpkernel.a new file mode 100644 index 00000000..9d3b3bf1 Binary files /dev/null and b/lib/libpkernel.a differ diff --git a/lib/libpmultiboot.a b/lib/libpmultiboot.a new file mode 100644 index 00000000..529966ba Binary files /dev/null and b/lib/libpmultiboot.a differ diff --git a/lib/libpsystem.a b/lib/libpsystem.a new file mode 100644 index 00000000..488126b5 Binary files /dev/null and b/lib/libpsystem.a differ diff --git a/lib/multiboot.o b/lib/multiboot.o new file mode 100644 index 00000000..6134b201 Binary files /dev/null and b/lib/multiboot.o differ diff --git a/lib/multiboot.ppu b/lib/multiboot.ppu new file mode 100644 index 00000000..dc309924 Binary files /dev/null and b/lib/multiboot.ppu differ diff --git a/lib/stub.o b/lib/stub.o new file mode 100644 index 00000000..6790d76b Binary files /dev/null and b/lib/stub.o differ diff --git a/lib/system.o b/lib/system.o new file mode 100644 index 00000000..736030d7 Binary files /dev/null and b/lib/system.o differ diff --git a/lib/system.ppu b/lib/system.ppu new file mode 100644 index 00000000..1e8c73eb Binary files /dev/null and b/lib/system.ppu differ diff --git a/linker.script b/linker.script new file mode 100644 index 00000000..e0452f1b --- /dev/null +++ b/linker.script @@ -0,0 +1,31 @@ +ENTRY(kstart) +SECTIONS +{ + .text 0x100000 : + { + text = .; _text = .; __text = .; + *(.text) + . = ALIGN(4096); + } + .data : + { + data = .; _data = .; __data = .; + *(.data) + kimage_text = .; + LONG(text); + kimage_data = .; + LONG(data); + kimage_bss = .; + LONG(bss); + kimage_end = .; + LONG(end); + . = ALIGN(4096); + } + .bss : + { + bss = .; _bss = .; __bss = .; + *(.bss) + . = ALIGN(4096); + } + end = .; _end = .; __end = .; +} \ No newline at end of file diff --git a/src/console.pas b/src/console.pas new file mode 100644 index 00000000..184ec35c --- /dev/null +++ b/src/console.pas @@ -0,0 +1,147 @@ +{ +///////////////////////////////////////////////////////// +// // +// Freepascal barebone OS // +// console.pas // +// // +///////////////////////////////////////////////////////// +// +// By: De Deyn Kim +// License: Public domain +// +} + +unit console; + +interface + +var + xpos: Integer = 0; + ypos: Integer = 0; + +procedure kclearscreen(); +procedure kwritechr(c: Char); +procedure kwritestr(s: PChar); +procedure kwriteint(i: Integer); +procedure kwritedword(i: DWORD); + +implementation + +var + vidmem: PChar = PChar($b8000); + +procedure kclearscreen(); [public, alias: 'kclearscreen']; +var + i: Integer; +begin + for i := 0 to 3999 do + vidmem[i] := #0; +end; + +procedure kwritechr(c: Char); [public, alias: 'kwritechr']; +var + offset: Integer; +begin + if (ypos > 24) then + ypos := 0; + + if (xpos > 79) then + xpos := 0; + + offset := (xpos shl 1) + (ypos * 160); + vidmem[offset] := c; + offset += 1; + vidmem[offset] := #7; + offset += 1; + + xpos := (offset mod 160); + ypos := (offset - xpos) div 160; + xpos := xpos shr 1; +end; + +procedure kwritestr(s: PChar); [public, alias: 'kwritestr']; +var + offset, i: Integer; +begin + if (ypos > 24) then + ypos := 0; + + if (xpos > 79) then + xpos := 0; + + offset := (xpos shl 1) + (ypos * 160); + i := 0; + + while (s[i] <> Char($0)) do + begin + vidmem[offset] := s[i]; + offset += 1; + vidmem[offset] := #7; + offset += 1; + i += 1; + end; + + xpos := (offset mod 160); + ypos := (offset - xpos) div 160; + xpos := xpos shr 1; +end; + +procedure kwriteint(i: Integer); [public, alias: 'kwriteint']; +var + buffer: array [0..11] of Char; + str: PChar; + digit: DWORD; + minus: Boolean; +begin + str := @buffer[11]; + str^ := #0; + + if (i < 0) then + begin + digit := -i; + minus := True; + end + else + begin + digit := i; + minus := False; + end; + + repeat + Dec(str); + str^ := Char((digit mod 10) + Byte('0')); + digit := digit div 10; + until (digit = 0); + + if (minus) then + begin + Dec(str); + str^ := '-'; + end; + + kwritestr(str); +end; + +procedure kwritedword(i: DWORD); [public, alias: 'kwritedword']; +var + buffer: array [0..11] of Char; + str: PChar; + digit: DWORD; +begin + for digit := 0 to 10 do + buffer[digit] := '0'; + + str := @buffer[11]; + str^ := #0; + + digit := i; + repeat + Dec(str); + str^ := Char((digit mod 10) + Byte('0')); + digit := digit div 10; + until (digit = 0); + + kwritestr(@Buffer[0]); +end; + +end. \ No newline at end of file diff --git a/src/kernel.pas b/src/kernel.pas new file mode 100644 index 00000000..72621e01 --- /dev/null +++ b/src/kernel.pas @@ -0,0 +1,70 @@ +{ +///////////////////////////////////////////////////////// +// // +// Freepascal barebone OS // +// kernel.pas // +// // +///////////////////////////////////////////////////////// +// +// By: De Deyn Kim +// License: Public domain +// +} + +unit kernel; + +interface + +uses + multiboot, + console; + +procedure kmain(mbinfo: Pmultiboot_info_t; mbmagic: DWORD); stdcall; + +implementation + +procedure kmain(mbinfo: Pmultiboot_info_t; mbmagic: DWORD); stdcall; [public, alias: 'kmain']; +begin + kclearscreen(); + kwritestr('Freepascal barebone OS booted!'); + xpos := 0; + ypos += 1; + + if (mbmagic <> MULTIBOOT_BOOTLOADER_MAGIC) then + begin + kwritestr('Halting system, a multiboot-compliant boot loader needed!'); + asm + cli + hlt + end; + end + else + begin + kwritestr('Booted by a multiboot-compliant boot loader!'); + xpos := 0; + ypos += 2; + kwritestr('Multiboot information:'); + xpos := 0; + ypos += 2; + kwritestr(' Lower memory = '); + kwriteint(mbinfo^.mem_lower); + kwritestr('KB'); + xpos := 0; + ypos += 1; + kwritestr(' Higher memory = '); + kwriteint(mbinfo^.mem_upper); + kwritestr('KB'); + xpos := 0; + ypos += 1; + kwritestr(' Total memory = '); + kwriteint(((mbinfo^.mem_upper + 1000) div 1024) +1); + kwritestr('MB'); + end; + + asm + @loop: + jmp @loop + end; +end; + +end. \ No newline at end of file diff --git a/src/multiboot.pas b/src/multiboot.pas new file mode 100644 index 00000000..140c9fc0 --- /dev/null +++ b/src/multiboot.pas @@ -0,0 +1,55 @@ +unit multiboot; + +interface + +const + KERNEL_STACKSIZE = $4000; + + MULTIBOOT_BOOTLOADER_MAGIC = $2BADB002; + +type + Pelf_section_header_table_t = ^elf_section_header_table_t; + elf_section_header_table_t = packed record + num: DWORD; + size: DWORD; + addr: DWORD; + shndx: DWORD; + end; + + Pmultiboot_info_t = ^multiboot_info_t; + multiboot_info_t = packed record + flags: DWORD; + mem_lower: DWORD; { Amount of memory available below 1mb } + mem_upper: DWORD; { Amount of memory available above 1mb } + boot_device: DWORD; + cmdline: DWORD; + mods_count: DWORD; + mods_addr: DWORD; + elf_sec: elf_section_header_table_t; + mmap_length: DWORD; + mmap_addr: DWORD; + end; + + Pmodule_t = ^module_t; + module_t = packed record + mod_start: DWORD; + mod_end: DWORD; + name: DWORD; + reserved: DWORD; + end; + + Pmemory_map_t = ^memory_map_t; + memory_map_t = packed record + size: DWORD; + { You can declare these two as a single qword if your compiler supports it } + base_addr_low: DWORD; + base_addr_high: DWORD; + { And again, these can be made into one qword variable. } + length_low: DWORD; + length_high: DWORD; + mtype: DWORD; + end; + +implementation + +end. \ No newline at end of file diff --git a/src/stub.asm b/src/stub.asm new file mode 100644 index 00000000..b361513e --- /dev/null +++ b/src/stub.asm @@ -0,0 +1,79 @@ +;///////////////////////////////////////////////////////// +;// // +;// Freepascal barebone OS // +;// stub.asm // +;// // +;///////////////////////////////////////////////////////// +;// +;// By: De Deyn Kim +;// License: Public domain +;// + +; +; Kernel stub +; + +; +; We are in 32bits protected mode +; +[bits 32] + +; +; Export entrypoint +; +[global kstart] + +; +; Import kernel entrypoint +; +[extern kmain] + +; +; Posible multiboot header flags +; +MULTIBOOT_MODULE_ALIGN equ 1<<0 +MULTIBOOT_MEMORY_MAP equ 1<<1 +MULTIBOOT_GRAPHICS_FIELDS equ 1<<2 +MULTIBOOT_ADDRESS_FIELDS equ 1<<16 + +; +; Multiboot header defines +; +MULTIBOOT_HEADER_MAGIC equ 0x1BADB002 +MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_MODULE_ALIGN | MULTIBOOT_MEMORY_MAP +MULTIBOOT_HEADER_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) + +; +; Kernel stack size +; +KERNEL_STACKSIZE equ 0x4000 + +section .text + +; +; Multiboot header +; +align 4 +dd MULTIBOOT_HEADER_MAGIC +dd MULTIBOOT_HEADER_FLAGS +dd MULTIBOOT_HEADER_CHECKSUM + +; +; Entrypoint +; +kstart: + mov esp, KERNEL_STACK+KERNEL_STACKSIZE ;Create kernel stack + push eax ;Multiboot magic number + push ebx ;Multiboot info + call kmain ;Call kernel entrypoint + cli ;Clear interrupts + hlt ;Halt machine + +section .bss + +; +; Kernel stack location +; +align 32 +KERNEL_STACK: + resb KERNEL_STACKSIZE \ No newline at end of file diff --git a/src/system.pas b/src/system.pas new file mode 100644 index 00000000..6f706f00 --- /dev/null +++ b/src/system.pas @@ -0,0 +1,15 @@ +unit system; + +interface + +type + cardinal = 0..$FFFFFFFF; + hresult = cardinal; + dword = cardinal; + integer = longint; + + pchar = ^char; + +implementation + +end. \ No newline at end of file