summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichal Idziorek <m.i@gmx.at>2014-09-04 23:38:33 +0200
committerMichal Idziorek <m.i@gmx.at>2014-09-04 23:38:33 +0200
commit467e19b262d435f1820539bc0c85ed4fa31b9687 (patch)
treec3ee7690b089713614309857ecfc046d6a13d160
parent487ecc1615ccc0368f1520c1146b2b43cdab6577 (diff)
Finally we have a 2 stage bootloader !
This one can load the ramdis up to 0xfffff :)
-rw-r--r--Makefile18
-rw-r--r--bochsrc2
-rw-r--r--boot/disk_load_16.asm206
-rw-r--r--boot/print_string_16.asm47
-rw-r--r--boot/stage2.asm (renamed from boot/mbr.asm)93
-rw-r--r--boot0/disk_load_16.asm78
-rw-r--r--boot0/mbr.asm54
-rw-r--r--kernel/kernel.c7
8 files changed, 320 insertions, 185 deletions
diff --git a/Makefile b/Makefile
index 426ae78..f12c36f 100644
--- a/Makefile
+++ b/Makefile
@@ -42,7 +42,10 @@ KERNEL_ENTRY=./boot/kernel_entry.o
#master boot record asm
-MBR=./boot/mbr.bin
+MBR=./boot0/mbr.bin
+
+#stage 2 bootloader
+STAGE2=./boot/stage2.bin
#multiprocessor binary entry
@@ -88,16 +91,17 @@ binfont.bin: data/binfont.src
# master boot record, kernel binary and fool-font
-FoolOS.img: $(MBR) kernel.bin $(FILLUP)
+FoolOS.img: $(MBR) $(STAGE2) kernel.bin $(FILLUP) FoolData.img
cp $(FILLUP) $@
- dd if=$(MBR) of=$@ bs=1 seek=0 conv=notrunc
- dd if=kernel.bin of=$@ bs=1 seek=512 conv=notrunc
+ dd if=$(MBR) of=$@ bs=512 seek=0 conv=notrunc
+ dd if=$(STAGE2) of=$@ bs=512 seek=1 conv=notrunc
+ dd if=kernel.bin of=$@ bs=512 seek=10 conv=notrunc
+ dd if=FoolData.img of=$@ bs=512 seek=906 conv=notrunc
binfont.img: binfont.bin
cat $^ > $@
-FoolData.img: $(FILLUP) binfont.bin $(MP_BIN)
- cp $(FILLUP) $@
+FoolData.img: binfont.bin $(MP_BIN)
dd if=$(MP_BIN) of=$@ bs=512 seek=0 conv=notrunc
dd if=binfont.bin of=$@ bs=512 seek=1 conv=notrunc
@@ -112,7 +116,7 @@ run: all
############ cleanup ############
clean:
- -rm *.bin *.img bochs.log $(KERNEL_ENTRY) $(ASMOBJECTS) $(OBJECTS) $(FILLUP) $(MBR) $(MP_BIN) bochs.out ne2k-tx.log ne2k-txdump.txt
+ -rm *.bin *.img bochs.log $(KERNEL_ENTRY) $(ASMOBJECTS) $(OBJECTS) $(FILLUP) $(MBR) $(MP_BIN) bochs.out ne2k-tx.log ne2k-txdump.txt $(STAGE2)
############ test stuff ############
stick: FoolOS.img
diff --git a/bochsrc b/bochsrc
index 5ebd7ad..8d1af39 100644
--- a/bochsrc
+++ b/bochsrc
@@ -242,7 +242,7 @@ cpu: count=2, ips=10000000 #, reset_on_triple_fault=1, ignore_bad_msrs=1, msrs="
#optromimage3: file=optionalrom.bin, address=0xd2000
#optromimage4: file=optionalrom.bin, address=0xd3000
-optramimage1: file=FoolData.img, address=0x90000
+#optramimage1: file=FoolData.img, address=0x90000
#optramimage2: file=/path/file2.img, address=0x0020000
#optramimage3: file=/path/file3.img, address=0x0030000
#optramimage4: file=/path/file4.img, address=0x0040000
diff --git a/boot/disk_load_16.asm b/boot/disk_load_16.asm
index eae15a0..2a209fc 100644
--- a/boot/disk_load_16.asm
+++ b/boot/disk_load_16.asm
@@ -7,146 +7,142 @@
[bits 16]
-DISK_LOAD:
- db "D",0
-DISK_LOAD_ERROR
- db "E",0
-Head:
- db 0
-Track:
- db 0
-Sector:
- db 1
-LBA:
- dw 0 ;; starts at zero!
-Sectors:
- dw 80
-Heads:
+STR_ERROR:
+ db "Disk Read Error",0
+
+STR_SPACE
+ db " ",0
+
+STR_OK:
+ db "Kernel Loaded",0
+
+bpbSectorsPerTrack:
+ dw 18
+
+bpbHeadsPerCylinder:
dw 2
+LBA:
+ dw 10 ; current lba
-;disk_load routune (load dh sectors from drive dl to es:bx)
-;lba mode has 52 sectors hardcoded!
disk_load_16:
pusha
-
-;;; ; check if LBA is supported
-;;; pusha
-;;; mov ah,0x41
-;;; mov bx,0x55aa
-;;; int 0x13
-;;; jnc disk_load_lba
-;;; popa
- disk_load_next:
+ mov bx,0x1000 ;target es:bx
+ mov es,bx
+ mov bx,0
+
+ next_sectors:
+
+ pusha
+
+ mov bx, STR_SPACE
+ call print_string
+
+ ;show es - target
+ mov bx,es
+
+ mov al,bh
+ call print_hex_byte
+
+ mov al,bl
+ call print_hex_byte
+
+ mov bx, STR_SPACE
+ call print_string
+
+ ;load and show next LBA numer
+ mov ax,[LBA]
+ mov al,ah
+ call print_hex_byte
+
+ mov ax,[LBA]
+ call print_hex_byte
+
+ mov bx, STR_SPACE
+ call print_string
+
+ popa
+
+ mov ax,[LBA]
+
+ ;calculate chs
+ call lba_to_chs
+
+ ;show chs sector/ head / cylinder
+ mov al,cl
+ call print_hex_byte
- call ESBX
+ mov al,dh
+ call print_hex_byte
- ; load using CHS
-;; mov al,80 ;read dh sectors (amount)
-;; mov cx,[LBA]
-;; cmp cx,0
-;; jne disk_read_normal
-;; mov al,79 ;read dh sectors (amount)
-;; disk_read_normal
+ mov al,ch
+ call print_hex_byte
- mov al,1
+ call print_nextline
- mov cl,[Sector] ;sector
- mov ch,[Track] ;cyl
+ ; and now READ it!
+ mov al,16 ;number of sectors to read
mov dl,[BOOT_DRIVE]
- mov dh,[Head] ;head
mov ah,0x02 ;BIOS read sector func
int 0x13 ;bios interrupt
- jnc disk_read_ok ;ready becaue of error :(
+ jc disk_read_error
+ disk_read_error_continue:
- mov bx, DISK_LOAD_ERROR
- call print_string
-
- jmp $
+ mov bx,es
+ add bx,0x0200
+ cmp bx,0x9e00
+ je disk_load_finish
- disk_read_ok:
+ mov es,bx
+ mov bx,0
mov ax,[LBA]
+ add ax,16
+ mov [LBA],ax
- cmp ax,880
- je disk_load_ready
+ jmp next_sectors
- mov bx, DISK_LOAD
+ disk_read_error:
+ pusha
+ mov bx, STR_ERROR
call print_string
+ call print_nextline
+ popa
+ jmp disk_read_error_continue
- add ax,1 ;conver LBA to CHS
- mov [LBA],ax
+disk_load_finish:
+
+ mov bx, STR_OK
+ call print_string
+ call print_nextline
- call LBACHS
+ popa
+ ret
- jmp disk_load_next
- disk_load_ready:
- jmp $
+;lba to chs translation:
+; input : ax - lba
+; output: cl - sector
+; dh - head
+; ch - cylinder / track
- popa
- ret
-
-ESBX:
+lba_to_chs:
- xor dx,dx
- xor dx,dx
- mov ax,[LBA]
- mov bx,0x200
- mul bx
- mov bx,ax
- add dx,1
- shl dx,12
- mov es,dx
-
-ret
-
-
- ; load using LBA
-
-;;;disk_load_lba:
-;;;
-;;; popa
-;;;
-;;; xor ah,ah
-;;; mov ah,0x42
-;;; lea si,[lba_adr]
-;;; int 0x13
-;;; jc skip_print
-;;; mov bx, DISK_LOAD
-;;; call print_string
-;;; skip_print:
-;;;
-;;; popa
-;;; ret
-;;;
-LBACHS:
xor dx, dx ; prepare dx:ax for operation
- div WORD [Sectors] ; divide by sectors per track
+ div WORD [bpbSectorsPerTrack] ; divide by sectors per track
inc dl ; add 1 (obsolute sector formula)
- mov BYTE [Sector], dl
+ mov cl, dl
xor dx, dx ; prepare dx:ax for operation
- div WORD [Heads] ; mod by number of heads (Absolue head formula)
- mov BYTE [Head], dl ; everything else was already done from the first formula
+ div WORD [bpbHeadsPerCylinder] ; mod by number of heads (Absolue head formula)
+ mov dh,dl ; everything else was already done from the first formula
- mov BYTE [Track], al ; not much else to do :)
+ mov ch, al ; not much else to do :)
ret
-lba_adr:
-
- dw 0x10 ; size of packet ( 16 byte)
-
- dw 53 ; number of sectors to read
-
- dw 0x0000 ; target addr. offset
- dw 0x1000 ; target addr. sector
-
- dd 1 ; first sector to read
- dd 0 ;
diff --git a/boot/print_string_16.asm b/boot/print_string_16.asm
index 5d8ad5c..92b4ea0 100644
--- a/boot/print_string_16.asm
+++ b/boot/print_string_16.asm
@@ -26,3 +26,50 @@ print_string:
popa ;pop all registers
ret ;return to caller
+
+print_nextline:
+
+ pusha
+
+ mov ah,0x3
+ mov bh,0
+ int 0x10
+
+ mov ah,0x2
+ mov bh,0
+ inc dh
+ mov dl,0
+ int 0x10
+ popa
+ ret
+
+
+;print byte from al to screen
+ print_hex_byte:
+
+ pusha
+
+ mov [.temp],al
+ shr al,4
+ cmp al,10
+ sbb al,69h
+ das
+
+ mov ah,0Eh
+ int 10h
+
+ mov al,[.temp]
+ ror al,4
+ shr al,4
+ cmp al,10
+ sbb al,69h
+ das
+
+ mov ah,0Eh
+ int 10h
+
+popa
+
+ ret
+
+ .temp db 0
diff --git a/boot/mbr.asm b/boot/stage2.asm
index a854615..27f9832 100644
--- a/boot/mbr.asm
+++ b/boot/stage2.asm
@@ -2,14 +2,6 @@
;
; THE FOOL-BOOT-LOADER
;
-; So we have just been loaded by the BIOS and are in 16-bits real mode!
-; Now the following work will bee accomplished (chronologically):
-;
-; NOTE: this should not exceed 446 bytes
-; (otherwise it does not work on my Acer Aspire)
-;
-; * BOOT_DRIVE set
-;
; * X sectors of our kernel loaded at KERNEL_OFFSET from floppy
;
; * memory map made available at MEMMAP_OFFSET
@@ -39,32 +31,30 @@
;this is where the BIOS per definition will put the first
;512 bytes of data from the boot device
;The Boot record is identified by the last 2 magic bytes: 0xaa55 (?)
-[org 0x7c00]
+[org 0x7e00]
;;define some constants
;;where we will load our kernel into memory and some
;;other memory locations
;
+KERNEL_DATA equ 10 ;sector on disk where the kernel starts
KERNEL_SECTOR equ 0x1000
KERNEL_OFFSET equ 0x0000
-MEMMAP_SIZE_OFFSET equ 0x7c00+0x600
-MEMMAP_OFFSET equ 0x7c00+0x400
+MEMMAP_SIZE_OFFSET equ 0x7c00
+MEMMAP_OFFSET equ 0x7c01
VESA_MODES equ 0x8300
VESA_MODE_INFO equ 0x8400
VESA_MODE_SELECT equ 0x4114
-
;
jmp boot_16 ;start boot process
- db 'X'
-jmp $ ;entry for other processors ;)
- db 'X'
-;
+
;;SOME Global Data, mainly info/error strings
BOOT_DRIVE:
db 0xff
STR_VERSION:
- db "v0.5",0
+ db "Fool Loader Stage 2 v0.5",0
+
VESA_CHECK1:
db "1",0
VESA_CHECK2:
@@ -73,6 +63,8 @@ VESA_CHECK3:
db "3",0
CHECK_A20:
db "A",0
+STR_LOAD
+ db "Loading Kernel...",0
;
;;lets put our temporary GDT (Global Descriptor Table) here
;;kernel should move this away
@@ -95,63 +87,37 @@ CHECK_A20:
;
;;lets start
[bits 16]
-;
-boot_16:
+boot_16:
- ;first of allsetup the stack (Right under mbr)
- ;guaranteed ~30KB space
- mov bp,0x07bff
- mov sp,bp
-;
-;
+; pr info
mov bx, STR_VERSION
call print_string
-;
-; ;remember BOOT_DRIVE (as was set by BIOS)
+ call print_nextline
+
+; remember BOOT_DRIVE again
mov [BOOT_DRIVE],dl
-; ;Load the IMAGE
+; Load the KERNEL Image
+ mov bx, STR_LOAD
+ call print_string
+ call print_nextline
call disk_load_16
-;
-; ;get memory map from bios before we enter 32 bit protected mode
+ ;get memory map from bios before we enter 32 bit protected mode
mov ax,0 ; set target address in es:di (0:offset)
mov es,ax
mov di,MEMMAP_OFFSET
call BiosGetMemoryMap ; this will also put the number of entries
-; ; of the memory map at MEMMAP_SIZE_OFFSET
+ ; of the memory map at MEMMAP_SIZE_OFFSET
+ call VesaSetup
-;a20check:
-;
-; mov bx, CHECK_A20
-; call print_string
-;
-; call check_a20
-; cmp ax,0
-; je a20check ;hang if a20 is disabled!
-
-; ;VESA: also setup vesa stuff before entering 32 bit protected mode
-; call VesaSetup
-;
-; ;finally lets enter Protected mode!!!
call switch_to_pm
;
;;;;;;;;; BOOT 32-bit protected mode;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[bits 32]
boot_32_pm:
-;
-
-; ;enable A20
-; ;http://www.brokenthorn.com/Resources/OSDev9.html
-; ;Method 3.1: Enables A20 through keyboard controller
-; ;Not all keyboard controllers support this
-; ; does not work in virtual box (with VT-x accerleration?)
-
-; mov al, 0xdd ; command 0xdd: enable a20
-; mov al, 0xdf ; command 0xdf: disable a20
-; out 0x64, al ; send command to controller
;Fast A20 Gate:
;http://wiki.osdev.org/A20_Line
@@ -161,18 +127,7 @@ boot_32_pm:
out 0x92, al
; call kernel!
- mov eax,0 ;booting processor
- call 0x10000 ;KERNEL_SECTOR:KERNEL_OFFSET ;jump into our Kernel!
-;
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;
-
-;fill partition table (4x16byte) with zeroes.
-;(otherwise my Acer Aspire will not boot)
-;times 64 db 0x0
-
-;so we get identified as MBR
-times 510-($-$$) db 0x0
-;dw 0x0
-dw 0xaa55
+ mov eax,0 ;tell the kernel
+ ; we are the booting processor
+ call 0x10000 ;jump into our Kernel!
diff --git a/boot0/disk_load_16.asm b/boot0/disk_load_16.asm
new file mode 100644
index 0000000..55ad4aa
--- /dev/null
+++ b/boot0/disk_load_16.asm
@@ -0,0 +1,78 @@
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;; Miguel's FoolOS Helper Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;
+;disk_load_16
+;
+
+
+[bits 16]
+
+STR_LBA:
+ db "\nLBA Support Detected (no support yet)",0
+
+STR_CHS
+ db "CHS Support Detected",0
+
+STR_ERROR
+ db "Disk Read Error",0
+
+STR_DONE
+ db "Stage 2 Loaded",0
+
+disk_load_16:
+
+ pusha
+
+ ; check if LBA is supported
+ mov ah,0x41
+ mov bx,0x55aa
+ int 0x13
+ jnc disk_load_lba
+ jmp disk_load_chs
+
+
+disk_load_lba:
+
+ mov bx, STR_LBA
+ call print_string
+ call print_nextline
+ jmp $
+ jmp disk_load_finish
+
+disk_load_chs:
+
+ mov bx, STR_CHS
+ call print_string
+ call print_nextline
+
+ mov bx,0 ;target es:bx
+ mov es,bx
+ mov bx,0x7e00
+
+ mov al,50 ;number of sectors to read
+ mov ah,0x02 ;BIOS read sector func
+
+ mov cl,2 ; sector
+ mov ch,0 ; cylinder
+
+ mov dl,[BOOT_DRIVE]
+ mov dh,0 ;head
+
+ int 0x13 ;bios interrupt
+
+ jnc disk_load_finish
+
+ jmp $
+
+disk_load_finish:
+
+ call print_nextline
+ mov bx, STR_DONE
+ call print_string
+ call print_nextline
+
+
+ popa
+ ret
+
diff --git a/boot0/mbr.asm b/boot0/mbr.asm
new file mode 100644
index 0000000..d24bbfe
--- /dev/null
+++ b/boot0/mbr.asm
@@ -0,0 +1,54 @@
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;
+; stage 1
+;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+[bits 16]
+
+[org 0x7c00]
+
+jmp stage1
+
+STR_1:
+ db "Starting Fool Loader Stage 1. v0.1",0
+
+STR_2:
+ db "Starting Stage 2",0
+
+BOOT_DRIVE:
+ db 0xff ; remember the bootdrive here
+
+%include "boot/print_string_16.asm"
+%include "boot0/disk_load_16.asm"
+
+stage1:
+
+ ;first of allsetup the stack (Right under mbr)
+ ;guaranteed ~30KB space
+ mov bp,0x07bff
+ mov sp,bp
+
+ ;remember BOOT_DRIVE (as was set by BIOS)
+ mov [BOOT_DRIVE],dl
+
+ mov bx, STR_1
+ call print_string
+ call print_nextline
+
+ call disk_load_16 ; loads stage 2
+
+ mov bx, STR_2
+ call print_string
+ call print_nextline
+ jmp 0x7e00 ; jump to next sector where we put stage 2
+
+;fill partition table (4x16byte) with zeroes.
+;(otherwise my Acer Aspire will not boot)
+times 64 db 0x0
+
+;so we get identified as MBR
+times 510-($-$$) db 0x0
+;dw 0x0
+dw 0xaa55
+
diff --git a/kernel/kernel.c b/kernel/kernel.c
index 33624ad..0feca14 100644
--- a/kernel/kernel.c
+++ b/kernel/kernel.c
@@ -34,6 +34,7 @@ void kernel_main(uint32_t initial_stack, int mp)
{
+
/// TODO
/////// SYMMETRIC MULTIPROCESSING, APS get caought here, move it away ///
// catch the APs (Application Processors)
@@ -80,7 +81,7 @@ void kernel_main(uint32_t initial_stack, int mp)
// this function returns the physical base address of
// our video memory
//
- uint32_t vesa_physbase=vesa_init(0x8300,0x8400,0x90000+0x200);
+ uint32_t vesa_physbase=vesa_init(0x8300,0x8400,0x80000+0x200);
// self-log message of logger :P
log_log();
@@ -115,7 +116,7 @@ void kernel_main(uint32_t initial_stack, int mp)
// we know that here, the bootloader placed the mamory map and
// the number of entries.
//
- mem_init(0x7c00+0x400,*((uint16_t *)(0x7c00+0x600)));
+ mem_init(0x7c00+1,*((uint16_t *)(0x7c00)));
//
@@ -132,7 +133,7 @@ void kernel_main(uint32_t initial_stack, int mp)
//
// Start the other Processors (also before paging !)
//
- smp_start_aps(&procdata,0x90000); // starts at 0x90000
+ smp_start_aps(&procdata,0x80000); // starts at 0x90000
// but it will be copied over mbr
//