Advertisement
Midnightas

Untitled

Oct 8th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. section .multiboot2
  2. align 8
  3. HEADER_START:
  4.     dd 0xE85250D6
  5.     dd 0
  6.     dd (HEADER_END - HEADER_START)
  7.     dd -(0xE85250D6 + (HEADER_END - HEADER_START))
  8.  
  9. ; Request 640x480 framebuffer.
  10. align 8
  11.     dw 5
  12.     dw 0
  13.     dd 20
  14.     dd 640
  15.     dd 480
  16.     dd 32
  17.    
  18. align 8
  19.     dw 0
  20.     dw 0
  21.     dd 8
  22. HEADER_END:
  23.  
  24. section .bss
  25. STACK_BOTTOM: resb 16384
  26. STACK_TOP:
  27.  
  28. align 4096
  29. BOOT_PAGE_DIR0: resb 4096
  30. ; Tables directly next to eachother for cleanliness.
  31. BOOT_PAGE_TBL0: resb 4096
  32. BOOT_PAGE_TBL1: resb 4096
  33.  
  34. section .text
  35. extern main
  36. extern _kernel_start
  37. extern _kernel_end
  38.  
  39. global _start:function (_start.end - _start)
  40. _start:
  41.     mov esp, STACK_TOP - 0xE0000000
  42.    
  43.     mov edi, BOOT_PAGE_TBL0 - 0xE0000000
  44.     mov esi, 0
  45.     mov ecx, 2048 - 301 ; 301 pages for the framebuffer in case they're not page aligned, which I'm not sure of yet.
  46. .loo:
  47.     cmp esi, _kernel_start - 0xE0000000
  48.     jl .loopcond
  49.     cmp esi, _kernel_end - 0xE0000000
  50.     jge .loopend
  51.    
  52.     mov eax, esi
  53.     or eax, 3
  54.     mov [edi], eax
  55. .loopcond:
  56.     add esi, 4096
  57.     add edi, 4
  58.     loop .loo
  59. .loopend:
  60.     call _find_framebuffer - 0xE0000000
  61.     call _page_framebuffer - 0xE0000000
  62.    
  63.     ; Identity mapping.
  64.     ; +3 is the flags.
  65.     mov dword [BOOT_PAGE_DIR0 - 0xE0000000 + (000 * 4)], (BOOT_PAGE_TBL0 - 0xE0000000 + 3)
  66.     mov dword [BOOT_PAGE_DIR0 - 0xE0000000 + (001 * 4)], (BOOT_PAGE_TBL1 - 0xE0000000 + 3)
  67.  
  68.     ; Mapping to 0xE0000000.
  69.     mov dword [BOOT_PAGE_DIR0 - 0xE0000000 + (896 * 4)], (BOOT_PAGE_TBL0 - 0xE0000000 + 3)
  70.     mov dword [BOOT_PAGE_DIR0 - 0xE0000000 + (897 * 4)], (BOOT_PAGE_TBL1 - 0xE0000000 + 3)
  71.    
  72.     mov ecx, BOOT_PAGE_DIR0 - 0xE0000000
  73.     mov cr3, ecx
  74.    
  75.     mov ecx, cr0
  76.     or ecx, 0x80010000
  77.     mov cr0, ecx
  78.    
  79.     add esp, 0xE0000000
  80.    
  81.     jmp .afterpage ; Higher-half jump.
  82. .afterpage:
  83.     push ebx
  84.     call main
  85.    
  86.     cli
  87. .hlt:
  88.     hlt
  89.     jmp .hlt
  90. .end:
  91.  
  92. ; Expects multiboot data to be in ebx.
  93. _find_framebuffer:
  94.     add ebx, 8
  95. .loop:
  96.     cmp dword [ebx], 0 ; End tag type.
  97.     jne .notzero
  98.     ret
  99. .notzero:
  100.     cmp dword [ebx], 8 ; Framebuffer tag type.
  101.     je .isfb
  102.     add ebx, [ebx + 4] ; Add size of tag to address, multiboot2 spec says it's at offset 4.
  103.     ; Align the address, as according to
  104.     add ebx, 7
  105.     and ebx, ~7
  106.     jmp .loop
  107. .isfb:
  108.     mov ebx, [ebx + 8] ; Get framebuffer address, at offset 8.
  109.     ret
  110.  
  111. ; Maps to the page at edi, which was set before in _start.
  112. _page_framebuffer:
  113.     mov ecx, 301 ; 301 in case the framebuffer address is not page aligned, to fit it perfectly. Might come up with a nicer solution later.
  114.     mov esi, ebx ; Copy to esi for readability and to keep ebx unchanged, as we pass it later to main.
  115.     and esi, ~4096 ; Align to page.
  116. .loo:
  117.     mov eax, esi
  118.     or eax, 3
  119.     mov [edi], eax
  120.     loop .loo
  121.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement