Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
431 serge 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
3
;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
593 mikedld 8
$Revision: 649 $
9
 
10
 
65 mario79 11
;*************************************************************
578 mario79 12
;* 12.07.2007 Check all 4 entry of MBR and EMBR
13
;* 29.04.2006 Elimination of hangup after the
14
;*             expiration hd_wait_timeout -  Mario79
15
;* 28.01.2006 find all Fat16/32 partition in all input point
16
;*            to MBR - Mario79
65 mario79 17
;*************************************************************
18
 
277 diamond 19
uglobal
65 mario79 20
align 4
256 diamond 21
 
65 mario79 22
;******************************************************
431 serge 23
; Please do not change this place - variables  in text
65 mario79 24
; Mario79
25
; START place
26
;******************************************************
27
PARTITION_START      dd 0x3f
28
PARTITION_END        dd 0
256 diamond 29
fs_type              db 0       ; 0=none, 1=NTFS, 16=FAT16, 32=FAT32
277 diamond 30
align 4
256 diamond 31
 
32
fs_dependent_data_start:
33
; FATxx data
34
 
65 mario79 35
SECTORS_PER_FAT      dd 0x1f3a
36
NUMBER_OF_FATS       dd 0x2
37
SECTORS_PER_CLUSTER  dd 0x8
38
BYTES_PER_SECTOR     dd 0x200   ; Note: if BPS <> 512 need lots of changes
39
ROOT_CLUSTER         dd 2       ; first rootdir cluster
40
FAT_START            dd 0       ; start of fat table
41
ROOT_START           dd 0       ; start of rootdir (only fat16)
42
ROOT_SECTORS         dd 0       ; count of rootdir sectors (only fat16)
43
DATA_START           dd 0       ; start of data area (=first cluster 2)
44
LAST_CLUSTER         dd 0       ; last availabe cluster
45
ADR_FSINFO           dd 0       ; used only by fat32
46
 
47
fatRESERVED          dd 0x0FFFFFF6
48
fatBAD               dd 0x0FFFFFF7
49
fatEND               dd 0x0FFFFFF8
50
fatMASK              dd 0x0FFFFFFF
51
 
649 diamond 52
fatStartScan         dd 2
53
 
256 diamond 54
fs_dependent_data_end:
321 diamond 55
file_system_data_size = $ - PARTITION_START
56
if file_system_data_size > 96
57
ERROR: sizeof(file system data) too big!
58
end if
256 diamond 59
 
60
virtual at fs_dependent_data_start
61
; NTFS data
62
ntfs_data:
63
.sectors_per_cluster    dd      ?
64
.mft_cluster            dd      ?
65
.mftmirr_cluster        dd      ?
66
.frs_size               dd      ?       ; FRS size in bytes
67
.iab_size               dd      ?       ; IndexAllocationBuffer size in bytes
68
.frs_buffer             dd      ?
69
.iab_buffer             dd      ?
70
.mft_retrieval          dd      ?
71
.mft_retrieval_size     dd      ?
72
.mft_retrieval_alloc    dd      ?
73
.mft_retrieval_end      dd      ?
74
.cur_index_size         dd      ?
75
.cur_index_buf          dd      ?
76
if $ > fs_dependent_data_end
77
ERROR: increase sizeof(fs_dependent_data)!
78
end if
79
end virtual
80
 
65 mario79 81
;***************************************************************************
82
; End place
83
; Mario79
84
;***************************************************************************
277 diamond 85
endg
86
iglobal
65 mario79 87
 
88
  partition_types:              ; list of fat16/32 partitions
89
    db    0x04                  ; DOS: fat16 <32M
90
    db    0x06                  ; DOS: fat16 >32M
91
    db    0x0b                  ; WIN95: fat32
92
    db    0x0c                  ; WIN95: fat32, LBA-mapped
93
    db    0x0e                  ; WIN95: fat16, LBA-mapped
94
    db    0x14                  ; Hidden DOS: fat16 <32M
95
    db    0x16                  ; Hidden DOS: fat16 >32M
96
    db    0x1b                  ; Hidden WIN95: fat32
97
    db    0x1c                  ; Hidden WIN95: fat32, LBA-mapped
98
    db    0x1e                  ; Hidden WIN95: fat16, LBA-mapped
99
    db    0xc4                  ; DRDOS/secured: fat16 <32M
100
    db    0xc6                  ; DRDOS/secured: fat16 >32M
101
    db    0xcb                  ; DRDOS/secured: fat32
102
    db    0xcc                  ; DRDOS/secured: fat32, LBA-mapped
103
    db    0xce                  ; DRDOS/secured: fat16, LBA-mapped
104
    db    0xd4                  ; Old Multiuser DOS secured: fat16 <32M
105
    db    0xd6                  ; Old Multiuser DOS secured: fat16 >32M
256 diamond 106
    db    0x07                  ; NTFS
65 mario79 107
  partition_types_end:
108
 
109
 
110
  extended_types:               ; list of extended partitions
111
    db    0x05                  ; DOS: extended partition
112
    db    0x0f                  ; WIN95: extended partition, LBA-mapped
113
    db    0xc5                  ; DRDOS/secured: extended partition
114
    db    0xd5                  ; Old Multiuser DOS secured: extended partition
115
  extended_types_end:
116
 
117
endg
118
 
119
; Partition chain used:
120
; MBR        ;   PARTITION2 ;   PARTITION3 ;   PARTITION4
121
;==========================================================
122
; fat16/32   +-- fat16/32   +-- fat16/32   +-- fat16/32   +--
123
; extended --+   extended --+   extended --+   extended --+
124
; 0              0              0              0
125
; 0              0              0              0
126
; Notes:
127
; - extended partition need to be in second entry on table
128
; - it will skip over removed partitions
129
 
130
set_FAT32_variables:
131
    mov   [problem_partition],0
132
    call  reserve_hd1
321 diamond 133
    call  reserve_hd_channel
65 mario79 134
 
135
    cmp   dword [hdpos],0
136
    je    problem_hd
137
 
138
    pushad
139
    xor   ecx,ecx               ; partition count
140
    mov   edx,-1                ; flag for partition
141
    xor   eax,eax               ; read MBR
142
    xor   ebp,ebp               ; extended partition start
143
 
144
new_partition:
145
    test  ebp,ebp               ; is there extended partition?
146
    jnz   extended_already_set  ; yes
147
    xchg  ebp,eax               ; no. set it now
148
 
149
extended_already_set:
150
    add   eax,ebp               ; mbr=mbr+0, ext_part=ext_start+relat_start
151
    mov   ebx,buffer
152
    call  hd_read
74 mario79 153
    cmp  [hd_error],0
154
    jne  problem_hd
65 mario79 155
 
156
    cmp   word [ebx+0x1fe],0xaa55 ; is it valid boot sector?
157
    jnz   end_partition_chain
158
    cmp   dword [ebx+0x1be+0xc],0 ; skip over empty partition
578 mario79 159
;    jz    next_partition
160
    jnz    .next_primary_partition
161
    cmp   dword [ebx+0x1be+0xc+16],0
162
    jnz    next_primary_partition
163
    cmp   dword [ebx+0x1be+0xc+16+16],0
164
    jnz    next_primary_partition_1
165
    cmp   dword [ebx+0x1be+0xc+16+16+16],0
166
    jnz    next_primary_partition_2
167
    jmp   next_partition
168
 
169
.next_primary_partition:
65 mario79 170
    push  eax
171
    mov   al,[ebx+0x1be+4]      ; get primary partition type
172
    call  scan_partition_types
173
    pop   eax
174
    jnz   next_primary_partition        ; no. skip over
175
 
176
    inc   ecx
177
    cmp   ecx,[fat32part]       ; is it wanted partition?
178
    jnz   next_primary_partition        ; no
179
 
256 diamond 180
        mov     edx, eax                ; start sector
181
        add     edx, [ebx+0x1be+8]      ; add relative start
182
        push    edx
183
        add     edx, [ebx+0x1be+12]     ; add length
184
        dec     edx                     ; PARTITION_END is inclusive
185
        mov     [PARTITION_END], edx    ; note that this can be changed
186
                                        ; when file system data will be available
187
        mov     dl, [ebx+0x1be+4]
188
        mov     [fs_type], dl           ; save for FS recognizer (separate FAT vs NTFS)
189
        pop     edx
190
 
65 mario79 191
next_primary_partition:
192
    push  eax
193
    mov   al,[ebx+0x1be+4+16]      ; get primary partition type
194
    call  scan_partition_types
195
    pop   eax
196
    jnz   next_primary_partition_1        ; no. skip over
197
 
198
    inc   ecx
199
    cmp   ecx,[fat32part]       ; is it wanted partition?
200
    jnz   next_primary_partition_1        ; no
201
 
256 diamond 202
        mov     edx, eax
203
        add     edx, [ebx+0x1be+8+16]
204
        push    edx
205
        add     edx, [ebx+0x1be+12+16]
206
        dec     edx
207
        mov     [PARTITION_END], edx
208
        mov     dl, [ebx+0x1be+4+16]
209
        mov     [fs_type], dl
210
        pop     edx
65 mario79 211
 
212
next_primary_partition_1:
213
    push  eax
214
    mov   al,[ebx+0x1be+4+16+16]      ; get primary partition type
215
    call  scan_partition_types
216
    pop   eax
217
    jnz   next_primary_partition_2        ; no. skip over
218
 
219
    inc   ecx
220
    cmp   ecx,[fat32part]       ; is it wanted partition?
221
    jnz   next_primary_partition_2        ; no
222
 
256 diamond 223
        mov     edx, eax
224
        add     edx, [ebx+0x1be+8+16+16]
225
        push    edx
226
        add     edx, [ebx+0x1be+12+16+16]
227
        dec     edx
228
        mov     [PARTITION_END], edx
229
        mov     dl, [ebx+0x1be+4+16+16]
230
        mov     [fs_type], dl
231
        pop     edx
65 mario79 232
 
233
next_primary_partition_2:
234
    push  eax
235
    mov   al,[ebx+0x1be+4+16+16+16]      ; get primary partition type
236
    call  scan_partition_types
237
    pop   eax
238
    jnz   next_partition        ; no. skip over
239
 
240
    inc   ecx
241
    cmp   ecx,[fat32part]       ; is it wanted partition?
242
    jnz   next_partition        ; no
243
 
256 diamond 244
        mov     edx, eax
245
        add     edx, [ebx+0x1be+8+16+16+16]
246
        push    edx
247
        add     edx, [ebx+0x1be+12+16+16+16]
248
        dec     edx
249
        mov     [PARTITION_END], edx
250
        mov     dl, [ebx+0x1be+4+16+16+16]
251
        mov     [fs_type], dl
252
        pop     edx
65 mario79 253
 
254
next_partition:
255
    push  eax
256
    mov   al,[ebx+0x1be+4]   ; get extended partition type
257
    call  scan_extended_types
258
    pop   eax
259
    jnz   next_partition_1
260
 
261
    mov   eax,[ebx+0x1be+8]     ; add relative start
262
    test  eax,eax               ; is there extended partition?
263
    jnz   new_partition         ; yes. read it
264
 
265
next_partition_1:
266
    push  eax
267
    mov   al,[ebx+0x1be+4+16]   ; get extended partition type
268
    call  scan_extended_types
269
    pop   eax
270
    jnz   next_partition_2
271
 
272
    mov   eax,[ebx+0x1be+8+16]     ; add relative start
273
    test  eax,eax               ; is there extended partition?
274
    jnz   new_partition         ; yes. read it
275
 
276
next_partition_2:
277
    push  eax
278
    mov   al,[ebx+0x1be+4+16+16]   ; get extended partition type
279
    call  scan_extended_types
280
    pop   eax
281
    jnz   next_partition_3
282
 
283
    mov   eax,[ebx+0x1be+8+16+16]     ; add relative start
284
    test  eax,eax               ; is there extended partition?
285
    jnz   new_partition         ; yes. read it
286
 
287
next_partition_3:
288
    push  eax
289
    mov   al,[ebx+0x1be+4+16+16+16]   ; get extended partition type
290
    call  scan_extended_types
291
    pop   eax
292
    jnz   end_partition_chain   ; no. end chain
293
 
294
    mov   eax,[ebx+0x1be+8+16+16+16]  ; get start of extended partition
295
    test  eax,eax               ; is there extended partition?
296
    jnz   new_partition         ; yes. read it
297
 
298
end_partition_chain:
299
    mov   [partition_count],ecx
300
 
301
    cmp   edx,-1                ; found wanted partition?
302
    jnz   hd_and_partition_ok   ; yes. install it
303
    jmp   problem_partition_or_fat
304
 
305
scan_partition_types:
306
    push  ecx
307
    mov   edi,partition_types
308
    mov   ecx,partition_types_end-partition_types
309
    cld
310
    repne scasb                 ; is partition type ok?
311
    pop   ecx
312
    ret
313
 
314
scan_extended_types:
315
    push  ecx
316
    mov   edi,extended_types
317
    mov   ecx,extended_types_end-extended_types
318
    cld
319
    repne scasb                 ; is it extended partition?
320
    pop   ecx
321
    ret
322
 
323
problem_fat_dec_count:          ; bootsector is missing or another problem
324
    dec   [partition_count]     ; remove it from partition_count
325
 
326
problem_partition_or_fat:
327
    popad
328
 
329
problem_hd:
256 diamond 330
    mov   [fs_type],0
321 diamond 331
    call  free_hd_channel
65 mario79 332
    mov   [hd1_status],0        ; free
333
    mov   [problem_partition],1
334
    ret
335
 
336
hd_and_partition_ok:
337
    mov   eax,edx
338
    mov   [PARTITION_START],eax
256 diamond 339
        mov     edx, [PARTITION_END]
340
        sub     edx, eax
341
        inc     edx     ; edx = length of partition
65 mario79 342
 
256 diamond 343
;    mov   [hd_setup],1
65 mario79 344
    mov   ebx,buffer
345
    call  hd_read               ; read boot sector of partition
256 diamond 346
        cmp     [hd_error], 0
347
        jz      boot_read_ok
348
        cmp     [fs_type], 7
349
        jnz     problem_fat_dec_count
350
; NTFS duplicates bootsector:
351
; NT4/2k/XP+ saves bootsector copy in the end of disk
352
; NT 3.51 saves bootsector copy in the middle of disk
353
        and     [hd_error], 0
354
        mov     eax, [PARTITION_END]
355
        call    hd_read
356
        cmp     [hd_error], 0
357
        jnz     @f
358
        call    ntfs_test_bootsec
359
        jnc     boot_read_ok
360
@@:
361
        and     [hd_error], 0
362
        mov     eax, edx
363
        shr     eax, 1
364
        add     eax, [PARTITION_START]
365
        call    hd_read
366
        cmp     [hd_error], 0
367
        jnz     problem_fat_dec_count   ; не судьба...
368
boot_read_ok:
369
;        mov     [hd_setup], 0
370
; if we are running on NTFS, check bootsector
371
        cmp     [fs_type], 7
372
        jz      ntfs_setup
74 mario79 373
 
65 mario79 374
    cmp   word [ebx+0x1fe],0xaa55 ; is it valid boot sector?
375
    jnz   problem_fat_dec_count
376
 
377
    movzx eax,word [ebx+0xe]    ; sectors reserved
378
    add   eax,[PARTITION_START]
379
    mov   [FAT_START],eax       ; fat_start = partition_start + reserved
380
 
381
    movzx eax,byte [ebx+0xd]    ; sectors per cluster
382
    mov   [SECTORS_PER_CLUSTER],eax
383
 
384
    movzx ecx,word [ebx+0xb]    ; bytes per sector
385
    mov   [BYTES_PER_SECTOR],ecx
386
 
387
    movzx eax,word [ebx+0x11]   ; count of rootdir entries (=0 fat32)
388
    mov   edx,32
389
    mul   edx
390
    dec   ecx
391
    add   eax,ecx               ; round up if not equal count
392
    inc   ecx                   ; bytes per sector
393
    div   ecx
394
    mov   [ROOT_SECTORS],eax    ; count of rootdir sectors
395
 
396
    movzx eax,word [ebx+0x16]   ; sectors per fat <65536
397
    test  eax,eax
398
    jnz   fat16_fatsize
399
    mov   eax,[ebx+0x24]        ; sectors per fat
400
  fat16_fatsize:
401
    mov   [SECTORS_PER_FAT],eax
402
 
403
    movzx eax,byte [ebx+0x10]   ; number of fats
404
    test  eax,eax               ; if 0 it's not fat partition
405
    jz    problem_fat_dec_count
406
    mov   [NUMBER_OF_FATS],eax
407
    imul  eax,[SECTORS_PER_FAT]
408
    add   eax,[FAT_START]
409
    mov   [ROOT_START],eax      ; rootdir = fat_start + fat_size * fat_count
410
    add   eax,[ROOT_SECTORS]    ; rootdir sectors should be 0 on fat32
411
    mov   [DATA_START],eax      ; data area = rootdir + rootdir_size
412
 
413
    movzx eax,word [ebx+0x13]   ; total sector count <65536
414
    test  eax,eax
415
    jnz   fat16_total
416
    mov   eax,[ebx+0x20]        ; total sector count
417
  fat16_total:
418
    add   eax,[PARTITION_START]
419
    dec   eax
420
    mov   [PARTITION_END],eax
421
    inc   eax
422
    sub   eax,[DATA_START]      ; eax = count of data sectors
423
    xor   edx,edx
424
    div   dword [SECTORS_PER_CLUSTER]
425
    inc   eax
426
    mov   [LAST_CLUSTER],eax
427
    dec   eax                   ; cluster count
649 diamond 428
    mov   [fatStartScan],2
65 mario79 429
 
430
    ; limits by Microsoft Hardware White Paper v1.03
431
    cmp   eax,4085              ; 0xff5
432
    jb    problem_fat_dec_count ; fat12 not supported
433
    cmp   eax,65525             ; 0xfff5
434
    jb    fat16_partition
435
 
436
fat32_partition:
437
    mov   eax,[ebx+0x2c]        ; rootdir cluster
438
    mov   [ROOT_CLUSTER],eax
439
    movzx eax,word [ebx+0x30]   ; fs info sector
440
    add   eax,[PARTITION_START]
441
    mov   [ADR_FSINFO],eax
649 diamond 442
    call  hd_read
443
    mov   eax,[ebx+0x1ec]
444
    cmp   eax,-1
445
    jz    @f
446
    mov   [fatStartScan],eax
447
@@:
65 mario79 448
 
449
    popad
450
 
451
    mov   [fatRESERVED],0x0FFFFFF6
452
    mov   [fatBAD],0x0FFFFFF7
453
    mov   [fatEND],0x0FFFFFF8
454
    mov   [fatMASK],0x0FFFFFFF
256 diamond 455
    mov   [fs_type],32         ; Fat32
321 diamond 456
    call  free_hd_channel
65 mario79 457
    mov   [hd1_status],0        ; free
458
    ret
459
 
460
fat16_partition:
461
    xor   eax,eax
462
    mov   [ROOT_CLUSTER],eax
463
 
464
    popad
465
 
466
    mov   [fatRESERVED],0x0000FFF6
467
    mov   [fatBAD],0x0000FFF7
468
    mov   [fatEND],0x0000FFF8
469
    mov   [fatMASK],0x0000FFFF
256 diamond 470
    mov   [fs_type],16         ; Fat16
321 diamond 471
    call  free_hd_channel
65 mario79 472
    mov   [hd1_status],0        ; free
473
    ret