From 9092d0b8cd7e390e1b95ce6944d83cb1f38dc41a Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 4 Mar 2019 22:34:07 +0100 Subject: [PATCH] Stuff --- Makefile | 13 ++- boot/loader/loader.asm | 191 ++------------------------------------ boot/loader/multiboot.inc | 14 +-- build/obj/boot/loader.bin | Bin 2053 -> 74 bytes 4 files changed, 22 insertions(+), 196 deletions(-) diff --git a/Makefile b/Makefile index 5570577..d5b21b2 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 bootloader on image...${CL3} + @echo ${CL2}[boot.mbr]${NC} Installing MBR 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} -bootloader: boot.mbr boot.loader.asm +loader: boot.loader.asm @mkdir -p $(BINDIR)/disk - @echo ${CL2}[bootloader]${NC} Constructing bootloader...${CL3} + @echo ${CL2}[loader]${NC} Constructing kernel loader...${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}[bootloader]${CL} OK${CL3} + @echo ${CL2}[loader]${CL} OK${CL3} @rmdir $(BINDIR)/disk make_disk: @@ -83,8 +83,11 @@ make_disk: @$(MBRDIR)/create_disk.sh $(BINDIR)/disk.img @echo ${CL2}[make_disk]${CL} OK${CL3} -boot: make_disk bootloader +boot: make_disk boot.mbr loader @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 807ed10..44315fa 100644 --- a/boot/loader/loader.asm +++ b/boot/loader/loader.asm @@ -35,17 +35,16 @@ _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 multiboot_header ; Header address + dd MB_header ; Header address dd _start ; Address of code entry point dd 00 ; (end of code) not necessary dd 00 ; (bss) not necessary @@ -61,189 +60,13 @@ MB_start: add esp, 8 ; Cleanup 8 bytes pushed as arguments jmp Die -%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 - +;; BEGIN OF THE HOLE -------------------------------------------------------- ;; Die: cli hlt ; die nooooow - jmp 0xF000:0xFFF0 + ;jmp 0xF000:0xFFF0 + jmp $ ;; END OF THE HOLE ---------------------------------------------------------- ;; -[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 - +main: jmp Die - diff --git a/boot/loader/multiboot.inc b/boot/loader/multiboot.inc index f013eda..8874596 100644 --- a/boot/loader/multiboot.inc +++ b/boot/loader/multiboot.inc @@ -24,10 +24,10 @@ ;=----------------------------------------------------------------------------=; ;; MULTIBOOT HEADER -%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 +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 diff --git a/build/obj/boot/loader.bin b/build/obj/boot/loader.bin index e12a50a9249c8e894d057034b1482000b1067b68..c5b1e9d04c8c64290d711872813bf39368172aab 100644 GIT binary patch literal 74 zcmYf6Inj5c?|{LjR~`%k3=9lR8`erQGcYpz_78j_02Tp>LVy8~zlVWAfgy`wZb0x0 TPM~P>5sudkzrMWw_xcwA7#|jm literal 2053 zcmeHI|4Um}6hAkKjg4k69kdMg!*1;y6gFqeDqC@M+NMNwHD9_KE1JdRT@xGgO7emo z`(YU}rgUgML%Uy zgkGq7g|@nYU1DxFbb}q?5t*Hh6%~i(*bQYb8(1XFfvOKOauu7xL~-FVdmR(SBTus_ zE^&PSTQpFaWH+;yFo1LBM}5+?)|jMg>@0JSWl`sv6mDAvUs%vzUedD;=FU!(XXIUz zPu;fUH?q$Y14aT+RqQ_dUX!CErSVr_zdKr~TOM_19|~ljfe18HNrb8lN>;6^>U)Tp zT8Dwvx&{4eCn<-A_f`uk6h_LH^%fW@yNmtXv(^K9AZVxGQwdqHw(463Nw=9xQJd*k1XMYxLiQF8Z=tq z8EBLwIT-H2vwFFi?l+svrrq>-K=hsC%H#I8noiWz)WARMtYL3=i1Qi_*HQ$cMs1K~ zvG2T0Wr;>aub+BpRP@PM6a7IsKpRds@XG1)hW%0GV?r~AccIM};IYuFMMC+e;6f$>5i8t{f>x*vl)xe}nQ&1j$}8=6`;<8(lzQO&hj zvi&p|E*5GZ&qbS}R1TDCxm)zcQBDF>$xR#@oM{5=%VKGGe zz1j_6QY`GpCH=d@iD2{`PB}__5pOgg7Rk+4=P7ForHxIeAYNb1tA@yXQkCglh!j7b z-^$PZ5IvI4=|{WXy(U~W2Y=$T;2PfC`A_qo{GHM<4LX1co@{Be-P2`zU-MD)e5oC!3cypqD(W=h{(Amz z|Lq;HyBy9oHuDEbezC@8ekX(TDAvi~0~Gg25>RYt;y%G&#jjV&J9rfv4;QU$V^ z0$FE$o|KU0Dn-(+*e#9=+p7Aw>Mxva?{H@w(|7RIO(YUaTkWNRqTufV%N6fpVnn+N zC<=HZu0vrrC?8^wR7LtX9R~cIVR)tM&es3{