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
objcopy -O binary -j .text -j .data boot.elf $@
wc -c $@
boot.elf: lboot.o loader.o fernlader.ld
$(LD) $(LDFLAGS) -o $@ lboot.o loader.o

View file

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

131
lboot.S
View file

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