boot.asm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. CURSORX equ 0x7c0a
  22. CURSORY equ 0x7c0b
  23. COM0 equ 0x7c0c
  24. COM1 equ 0x7c0e
  25. COM2 equ 0x7c10
  26. COM3 equ 0x7c12
  27. _start:
  28. xor ax, ax
  29. mov ds, ax
  30. mov es, ax
  31. mov ss, ax
  32. mov sp, 0x7c00
  33. ; save startup driver
  34. mov [BOOTDRIVER], dl
  35. ; display message 'Loading System'
  36. mov si, boot_msg
  37. call print
  38. call read_sb
  39. ; get e820 memory
  40. xor ebx, ebx
  41. ; yeah, we used it in .set_vga.
  42. ; and I think this is a good place (no one used it)
  43. xor ax, ax
  44. mov es, ax
  45. mov di, 0x7000
  46. .get_loop:
  47. mov eax, 0x0000e820
  48. mov ecx, 20
  49. mov edx, 0x534d4150
  50. int 0x15
  51. jc .get_failed
  52. add di, 20
  53. cmp ebx, 0
  54. jne .get_loop
  55. .serial_init:
  56. ; get serial ports, and save it to bootarg
  57. mov ax, [0x400]
  58. mov [COM0], ax
  59. mov ax, [0x402]
  60. mov [COM1], ax
  61. mov ax, [0x404]
  62. mov [COM2], ax
  63. mov ax, [0x406]
  64. mov [COM3], ax
  65. ; jump to loader, 3488 bytes max
  66. jmp 0x0000:0x7e60
  67. .get_failed:
  68. mov si, e820_fail_msg
  69. call print
  70. .die: hlt
  71. jmp .die
  72. print:
  73. cld
  74. lodsb
  75. test al,al
  76. jz .ret
  77. mov ah, 0x0e
  78. mov bx, 0x0f
  79. int 0x10
  80. jmp print
  81. .ret:
  82. ret
  83. ; load the first block (4096 bytes)
  84. read_sb:
  85. ; we try LBA first because some disks supports wrong CHS. (bad sector per track)
  86. ; if this boot disk doesn't support LBA mode, it should be a floppy.
  87. mov ax, 0x07e0
  88. mov es, ax
  89. mov ax, 1
  90. mov cl, 1
  91. xor bx, bx
  92. call read_lba
  93. jc read_failed
  94. using_lba:
  95. ; put message on screen
  96. mov si, lamsg
  97. call print
  98. ; the first sector (LBA address starts at 0) read,
  99. ; so we read start at address 1
  100. ; and we should add address
  101. mov ax, 0x07e0
  102. mov es, ax
  103. mov ax, 1
  104. .read_loop:
  105. mov bx, es
  106. add bx, 512 / 16
  107. mov es, bx
  108. inc ax
  109. mov cl, 1
  110. xor bx, bx
  111. call read_lba
  112. ; 8 sectors = 4KB
  113. cmp ax, 8
  114. jb .read_loop
  115. ret
  116. read_failed:
  117. mov si, read_fail_msg
  118. call print
  119. .die: hlt
  120. jmp .die
  121. read_lba:
  122. pushaw
  123. push dword 0x0000
  124. push dword eax
  125. ; buffer address
  126. push word es
  127. push word bx
  128. ; sectors count
  129. push word cx
  130. ; package size
  131. push word 16
  132. mov ah, 0x42
  133. mov dl, [BOOTDRIVER]
  134. mov si, sp
  135. int 0x13
  136. add sp, 16
  137. popaw
  138. ret
  139. boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
  140. read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
  141. lamsg: db "LBA mode is avaliable. ", 0x0d, 0x0a, 0x00
  142. e820_fail_msg: db "panic: read E820 memory info failed. ", 0x0d, 0x0a, 0x00
  143. times 510 - ($ - $$) db 0x00
  144. db 0x55, 0xaa