Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
65 mario79 1
;*************************************************************
74 mario79 2
;* 29.04.2006 Elimination of hangup after the                *
3
;*             expiration hd_wait_timeout -  Mario79         *
65 mario79 4
;* 28.01.2006 find all Fat16/32 partition in all input point *
5
;*            to MBR - Mario79                               *
6
;*************************************************************
7
 
8
align 4
9
;******************************************************
10
; Please do not change this place - variables  in text
11
; Mario79
12
; START place
13
;******************************************************
14
PARTITION_START      dd 0x3f
15
PARTITION_END        dd 0
16
SECTORS_PER_FAT      dd 0x1f3a
17
NUMBER_OF_FATS       dd 0x2
18
SECTORS_PER_CLUSTER  dd 0x8
19
BYTES_PER_SECTOR     dd 0x200   ; Note: if BPS <> 512 need lots of changes
20
ROOT_CLUSTER         dd 2       ; first rootdir cluster
21
FAT_START            dd 0       ; start of fat table
22
ROOT_START           dd 0       ; start of rootdir (only fat16)
23
ROOT_SECTORS         dd 0       ; count of rootdir sectors (only fat16)
24
DATA_START           dd 0       ; start of data area (=first cluster 2)
25
LAST_CLUSTER         dd 0       ; last availabe cluster
26
ADR_FSINFO           dd 0       ; used only by fat32
27
 
28
fatRESERVED          dd 0x0FFFFFF6
29
fatBAD               dd 0x0FFFFFF7
30
fatEND               dd 0x0FFFFFF8
31
fatMASK              dd 0x0FFFFFFF
32
 
33
fat_type             db 0       ; 0=none, 16=fat16, 32=fat32
34
;***************************************************************************
35
; End place
36
; Mario79
37
;***************************************************************************
38
 
39
iglobal
40
  partition_types:              ; list of fat16/32 partitions
41
    db    0x04                  ; DOS: fat16 <32M
42
    db    0x06                  ; DOS: fat16 >32M
43
    db    0x0b                  ; WIN95: fat32
44
    db    0x0c                  ; WIN95: fat32, LBA-mapped
45
    db    0x0e                  ; WIN95: fat16, LBA-mapped
46
    db    0x14                  ; Hidden DOS: fat16 <32M
47
    db    0x16                  ; Hidden DOS: fat16 >32M
48
    db    0x1b                  ; Hidden WIN95: fat32
49
    db    0x1c                  ; Hidden WIN95: fat32, LBA-mapped
50
    db    0x1e                  ; Hidden WIN95: fat16, LBA-mapped
51
    db    0xc4                  ; DRDOS/secured: fat16 <32M
52
    db    0xc6                  ; DRDOS/secured: fat16 >32M
53
    db    0xcb                  ; DRDOS/secured: fat32
54
    db    0xcc                  ; DRDOS/secured: fat32, LBA-mapped
55
    db    0xce                  ; DRDOS/secured: fat16, LBA-mapped
56
    db    0xd4                  ; Old Multiuser DOS secured: fat16 <32M
57
    db    0xd6                  ; Old Multiuser DOS secured: fat16 >32M
58
  partition_types_end:
59
 
60
 
61
  extended_types:               ; list of extended partitions
62
    db    0x05                  ; DOS: extended partition
63
    db    0x0f                  ; WIN95: extended partition, LBA-mapped
64
    db    0xc5                  ; DRDOS/secured: extended partition
65
    db    0xd5                  ; Old Multiuser DOS secured: extended partition
66
  extended_types_end:
67
 
68
endg
69
 
70
; Partition chain used:
71
; MBR        ;   PARTITION2 ;   PARTITION3 ;   PARTITION4
72
;==========================================================
73
; fat16/32   +-- fat16/32   +-- fat16/32   +-- fat16/32   +--
74
; extended --+   extended --+   extended --+   extended --+
75
; 0              0              0              0
76
; 0              0              0              0
77
; Notes:
78
; - extended partition need to be in second entry on table
79
; - it will skip over removed partitions
80
 
81
set_FAT32_variables:
82
    mov   [0xfe10],dword 0      ; entries in hd cache
83
    mov   [problem_partition],0
84
    call  reserve_hd1
85
    call  clear_hd_cache
86
 
87
    cmp   dword [hdpos],0
88
    je    problem_hd
89
 
90
    pushad
91
    xor   ecx,ecx               ; partition count
92
    mov   edx,-1                ; flag for partition
93
    xor   eax,eax               ; read MBR
94
    xor   ebp,ebp               ; extended partition start
95
 
96
new_partition:
97
    test  ebp,ebp               ; is there extended partition?
98
    jnz   extended_already_set  ; yes
99
    xchg  ebp,eax               ; no. set it now
100
 
101
extended_already_set:
102
    add   eax,ebp               ; mbr=mbr+0, ext_part=ext_start+relat_start
103
    mov   ebx,buffer
104
    call  hd_read
74 mario79 105
    cmp  [hd_error],0
106
    jne  problem_hd
65 mario79 107
 
108
    cmp   word [ebx+0x1fe],0xaa55 ; is it valid boot sector?
109
    jnz   end_partition_chain
110
    cmp   dword [ebx+0x1be+0xc],0 ; skip over empty partition
111
    jz    next_partition
112
 
113
    push  eax
114
    mov   al,[ebx+0x1be+4]      ; get primary partition type
115
    call  scan_partition_types
116
    pop   eax
117
    jnz   next_primary_partition        ; no. skip over
118
 
119
    inc   ecx
120
    cmp   ecx,[fat32part]       ; is it wanted partition?
121
    jnz   next_primary_partition        ; no
122
 
123
    mov   edx,eax               ; start sector
124
    add   edx,[ebx+0x1be+8]     ; add relative start
125
 
126
next_primary_partition:
127
    push  eax
128
    mov   al,[ebx+0x1be+4+16]      ; get primary partition type
129
    call  scan_partition_types
130
    pop   eax
131
    jnz   next_primary_partition_1        ; no. skip over
132
 
133
    inc   ecx
134
    cmp   ecx,[fat32part]       ; is it wanted partition?
135
    jnz   next_primary_partition_1        ; no
136
 
137
    mov   edx,eax               ; start sector
138
    add   edx,[ebx+0x1be+8+16]     ; add relative start
139
 
140
next_primary_partition_1:
141
    push  eax
142
    mov   al,[ebx+0x1be+4+16+16]      ; get primary partition type
143
    call  scan_partition_types
144
    pop   eax
145
    jnz   next_primary_partition_2        ; no. skip over
146
 
147
    inc   ecx
148
    cmp   ecx,[fat32part]       ; is it wanted partition?
149
    jnz   next_primary_partition_2        ; no
150
 
151
    mov   edx,eax               ; start sector
152
    add   edx,[ebx+0x1be+8+16+16]     ; add relative start
153
 
154
next_primary_partition_2:
155
    push  eax
156
    mov   al,[ebx+0x1be+4+16+16+16]      ; get primary partition type
157
    call  scan_partition_types
158
    pop   eax
159
    jnz   next_partition        ; no. skip over
160
 
161
    inc   ecx
162
    cmp   ecx,[fat32part]       ; is it wanted partition?
163
    jnz   next_partition        ; no
164
 
165
    mov   edx,eax               ; start sector
166
    add   edx,[ebx+0x1be+8+16+16+16]     ; add relative start
167
 
168
next_partition:
169
    push  eax
170
    mov   al,[ebx+0x1be+4]   ; get extended partition type
171
    call  scan_extended_types
172
    pop   eax
173
    jnz   next_partition_1
174
 
175
    mov   eax,[ebx+0x1be+8]     ; add relative start
176
    test  eax,eax               ; is there extended partition?
177
    jnz   new_partition         ; yes. read it
178
 
179
next_partition_1:
180
    push  eax
181
    mov   al,[ebx+0x1be+4+16]   ; get extended partition type
182
    call  scan_extended_types
183
    pop   eax
184
    jnz   next_partition_2
185
 
186
    mov   eax,[ebx+0x1be+8+16]     ; add relative start
187
    test  eax,eax               ; is there extended partition?
188
    jnz   new_partition         ; yes. read it
189
 
190
next_partition_2:
191
    push  eax
192
    mov   al,[ebx+0x1be+4+16+16]   ; get extended partition type
193
    call  scan_extended_types
194
    pop   eax
195
    jnz   next_partition_3
196
 
197
    mov   eax,[ebx+0x1be+8+16+16]     ; add relative start
198
    test  eax,eax               ; is there extended partition?
199
    jnz   new_partition         ; yes. read it
200
 
201
next_partition_3:
202
    push  eax
203
    mov   al,[ebx+0x1be+4+16+16+16]   ; get extended partition type
204
    call  scan_extended_types
205
    pop   eax
206
    jnz   end_partition_chain   ; no. end chain
207
 
208
    mov   eax,[ebx+0x1be+8+16+16+16]  ; get start of extended partition
209
    test  eax,eax               ; is there extended partition?
210
    jnz   new_partition         ; yes. read it
211
 
212
end_partition_chain:
213
    mov   [partition_count],ecx
214
 
215
    cmp   edx,-1                ; found wanted partition?
216
    jnz   hd_and_partition_ok   ; yes. install it
217
    jmp   problem_partition_or_fat
218
 
219
scan_partition_types:
220
    push  ecx
221
    mov   edi,partition_types
222
    mov   ecx,partition_types_end-partition_types
223
    cld
224
    repne scasb                 ; is partition type ok?
225
    pop   ecx
226
    ret
227
 
228
scan_extended_types:
229
    push  ecx
230
    mov   edi,extended_types
231
    mov   ecx,extended_types_end-extended_types
232
    cld
233
    repne scasb                 ; is it extended partition?
234
    pop   ecx
235
    ret
236
 
237
problem_fat_dec_count:          ; bootsector is missing or another problem
238
    dec   [partition_count]     ; remove it from partition_count
239
 
240
problem_partition_or_fat:
241
    popad
242
 
243
problem_hd:
244
    mov   [fat_type],0
245
    mov   [hd1_status],0        ; free
246
    mov   [problem_partition],1
247
    ret
248
 
249
hd_and_partition_ok:
250
    mov   eax,edx
251
    mov   [PARTITION_START],eax
252
 
253
    mov   [hd_setup],1
254
    mov   ebx,buffer
255
    call  hd_read               ; read boot sector of partition
74 mario79 256
    cmp  [hd_error],0
257
    jne  problem_fat_dec_count
258
 
65 mario79 259
    mov   [hd_setup],0
260
 
261
    cmp   word [ebx+0x1fe],0xaa55 ; is it valid boot sector?
262
    jnz   problem_fat_dec_count
263
 
264
    movzx eax,word [ebx+0xe]    ; sectors reserved
265
    add   eax,[PARTITION_START]
266
    mov   [FAT_START],eax       ; fat_start = partition_start + reserved
267
 
268
    movzx eax,byte [ebx+0xd]    ; sectors per cluster
269
    mov   [SECTORS_PER_CLUSTER],eax
270
 
271
    movzx ecx,word [ebx+0xb]    ; bytes per sector
272
    mov   [BYTES_PER_SECTOR],ecx
273
 
274
    movzx eax,word [ebx+0x11]   ; count of rootdir entries (=0 fat32)
275
    mov   edx,32
276
    mul   edx
277
    dec   ecx
278
    add   eax,ecx               ; round up if not equal count
279
    inc   ecx                   ; bytes per sector
280
    div   ecx
281
    mov   [ROOT_SECTORS],eax    ; count of rootdir sectors
282
 
283
    movzx eax,word [ebx+0x16]   ; sectors per fat <65536
284
    test  eax,eax
285
    jnz   fat16_fatsize
286
    mov   eax,[ebx+0x24]        ; sectors per fat
287
  fat16_fatsize:
288
    mov   [SECTORS_PER_FAT],eax
289
 
290
    movzx eax,byte [ebx+0x10]   ; number of fats
291
    test  eax,eax               ; if 0 it's not fat partition
292
    jz    problem_fat_dec_count
293
    mov   [NUMBER_OF_FATS],eax
294
    imul  eax,[SECTORS_PER_FAT]
295
    add   eax,[FAT_START]
296
    mov   [ROOT_START],eax      ; rootdir = fat_start + fat_size * fat_count
297
    add   eax,[ROOT_SECTORS]    ; rootdir sectors should be 0 on fat32
298
    mov   [DATA_START],eax      ; data area = rootdir + rootdir_size
299
 
300
    movzx eax,word [ebx+0x13]   ; total sector count <65536
301
    test  eax,eax
302
    jnz   fat16_total
303
    mov   eax,[ebx+0x20]        ; total sector count
304
  fat16_total:
305
    add   eax,[PARTITION_START]
306
    dec   eax
307
    mov   [PARTITION_END],eax
308
    inc   eax
309
    sub   eax,[DATA_START]      ; eax = count of data sectors
310
    xor   edx,edx
311
    div   dword [SECTORS_PER_CLUSTER]
312
    inc   eax
313
    mov   [LAST_CLUSTER],eax
314
    dec   eax                   ; cluster count
315
 
316
    ; limits by Microsoft Hardware White Paper v1.03
317
    cmp   eax,4085              ; 0xff5
318
    jb    problem_fat_dec_count ; fat12 not supported
319
    cmp   eax,65525             ; 0xfff5
320
    jb    fat16_partition
321
 
322
fat32_partition:
323
    mov   eax,[ebx+0x2c]        ; rootdir cluster
324
    mov   [ROOT_CLUSTER],eax
325
    movzx eax,word [ebx+0x30]   ; fs info sector
326
    add   eax,[PARTITION_START]
327
    mov   [ADR_FSINFO],eax
328
 
329
    popad
330
 
331
    mov   [fatRESERVED],0x0FFFFFF6
332
    mov   [fatBAD],0x0FFFFFF7
333
    mov   [fatEND],0x0FFFFFF8
334
    mov   [fatMASK],0x0FFFFFFF
335
    mov   [fat_type],32         ; Fat32
336
    mov   [hd1_status],0        ; free
337
    ret
338
 
339
fat16_partition:
340
    xor   eax,eax
341
    mov   [ROOT_CLUSTER],eax
342
 
343
    popad
344
 
345
    mov   [fatRESERVED],0x0000FFF6
346
    mov   [fatBAD],0x0000FFF7
347
    mov   [fatEND],0x0000FFF8
348
    mov   [fatMASK],0x0000FFFF
349
    mov   [fat_type],16         ; Fat16
350
    mov   [hd1_status],0        ; free
351
    ret