Skip to content

Instantly share code, notes, and snippets.

@amanuel2
Created October 4, 2016 12:08
Show Gist options
  • Select an option

  • Save amanuel2/16c94647125fe00bfbad17e3c09e0d5d to your computer and use it in GitHub Desktop.

Select an option

Save amanuel2/16c94647125fe00bfbad17e3c09e0d5d to your computer and use it in GitHub Desktop.
;----------------------------------------------;
;
; The BoneOS Stage 2 Bootloader
; -----------------------------
;
;
; Contributors : @amanuel2
;
;
;----------------------------------------------;
stage2:
MOV si, SECONDSTAGEXECUTION
CALL printfbln
;-------------------
;Load Kernel
;-------------------
; enable A20 gate
enable_A20: ; Enabling A20 Line For Full Memory
cli ; Stop Interupts before doing so
call a20wait ; a20wait call
mov al,0xAD ; Send 0xAD Command to al register
out 0x64,al ; Send command 0xad (disable keyboard).
call a20wait ; When controller ready for command
mov al,0xD0 ; Send 0xD0 Command to al register
out 0x64,al ; Send command 0xd0 (read from input)
call a20wait2 ; When controller ready for command
in al,0x60 ; Read input from keyboard
push eax ; Save Input by pushing to stack
call a20wait ; When controller ready for command
mov al,0xD1 ; mov 0xD1 Command to al register
out 0x64,al ; Set command 0xd1 (write to output)
call a20wait ; When controller ready for command
pop eax ; Pop Input from Keyboard
or al,2 ; Mov 0xD3 to al register
out 0x60,al ; Set Command 0xD3
call a20wait ; When controller ready for command
mov al,0xAE ; Mov Command 0xAE To al register
out 0x64,al ; Write command 0xae (enable keyboard)
call a20wait ; When controller ready for command
sti ; Enable Interrupts after enabling A20 Line
ret
a20wait:
in al,0x64 ; input from 0x64 port, goes to al register
test al,2 ; compares al register with 2
jnz a20wait ; If it is zero loop again
ret
a20wait2:
in al,0x64 ; input from 0x64 port, goes to al register
test al,1 ; compares al register with 2
jz a20wait2 ; If it is zero loop again
ret
;load a GDT
; enter pmode
loadgdt:
cli ; disable int
LGDT [gdt_descriptor] ; Load global descriptor table for protected mode
MOV EAX, CR0 ; Move CR0 to GP register
OR EAX, 0x1 ; Set first bit to switch to protected mode
MOV CR0, EAX ; Update CR0 from GP register to complete switch
JMP gdt_codeSeg:start32 ; Jump to start of 32-bit code
[BITS 32] ; 32-bit protected mode
start32: ; Jump target for start of protected mode code
MOV AX, gdt_dataSeg ; Update segments for protected mode as defined in GDT
MOV DS, AX
MOV SS, AX
MOV ES, AX
MOV FS, AX
MOV GS, AX
MOV EBP, 0x90000 ; Set stack base at top of free space
MOV ESP, EBP ; Set stack pointer at base of stack
CALL ADDR_KERNEL_OFFSET ; Begin executing the kernel
ADDR_KERNEL_OFFSET EQU 0x1000 ; Define location for kernel in memory
; jump to kernel
cli
hlt
times ((0x400) - ($ - $$)) db 0x00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment