From a4470b4733b1a58d8dfecd385240244974ee8167 Mon Sep 17 00:00:00 2001 From: Thomas Oltmann Date: Wed, 18 Feb 2026 10:40:02 +0100 Subject: [PATCH] Reorganized source files --- Makefile | 13 ++- config.mk | 2 +- src/Rnbp.S | 228 ------------------------------------- src/bios.S | 79 +++++++++++++ src/{Qcommon.c => main.c} | 2 +- src/mode.S | 57 ++++++++++ src/nbp.S | 97 ++++++++++++++++ fernlader.ld => src/nbp.ld | 2 +- src/{Ptftp.S => std.c} | 0 src/std.h | 0 10 files changed, 246 insertions(+), 234 deletions(-) delete mode 100644 src/Rnbp.S create mode 100644 src/bios.S rename src/{Qcommon.c => main.c} (96%) create mode 100644 src/mode.S create mode 100644 src/nbp.S rename fernlader.ld => src/nbp.ld (92%) rename src/{Ptftp.S => std.c} (100%) create mode 100644 src/std.h diff --git a/Makefile b/Makefile index 5b090db..7bc8fbb 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,18 @@ -include config.mk +OBJS=\ + src/nbp.o \ + src/bios.o \ + src/std.o \ + src/main.o \ + # end of object list + .PHONY: all clean all: boot.bin clean: - rm -f src/*.o boot.elf boot.bin + rm -f $(OBJS) boot.elf boot.bin config.mk: | config.default.mk cp config.default.mk $@ @@ -14,8 +21,8 @@ boot.bin: boot.elf objcopy -O binary -j .text -j .data boot.elf $@ wc -c $@ -boot.elf: src/Rnbp.o src/Ptftp.o src/Qcommon.o fernlader.ld - $(LD) $(LDFLAGS) -o $@ src/Rnbp.o src/Ptftp.o src/Qcommon.o +boot.elf: $(OBJS) src/nbp.ld + $(LD) $(LDFLAGS) -T src/nbp.ld -o $@ $(OBJS) %.o: %.S $(CC) $(CFLAGS) -c -o $@ $(@:.o=.S) diff --git a/config.mk b/config.mk index 7c43e8c..3b2d1a4 100644 --- a/config.mk +++ b/config.mk @@ -4,4 +4,4 @@ CC = gcc LD = ld CFLAGS = -no-pie -fno-pic -fno-stack-protector -nostdinc -m32 -LDFLAGS = -Tfernlader.ld -m elf_i386 +LDFLAGS = -m elf_i386 diff --git a/src/Rnbp.S b/src/Rnbp.S deleted file mode 100644 index 2997aac..0000000 --- a/src/Rnbp.S +++ /dev/null @@ -1,228 +0,0 @@ - // vim: ft=gas:et:sw=12:ts=12:sts=12 - - .global _start - - .set SS_CODE16, 0x08 - .set SS_DATA16, 0x10 - .set SS_CODE32, 0x18 - .set SS_DATA32, 0x20 - - - -.macro PROT16 - .code32 - mov $SS_DATA16, %ax - mov %ax, %ds - mov %ax, %es - mov %ax, %ss - ljmp $SS_CODE16, $9f -9: .code16 -.endm - -.macro PROT32 - .code16 - mov $SS_DATA32, %ax - mov %ax, %ds - mov %ax, %es - mov %ax, %ss - ljmp $SS_CODE32, $9f -9: .code32 -.endm - -.macro REAL - mov %cr0, %eax - and $0xFFFE, %ax - mov %eax, %cr0 - xor %ax, %ax - mov %ax, %ds - mov %ax, %es - mov real_ss, %ax - mov %ax, %ss - shl $4, %eax - sub %eax, %esp - ljmp $0x0000, $9f -9: -.endm - -.macro PROT - mov %cr0, %eax - or $1, %eax - mov %eax, %cr0 - xor %eax, %eax - mov $SS_DATA16, %ax - mov %ax, %ds - mov %ax, %es - mov %ax, %ss - mov real_ss, %ax - shl $4, %eax - add %eax, %esp -.endm - - - - .text - .code16 - // _start: entry point -_start: cli - cld - mov %sp, %bp - xor %ax, %ax - mov %ax, %ds - mov %ax, %es - - mov %ss, %ax - mov %ax, real_ss - - // initialize our own BSS section - mov $_bss_start, %di - mov $_bss_end, %cx - sub %di, %cx - xor %al, %al - rep stosb - - // a20_enable: Allow use of 'high' (>1Mb) memory -a20_enable: // Of all the ways to toggle A20, we only try the Fast A20 Gate. - // This is known to cause problems on some ancient systems, - // but as our bootloader exclusively runs on 64-bit machines, - // we should not run into any of those systems. - // Modern machines apparently don't even have the A20 line anymore. - push $msg_a20 - call dbgmsg - add $2, %sp - - inb $0x92, %al - or $2, %al - outb %al, $0x92 - - // prot_enter: Set up GDT, switch into 32-bit protected mode. -prot_enter: - push $msg_prot - call dbgmsg - add $2, %sp - - lgdt GDT_PTR - - PROT - PROT32 - - .code32 - jmp main - - .code16 -dbgmsg: push %bp - mov %sp, %bp - push %ax - push %si - mov 4(%bp), %si - -1: lodsb - test %al, %al - jz 2f - outb %al, $0xE9 - jmp 1b - -2: pop %si - pop %ax - pop %bp - ret - - .global bios_write - .code32 -bios_write: push %ebp - mov %esp, %ebp - push %ebx - - mov 8+0(%ebp), %edx - mov 8+4(%ebp), %ecx - - PROT16 - REAL - -.bwloop: test %ecx, %ecx - jz .bwreturn - - xor %bx, %bx - mov $0x0E, %ah - mov (%edx), %al - - push %ecx - push %edx - int $0x10 - pop %edx - pop %ecx - - add $1, %edx - sub $1, %ecx - - jmp .bwloop - -.bwreturn: PROT - PROT32 - - pop %ebx - mov %ebp, %esp - pop %ebp - ret - - .global bios_getmap - .code32 -bios_getmap:push %ebp - mov %esp, %ebp - push %ebx - push %edi - - mov 8+0(%ebp), %edi - - PROT16 - REAL - - xor %ebx, %ebx -_gmnext: movl $0, 20(%di) - mov $0x534D4150, %edx // e820 magic number - mov $24, %ecx - mov $0xE820, %eax - int $0x15 - jc _gmdone - test %ebx, %ebx - jz _gmdone - add $24, %di - jmp _gmnext -_gmdone: add $24, %di - - - PROT - PROT32 - - mov %edi, %eax - - pop %edi - pop %ebx - mov %ebp, %esp - pop %ebp - ret - - .data - -real_ss: .word 0 - - .global GDT - -GDT: // entry 0: null descriptor - .space 8, 0 - // entry 1: 16-bit code segment - .byte 0xFF, 0xFF, 0, 0, 0, 0b10011010, 0x8F, 0 - // entry 2: 16-bit data segment - .byte 0xFF, 0xFF, 0, 0, 0, 0b10010010, 0x8F, 0 - // entry 3: 32-bit code segment - .byte 0xFF, 0xFF, 0, 0, 0, 0b10011010, 0xCF, 0 - // entry 4: 32-bit data segment - .byte 0xFF, 0xFF, 0, 0, 0, 0b10010010, 0xCF, 0 - // TODO: 32-bit TSS - .set GDT_SIZE, . - GDT -GDT_PTR: .word GDT_SIZE - 1 - .word GDT - .word 0, 0, 0 - -msg_start: .asciz "Netboot via fernlader v2 ...\r\n" -msg_a20: .asciz " * Enabling A20 Gate\r\n" -msg_prot: .asciz " * Protected Mode\r\n" diff --git a/src/bios.S b/src/bios.S new file mode 100644 index 0000000..fb0b97c --- /dev/null +++ b/src/bios.S @@ -0,0 +1,79 @@ + // vim: ft=gas:et:sw=12:ts=12:sts=12 + + .include "src/mode.S" + + .global bios_write + .code32 +bios_write: push %ebp + mov %esp, %ebp + push %ebx + + mov 8+0(%ebp), %edx + mov 8+4(%ebp), %ecx + + PROT16 + REAL + +.bwloop: test %ecx, %ecx + jz .bwreturn + + xor %bx, %bx + mov $0x0E, %ah + mov (%edx), %al + + push %ecx + push %edx + int $0x10 + pop %edx + pop %ecx + + add $1, %edx + sub $1, %ecx + + jmp .bwloop + +.bwreturn: PROT + PROT32 + + pop %ebx + mov %ebp, %esp + pop %ebp + ret + + .global bios_getmap + .code32 +bios_getmap:push %ebp + mov %esp, %ebp + push %ebx + push %edi + + mov 8+0(%ebp), %edi + + PROT16 + REAL + + xor %ebx, %ebx +_gmnext: movl $0, 20(%di) + mov $0x534D4150, %edx // e820 magic number + mov $24, %ecx + mov $0xE820, %eax + int $0x15 + jc _gmdone + test %ebx, %ebx + jz _gmdone + add $24, %di + jmp _gmnext +_gmdone: add $24, %di + + + PROT + PROT32 + + mov %edi, %eax + + pop %edi + pop %ebx + mov %ebp, %esp + pop %ebp + ret + diff --git a/src/Qcommon.c b/src/main.c similarity index 96% rename from src/Qcommon.c rename to src/main.c index 4221576..495daa7 100644 --- a/src/Qcommon.c +++ b/src/main.c @@ -52,7 +52,7 @@ main() bios_write("karlos\r\n", 8); for (int i = 0; ; i++) { unsigned char *entry = memmap + 24 * i; - if (entry >= end) break; + if ((void *)entry >= end) break; unsigned char type = *(entry + 16); char c = type < 10 ? type + '0' : '?'; bios_write(&c, 1); diff --git a/src/mode.S b/src/mode.S new file mode 100644 index 0000000..a1fe0f4 --- /dev/null +++ b/src/mode.S @@ -0,0 +1,57 @@ + // vim: ft=gas:et:sw=12:ts=12:sts=12 + + .set SS_CODE16, 0x08 + .set SS_DATA16, 0x10 + .set SS_CODE32, 0x18 + .set SS_DATA32, 0x20 + + + +.macro PROT16 + .code32 + mov $SS_DATA16, %ax + mov %ax, %ds + mov %ax, %es + mov %ax, %ss + ljmp $SS_CODE16, $9f +9: .code16 +.endm + +.macro PROT32 + .code16 + mov $SS_DATA32, %ax + mov %ax, %ds + mov %ax, %es + mov %ax, %ss + ljmp $SS_CODE32, $9f +9: .code32 +.endm + +.macro REAL + mov %cr0, %eax + and $0xFFFE, %ax + mov %eax, %cr0 + xor %ax, %ax + mov %ax, %ds + mov %ax, %es + mov real_ss, %ax + mov %ax, %ss + shl $4, %eax + sub %eax, %esp + ljmp $0x0000, $9f +9: +.endm + +.macro PROT + mov %cr0, %eax + or $1, %eax + mov %eax, %cr0 + xor %eax, %eax + mov $SS_DATA16, %ax + mov %ax, %ds + mov %ax, %es + mov %ax, %ss + mov real_ss, %ax + shl $4, %eax + add %eax, %esp +.endm diff --git a/src/nbp.S b/src/nbp.S new file mode 100644 index 0000000..4c724a7 --- /dev/null +++ b/src/nbp.S @@ -0,0 +1,97 @@ + // vim: ft=gas:et:sw=12:ts=12:sts=12 + + .include "src/mode.S" + + .global _start + + .text + .code16 + // _start: entry point +_start: cli + cld + mov %sp, %bp + xor %ax, %ax + mov %ax, %ds + mov %ax, %es + + mov %ss, %ax + mov %ax, real_ss + + // initialize our own BSS section + mov $_bss_start, %di + mov $_bss_end, %cx + sub %di, %cx + xor %al, %al + rep stosb + + // a20_enable: Allow use of 'high' (>1Mb) memory +a20_enable: // Of all the ways to toggle A20, we only try the Fast A20 Gate. + // This is known to cause problems on some ancient systems, + // but as our bootloader exclusively runs on 64-bit machines, + // we should not run into any of those systems. + // Modern machines apparently don't even have the A20 line anymore. + push $msg_a20 + call dbgmsg + add $2, %sp + + inb $0x92, %al + or $2, %al + outb %al, $0x92 + + // prot_enter: Set up GDT, switch into 32-bit protected mode. +prot_enter: + push $msg_prot + call dbgmsg + add $2, %sp + + lgdt GDT_PTR + + PROT + PROT32 + + .code32 + jmp main + + .code16 +dbgmsg: push %bp + mov %sp, %bp + push %ax + push %si + mov 4(%bp), %si + +1: lodsb + test %al, %al + jz 2f + outb %al, $0xE9 + jmp 1b + +2: pop %si + pop %ax + pop %bp + ret + + .data + + .global real_ss +real_ss: .word 0 + + .global GDT +GDT: // entry 0: null descriptor + .space 8, 0 + // entry 1: 16-bit code segment + .byte 0xFF, 0xFF, 0, 0, 0, 0b10011010, 0x8F, 0 + // entry 2: 16-bit data segment + .byte 0xFF, 0xFF, 0, 0, 0, 0b10010010, 0x8F, 0 + // entry 3: 32-bit code segment + .byte 0xFF, 0xFF, 0, 0, 0, 0b10011010, 0xCF, 0 + // entry 4: 32-bit data segment + .byte 0xFF, 0xFF, 0, 0, 0, 0b10010010, 0xCF, 0 + // TODO: 32-bit TSS + .set GDT_SIZE, . - GDT +GDT_PTR: .word GDT_SIZE - 1 + .word GDT + .word 0, 0, 0 + +msg_start: .asciz "Netboot via fernlader v2 ...\r\n" +msg_a20: .asciz " * Enabling A20 Gate\r\n" +msg_prot: .asciz " * Protected Mode\r\n" diff --git a/fernlader.ld b/src/nbp.ld similarity index 92% rename from fernlader.ld rename to src/nbp.ld index 2f14ffb..9f576b0 100644 --- a/fernlader.ld +++ b/src/nbp.ld @@ -4,7 +4,7 @@ PHDRS { } SECTIONS { .text 0x7C00: { - src/Rnbp.o(.text) + src/nbp.o(.text) *(.text*, .rodata*) } :all .data : { diff --git a/src/Ptftp.S b/src/std.c similarity index 100% rename from src/Ptftp.S rename to src/std.c diff --git a/src/std.h b/src/std.h new file mode 100644 index 0000000..e69de29