boot.asm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. ; Copyright (C) 2021 Rain
  2. ; This file is part of Cunix.
  3. ; Cunix 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. ; Cunix 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 Cunix. If not, see <https://www.gnu.org/licenses/>.
  13. CYLINDERS_NEED equ 10
  14. org 0x7c00
  15. align 8
  16. _start:
  17. xor ax, ax
  18. mov ds, ax
  19. mov es, ax
  20. mov ss, ax
  21. mov sp, 0x7c00
  22. ; display message: `Cunix is booting...`
  23. ; boot device driver number is in DL (by BIOS-19)
  24. mov [BI_BOOTDEV], dl
  25. call get_e820
  26. call read_it
  27. call set_vbe_mode
  28. jmp 0x0000:0x8000
  29. diskpanic_message: db "read disk failed", 0x0d, 0x0a, 0x00
  30. e820panic_message: db "get E820 info failed", 0x0d, 0x0a, 0x00
  31. panic_message: db "boot panic: ", 0x00
  32. %include "arch/x86/readdisk.inc"
  33. print:
  34. ; SI: message address
  35. mov al, [si]
  36. add si, 1
  37. cmp al, 0x00
  38. je .ret
  39. mov ah, 0x0e
  40. mov bx, 0x0f
  41. int 0x10
  42. jmp print
  43. .ret:
  44. ret
  45. get_e820:
  46. xor ebx, ebx
  47. xor ax, ax
  48. mov es, ax
  49. ; address of E820 buffer
  50. mov di, 0x6000
  51. .get_loop:
  52. mov eax, 0x0000e820
  53. mov ecx, 20
  54. mov edx, 0x534d4150
  55. int 0x15
  56. jc .get_failed
  57. add di, 20
  58. cmp ebx, 0
  59. jne .get_loop
  60. ret
  61. .get_failed:
  62. mov di, e820panic_message
  63. call panic
  64. read_it:
  65. ; segment address of loader+kernel
  66. mov ax, 0x0800
  67. mov es, ax
  68. ; cylinder 0
  69. mov ch, 0
  70. ; header 0
  71. mov dh, 0
  72. ; start sector 2
  73. mov cl, 2
  74. .read_loop:
  75. ; I know it use to save source-address, but there isn't another
  76. ; register free.
  77. ; error counts
  78. mov si, 0
  79. .retry:
  80. mov ah, 0x02
  81. ; one sector
  82. mov al, 1
  83. ; offset 0x0000
  84. mov bx, 0
  85. ; boot device
  86. mov dl, [BI_BOOTDEV]
  87. ; call -> BIOS
  88. int 0x13
  89. ; if not error
  90. jnc .cont_read
  91. ; okay, something wrong. best way is reset it.
  92. .error:
  93. add si, 1
  94. ; we try it for 5 times
  95. mov di, diskpanic_message
  96. cmp si, 5
  97. jae panic
  98. .reset_disk:
  99. mov ah, 0x00
  100. mov dl, [BI_BOOTDEV]
  101. int 0x13
  102. ; and retry
  103. jmp .retry
  104. .cont_read:
  105. ; add for 0x0020 (not 0x0200)
  106. mov ax, es
  107. add ax, 0x0020
  108. mov es, ax
  109. ; we've been read one sector
  110. add cl, 1
  111. ; if read done
  112. SECTORS_1_TRACK equ 18
  113. cmp cl, SECTORS_1_TRACK
  114. jbe .read_loop
  115. .update_track:
  116. mov cl, 1
  117. add dh, 1
  118. HEADS_1_CYLINDER equ 2
  119. cmp dh, HEADS_1_CYLINDER
  120. jb .read_loop
  121. .update_head:
  122. mov dh, 0
  123. add ch, 1
  124. cmp ch, CYLINDERS_NEED
  125. jb .read_loop
  126. ret
  127. set_vbe_mode:
  128. mov bx, 0x10c
  129. .set_repeat:
  130. mov ax, 0x4f02
  131. ; we can use mode 0x108-0x10c, and save it
  132. push bx
  133. int 0x10
  134. ; failed?
  135. cmp ax, 0x4f
  136. je .ret
  137. .set_retry:
  138. pop bx
  139. ; bx--
  140. dec bx
  141. cmp bx, 0x107
  142. ja .set_repeat
  143. ; set failed, use default
  144. ret
  145. .ret:
  146. pop bx
  147. ret
  148. panic:
  149. mov si, panic_message
  150. call print
  151. mov si, di
  152. call print
  153. die: hlt
  154. jmp die
  155. times 510 - ($ - $$) db 0x00
  156. db 0x55, 0xaa