From fdfc83abfb41f0a0d337ca447563df4c97c17b22 Mon Sep 17 00:00:00 2001 From: mintsuki Date: Wed, 22 Jan 2020 04:12:55 +0100 Subject: [PATCH] Clean bootsect code up --- src/bootsect/bootsect.asm | 122 +++++++++++++++---------------- src/bootsect/disk.inc | 131 ++++++++++++++++------------------ src/bootsect/simple_print.inc | 13 ++-- 3 files changed, 128 insertions(+), 138 deletions(-) diff --git a/src/bootsect/bootsect.asm b/src/bootsect/bootsect.asm index 29a1b648..a9d58a5d 100644 --- a/src/bootsect/bootsect.asm +++ b/src/bootsect/bootsect.asm @@ -1,52 +1,52 @@ org 0x7C00 bits 16 -code_start: +start: + cli + jmp 0x0000:.initialise_cs + .initialise_cs: + xor ax, ax + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + mov sp, 0x7c00 + sti -cli -jmp 0x0000:initialise_cs -initialise_cs: -xor ax, ax -mov ds, ax -mov es, ax -mov fs, ax -mov gs, ax -mov ss, ax -mov sp, 0x7c00 -sti + mov si, LoadingMsg + call simple_print -mov si, LoadingMsg -call simple_print + ; ****************** Load stage 2 ****************** -; ****************** Load stage 2 ****************** + mov si, Stage2Msg + call simple_print -mov si, Stage2Msg -call simple_print + mov eax, 1 + mov ebx, 0x7e00 + mov ecx, 1 + call read_sectors -mov ax, 1 -mov ebx, 0x7e00 -mov cx, 1 -call read_sectors + jc err_reading_disk -jc err_reading_disk + mov si, DoneMsg + call simple_print -mov si, DoneMsg -call simple_print - -jmp 0x7e00 + jmp 0x7e00 err_reading_disk: -mov si, ErrReadDiskMsg -call simple_print -jmp halt + mov si, ErrReadDiskMsg + call simple_print + jmp halt err_enabling_a20: -mov si, ErrEnableA20Msg -call simple_print + mov si, ErrEnableA20Msg + call simple_print + jmp halt halt: -hlt -jmp halt + hlt + jmp halt ; Data @@ -70,44 +70,40 @@ dw 0xaa55 ; ********************* Stage 2 ********************* -; Load stage 3 +stage2: + ; Load stage 3 + mov eax, 2 + mov ebx, 0x8000 + mov ecx, 62 + call read_sectors + jc err_reading_disk -mov ax, 2 -mov ebx, 0x8000 -mov cx, 62 -call read_sectors -jc err_reading_disk + ; Enable A20 + call enable_a20 + jc err_enabling_a20 -; Enable A20 + ; Enter 32 bit pmode + lgdt [GDT] ; Load the GDT -call enable_a20 -jc err_enabling_a20 + cli -; Enter 32 bit pmode + mov eax, cr0 + or al, 1 + mov cr0, eax -lgdt [GDT] ; Load the GDT + jmp 0x18:.pmode + bits 32 + .pmode: + mov ax, 0x20 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax -cli + jmp 0x8000 -mov eax, cr0 -or eax, 00000001b -mov cr0, eax - -jmp 0x18:.pmode - -bits 32 -.pmode: - -mov ax, 0x20 -mov ds, ax -mov es, ax -mov fs, ax -mov gs, ax -mov ss, ax - -jmp 0x8000 bits 16 - %include 'a20_enabler.inc' %include 'gdt.inc' diff --git a/src/bootsect/disk.inc b/src/bootsect/disk.inc index e685eba8..a8f429e3 100644 --- a/src/bootsect/disk.inc +++ b/src/bootsect/disk.inc @@ -1,5 +1,3 @@ -read_sector: - ; *********************************************** ; Reads a disk sector with an LBA address ; *********************************************** @@ -13,35 +11,35 @@ read_sector: ; OUT: ; Carry if error -push eax -push ebx -push ecx -push edx -push esi -push edi +read_sector: + push eax + push ebx + push ecx + push edx + push esi + push edi -push es -pop word [.target_segment] -mov word [.target_offset], bx -mov dword [.lba_address_low], eax + 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 ah, 0x42 + xor esi, esi + mov si, .da_struct + mov ah, 0x42 -clc ; Clear carry for int 0x13 because some BIOSes may not clear it on success + clc ; Clear carry for int 0x13 because some BIOSes may not clear it on success -int 0x13 ; Call int 0x13 + int 0x13 ; Call int 0x13 -.done: - -pop edi -pop esi -pop edx -pop ecx -pop ebx -pop eax -ret ; Exit routine + .done: + pop edi + pop esi + pop edx + pop ecx + pop ebx + pop eax + ret ; Exit routine align 4 .da_struct: @@ -53,9 +51,6 @@ align 4 .lba_address_low dd 0 .lba_address_high dd 0 - -read_sectors: - ; ******************************************** ; Reads multiple LBA addressed sectors ; ******************************************** @@ -65,58 +60,58 @@ read_sectors: ; DL = Drive number ; ES = Buffer segment ; EBX = Buffer offset -; CX = Sectors count +; ECX = Sectors count ; OUT: ; Carry if error -push eax ; Save GPRs -push ebx -push ecx -push edx -push esi -push edi +read_sectors: + push eax ; Save GPRs + push ebx + push ecx + push edx + push esi + push edi -.loop: + .loop: + push es + push ebx -push es -push ebx + mov bx, 0x7000 ; Load in a temp buffer + mov es, bx + xor bx, bx -mov bx, 0x7000 ; Load in a temp buffer -mov es, bx -xor bx, bx + call read_sector ; Read sector -call read_sector ; Read sector + pop ebx + pop es -pop ebx -pop es + jc .done ; If carry exit with flag -jc .done ; If carry exit with flag + push ds -push ds + mov si, 0x7000 + mov ds, si + mov edi, ebx + xor esi, esi -mov si, 0x7000 -mov ds, si -mov edi, ebx -xor esi, esi + push ecx + mov ecx, 512 + a32 o32 rep movsb + pop ecx -push ecx -mov ecx, 512 -a32 o32 rep movsb -pop ecx + pop ds -pop ds + inc eax ; Increment sector + add ebx, 512 ; Add 512 to the buffer -inc eax ; Increment sector -add ebx, 512 ; Add 512 to the buffer + loop .loop ; Loop! -loop .loop ; Loop! - -.done: -pop edi -pop esi -pop edx -pop ecx ; Restore GPRs -pop ebx -pop eax -ret ; Exit routine + .done: + pop edi + pop esi + pop edx + pop ecx ; Restore GPRs + pop ebx + pop eax + ret ; Exit routine diff --git a/src/bootsect/simple_print.inc b/src/bootsect/simple_print.inc index add926af..fc40edf3 100644 --- a/src/bootsect/simple_print.inc +++ b/src/bootsect/simple_print.inc @@ -1,5 +1,3 @@ -simple_print: - ; ************************************** ; Prints a string using the BIOS ; ************************************** @@ -7,16 +5,17 @@ simple_print: ; IN: ; SI = points to a 0x00 terminated string -push ax ; Save registers -push si -mov ah, 0x0E ; int 0x10, function 0x0E (print character) -.loop: +simple_print: + push ax ; Save registers + push si + mov ah, 0x0E ; int 0x10, function 0x0E (print character) + .loop: lodsb ; Load character from string test al, al ; Is is the 0x00 terminator? jz .done ; If it is, exit routine int 0x10 ; Call BIOS jmp .loop ; Repeat! -.done: + .done: pop si ; Restore registers pop ax ret ; Exit routine