boot.asm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_sb
  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. ; load the first block (4096 bytes)
  68. read_sb:
  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. jc read_failed
  78. using_lba:
  79. ; put message on screen
  80. mov si, lamsg
  81. call print
  82. ; the first sector (LBA address starts at 0) read,
  83. ; so we read start at address 1
  84. ; and we should add address
  85. mov ax, 0x07e0
  86. mov es, ax
  87. mov ax, 1
  88. .read_loop:
  89. mov bx, es
  90. add bx, 512 / 16
  91. mov es, bx
  92. inc ax
  93. mov cl, 1
  94. xor bx, bx
  95. call read_lba
  96. ; 8 sectors = 4KB
  97. cmp ax, 8
  98. jb .read_loop
  99. ret
  100. read_failed:
  101. mov si, read_fail_msg
  102. call print
  103. .die: hlt
  104. jmp .die
  105. read_lba:
  106. pushaw
  107. push dword 0x0000
  108. push dword eax
  109. ; buffer address
  110. push word es
  111. push word bx
  112. ; sectors count
  113. push word cx
  114. ; package size
  115. push word 16
  116. mov ah, 0x42
  117. mov dl, [BOOTDRIVER]
  118. mov si, sp
  119. int 0x13
  120. add sp, 16
  121. popaw
  122. ret
  123. boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
  124. read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
  125. lamsg: db "LBA mode is avaliable. ", 0x0d, 0x0a, 0x00
  126. e820_fail_msg: db "panic: read E820 memory info failed. ", 0x0d, 0x0a, 0x00
  127. times 510 - ($ - $$) db 0x00
  128. db 0x55, 0xaa