boot.asm 4.1 KB

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