Advertisement
Guest User

Untitled

a guest
Jun 26th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [ORG 0xA000]
  2. [BITS 16]
  3.  
  4. mov ax, 0x200           ; destination segment
  5. mov es, ax              ; is stored in es
  6. mov ah, 0x02            ; function = 02h to read
  7. mov al, 64              ; read 47 (for some reason this is the most that can be read)
  8. mov bx, 0               ; track number 0
  9. mov ch, bl              ; in ch
  10. mov bx, 7               ; sector number 7
  11. mov cl, bl              ; in cl
  12. inc cl                  ; make it one-based
  13. mov dh, 0               ; head number 0 goes in dh
  14. mov dl, 0x80            ; hard drive
  15.  
  16. mov bx, 0x0000          ; offset goes in bx
  17. ; stage3 will be stored at 0x2000
  18.  
  19. ; Call the BIOS to read the disk
  20. int 0x13
  21.  
  22. ; If the carry flag was set, then there was an error.
  23. mov bp, error_stage_3
  24. jc near error
  25.  
  26. ; Get the BIOS memory map
  27. do_e820:
  28.     xor bp, bp                  ; Keep the number of entries in BP
  29.    
  30.     mov ax, 0x00
  31.     mov es, ax                  ; Segment 0x00
  32.     mov di, 0x500               ; Offset 0x500
  33.     xor ebx, ebx                ; Clear EBX
  34.     mov edx, 0x534D4150         ; Move "SMAP" into EDX
  35.     mov eax, 0xE820
  36.     mov [es:di + 20], dword 1   ; Force a valid ACPI 3.x entry
  37.     mov ecx, 24                 ; 24 byte entry
  38.     int 0x15                    ; Get the memory map
  39.    
  40.     jc short .failed            ; Error message on failure
  41.     mov edx, 0x534D4150         ; Some BIOSes trash EDX
  42.     cmp eax, edx                ; On success, EAX == "SMAP"
  43.     jne short .failed           ; If not, error message
  44.     test ebx, ebx               ; EBX = 0 means 1 entry
  45.     je short .failed            ; If so, error message
  46.     jmp short .jmpin            ; Continue
  47. .e820lp:
  48.     mov eax, 0xe820             ; EAX gets trashed on every int 0x15 call
  49.     mov [es:di + 20], dword 1   ; force a valid ACPI 3.X entry
  50.     mov ecx, 24                 ; ECX also trashed
  51.     int 0x15
  52.     jc short .e820f             ; Carry means "end of list already reached"
  53.     mov edx, 0x0534D4150        ; repair potentially trashed register
  54. .jmpin:
  55.     jcxz .skipent               ; Skip any 0 length entries
  56.     cmp cl, 20                  ; Got a 24 byte ACPI 3.X response?
  57.     jbe short .notext
  58.     test byte [es:di + 20], 1   ; If so, is the "ignore this data" bit clear?
  59.     je short .skipent
  60. .notext:
  61.     mov ecx, [es:di + 8]        ; get lower dword of memory region length
  62.     or ecx, [es:di + 12]        ; "or" it with upper dword to test for zero
  63.     jz .skipent                 ; if length qword is 0, skip entry
  64.     inc bp                      ; got a good entry: ++count, move to next storage spot
  65.     add di, 24
  66. .skipent:
  67.     test ebx, ebx               ; if ebx resets to 0, list is complete
  68.     jne short .e820lp
  69. .e820f:
  70.     mov [os_info + 6], bp       ; Store the entry count in the OS info structure
  71.     clc                         ; There is a carry flag at this point, so it must be cleared
  72.     jmp enable_a20              ; Leave the function
  73. .failed:
  74.     mov bp, error_mem_map
  75.     jmp error
  76.  
  77. ; Enable the A20 gate
  78. enable_a20:
  79.     in al, 0x92
  80.     or al, 2
  81.     out 0x92, al
  82.  
  83. lgdt [gdtr]     ; Load our GDT
  84.  
  85. mov eax, cr0    ; Switch to protected mode by
  86. or al,1         ; setting the protected mode bit
  87. mov cr0, eax    ; in CR0
  88.  
  89. ; Reload the segment registers
  90. jmp 0x08:reload_segs
  91.  
  92. error:
  93.     mov ax, 0x00
  94.     mov es, ax                  ; segment
  95.     mov al, 0x01                ; write mode
  96.     mov ah, 0x13                ; interrupt #
  97.     mov bh, 0x00                ; page #
  98.     mov bl, 0x04                ; color (red)
  99.     mov cx, 45                  ; string length
  100.     mov dh, 0x00                ; row
  101.     mov dl, 0x00                ; column
  102.     int 0x10
  103.     jmp eternal
  104.  
  105. error_stage_3   db      'Error loading stage3 in bigbang.  Aborting...'
  106. error_mem_map   db      'Error retrieving E820 memory map. Aborting...'
  107.  
  108. eternal:
  109.     jmp eternal
  110.        
  111. os_info:
  112.     dw 0, 0x500 ; BIOS memory map
  113.     dd 0        ; Number of memory map entries
  114.     dw 0, 0     ; VBE mode info
  115.  
  116. gdtr:
  117.     dw gdt_end - null_seg - 1   ; last byte in table
  118.     dd null_seg                 ; start of table
  119.  
  120. null_seg                    db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00   ; entry 0 (null seg) is always unused
  121. code_seg                    db 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00   ; entry 1 (code seg)
  122. data_seg                    db 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00   ; entry 2 (data seg)
  123. gdt_end:
  124.  
  125. [BITS 32]
  126.  
  127. reload_segs:
  128.     mov ax, 0x10
  129.     mov ds, ax
  130.     mov es, ax
  131.     mov fs, ax
  132.     mov gs, ax
  133.     mov ss, ax
  134.    
  135. mov ebx, os_info    ; Give stage3 the OS info struct
  136. jmp 0x2000          ; Jump to stage3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement