Rev 4441 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4440 | Serge | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3 | ;; Copyright (C) KolibriOS team 2014. All rights reserved. ;; |
||
4 | ;; Distributed under terms of the GNU General Public License ;; |
||
5 | ;; ;; |
||
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
7 | |||
8 | ; Kolibri OS support loader for GRUB |
||
9 | ; |
||
10 | ; Copyright (C) Alex Nogueira Teixeira |
||
11 | ; Copyright (C) Diamond |
||
12 | ; Copyright (C) Dmitry Kartashov aka shurf |
||
13 | ; Copyright (C) Serge |
||
14 | ; |
||
15 | ; Distributed under GPL, see file COPYING for details |
||
16 | ; |
||
17 | ; Version 1.0 |
||
18 | |||
19 | lf equ 0x0A |
||
20 | cr equ 0x0D |
||
21 | |||
22 | use32 |
||
23 | |||
24 | |||
25 | org 0x100000 |
||
26 | |||
27 | mboot: |
||
28 | dd 0x1BADB002 |
||
29 | dd 0x00010003 |
||
30 | dd -(0x1BADB002 + 0x00010003) |
||
31 | dd mboot |
||
32 | dd 0x100000 |
||
33 | dd __edata |
||
34 | dd __end |
||
35 | dd __start |
||
36 | |||
37 | align 16 |
||
38 | __start: |
||
39 | |||
40 | virtual at ebp+3 |
||
41 | .BS_OEMName rb 8 |
||
42 | .BPB_BytsPerSec rw 1 ; bytes per sector |
||
43 | .BPB_SecPerClus rb 1 ; sectors per cluster |
||
44 | .BPB_RsvdSecCnt rw 1 ; number of reserver sectors |
||
45 | .BPB_NumFATs rb 1 ; count of FAT data structures |
||
46 | .BPB_RootEntCnt rw 1 ; count of 32-byte dir. entries (224*32 = 14 sectors) |
||
47 | .BPB_TotSec16 rw 1 ; count of sectors on the volume (2880 for 1.44 mbytes disk) |
||
48 | .BPB_Media rb 1 ; f0 - used for removable media |
||
49 | .BPB_FATSz16 rw 1 ; count of sectors by one copy of FAT |
||
50 | .BPB_SecPerTrk rw 1 ; sectors per track |
||
51 | .BPB_NumHeads rw 1 ; number of heads |
||
52 | .BPB_HiddSec rd 1 ; count of hidden sectors |
||
53 | .BPB_TotSec32 rd 1 ; count of sectors on the volume (if > 65535) |
||
54 | end virtual |
||
55 | |||
56 | cld |
||
57 | mov esi, mboot |
||
58 | mov edi, 0x80000 |
||
59 | mov ecx, 600/4 ;magic value |
||
60 | rep movsd |
||
61 | jmp .check_mbi |
||
62 | |||
63 | org $-0x80000 |
||
64 | align 4 |
||
65 | .check_mbi: |
||
66 | cmp eax, 0x2BADB002 |
||
67 | mov ecx, sz_invboot |
||
68 | jne .fault |
||
69 | |||
70 | bt dword [ebx], 3 |
||
71 | mov ecx, sz_nomods |
||
72 | jnc .fault |
||
73 | |||
74 | mov edx, [ebx+20] ;mods_count |
||
75 | mov esi, [ebx+24] ;mods_addr |
||
76 | cmp edx, 1 |
||
77 | jne .fault |
||
78 | |||
79 | .scan_mod: |
||
80 | mov ebp, [esi] ;image start |
||
81 | mov ecx, [esi+4] ;image end |
||
82 | sub ecx, ebp ;image size |
||
83 | cmp ecx, 512*18*80*2 ;1.44 floppy |
||
84 | jne .fault |
||
85 | |||
86 | mov [_image_start], ebp |
||
87 | mov [_image_size], ecx |
||
88 | |||
89 | ; calculate some disk parameters |
||
90 | ; - beginning sector of RootDir |
||
91 | |||
92 | movzx eax, word [.BPB_FATSz16] |
||
93 | movzx ecx, byte [.BPB_NumFATs] |
||
94 | mul ecx |
||
95 | add ax, [.BPB_RsvdSecCnt] |
||
96 | mov [FirstRootDirSecNum], eax |
||
97 | mov esi, eax |
||
98 | |||
99 | ; - count of sectors in RootDir |
||
100 | movzx ebx, word [.BPB_BytsPerSec] |
||
101 | mov cl, 5 ; divide ax by 32 |
||
102 | shr ebx, cl ; bx = directory entries per sector |
||
103 | movzx eax, word [.BPB_RootEntCnt] |
||
104 | xor edx, edx |
||
105 | div ebx |
||
106 | mov [RootDirSecs], eax |
||
107 | |||
108 | ; - data start |
||
109 | add esi, eax ; add beginning sector of RootDir and count sectors in RootDir |
||
110 | mov [data_start], esi |
||
111 | |||
112 | ; reading root directory |
||
113 | ; al=count root dir sectrors !!!! TODO: al, max 255 sectors !!!! |
||
114 | |||
115 | mov eax, [FirstRootDirSecNum] |
||
116 | mul word [.BPB_BytsPerSec] |
||
117 | lea esi, [ebp+eax] |
||
118 | |||
119 | mov eax, [RootDirSecs] |
||
120 | mul word [.BPB_BytsPerSec] |
||
121 | add eax, esi ; EAX = end of root dir. in buffer pos_read_tmp |
||
122 | |||
123 | ; find kernel file in root directory |
||
124 | |||
125 | .loop_find_dir_entry: |
||
126 | push esi |
||
127 | mov ecx, 11 |
||
128 | mov edi, kernel_name |
||
129 | rep cmpsb ; compare es:si and es:di, cx bytes long |
||
130 | pop esi |
||
131 | je .found_kernel_file |
||
132 | add esi, 32 ; next dir. entry |
||
133 | cmp esi, eax ; end of directory |
||
134 | jb .loop_find_dir_entry |
||
135 | |||
136 | mov ecx, sz_kernel |
||
137 | jmp .fault |
||
138 | |||
139 | ; === KERNEL FOUND. LOADING... === |
||
140 | |||
141 | .found_kernel_file: |
||
142 | |||
143 | movzx ecx, word [esi+01ah] ; first cluster of kernel file |
||
144 | |||
145 | ; reading first FAT table |
||
146 | movzx eax, word [.BPB_RsvdSecCnt] ; begin first FAT abs sector number |
||
147 | mul word [.BPB_BytsPerSec] |
||
148 | lea ebx, [ebp+eax] ; FAT address |
||
149 | |||
150 | ;ebx = FAT |
||
151 | ;ecx = cluster |
||
152 | ;esi = src |
||
153 | ;edi = dst |
||
154 | ;ebp = image |
||
155 | |||
156 | ; copy kernel file |
||
157 | |||
158 | movzx eax, word [.BPB_BytsPerSec] |
||
159 | movsx edx, byte [.BPB_SecPerClus] |
||
160 | mul edx |
||
161 | shr eax, 2 |
||
162 | mov [cluster_size], eax |
||
163 | |||
164 | mov edi, 0x10000 ;kernel base address |
||
165 | |||
166 | .copy_kernel: |
||
167 | |||
168 | ; convert cluster number to sector number |
||
169 | mov eax, ecx ; data cluster to read |
||
170 | sub eax, 2 |
||
171 | movzx edx, byte [.BPB_SecPerClus] |
||
172 | mul edx |
||
173 | add eax, [data_start] |
||
174 | movzx edx, word [.BPB_BytsPerSec] |
||
175 | mul edx |
||
176 | |||
177 | lea esi, [ebp+eax] |
||
178 | mov edx, ecx |
||
179 | mov ecx, [cluster_size] |
||
180 | rep movsd |
||
181 | mov ecx, edx |
||
182 | |||
183 | shr edx, 1 |
||
184 | pushf |
||
185 | add edx, ecx ; di = bp * 1.5 |
||
186 | mov ax, word [ebx+edx] ; read next entry from FAT-chain |
||
187 | popf |
||
188 | jc .move_4_right |
||
189 | and ax, 0fffh |
||
190 | jmp .verify_end_sector |
||
191 | .move_4_right: |
||
192 | shr ax, 4 |
||
193 | .verify_end_sector: |
||
194 | cmp ax, 0ff8h ; last cluster |
||
195 | jae .execute_kernel |
||
196 | movzx ecx, ax |
||
197 | jmp .copy_kernel |
||
198 | |||
199 | .execute_kernel: |
||
200 | |||
201 | mov edi, 0x100000 |
||
202 | mov esi, [_image_start] |
||
203 | mov ecx, [_image_size] |
||
204 | shr ecx, 2 |
||
205 | rep movsd |
||
206 | xor eax, eax |
||
207 | mov ecx, 1024 |
||
208 | rep stosd |
||
209 | |||
210 | xor ebx, ebx |
||
211 | xor ecx, ecx |
||
212 | xor edx, edx |
||
213 | xor esi, esi |
||
214 | xor edi, edi |
||
215 | xor ebp, ebp |
||
216 | xor esp, esp |
||
217 | |||
218 | lgdt [.tmp_gdt] |
||
219 | jmp far 0x08:.mode_16 and 0xFFFF |
||
220 | |||
221 | .fault: |
||
222 | ; push ecx |
||
223 | ; call _lcls |
||
224 | ; call __bprintf |
||
225 | ._hlt: |
||
226 | hlt |
||
227 | jmp ._hlt |
||
228 | |||
229 | align 8 |
||
230 | .tmp_gdt: dw 15 |
||
231 | dd .tmp_gdt |
||
232 | dw 0 |
||
233 | |||
234 | .code16: dw 0xFFFF |
||
235 | dw 0 |
||
236 | db 8 |
||
237 | db 10011010b |
||
238 | dw 0 |
||
239 | |||
240 | use16 |
||
241 | .mode_16: |
||
242 | mov eax, cr0 |
||
243 | and eax, not 0x80000001 |
||
244 | mov cr0, eax |
||
245 | jmp far 0x8000:.real_mode and 0xFFFF |
||
246 | |||
247 | .real_mode: |
||
248 | xor eax, eax |
||
249 | mov ds, ax |
||
250 | mov es, ax |
||
251 | mov ss, ax |
||
252 | mov gs, ax |
||
253 | mov fs, ax |
||
254 | jmp far 0x1000:0000 |
||
255 | |||
256 | sz_invboot db 'Invalid multiboot loader magic value',cr,lf |
||
257 | db 'Halted',0 |
||
258 | |||
259 | sz_nomods db 'No modules loaded',cr,lf |
||
260 | db 'Halted',0 |
||
261 | |||
262 | sz_kernel db cr,lf |
||
263 | kernel_name db 'KERNEL MNT ?',cr,lf,0 |
||
264 | |||
265 | org $+0x80000 |
||
266 | __edata: |
||
267 | |||
268 | align 4 |
||
269 | _image_start rd 1 |
||
270 | _image_size rd 1 |
||
271 | |||
272 | FirstRootDirSecNum rd 1 |
||
273 | RootDirSecs rd 1 |
||
274 | data_start rd 1 |
||
275 | cluster_size rd 1 |
||
276 | __end: |