loader.asm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 0x7e60
  14. %include "boot/paging.asm"
  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. ; put a message on screen
  28. mov di, msg
  29. call print
  30. call load_ksetup
  31. .die: jmp .die
  32. .set_vga:
  33. ; get mode info
  34. mov ax, 0x4f01
  35. mov cx, 0x4118
  36. mov di, 0x7000
  37. int 0x10
  38. die: jc die
  39. mov al,[es:di]
  40. mov [VMODE], al
  41. mov ax, [es:di + 18]
  42. mov [SCREENX], ax
  43. mov ax, [es:di + 20]
  44. mov [SCREENY], ax
  45. mov eax, [es:di + 40]
  46. mov [FB_ADDR], eax
  47. mov ax, 0x4f02
  48. mov bx, 0x4118
  49. int 0x10
  50. .open_a20:
  51. ; disable interrupts before we initial done (enable in kernel)
  52. ; A20 line uses to control memory higher than 1MB. we can't access mem > 1MB when it's closing.
  53. ; So we must open it before switching to protect-mode.
  54. cli
  55. in al, 0x92
  56. or al, 0x02
  57. out 0x92, al
  58. .to_prot:
  59. ; switch to protect-mode.
  60. lgdt [gdt32_desc]
  61. ; CR0 (control register 0) uses to control CPU. It's first bit PE controls Protect-mode Enable.
  62. mov eax, cr0
  63. or eax, 1
  64. mov cr0, eax
  65. ; CPU is in protect-mode now, but segment registers didn't initialze.
  66. ; we cannot 'mov cs, xxx'
  67. jmp dword code32:init_prot
  68. print:
  69. pushaw
  70. ; message in register di, but lodsb uses si.
  71. mov si, di
  72. .loop:
  73. cld
  74. lodsb
  75. ; zero? -> return
  76. test al, al
  77. jz .ret
  78. mov ah, 0x0e
  79. mov bx, 0x000f
  80. int 0x10
  81. jmp .loop
  82. .ret:
  83. popaw
  84. ret
  85. MAGIC equ 0
  86. NINODES equ 4
  87. NBLOCKS equ 8
  88. NB_BBM equ 12
  89. NB_IBM equ 16
  90. BBM_BN equ 20
  91. IBM_BN equ 24
  92. NB_BIM equ 28
  93. NB_INODES equ 32
  94. BIM_BN equ 36
  95. INODES_BN equ 40
  96. ROOT_INO equ 44
  97. INODE_SIZE equ 32
  98. FIRSTDATABLOCK equ 76
  99. NEXT_INODE equ 80
  100. ZERO equ 84
  101. DENTRY_NAME equ 4
  102. load_ksetup:
  103. ; find 'ksetup' in the root-directory, and then read it to 0x8000
  104. ; get the inode of 'ksetup', then we can find in bim.
  105. ; the first block of root-inode must be first-data-block
  106. mov eax, [0x7e00 + FIRSTDATABLOCK]
  107. ; the fs buffer
  108. mov bx, 0x5000
  109. call read_block
  110. ; and start find in this directory
  111. mov si, 0x5000 + DENTRY_NAME
  112. mov di, fname
  113. mov cx, 6
  114. .cmp_loop:
  115. repe
  116. cmpsb
  117. jz found
  118. ; not found, continue
  119. add si, 128
  120. cmp si, 0x6000
  121. je not_found
  122. jmp .cmp_loop
  123. found:
  124. mov di, found_msg
  125. call print
  126. .die: hlt
  127. jmp .die
  128. not_found:
  129. mov di, not_found_msg
  130. call print
  131. .die: hlt
  132. jmp .die
  133. read_block:
  134. push ax
  135. push cx
  136. ; 8 sectors = 4 KB = 1 block
  137. mov cx, 8
  138. shl eax, 3
  139. .loop:
  140. call read_lba
  141. jc read_failed
  142. inc eax
  143. dec cx
  144. add bx, 512
  145. cmp cx, 0
  146. jne .loop
  147. pop cx
  148. pop ax
  149. ret
  150. read_lba:
  151. pushaw
  152. push dword 0x0000
  153. push dword eax
  154. ; buffer address
  155. push word es
  156. push word bx
  157. ; sectors count
  158. push word cx
  159. ; package size
  160. push word 16
  161. mov ah, 0x42
  162. mov dl, [BOOTDRIVER]
  163. mov si, sp
  164. int 0x13
  165. add sp, 16
  166. popaw
  167. ret
  168. read_failed:
  169. mov di, read_failed_msg
  170. call print
  171. .die: hlt
  172. jmp die
  173. bits 32
  174. init_prot:
  175. ; we set segment selectors for segment register (CS was set by far-jmp)
  176. mov ax, data32
  177. mov ds, ax
  178. mov es, ax
  179. mov fs, ax
  180. mov gs, ax
  181. mov ss, ax
  182. mov sp, 0x00007c00
  183. ; now we switch to long-mode
  184. .to_long_mode:
  185. ; clear page map 0 - 0x4000 (0x3000-0x4000 maybe not used)
  186. xor edi, edi
  187. ; mov edi, 0x00000000
  188. xor eax, eax
  189. ; store for 4096 times
  190. mov ecx, 4096
  191. cld
  192. rep stosd
  193. .init_paging:
  194. ; we set page map at 0x00000000 (a good address)
  195. mov dword [0x00000000], 0x00001000 | PAGE_USER
  196. ; mov dword [0x00000004], 0x00000000
  197. mov dword [0x00000800], 0x00001000 | PAGE_USER
  198. ; mov dword [0x00000804], 0x00000000
  199. ; and page directory pointer table
  200. mov dword [0x00001000], 0x00002000 | PAGE_USER
  201. ; mov dword [0x00001004], 0x00000000
  202. ; page directory entry (2 MB)
  203. mov dword [0x00002000], 0x00000000 | PAGE_KERNEL
  204. ; mov dword [0x00002004], 0x00000000
  205. mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
  206. ; mov dword [0x0000200c], 0x00000000
  207. ; get frame buffer address and map it here.
  208. mov eax, [0x7c06]
  209. ; eax &= PAGE_SIZE
  210. and eax, ~(1048576 * 2 - 1)
  211. or eax, PAGE_KERNEL
  212. mov dword [0x00002010], eax
  213. ; mov dword [0x00002014], 0x00000000
  214. add eax, 1048576 * 2
  215. mov dword [0x00002018], eax
  216. add eax, 1048576 * 2
  217. mov dword [0x00002020], eax
  218. .enable_long_mode:
  219. lgdt [gdt64_desc]
  220. mov ax, data64
  221. mov ds, ax
  222. mov es, ax
  223. mov fs, ax
  224. mov gs, ax
  225. mov ss, ax
  226. ; open PAE (page map level 3)
  227. mov eax, cr4
  228. bts eax, 5
  229. mov cr4, eax
  230. ; set Page Table Base Register
  231. ; mov eax, 0x00000000
  232. xor eax, eax
  233. mov cr3, eax
  234. ; enable long mode (must before paging)
  235. mov ecx, 0xc0000080
  236. rdmsr
  237. bts eax, 8
  238. wrmsr
  239. ; enable paging
  240. mov eax, cr0
  241. ; PE again
  242. or eax, 1
  243. bts eax, 31
  244. mov cr0, eax
  245. ; okay! now set segment registers and init long mode
  246. jmp code64:0x8000
  247. msg: db "welcome to XNIX Loader. ", 0x0d, 0x0a, 0x00
  248. read_failed_msg: db "fatal: read disk failed. ", 0x0d, 0x0a, 0x00
  249. fname: db "ksetup", 0x00
  250. found_msg: db "found ksetup in the root. ", 0x0d, 0x0a, 0x00
  251. not_found_msg: db "ksetup not found. ", 0x0d, 0x0a, 0x00
  252. alignb 16
  253. gdt32:
  254. dd 0x00000000, 0x00000000
  255. gdt32_code:
  256. dd 0x0000ffff, 0x00cf9a00
  257. gdt32_data:
  258. dd 0x0000ffff, 0x00cf9200
  259. gdt32_len equ $ - gdt32
  260. gdt32_desc:
  261. dw gdt32_len - 1
  262. dd gdt32
  263. code32 equ gdt32_code - gdt32
  264. data32 equ gdt32_data - gdt32
  265. gdt64:
  266. dq 0x0000000000000000
  267. gdt64_code:
  268. dq 0x0020980000000000
  269. gdt64_data:
  270. dq 0x0000920000000000
  271. gdt64_len equ $ - gdt64
  272. gdt64_desc:
  273. dw gdt64_len - 1
  274. dd gdt64
  275. code64 equ gdt64_code - gdt64
  276. data64 equ gdt64_data - gdt64