diff --git a/Makefile b/Makefile index d5b21b2..5570577 100644 --- a/Makefile +++ b/Makefile @@ -59,7 +59,7 @@ NC='\033[1;37m' boot.mbr: $(BINDIR)/disk.img $(MBRDIR)/grub.cfg @mkdir -p $(BINDIR)/disk - @echo ${CL2}[boot.mbr]${NC} Installing MBR on image...${CL3} + @echo ${CL2}[boot.mbr]${NC} Installing bootloader on image...${CL3} @$(MBRDIR)/grub-install.sh $(BINDIR)/disk.img $(BINDIR)/disk $(MBRDIR)/grub.cfg @echo ${CL2}[boot.mbr]${CL} OK${CL3} @rmdir $(BINDIR)/disk @@ -69,13 +69,13 @@ boot.loader.asm: $(LOADERDIR)/loader.asm @$(ASM) $(BOOTFLAGS) $(LOADERDIR)/loader.asm -o $(OBJDIR)/boot/loader.bin > /dev/null @echo ${CL2}[boot.loader.asm]${CL} OK${CL3} -loader: boot.loader.asm +bootloader: boot.mbr boot.loader.asm @mkdir -p $(BINDIR)/disk - @echo ${CL2}[loader]${NC} Constructing kernel loader...${CL3} + @echo ${CL2}[bootloader]${NC} Constructing bootloader...${CL3} @$(MBRDIR)/mount.sh $(BINDIR)/disk.img $(BINDIR)/disk @cp $(OBJDIR)/boot/loader.bin $(BINDIR)/disk/boot/loader.bin @$(MBRDIR)/umount.sh $(BINDIR)/disk - @echo ${CL2}[loader]${CL} OK${CL3} + @echo ${CL2}[bootloader]${CL} OK${CL3} @rmdir $(BINDIR)/disk make_disk: @@ -83,11 +83,8 @@ make_disk: @$(MBRDIR)/create_disk.sh $(BINDIR)/disk.img @echo ${CL2}[make_disk]${CL} OK${CL3} -boot: make_disk boot.mbr loader +boot: make_disk bootloader @echo ${CL2}[[boot]]${CL} Terminated without error.${CL3} all: boot kernel @echo ${CL2}[[all]]${CL} Terminated without error.${CL3} - -# TEST - diff --git a/boot/loader/loader.asm b/boot/loader/loader.asm index 44315fa..807ed10 100644 --- a/boot/loader/loader.asm +++ b/boot/loader/loader.asm @@ -35,16 +35,17 @@ _start: mov ax, cs ; correcting cs after the horrible far jump mov ds, ax ; hm... And ds too mov es, ax ; And es because it is jealous - ;mov [Bootdrv], dl + + mov [Bootdrv], dl xor dl, dl jmp 0x0000:main ; pas sûr +multiboot_header: -MB_header: align 4 dd MB_HEADER_MAGIC dd MB_HEADER_FLAGS dd CHECKSUM - dd MB_header ; Header address + dd multiboot_header ; Header address dd _start ; Address of code entry point dd 00 ; (end of code) not necessary dd 00 ; (bss) not necessary @@ -60,13 +61,189 @@ MB_start: add esp, 8 ; Cleanup 8 bytes pushed as arguments jmp Die -;; BEGIN OF THE HOLE -------------------------------------------------------- ;; +%include "boot/loader/cpu/cpuid.asm" +%include "boot/loader/io/rmterm.asm" +%include "boot/loader/io/rmmem.asm" + +main: + + ;;GO GDT64 + cli ; disable interrupts + lgdt [GDT64] + ;; ACTIVATE PROTECTED MODE + mov eax, cr0 + or al, 1b ; PE = 1 + mov cr0, eax + ;; DISABLE PAGING + mov eax, cr0 + and eax, 0x7FFFFFFF ; PG = 0 + ; |0|111111111111111111111111111111 + ; | + ; `------ Paging bit + mov cr0, eax + + push dword [VIDEO_MODE] + push dword [VGA_HEIGHT] + jmp (CODE_SELECTOR-GDT64):main32 + Die: cli hlt ; die nooooow - ;jmp 0xF000:0xFFF0 - jmp $ + jmp 0xF000:0xFFF0 ;; END OF THE HOLE ---------------------------------------------------------- ;; -main: +[BITS 32] +main32: + pop dword [VGA_HEIGHT32] + pop dword [VIDEO_MODE32] + + ;; VERIFY A20 + pushad + mov edi,0x112345 ;odd megabyte address. + mov esi,0x012345 ;even megabyte address. + mov [esi],esi ;making sure that both addresses contain diffrent values. + mov [edi],edi ;(if A20 line is cleared the two pointers would point to the address 0x012345 that would contain 0x112345 (edi)) + cmpsd ;compare addresses to see if the're equivalent. + popad + jne .A20_on ;if not equivalent , A20 line is set. + mov WORD [A20_OK], 0 + jmp .A20_end +.A20_on: + mov BYTE [A20_OK], 1 +.A20_end: + + ;; INITIALIZE PROTECTED MODE SEGMENT REGISTERS + mov ax, DATA_SELECTOR-GDT64 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + + ;; ACTIVATE PHYSICAL ADRESS EXTENSION + mov eax, cr4 + or eax, 100000b ; PAE = 1 = 4 Mo / page. 0 = 4 Ko + mov cr4, eax + + ;;cleanup + mov edi, 0x70000 + mov ecx, 0x10000 + xor eax, eax + rep stosd + ;;formatting + mov dword [0x70000], 0x71000 + 7 ; first PDP table + mov dword [0x71000], 0x72000 + 7 ; first page directory + mov dword [0x72000], 0x73000 + 7 ; first page table + mov edi, 0x73000 ; address of first page table + mov eax, 7 + mov ecx, 256 ; number of pages to map (1 MB) +.make_page_entries: + stosd + add edi, 4 + add eax, 0x1000 + loop .make_page_entries + ;; pointing pml4 + mov eax, 0x70000 ; Bass address of PML4 + mov cr3, eax ; load page-map level-4 base + + ;; ACTIVATE LONG MODE + mov ecx, 0xC0000080 ; address of MSR + rdmsr ; read MSR + or eax, 100000000b ; LME = 1. (Long Mode Enable) + wrmsr ; write MSR + + ;; ACTIVATE PAGING + mov eax, cr0 + or eax, 0x80000000 ; make bit 31 (PG = Paging) to 1 : + ; |1|000000000000000000000000000000 + ; | + ; `------ Paging bit + mov cr0, eax + push dword 0 + push dword [VIDEO_MODE] + push dword 0 + push dword [VGA_HEIGHT] + jmp (LONG_SELECTOR-GDT64):main64 + +[BITS 64] + +;; DATA + Init db "Booting OS/K !", 0x0D, 0x0A, 0x0D, 0x0A, 0x09, " Checking CPUID...",0 + CPUIDD db 0x09, " Checking CPUID...", 0 + EnA20 db 0x09, " Enabling A20 line...", 0 +%ifdef DEBUG + ReadAttempt db 0x09, " Attempt to read a sector with ATA commands...", 0x0A, 0x0D, 0x0A, 0x0D, 0 +%else + ReadAttempt db 0x09, " Attempt to read a sector with ATA commands...", 0 +%endif + txt db 0x09, " Switching to Long Mode... ", 0 + EndOfLoader db "End of loader.bin. System will halt !", 0x0A, 0x0D, 0 + Reinit db "Booting OS/K !", 0x0D, 0x0A, 0x0D, 0x0A, 0 + Pass db " OK", 0x0A, 0x0D, 0 + Fail db " FAIL!", 0x0A, 0x0D, 0 + msg db "The system is now in x64 mode. Is this not beautiful ?", 0x0A, 0x0D, 0 + FileNotFound db "Second Stage Error : The Kernel was not found.", 0x0A, 0x0D, 0 + DiskError db "Second Stage Error : The Disk has crashed.", 0x0A, 0x0D, 0 + filename db "KERNEL BIN" + +%include "boot/loader/io/lmmem.asm" +%include "boot/loader/io/lmterm.asm" +%include "boot/loader/io/ata.asm" +%include "boot/loader/cpu/cpu.asm" +%include "boot/loader/fs/fat.asm" + +main64: + pop qword [VGA_HEIGHT64] + pop qword [VIDEO_MODE64] + ;; INITIALIZE STACK + mov rsp, 0x9F000 + + call clear + + ;; Printing + mov bl, 0x0B + mov esi, Reinit + call write + + mov bl, 0x0F + mov esi, CPUIDD + call write + + mov bl, 0x0A + mov esi, Pass + call write + + mov bl, 0x0F + mov esi, EnA20 + call write + + call check_a20 + + mov bl, 0x0F + mov esi, txt + call write + + mov bl, 0x0A + mov esi, Pass + call write + + mov bl, 0x0D + mov esi, msg + call write + + mov bl, 0x0F + mov esi, ReadAttempt + call write + + call temporize ; Temporized because the ATA drive must be ready + + mov bl, 1 + mov bh, 1 + call ata_read + + mov bl, 0x0D + mov esi, EndOfLoader + call write + jmp Die + diff --git a/boot/loader/multiboot.inc b/boot/loader/multiboot.inc index 8874596..f013eda 100644 --- a/boot/loader/multiboot.inc +++ b/boot/loader/multiboot.inc @@ -24,10 +24,10 @@ ;=----------------------------------------------------------------------------=; ;; MULTIBOOT HEADER -MB_AOUT_KLUDGE equ 1 << 16 ; We are not an ELF executable -MB_ALIGN equ 1 << 0 ; Ask to align loaded modules on page boundaries -MB_MEMINFO equ 1 << 1 ; Ask to provide memory map -MB_HEADER_MAGIC equ 0x1BADB002 -MB_HEADER_FLAGS equ MB_AOUT_KLUDGE|MB_ALIGN|MB_MEMINFO -CHECKSUM equ -(MB_HEADER_MAGIC + MB_HEADER_FLAGS) -KERNEL_STACK equ 0x00200000 ; Stack starts at the 2mb address & grows down +%define MB_AOUT_KLUDGE 1 << 16 ; We are not an ELF executable +%define MB_ALIGN 1 << 0 ; Ask to align loaded modules on page boundaries +%define MB_MEMINFO 1 << 1 ; Ask to provide memory map +%define MB_HEADER_MAGIC 0x1BADB002 +%define MB_HEADER_FLAGS MB_AOUT_KLUDGE|MB_ALIGN|MB_MEMINFO +%define CHECKSUM -(MB_HEADER_MAGIC + MB_HEADER_FLAGS) +%define KERNEL_STACK 0x00200000 ; Stack starts at the 2mb address & grows down diff --git a/build/obj/boot/loader.bin b/build/obj/boot/loader.bin index c5b1e9d..e12a50a 100644 Binary files a/build/obj/boot/loader.bin and b/build/obj/boot/loader.bin differ