Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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