boot.asm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. ; Copyright (C) 2021 Rain
  2. ; This file is part of XNIX.
  3. ; XNIX is free software: you can redistribute it and/or modify
  4. ; it under the terms of the GNU General Public License as published by
  5. ; the Free Software Foundation, either version 3 of the License, or
  6. ; (at your option) and later version.
  7. ; XNIX is distributed in the hope that it will be useful,
  8. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ; GNU General Public License for more details.
  11. ; You should have received a copy of the GNU General Public License
  12. ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  13. org 0x7c00
  14. cyldsneed equ 10
  15. ; yeah, it's really 0x7c00 because I don't want to use other memory.
  16. BOOTDRIVER equ 0x7c00
  17. VMODE equ 0x7c01
  18. SCREENX equ 0x7c02
  19. SCREENY equ 0x7c04
  20. FB_ADDR equ 0x7c06
  21. _start:
  22. xor ax, ax
  23. mov ds, ax
  24. mov es, ax
  25. mov ss, ax
  26. mov sp, 0x7c00
  27. ; save startup driver
  28. mov [BOOTDRIVER], dl
  29. ; display message 'Loading System'
  30. mov si, boot_msg
  31. call print
  32. call read_it
  33. .set_vga:
  34. ; get mode info
  35. mov ax, 0x4f01
  36. mov cx, 0x4118
  37. mov di, 0x7000
  38. int 0x10
  39. die: jc die
  40. mov al,[es:di]
  41. mov [VMODE], al
  42. mov ax, [es:di + 18]
  43. mov [SCREENX], ax
  44. mov ax, [es:di + 20]
  45. mov [SCREENY], ax
  46. mov eax, [es:di + 40]
  47. mov [FB_ADDR], eax
  48. mov ax, 0x4f02
  49. mov bx, 0x4118
  50. int 0x10
  51. ; get e820 memory
  52. xor ebx, ebx
  53. ; yeah, we used it in .set_vga.
  54. ; and I think this is a good place (no one used it)
  55. xor ax, ax
  56. mov es, ax
  57. mov di, 0x7000
  58. .get_loop:
  59. mov eax, 0x0000e820
  60. mov ecx, 20
  61. mov edx, 0x534d4150
  62. int 0x15
  63. jc .get_failed
  64. add di, 20
  65. cmp ebx, 0
  66. jne .get_loop
  67. jmp 0x0000:0x7e00
  68. .get_failed:
  69. mov si, e820_fail_msg
  70. call print
  71. .die: hlt
  72. jmp .die
  73. print:
  74. cld
  75. lodsb
  76. test al,al
  77. jz .ret
  78. mov ah, 0x0e
  79. mov bx, 0x0f
  80. int 0x10
  81. jmp print
  82. .ret:
  83. ret
  84. ; NOTE: QEMU's HD only supports LBA mode. So this MBR can't load it successfully. :-)
  85. read_it:
  86. ; we try LBA first because some disks supports wrong CHS. (bad sector per track)
  87. ; if this boot disk doesn't support LBA mode, it should be a floppy.
  88. mov ax, 0x07e0
  89. mov es, ax
  90. mov ax, 1
  91. mov cl, 1
  92. xor bx, bx
  93. call read_lba
  94. jnc using_lba
  95. ; so we use CHS to read now.
  96. mov ax, 0x07e0
  97. mov es, ax
  98. xor ax, ax
  99. .read_it:
  100. xor bx, bx
  101. inc ax
  102. mov cl, 1
  103. push ax
  104. push bx
  105. call read_chs
  106. pop bx
  107. pop ax
  108. mov bx, es
  109. add bx, 512 / 16
  110. mov es, bx
  111. cmp ax, 1024
  112. jb .read_it
  113. read_chs:
  114. pushaw
  115. ; LBA address in AX, and sectors count in CL, buffer in ES:BX
  116. ; LBA to CHS:
  117. ; TEMP = LBA / SECTORS_PER_TRACK
  118. ; SECTOR = (LBA % SECTORS_PER_TRACK) + 1
  119. ; CYLINDER = TEMP >> 1
  120. ; HEAD = TEMP & 1
  121. ; DIV: unsigned divide
  122. ; 12345 / 123:
  123. ; mov ax, 12345 : dividend
  124. ; mov bl, 123 : divisor
  125. ; AL: quotient
  126. ; AH: remainder
  127. ; 18: sectors per track for 1.44M floppy (and for my u-disk)
  128. ; save bx first
  129. push bx
  130. mov bl, 18
  131. div bl
  132. ; BIOS standard interrupt: 0x13 AH = 0x02: read disk in CHS mode
  133. ; AL: sectors count
  134. ; CH: cylinder number low
  135. ; CL: 0-5b: sector number, 6-7b: cylinder number (disk only)
  136. ; DH: head
  137. ; DL: driver
  138. ; ES:BX: buffer
  139. ; SECTOR = AH + 1
  140. ; but CL used to save sectors count, so we save it first.
  141. push cx
  142. ; we inc ah because it's shorter than inc cl
  143. inc ah
  144. mov cl, ah
  145. ; CH: CYLINDER = (LBA / SECTORS_PER_TRACK) >> 1
  146. mov dh, al
  147. and dh, 1
  148. shr al, 1
  149. mov ch, al
  150. ; for 'push cx'
  151. pop ax
  152. ; for 'push bx'
  153. pop bx
  154. mov dl, [BOOTDRIVER]
  155. ; okay, now we read it
  156. mov ah, 2
  157. int 0x13
  158. jc read_failed
  159. popaw
  160. ret
  161. using_lba:
  162. ; put message on screen
  163. mov si, lamsg
  164. call print
  165. ; the first sector (LBA address starts at 0) read,
  166. ; so we read start at address 1
  167. ; and we should add address
  168. mov ax, 0x07e0
  169. mov es, ax
  170. mov ax, 1
  171. .read_loop:
  172. mov bx, es
  173. add bx, 512 / 16
  174. mov es, bx
  175. inc ax
  176. mov cl, 1
  177. xor bx, bx
  178. call read_lba
  179. ; 1024 sectors = 512 KB,
  180. ; I think this is enough anytime for our kernel.
  181. cmp ax, 1024
  182. jb .read_loop
  183. ret
  184. read_failed:
  185. mov si, read_fail_msg
  186. call print
  187. .die: hlt
  188. jmp .die
  189. read_lba:
  190. pushaw
  191. push dword 0x0000
  192. push dword eax
  193. ; buffer address
  194. push word es
  195. push word bx
  196. ; sectors count
  197. push word cx
  198. ; package size
  199. push word 16
  200. mov ah, 0x42
  201. mov dl, [BOOTDRIVER]
  202. mov si, sp
  203. int 0x13
  204. add sp, 16
  205. popaw
  206. ret
  207. boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
  208. read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
  209. lamsg: db "LBA mode is avaliable. ", 0x0d, 0x0a, 0x00
  210. e820_fail_msg: db "panic: read E820 memory info failed. ", 0x0d, 0x0a, 0x00
  211. times 510 - ($ - $$) db 0x00
  212. db 0x55, 0xaa