Subversion Repositories Kolibri OS

Rev

Rev 7136 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2455 mario79 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
5363 yogev_ezra 3
;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;;
2455 mario79 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
796 shurf 8
; FAT12 boot sector for Kolibri OS
9
;
10
; Copyright (C) Alex Nogueira Teixeira
11
; Copyright (C) Diamond
12
; Copyright (C) Dmitry Kartashov aka shurf
13
;
14
; Distributed under GPL, see file COPYING for details
15
;
16
; Version 1.0
17
 
3274 esevece 18
include "lang.inc"
19
 
7136 dunkaist 20
lf              =     0ah
21
cr              =     0dh
796 shurf 22
 
7136 dunkaist 23
pos_read_tmp    =     0700h                   ;position for temporary read
24
boot_program    =     07c00h                  ;position for boot code
25
seg_read_kernel =     01000h                  ;segment to kernel read
796 shurf 26
 
2288 clevermous 27
        jmp     start_program
28
        nop
796 shurf 29
 
30
; Boot Sector and BPB Structure
31
include 'floppy1440.inc'
32
;include 'floppy2880.inc'
33
;include 'floppy1680.inc'
34
;include 'floppy1743.inc'
35
 
36
start_program:
7215 theonlymir 37
; 
38
        cld     ;clear direction flag for Phoenix BIOS, see next "lodsb"
2288 clevermous 39
        xor     ax, ax
7215 theonlymir 40
        cli
2288 clevermous 41
        mov     ss, ax
42
        mov     sp, boot_program
7215 theonlymir 43
        sti
44
; <\Efremenkov S.V.>
2288 clevermous 45
        push    ss
46
        pop     ds
796 shurf 47
 
2288 clevermous 48
        ; print loading string
49
        mov     si, loading+boot_program
796 shurf 50
loop_loading:
2288 clevermous 51
        lodsb
52
        or      al, al
53
        jz      read_root_directory
54
        mov     ah, 0eh
55
        mov     bx, 7
56
        int     10h
57
        jmp     loop_loading
796 shurf 58
 
59
read_root_directory:
2288 clevermous 60
        push    ss
61
        pop     es
796 shurf 62
 
2288 clevermous 63
        ; calculate some disk parameters
64
        ; - beginning sector of RootDir
65
        mov     ax, word [BPB_FATSz16+boot_program]
66
        xor     cx, cx
67
        mov     cl, byte [BPB_NumFATs+boot_program]
68
        mul     cx
69
        add     ax, word [BPB_RsvdSecCnt+boot_program]
70
        mov     word [FirstRootDirSecNum+boot_program], ax      ; 19
71
        mov     si, ax
796 shurf 72
 
2288 clevermous 73
        ; - count of sectors in RootDir
74
        mov     bx, word [BPB_BytsPerSec+boot_program]
75
        mov     cl, 5                           ; divide ax by 32
76
        shr     bx, cl                          ; bx = directory entries per sector
77
        mov     ax, word [BPB_RootEntCnt+boot_program]
78
        xor     dx, dx
79
        div     bx
80
        mov     word [RootDirSecs+boot_program], ax             ; 14
796 shurf 81
 
2288 clevermous 82
        ; - data start
83
        add     si, ax                          ; add beginning sector of RootDir and count sectors in RootDir
84
        mov     word [data_start+boot_program], si              ; 33
85
        ; reading root directory
86
        ; al=count root dir sectrors !!!! TODO: al, max 255 sectors !!!!
87
        mov     ah, 2                           ; read
88
        push    ax
796 shurf 89
 
2288 clevermous 90
        mov     ax, word [FirstRootDirSecNum+boot_program]
91
        call    conv_abs_to_THS                 ; convert abs sector (AX) to BIOS T:H:S (track:head:sector)
92
        pop     ax
93
        mov     bx, pos_read_tmp                ; es:bx read buffer
94
        call    read_sector
796 shurf 95
 
2288 clevermous 96
        mov     si, bx                          ; read buffer address: es:si
97
        mov     ax, [RootDirSecs+boot_program]
98
        mul     word [BPB_BytsPerSec+boot_program]
99
        add     ax, si                          ; AX = end of root dir. in buffer pos_read_tmp
796 shurf 100
 
2288 clevermous 101
        ; find kernel file in root directory
796 shurf 102
loop_find_dir_entry:
2288 clevermous 103
        push    si
104
        mov     cx, 11
105
        mov     di, kernel_name+boot_program
106
        rep cmpsb                               ; compare es:si and es:di, cx bytes long
107
        pop     si
108
        je      found_kernel_file
109
        add     si, 32                          ; next dir. entry
110
        cmp     si, ax                          ; end of directory
111
        jb      loop_find_dir_entry
796 shurf 112
 
113
file_error_message:
2288 clevermous 114
        mov     si, error_message+boot_program
796 shurf 115
 
116
loop_error_message:
2288 clevermous 117
        lodsb
118
        or      al, al
119
        jz      freeze_pc
120
        mov     ah, 0eh
121
        mov     bx, 7
122
        int     10h
123
        jmp     loop_error_message
796 shurf 124
 
125
freeze_pc:
2288 clevermous 126
        jmp     $                               ; endless loop
796 shurf 127
 
2288 clevermous 128
        ; === KERNEL FOUND. LOADING... ===
796 shurf 129
 
130
found_kernel_file:
2288 clevermous 131
        mov     bp, [si+01ah]                   ; first cluster of kernel file
132
        ; 
133
        mov     [cluster1st+boot_program], bp   ; starting cluster of kernel file
134
        ; <\diamond>
796 shurf 135
 
2288 clevermous 136
        ; reading first FAT table
137
        mov     ax, word [BPB_RsvdSecCnt+boot_program]  ; begin first FAT abs sector number
138
        call    conv_abs_to_THS                 ; convert abs sector (AX) to BIOS T:H:S (track:head:sector)
139
        mov     bx, pos_read_tmp                ; es:bx read position
140
        mov     ah, 2                           ; ah=2 (read)
141
        mov     al, byte [BPB_FATSz16+boot_program]     ; FAT size in sectors (TODO: max 255 sectors)
142
        call    read_sector
143
        jc      file_error_message              ; read error
796 shurf 144
 
2288 clevermous 145
        mov     ax, seg_read_kernel
146
        mov     es, ax
147
        xor     bx, bx                          ; es:bx = 1000h:0000h
796 shurf 148
 
149
 
2288 clevermous 150
        ; reading kernel file
796 shurf 151
loop_obtains_kernel_data:
2288 clevermous 152
        ; read one cluster of file
153
        call    obtain_cluster
154
        jc      file_error_message              ; read error
796 shurf 155
 
2288 clevermous 156
        ; add one cluster length to segment:offset
157
        push    bx
158
        mov     bx, es
159
        mov     ax, word [BPB_BytsPerSec+boot_program]  ;\
160
        movsx   cx, byte [BPB_SecPerClus+boot_program]  ; | !!! TODO: !!!
161
        mul     cx                                      ; | out this from loop !!!
162
        shr     ax, 4                                   ;/
163
        add     bx, ax
164
        mov     es, bx
165
        pop     bx
796 shurf 166
 
2288 clevermous 167
        mov     di, bp
168
        shr     di, 1
169
        pushf
170
        add     di, bp                          ; di = bp * 1.5
171
        add     di, pos_read_tmp
172
        mov     ax, [di]                        ; read next entry from FAT-chain
173
        popf
174
        jc      move_4_right
175
        and     ax, 0fffh
176
        jmp     verify_end_sector
796 shurf 177
move_4_right:
2288 clevermous 178
        mov     cl, 4
179
        shr     ax, cl
796 shurf 180
verify_end_sector:
2288 clevermous 181
        cmp     ax, 0ff8h                       ; last cluster
182
        jae     execute_kernel
183
        mov     bp, ax
184
        jmp     loop_obtains_kernel_data
796 shurf 185
 
186
execute_kernel:
2288 clevermous 187
        ; 
188
        mov     ax, 'KL'
189
        push    0
190
        pop     ds
191
        mov     si, loader_block+boot_program
192
        ; 
193
        push    word seg_read_kernel
194
        push    word 0
195
        retf                                    ; jmp far 1000:0000
796 shurf 196
 
197
 
198
;------------------------------------------
2288 clevermous 199
        ; loading cluster from file to es:bx
796 shurf 200
obtain_cluster:
2288 clevermous 201
        ; bp - cluster number to read
202
        ; carry = 0 -> read OK
203
        ; carry = 1 -> read ERROR
796 shurf 204
 
2288 clevermous 205
        ; print one dot
206
        push    bx
207
        mov     ax, 0e2eh                       ; ah=0eh (teletype), al='.'
208
        xor     bh, bh
209
        int     10h
210
        pop     bx
796 shurf 211
 
1738 clevermous 212
writesec:
2288 clevermous 213
        ; convert cluster number to sector number
214
        mov     ax, bp                          ; data cluster to read
215
        sub     ax, 2
216
        xor     dx, dx
217
        mov     dl, byte [BPB_SecPerClus+boot_program]
218
        mul     dx
219
        add     ax, word [data_start+boot_program]
796 shurf 220
 
2288 clevermous 221
        call    conv_abs_to_THS                 ; convert abs sector (AX) to BIOS T:H:S (track:head:sector)
796 shurf 222
patchhere:
2288 clevermous 223
        mov     ah, 2                           ; ah=2 (read)
224
        mov     al, byte [BPB_SecPerClus+boot_program]  ; al=(one cluster)
225
        call    read_sector
226
        retn
796 shurf 227
;------------------------------------------
228
 
229
;------------------------------------------
2288 clevermous 230
        ; read sector from disk
796 shurf 231
read_sector:
2288 clevermous 232
        push    bp
233
        mov     bp, 20                          ; try 20 times
796 shurf 234
newread:
2288 clevermous 235
        dec     bp
236
        jz      file_error_message
237
        push    ax bx cx dx
238
        int     13h
239
        pop     dx cx bx ax
240
        jc      newread
241
        pop     bp
242
        retn
796 shurf 243
;------------------------------------------
2288 clevermous 244
        ; convert abs. sector number (AX) to BIOS T:H:S
245
        ; sector number = (abs.sector%BPB_SecPerTrk)+1
246
        ; pre.track number = (abs.sector/BPB_SecPerTrk)
247
        ; head number = pre.track number%BPB_NumHeads
248
        ; track number = pre.track number/BPB_NumHeads
249
        ; Return: cl - sector number
250
        ;         ch - track number
251
        ;         dl - drive number (0 = a:)
252
        ;         dh - head number
796 shurf 253
conv_abs_to_THS:
2288 clevermous 254
        push    bx
255
        mov     bx, word [BPB_SecPerTrk+boot_program]
256
        xor     dx, dx
257
        div     bx
258
        inc     dx
259
        mov     cl, dl                          ; cl = sector number
260
        mov     bx, word [BPB_NumHeads+boot_program]
261
        xor     dx, dx
262
        div     bx
263
        ; !!!!!!! ax = track number, dx = head number
264
        mov     ch, al                          ; ch=track number
265
        xchg    dh, dl                          ; dh=head number
266
        mov     dl, 0                           ; dl=0 (drive 0 (a:))
267
        pop     bx
268
        retn
796 shurf 269
;------------------------------------------
270
 
3274 esevece 271
if lang eq sp
272
loading         db      cr,lf,'Iniciando el sistema ',00h
273
else
2288 clevermous 274
loading         db      cr,lf,'Starting system ',00h
3274 esevece 275
end if
2288 clevermous 276
error_message   db      13,10
277
kernel_name     db      'KERNEL  MNT ?',cr,lf,00h
278
FirstRootDirSecNum      dw      ?
279
RootDirSecs     dw      ?
280
data_start      dw      ?
796 shurf 281
 
282
; 
283
write1st:
2288 clevermous 284
        push    cs
285
        pop     ds
286
        mov     byte [patchhere+1+boot_program], 3      ; change ah=2 to ah=3
287
        mov     bp, [cluster1st+boot_program]
288
        push    1000h
289
        pop     es
290
        xor     bx, bx
291
        call    writesec
292
        mov     byte [patchhere+1+boot_program], 2      ; change back ah=3 to ah=2
293
        retf
294
cluster1st      dw      ?
796 shurf 295
loader_block:
2288 clevermous 296
                db      1
297
                dw      0
298
                dw      write1st+boot_program
299
                dw      0
796 shurf 300
; <\diamond>
301
 
2288 clevermous 302
times   0x1fe-$ db 00h
796 shurf 303
 
2288 clevermous 304
        db      55h,0aah                        ;boot signature