From a523e17d27a3a5c56c24f7d17d02bb05e0fc06ec Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 12:55:29 -0400 Subject: [PATCH 01/25] moved stuff that is only referenced in Stage2 to stage2 bss --- include/mem.inc | 28 ++++------------------------ src/stage2/stage2.nasm | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/include/mem.inc b/include/mem.inc index ca8ac77..0ac49da 100755 --- a/include/mem.inc +++ b/include/mem.inc @@ -43,13 +43,6 @@ ; 0x0000000100000000 ??? ??? (whatever exists) RAM -- free for use (PAE/64bit)/More Extended memory ; ???????????????? ??? ??? Potentially usable for memory mapped PCI devices in modern hardware (but typically not, due to backward compatibility) -; 0x2700 -> 0x28FF -%define disk_buffer 0x2700 -; 0x2900 -> 0x2AFF -%define fat_buffer 0x2900 -; 0x2B00 -> 0x2CFF -%define dir_buffer 0x2B00 - ; copy of partition table, 72 bytes %define partition_table 0x3000 %define partition_table_SIZE 72 @@ -64,15 +57,6 @@ %define fat32_ebpb 0x306E %define fat32_ebpb_SIZE 54 -; FAT32 FSInfo, 512 bytes -;0x30A2 -%define fat32_fsinfo 0x30B0 -%define fat32_fsinfo_SIZE 512 - -; some stored state for the fat32 driver -;0x32A2 -%define fat32_state 0x34B0 -%define fat32_state_SIZE 32 ; next free space is 0x32D0 %define fat32_nc_data 0x35D0 @@ -81,14 +65,6 @@ ; lba_packet for raw_disk_read %define lba_packet 0x4000 -%define BIOSMemoryMap 0x4200 -%define SteviaInfo 0x5200 - - -; High memory addresses for loading kernel (for use with unreal mode and 32bit override) - -; file load buffer at 16MB -%define HMEM_load_buffer 0x1000000 ;PhysicalAddress = Segment * 16 + Offset @@ -104,6 +80,10 @@ ; Segement = linear >> 4 (top 16 bits) ; offset = linear & 0x0F (low 4 bits) +struc EarlyBootStruct_t + .lba_packet_offset resw 1 +endstruc + ; 20 bytes, passed to loaded kernel struc SteviaInfoStruct_t .MemoryMapPtr resd 1 diff --git a/src/stage2/stage2.nasm b/src/stage2/stage2.nasm index ca7484b..8d4df81 100755 --- a/src/stage2/stage2.nasm +++ b/src/stage2/stage2.nasm @@ -472,8 +472,24 @@ STAGE2_SIG: dd 0xDEADBEEF ; Signature to mark the end of the stage section .bss follows=.sign align 512 begin_bss: -buffer1 resb 512 -buffer2 resb 512 -buffer3 resb 512 -buffer4 resb 512 + +disk_buffer resb 512 + +fat_buffer resb 512 + +dir_buffer resb 512 + +fat_fsinfo resb 512 + +fat32_state resb FAT32_State_t_size + +%define BIOSMemoryMap_SIZE 4096 +BIOSMemoryMap resb 4096 + +SteviaInfo resd 4 + +align 16 +stack_bottom: + stack resb 4096 +stack_top: end_bss: \ No newline at end of file From 31c45051e6b6b6576f49e2e9ff71caa7ded44550 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 12:57:56 -0400 Subject: [PATCH 02/25] minor cleanup related to memory refactor --- src/mbr/mbr.nasm | 23 ++--------------------- src/vbr/vbr.nasm | 16 +++++++++------- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/mbr/mbr.nasm b/src/mbr/mbr.nasm index cfec883..efb5e84 100755 --- a/src/mbr/mbr.nasm +++ b/src/mbr/mbr.nasm @@ -25,14 +25,11 @@ [WARNING -reloc-abs-word] ; Yes, we use absolute addresses. surpress these warnings. [map all mbr.map] %define __STEVIA_MBR - jmp short (init - $$) nop ; ############### -; ; Headers/Includes/Definitions -; ; ############### %include "util/bochs_magic.inc" @@ -43,11 +40,7 @@ nop %include "error_codes.inc" %include "partition_table.inc" -; ############### -; End Section -; ############### - -ALIGN 4, db 0x90 +ALIGN 4 init: cli ; We do not want to be interrupted @@ -69,23 +62,15 @@ init: jmp 0:main ; ############### -; ; Extra/Shared Functions -; ; ############### %include "util/kmem_func.nasm" %include "util/error_func.nasm" -; ############### -; End Section -; ############### - ; ; bp - 2 : uint8_t boot_drive ; bp - 4 : uint16_t part_offset -; - ALIGN 4, db 0x90 main: mov byte [bp - 2], dl ; BIOS passes drive number in DL @@ -158,7 +143,7 @@ main: mov si, word [bp - 4] mov dl, byte [bp - 2] - jmp 0:0x7C00 + jmp word 0x0000:0x7C00 ; ############### ; @@ -168,10 +153,6 @@ main: %include 'BIOS/func/ext_read.nasm' -; ############### -; End Section -; ############### - %assign bytes_remaining (440 - ($ - $$)) %warning MBR has bytes_remaining bytes remaining for code (MAX: 440 bytes) times ((512 - 72) - ($ - $$)) nop ; Fill the rest of sector with nop diff --git a/src/vbr/vbr.nasm b/src/vbr/vbr.nasm index 009f7ae..c68a37b 100755 --- a/src/vbr/vbr.nasm +++ b/src/vbr/vbr.nasm @@ -24,7 +24,7 @@ [WARNING -reloc-abs-word] [map all vbr.map] ; Yes, we use absolute addresses. surpress these warnings. %define __STEVIA_VBR - +section .text __ENTRY: jmp short (init - $$) nop @@ -51,11 +51,8 @@ times 54 db 0x00 %include "error_codes.inc" %include "fat32/bpb_offset_bx.inc" -; ############### -; End Section -; ############### -ALIGN 4, db 0x90 +ALIGN 4 init: cli ; We do not want to be interrupted @@ -85,6 +82,9 @@ init: ; End Section ; ############### +; +; byte boot_drive @ bp - 2 +; word part_offset @ bp - 4 ALIGN 4, db 0x90 main: mov byte [bp - 2], dl ; boot_drive @@ -101,7 +101,7 @@ main: push ax mov ax, fat32_bpb ; defined in memory.inc, destination push ax - call kmemcpy ; copy bpb to memory + call kmemcpy ; copy bpb & ebpb to memory add sp, 0x6 mov bx, fat32_bpb ; bx now points to aligned memory structure @@ -130,6 +130,8 @@ main: add sp, 0xC .check_sig: + ; BUG: this is hard coded to check @ ((0x7E0 << 4) + 0x7FFC)...i.e (STAGE2_ENTRY + (STAGE2_MAX_BYTES - 4)) + ; this should be removed or done properly mov ax, 0x7E0 mov fs, ax cmp dword [fs:0x7FFC], 0xDEADBEEF @@ -140,7 +142,7 @@ main: .sig_ok: mov si, word [bp - 4] mov dl, byte [bp - 2] - jmp 0:0x7E00 + jmp word 0x0000:0x7E00 ; ############### ; Required BIOS function(s) From 3fbfcbef53452e972111d9f541aeb8b62c163501 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 12:58:16 -0400 Subject: [PATCH 03/25] BIOSMemoryMap is a location in bss now, use lea --- include/BIOS/func/E820_memory_map.nasm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/BIOS/func/E820_memory_map.nasm b/include/BIOS/func/E820_memory_map.nasm index 6752bf3..abc944a 100644 --- a/include/BIOS/func/E820_memory_map.nasm +++ b/include/BIOS/func/E820_memory_map.nasm @@ -48,7 +48,8 @@ GetMemoryMap: mov eax, 0xE820 ; select 0xE820 function xor ebx, ebx ; Continuation value, 0 for the first call - mov dx, (BIOSMemoryMap >> 4) + lea dx, [BIOSMemoryMap] + shr dx, 4 mov es, dx xor di, di ; (BIOSMemoryMap >> 4):0 makes di an index into BIOSMemoryMap From bd80f48b1b5f89daafed5e980d25d5614040c4c3 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 12:58:32 -0400 Subject: [PATCH 04/25] use NASM defined size value from structure macro --- include/fat32/FAT32_SYS.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/fat32/FAT32_SYS.inc b/include/fat32/FAT32_SYS.inc index 9c8d01b..9f00869 100644 --- a/include/fat32/FAT32_SYS.inc +++ b/include/fat32/FAT32_SYS.inc @@ -28,7 +28,7 @@ ALIGN 4, db 0x90 InitFATDriver: __CDECL16_ENTRY .func: - mov ax, fat32_state_SIZE + mov ax, FAT32_State_t_size push ax ; length of fat32_state structure xor ax, ax push ax ; init fat32_state with zero From 9656a9465bf2e9c3aaa677059dda78c98a223858 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 14:48:26 -0400 Subject: [PATCH 05/25] first version of kmemcpy5/kmemset4 --- include/util/kmemcpy5_func.nasm | 53 +++++++++++++++++++++++++++++++++ include/util/kmemset4_func.nasm | 47 +++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 include/util/kmemcpy5_func.nasm create mode 100644 include/util/kmemset4_func.nasm diff --git a/include/util/kmemcpy5_func.nasm b/include/util/kmemcpy5_func.nasm new file mode 100644 index 0000000..81a6b64 --- /dev/null +++ b/include/util/kmemcpy5_func.nasm @@ -0,0 +1,53 @@ +; Copyright (c) 2024 Elaina Claus +; +; Permission is hereby granted, free of charge, to any person obtaining a copy +; of this software and associated documentation files (the "Software"), to deal +; in the Software without restriction, including without limitation the rights +; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +; copies of the Software, and to permit persons to whom the Software is +; furnished to do so, subject to the following conditions: +; +; The above copyright notice and this permission notice shall be included in all +; copies or substantial portions of the Software. +; +; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +; SOFTWARE. + +%ifndef __INC_KMEMCPY5_FUNC +%include 'cdecl16.inc' + +; uint8_t* kmemset(word dest_segment, word dest, word src_segment, word src, byte len); +; not overlap safe, only for +ALIGN 4, db 0x90 +kmemcpy5: + __CDECL16_ENTRY + push ds + push es +.setup_segments + mov ax, [bp + 4] + mov ds, ax ; destination segment + + mov ax, [ bp + 8] + mov es, ax ; source segment +.func: + mov cx, [bp + 12] ; len + mov si, [bp + 10] ; src + mov di, [bp + 6] ; dest + + cld ; ensure we are incrementing + rep movsb ; move ds:si -> es:di + mov ax, di ; return pointer to dest +.restore_segments: + pop es + pop ds +.endf: + __CDECL16_EXIT + ret + +%define __INC_KMEMCPY5_FUNC +%endif \ No newline at end of file diff --git a/include/util/kmemset4_func.nasm b/include/util/kmemset4_func.nasm new file mode 100644 index 0000000..9f0a734 --- /dev/null +++ b/include/util/kmemset4_func.nasm @@ -0,0 +1,47 @@ +; Copyright (c) 2024 Elaina Claus +; +; Permission is hereby granted, free of charge, to any person obtaining a copy +; of this software and associated documentation files (the "Software"), to deal +; in the Software without restriction, including without limitation the rights +; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +; copies of the Software, and to permit persons to whom the Software is +; furnished to do so, subject to the following conditions: +; +; The above copyright notice and this permission notice shall be included in all +; copies or substantial portions of the Software. +; +; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +; SOFTWARE. + +%ifndef __INC_KMEMSET4_FUNC +%include 'cdecl16.inc' + +; word kmemset_byte(word segment, word dst, byte val, word len); +ALIGN 4, db 0x90 +kmemset4: + __CDECL16_ENTRY +.setup_segment: + push es + mov ax, [bp + 4] + mov es, ax + .func: + mov cx, [bp + 10] ; size_t len + mov al, [bp + 8] ; uint8_t val + mov di, [bp + 6] ; word dst + + cld + rep stosb ; move al -> es:di + mov ax, di ; return pointer to dest +.restore_segments: + pop es +.endf: + __CDECL16_EXIT + ret + +%endif +%define __INC_KMEMSET4_FUNC \ No newline at end of file From 73b42c66fd7246ee286237caf39c1989e9a8ecd6 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 15:38:24 -0400 Subject: [PATCH 06/25] renamed mem.inc and removed unused items --- include/{mem.inc => early_mem.inc} | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) rename include/{mem.inc => early_mem.inc} (88%) diff --git a/include/mem.inc b/include/early_mem.inc similarity index 88% rename from include/mem.inc rename to include/early_mem.inc index 0ac49da..d2b3ea2 100755 --- a/include/mem.inc +++ b/include/early_mem.inc @@ -18,7 +18,7 @@ ; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ; SOFTWARE. -%ifndef __INC_MEM +%ifndef __INC_EARLY_MEM ; ## Generic Low mem map (from osdev wiki) ## @@ -60,13 +60,11 @@ ; next free space is 0x32D0 %define fat32_nc_data 0x35D0 -%define fat32_nc_data_size 16 +%define fat32_nc_data_SIZE 16 ; lba_packet for raw_disk_read %define lba_packet 0x4000 - - ;PhysicalAddress = Segment * 16 + Offset %define SEG_TO_LINEAR(s,o) ((s << 4) + o) @@ -76,21 +74,5 @@ ; Seg = (physical - offset) / 16 %define LINEAR_TO_SEGMENT(p,o) ((p - o) >> 4) -; create normalized linear addres from seg:off (16:4) -; Segement = linear >> 4 (top 16 bits) -; offset = linear & 0x0F (low 4 bits) - -struc EarlyBootStruct_t - .lba_packet_offset resw 1 -endstruc - -; 20 bytes, passed to loaded kernel -struc SteviaInfoStruct_t - .MemoryMapPtr resd 1 - .MemoryMapEntries resd 1 - .BPBDataPtr resd 1 - .EBPBDataPtr resd 1 -endstruc - %endif -%define __INC_MEM \ No newline at end of file +%define __INC_EARLY_MEM \ No newline at end of file From 276fe7057297c0ee08234c02ef4857d0e4956472 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 15:38:47 -0400 Subject: [PATCH 07/25] use early_mem.inc --- src/mbr/mbr.nasm | 4 ++-- src/vbr/vbr.nasm | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mbr/mbr.nasm b/src/mbr/mbr.nasm index efb5e84..410f9a0 100755 --- a/src/mbr/mbr.nasm +++ b/src/mbr/mbr.nasm @@ -36,7 +36,7 @@ nop %include "cdecl16.inc" %include "entry.inc" %include "config.inc" -%include "mem.inc" +%include "early_mem.inc" %include "error_codes.inc" %include "partition_table.inc" @@ -136,7 +136,7 @@ main: push ax mov ax, DiskSig ; start of partition table push ax - mov ax, partition_table ; defined in memory.inc + mov ax, partition_table ; defined in early_mem.inc push ax call kmemcpy ; copy partition table to memory add sp, 0x6 diff --git a/src/vbr/vbr.nasm b/src/vbr/vbr.nasm index c68a37b..3bc198b 100755 --- a/src/vbr/vbr.nasm +++ b/src/vbr/vbr.nasm @@ -47,7 +47,7 @@ times 54 db 0x00 %include "cdecl16.inc" %include "entry.inc" %include "config.inc" -%include "mem.inc" +%include "early_mem.inc" %include "error_codes.inc" %include "fat32/bpb_offset_bx.inc" From 1d84f526908c59badc2a4a8b3dd5be579f8e75ae Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 15:39:04 -0400 Subject: [PATCH 08/25] definition corrections in part table stuff --- include/partition_table.inc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/partition_table.inc b/include/partition_table.inc index ddd9ab6..abd895a 100755 --- a/include/partition_table.inc +++ b/include/partition_table.inc @@ -34,15 +34,15 @@ struc PartEntry_t .chs_start resb 3 .part_type resb 1 .chs_end resb 3 - .lba_start resb 4 - .lba_length resb 4 + .lba_start resd 1 + .lba_length resd 1 endstruc struc PartTable_t - .partition1 resb 16 - .partition2 resb 16 - .partition3 resb 16 - .partition4 resb 16 + .partition1 resb PartEntry_t_size + .partition2 resb PartEntry_t_size + .partition3 resb PartEntry_t_size + .partition4 resb PartEntry_t_size endstruc %endif From 54b7a81648c41e4963b0725894729bbc0f7d9783 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 15:39:16 -0400 Subject: [PATCH 09/25] correct typo --- include/util/kmemcpy5_func.nasm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/util/kmemcpy5_func.nasm b/include/util/kmemcpy5_func.nasm index 81a6b64..d3c698e 100644 --- a/include/util/kmemcpy5_func.nasm +++ b/include/util/kmemcpy5_func.nasm @@ -28,7 +28,7 @@ kmemcpy5: __CDECL16_ENTRY push ds push es -.setup_segments +.setup_segments: mov ax, [bp + 4] mov ds, ax ; destination segment @@ -48,6 +48,6 @@ kmemcpy5: .endf: __CDECL16_EXIT ret - + %define __INC_KMEMCPY5_FUNC %endif \ No newline at end of file From f535840572d2c1cfb7425e84b97751f65e99a07b Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 15:41:39 -0400 Subject: [PATCH 10/25] big step...and also its completely broken right now we are going to use 0x07E0 as our base segment so we have a bit more memory to work with until we are fully in 32bit protected mode. this commit is broken but is just one step towards a working release. --- src/stage2/stage2.nasm | 96 +++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 44 deletions(-) diff --git a/src/stage2/stage2.nasm b/src/stage2/stage2.nasm index 8d4df81..ba29d1c 100755 --- a/src/stage2/stage2.nasm +++ b/src/stage2/stage2.nasm @@ -19,7 +19,7 @@ ; SOFTWARE. [BITS 16] -[ORG 0X7E00] +[ORG 0x0000] [CPU KATMAI] [map all stage2.map] [WARNING -reloc-abs-byte] @@ -27,19 +27,15 @@ [WARNING -reloc-abs-dword] ; Yes, we use absolute addresses. surpress these warnings. %define __STEVIA_STAGE2 - - ; ############### -; ; Headers/Includes/Definitions -; ; ############### %include "util/bochs_magic.inc" %include "cdecl16.inc" %include "entry.inc" %include "config.inc" -%include "mem.inc" +%include "early_mem.inc" %include "error_codes.inc" %macro print_string 1 @@ -50,39 +46,72 @@ %endmacro section .text -org 0x7E00 +org 0x0000 + begin_text: jmp short (init - $$) nop ALIGN 4, db 0x90 init: - cli ; We do not want to be interrupted + cli ; We do not want to be interrupted - xor ax, ax ; 0 AX - mov ds, ax ; Set segment registers to 0 - mov es, ax ; * - mov fs, ax ; * - mov gs, ax ; * + mov ax, 0x07E0 ; 0x07E0 i.e 0x7E00 >> 4 in AX + mov ds, ax ; Set segment registers to 0 + mov es, ax ; * + mov fs, ax ; * + mov gs, ax ; * - mov ss, ax ; Set Stack Segment to 0 - mov sp, EARLY_STACK_START ; Set Stack Pointer + mov ss, ax ; Set Stack Segment to 0x07E0 + mov sp, stack_top ; Set Stack Pointer mov bp, sp sub sp, 0x20 ; 32 bytes for local varibles sti - jmp 0:main + jmp word 0x07E0:main ; ############### -; -; Extra/Shared Functions -; +; Functions ; ############### %include "util/kmem_func.nasm" +%include "util/kmemcpy5_func.nasm" +%include "util/kmemset4_func.nasm" %include "util/error_func.nasm" +; ############### +; FAT32 Driver +; ############### + +boot_drive_ptr: + dw 0x0000 +partition_offset_ptr: + dw 0x0000 + +%include 'fat32/FAT32_SYS.inc' + +; ############### +; BIOS functions +; ############### + +%include 'BIOS/BIOS_SYS.inc' + +; structures + +struc SteviaInfoStruct_t + .MemoryMapPtr resd 1 + .MemoryMapEntries resd 1 +endstruc + +struc EarlyBootStruct_t + .lba_packet resb LBAPkt_t_size + .partition_table resb partition_table_SIZE + .fat32_bpb resb fat32_bpb_SIZE + .fat32_ebpb resb fat32_ebpb_SIZE + .fat32_nc_data resb fat32_nc_data_SIZE +endstruc + ; bp - 2 : uint8_t boot_drive ; bp - 4 : uint16_t part_offset ALIGN 4, db 0x90 @@ -136,27 +165,6 @@ hcf: hlt jmp short (hcf - $$) -; ############### -; -; FAT32 Driver -; -; ############### - -boot_drive_ptr: - dw 0x0000 -partition_offset_ptr: - dw 0x0000 - -%include 'fat32/FAT32_SYS.inc' - -; ############### -; -; BIOS functions -; -; ############### - -%include 'BIOS/BIOS_SYS.inc' - ; ############################## ; ; SYSTEM CONFIGURATION FUNCTIONS @@ -465,7 +473,7 @@ end_data: ; section start location needs to be a 'critical expression' ; i.e resolvable at build time, we are setting 0x7E00 as the offset since -section .sign start=((MAX_STAGE2_BYTES - 512) + 0x7E00) +section .sign start=(MAX_STAGE2_BYTES - 512) times ( (512 - 4) - ($ -$$)) db 0x90 ; nop STAGE2_SIG: dd 0xDEADBEEF ; Signature to mark the end of the stage2 @@ -481,15 +489,15 @@ dir_buffer resb 512 fat_fsinfo resb 512 -fat32_state resb FAT32_State_t_size - %define BIOSMemoryMap_SIZE 4096 BIOSMemoryMap resb 4096 SteviaInfo resd 4 +fat32_state resb FAT32_State_t_size align 16 stack_bottom: - stack resb 4096 + stack resb 8192 stack_top: +stage2_main_redzone resb 32 end_bss: \ No newline at end of file From 2fa03c31d70bad2b69a09d716c639a3298175008 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 20:25:04 -0400 Subject: [PATCH 11/25] partially roll back changing the segment to 0x07E0 --- include/util/error_func.nasm | 4 +- src/stage2/stage2.nasm | 114 +++++++++++++++-------------------- 2 files changed, 51 insertions(+), 67 deletions(-) diff --git a/include/util/error_func.nasm b/include/util/error_func.nasm index 6c2aa92..5682266 100644 --- a/include/util/error_func.nasm +++ b/include/util/error_func.nasm @@ -22,9 +22,7 @@ %macro ERROR 1 mov al, %1 ; al = 1 byte error code mapped to ascii values - db 0xEA ; jmp far imm16:imm16 - dw error ; error_far_seg - dw 0x0000 ; error_far_ptr + jmp error %endmacro ; pass error as ascii character in al, errors a-zA-Z or 0-9 diff --git a/src/stage2/stage2.nasm b/src/stage2/stage2.nasm index ba29d1c..78b4980 100755 --- a/src/stage2/stage2.nasm +++ b/src/stage2/stage2.nasm @@ -19,13 +19,14 @@ ; SOFTWARE. [BITS 16] -[ORG 0x0000] +[ORG 0x7E00] [CPU KATMAI] [map all stage2.map] [WARNING -reloc-abs-byte] [WARNING -reloc-abs-word] [WARNING -reloc-abs-dword] ; Yes, we use absolute addresses. surpress these warnings. %define __STEVIA_STAGE2 +%define __STAGE2_SEGMENT 0x0000 ; ############### ; Headers/Includes/Definitions @@ -46,7 +47,7 @@ %endmacro section .text -org 0x0000 +org 0x7E00 begin_text: jmp short (init - $$) @@ -56,20 +57,21 @@ ALIGN 4, db 0x90 init: cli ; We do not want to be interrupted - mov ax, 0x07E0 ; 0x07E0 i.e 0x7E00 >> 4 in AX + mov ax, __STAGE2_SEGMENT ; configured segment mov ds, ax ; Set segment registers to 0 mov es, ax ; * mov fs, ax ; * mov gs, ax ; * - mov ss, ax ; Set Stack Segment to 0x07E0 + mov ss, ax ; Set Stack Segment to data segment mov sp, stack_top ; Set Stack Pointer + mov bp, sp sub sp, 0x20 ; 32 bytes for local varibles sti - jmp word 0x07E0:main + jmp word __STAGE2_SEGMENT:main ; ############### ; Functions @@ -126,10 +128,12 @@ main: mov eax, dword [STAGE2_SIG] cmp eax, 0xDEADBEEF - je main.signature_present + je main.bss_init ERROR STAGE2_SIGNATURE_MISSING -.signature_present: +.bss_init: + nop ; placeholder +.stage2_main: call SetTextMode call disable_cursor print_string HelloPrompt_cstr @@ -288,14 +292,19 @@ EnterUnrealMode: push ss push cs ; save real mode code selector + xor ax, ax ; pop ax ; save cs to ax to setup far jump - mov word [ds:__UNREAL_SEGMENT], ax + mov word [__UNREAL_SEGMENT], ax + + shl eax, 4 + add eax, unreal_gdt_start ; ax contains the physical address of gdt table + mov dword [unreal_gdt_ptr], eax ; update gdt ptr in unreal_gdt_info + + lgdt [dword ((__STAGE2_SEGMENT << 4) + unreal_gdt_info)] ; calculate linear address for lgdt to consume - lgdt [unreal_gdt_info] mov eax, cr0 or al,1 ; set pmode bit mov cr0, eax ; switch to pmode - jmp $+2 ; clear instruction cache ;jmp far 0x0008:EnterUnrealMode.load_cs db 0xEA ; jmp far imm16:imm16 @@ -310,9 +319,8 @@ EnterUnrealMode: mov fs, bx mov gs, bx ; other data/stack to index 2 (off 0x10) - and al,0xFE ; toggle bit 1 of cr0 - mov cr0, eax ; back to realmode - jmp $+2 ; clear instruction cache again + and al,0xFE ; toggle bit 1 of cr0 + mov cr0, eax ; back to realmode ;jmp far 0x0008:EnterUnrealMode.unload_cs db 0xEA ; jmp far imm16:imm16 @@ -375,58 +383,36 @@ IntToHex_table: db '0123456789ABCDEF' ; see docs/gdt.txt for a quick refresher on GDT -ALIGN 4, db 0 +ALIGN 16, db 0 unreal_gdt_info: unreal_gdt_size: dw (unreal_gdt_end - unreal_gdt_start) - 1 - unreal_gdt_ptr: dd unreal_gdt_start + unreal_gdt_ptr: dd ((__STAGE2_SEGMENT << 4) + unreal_gdt_start) unreal_gdt_start: - ; entry 0 + ; entry 0 (null descriptor) dq 0 ; first entry is null - ; entry 1 (4 GiB flat code map) - dw 0xFFFF ; Segment Limit 15:0 (Same large limit as data segment) + ; entry 1 (16-bit code segment with 4 GiB flat mapping) + dw 0xFFFF ; Segment Limit 15:0 dw 0x0000 ; Base Address 15:0 - db 0x00 ; Base Address 23:16 - db 1001_1010b ; Access Byte: 1001_1010b for executable code - db 1000_1111b ; Flags and Segment Limit 19:16 (Same as data segment) - db 0x00 ; Base Address 31:24 + db 0000_0000b ; Base Address 23:16 + db 1001_1010b ; Access Byte: executable, readable, present + db 0000_1111b ; Flags: 16-bit, Granularity = 4KiB + db 0000_0000b ; Base Address 31:24 - ; entry 2 (4 GiB flat data map) - dw 0xFFFF ; 0:15 limit - dw 0x0000 ; 0:15 base - db 0x00 ; 16:23 base - db 1001_0010b ; bit 0:4 = S/Type, [1, DC, RW, Ac] or [0, E, RW, Ac] - ; bit 5:6 = Privl - ; bit 7 = Present - - db 1000_1111b ; bit 0:3 = 16:19 of Limit - ; bit 4 = Availible to software bit - ; bit 5 = Reserved (?) - ; bit 6 = D/B bit, depending on if this is code/data 1 = 32 bit operands or stack size - ; bit 7 = Granularity bit. 1 = multiply limit by 4096 - db 0x00 ; base 24:31 - ; at the end of the day... - ; base = 0x00000000 - ; limit = 0xFFFFF - ; Accessed = 0, ignore this field - ; RW = 1, data is Read/Write - ; E = 0, Expand up, valid data is from base -> limit, if 1 valid data is from (limit + 1) -> base - ; C/D = 0, Segment is a data segment - ; S = 1, Segment is a system segment - ; Privl = 00b, Ring0 segment - ; Pr = 1, segment is present - ; AVL = 0, ignore this field - ; D/B bit = 0, 16bit code/stack - ; Gr = 1, multiply limit by 4096 + ; entry 2 (16-bit data segment with 4 GiB flat mapping) + dw 0xFFFF ; Segment Limit 15:0 + dw 0x0000 ; Base Address 15:0 + db 0000_0000b ; Base Address 23:16 + db 1001_0010b ; Access Byte: readable, writable, present + db 0000_1111b ; Flags: 16-bit, Granularity = 4KiB + db 0000_0000b ; Base Address 31:24 unreal_gdt_end: -ALIGN 4, db 0 +ALIGN 16, db 0 gdt32_info: gdt32_size: dw (gdt32_end - gdt32_start) - 1 - gdt32_ptr: dd gdt32_start - -; check above for detailed information + gdt32_ptr: dd ((__STAGE2_SEGMENT << 4) + gdt32_start) gdt32_start: dq 0 .gdt32_code: @@ -473,14 +459,22 @@ end_data: ; section start location needs to be a 'critical expression' ; i.e resolvable at build time, we are setting 0x7E00 as the offset since -section .sign start=(MAX_STAGE2_BYTES - 512) -times ( (512 - 4) - ($ -$$)) db 0x90 ; nop -STAGE2_SIG: dd 0xDEADBEEF ; Signature to mark the end of the stage2 +section .sign start=((MAX_STAGE2_BYTES - 512) + 0x7E00) +times ((512 - 4) - ($ -$$) ) db 0x90 ; nop +STAGE2_SIG: dd 0xDEADBEEF ; Signature to mark the end of the stage2 section .bss follows=.sign align 512 begin_bss: +stack_bottom: + stack resb 4096 +stack_top: +stage2_main_redzone resb 32 +SteviaInfo resd 4 +fat32_state resb FAT32_State_t_size + +align 512 disk_buffer resb 512 fat_buffer resb 512 @@ -492,12 +486,4 @@ fat_fsinfo resb 512 %define BIOSMemoryMap_SIZE 4096 BIOSMemoryMap resb 4096 -SteviaInfo resd 4 -fat32_state resb FAT32_State_t_size - -align 16 -stack_bottom: - stack resb 8192 -stack_top: -stage2_main_redzone resb 32 end_bss: \ No newline at end of file From d1f8ec4a9e4c023ef92b22adfc99bcfa6b875f72 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 21:21:43 -0400 Subject: [PATCH 12/25] follow standard proceedure and reallocate the mbr to 0x600 --- include/entry.inc | 2 +- src/mbr/mbr.nasm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/entry.inc b/include/entry.inc index fa5e710..b21aa8d 100755 --- a/include/entry.inc +++ b/include/entry.inc @@ -22,7 +22,7 @@ ; 8KiB from 0x2500 -> 0x500 %define EARLY_STACK_START 0x2500 -%define MBR_ENTRY 0x7A00 +%define MBR_ENTRY 0x0600 %define VBR_ENTRY 0x7C00 %define STAGE2_ENTRY 0x7E00 diff --git a/src/mbr/mbr.nasm b/src/mbr/mbr.nasm index 410f9a0..9cbd24c 100755 --- a/src/mbr/mbr.nasm +++ b/src/mbr/mbr.nasm @@ -19,7 +19,7 @@ ; SOFTWARE. [BITS 16] -[ORG 0x7A00] +[ORG 0x0600] [CPU KATMAI] [WARNING -reloc-abs-byte] [WARNING -reloc-abs-word] ; Yes, we use absolute addresses. surpress these warnings. From 3d3bcc6ea51dd741c6142ea10a7d25a7eb2eeb07 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 22:13:12 -0400 Subject: [PATCH 13/25] reduced stage2 to 24KiB --- include/config.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/config.inc b/include/config.inc index db74252..ea4bbd7 100755 --- a/include/config.inc +++ b/include/config.inc @@ -21,7 +21,7 @@ %ifndef __INC_STEVIA_CONFIG %define SECTOR_SIZE 512 -%define STAGE2_SECTOR_COUNT 0x40 +%define STAGE2_SECTOR_COUNT 0x30 ; 32 KiB %define MAX_STAGE2_BYTES (SECTOR_SIZE * STAGE2_SECTOR_COUNT) From bd40d6d3247128a7c27ac7c691b575cd28e7fcf9 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 22:14:47 -0400 Subject: [PATCH 14/25] relocate code sections to end up with stage2 starting at 0x500 --- include/entry.inc | 4 ++-- src/mbr/mbr.nasm | 27 ++++++++++++++------------- src/stage2/stage2.nasm | 29 +++++++++++++++++++---------- src/vbr/vbr.nasm | 18 +++++++----------- 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/include/entry.inc b/include/entry.inc index b21aa8d..0d276ee 100755 --- a/include/entry.inc +++ b/include/entry.inc @@ -21,10 +21,10 @@ %ifndef __INC_ENTRY ; 8KiB from 0x2500 -> 0x500 -%define EARLY_STACK_START 0x2500 +%define EARLY_STACK_START 0xFFFF %define MBR_ENTRY 0x0600 %define VBR_ENTRY 0x7C00 -%define STAGE2_ENTRY 0x7E00 +%define STAGE2_ENTRY 0x0500 %endif %define __INC_ENTRY \ No newline at end of file diff --git a/src/mbr/mbr.nasm b/src/mbr/mbr.nasm index 9cbd24c..a382ba6 100755 --- a/src/mbr/mbr.nasm +++ b/src/mbr/mbr.nasm @@ -42,16 +42,18 @@ nop ALIGN 4 init: - cli ; We do not want to be interrupted + cli ; We do not want to be interrupted - xor ax, ax ; 0 AX - mov ds, ax ; Set segment registers to 0 - - mov ss, ax ; Set Stack Segment to 0 - mov sp, EARLY_STACK_START ; Setup stack - mov bp, sp ; base ptr = stack ptr - sub sp, 0x20 ; local varible space + xor ax, ax ; 0 AX + mov ds, ax ; Set segment registers to 0 + mov es, ax + + mov ss, ax ; Set Stack Segment to 0 + mov sp, EARLY_STACK_START ; Setup stack + mov bp, sp ; base ptr = stack ptr + sub sp, 0x10 ; local varible space + xor cx, cx mov ch, 0x01 ; 256 WORDs in MBR (512 bytes), 0x0100 in cx mov si, 0x7C00 ; Current MBR Address (loaded here by BIOS) mov di, MBR_ENTRY ; New MBR Address (our new relocation address) @@ -115,12 +117,11 @@ main: mov dword eax, dword [bx + PartEntry_t.lba_start] push dword eax ; lba - xor ax, ax - push ax ; offset = 0 - mov ax, VBR_ENTRY - shr ax, 4 - push ax ; segment = 7C0 + push ax ; offset = 0x7c00 + + xor ax, ax + push ax ; segment = 0 ; uint8_t read_stage2_raw(uint16_t buf_segment, uint16_t buf_offset, ; uint32_t lba, diff --git a/src/stage2/stage2.nasm b/src/stage2/stage2.nasm index 78b4980..08536e4 100755 --- a/src/stage2/stage2.nasm +++ b/src/stage2/stage2.nasm @@ -19,7 +19,7 @@ ; SOFTWARE. [BITS 16] -[ORG 0x7E00] +[ORG 0x0500] ; IF YOU CHANGE ORG CHANGE THE SIGN OFFSET AT THE END [CPU KATMAI] [map all stage2.map] [WARNING -reloc-abs-byte] @@ -47,7 +47,7 @@ %endmacro section .text -org 0x7E00 +org 0x0500 begin_text: jmp short (init - $$) @@ -57,6 +57,18 @@ ALIGN 4, db 0x90 init: cli ; We do not want to be interrupted + mov cx, (end_bss - begin_bss) ; count = bss length + + mov ax, begin_bss + shr ax, 4 + mov es, ax ; es = begining of bss section + + xor ax, ax + mov di, ax ; dst = 0 + + cld + rep stosb ; zero bss section + mov ax, __STAGE2_SEGMENT ; configured segment mov ds, ax ; Set segment registers to 0 mov es, ax ; * @@ -128,11 +140,8 @@ main: mov eax, dword [STAGE2_SIG] cmp eax, 0xDEADBEEF - je main.bss_init + je main.stage2_main ERROR STAGE2_SIGNATURE_MISSING - -.bss_init: - nop ; placeholder .stage2_main: call SetTextMode call disable_cursor @@ -396,8 +405,9 @@ unreal_gdt_start: dw 0xFFFF ; Segment Limit 15:0 dw 0x0000 ; Base Address 15:0 db 0000_0000b ; Base Address 23:16 + db 1001_1010b ; Access Byte: executable, readable, present - db 0000_1111b ; Flags: 16-bit, Granularity = 4KiB + db 1000_1111b ; 24:20 G/DB/L/AVL & SegLimit 19:16 db 0000_0000b ; Base Address 31:24 ; entry 2 (16-bit data segment with 4 GiB flat mapping) @@ -405,7 +415,7 @@ unreal_gdt_start: dw 0x0000 ; Base Address 15:0 db 0000_0000b ; Base Address 23:16 db 1001_0010b ; Access Byte: readable, writable, present - db 0000_1111b ; Flags: 16-bit, Granularity = 4KiB + db 1000_1111b ; Flags: 16-bit, Granularity = 4KiB db 0000_0000b ; Base Address 31:24 unreal_gdt_end: @@ -459,7 +469,7 @@ end_data: ; section start location needs to be a 'critical expression' ; i.e resolvable at build time, we are setting 0x7E00 as the offset since -section .sign start=((MAX_STAGE2_BYTES - 512) + 0x7E00) +section .sign start=((MAX_STAGE2_BYTES - 512) + 0x0500) times ((512 - 4) - ($ -$$) ) db 0x90 ; nop STAGE2_SIG: dd 0xDEADBEEF ; Signature to mark the end of the stage2 @@ -474,7 +484,6 @@ stage2_main_redzone resb 32 SteviaInfo resd 4 fat32_state resb FAT32_State_t_size -align 512 disk_buffer resb 512 fat_buffer resb 512 diff --git a/src/vbr/vbr.nasm b/src/vbr/vbr.nasm index 3bc198b..50e312f 100755 --- a/src/vbr/vbr.nasm +++ b/src/vbr/vbr.nasm @@ -115,13 +115,12 @@ main: mov dword eax, 0x1 push dword eax ; lba - xor ax, ax - push ax ; offset = 0 - ; 07E0:0 = 0x00007e00 mov ax, STAGE2_ENTRY - shr ax, 4 - push ax ; segment = 7E0 + push ax ; offset + + xor ax, ax + push ax ; segment = 0 ; uint8_t read_stage2_raw(uint16_t buf_segment, uint16_t buf_offset, ; uint32_t lba, @@ -130,11 +129,8 @@ main: add sp, 0xC .check_sig: - ; BUG: this is hard coded to check @ ((0x7E0 << 4) + 0x7FFC)...i.e (STAGE2_ENTRY + (STAGE2_MAX_BYTES - 4)) - ; this should be removed or done properly - mov ax, 0x7E0 - mov fs, ax - cmp dword [fs:0x7FFC], 0xDEADBEEF + mov eax, dword [(MAX_STAGE2_BYTES - 4) + 0x500] + cmp eax, 0xDEADBEEF je main.sig_ok ERROR VBR_ERROR_NO_SIGNATURE ; no signature present in stage2 @@ -142,7 +138,7 @@ main: .sig_ok: mov si, word [bp - 4] mov dl, byte [bp - 2] - jmp word 0x0000:0x7E00 + jmp word 0x0000:STAGE2_ENTRY ; ############### ; Required BIOS function(s) From 0983ec3fd557977dc84adb844cb3f00ffe2d6f86 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sat, 12 Oct 2024 22:16:17 -0400 Subject: [PATCH 15/25] remove map files as well --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index ead41f7..baf15ff 100755 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ stage2: $(stage2_binary_files) boottest: $(boottest_binary_files) clean: + @rm -rvf *.map @rm -rvf $(build_dir)/* @rm -rvf $(iso) @rm -rvf $(isoz) From a62f3de26fb3778b04627ec0e7a72e6eb273bca3 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sun, 13 Oct 2024 13:22:01 -0400 Subject: [PATCH 16/25] unreal gdt tweaks & fixes --- src/stage2/stage2.nasm | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/stage2/stage2.nasm b/src/stage2/stage2.nasm index 08536e4..3cfb951 100755 --- a/src/stage2/stage2.nasm +++ b/src/stage2/stage2.nasm @@ -396,27 +396,23 @@ ALIGN 16, db 0 unreal_gdt_info: unreal_gdt_size: dw (unreal_gdt_end - unreal_gdt_start) - 1 unreal_gdt_ptr: dd ((__STAGE2_SEGMENT << 4) + unreal_gdt_start) - unreal_gdt_start: ; entry 0 (null descriptor) dq 0 ; first entry is null ; entry 1 (16-bit code segment with 4 GiB flat mapping) - dw 0xFFFF ; Segment Limit 15:0 - dw 0x0000 ; Base Address 15:0 - db 0000_0000b ; Base Address 23:16 - - db 1001_1010b ; Access Byte: executable, readable, present - db 1000_1111b ; 24:20 G/DB/L/AVL & SegLimit 19:16 + dq 0x0000FFFF ; Base Address(15:0) 31:16, Segment Limit(15:0) 15:0 + db 0x00 ; Base Address 23:16 + db 1001_1010b ; Access Byte: Present, ring0, S = 1, executable (1), non-conforming, readable, Accessed + db 1000_1111b ; Flags: GR = 4KiB, attr = , Granularity = 4KiB & 16:19 of limit db 0000_0000b ; Base Address 31:24 ; entry 2 (16-bit data segment with 4 GiB flat mapping) - dw 0xFFFF ; Segment Limit 15:0 - dw 0x0000 ; Base Address 15:0 - db 0000_0000b ; Base Address 23:16 - db 1001_0010b ; Access Byte: readable, writable, present - db 1000_1111b ; Flags: 16-bit, Granularity = 4KiB - db 0000_0000b ; Base Address 31:24 + dq 0x0000FFFF ; Base Address(15:0) 31:16, Segment Limit(15:0) 15:0 + db 0x00 ; Base Address(23:16) + db 1001_0010b ; Access Byte: Present, ring0, S = 1, data (0), non-confirming, writable, present + db 1000_1111b ; Flags: GR = 4KiB, attr = <16-bit/?/?>, Granularity = 4KiB & 16:19 of limit + db 0000_0000b ; Base Address(31:24) unreal_gdt_end: ALIGN 16, db 0 From c9191444963ce4d0cbafba0113f41e08daec5762 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sun, 13 Oct 2024 14:50:49 -0400 Subject: [PATCH 17/25] turns out huge unreal mode isn't supported in bochs huge unreal mode is 4GiB code in 16bit mode + 4GiB data we will just use the regular big flat model instead (64KiB code/4GiB data) --- src/stage2/stage2.nasm | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/stage2/stage2.nasm b/src/stage2/stage2.nasm index 3cfb951..29e2f02 100755 --- a/src/stage2/stage2.nasm +++ b/src/stage2/stage2.nasm @@ -305,15 +305,12 @@ EnterUnrealMode: pop ax ; save cs to ax to setup far jump mov word [__UNREAL_SEGMENT], ax - shl eax, 4 - add eax, unreal_gdt_start ; ax contains the physical address of gdt table - mov dword [unreal_gdt_ptr], eax ; update gdt ptr in unreal_gdt_info - - lgdt [dword ((__STAGE2_SEGMENT << 4) + unreal_gdt_info)] ; calculate linear address for lgdt to consume + lgdt [((__STAGE2_SEGMENT << 4) + unreal_gdt_info)] ; load unreal gdt mov eax, cr0 or al,1 ; set pmode bit mov cr0, eax ; switch to pmode + jmp short $+2 ;jmp far 0x0008:EnterUnrealMode.load_cs db 0xEA ; jmp far imm16:imm16 @@ -330,7 +327,8 @@ EnterUnrealMode: and al,0xFE ; toggle bit 1 of cr0 mov cr0, eax ; back to realmode - + jmp short $+2 + ;jmp far 0x0008:EnterUnrealMode.unload_cs db 0xEA ; jmp far imm16:imm16 dw EnterUnrealMode.unload_cs ; error_far_ptr @@ -400,19 +398,19 @@ unreal_gdt_start: ; entry 0 (null descriptor) dq 0 ; first entry is null - ; entry 1 (16-bit code segment with 4 GiB flat mapping) - dq 0x0000FFFF ; Base Address(15:0) 31:16, Segment Limit(15:0) 15:0 + ; entry 1 (16bit code 64KiB limit) + dd 0x0000FFFF ; Base Address(15:0) 31:16, Segment Limit(15:0) 15:0 db 0x00 ; Base Address 23:16 db 1001_1010b ; Access Byte: Present, ring0, S = 1, executable (1), non-conforming, readable, Accessed - db 1000_1111b ; Flags: GR = 4KiB, attr = , Granularity = 4KiB & 16:19 of limit - db 0000_0000b ; Base Address 31:24 + db 0000_0000b ; Flags: GR = 4KiB, attr = , Granularity = 4KiB & 16:19 of limit + db 0x00 ; Base Address 31:24 - ; entry 2 (16-bit data segment with 4 GiB flat mapping) - dq 0x0000FFFF ; Base Address(15:0) 31:16, Segment Limit(15:0) 15:0 + ; entry 2 (16bit data segment with 4 GiB flat mapping) + dd 0x0000FFFF ; Base Address(15:0) 31:16, Segment Limit(15:0) 15:0 db 0x00 ; Base Address(23:16) db 1001_0010b ; Access Byte: Present, ring0, S = 1, data (0), non-confirming, writable, present db 1000_1111b ; Flags: GR = 4KiB, attr = <16-bit/?/?>, Granularity = 4KiB & 16:19 of limit - db 0000_0000b ; Base Address(31:24) + db 0x00 ; Base Address(31:24) unreal_gdt_end: ALIGN 16, db 0 From 832141efb48fcade55cc561159fa6695e134522d Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sun, 13 Oct 2024 16:36:11 -0400 Subject: [PATCH 18/25] bss convertion for mbr stage --- src/mbr/mbr.nasm | 62 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/src/mbr/mbr.nasm b/src/mbr/mbr.nasm index a382ba6..7745791 100755 --- a/src/mbr/mbr.nasm +++ b/src/mbr/mbr.nasm @@ -36,22 +36,39 @@ nop %include "cdecl16.inc" %include "entry.inc" %include "config.inc" -%include "early_mem.inc" %include "error_codes.inc" %include "partition_table.inc" +%include "fat32/fat32_structures.inc" ALIGN 4 init: cli ; We do not want to be interrupted - - xor ax, ax ; 0 AX - mov ds, ax ; Set segment registers to 0 + xor ax, ax + mov ds, ax ; Set segment registers to 0x0000 mov es, ax - + mov fs, ax + mov gs, ax + + ; + ; Zero BSS section + ; + mov cx, (end_bss - begin_bss) ; count = bss length + + mov ax, begin_bss + shr ax, 4 + mov es, ax ; es = begining of bss section + + xor ax, ax + mov di, ax ; dst = 0 + + cld + rep stosb ; zero bss section + + xor ax, ax mov ss, ax ; Set Stack Segment to 0 - mov sp, EARLY_STACK_START ; Setup stack + mov sp, stack_top ; Setup stack mov bp, sp ; base ptr = stack ptr - sub sp, 0x10 ; local varible space + sub sp, 0x10 ; local varible space xor cx, cx mov ch, 0x01 ; 256 WORDs in MBR (512 bytes), 0x0100 in cx @@ -133,13 +150,13 @@ main: je main.sig_ok ERROR MBR_ERROR_NO_VBR_SIG ; no signature present .sig_ok: - mov ax, partition_table_SIZE ; 72 bytes of data + mov ax, PartTable_t_size push ax mov ax, DiskSig ; start of partition table push ax - mov ax, partition_table ; defined in early_mem.inc + mov ax, partition_table push ax - call kmemcpy ; copy partition table to memory + call kmemcpy ; copy partition table to bss add sp, 0x6 mov si, word [bp - 4] @@ -174,4 +191,27 @@ PartEntry3: PartEntry4: times 16 db 0x00 BootSig: - dw 0xAA55 ; Add boot signature at the end of bootloader \ No newline at end of file + dw 0xAA55 ; Add boot signature at the end of bootloader +; this should mark the 512 byte mark...if not, something has gone wrong. +section .bss follows=.text +begin_bss: + +align 16, resb 1 +partition_table resb PartTable_t_size + +align 16, resb 1 +fat32_bpb resb FAT32_bpb_t_size +fat32_ebpb resb FAT32_ebpb_t_size + +align 16, resb 1 +fat32_nc_data resb 16 + +align 16, resb 1 +lba_packet resb LBAPkt_t_size + +align 512, resb 1 +stack_bottom resb 1024 ; 1Kib stack early on + +stack_top: +mbr_redzone resb 32 +end_bss: \ No newline at end of file From 4faffbf7feed18312e1ae0b01a9ca5ce01dc8302 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sun, 13 Oct 2024 16:36:55 -0400 Subject: [PATCH 19/25] remove defs for memory locations (moving to bss) --- include/early_mem.inc | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/include/early_mem.inc b/include/early_mem.inc index d2b3ea2..bce18d9 100755 --- a/include/early_mem.inc +++ b/include/early_mem.inc @@ -43,28 +43,6 @@ ; 0x0000000100000000 ??? ??? (whatever exists) RAM -- free for use (PAE/64bit)/More Extended memory ; ???????????????? ??? ??? Potentially usable for memory mapped PCI devices in modern hardware (but typically not, due to backward compatibility) -; copy of partition table, 72 bytes -%define partition_table 0x3000 -%define partition_table_SIZE 72 - -; copy of FAT32 BPB, 33 bytes (+1 to the next value to align to uint16_t) -;0x3048 -%define fat32_bpb 0x304A -%define fat32_bpb_SIZE 36 - -; copy of FAT32 EBPB, 54 bytes -;0x306A -%define fat32_ebpb 0x306E -%define fat32_ebpb_SIZE 54 - - -; next free space is 0x32D0 -%define fat32_nc_data 0x35D0 -%define fat32_nc_data_SIZE 16 - -; lba_packet for raw_disk_read -%define lba_packet 0x4000 - ;PhysicalAddress = Segment * 16 + Offset %define SEG_TO_LINEAR(s,o) ((s << 4) + o) From 7f06d47f32b73789663bd7087cd13fb4b5e57f74 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sun, 13 Oct 2024 16:37:29 -0400 Subject: [PATCH 20/25] add reserved space for jmp short init at the start, technically this is part of the bpb anyways --- include/fat32/fat32_structures.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/include/fat32/fat32_structures.inc b/include/fat32/fat32_structures.inc index 0bba2a5..7fb0289 100755 --- a/include/fat32/fat32_structures.inc +++ b/include/fat32/fat32_structures.inc @@ -63,6 +63,7 @@ ; resulting in a value which does not fit in the Number of Sectors entry at 0x13. struc FAT32_bpb_t + .reserved_init resb 3 .ident_8 resb 8 .bytes_per_sector_16 resb 2 .sectors_per_cluster_8 resb 1 From 29db27925fe4fa63ac167a0357b6a939db159703 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sun, 13 Oct 2024 16:37:52 -0400 Subject: [PATCH 21/25] initial work on a macro to call read_disk_raw --- include/BIOS/func/ext_read.nasm | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/include/BIOS/func/ext_read.nasm b/include/BIOS/func/ext_read.nasm index 42d1cdc..826729a 100644 --- a/include/BIOS/func/ext_read.nasm +++ b/include/BIOS/func/ext_read.nasm @@ -36,6 +36,29 @@ struc LBAPkt_t .lower_lba resd 1 .upper_lba resd 1 endstruc +; call_read_disk_raw +%macro call_read_disk_raw 5 + movzx ax, %5 + push ax ; drive_num + + movzx ax, %4 + push ax ; count + + movzx dword eax, %3 + push dword eax ; lba + + movzx ax, %2 + push ax ; offset + + movzx ax, %1 + push ax ; segment = 0 + + ; uint8_t read_stage2_raw(uint16_t buf_segment, uint16_t buf_offset, + ; uint32_t lba, + ; uint16_t count, uint16_t drive_num) + call read_disk_raw + add sp, 0xC +%endmacro ; Wrapper for AH=0x42 INT13h (Extended Read) ; From dd1a74bff03b202a4bb01e2b8817f088e1b56565 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sun, 13 Oct 2024 16:41:39 -0400 Subject: [PATCH 22/25] pass location of partition_table structure to vbr --- src/mbr/mbr.nasm | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mbr/mbr.nasm b/src/mbr/mbr.nasm index 7745791..ba036de 100755 --- a/src/mbr/mbr.nasm +++ b/src/mbr/mbr.nasm @@ -161,6 +161,7 @@ main: mov si, word [bp - 4] mov dl, byte [bp - 2] + mov bx, partition_table jmp word 0x0000:0x7C00 ; ############### From 9d33469e32d4578b5d760a3cce23b96a8b18a550 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sun, 13 Oct 2024 17:52:09 -0400 Subject: [PATCH 23/25] fixed bss init in mbr --- src/mbr/mbr.nasm | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/mbr/mbr.nasm b/src/mbr/mbr.nasm index ba036de..02f61e4 100755 --- a/src/mbr/mbr.nasm +++ b/src/mbr/mbr.nasm @@ -51,20 +51,13 @@ init: ; ; Zero BSS section - ; - mov cx, (end_bss - begin_bss) ; count = bss length - + mov cx, (end_bss - begin_bss) ; count = bss length mov ax, begin_bss - shr ax, 4 - mov es, ax ; es = begining of bss section - + mov di, ax ; es:di is dest xor ax, ax - mov di, ax ; dst = 0 - cld rep stosb ; zero bss section - xor ax, ax mov ss, ax ; Set Stack Segment to 0 mov sp, stack_top ; Setup stack mov bp, sp ; base ptr = stack ptr From e3a646675fdb7372500ffd065fb233eece4ec24d Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sun, 13 Oct 2024 17:52:28 -0400 Subject: [PATCH 24/25] bss for vbr stage --- src/vbr/vbr.nasm | 115 +++++++++++++++++++++++++++++++---------------- 1 file changed, 76 insertions(+), 39 deletions(-) diff --git a/src/vbr/vbr.nasm b/src/vbr/vbr.nasm index 50e312f..f17c041 100755 --- a/src/vbr/vbr.nasm +++ b/src/vbr/vbr.nasm @@ -29,7 +29,6 @@ __ENTRY: jmp short (init - $$) nop -phy_bpb_start: ; fill BPB area with 0x00 since we skip writing this part to disk ; but we need it for the 'jmp short entry; nop' above times 33 db 0x00 @@ -49,30 +48,41 @@ times 54 db 0x00 %include "config.inc" %include "early_mem.inc" %include "error_codes.inc" +%include "partition_table.inc" +%include "fat32/fat32_structures.inc" %include "fat32/bpb_offset_bx.inc" - - +; +; dl = boot_drive +; si = part_offset +; bx = partition_table location from mbr ALIGN 4 init: - cli ; We do not want to be interrupted + cli ; We do not want to be interrupted + xor ax, ax + mov ds, ax ; Set segment registers to 0x0000 + mov es, ax - xor ax, ax ; 0 AX - mov ds, ax ; Set segment registers to 0 + ; zero bss section + mov cx, (end_bss - begin_bss) ; count = bss length + mov ax, begin_bss + mov di, ax ; es:di is dest + xor ax, ax + cld + rep stosb - mov ss, ax ; Set Stack Segment to 0 - mov sp, EARLY_STACK_START ; Setup stack - mov bp, sp ; base ptr = stack ptr - sub sp, 0x20 ; local varible space + xor ax, ax + mov ss, ax ; Set Stack Segment to 0 + mov sp, stack_top ; Setup stack + mov bp, sp ; base ptr = stack ptr + sub sp, 0x10 ; local varible space - mov bx, VBR_ENTRY ; move Bx to the new start of the initial boot sector - sti ; all done with inital setup and relocation, reenable interupts + mov cx, bx ; mov partition_table locaiton to cx + sti ; all done with inital setup and relocation, reenable interupts - jmp 0:main ; fix up cs:ip just in case and jump to relocated code + jmp 0:main ; fix up cs:ip just in case and jump to relocated code ; ############### -; ; Extra/Shared Functions -; ; ############### %include "util/kmem_func.nasm" @@ -85,27 +95,38 @@ init: ; ; byte boot_drive @ bp - 2 ; word part_offset @ bp - 4 +; ptr partition_table ALIGN 4, db 0x90 main: - mov byte [bp - 2], dl ; boot_drive - mov [bp - 4], si ; part_offset + mov byte [bp - 2], dl ; boot_drive + mov word [bp - 4], si ; part_offset + mov word [bp - 6], cx ; partition_table -.check_FAT_size: ; we only support a very specific setup of FAT32 - cmp dword [bsSectorHuge], 0 ; SectorsHuge will not be set if FAT12/16 - ja main.load_stage2 - ERROR VBR_ERROR_WRONG_FAT_SIZE -.load_stage2: ; read sectors 1-63 to stage2 entry point - mov ax, (fat32_bpb_SIZE + fat32_ebpb_SIZE) ; size in byte +.load_fs_data: + mov ax, PartTable_t_size push ax - mov ax, (phy_bpb_start - 0x3) ; start of bpb - 0x3 for the jump short main at the start + mov ax, [bp - 6] ; ptr partition_table + mov ax, partition_table push ax - mov ax, fat32_bpb ; defined in memory.inc, destination + call kmemcpy ; copy partition table data + add sp, 0x6 + + mov ax, (FAT32_bpb_t_size + FAT32_ebpb_t_size) ; size in byte + push ax + mov ax, __ENTRY + push ax + mov ax, fat32_bpb ; push ax call kmemcpy ; copy bpb & ebpb to memory add sp, 0x6 mov bx, fat32_bpb ; bx now points to aligned memory structure - +.check_FAT_size: ; we only support a very specific setup of FAT32 + cmp dword [bsSectorHuge], 0 ; SectorsHuge will not be set if FAT12/16 + ja main.load_stage2 + ERROR VBR_ERROR_WRONG_FAT_SIZE +.load_stage2: + ; read sectors 1-(MAX_STAGE2_BYTES / 512) to stage2 entry point movzx ax, byte [bp - 2] push ax ; drive_num @@ -115,7 +136,6 @@ main: mov dword eax, 0x1 push dword eax ; lba - mov ax, STAGE2_ENTRY push ax ; offset @@ -127,17 +147,11 @@ main: ; uint16_t count, uint16_t drive_num) call read_disk_raw add sp, 0xC - -.check_sig: - mov eax, dword [(MAX_STAGE2_BYTES - 4) + 0x500] - cmp eax, 0xDEADBEEF - je main.sig_ok - - ERROR VBR_ERROR_NO_SIGNATURE ; no signature present in stage2 - -.sig_ok: - mov si, word [bp - 4] - mov dl, byte [bp - 2] +.enter_stage2: + mov dl, byte [bp - 2] ; boot_drive + mov si, word [bp - 4] ; part_offset + mov bx, partition_table + mov dx, fat32_bpb jmp word 0x0000:STAGE2_ENTRY ; ############### @@ -152,4 +166,27 @@ main: times (510 - ($ - $$)) nop ; Fill the rest of sector with nop BootSig: - dw 0xAA55 ; Add boot signature at the end of bootloader \ No newline at end of file + dw 0xAA55 ; Add boot signature at the end of bootloader + +section .bss follows=.text +begin_bss: + +align 16, resb 1 +partition_table resb PartTable_t_size + +align 16, resb 1 +fat32_bpb resb FAT32_bpb_t_size +fat32_ebpb resb FAT32_ebpb_t_size + +align 16, resb 1 +fat32_nc_data resb 16 + +align 16, resb 1 +lba_packet resb LBAPkt_t_size + +align 512, resb 1 +stack_bottom resb 1024 ; 1Kib stack early on + +stack_top: +vbr_redzone resb 32 +end_bss: \ No newline at end of file From e4b9c5d77f7195268d60b50d8fea3ffacb60b853 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Sun, 13 Oct 2024 17:54:06 -0400 Subject: [PATCH 25/25] added partition_table and fat32_bpb params to stage2 also the partition table and fat32_bpb and passed/copied from the mbr/vbr now --- src/stage2/stage2.nasm | 99 +++++++++++++++++++++++++++++++++--------- 1 file changed, 78 insertions(+), 21 deletions(-) diff --git a/src/stage2/stage2.nasm b/src/stage2/stage2.nasm index 29e2f02..fb4e88a 100755 --- a/src/stage2/stage2.nasm +++ b/src/stage2/stage2.nasm @@ -53,10 +53,17 @@ begin_text: jmp short (init - $$) nop +; dl = byte boot_drive +; si = word part_offset (active partition offset) +; bx = ptr PartTable_t partition_table +; dx = ptr FAT32_bpb_t fat32_bpb ALIGN 4, db 0x90 init: cli ; We do not want to be interrupted + ; + ; Zero BSS section + ; mov cx, (end_bss - begin_bss) ; count = bss length mov ax, begin_bss @@ -68,9 +75,10 @@ init: cld rep stosb ; zero bss section + ; done zeroing BSS - mov ax, __STAGE2_SEGMENT ; configured segment - mov ds, ax ; Set segment registers to 0 + mov ax, __STAGE2_SEGMENT ; set all our segments to the configured segment + mov ds, ax ; * mov es, ax ; * mov fs, ax ; * mov gs, ax ; * @@ -119,30 +127,50 @@ struc SteviaInfoStruct_t endstruc struc EarlyBootStruct_t - .lba_packet resb LBAPkt_t_size - .partition_table resb partition_table_SIZE - .fat32_bpb resb fat32_bpb_SIZE - .fat32_ebpb resb fat32_ebpb_SIZE - .fat32_nc_data resb fat32_nc_data_SIZE + .partition_table resb PartTable_t_size + .fat32_bpb resb FAT32_bpb_t_size + .fat32_ebpb resb FAT32_ebpb_t_size endstruc -; bp - 2 : uint8_t boot_drive -; bp - 4 : uint16_t part_offset +; bp - 2 : byte boot_drive +; bp - 4 : word part_offset +; bp - 6 : ptr PartTable_t partition_table +; bp - 8 : ptr FAT32_bpb_t fat32_bpb ALIGN 4, db 0x90 main: lea ax, [bp - 2] mov [boot_drive_ptr], ax + lea ax, [bp - 4] mov [partition_offset_ptr], ax ; setup pointers to boot_drive and partition offset on stack mov byte [bp - 2], dl ; boot_drive (probably 0x80) mov word [bp - 4], si ; partition_offset - + mov word [bp - 6], bx ; partition_table + mov word [bp - 8], dx ; fat32_bpb +.check_sig: mov eax, dword [STAGE2_SIG] cmp eax, 0xDEADBEEF je main.stage2_main ERROR STAGE2_SIGNATURE_MISSING .stage2_main: + mov ax, PartTable_t_size + push ax + mov ax, [bp - 6] ; ptr partition_table + mov ax, partition_table + push ax + call kmemcpy ; copy partition table data + add sp, 0x6 + + mov ax, (FAT32_bpb_t_size + FAT32_ebpb_t_size) ; size in byte + push ax + mov ax, [bp - 8] ; start of bpb - 0x3 for the jump short main at the start + push ax + mov ax, fat32_bpb ; defined in memory.inc, destination + push ax + call kmemcpy ; copy bpb & ebpb to memory + add sp, 0x6 + call SetTextMode call disable_cursor print_string HelloPrompt_cstr @@ -163,6 +191,7 @@ main: call InitFATDriver print_string InitFATSYS_OK_cstr + ERROR STEVIA_DEBUG_HALT ; ; Find first cluster of bootable file ; @@ -468,25 +497,53 @@ times ((512 - 4) - ($ -$$) ) db 0x90 ; nop STAGE2_SIG: dd 0xDEADBEEF ; Signature to mark the end of the stage2 section .bss follows=.sign -align 512 +align 512, resb 1 begin_bss: stack_bottom: - stack resb 4096 + resb 4096 stack_top: -stage2_main_redzone resb 32 +stage2_main_redzone: + resb 32 -SteviaInfo resd 4 -fat32_state resb FAT32_State_t_size +align 16, resb 1 +partition_table resb PartTable_t_size -disk_buffer resb 512 +align 16, resb 1 +fat32_bpb resb FAT32_bpb_t_size +fat32_ebpb resb FAT32_ebpb_t_size -fat_buffer resb 512 +align 16, resb 1 +fat32_nc_data resb 16 -dir_buffer resb 512 +align 16, resb 1 +lba_packet resb LBAPkt_t_size -fat_fsinfo resb 512 +align 16, resb 1 +SteviaInfo: + resd 4 +align 16, resb 1 +fat32_state: + resb FAT32_State_t_size -%define BIOSMemoryMap_SIZE 4096 -BIOSMemoryMap resb 4096 +align 16, resb 1 +mbr_sector_data: + resb 512 +vbr_sector_data: + resb 512 + +align 16, resb 1 +disk_buffer: + resb 512 +fat_buffer: + resb 512 +dir_buffer: + resb 512 +fat_fsinfo: + resb 512 + +align 16, resb 1 +%define BIOSMemoryMap_SIZE 2048 +BIOSMemoryMap: + resb 2048 end_bss: \ No newline at end of file