123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836 |
- org 0x10000
- jmp Label_Start
- %include 'fat12.inc'
- Base_Of_Kernel_File equ 0x00
- Offset_Of_Kernel_File equ 0x100000
- Base_Tmp_Of_Kernel_Addr equ 0x00
- Offset_Tmp_Of_Kernel_File equ 0x7e00
- Memory_Struct_Buffer_Addr equ 0x7e00
- [SECTION gdt]
- LABEL_GDT: dd 0,0
- LABEL_DESC_CODE32: dd 0x0000FFFF,0x00CF9A00
- LABEL_DESC_DATA32: dd 0x0000FFFF,0x00CF9200
- GdtLen equ $ - LABEL_GDT
- GdtPtr dw GdtLen - 1
- dd LABEL_GDT
- SelectorCode32 equ LABEL_DESC_CODE32 - LABEL_GDT
- SelectorData32 equ LABEL_DESC_DATA32 - LABEL_GDT
- [SECTION gdt64]
- LABEL_GDT64: dq 0x0000000000000000
- LABEL_DESC_CODE64: dq 0x0020980000000000
- LABEL_DESC_DATA64: dq 0x0000920000000000
- GdtLen64 equ $ - LABEL_GDT64
- GdtPtr64 dw GdtLen64-1,
- dd LABEL_GDT64
- SelectorCode64 equ LABEL_DESC_CODE64 - LABEL_GDT64
- SelectorData64 equ LABEL_DESC_DATA64 - LABEL_GDT64
- [SECTION .s16]
- [BITS 16]
- Label_Start:
- mov ax, cs
- mov ds, ax
- mov es, ax
- mov ax, 0x00
- mov ss, ax
- mov sp, 0x7c00
-
- mov ax, 0x1301
- mov bx, 0x000f
- mov dx, 0x0100
- mov cx, 23
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Start_Loader
- int 0x10
-
-
- push ax
- in al, 0x92
- or al, 0x02
- out 0x92, al
- pop ax
- cli
- db 0x66
- lgdt [GdtPtr]
-
- mov eax, cr0
- or eax, 1
- mov cr0, eax
-
- mov ax, SelectorData32
- mov fs, ax
-
- mov eax, cr0
- and al, 11111110b
- mov cr0, eax
- sti
- mov word [SectorNo], SectorNumOfRootDirStart
- Label_Search_In_Root_Dir_Begin:
- cmp word [RootDirSizeForLoop], 0
- jz Label_No_KernelBin
- dec word [RootDirSizeForLoop]
- mov ax, 0x00
- mov es, ax
- mov bx, 0x8000
- mov ax, [SectorNo]
- mov cl, 1
- call Func_ReadOneSector
- mov si, Kernel_FileName
- mov di, 0x8000
- cld
- mov dx, 0x10
- Label_Search_For_LoaderBin:
- cmp dx, 0
- jz Label_Goto_Next_Sector_In_Root_Dir
- dec dx
- mov cx, 11
- Label_Cmp_FileName:
- cmp cx, 0
- jz Label_FileName_Found
- dec cx
- lodsb
- cmp al, byte [es:di]
- jz Label_Go_On
- jmp Label_Different
- Label_Go_On:
- inc di
- jmp Label_Cmp_FileName
- Label_Different:
- and di, 0xffe0
- add di, 0x20
- mov si, Kernel_FileName
- jmp Label_Search_For_LoaderBin
- Label_Goto_Next_Sector_In_Root_Dir:
- add word [SectorNo], 1
- jmp Label_Search_In_Root_Dir_Begin
- Label_No_KernelBin:
-
- mov ax, 0x1301
- mov bx, 0x000c
- mov dx, 0x0200
- mov cx, 24
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_No_Loader
- int 0x10
- jmp $
- Label_FileName_Found:
- mov ax, RootDirSectors
-
- and di, 0xffe0
- add di, 0x1a
- mov cx, word [es:di]
- push cx
- add cx, ax
- add cx, SectorBalance
- mov eax, Base_Tmp_Of_Kernel_Addr
- mov es, eax
- mov bx, Offset_Tmp_Of_Kernel_File
- mov ax, cx
- Label_Go_On_Loading_File:
- push ax
- push bx
-
- mov ah, 0x0e
- mov al, "."
- mov bl, 0x0f
- int 0x10
- pop bx
- pop ax
-
- mov cl, 1
- call Func_ReadOneSector
- pop ax
-
- push cx
- push eax
- push fs
- push edi
- push ds
- push esi
- mov cx, 0x0200
- mov ax, Base_Of_Kernel_File
- mov fs, ax
- mov edi, dword [OffsetOfKernelFileCount]
- mov ax, Base_Tmp_Of_Kernel_Addr
- mov ds, ax
- mov esi, Offset_Tmp_Of_Kernel_File
- Label_Move_Kernel:
-
- mov al, byte [ds:esi]
- mov byte [fs:edi], al
- inc esi
- inc edi
- loop Label_Move_Kernel
-
- mov eax, 0x1000
- mov ds, eax
- mov dword [OffsetOfKernelFileCount], edi
- pop esi
- pop ds
- pop edi
- pop fs
- pop eax
- pop cx
- call Func_GetFATEntry
- cmp ax, 0x0fff
- jz Label_File_Loaded
- push ax
- mov dx, RootDirSectors
- add ax, dx
- add ax, SectorBalance
-
- jmp Label_Go_On_Loading_File
- Label_File_Loaded:
-
- mov ax, 0x1301
- mov bx, 0x000f
- mov dx, 0x0200
- mov cx, 20
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Kernel_Loaded
- int 0x10
-
-
-
- mov ax, 0xB800
- mov gs, ax
- mov ah, 0x0F
- mov al, '.'
- mov [gs:((80 * 2 + 20) * 2)], ax
- Label_Kill_Motor:
-
-
- push dx
- mov dx, 0x03F2
- mov al, 0
- out dx, al
- pop dx
-
-
- mov ax, 0x1301
- mov bx, 0x000F
- mov dx, 0x0300
- mov cx, 34
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Start_Get_Mem_Struct
- int 0x10
- mov ax, 0x00
- mov es, ax
- mov di, Memory_Struct_Buffer_Addr
- mov ebx, 0
- Label_Get_Mem_Struct:
-
-
-
-
-
- mov eax, 0xe820
- mov ecx, 20
- mov edx, 0x534d4150
- int 0x15
- jc Label_Get_Mem_Fail
- add di, 20
- cmp ebx, 0
- jne Label_Get_Mem_Struct
- jmp Label_Get_Mem_OK
- Label_Get_Mem_Fail:
-
-
- mov ax, 0x1301
- mov bx, 0x000c
- mov dx, 0x0400
- mov cx, 33
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Get_Mem_Failed
- int 0x10
- jmp $
- Label_Get_Mem_OK:
-
- mov ax, 0x1301
- mov bx, 0x000f
- mov dx, 0x0400
- mov cx, 38
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Get_Mem_Success
- int 0x10
- jmp Label_Get_SVGA_Info
- Label_Get_SVGA_Info:
-
- mov ax, 0x1301
- mov bx, 0x000f
- mov dx, 0x0500
- mov cx, 34
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Start_Get_SVGA_VBE_Info
- int 0x10
-
-
- mov ax, 0x00
- mov es, ax
- mov di, 0x8000
- mov ax, 0x4F00
- int 0x10
- cmp ax, 0x004F
- jz Label_Get_SVGA_VBE_Success
- Label_Get_SVGA_VBE_Failed:
-
- mov ax, 0x1301
- mov bx, 0x008c
- mov dx, 0x0600
- mov cx, 33
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Get_SVGA_VBE_Failed
- int 0x10
- jmp $
- Label_Get_SVGA_VBE_Success:
- mov ax, 0x1301
- mov bx, 0x000f
- mov dx, 0x0600
- mov cx, 38
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Get_SVGA_VBE_Success
- int 0x10
- Label_Get_SVGA_Mode_Info:
-
- mov ax, 0x1301
- mov bx, 0x000f
- mov dx, 0x0700
- mov cx, 35
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Start_Get_SVGA_Mode_Info
- int 0x10
- mov ax, 0x00
- mov es, ax
- mov si, 0x800e
-
- mov esi, dword [es:si]
- mov edi, 0x8200
- Label_SVGA_Mode_Info_Get:
- mov cx, word [es:esi]
-
-
-
-
-
-
-
-
-
- cmp cx, 0xFFFF
- jz Label_SVGA_Mode_Info_Finish
- mov ax, 0x4f01
- int 0x10
- cmp ax, 0x004f
- jnz Label_SVGA_Mode_Info_Fail
- add esi, 2
- add edi, 0x100
- jmp Label_SVGA_Mode_Info_Get
- Label_SVGA_Mode_Info_Fail:
-
- mov ax, 0x1301
- mov bx, 0x008c
- mov dx, 0x0800
- mov cx, 34
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Get_SVGA_Mode_Failed
- int 0x10
- jmp $
- Label_SVGA_Mode_Info_Finish:
-
- mov ax, 0x1301
- mov bx, 0x000f
- mov dx, 0x0800
- mov cx, 39
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Get_SVGA_Mode_Success
- int 0x10
- jmp Label_Set_SVGA_Mode
- Label_SET_SVGA_Mode_VESA_VBE_FAIL:
-
- mov ax, 0x1301
- mov bx, 0x008c
- mov dx, 0x0800
- mov cx, 29
- push ax
- mov ax, ds
- mov es, ax
- pop ax
- mov bp, Message_Set_SVGA_Mode_Failed
- int 0x10
- jmp $
- Label_Set_SVGA_Mode:
- mov ax, 0x4f02
- mov bx, 0x4180
- int 0x10
- cmp ax, 0x004F
- jnz Label_SET_SVGA_Mode_VESA_VBE_FAIL
- cli
- db 0x66
- lgdt [GdtPtr]
- db 0x66
- lidt [IDT_POINTER]
- mov eax, cr0
- or eax, 1
- mov cr0, eax
-
- jmp dword SelectorCode32:GO_TO_TMP_Protect
- [SECTION .s32]
- [BITS 32]
- GO_TO_TMP_Protect:
-
- mov ax, 0x10
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov ss, ax
- mov esp, 0x7e00
- call support_long_mode
- test eax, eax
- jz no_support
-
-
- mov dword [0x90000], 0x91007
- mov dword [0x90004], 0x00000
- mov dword [0x90800], 0x91007
- mov dword [0x90804], 0x00000
- mov dword [0x91000], 0x92007
- mov dword [0x91004], 0x00000
- mov dword [0x92000], 0x000083
- mov dword [0x92004], 0x000000
- mov dword [0x92008], 0x200083
- mov dword [0x9200c], 0x000000
- mov dword [0x92010], 0x400083
- mov dword [0x92014], 0x000000
- mov dword [0x92018], 0x600083
- mov dword [0x9201c], 0x000000
- mov dword [0x92020], 0x800083
- mov dword [0x92024], 0x000000
- mov dword [0x92028], 0xa00083
- mov dword [0x9202c], 0x000000
-
- db 0x66
- lgdt [GdtPtr64]
-
- mov ax, 0x10
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
- mov ss, ax
- mov esp, 0x7e00
-
-
- mov eax, cr4
- bts eax, 5
- mov cr4, eax
-
- mov eax, 0x90000
- mov cr3, eax
-
-
-
- mov ecx, 0xC0000080
- rdmsr
- bts eax, 8
- wrmsr
-
- mov eax, cr0
- bts eax, 0
- bts eax, 31
- mov cr0, eax
-
- jmp SelectorCode64:Offset_Of_Kernel_File
- support_long_mode:
-
- mov eax, 0x80000000
- cpuid
- cmp eax, 0x80000001
- setnb al
- jb support_long_mode_done
- mov eax, 0x80000001
- cpuid
- bt edx, 29
-
- setc al
- support_long_mode_done:
- movzx eax, al
- ret
- no_support:
-
- jmp $
- [SECTION .s16lib]
- [BITS 16]
- Func_ReadOneSector:
- push bp
- mov bp, sp
- sub esp, 2
- mov byte [bp-2], cl
- push bx
- mov bl, [BPB_SecPerTrk]
- div bl
- inc ah
- mov cl, ah
- mov dh, al
- shr al, 1
- mov ch, al
- and dh, 1
- pop bx
- mov dl, [BS_DrvNum]
-
-
- Label_Go_On_Reading:
-
- mov ah, 2
- mov al, byte [bp-2]
- int 0x13
- jc Label_Go_On_Reading
- add esp, 2
- pop bp
- ret
- Func_GetFATEntry:
-
-
- push es
- push bx
- push ax
-
- mov ax, 00
- mov es, ax
- pop ax
- mov byte [Odd], 0
-
- mov bx, 3
- mul bx
- mov bx, 2
- div bx
- cmp dx, 0
- jz Label_Even
- mov byte [Odd], 1
- Label_Even:
- xor dx, dx
-
- mov bx, [BPB_BytesPerSec]
- div bx
- push dx
-
- mov bx, 0x8000
- add ax, SectorNumOfFAT1Start
- mov cl, 2
- call Func_ReadOneSector
- pop dx
- add bx, dx
- mov ax, [es:bx]
- cmp byte [Odd], 1
- jnz Label_Even_2
- shr ax, 4
- Label_Even_2:
- and ax, 0x0fff
- pop bx
- pop es
- ret
- Label_DispAL:
- push ecx
- push edx
- push edi
- mov edi, [DisplayPosition]
- mov ah, 0x0F
- mov dl, al
- shr al, 4
- mov ecx, 2
- .begin:
- and al, 0x0F
- cmp al, 9
- ja .1
- add al, '0'
- jmp .2
- .1:
- sub al, 0x0a
- add al, 'A'
- .2:
-
- mov [gs:edi], ax
- add edi, 2
- mov al, dl
- loop .begin
- mov [DisplayPosition], edi
- pop edi
- pop edx
- pop ecx
- ret
- IDT:
- times 0x50 dq 0
- IDT_END:
- IDT_POINTER:
- dw IDT_END - IDT - 1
- dd IDT
- RootDirSizeForLoop dw RootDirSectors
- SectorNo dw 0
- Odd db 0
- OffsetOfKernelFileCount dd Offset_Of_Kernel_File
- DisplayPosition dd 0
- Message_Start_Loader: db "[DragonOS] Start Loader"
- Message_No_Loader: db "[ERROR] No Kernel Found."
- Message_Kernel_Loaded: db "[INFO] Kernel loaded"
- Message_Start_Get_Mem_Struct: db "[INFO] Try to get memory struct..."
- Message_Get_Mem_Failed: db "[ERROR] Get memory struct failed."
- Message_Get_Mem_Success: db "[INFO] Successfully got memory struct."
- Message_Start_Get_SVGA_VBE_Info: db "[INFO] Try to get SVGA VBE info..."
- Message_Get_SVGA_VBE_Failed: db "[ERROR] Get SVGA VBE info failed."
- Message_Get_SVGA_VBE_Success: db "[INFO] Successfully got SVGA VBE info."
- Message_Start_Get_SVGA_Mode_Info: db "[INFO] Try to get SVGA mode info..."
- Message_Get_SVGA_Mode_Failed: db "[ERROR] Get SVGA Mode info failed."
- Message_Get_SVGA_Mode_Success: db "[INFO] Successfully got SVGA Mode info."
- Message_Set_SVGA_Mode_Failed: db "[ERROR] Set SVGA Mode failed."
- Kernel_FileName: db "KERNEL BIN", 0
|