Subversion Repositories Kolibri OS

Rev

Rev 5363 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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