Advertisement
MichaelPetch

Bootloader SO

Sep 27th, 2022
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. [bits 16]
  2. [extern kernel]
  3.  
  4. section .boot
  5. global boot
  6.  
  7. KERNEL_SIZE_SECTORS equ 128 ;Kernel size in sectors - values > 128 not
  8. ; usually supported with Int 13h/AH=42h!
  9.  
  10. boot:
  11. cld ;set direction flag forward, needed for C code
  12. ; and any MOVS instructions that expect normal
  13. ; behaviour
  14.  
  15. xor ax, ax
  16. mov ds, ax ; set DS to 0
  17. mov ss, ax
  18. mov sp, 0x7c00 ;Set stack SS:SP below bootloader@0x0000:0x7c00
  19.  
  20. mov [disk], dl ;Save bootdrive in DL to disk variable
  21. mov ax, 0x2401 ;Enable A20 line, Int 15h/AX=2401 doesn't work
  22. ; in all environments
  23. int 0x15
  24.  
  25. mov ax, 0x3 ;Set the VGA mode, unknown at boot
  26. int 0x10
  27.  
  28. mov ah, 42h ;Do modern disk reading (no fuckery)
  29. mov dl, [disk] ;Account for cosmic bit flips
  30. mov si, dap ;Load disk address packet
  31. int 0x13
  32.  
  33. jc disk_err ;Jump if disk read error
  34.  
  35. cli ;Clear the interrupts
  36. lgdt [GDT_POINTER] ;Load the GDT using a gdt pointer
  37. mov eax, cr0
  38. or eax, 0x1
  39. mov cr0, eax
  40. jmp CODE_SEG:boot2
  41.  
  42. print:
  43. pusha
  44. start:
  45. mov al, [bx]
  46. cmp al, 0
  47. je done
  48.  
  49. mov ah, 0x0e
  50. int 0x10
  51.  
  52. add bx, 1
  53. jmp start
  54.  
  55. done:
  56. popa
  57. ret
  58.  
  59. print_nl:
  60. pusha
  61.  
  62. mov ah, 0x0e
  63. mov al, 0x0a
  64. int 0x10
  65. mov al, 0x0d
  66. int 0x10
  67.  
  68. popa
  69. ret
  70.  
  71. print_hex:
  72. pusha
  73.  
  74. mov cx, 0
  75.  
  76. hex_loop:
  77. cmp cx, 4
  78. je end
  79.  
  80. mov ax, dx
  81. and ax, 0x000f
  82. add al, 0x30
  83. cmp al, 0x39
  84. jle step2
  85. add al, 7
  86.  
  87. step2:
  88. mov bx, HEX_OUT + 5
  89. sub bx, cx
  90. mov [bx], al
  91. ror dx, 4
  92.  
  93. add cx, 1
  94. jmp hex_loop
  95.  
  96. end:
  97. mov bx, HEX_OUT
  98. call print
  99.  
  100. popa
  101. ret
  102.  
  103. disk_err: ;Label for any disk errors
  104. mov bx, disk_read_error ;Print the disk error
  105. call print
  106. call print_nl
  107. mov dh, ah ;Load error code in ah register
  108. call print_hex
  109. jmp $
  110.  
  111. GDT_START:
  112. dq 0x0
  113. GDT_CODE:
  114. dw 0xFFFF
  115. dw 0x0
  116. db 0x0
  117. db 10011010b
  118. db 11001111b
  119. db 0x0
  120. GDT_DATA:
  121. dw 0xFFFF
  122. dw 0x0
  123. db 0x0
  124. db 10010010b
  125. db 11001111b
  126. db 0x0
  127. GDT_END:
  128. GDT_POINTER:
  129. dw GDT_END - GDT_START
  130. dd GDT_START
  131.  
  132. CODE_SEG equ GDT_CODE - GDT_START
  133. DATA_SEG equ GDT_DATA - GDT_START
  134.  
  135. ; Bootloader data
  136. disk:
  137. db 0x0
  138.  
  139. dap:
  140. db 0x10
  141. db 0
  142. dw KERNEL_SIZE_SECTORS
  143. dw 0 ;
  144. dw 0x07e0 ;value recommended by a friend
  145. dq 1 ;Start reading from the second sector
  146.  
  147. HEX_OUT:
  148. db '0x0000',0
  149.  
  150. disk_read_error: db "Disk read error!", 0
  151.  
  152. times 510 - ($-$$) db 0
  153. dw 0xaa55
  154.  
  155. [bits 32]
  156. copy_target:
  157. boot2:
  158. mov ax, DATA_SEG
  159. mov ds, ax
  160. mov es, ax
  161. mov fs, ax
  162. mov gs, ax
  163. mov ss, ax
  164. mov esi, loaded_msg
  165. mov ebx, 0xb8000
  166. .loop: ;Print a message using a loop
  167. lodsb
  168. or al, al
  169. jz load_kernel ;If the message is finished, load kern
  170. or eax,0x0F00
  171. mov word [ebx], ax
  172. add ebx,2
  173. jmp .loop
  174. load_kernel:
  175. mov esp, kernel_stack_top;Load the stack to call C functions
  176. call kernel ;Call the actual kernel
  177. halt:
  178. cli
  179. hlt
  180.  
  181. loaded_msg: db "Operating system loaded",0
  182. ;Message here to verify disk read
  183.  
  184. section .bss
  185. align 4
  186. kernel_stack_bottom: equ $ ;equ the current address for the stack
  187. resb 16384 ;Use 16kb for stack size
  188. kernel_stack_top: ;Top of the stack
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement