Some asm restructuring; Reduced binary size

This commit is contained in:
Thomas Oltmann 2025-07-17 07:51:07 +02:00
parent 001f3b0a5c
commit 68eb1914b1
3 changed files with 84 additions and 50 deletions

View file

@ -12,6 +12,7 @@ config.mk: | config.default.mk
boot.bin: boot.elf boot.bin: boot.elf
objcopy -O binary -j .text -j .data boot.elf $@ objcopy -O binary -j .text -j .data boot.elf $@
wc -c $@
boot.elf: lboot.o loader.o fernlader.ld boot.elf: lboot.o loader.o fernlader.ld
$(LD) $(LDFLAGS) -o $@ lboot.o loader.o $(LD) $(LDFLAGS) -o $@ lboot.o loader.o

View file

@ -9,11 +9,11 @@ SECTIONS {
} :all } :all
.data : { .data : {
*(.data) *(.data)
. = ALIGN(4K);
} :all } :all
_bss_start = .; _bss_start = .;
.bss (NOLOAD) : { .bss (NOLOAD) : {
*(.bss, COMMON) *(.bss, COMMON)
. = ALIGN(4K);
*(.bootboot) *(.bootboot)
} :all } :all
_bss_end = .; _bss_end = .;

131
lboot.S
View file

@ -24,30 +24,9 @@
.set PACKET_SIZE, 512 .set PACKET_SIZE, 512
.set TX_BUF_SIZE, 4096 .set TX_BUF_SIZE, 4096
.macro pxe_call, opcode /***********************************\
push %eax * Core Functionality
push %ebx \***********************************/
push %ecx
push %edx
mov %sp, %bx
add $16, %bx
push %ss
push %bx
push $\opcode
lcall *pxe_api
add $6, %sp
mov %ss:(%bx), %cx
or %cx, %ax
jnz _pcerr
pop %edx
pop %ecx
pop %ebx
pop %eax
.endm
// _start: entry point // _start: entry point
_start: cli _start: cli
@ -57,6 +36,7 @@ _start: cli
mov %ax, %ds mov %ax, %ds
mov %ax, %es mov %ax, %es
// initialize our own BSS section
mov $_bss_start, %di mov $_bss_start, %di
mov $_bss_end, %cx mov $_bss_end, %cx
sub %di, %cx sub %di, %cx
@ -90,20 +70,6 @@ _start: cli
call print call print
call enable_a20 call enable_a20
.set PXE_GET_CACHED_INFO, 0x0071
push $0
push %cs
push $tx_buf
push $TX_BUF_SIZE
push $2
push $0
pxe_call PXE_GET_CACHED_INFO
add $12, %sp
mov tx_buf+20, %eax
mov %eax, server_ip
mov tx_buf+24, %eax
mov %eax, gateway_ip
mov $msg_unreal, %si mov $msg_unreal, %si
call print call print
call unreal call unreal
@ -171,17 +137,17 @@ unreal: push %ds
mov %cr0, %eax mov %cr0, %eax
or $0x01, %al or $0x01, %al
mov %eax, %cr0 mov %eax, %cr0
ljmp $0x8, $_urprot ljmp $0x8, $1f // enter protected mode
_urprot: mov $0x10, %cx 1: mov $0x10, %cx
mov %cx, %ds mov %cx, %ds
mov %cx, %es mov %cx, %es
and $0xFE, %al and $0xFE, %al
mov %eax, %cr0 mov %eax, %cr0
ljmp $0x0, $_urunreal ljmp $0x0, $1f // enter (un)real mode
_urunreal: pop %es 1: pop %es
pop %ds pop %ds
ret ret
@ -308,13 +274,48 @@ _mhnext: add $24, %si
_mhdone: ret _mhdone: ret
out_of_mem: mov $msg_memerr, %si
call print
jmp hang
/***********************************\
* Networked File Access
\***********************************/
.set PXE_GET_CACHED_INFO, 0x0071
.set PXE_TFTP_OPEN, 0x0020 .set PXE_TFTP_OPEN, 0x0020
.set PXE_TFTP_CLOSE, 0x0021 .set PXE_TFTP_CLOSE, 0x0021
.set PXE_TFTP_READ, 0x0022 .set PXE_TFTP_READ, 0x0022
.macro pxe_call, opcode
push %eax
push %ebx
push %ecx
push %edx
mov %sp, %bx
add $16, %bx
push %ss
push %bx
push $\opcode
lcall *pxe_api
add $6, %sp
mov %ss:(%bx), %cx
or %cx, %ax
jnz _pcerr
pop %edx
pop %ecx
pop %ebx
pop %eax
.endm
read_file: push %bp read_file: push %bp
mov %sp, %bp mov %sp, %bp
call pxe_getinfo
call tftp_open call tftp_open
mov heap_ptr, %ebx mov heap_ptr, %ebx
mov %ebx, %edi mov %ebx, %edi
@ -334,6 +335,27 @@ _rdloop: call tftp_read
leave leave
ret ret
pxe_getinfo:push %eax
push %bp
mov %sp, %bp
push $0
push %cs
push $tx_buf
push $TX_BUF_SIZE
push $2
push $0
pxe_call PXE_GET_CACHED_INFO
add $12, %sp
mov tx_buf+20, %eax
mov %eax, server_ip
mov tx_buf+24, %eax
mov %eax, gateway_ip
leave
pop %eax
ret
// tftp_open: Open a file stream over TFTP. // tftp_open: Open a file stream over TFTP.
// A pointer to the filename is passed in ESI. // A pointer to the filename is passed in ESI.
tftp_open: push %ecx tftp_open: push %ecx
@ -418,7 +440,7 @@ tftp_read: push %ebx
pop %ebx pop %ebx
ret ret
out_of_mem: mov $msg_memerr, %si _pcerr: mov $msg_pcerr, %si
call print call print
jmp hang jmp hang
@ -513,6 +535,10 @@ dump: push %eax
pop %eax pop %eax
ret ret
/***********************************\
* Framebuffer Configuration
\***********************************/
vbe_setup: vbe_setup:
push %eax push %eax
push %ecx push %ecx
@ -565,6 +591,8 @@ vbe_getinfo:
push %bp push %bp
mov %sp, %bp mov %sp, %bp
movl $0x32454256, vbe_info // "VBE2"
mov $0x4F00, %ax mov $0x4F00, %ax
mov $vbe_info, %di mov $vbe_info, %di
int $0x10 int $0x10
@ -626,9 +654,11 @@ vbe_setmode:
pop %eax pop %eax
ret ret
vbe_info: .bss
.ascii "VBE2"
.space 512 - 4 vbe_info: .space 512
.text
// long: Enter long mode // long: Enter long mode
long: long:
@ -667,9 +697,7 @@ long:
hang: hlt hang: hlt
jmp hang jmp hang
_pcerr: mov $msg_pcerr, %si .data
call print
jmp hang
// gdt16: Protected mode / Unreal mode 16-bit GDT // gdt16: Protected mode / Unreal mode 16-bit GDT
gdt16: // entry 0: null descriptor gdt16: // entry 0: null descriptor
@ -754,9 +782,12 @@ pml4_ptr: .long 0
// Points to the end of the memory map // Points to the end of the memory map
memmap_end: .short 0 memmap_end: .short 0
.bss
tx_buf: .space TX_BUF_SIZE tx_buf: .space TX_BUF_SIZE
.code64 .code64
.text
// trampo64: Trampoline function to load long-mode segments // trampo64: Trampoline function to load long-mode segments
// before entering the loader. // before entering the loader.
trampo64: trampo64:
@ -808,6 +839,8 @@ trampo64:
mov $bootboot, %edi mov $bootboot, %edi
jmp loader_main jmp loader_main
.data
type_table: .byte 0 type_table: .byte 0
.byte 1 .byte 1
.byte 0 .byte 0