Guest User

Mtgos Bootloader

a guest
May 22nd, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ORG 0x7C00
  2. jmp 0x0000:start
  3. bootdrv db 0
  4. start:
  5.     cli
  6.     mov ax, 0x9000
  7.     mov ss, ax
  8.     mov sp, 0
  9.     sti
  10.     mov ax, 0x0000
  11.     mov es,ax
  12.     mov ds,ax
  13.     mov [bootdrv],dl
  14.     call load
  15.     call A20
  16.     mov ax, 0x1000
  17.     mov es,ax
  18.     mov ds,ax
  19.     jmp 0x1000:0x0000
  20. load:
  21.     mov ax,0
  22.     mov dl, [bootdrv]
  23.     int 13h
  24.     jc load
  25. load1:
  26.     mov ax, 0x1000
  27.     mov es,ax
  28.     mov bx,0
  29.     mov ah,2
  30.     mov al,20
  31.     mov cx,2
  32.     mov dh,0
  33.     mov dl,[bootdrv]
  34.     int 13h
  35.     jc load1
  36.     retn
  37. A20:
  38.     mov ax,0x2401
  39.     int 0x15
  40.     jc A20OLD
  41.     retn
  42. A20OLD:
  43.     cli
  44.     call a20wait
  45.     mov al,0xAD
  46.     out 0x64,al
  47.     call a20wait
  48.     mov al,0xD0
  49.     out 0x64,al
  50.     call a20wait2
  51.     in al,0x60
  52.     push ax
  53.     call a20wait
  54.     mov al,0xD1
  55.     out 0x64,al
  56.     call a20wait
  57.     pop ax
  58.     or al,2
  59.     out 0x60,al
  60.     call a20wait
  61.     mov al,0xAE
  62.     out 0x64,al
  63.     call a20wait
  64.     sti
  65.     ret
  66. a20wait:
  67.     in al,0x64
  68.     test al,2
  69.     jnz a20wait
  70.     ret
  71. a20wait2:
  72.     in al,0x64
  73.     test al,1
  74.     jz a20wait2
  75.     ret
  76.  
  77. times 510-($-$$) db 0
  78. dw 0x0AA55
  79. mov ax,0x1000
  80. mov ds,ax
  81. mov es,ax
  82. starts:
  83.     call protected_mode_supported
  84.     jmp pmode
  85. protected_mode_supported:
  86.     cli
  87.     pushf
  88.     pushf
  89.     pop ax
  90.     mov bx,ax
  91.     and ax,0x0FFF
  92.     or bx,0x7000
  93.     push ax
  94.     popf
  95.     pushf
  96.     pop ax
  97.     push bx
  98.     popf
  99.     pushf
  100.     pop bx
  101.     popf
  102.     and ax,0xF000
  103.     cmp ax,0xF000
  104.     je not_supported
  105.     test bx,0x7000
  106.     je not_supported
  107.     retn
  108. not_supported:
  109.     mov si,nsstring
  110.     call putstr
  111.     cli
  112. hang:
  113.     halt
  114.     jmp hang
  115. putstr:
  116.     lodsb
  117.     or al,al
  118.     jz putstrd
  119.     mov ah,0x0E
  120.     mov bx,0x00F0
  121.     int 0x10
  122.     jmp putstr
  123. putstrd:
  124.     retn
  125. pmode:
  126.     cli
  127.     lgdt [gdt]
  128.     mov eax,cr0
  129.     or al,1
  130.     mov cr0,eax
  131.     jmp kcodesel:weiter
  132. nsstring db "[FATAL] Dies ist ein 32-Bit OS! Sie Benutzen aber einen 8086, 80186 oder 80286er! System wird angehalten!",13,10,0
  133. [BITS 32]
  134. weiter:
  135.     call pae
  136.     call lmode
  137. pae:
  138.     mov eax,0x00000001
  139.     cpuid
  140.     and edx, 0x00000020
  141.     jnz activatepae
  142.     retn
  143. activatepae:
  144.     mov eax,cr4
  145.     or eax, 0x00000010
  146.     mov cr4,eax
  147.     retn
  148. lmode:
  149.     mov eax, 0x80000001
  150.     cpuid
  151.     and edx, 0x20000000
  152.     test edx, edx
  153.     jz non64
  154.     mov eax,1
  155.     cpuid
  156.     and edx, 0x00000040
  157.     test edx, edx
  158.     jz non64
  159.     mov edi, 0x300000
  160.     mov eax, 0x30100F
  161.     stosd
  162.     mov ecx, 0x03FF
  163.     xor eax,eax
  164.     rep stosd
  165.     mov eax,0x30200F
  166.     stosd
  167.     mov ecx,0x03FF
  168.     xor eax,eax
  169.     rep stosd
  170.     mov ecx,0x200
  171.     mov esi, 0x8B
  172. map.loop:
  173.     mov eax,esi
  174.     stosd
  175.     xor eax,eax
  176.     stosd
  177.     add esi,0x200000
  178.     sub ecx,1
  179.     jnz map.loop
  180.     mov eax,0x300000
  181.     mov cr3,eax
  182.     mov ecx,0xC0000080
  183.     rdmsr
  184.     or eax, 0x00000100
  185.     wrmsr
  186.     mov eax,cr0
  187.     or eax,0x80000000  
  188.     mov cr0,eax
  189.     jmp 0x30:bootLM
  190. non64:
  191.     retn
  192. [bits 64]
  193. bootLM:
  194.     xor rax,rax
  195.     retn
  196. [bits 32]
  197. gdt:
  198. dw 31
  199. dd GDTPR
  200. GDTPR:
  201. ; NULLSEGMENT
  202. dw 0 ;Segment Limit 0-15
  203. dw 0 ;Base Address 0-15
  204. db 0 ;Base Address 16-23
  205. db 0 ;Flags
  206. db 0 ;Sonstiges
  207. db 0 ;Base Address 24-31
  208. kcodesel equ $-gdt
  209. ; CODESEGMENT KERNEL
  210. dw 0xFFFF
  211. db 0x0000
  212. db 0x00
  213. db 10011010b ; Present, RING 0, Segment, Executable, Readable Bit
  214. db 01011111b
  215. db 0x00
  216. kdatasel equ $-gdt
  217. ; DATASEGMENT KERNEL
  218. dw 0xFFFF
  219. db 0x0000
  220. db 0x00
  221. db 10010010b ; Present, RING 0, Segment, RW Bit
  222. db 01011111b
  223. db 0x00
  224. ucodesel equ $-gdt
  225. ; CODESEGMENT USERSPACE
  226. dw 0xFFFF
  227. db 0x0000
  228. db 0x00
  229. db 11111010b ; Present, RING 3, Segment, Executable, Readable Bit
  230. db 01011111b
  231. db 0x00
  232. udatasel equ $-gdt
  233. ; DATASEGMENT USERSPACE
  234. dw 0xFFFF
  235. db 0x0000
  236. db 0x00
  237. db 11110010b ; Present, RING 3, Segment, RW Bit
  238. db 01011111b
  239. db 0x00
Advertisement
Add Comment
Please, Sign In to add comment