seb15753

boot/bootsect.asm

Jun 24th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;a.michelizza.free.fr
  2. %define    BASE    0x100
  3. %define KSIZE    30
  4.  
  5. [BITS 16]
  6. [ORG 0x0]
  7.  
  8. jmp start
  9.  
  10. %include "GDT.INC"
  11.  
  12. start:
  13.  
  14. ; init segments at 0x07C0
  15.     mov ax, 0x07C0
  16.     mov ds, ax
  17.     mov es, ax
  18.     mov ax, 0x8000    
  19.     mov ss, ax
  20.     mov sp, 0xf000
  21.  
  22. ; boot unit
  23.     mov [bootdrv], dl    
  24.  
  25. ; load kernel
  26.     xor ax, ax
  27.     int 0x13
  28.  
  29.     push es
  30.     mov ax, BASE
  31.     mov es, ax
  32.     mov bx, 0
  33.    
  34.     mov ah, 2
  35.     mov al, KSIZE
  36.     mov ch, 0
  37.     mov cl, 2
  38.     mov dh, 0
  39.     mov dl, [bootdrv]
  40.     int 0x13
  41.     pop es
  42.  
  43.  
  44. ; init GDT see GDT.inc for functions and macros
  45.     ; descInit base(32), limite(20/32), acces/type(8), flags(4/8), adresse(16)
  46.     descInit 0, 0xFFFFF, 10011011b, 1101b, gdt_cs
  47.     descInit 0, 0xFFFFF, 10010011b, 1101b, gdt_ds
  48.  
  49.     mov ax, gdtend    
  50.     mov bx, gdt
  51.     sub ax, bx
  52.     mov word [gdtptr], ax
  53.  
  54.     xor eax, eax    
  55.     mov  ax, ds
  56.     mov  bx, gdt
  57.     call calcadr
  58.     mov dword [gdtptr+2], ecx
  59.  
  60. ; switching to protected mode
  61.     cli
  62.     lgdt [gdtptr]    
  63.     mov eax, cr0
  64.     or   ax, 1
  65.     mov cr0, eax  
  66.  
  67.     jmp next
  68. next:
  69.     mov ax, 0x10    ;data segment
  70.     mov ds, ax
  71.     mov fs, ax
  72.     mov gs, ax
  73.     mov es, ax
  74.     mov ss, ax
  75.     mov esp, 0x9F000    
  76.  
  77.  
  78.     call enable_A20
  79.  
  80.     jmp dword 0x8:0x1000
  81.  
  82. end:
  83.     jmp end
  84. enable_A20:
  85.  
  86.         call    a20wait
  87.         mov     al,0xAD
  88.         out     0x64,al
  89.  
  90.         call    a20wait
  91.         mov     al,0xD0
  92.         out     0x64,al
  93.  
  94.         call    a20wait2
  95.         in      al,0x60
  96.         push    eax
  97.  
  98.         call    a20wait
  99.         mov     al,0xD1
  100.         out     0x64,al
  101.  
  102.         call    a20wait
  103.         pop     eax
  104.         or      al,2
  105.         out     0x60,al
  106.  
  107.         call    a20wait
  108.         mov     al,0xAE
  109.         out     0x64,al
  110.  
  111.         call    a20wait
  112.         ret
  113.  
  114. a20wait:
  115.         in      al,0x64
  116.         test    al,2
  117.         jnz     a20wait
  118.         ret
  119.  
  120.  
  121. a20wait2:
  122.         in      al,0x64
  123.         test    al,1
  124.         jz      a20wait2
  125.         ret
  126.  
  127. ;--------------------------------------------------------------------
  128.  
  129. gdt:
  130. gdt_null:
  131.     dw 0, 0, 0, 0
  132. gdt_cs:
  133.     dw 0, 0, 0, 0
  134. gdt_ds:
  135.     dw 0, 0, 0, 0
  136. gdtend:
  137.  
  138. gdtptr:
  139.     dw    0x0000    ; limit
  140.     dd    0         ; base
  141.  
  142. bootdrv: db 0
  143.  
  144. ;--------------------------------------------------------------------
  145. times 510-($-$$) db 144
  146. dw 0xAA55
Advertisement
Add Comment
Please, Sign In to add comment