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