boot.asm 3.8 KB

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