Shuffle around memory layout of bootloader

This commit is contained in:
mintsuki 2020-09-06 05:15:55 +02:00
parent 45aaa72e85
commit b16f63458d
9 changed files with 41 additions and 105 deletions

View File

@ -15,7 +15,7 @@ start:
mov fs, ax
mov gs, ax
mov ss, ax
mov sp, 0xfff0
mov esp, 0x4000
sti
; Some BIOSes don't pass the correct boot drive number,
@ -39,12 +39,12 @@ start:
mov si, LoadingMsg
call simple_print
; ****************** Load stage 2 ******************
; ****************** Load stage 1.5 ******************
mov si, Stage2Msg
mov si, Stage15Msg
call simple_print
mov eax, dword [stage2_sector]
mov eax, dword [stage15_sector]
mov ebx, 0x7e00
mov ecx, 1
call read_sectors
@ -73,7 +73,7 @@ halt:
; Data
LoadingMsg db 0x0D, 0x0A, 'Limine', 0x0D, 0x0A, 0x0A, 0x00
Stage2Msg db 'Loading stage2...', 0x00
Stage15Msg db 'Loading stage 1.5...', 0x00
ErrReadDiskMsg db 0x0D, 0x0A, 'Disk read error, system halted', 0x00
ErrEnableA20Msg db 0x0D, 0x0A, 'A20 enable error, system halted', 0x00
DoneMsg db ' DONE', 0x0D, 0x0A, 0x00
@ -87,7 +87,7 @@ times 6 db 0
%include 'disk.inc'
times 0x1b0-($-$$) db 0
stage2_sector: dd 1
stage15_sector: dd 1
times 0x1b8-($-$$) db 0
times 510-($-$$) db 0
@ -97,9 +97,9 @@ dw 0xaa55
stage15:
push es
push 0x6000
push 0x7000
pop es
mov eax, dword [stage2_sector]
mov eax, dword [stage15_sector]
inc eax
xor ebx, ebx
mov ecx, 62
@ -132,9 +132,9 @@ stage15:
push edx
push stage2.size
push (stage2 - 0x8000) + 0x60000
push (stage2 - 0x8000) + 0x70000
call 0x60000
call 0x70000
bits 16
%include 'a20_enabler.inc'

View File

@ -7,41 +7,44 @@
; DL = Drive number
; ES = Buffer segment
; BX = Buffer offset
; CX = Sectors count
; OUT:
; Carry if error
read_sector:
push eax
push ebx
push ecx
push edx
push esi
push edi
read_sectors:
pusha
push es
pop word [.target_segment]
mov word [.target_offset], bx
mov dword [.lba_address_low], eax
xor esi, esi
mov si, .da_struct
mov word [.countdown], cx
.loop:
mov esi, .da_struct
mov ah, 0x42
clc
int 0x13
jc .done
add word [.target_offset], 512
inc dword [.lba_address_low]
dec word [.countdown]
jnz .loop
.done:
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
popa
ret
align 2
.countdown: dw 0
align 4
.da_struct:
.da_struct:
.packet_size db 16
.unused db 0
.count dw 1
@ -49,71 +52,3 @@ align 4
.target_segment dw 0
.lba_address_low dd 0
.lba_address_high dd 0
; ********************************************
; Reads multiple LBA addressed sectors
; ********************************************
; IN:
; EAX = LBA starting sector
; DL = Drive number
; ES = Buffer segment
; EBX = Buffer offset
; ECX = Sectors count
; OUT:
; Carry if error
%define TEMP_BUFFER_SEG 0x7000
%define BYTES_PER_SECT 512
read_sectors:
push eax ; Save GPRs
push ebx
push ecx
push edx
push esi
push edi
.loop:
push es
push ebx
mov bx, TEMP_BUFFER_SEG
mov es, bx
xor bx, bx
call read_sector
pop ebx
pop es
jc .done
push ds
mov si, TEMP_BUFFER_SEG
mov ds, si
mov edi, ebx
xor esi, esi
push ecx
mov ecx, BYTES_PER_SECT
a32 o32 rep movsb
pop ecx
pop ds
inc eax
add ebx, BYTES_PER_SECT
loop .loop
.done:
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
ret

View File

@ -2,7 +2,7 @@ load_gdt:
pusha
push es
push ds
push 0x7000
push 0x7ff0
pop es
xor di, di
push 0
@ -19,7 +19,7 @@ load_gdt:
GDT:
dw .GDTEnd - .GDTStart - 1 ; GDT size
dd 0x70000 ; GDT start
dd 0x7ff00 ; GDT start
.GDTStart:

View File

@ -3,7 +3,7 @@ ENTRY(main)
SECTIONS
{
. = 0x60000;
. = 0x70000;
.text : {
KEEP(*(.entry*))

View File

@ -22,9 +22,8 @@ ASM_BASIC(
__attribute__((noreturn))
void main(uint8_t *compressed_stage2, size_t stage2_size, uint8_t boot_drive) {
// The decompressor should decompress compressed_stage2 to address 0x500.
// For now, just copy it over as it is not compressed. TODO: implement decompressor.
volatile uint8_t *dest = (volatile uint8_t *)0x500;
// The decompressor should decompress compressed_stage2 to address 0x4000.
volatile uint8_t *dest = (volatile uint8_t *)0x4000;
tinf_gzip_uncompress(dest, compressed_stage2, stage2_size);

View File

@ -45,8 +45,9 @@ __attribute__((noreturn)) void panic(const char *fmt, ...) {
}
}
static size_t bump_allocator_base = 0x10000;
#define BUMP_ALLOCATOR_LIMIT ((size_t)0x70000)
extern symbol bss_end;
static size_t bump_allocator_base = (size_t)bss_end;
#define BUMP_ALLOCATOR_LIMIT ((size_t)0x7ff00)
void brewind(size_t count) {
bump_allocator_base -= count;

View File

@ -1,4 +1,4 @@
section .text
section .realmode
global rm_int
rm_int:

View File

@ -1,4 +1,4 @@
section .text
section .realmode
int_08_ticks_counter: dd 0

View File

@ -3,10 +3,11 @@ ENTRY(main)
SECTIONS
{
. = 0x500;
. = 0x4000;
.text : {
KEEP(*(.entry*))
KEEP(*(.realmode*))
*(.text*)
}