loader.asm 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. CURSORX equ 0x7c0a
  22. CURSORY equ 0x7c0b
  23. _start:
  24. xor ax, ax
  25. mov ds, ax
  26. mov es, ax
  27. mov ss, ax
  28. mov sp, 0x7c00
  29. ; put a message on screen
  30. mov di, msg
  31. call print
  32. call load_ksetup
  33. .set_vga:
  34. ; well, now we use the VGA text mode, so...
  35. mov byte [VMODE], 0x00
  36. mov word [SCREENX], 80
  37. mov word [SCREENY], 25
  38. mov dword [FB_ADDR], 0xb8000
  39. ; we can't put any messages after this, because kernel will
  40. ; use this to put messages.
  41. ; if we put msgs again, so must get cursor position again.
  42. .get_cursor:
  43. mov ah, 3
  44. ; page number
  45. xor bh, bh
  46. int 0x10
  47. ; dh: y dl: x
  48. mov [CURSORX], dl
  49. mov [CURSORY], dh
  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. jae load_failed
  122. jmp .cmp_loop
  123. found:
  124. mov di, found_msg
  125. call print
  126. .get_inode:
  127. ; store its inode number in te eax
  128. ; 6: the length of file name
  129. mov ecx, [si - 6 - DENTRY_NAME]
  130. ; and then read this inode (only read the first block of inodes)
  131. mov eax, [0x7e00 + INODES_BN]
  132. mov bx, 0x5000
  133. call read_block
  134. ; back up the inode nr, for .search_bim
  135. mov edx, ecx
  136. ; * sizeof(struct inode)
  137. shl cx, 5
  138. mov si, 0x5000
  139. add si, cx
  140. ; and get its size
  141. INO_INODE equ 0
  142. INO_PINODE equ 4
  143. INO_ACCESS equ 8
  144. INO_BLOCKSNR equ 9
  145. INO_UPSIZE equ 13
  146. INO_CTIME equ 17
  147. INO_WTIME equ 21
  148. INO_NLINKS equ 25
  149. INO_UID equ 26
  150. INO_GID equ 28
  151. INO_EACCESS equ 30
  152. mov eax, [si + INO_BLOCKSNR]
  153. cmp dword [si + INO_UPSIZE], 0
  154. ; upsize is zero, so eax is blocks we need.
  155. je .read_ksetup
  156. ; okay, this is enough
  157. inc eax
  158. .read_ksetup:
  159. ; find in the bim (only first block, too)
  160. ; first read the first block of bim to 0x5000
  161. ; blocks need is in the eax, save it
  162. BIM_INODE equ 0
  163. BIM_THBLOCK equ 4
  164. mov ecx, eax
  165. mov eax, [0x7e00 + BIM_BN]
  166. mov bx, 0x5000
  167. call read_block
  168. ; and start search
  169. mov si, 0x5000
  170. mov eax, ecx
  171. xor ecx, ecx
  172. ; the to address
  173. ; and this is the address of kernel
  174. xor bx, bx
  175. push eax
  176. mov ax, 0x0900
  177. mov es, ax
  178. pop eax
  179. .search_bim:
  180. cmp ecx, eax
  181. ; ecx is the blocks we loaded (also bim->th_block), eax is the blocks of this file
  182. je .load_done
  183. ; the inode of this file is edx
  184. cmp edx, [si]
  185. ; not match, search again
  186. jne .search_again
  187. cmp ecx, [si + BIM_THBLOCK]
  188. jne .search_again
  189. .load_file:
  190. ; only one block, and coutinue search
  191. ; the block is the index of si (base is 0x5000), [si] is the inode
  192. ; not this!!! loader must uses 32-bit registers, so we should save/restore
  193. ; them in 32-bits
  194. ; pushaw
  195. push eax
  196. push ecx
  197. push edx
  198. push ebx
  199. push esp
  200. push ebp
  201. push esi
  202. push edi
  203. ; first get the block nr
  204. xor eax, eax
  205. mov ax, si
  206. ; - base
  207. sub eax, 0x5000
  208. ; / 8, get the index
  209. shr eax, 3
  210. ; and += first_data_block, because bim start at this.
  211. add eax, [0x7e00 + FIRSTDATABLOCK]
  212. ; now eax is the real block nr of this file
  213. ; read it!
  214. ; bx is the address, we don't need change
  215. call read_block
  216. pop edi
  217. pop esi
  218. pop ebp
  219. pop esp
  220. pop ebx
  221. pop edx
  222. pop ecx
  223. pop eax
  224. add bx, 4096
  225. inc ecx
  226. add si, 8
  227. jmp .search_bim
  228. .search_again:
  229. ; the size of a bim entry
  230. add si, 8
  231. cmp si, 0x6000
  232. jae load_failed
  233. jmp .search_bim
  234. .load_done:
  235. mov di, load_done_msg
  236. call print
  237. ret
  238. load_failed:
  239. mov di, load_failed_msg
  240. call print
  241. die: hlt
  242. jmp die
  243. read_block:
  244. push ax
  245. push cx
  246. ; 8 sectors = 4 KB = 1 block
  247. mov cx, 8
  248. shl eax, 3
  249. .loop:
  250. call read_lba
  251. inc eax
  252. dec cx
  253. add bx, 512
  254. cmp bx, 0
  255. je .go_on_es
  256. .cont:
  257. cmp cx, 0
  258. jne .loop
  259. pop cx
  260. pop ax
  261. ret
  262. .go_on_es:
  263. ; update the es, and continue
  264. push ax
  265. mov ax, es
  266. add ax, 512 / 16
  267. mov es, ax
  268. pop ax
  269. jmp .cont
  270. read_lba:
  271. push eax
  272. push ecx
  273. push edx
  274. push ebx
  275. push esp
  276. push ebp
  277. push esi
  278. push edi
  279. push dword 0x0000
  280. push dword eax
  281. ; buffer address
  282. push word es
  283. push word bx
  284. ; sectors count
  285. push word cx
  286. ; package size
  287. push word 16
  288. mov ah, 0x42
  289. mov dl, [BOOTDRIVER]
  290. mov si, sp
  291. int 0x13
  292. add sp, 16
  293. pop edi
  294. pop esi
  295. pop ebp
  296. pop esp
  297. pop ebx
  298. pop edx
  299. pop ecx
  300. pop eax
  301. jc read_failed
  302. ret
  303. read_failed:
  304. mov di, read_failed_msg
  305. call print
  306. jmp die
  307. bits 32
  308. init_prot:
  309. ; we set segment selectors for segment register (CS was set by far-jmp)
  310. mov ax, data32
  311. mov ds, ax
  312. mov es, ax
  313. mov fs, ax
  314. mov gs, ax
  315. mov ss, ax
  316. mov sp, 0x00007c00
  317. ; now we switch to long-mode
  318. .to_long_mode:
  319. ; clear page map 0 - 0x4000 (0x3000-0x4000 maybe not used)
  320. xor edi, edi
  321. ; mov edi, 0x00000000
  322. xor eax, eax
  323. ; store for 4096 times
  324. mov ecx, 4096
  325. cld
  326. rep stosd
  327. .init_paging:
  328. ; we set page map at 0x00000000 (a good address)
  329. mov dword [0x00000000], 0x00001000 | PAGE_DIR
  330. ; mov dword [0x00000004], 0x00000000
  331. mov dword [0x00000800], 0x00001000 | PAGE_DIR
  332. ; mov dword [0x00000804], 0x00000000
  333. ; and page directory pointer table
  334. mov dword [0x00001000], 0x00002000 | PAGE_DIR
  335. ; mov dword [0x00001004], 0x00000000
  336. ; page directory entry (2 MB)
  337. mov dword [0x00002000], 0x00000000 | PAGE_KERNEL
  338. ; mov dword [0x00002004], 0x00000000
  339. mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
  340. ; mov dword [0x0000200c], 0x00000000
  341. .enable_long_mode:
  342. lgdt [gdt64_desc]
  343. mov ax, data64
  344. mov ds, ax
  345. mov es, ax
  346. mov fs, ax
  347. mov gs, ax
  348. mov ss, ax
  349. ; open PAE (page map level 3)
  350. mov eax, cr4
  351. bts eax, 5
  352. mov cr4, eax
  353. ; set Page Table Base Register
  354. ; mov eax, 0x00000000
  355. xor eax, eax
  356. mov cr3, eax
  357. ; enable long mode (must before paging)
  358. mov ecx, 0xc0000080
  359. rdmsr
  360. bts eax, 8
  361. wrmsr
  362. ; enable paging
  363. mov eax, cr0
  364. ; PE again
  365. or eax, 1
  366. bts eax, 31
  367. mov cr0, eax
  368. ; okay! now set segment registers and init long mode
  369. jmp code64:0x9000
  370. msg: db "welcome to XNIX Loader. ", 0x0d, 0x0a, 0x00
  371. read_failed_msg: db "fatal: read disk failed. ", 0x0d, 0x0a, 0x00
  372. fname: db "ksetup", 0x00
  373. found_msg: db "found ksetup in the root. ", 0x0d, 0x0a, 0x00
  374. load_failed_msg: db "failed when trying to load ksetup. ", 0x0d, 0x0a, 0x00
  375. load_done_msg: db "load ksetup done. ", 0x0d, 0x0a, 0x00
  376. alignb 16
  377. gdt32:
  378. dd 0x00000000, 0x00000000
  379. gdt32_code:
  380. dd 0x0000ffff, 0x00cf9a00
  381. gdt32_data:
  382. dd 0x0000ffff, 0x00cf9200
  383. gdt32_len equ $ - gdt32
  384. gdt32_desc:
  385. dw gdt32_len - 1
  386. dd gdt32
  387. code32 equ gdt32_code - gdt32
  388. data32 equ gdt32_data - gdt32
  389. gdt64:
  390. dq 0x0000000000000000
  391. gdt64_code:
  392. dq 0x0020980000000000
  393. gdt64_data:
  394. dq 0x0000920000000000
  395. gdt64_len equ $ - gdt64
  396. gdt64_desc:
  397. dw gdt64_len - 1
  398. dd gdt64
  399. code64 equ gdt64_code - gdt64
  400. data64 equ gdt64_data - gdt64