Rev 5363 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4440 | Serge | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
5363 | yogev_ezra | 3 | ;; Copyright (C) KolibriOS team 2014-2015. All rights reserved. ;; |
4440 | Serge | 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 | |||
7136 | dunkaist | 19 | lf = 0x0A |
20 | cr = 0x0D |
||
4440 | Serge | 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 |
||
4441 | Serge | 59 | mov ecx, 624/4 ;magic value |
4440 | Serge | 60 | rep movsd |
61 | jmp .check_mbi |
||
62 | |||
63 | org $-0x80000 |
||
64 | align 4 |
||
65 | .check_mbi: |
||
66 | cmp eax, 0x2BADB002 |
||
4441 | Serge | 67 | mov esi, sz_invboot |
68 | jne .panic |
||
4440 | Serge | 69 | |
70 | bt dword [ebx], 3 |
||
4441 | Serge | 71 | mov esi, sz_nomods |
72 | jnc .panic |
||
4440 | Serge | 73 | |
4441 | Serge | 74 | mov edx, [ebx+20] ;mods_count |
75 | mov edi, [ebx+24] ;mods_addr |
||
4440 | Serge | 76 | cmp edx, 1 |
4441 | Serge | 77 | mov esi, sz_nomods |
78 | jne .panic |
||
4440 | Serge | 79 | |
80 | .scan_mod: |
||
4441 | Serge | 81 | mov ebp, [edi] ;image start |
82 | mov ecx, [edi+4] ;image end |
||
83 | sub ecx, ebp ;image size |
||
84 | cmp ecx, 512*18*80*2 ;1.44 floppy |
||
85 | mov esi, sz_image |
||
86 | jne .panic |
||
4440 | Serge | 87 | |
88 | mov [_image_start], ebp |
||
89 | mov [_image_size], ecx |
||
90 | |||
91 | ; calculate some disk parameters |
||
92 | ; - beginning sector of RootDir |
||
93 | |||
94 | movzx eax, word [.BPB_FATSz16] |
||
95 | movzx ecx, byte [.BPB_NumFATs] |
||
96 | mul ecx |
||
97 | add ax, [.BPB_RsvdSecCnt] |
||
98 | mov [FirstRootDirSecNum], eax |
||
99 | mov esi, eax |
||
100 | |||
101 | ; - count of sectors in RootDir |
||
102 | movzx ebx, word [.BPB_BytsPerSec] |
||
103 | mov cl, 5 ; divide ax by 32 |
||
104 | shr ebx, cl ; bx = directory entries per sector |
||
105 | movzx eax, word [.BPB_RootEntCnt] |
||
106 | xor edx, edx |
||
107 | div ebx |
||
108 | mov [RootDirSecs], eax |
||
109 | |||
110 | ; - data start |
||
111 | add esi, eax ; add beginning sector of RootDir and count sectors in RootDir |
||
112 | mov [data_start], esi |
||
113 | |||
114 | ; reading root directory |
||
115 | ; al=count root dir sectrors !!!! TODO: al, max 255 sectors !!!! |
||
116 | |||
117 | mov eax, [FirstRootDirSecNum] |
||
118 | mul word [.BPB_BytsPerSec] |
||
119 | lea esi, [ebp+eax] |
||
120 | |||
121 | mov eax, [RootDirSecs] |
||
122 | mul word [.BPB_BytsPerSec] |
||
123 | add eax, esi ; EAX = end of root dir. in buffer pos_read_tmp |
||
124 | |||
125 | ; find kernel file in root directory |
||
126 | |||
127 | .loop_find_dir_entry: |
||
128 | push esi |
||
129 | mov ecx, 11 |
||
130 | mov edi, kernel_name |
||
131 | rep cmpsb ; compare es:si and es:di, cx bytes long |
||
132 | pop esi |
||
133 | je .found_kernel_file |
||
134 | add esi, 32 ; next dir. entry |
||
135 | cmp esi, eax ; end of directory |
||
136 | jb .loop_find_dir_entry |
||
137 | |||
4441 | Serge | 138 | mov esi, sz_kernel |
139 | jmp .panic |
||
4440 | Serge | 140 | |
141 | ; === KERNEL FOUND. LOADING... === |
||
142 | |||
143 | .found_kernel_file: |
||
144 | |||
145 | movzx ecx, word [esi+01ah] ; first cluster of kernel file |
||
146 | |||
147 | ; reading first FAT table |
||
148 | movzx eax, word [.BPB_RsvdSecCnt] ; begin first FAT abs sector number |
||
149 | mul word [.BPB_BytsPerSec] |
||
150 | lea ebx, [ebp+eax] ; FAT address |
||
151 | |||
152 | ;ebx = FAT |
||
153 | ;ecx = cluster |
||
154 | ;esi = src |
||
155 | ;edi = dst |
||
156 | ;ebp = image |
||
157 | |||
158 | ; copy kernel file |
||
159 | |||
160 | movzx eax, word [.BPB_BytsPerSec] |
||
161 | movsx edx, byte [.BPB_SecPerClus] |
||
162 | mul edx |
||
163 | shr eax, 2 |
||
164 | mov [cluster_size], eax |
||
165 | |||
166 | mov edi, 0x10000 ;kernel base address |
||
167 | |||
168 | .copy_kernel: |
||
169 | |||
170 | ; convert cluster number to sector number |
||
171 | mov eax, ecx ; data cluster to read |
||
172 | sub eax, 2 |
||
173 | movzx edx, byte [.BPB_SecPerClus] |
||
174 | mul edx |
||
175 | add eax, [data_start] |
||
176 | movzx edx, word [.BPB_BytsPerSec] |
||
177 | mul edx |
||
178 | |||
179 | lea esi, [ebp+eax] |
||
180 | mov edx, ecx |
||
181 | mov ecx, [cluster_size] |
||
182 | rep movsd |
||
183 | mov ecx, edx |
||
184 | |||
185 | shr edx, 1 |
||
186 | pushf |
||
187 | add edx, ecx ; di = bp * 1.5 |
||
188 | mov ax, word [ebx+edx] ; read next entry from FAT-chain |
||
189 | popf |
||
190 | jc .move_4_right |
||
191 | and ax, 0fffh |
||
192 | jmp .verify_end_sector |
||
193 | .move_4_right: |
||
194 | shr ax, 4 |
||
195 | .verify_end_sector: |
||
196 | cmp ax, 0ff8h ; last cluster |
||
197 | jae .execute_kernel |
||
198 | movzx ecx, ax |
||
199 | jmp .copy_kernel |
||
200 | |||
201 | .execute_kernel: |
||
202 | |||
203 | mov edi, 0x100000 |
||
204 | mov esi, [_image_start] |
||
205 | mov ecx, [_image_size] |
||
206 | shr ecx, 2 |
||
207 | rep movsd |
||
208 | xor eax, eax |
||
209 | mov ecx, 1024 |
||
210 | rep stosd |
||
211 | |||
212 | xor ebx, ebx |
||
213 | xor ecx, ecx |
||
214 | xor edx, edx |
||
215 | xor esi, esi |
||
216 | xor edi, edi |
||
217 | xor ebp, ebp |
||
218 | xor esp, esp |
||
219 | |||
220 | lgdt [.tmp_gdt] |
||
221 | jmp far 0x08:.mode_16 and 0xFFFF |
||
222 | |||
4441 | Serge | 223 | .panic: |
224 | mov ebx, sz_halt |
||
225 | mov edx, 0xb8000+160*10+2 |
||
226 | mov ah, 0x07 |
||
227 | .line: |
||
228 | mov edi, edx |
||
229 | .print: |
||
230 | lodsb |
||
231 | test al, al |
||
232 | jz .print_next |
||
233 | stosw |
||
234 | jmp .print |
||
235 | |||
236 | .print_next: |
||
237 | test ebx, ebx |
||
238 | jz ._hlt |
||
239 | |||
240 | mov esi, ebx |
||
241 | xor ebx, ebx |
||
242 | add edx, 160 |
||
243 | jmp .line |
||
244 | |||
4440 | Serge | 245 | ._hlt: |
246 | hlt |
||
247 | jmp ._hlt |
||
248 | |||
249 | align 8 |
||
250 | .tmp_gdt: dw 15 |
||
251 | dd .tmp_gdt |
||
252 | dw 0 |
||
253 | |||
254 | .code16: dw 0xFFFF |
||
255 | dw 0 |
||
256 | db 8 |
||
257 | db 10011010b |
||
258 | dw 0 |
||
259 | |||
260 | use16 |
||
261 | .mode_16: |
||
262 | mov eax, cr0 |
||
263 | and eax, not 0x80000001 |
||
264 | mov cr0, eax |
||
265 | jmp far 0x8000:.real_mode and 0xFFFF |
||
266 | |||
267 | .real_mode: |
||
268 | xor eax, eax |
||
269 | mov ds, ax |
||
270 | mov es, ax |
||
271 | mov ss, ax |
||
272 | mov gs, ax |
||
273 | mov fs, ax |
||
274 | jmp far 0x1000:0000 |
||
275 | |||
276 | |||
4441 | Serge | 277 | sz_invboot db 'Invalid multiboot loader magic value',0 |
278 | sz_nomods db 'No image loaded',0 |
||
279 | sz_image db 'Image size invalid',0 |
||
280 | sz_halt db 'Halted',0 |
||
4440 | Serge | 281 | |
4441 | Serge | 282 | sz_kernel db cr |
283 | kernel_name db 'KERNEL MNT ?',0 |
||
4440 | Serge | 284 | |
285 | org $+0x80000 |
||
286 | __edata: |
||
287 | |||
288 | align 4 |
||
289 | _image_start rd 1 |
||
290 | _image_size rd 1 |
||
291 | |||
292 | FirstRootDirSecNum rd 1 |
||
293 | RootDirSecs rd 1 |
||
294 | data_start rd 1 |
||
295 | cluster_size rd 1 |
||
296 | __end: |