From f3c41cc6ded991a850277972bebd82f1cea22bfd Mon Sep 17 00:00:00 2001 From: Tom-on64 Date: Wed, 24 Apr 2024 20:14:35 +0200 Subject: [PATCH] Started working on Physical memory map for memory manager --- Makefile | 6 ++--- notes.txt | 8 ++++++ src/filetable.asm | 12 ++++----- src/kernel.c | 7 +++--- src/stage1.asm | 5 ++-- src/stage2.asm | 64 +++++++++++++++++++++++++++++++++++++++++------ todo.txt | 2 +- 7 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 notes.txt diff --git a/Makefile b/Makefile index 8e1c96e..eaa401a 100644 --- a/Makefile +++ b/Makefile @@ -22,9 +22,9 @@ os: $(BUILD) bootloader kernel $(C_FILES) @echo "Building kern.iso..." @dd if=/dev/zero of=kern.iso bs=512 count=2880 status=none @dd if=$(BUILD)/os.bin of=kern.iso conv=notrunc status=none - @dd if=$(BUILD)/testfont.bin of=kern.iso bs=512 seek=35 conv=notrunc status=none - @dd if=$(SRC)/fs/test.txt of=kern.iso bs=512 seek=39 conv=notrunc status=none - @dd if=$(BUILD)/calc.bin of=kern.iso bs=512 seek=40 conv=notrunc status=none + @dd if=$(BUILD)/testfont.bin of=kern.iso bs=512 seek=36 conv=notrunc status=none + @dd if=$(SRC)/fs/test.txt of=kern.iso bs=512 seek=40 conv=notrunc status=none + @dd if=$(BUILD)/calc.bin of=kern.iso bs=512 seek=41 conv=notrunc status=none @echo "\nDone!\n" $(BUILD): diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..6477ce0 --- /dev/null +++ b/notes.txt @@ -0,0 +1,8 @@ +--- Memory Map --- +0x1000 - Kernel +0x5000 - VBE Mode Info Block +0x6000 - Font +0x7000 - Filetable (0x7c00 - Bootloader) +... +0x10000 - Loaded programs + diff --git a/src/filetable.asm b/src/filetable.asm index 452f69b..7837e25 100644 --- a/src/filetable.asm +++ b/src/filetable.asm @@ -10,12 +10,12 @@ ;; filetable: - db "boot",0,0,0,0,0,0, "bin",0,0,4 - db "filetable",0, "tab",0,4,1 - db "kernel",0,0,0,0, "bin",0,5,30 - db "testfont",0,0, "fnt",0,35,4 - db "test",0,0,0,0,0,0, "txt",0,39,1 - db "calc",0,0,0,0,0,0, "bin",0,40,2 + db "boot",0,0,0,0,0,0, "bin",0,0,5 + db "filetable",0, "tab",0,5,1 + db "kernel",0,0,0,0, "bin",0,6,30 + db "testfont",0,0, "fnt",0,36,4 + db "test",0,0,0,0,0,0, "txt",0,40,1 + db "calc",0,0,0,0,0,0, "bin",0,41,2 times 512-($-$$) db 0 diff --git a/src/kernel.c b/src/kernel.c index 7e13500..5edfadf 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -11,8 +11,7 @@ #define PROMPT "#> " -// TODO: Put this at a set address -char filetable[512]; +char* filetable = (char*)0x7000; // Pretty sure we don't have to care about the bootloader at 0x7c00 // Function declarations void printFiletable(char* ft); @@ -30,14 +29,14 @@ void main() { // Screen setup // TODO: Find font in filetable - diskRead(35, 4, (char*)0x6000); // Read font from disk + diskRead(36, 4, (char*)0x6000); // Read font from disk setupScreen(); loadFont((char*)0x6000); clear(); print("kern.\n\n"); - diskRead(4, 1, filetable); // Read the filetable and store it in memory + diskRead(5, 1, filetable); // Read the filetable and store it in memory // Run Interactive Shell Program // TODO: Make it in another file diff --git a/src/stage1.asm b/src/stage1.asm index b05f18b..fa89ea7 100644 --- a/src/stage1.asm +++ b/src/stage1.asm @@ -4,6 +4,7 @@ %define VIDMEM 0xb800 %define KERNEL_LOC 0x1000 +%define FILETAB_LOC 0x7000 ; TODO: Read kernel size and location from ft %define KERNEL_SIZE 30 ; 15kB start: @@ -25,7 +26,7 @@ start: mov bx, 0x7e00 ; 2nd stage bootloader location ; CHS Location - mov al, 3 ; Number of sectors to read (2 = 1kB) + mov al, 4 ; Number of sectors to read (4 = 2kB) mov ch, 0 ; Cylinder mov dh, 0 ; Head mov cl, 2 ; Sector @@ -68,7 +69,7 @@ loadKernel: mov al, KERNEL_SIZE mov ch, 0 ; Cylinder mov dh, 0 ; Head - mov cl, 6 ; Sector + mov cl, 7 ; Sector mov dl, [driveNum] mov ah, 2 ; Read from disk diff --git a/src/stage2.asm b/src/stage2.asm index c41f7fb..8570419 100644 --- a/src/stage2.asm +++ b/src/stage2.asm @@ -7,6 +7,58 @@ start: mov [driveNum], dl + ; Physical memory map setup + xor ax, ax + mov es, ax + +memMapEntries equ 0x8500 +getMemoryMap: + mov di, memMapEntries + 4 ; 4 bytes after memMapEntries + xor ebx, ebx + xor bp, bp + mov edx, "PAMS" ; SMAP in little endian + mov eax, 0xe820 + mov dword [es:di+20], 1 ; Force valid ACPI 3.X entry + mov ecx, 24 + int 0x15 + jc .error + + cmp eax, "PAMS" + jne .error + test ebx, ebx + jz .error + +.nextEntry: + mov edx, "PAMS" ; In case BIOS messes with edx + mov ecx, 24 + mov eax, 0xe820 + int 0x15 + + jcxz .skipEntry + mov ecx, [es:di+8] ; Low 32 bits of length + or ecx, [es:di+12] ; High 32 bits of length + jz .skipEntry + + ; Valid entry + inc bp + add di, 24 ; Next entry + +.skipEntry: + test ebx, ebx + jz .done + jmp .nextEntry + +.error: + mov si, acpiFuncErr + call print + cli + hlt + +.done: + mov [memMapEntries], bp + clc + +vbeSetup: ; Setup VBE info xor ax, ax mov es, ax @@ -44,7 +96,7 @@ start: cmp ax, 0x4f jne error - ; Print values + ; Print values (Debug) ; mov ax, [modeInfoBlock.xRes] ; ror ax, 8 ; call printHex @@ -52,7 +104,6 @@ start: ; call printHex ; mov ax, 0x0e20 ; int 0x10 - ; mov ax, [modeInfoBlock.yRes] ; ror ax, 8 ; call printHex @@ -60,13 +111,11 @@ start: ; call printHex ; mov ax, 0x0e20 ; int 0x10 - ; xor ax, ax ; mov al, [modeInfoBlock.bpp] ; call printHex ; mov ax, 0x0e20 - ; int 0x10 - + ; int 0x10 ; mov al, 0x0a ; int 0x10 ; mov al, 0x0d @@ -218,6 +267,7 @@ driveNum: db 0 vbeErr: db "VBE Error!", 0 modeNotFoundErr: db "VBE Mode not found!", 0 +acpiFuncErr: db "ACPI Function not supported!", 0 ;; GDT GDT: @@ -256,7 +306,7 @@ offset: dw 0 _segment: dw 0 ; segment is a keyword mode: dw 0 -times 512-($-$$) db 0 ; End Sector 1 +times (512*2)-($-$$) db 0 ; End Sector 2 ;; Sector 2 vbeInfoBlock: @@ -331,5 +381,5 @@ modeInfoBlock: .reserved4: times 190 db 0 -times (512*3)-($-$$) db 0 ; 2 Pad out to 3 sectors +times (512*4)-($-$$) db 0 diff --git a/todo.txt b/todo.txt index d923e0c..ae8a8c0 100644 --- a/todo.txt +++ b/todo.txt @@ -3,7 +3,7 @@ - [x] Multistage Bootloader - [ ] Bootloader reads filetable to find files - [ ] Makefile refactorin' -- [ ] Write down where stuff is in memory (0x1000 - kernel, 0x4000? - filetable, 0x5000 - VBE Mode Info, 0x6000 - Font) +- [x] Write down where stuff is in memory - [ ] "Text" mode (not actually) - [ ] Scrolling - [ ] Scroll-back