loader.asm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. .set_vga:
  32. ; get mode info
  33. mov ax, 0x4f01
  34. mov cx, 0x4118
  35. mov di, 0x7000
  36. int 0x10
  37. jc die
  38. mov al,[es:di]
  39. mov [VMODE], al
  40. mov ax, [es:di + 18]
  41. mov [SCREENX], ax
  42. mov ax, [es:di + 20]
  43. mov [SCREENY], ax
  44. mov eax, [es:di + 40]
  45. mov [FB_ADDR], eax
  46. mov ax, 0x4f02
  47. mov bx, 0x4118
  48. int 0x10
  49. .open_a20:
  50. ; disable interrupts before we initial done (enable in kernel)
  51. ; A20 line uses to control memory higher than 1MB. we can't access mem > 1MB when it's closing.
  52. ; So we must open it before switching to protect-mode.
  53. cli
  54. in al, 0x92
  55. or al, 0x02
  56. out 0x92, al
  57. .to_prot:
  58. ; switch to protect-mode.
  59. lgdt [gdt32_desc]
  60. ; CR0 (control register 0) uses to control CPU. It's first bit PE controls Protect-mode Enable.
  61. mov eax, cr0
  62. or eax, 1
  63. mov cr0, eax
  64. ; CPU is in protect-mode now, but segment registers didn't initialze.
  65. ; we cannot 'mov cs, xxx'
  66. jmp dword code32:init_prot
  67. print:
  68. pushaw
  69. ; message in register di, but lodsb uses si.
  70. mov si, di
  71. .loop:
  72. cld
  73. lodsb
  74. ; zero? -> return
  75. test al, al
  76. jz .ret
  77. mov ah, 0x0e
  78. mov bx, 0x000f
  79. int 0x10
  80. jmp .loop
  81. .ret:
  82. popaw
  83. ret
  84. MAGIC equ 0
  85. NINODES equ 4
  86. NBLOCKS equ 8
  87. NB_BBM equ 12
  88. NB_IBM equ 16
  89. BBM_BN equ 20
  90. IBM_BN equ 24
  91. NB_BIM equ 28
  92. NB_INODES equ 32
  93. BIM_BN equ 36
  94. INODES_BN equ 40
  95. ROOT_INO equ 44
  96. INODE_SIZE equ 32
  97. FIRSTDATABLOCK equ 76
  98. NEXT_INODE equ 80
  99. ZERO equ 84
  100. DENTRY_NAME equ 4
  101. load_ksetup:
  102. ; find 'ksetup' in the root-directory, and then read it to 0x8000
  103. ; get the inode of 'ksetup', then we can find in bim.
  104. ; the first block of root-inode must be first-data-block
  105. mov eax, [0x7e00 + FIRSTDATABLOCK]
  106. ; the fs buffer
  107. mov bx, 0x5000
  108. call read_block
  109. ; and start find in this directory
  110. mov si, 0x5000 + DENTRY_NAME
  111. mov di, fname
  112. mov cx, 6
  113. .cmp_loop:
  114. repe
  115. cmpsb
  116. jz found
  117. ; not found, continue
  118. add si, 128
  119. cmp si, 0x6000
  120. jae load_failed
  121. jmp .cmp_loop
  122. found:
  123. mov di, found_msg
  124. call print
  125. .get_inode:
  126. ; store its inode number in te eax
  127. ; 6: the length of file name
  128. mov ecx, [si - 6 - DENTRY_NAME]
  129. ; and then read this inode (only read the first block of inodes)
  130. mov eax, [0x7e00 + INODES_BN]
  131. mov bx, 0x5000
  132. call read_block
  133. ; back up the inode nr, for .search_bim
  134. mov edx, ecx
  135. ; * sizeof(struct inode)
  136. shl cx, 5
  137. mov si, 0x5000
  138. add si, cx
  139. ; and get its size
  140. INO_INODE equ 0
  141. INO_PINODE equ 4
  142. INO_ACCESS equ 8
  143. INO_BLOCKSNR equ 9
  144. INO_UPSIZE equ 13
  145. INO_CTIME equ 17
  146. INO_WTIME equ 21
  147. INO_NLINKS equ 25
  148. INO_UID equ 26
  149. INO_GID equ 28
  150. INO_EACCESS equ 30
  151. mov eax, [si + INO_BLOCKSNR]
  152. cmp dword [si + INO_UPSIZE], 0
  153. ; upsize is zero, so eax is blocks we need.
  154. je .read_ksetup
  155. ; okay, this is enough
  156. inc eax
  157. .read_ksetup:
  158. ; find in the bim (only first block, too)
  159. ; first read the first block of bim to 0x5000
  160. ; blocks need is in the eax, save it
  161. BIM_INODE equ 0
  162. BIM_THBLOCK equ 4
  163. mov ecx, eax
  164. mov eax, [0x7e00 + BIM_BN]
  165. mov bx, 0x5000
  166. call read_block
  167. ; and start search
  168. mov si, 0x5000
  169. mov eax, ecx
  170. xor ecx, ecx
  171. ; the to address
  172. ; and this is the address of kernel
  173. xor bx, bx
  174. push eax
  175. mov ax, 0x0900
  176. mov es, ax
  177. pop eax
  178. .search_bim:
  179. cmp ecx, eax
  180. ; ecx is the blocks we loaded (also bim->th_block), eax is the blocks of this file
  181. je .load_done
  182. ; the inode of this file is edx
  183. cmp edx, [si]
  184. ; not match, search again
  185. jne .search_again
  186. cmp ecx, [si + BIM_THBLOCK]
  187. jne .search_again
  188. .load_file:
  189. ; only one block, and coutinue search
  190. ; the block is the index of si (base is 0x5000), [si] is the inode
  191. ; not this!!! loader must uses 32-bit registers, so we should save/restore
  192. ; them in 32-bits
  193. ; pushaw
  194. push eax
  195. push ecx
  196. push edx
  197. push ebx
  198. push esp
  199. push ebp
  200. push esi
  201. push edi
  202. ; first get the block nr
  203. xor eax, eax
  204. mov ax, si
  205. ; - base
  206. sub eax, 0x5000
  207. ; / 8, get the index
  208. shr eax, 3
  209. ; and += first_data_block, because bim start at this.
  210. add eax, [0x7e00 + FIRSTDATABLOCK]
  211. ; now eax is the real block nr of this file
  212. ; read it!
  213. ; bx is the address, we don't need change
  214. call read_block
  215. pop edi
  216. pop esi
  217. pop ebp
  218. pop esp
  219. pop ebx
  220. pop edx
  221. pop ecx
  222. pop eax
  223. add bx, 4096
  224. inc ecx
  225. add si, 8
  226. jmp .search_bim
  227. .search_again:
  228. ; the size of a bim entry
  229. add si, 8
  230. cmp si, 0x6000
  231. jae load_failed
  232. jmp .search_bim
  233. .load_done:
  234. mov di, load_done_msg
  235. call print
  236. ret
  237. load_failed:
  238. mov di, load_failed_msg
  239. call print
  240. die: hlt
  241. jmp die
  242. read_block:
  243. push ax
  244. push cx
  245. ; 8 sectors = 4 KB = 1 block
  246. mov cx, 8
  247. shl eax, 3
  248. .loop:
  249. call read_lba
  250. inc eax
  251. dec cx
  252. add bx, 512
  253. cmp bx, 0
  254. je .go_on_es
  255. .cont:
  256. cmp cx, 0
  257. jne .loop
  258. pop cx
  259. pop ax
  260. ret
  261. .go_on_es:
  262. ; update the es, and continue
  263. push ax
  264. mov ax, es
  265. add ax, 512 / 16
  266. mov es, ax
  267. pop ax
  268. jmp .cont
  269. read_lba:
  270. push eax
  271. push ecx
  272. push edx
  273. push ebx
  274. push esp
  275. push ebp
  276. push esi
  277. push edi
  278. push dword 0x0000
  279. push dword eax
  280. ; buffer address
  281. push word es
  282. push word bx
  283. ; sectors count
  284. push word cx
  285. ; package size
  286. push word 16
  287. mov ah, 0x42
  288. mov dl, [BOOTDRIVER]
  289. mov si, sp
  290. int 0x13
  291. add sp, 16
  292. pop edi
  293. pop esi
  294. pop ebp
  295. pop esp
  296. pop ebx
  297. pop edx
  298. pop ecx
  299. pop eax
  300. jc read_failed
  301. ret
  302. read_failed:
  303. mov di, read_failed_msg
  304. call print
  305. jmp die
  306. bits 32
  307. init_prot:
  308. ; we set segment selectors for segment register (CS was set by far-jmp)
  309. mov ax, data32
  310. mov ds, ax
  311. mov es, ax
  312. mov fs, ax
  313. mov gs, ax
  314. mov ss, ax
  315. mov sp, 0x00007c00
  316. ; now we switch to long-mode
  317. .to_long_mode:
  318. ; clear page map 0 - 0x4000 (0x3000-0x4000 maybe not used)
  319. xor edi, edi
  320. ; mov edi, 0x00000000
  321. xor eax, eax
  322. ; store for 4096 times
  323. mov ecx, 4096
  324. cld
  325. rep stosd
  326. .init_paging:
  327. ; we set page map at 0x00000000 (a good address)
  328. mov dword [0x00000000], 0x00001000 | PAGE_USER
  329. ; mov dword [0x00000004], 0x00000000
  330. mov dword [0x00000800], 0x00001000 | PAGE_USER
  331. ; mov dword [0x00000804], 0x00000000
  332. ; and page directory pointer table
  333. mov dword [0x00001000], 0x00002000 | PAGE_USER
  334. ; mov dword [0x00001004], 0x00000000
  335. ; page directory entry (2 MB)
  336. mov dword [0x00002000], 0x00000000 | PAGE_KERNEL
  337. ; mov dword [0x00002004], 0x00000000
  338. mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
  339. ; mov dword [0x0000200c], 0x00000000
  340. ; get frame buffer address and map it here.
  341. mov eax, [0x7c06]
  342. ; eax &= PAGE_SIZE
  343. and eax, ~(1048576 * 2 - 1)
  344. or eax, PAGE_KERNEL
  345. mov dword [0x00002010], eax
  346. ; mov dword [0x00002014], 0x00000000
  347. add eax, 1048576 * 2
  348. mov dword [0x00002018], eax
  349. add eax, 1048576 * 2
  350. mov dword [0x00002020], eax
  351. .enable_long_mode:
  352. lgdt [gdt64_desc]
  353. mov ax, data64
  354. mov ds, ax
  355. mov es, ax
  356. mov fs, ax
  357. mov gs, ax
  358. mov ss, ax
  359. ; open PAE (page map level 3)
  360. mov eax, cr4
  361. bts eax, 5
  362. mov cr4, eax
  363. ; set Page Table Base Register
  364. ; mov eax, 0x00000000
  365. xor eax, eax
  366. mov cr3, eax
  367. ; enable long mode (must before paging)
  368. mov ecx, 0xc0000080
  369. rdmsr
  370. bts eax, 8
  371. wrmsr
  372. ; enable paging
  373. mov eax, cr0
  374. ; PE again
  375. or eax, 1
  376. bts eax, 31
  377. mov cr0, eax
  378. ; okay! now set segment registers and init long mode
  379. jmp code64:0x9000
  380. msg: db "welcome to XNIX Loader. ", 0x0d, 0x0a, 0x00
  381. read_failed_msg: db "fatal: read disk failed. ", 0x0d, 0x0a, 0x00
  382. fname: db "ksetup", 0x00
  383. found_msg: db "found ksetup in the root. ", 0x0d, 0x0a, 0x00
  384. load_failed_msg: db "failed when trying to load ksetup. ", 0x0d, 0x0a, 0x00
  385. load_done_msg: db "load ksetup done. ", 0x0d, 0x0a, 0x00
  386. alignb 16
  387. gdt32:
  388. dd 0x00000000, 0x00000000
  389. gdt32_code:
  390. dd 0x0000ffff, 0x00cf9a00
  391. gdt32_data:
  392. dd 0x0000ffff, 0x00cf9200
  393. gdt32_len equ $ - gdt32
  394. gdt32_desc:
  395. dw gdt32_len - 1
  396. dd gdt32
  397. code32 equ gdt32_code - gdt32
  398. data32 equ gdt32_data - gdt32
  399. gdt64:
  400. dq 0x0000000000000000
  401. gdt64_code:
  402. dq 0x0020980000000000
  403. gdt64_data:
  404. dq 0x0000920000000000
  405. gdt64_len equ $ - gdt64
  406. gdt64_desc:
  407. dw gdt64_len - 1
  408. dd gdt64
  409. code64 equ gdt64_code - gdt64
  410. data64 equ gdt64_data - gdt64