boot.asm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. ; get e820 memory
  34. xor ebx, ebx
  35. ; yeah, we used it in .set_vga.
  36. ; and I think this is a good place (no one used it)
  37. xor ax, ax
  38. mov es, ax
  39. mov di, 0x7000
  40. .get_loop:
  41. mov eax, 0x0000e820
  42. mov ecx, 20
  43. mov edx, 0x534d4150
  44. int 0x15
  45. jc .get_failed
  46. add di, 20
  47. cmp ebx, 0
  48. jne .get_loop
  49. ; jump to loader, 3488 bytes max
  50. jmp 0x0000:0x7e60
  51. .get_failed:
  52. mov si, e820_fail_msg
  53. call print
  54. .die: hlt
  55. jmp .die
  56. print:
  57. cld
  58. lodsb
  59. test al,al
  60. jz .ret
  61. mov ah, 0x0e
  62. mov bx, 0x0f
  63. int 0x10
  64. jmp print
  65. .ret:
  66. ret
  67. ; NOTE: QEMU's HD only supports LBA mode. So this MBR can't load it successfully. :-)
  68. read_it:
  69. ; we try LBA first because some disks supports wrong CHS. (bad sector per track)
  70. ; if this boot disk doesn't support LBA mode, it should be a floppy.
  71. mov ax, 0x07e0
  72. mov es, ax
  73. mov ax, 1
  74. mov cl, 1
  75. xor bx, bx
  76. call read_lba
  77. jnc using_lba
  78. ; so we use CHS to read now.
  79. mov ax, 0x07e0
  80. mov es, ax
  81. xor ax, ax
  82. .read_it:
  83. xor bx, bx
  84. inc ax
  85. mov cl, 1
  86. push ax
  87. push bx
  88. call read_chs
  89. pop bx
  90. pop ax
  91. mov bx, es
  92. add bx, 512 / 16
  93. mov es, bx
  94. cmp ax, 1024
  95. jb .read_it
  96. read_chs:
  97. pushaw
  98. ; LBA address in AX, and sectors count in CL, buffer in ES:BX
  99. ; LBA to CHS:
  100. ; TEMP = LBA / SECTORS_PER_TRACK
  101. ; SECTOR = (LBA % SECTORS_PER_TRACK) + 1
  102. ; CYLINDER = TEMP >> 1
  103. ; HEAD = TEMP & 1
  104. ; DIV: unsigned divide
  105. ; 12345 / 123:
  106. ; mov ax, 12345 : dividend
  107. ; mov bl, 123 : divisor
  108. ; AL: quotient
  109. ; AH: remainder
  110. ; 18: sectors per track for 1.44M floppy (and for my u-disk)
  111. ; save bx first
  112. push bx
  113. mov bl, 18
  114. div bl
  115. ; BIOS standard interrupt: 0x13 AH = 0x02: read disk in CHS mode
  116. ; AL: sectors count
  117. ; CH: cylinder number low
  118. ; CL: 0-5b: sector number, 6-7b: cylinder number (disk only)
  119. ; DH: head
  120. ; DL: driver
  121. ; ES:BX: buffer
  122. ; SECTOR = AH + 1
  123. ; but CL used to save sectors count, so we save it first.
  124. push cx
  125. ; we inc ah because it's shorter than inc cl
  126. inc ah
  127. mov cl, ah
  128. ; CH: CYLINDER = (LBA / SECTORS_PER_TRACK) >> 1
  129. mov dh, al
  130. and dh, 1
  131. shr al, 1
  132. mov ch, al
  133. ; for 'push cx'
  134. pop ax
  135. ; for 'push bx'
  136. pop bx
  137. mov dl, [BOOTDRIVER]
  138. ; okay, now we read it
  139. mov ah, 2
  140. int 0x13
  141. jc read_failed
  142. popaw
  143. ret
  144. using_lba:
  145. ; put message on screen
  146. mov si, lamsg
  147. call print
  148. ; the first sector (LBA address starts at 0) read,
  149. ; so we read start at address 1
  150. ; and we should add address
  151. mov ax, 0x07e0
  152. mov es, ax
  153. mov ax, 1
  154. .read_loop:
  155. mov bx, es
  156. add bx, 512 / 16
  157. mov es, bx
  158. inc ax
  159. mov cl, 1
  160. xor bx, bx
  161. call read_lba
  162. ; 1024 sectors = 512 KB,
  163. ; I think this is enough anytime for our kernel.
  164. cmp ax, 1024
  165. jb .read_loop
  166. ret
  167. read_failed:
  168. mov si, read_fail_msg
  169. call print
  170. .die: hlt
  171. jmp .die
  172. read_lba:
  173. pushaw
  174. push dword 0x0000
  175. push dword eax
  176. ; buffer address
  177. push word es
  178. push word bx
  179. ; sectors count
  180. push word cx
  181. ; package size
  182. push word 16
  183. mov ah, 0x42
  184. mov dl, [BOOTDRIVER]
  185. mov si, sp
  186. int 0x13
  187. add sp, 16
  188. popaw
  189. ret
  190. boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
  191. read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
  192. lamsg: db "LBA mode is avaliable. ", 0x0d, 0x0a, 0x00
  193. e820_fail_msg: db "panic: read E820 memory info failed. ", 0x0d, 0x0a, 0x00
  194. times 510 - ($ - $$) db 0x00
  195. db 0x55, 0xaa