Subversion Repositories Kolibri OS

Rev

Rev 6850 | Rev 6868 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6850 Rev 6867
Line 3... Line 3...
3
;; Copyright (C) KolibriOS team 2004-2016. All rights reserved. ;;
3
;; Copyright (C) KolibriOS team 2004-2016. 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 7... Line 7...
7
 
7
 
Line 8... Line 8...
8
$Revision: 6850 $
8
$Revision: 6867 $
9
 
9
 
10
; FAT external functions
10
; FAT external functions
11
;   in:
11
;   in:
Line 101... Line 101...
101
        call    free
101
        call    free
102
        pop     ebp
102
        pop     ebp
103
fat_create_partition.return0:
103
fat_create_partition.return0:
104
        xor     eax, eax
104
        xor     eax, eax
105
        ret
105
        ret
-
 
106
 
106
fat_create_partition:
107
fat_create_partition:
107
; sector size must be 512
-
 
108
        cmp     dword [esi+DISK.MediaInfo.SectorSize], 512
108
        cmp     dword [esi+DISK.MediaInfo.SectorSize], 512
109
        jnz     .return0
109
        jnz     .return0
110
; bootsector must have been successfully read
110
; bootsector must have been successfully read
111
        cmp     dword [esp+4], 0
111
        cmp     dword [esp+4], 0
112
        jnz     .return0
112
        jnz     .return0
Line 143... Line 143...
143
        mov     [eax+FAT.FSUserFunctions], fat_user_functions
143
        mov     [eax+FAT.FSUserFunctions], fat_user_functions
144
        or      [eax+FAT.fat_in_cache], -1
144
        or      [eax+FAT.fat_in_cache], -1
145
        mov     [eax+FAT.fat_change], 0
145
        mov     [eax+FAT.fat_change], 0
146
        push    ebp
146
        push    ebp
147
        mov     ebp, eax
147
        mov     ebp, eax
148
 
-
 
149
        lea     ecx, [ebp+FAT.Lock]
148
        lea     ecx, [ebp+FAT.Lock]
150
        call    mutex_init
149
        call    mutex_init
151
 
-
 
152
        movzx   eax, word [ebx+0xe]     ; sectors reserved
150
        movzx   eax, word [ebx+0xe]     ; sectors reserved
153
        mov     [ebp+FAT.FAT_START], eax
151
        mov     [ebp+FAT.FAT_START], eax
154
 
-
 
155
        movzx   eax, byte [ebx+0xd]     ; sectors per cluster
152
        movzx   eax, byte [ebx+0xd]     ; sectors per cluster
156
        mov     [ebp+FAT.SECTORS_PER_CLUSTER], eax
153
        mov     [ebp+FAT.SECTORS_PER_CLUSTER], eax
157
 
-
 
158
        movzx   ecx, word [ebx+0xb]     ; bytes per sector
154
        movzx   ecx, word [ebx+0xb]     ; bytes per sector
159
        mov     [ebp+FAT.BYTES_PER_SECTOR], ecx
155
        mov     [ebp+FAT.BYTES_PER_SECTOR], ecx
160
 
-
 
161
        movzx   eax, word [ebx+0x11]    ; count of rootdir entries (=0 fat32)
156
        movzx   eax, word [ebx+0x11]    ; count of rootdir entries (=0 fat32)
162
        shl     eax, 5                  ; mul 32
157
        shl     eax, 5                  ; mul 32
163
        dec     ecx
158
        dec     ecx
164
        add     eax, ecx                ; round up if not equal count
159
        add     eax, ecx                ; round up if not equal count
165
        inc     ecx                     ; bytes per sector
160
        inc     ecx                     ; bytes per sector
166
        xor     edx, edx
161
        xor     edx, edx
167
        div     ecx
162
        div     ecx
168
        mov     [ebp+FAT.ROOT_SECTORS], eax     ; count of rootdir sectors
163
        mov     [ebp+FAT.ROOT_SECTORS], eax     ; count of rootdir sectors
169
 
-
 
170
        movzx   eax, word [ebx+0x16]    ; sectors per fat <65536
164
        movzx   eax, word [ebx+0x16]    ; sectors per fat <65536
171
        test    eax, eax
165
        test    eax, eax
172
        jnz     @f
166
        jnz     @f
173
        mov     eax, [ebx+0x24]         ; sectors per fat
167
        mov     eax, [ebx+0x24]         ; sectors per fat
174
@@:
168
@@:
175
        mov     [ebp+FAT.SECTORS_PER_FAT], eax
169
        mov     [ebp+FAT.SECTORS_PER_FAT], eax
176
 
-
 
177
        movzx   eax, byte [ebx+0x10]    ; number of fats
170
        movzx   eax, byte [ebx+0x10]    ; number of fats
178
        mov     [ebp+FAT.NUMBER_OF_FATS], eax
171
        mov     [ebp+FAT.NUMBER_OF_FATS], eax
179
        mul     [ebp+FAT.SECTORS_PER_FAT]
172
        mul     [ebp+FAT.SECTORS_PER_FAT]
180
        test    edx, edx
173
        test    edx, edx
181
        jnz     .free_return0
174
        jnz     .free_return0
Line 183... Line 176...
183
        jc      .free_return0
176
        jc      .free_return0
184
        mov     [ebp+FAT.ROOT_START], eax       ; rootdir = fat_start + fat_size * fat_count
177
        mov     [ebp+FAT.ROOT_START], eax       ; rootdir = fat_start + fat_size * fat_count
185
        add     eax, [ebp+FAT.ROOT_SECTORS]     ; rootdir sectors should be 0 on fat32
178
        add     eax, [ebp+FAT.ROOT_SECTORS]     ; rootdir sectors should be 0 on fat32
186
        jc      .free_return0
179
        jc      .free_return0
187
        mov     [ebp+FAT.DATA_START], eax       ; data area = rootdir + rootdir_size
180
        mov     [ebp+FAT.DATA_START], eax       ; data area = rootdir + rootdir_size
188
 
-
 
189
        movzx   eax, word [ebx+0x13]    ; total sector count <65536
181
        movzx   eax, word [ebx+0x13]    ; total sector count <65536
190
        test    eax, eax
182
        test    eax, eax
191
        jnz     @f
183
        jnz     @f
192
        mov     eax, [ebx+0x20]         ; total sector count
184
        mov     eax, [ebx+0x20]         ; total sector count
193
@@:
185
@@:
194
; total sector count must not exceed partition size
-
 
195
        cmp     dword [ebp+FAT.Length+4], 0
186
        cmp     dword [ebp+FAT.Length+4], 0
196
        jnz     @f
187
        jnz     @f
197
        cmp     eax, dword [ebp+FAT.Length]
188
        cmp     eax, dword [ebp+FAT.Length]
198
        ja      .free_return0
189
        ja      .free_return0
199
@@:
190
@@:
Line 206... Line 197...
206
        inc     eax
197
        inc     eax
207
        mov     [ebp+FAT.LAST_CLUSTER], eax
198
        mov     [ebp+FAT.LAST_CLUSTER], eax
208
        dec     eax                     ; cluster count
199
        dec     eax                     ; cluster count
209
        jz      .free_return0
200
        jz      .free_return0
210
        mov     [ebp+FAT.fatStartScan], 2
201
        mov     [ebp+FAT.fatStartScan], 2
211
 
-
 
212
        ; limits by Microsoft Hardware White Paper v1.03
202
; limits by Microsoft Hardware White Paper v1.03
213
        cmp     eax, 4085               ; 0xff5
203
        cmp     eax, 0xff5
214
        jb      .fat12
204
        jb      .fat12
215
        cmp     eax, 65525              ; 0xfff5
205
        cmp     eax, 0xfff5
216
        jb      .fat16
206
        jb      .fat16
217
.fat32:
207
.fat32:
218
        mov     eax, [ebx+0x2c]         ; rootdir cluster
208
        mov     eax, [ebx+0x2c]         ; rootdir cluster
219
        mov     [ebp+FAT.ROOT_CLUSTER], eax
209
        mov     [ebp+FAT.ROOT_CLUSTER], eax
220
        movzx   eax, word [ebx+0x30]
210
        movzx   eax, word [ebx+0x30]
Line 244... Line 234...
244
        jz      .free_return0
234
        jz      .free_return0
245
        mov     [ebp+FAT.fat_cache_ptr], eax
235
        mov     [ebp+FAT.fat_cache_ptr], eax
246
        mov     eax, ebp
236
        mov     eax, ebp
247
        pop     ebp
237
        pop     ebp
248
        ret
238
        ret
-
 
239
 
249
.fat16:
240
.fat16:
250
        and     [ebp+FAT.ROOT_CLUSTER], 0
241
        and     [ebp+FAT.ROOT_CLUSTER], 0
251
        mov     [ebp+FAT.fatRESERVED], 0x0000FFF6
242
        mov     [ebp+FAT.fatRESERVED], 0x0000FFF6
252
        mov     [ebp+FAT.fatBAD], 0x0000FFF7
243
        mov     [ebp+FAT.fatBAD], 0x0000FFF7
253
        mov     [ebp+FAT.fatEND], 0x0000FFF8
244
        mov     [ebp+FAT.fatEND], 0x0000FFF8
254
        mov     [ebp+FAT.fatMASK], 0x0000FFFF
245
        mov     [ebp+FAT.fatMASK], 0x0000FFFF
255
        mov     al, 16
246
        mov     al, 16
256
        jmp     .fat_not_12_finalize
247
        jmp     .fat_not_12_finalize
-
 
248
 
257
.fat12:
249
.fat12:
258
        and     [ebp+FAT.ROOT_CLUSTER], 0
250
        and     [ebp+FAT.ROOT_CLUSTER], 0
259
        mov     [ebp+FAT.fatRESERVED], 0xFF6
251
        mov     [ebp+FAT.fatRESERVED], 0xFF6
260
        mov     [ebp+FAT.fatBAD], 0xFF7
252
        mov     [ebp+FAT.fatBAD], 0xFF7
261
        mov     [ebp+FAT.fatEND], 0xFFF
253
        mov     [ebp+FAT.fatEND], 0xFFF
262
        mov     [ebp+FAT.fatMASK], 0xFFF
254
        mov     [ebp+FAT.fatMASK], 0xFFF
263
        mov     al, 12
255
        mov     al, 12
264
        mov     [ebp+FAT.fs_type], al
256
        mov     [ebp+FAT.fs_type], al
265
; For FAT12, allocate&read data for entire table:
257
; For FAT12, allocate&read data for entire table:
266
; calculate A = ALIGN_UP(NUM_CLUSTERS, 8),
258
; calculate A = ALIGN_UP(NUM_CLUSTERS, 8),
267
; calculatefatchain/restorefatchain will process A items,
-
 
268
; allocate ALIGN_UP(A*3/2, 512) bytes for FAT table plus A*2 bytes for unpacked data.
259
; allocate ALIGN_UP(A*3/2, 512) bytes for FAT table plus A*2 bytes for unpacked data.
269
        mov     eax, [ebp+FAT.LAST_CLUSTER]
260
        mov     eax, [ebp+FAT.LAST_CLUSTER]
270
        and     eax, not 7
261
        and     eax, not 7
271
        add     eax, 8
262
        add     eax, 8
272
        mov     edx, eax
263
        mov     edx, eax
Line 277... Line 268...
277
        lea     eax, [eax+edx*2]
268
        lea     eax, [eax+edx*2]
278
        call    malloc
269
        call    malloc
279
        test    eax, eax
270
        test    eax, eax
280
        jz      .free_return0
271
        jz      .free_return0
281
; Read ALIGN_UP(NUM_CLUSTERS*3/2, 512) bytes.
272
; Read ALIGN_UP(NUM_CLUSTERS*3/2, 512) bytes.
282
; Note that this can be less than allocated, this is ok,
-
 
283
; overallocation simplifies calculatefatchain/restorefatchain.
-
 
284
        push    ebx
273
        push    ebx
285
        mov     [ebp+FAT.fat_cache_ptr], eax
274
        mov     [ebp+FAT.fat_cache_ptr], eax
286
        mov     edx, [ebp+FAT.LAST_CLUSTER]
275
        mov     edx, [ebp+FAT.LAST_CLUSTER]
287
        lea     edx, [(edx+1)*3 + 512*2-1]
276
        lea     edx, [(edx+1)*3 + 512*2-1]
288
        shr     edx, 10
277
        shr     edx, 10
Line 293... Line 282...
293
        add     eax, [ebp+FAT.FAT_START]
282
        add     eax, [ebp+FAT.FAT_START]
294
        call    fs_read32_sys
283
        call    fs_read32_sys
295
        test    eax, eax
284
        test    eax, eax
296
        pop     eax
285
        pop     eax
297
        jz      @f
286
        jz      @f
298
        dbgstr 'Failed to read FAT table'
-
 
299
        mov     eax, [ebp+FAT.fat_cache_ptr]
287
        mov     eax, [ebp+FAT.fat_cache_ptr]
300
        call    free
288
        call    free
301
        pop     ebx
289
        pop     ebx
302
        jmp     .free_return0
290
        jmp     .free_return0
-
 
291
 
303
@@:
292
@@:
304
        add     ebx, 512
293
        add     ebx, 512
305
        inc     eax
294
        inc     eax
306
        cmp     eax, edx
295
        cmp     eax, edx
307
        jb      .read_fat
296
        jb      .read_fat
308
        mov     [ebp+FAT.fat12_unpacked_ptr], ebx
297
        mov     [ebp+FAT.fat12_unpacked_ptr], ebx
309
        call    calculatefatchain
-
 
310
        pop     ebx
-
 
311
        mov     eax, ebp
-
 
312
        pop     ebp
-
 
313
        ret
-
 
314
 
-
 
315
fat_free:
-
 
316
        push    eax
-
 
317
        mov     eax, [eax+FAT.fat_cache_ptr]
-
 
318
        call    free
-
 
319
        pop     eax
-
 
320
        jmp     free
-
 
321
 
-
 
322
calculatefatchain:
-
 
323
 
-
 
324
        pushad
298
        pushad
325
 
-
 
326
        mov     esi, [ebp+FAT.fat_cache_ptr]
299
        mov     esi, [ebp+FAT.fat_cache_ptr]
327
        mov     edi, [ebp+FAT.fat12_unpacked_ptr]
300
        mov     edi, [ebp+FAT.fat12_unpacked_ptr]
328
 
-
 
329
        mov     edx, [ebp+FAT.LAST_CLUSTER]
301
        mov     edx, [ebp+FAT.LAST_CLUSTER]
330
        and     edx, not 7
302
        and     edx, not 7
331
        lea     edx, [edi+(edx+8)*2]
303
        lea     edx, [edi+(edx+8)*2]
332
        push    edx
304
        push    edx
333
 
-
 
334
 fcnew:
305
@@:
335
        mov     eax, dword [esi]
306
        mov     eax, dword [esi]
336
        mov     ebx, dword [esi+4]
307
        mov     ebx, dword [esi+4]
337
        mov     ecx, dword [esi+8]
308
        mov     ecx, dword [esi+8]
338
        mov     edx, ecx
309
        mov     edx, ecx
339
        shr     edx, 4;8 ok
310
        shr     edx, 4
340
        shr     dx, 4;7 ok
311
        shr     dx, 4
341
        xor     ch, ch
312
        xor     ch, ch
342
        shld    ecx, ebx, 20;6 ok
313
        shld    ecx, ebx, 20
343
        shr     cx, 4;5 ok
314
        shr     cx, 4
344
        shld    ebx, eax, 12
315
        shld    ebx, eax, 12
345
        and     ebx, 0x0fffffff;4 ok
316
        and     ebx, 0x0fffffff
346
        shr     bx, 4;3 ok
317
        shr     bx, 4
347
        shl     eax, 4
318
        shl     eax, 4
348
        and     eax, 0x0fffffff;2 ok
319
        and     eax, 0x0fffffff
349
        shr     ax, 4;1 ok
320
        shr     ax, 4
350
        mov     dword [edi], eax
321
        mov     dword [edi], eax
351
        mov     dword [edi+4], ebx
322
        mov     dword [edi+4], ebx
352
        mov     dword [edi+8], ecx
323
        mov     dword [edi+8], ecx
353
        mov     dword [edi+12], edx
324
        mov     dword [edi+12], edx
354
        add     edi, 16
325
        add     edi, 16
355
        add     esi, 12
326
        add     esi, 12
356
 
-
 
357
        cmp     edi, [esp]
327
        cmp     edi, [esp]
358
        jnz     fcnew
328
        jnz     @b
359
        pop     eax
329
        pop     eax
360
 
-
 
361
        popad
330
        popad
-
 
331
        mov     eax, ebp
-
 
332
        pop     ebx ebp
362
        ret
333
        ret
Line 363... Line -...
363
 
-
 
364
 
-
 
365
restorefatchain:   ; restore fat chain
-
 
366
 
-
 
367
        pushad
-
 
368
 
-
 
369
        mov     esi, [ebp+FAT.fat12_unpacked_ptr]
-
 
370
        mov     edi, [ebp+FAT.fat_cache_ptr]
-
 
371
 
-
 
372
        mov     edx, [ebp+FAT.LAST_CLUSTER]
-
 
373
        and     edx, not 7
-
 
374
        lea     edx, [esi+(edx+8)*2]
-
 
375
 
-
 
376
  fcnew2:
-
 
377
        mov     eax, dword [esi]
-
 
378
        mov     ebx, dword [esi+4]
-
 
379
        shl     ax, 4
-
 
380
        shl     eax, 4
-
 
381
        shl     bx, 4
-
 
382
        shr     ebx, 4
-
 
383
        shrd    eax, ebx, 8
-
 
384
        shr     ebx, 8
-
 
385
        mov     dword [edi], eax
-
 
386
        mov     word [edi+4], bx
-
 
387
        add     edi, 6
-
 
388
        add     esi, 8
-
 
389
 
-
 
390
        cmp     esi, edx
-
 
391
        jb      fcnew2
-
 
392
 
-
 
393
        mov     esi, [ebp+FAT.NUMBER_OF_FATS]
-
 
394
        mov     edx, [ebp+FAT.LAST_CLUSTER]
-
 
395
        lea     edx, [(edx+1)*3 + 512*2-1]
-
 
396
        shr     edx, 10
-
 
397
        push    [ebp+FAT.FAT_START]
-
 
398
 
334
 
399
.write_fats:
-
 
400
        xor     eax, eax
-
 
401
        mov     ebx, [ebp+FAT.fat_cache_ptr]
-
 
402
.loop1:
-
 
403
        push    eax
-
 
404
        add     eax, [esp+4]
-
 
405
        call    fs_write32_sys
-
 
406
        test    eax, eax
-
 
407
        pop     eax
-
 
408
        jnz     .fail
-
 
409
        add     ebx, 512
-
 
410
        inc     eax
-
 
411
        cmp     eax, edx
-
 
412
        jb      .loop1
-
 
413
        pop     eax
-
 
414
        add     eax, [ebp+FAT.SECTORS_PER_FAT]
335
fat_free:
415
        push    eax
336
        push    eax
416
        dec     esi
337
        mov     eax, [eax+FAT.fat_cache_ptr]
417
        jnz     .write_fats
338
        call    free
418
        pop     eax
-
 
419
 
-
 
420
        popad
339
        pop     eax
421
        ret
-
 
422
.fail:
-
 
423
        dbgstr 'Failed to save FAT'
-
 
424
        popad
-
 
Line 425... Line 340...
425
        ret
340
        jmp     free
426
 
341
 
427
iglobal
342
iglobal
428
label fat_legal_chars byte
343
label fat_legal_chars byte
Line 462... Line 377...
462
        pop     esi
377
        pop     esi
463
        ret
378
        ret
Line 464... Line 379...
464
 
379
 
465
fat_next_short_name:
380
fat_next_short_name:
466
; in: edi->8+3 name
381
; in: edi->8+3 name
467
; out: name corrected
-
 
468
;      CF=1 <=> error
382
; out: name corrected, CF=1 -> error
469
        pushad
383
        pushad
470
        mov     ecx, 8
384
        mov     ecx, 8
471
        mov     al, '~'
385
        mov     al, '~'
472
        std
386
        std
Line 488... Line 402...
488
.insert_tilde:
402
.insert_tilde:
489
        mov     word [edi], '~1'
403
        mov     word [edi], '~1'
490
        popad
404
        popad
491
        clc
405
        clc
492
        ret
406
        ret
-
 
407
 
493
.tilde:
408
.tilde:
494
        push    edi
409
        push    edi
495
        add     edi, 7
410
        add     edi, 7
496
        xor     ecx, ecx
411
        xor     ecx, ecx
497
@@:
-
 
498
; after tilde may be only digits and trailing spaces
412
@@: ; after tilde may be only digits and trailing spaces
499
        cmp     byte [edi], '~'
413
        cmp     byte [edi], '~'
500
        jz      .break
414
        jz      .break
501
        cmp     byte [edi], ' '
415
        cmp     byte [edi], ' '
502
        jz      .space
416
        jz      .space
503
        cmp     byte [edi], '9'
417
        cmp     byte [edi], '9'
504
        jnz     .found
418
        jnz     .found
505
        dec     edi
419
        dec     edi
506
        jmp     @b
420
        jmp     @b
-
 
421
 
507
.space:
422
.space:
508
        dec     edi
423
        dec     edi
509
        inc     ecx
424
        inc     ecx
510
        jmp     @b
425
        jmp     @b
-
 
426
 
511
.found:
427
.found:
512
        inc     byte [edi]
428
        inc     byte [edi]
513
        add     dword [esp], 8
429
        add     dword [esp], 8
514
        jmp     .zerorest
430
        jmp     .zerorest
-
 
431
 
515
.break:
432
.break:
516
        jecxz   .noplace
433
        jecxz   .noplace
517
        inc     edi
434
        inc     edi
518
        mov     al, '1'
435
        mov     al, '1'
519
@@:
436
@@:
Line 525... Line 442...
525
.succ:
442
.succ:
526
        pop     edi
443
        pop     edi
527
        popad
444
        popad
528
        clc
445
        clc
529
        ret
446
        ret
-
 
447
 
530
.noplace:
448
.noplace:
531
        dec     edi
449
        dec     edi
532
        cmp     edi, [esp]
450
        cmp     edi, [esp]
533
        jz      .err
451
        jz      .err
534
        add     dword [esp], 8
452
        add     dword [esp], 8
Line 541... Line 459...
541
        inc     edi
459
        inc     edi
542
        cmp     edi, [esp]
460
        cmp     edi, [esp]
543
        jb      @b
461
        jb      @b
544
        pop     edi
462
        pop     edi
545
        popad
463
        popad
546
        ;clc    ; automatically
-
 
547
        ret
464
        ret
-
 
465
 
548
.err:
466
.err:
549
        pop     edi
467
        pop     edi
550
        popad
468
        popad
551
        stc
469
        stc
552
        ret
470
        ret
Line 609... Line 527...
609
        cmp     edi, ecx
527
        cmp     edi, ecx
610
        ja      @b
528
        ja      @b
611
.skip:
529
.skip:
612
        mov     bh, 3
530
        mov     bh, 3
613
        jmp     @f
531
        jmp     @f
-
 
532
 
614
.firstdot:
533
.firstdot:
615
        cmp     bl, 8
534
        cmp     bl, 8
616
        jz      .space
535
        jz      .space
617
        push    edi
536
        push    edi
618
        or      bh, 2
537
        or      bh, 2
619
@@:
538
@@:
620
        mov     edi, ecx
539
        mov     edi, ecx
621
        mov     bl, 3
540
        mov     bl, 3
622
        jmp     .loop
541
        jmp     .loop
-
 
542
 
623
.done:
543
.done:
624
        test    bh, 2
544
        test    bh, 2
625
        jz      @f
545
        jz      @f
626
        pop     edi
546
        pop     edi
627
@@:
547
@@:
Line 631... Line 551...
631
        call    fat_next_short_name
551
        call    fat_next_short_name
632
@@:
552
@@:
633
        popad
553
        popad
634
        ret
554
        ret
Line 635... Line -...
635
 
-
 
636
fat12_free_space:
-
 
637
;---------------------------------------------
-
 
638
;
-
 
639
; returns free space in edi
-
 
640
; rewr.by Mihasik
-
 
641
;---------------------------------------------
-
 
642
 
-
 
643
        push    eax ebx ecx
-
 
644
 
-
 
645
        mov     edi, [ebp+FAT.fat12_unpacked_ptr];start of FAT
-
 
646
        xor     ax, ax;Free cluster=0x0000 in FAT
-
 
647
        xor     ebx, ebx;counter
-
 
648
        mov     ecx, [ebp+FAT.LAST_CLUSTER]
-
 
649
        inc     ecx
-
 
650
        cld
-
 
651
    rdfs1:
-
 
652
        repne scasw
-
 
653
        jnz     rdfs2 ;if last cluster not 0
-
 
654
        inc     ebx
-
 
655
        test    ecx, ecx
-
 
656
        jnz     rdfs1
-
 
657
    rdfs2:
-
 
658
        shl     ebx, 9;free clusters*512
-
 
659
        mov     edi, ebx
-
 
660
 
-
 
661
        pop     ecx ebx eax
-
 
662
        ret
-
 
663
 
-
 
664
 
-
 
665
 
555
 
666
set_FAT:
-
 
667
;--------------------------------
-
 
668
; input  : EAX = cluster
556
set_FAT:
669
;          EDX = value to save
-
 
670
;          EBP = pointer to FAT structure
-
 
671
; output : EDX = old value
-
 
672
;--------------------------------
557
; in: eax = cluster, edx = value to save
673
; out: CF set <=> error
558
; out: edx = old value, CF=1 -> error
674
        push    eax ebx esi
-
 
675
 
559
        push    eax ebx esi
676
        cmp     eax, 2
560
        cmp     eax, 2
677
        jb      sfc_error
561
        jc      .ret
678
        cmp     eax, [ebp+FAT.LAST_CLUSTER]
562
        cmp     [ebp+FAT.LAST_CLUSTER], eax
679
        ja      sfc_error
563
        jc      .ret
680
        cmp     [ebp+FAT.fs_type], 12
564
        cmp     [ebp+FAT.fs_type], 12
681
        je      set_FAT12
565
        je      .FAT12
682
        cmp     [ebp+FAT.fs_type], 16
566
        cmp     [ebp+FAT.fs_type], 16
683
        je      sfc_1
567
        je      @f
684
        add     eax, eax
568
        add     eax, eax
685
  sfc_1:
569
@@:
686
        add     eax, eax
570
        add     eax, eax
687
        mov     esi, 511
571
        mov     esi, 511
688
        and     esi, eax        ; esi = position in fat sector
572
        and     esi, eax
689
        shr     eax, 9          ; eax = fat sector
573
        shr     eax, 9
690
        add     eax, [ebp+FAT.FAT_START]
574
        add     eax, [ebp+FAT.FAT_START]
691
        mov     ebx, [ebp+FAT.fat_cache_ptr]
-
 
692
 
575
        mov     ebx, [ebp+FAT.fat_cache_ptr]
693
        cmp     eax, [ebp+FAT.fat_in_cache]; is fat sector already in memory?
576
        cmp     eax, [ebp+FAT.fat_in_cache]
694
        je      sfc_in_cache    ; yes
-
 
695
 
577
        je      .inCache
696
        cmp     [ebp+FAT.fat_change], 0; is fat changed?
578
        cmp     [ebp+FAT.fat_change], 0
697
        je      sfc_no_change   ; no
579
        je      @f
698
        call    write_fat_sector; yes. write it into disk
580
        call    write_fat_sector
699
        jc      sfc_error
581
        jc      .ret
700
 
-
 
701
  sfc_no_change:
582
@@:
702
        mov     [ebp+FAT.fat_in_cache], eax; save fat sector
583
        mov     [ebp+FAT.fat_in_cache], eax
703
        call    fs_read32_sys
584
        call    fs_read32_sys
704
        test    eax, eax
585
        test    eax, eax
705
        jne     sfc_error
-
 
706
 
-
 
707
 
586
        jne     .error
708
  sfc_in_cache:
587
.inCache:
709
        cmp     [ebp+FAT.fs_type], 16
588
        cmp     [ebp+FAT.fs_type], 16
710
        jne     sfc_test32
-
 
711
 
-
 
712
  sfc_set16:
589
        jne     .test32
713
        xchg    [ebx+esi], dx   ; save new value and get old value
590
        xchg    [ebx+esi], dx   ; save new value and get old value
Line 714... Line 591...
714
        jmp     sfc_write
591
        jmp     .write
715
 
592
 
716
  sfc_test32:
-
 
717
        mov     eax, [ebp+FAT.fatMASK]
-
 
718
 
593
.test32:
719
  sfc_set32:
594
        mov     eax, [ebp+FAT.fatMASK]
720
        and     edx, eax
595
        and     edx, eax
721
        xor     eax, -1         ; mask for high bits
596
        xor     eax, -1         ; mask for high bits
722
        and     eax, [ebx+esi]  ; get high 4 bits
597
        and     eax, [ebx+esi]  ; get high 4 bits
723
        or      eax, edx
598
        or      eax, edx
724
        mov     edx, [ebx+esi]  ; get old value
-
 
725
        mov     [ebx+esi], eax  ; save new value
599
        mov     edx, [ebx+esi]  ; get old value
726
 
600
        mov     [ebx+esi], eax  ; save new value
727
  sfc_write:
-
 
728
        mov     [ebp+FAT.fat_change], 1; fat has changed
-
 
729
 
601
.write:
730
  sfc_nonzero:
-
 
731
        and     edx, [ebp+FAT.fatMASK]
602
        mov     [ebp+FAT.fat_change], 1
732
 
603
        and     edx, [ebp+FAT.fatMASK]
733
  sfc_return:
604
.ret:
-
 
605
        pop     esi ebx eax
734
        pop     esi ebx eax
606
        ret
735
        ret
607
 
736
  sfc_error:
608
.error:
Line 737... Line 609...
737
        stc
609
        stc
738
        jmp     sfc_return
610
        jmp     .ret
739
 
611
 
740
  set_FAT12:
612
.FAT12:
741
        test    edx, 0xF000
613
        test    edx, 0xF000
742
        jnz     sfc_error
614
        jnz     .error
743
        mov     ebx, [ebp+FAT.fat12_unpacked_ptr]
615
        mov     ebx, [ebp+FAT.fat12_unpacked_ptr]
744
        xchg    [ebx+eax*2], dx
-
 
745
        mov     [ebp+FAT.fat_change], 1
-
 
Line 746... Line 616...
746
        pop     esi ebx eax
616
        xchg    [ebx+eax*2], dx
747
        clc
-
 
748
        ret
617
        mov     [ebp+FAT.fat_change], 1
749
 
-
 
750
get_FAT:
618
        jmp     .ret
751
;--------------------------------
-
 
752
; input  : EAX = cluster
-
 
753
;          EBP = pointer to FAT structure
619
 
754
; output : EAX = next cluster
-
 
755
;--------------------------------
620
get_FAT:
756
; out: CF set <=> error
621
; in: eax = cluster
757
        push    ebx esi
-
 
758
 
622
; out: eax = next cluster, CF=1 -> error
759
        cmp     [ebp+FAT.fs_type], 12
623
        push    ebx esi
760
        je      get_FAT12
624
        cmp     [ebp+FAT.fs_type], 12
761
 
625
        je      .FAT12
762
        cmp     [ebp+FAT.fs_type], 16
626
        cmp     [ebp+FAT.fs_type], 16
763
        je      gfc_1
627
        je      @f
764
        add     eax, eax
628
        add     eax, eax
765
  gfc_1:
629
@@:
766
        add     eax, eax
630
        add     eax, eax
767
        mov     esi, 511
631
        mov     esi, 511
768
        and     esi, eax        ; esi = position in fat sector
-
 
769
        shr     eax, 9          ; eax = fat sector
632
        and     esi, eax
770
        add     eax, [ebp+FAT.FAT_START]
633
        shr     eax, 9
771
        mov     ebx, [ebp+FAT.fat_cache_ptr]
-
 
772
 
634
        add     eax, [ebp+FAT.FAT_START]
773
        cmp     eax, [ebp+FAT.fat_in_cache]; is fat sector already in memory?
635
        mov     ebx, [ebp+FAT.fat_cache_ptr]
774
        je      gfc_in_cache
636
        cmp     eax, [ebp+FAT.fat_in_cache]
775
 
637
        je      .inCache
776
        cmp     [ebp+FAT.fat_change], 0; is fat changed?
638
        cmp     [ebp+FAT.fat_change], 0
777
        je      gfc_no_change   ; no
-
 
778
        call    write_fat_sector; yes. write it into disk
639
        je      @f
779
        jc      hd_error_01
640
        call    write_fat_sector
780
 
641
        jc      .ret
781
  gfc_no_change:
642
@@:
782
        mov     [ebp+FAT.fat_in_cache], eax
-
 
783
        call    fs_read32_sys
643
        mov     [ebp+FAT.fat_in_cache], eax
784
        test    eax, eax
644
        call    fs_read32_sys
785
        jne     hd_error_01
645
        test    eax, eax
786
 
646
        jnz     .error
787
  gfc_in_cache:
647
.inCache:
788
        mov     eax, [ebx+esi]
648
        mov     eax, [ebx+esi]
-
 
649
        and     eax, [ebp+FAT.fatMASK]
789
        and     eax, [ebp+FAT.fatMASK]
650
.ret:
790
  gfc_return:
651
        pop     esi ebx
791
        pop     esi ebx
652
        ret
Line 792... Line 653...
792
        ret
653
 
793
 hd_error_01:
654
.error:
794
        stc
655
        stc
795
        jmp     gfc_return
656
        jmp     .ret
796
 
-
 
797
get_FAT12:
-
 
798
        mov     ebx, [ebp+FAT.fat12_unpacked_ptr]
-
 
Line 799... Line 657...
799
        movzx   eax, word [ebx+eax*2]
657
 
800
        pop     esi ebx
-
 
801
        clc
658
.FAT12:
802
        ret
-
 
803
 
-
 
804
 
-
 
805
get_free_FAT:
659
        mov     ebx, [ebp+FAT.fat12_unpacked_ptr]
806
;-----------------------------------------------------------
660
        movzx   eax, word [ebx+eax*2]
807
; output : if CARRY=0 EAX = # first cluster found free
661
        jmp     .ret
808
;          if CARRY=1 disk full
662
 
809
; Note   : for more speed need to use fat_cache directly
663
get_free_FAT:
810
;-----------------------------------------------------------
664
; out: eax = number of first free cluster, CF=1 -> disk full
811
        push    ecx
665
        push    ecx
812
        mov     ecx, [ebp+FAT.LAST_CLUSTER]; counter for full disk
666
        mov     ecx, [ebp+FAT.LAST_CLUSTER]
813
        mov     eax, [ebp+FAT.fatStartScan]
-
 
814
        cmp     [ebp+FAT.fs_type], 12
667
        mov     eax, [ebp+FAT.fatStartScan]
815
        jz      get_free_FAT12
668
        cmp     [ebp+FAT.fs_type], 12
816
        dec     ecx
669
        jz      get_free_FAT12
817
        cmp     eax, 2
670
        dec     ecx
818
        jb      gff_reset
671
        cmp     eax, 2
819
 
-
 
820
  gff_test:
672
        jb      .reset
821
        cmp     eax, [ebp+FAT.LAST_CLUSTER]; if above last cluster start at cluster 2
673
.test:
822
        jbe     gff_in_range
674
        cmp     eax, [ebp+FAT.LAST_CLUSTER]
823
  gff_reset:
675
        jbe     .inRange
824
        mov     eax, 2
-
 
825
 
676
.reset:
826
  gff_in_range:
677
        mov     eax, 2
827
        push    eax
678
.inRange:
828
        call    get_FAT         ; get cluster state
679
        push    eax
829
        jc      gff_not_found_1
680
        call    get_FAT
830
 
681
        jc      @f
831
        test    eax, eax        ; is it free?
-
 
832
        pop     eax
682
        test    eax, eax
833
        je      gff_found       ; yes
683
        pop     eax
834
        inc     eax             ; next cluster
684
        je      .found
835
        dec     ecx             ; is all checked?
685
        inc     eax
Line 836... Line 686...
836
        jnz     gff_test        ; no
686
        dec     ecx
837
 
687
        jnz     .test
838
  gff_not_found:
688
.notFound:
Line 839... Line 689...
839
        pop     ecx             ; yes. disk is full
689
        pop     ecx
840
        stc
690
        stc
841
        ret
691
        ret
842
 
692
 
843
  gff_not_found_1:
693
@@:
844
        pop     eax
694
        pop     eax
Line 877... Line 727...
877
.found:
727
.found:
878
        sub     edi, [ebp+FAT.fat12_unpacked_ptr]
728
        sub     edi, [ebp+FAT.fat12_unpacked_ptr]
879
        shr     edi, 1
729
        shr     edi, 1
880
        mov     [ebp+FAT.fatStartScan], edi
730
        mov     [ebp+FAT.fatStartScan], edi
881
        lea     eax, [edi-1]
731
        lea     eax, [edi-1]
-
 
732
@@:
882
        pop     edi edx ecx
733
        pop     edi edx ecx
883
        ret
734
        ret
-
 
735
 
884
.notfound:
736
.notfound:
885
        pop     edi edx ecx
-
 
886
        stc
737
        stc
887
        ret
738
        jmp     @b
888
 
-
 
Line 889... Line 739...
889
 
739
 
890
write_fat_sector:
-
 
891
;-----------------------------------------------------------
-
 
892
; write changed fat to disk
-
 
893
;-----------------------------------------------------------
740
write_fat_sector:
894
        push    eax ebx ecx
-
 
895
 
741
        push    eax ebx ecx
896
        mov     [ebp+FAT.fat_change], 0
742
        mov     [ebp+FAT.fat_change], 0
897
        mov     eax, [ebp+FAT.fat_in_cache]
743
        mov     eax, [ebp+FAT.fat_in_cache]
898
        cmp     eax, -1
744
        cmp     eax, -1
899
        jz      write_fat_not_used
745
        jz      @f
900
        mov     ebx, [ebp+FAT.fat_cache_ptr]
746
        mov     ebx, [ebp+FAT.fat_cache_ptr]
901
        mov     ecx, [ebp+FAT.NUMBER_OF_FATS]
-
 
902
 
747
        mov     ecx, [ebp+FAT.NUMBER_OF_FATS]
903
  write_next_fat:
748
.write_next_fat:
904
        push    eax
749
        push    eax
905
        call    fs_write32_sys
750
        call    fs_write32_sys
906
        test    eax, eax
751
        test    eax, eax
907
        pop     eax
752
        pop     eax
908
        jnz     write_fat_not_used
-
 
909
 
753
        jnz     @f
910
        add     eax, [ebp+FAT.SECTORS_PER_FAT]
754
        add     eax, [ebp+FAT.SECTORS_PER_FAT]
911
        dec     ecx
755
        dec     ecx
912
        jnz     write_next_fat
756
        jnz     .write_next_fat
913
 
-
 
914
  write_fat_not_used:
757
@@:
915
        pop     ecx ebx eax
758
        pop     ecx ebx eax
Line 916... Line -...
916
        ret
-
 
917
 
-
 
918
 
-
 
919
 
-
 
920
 
-
 
921
 
-
 
922
bcd2bin:
-
 
923
;----------------------------------
-
 
924
; input  : AL=BCD number (eg. 0x11)
-
 
925
; output : AH=0
-
 
926
;          AL=decimal number (eg. 11)
-
 
927
;----------------------------------
-
 
928
        xor     ah, ah
-
 
929
        shl     ax, 4
-
 
930
        shr     al, 4
-
 
931
        aad
-
 
932
        ret
-
 
933
 
759
        ret
934
 
-
 
935
get_date_for_file:
760
 
936
;-----------------------------------------------------
761
get_date_for_file:
937
; Get date from CMOS and pack day,month,year in AX
762
;   out in ax:
938
; DATE   bits  0..4   : day of month 0..31
763
; bits 0-4 = day
939
;              5..8   : month of year 1..12
-
 
940
;              9..15  : count of years from 1980
-
 
941
;-----------------------------------------------------
-
 
942
        mov     al, 0x7 ;day
764
; bits 5-8 = month
943
        out     0x70, al
765
; bits 9-15 = count of years from 1980
944
        in      al, 0x71
766
        mov     al, 7
945
        call    bcd2bin
-
 
946
        ror     eax, 5
-
 
947
 
-
 
948
        mov     al, 0x8 ;month
767
        call    fsReadCMOS
949
        out     0x70, al
768
        ror     eax, 5
950
        in      al, 0x71
769
        mov     al, 8
951
        call    bcd2bin
-
 
952
        ror     eax, 4
770
        call    fsReadCMOS
953
 
771
        ror     eax, 4
954
        mov     al, 0x9 ;year
772
        mov     al, 9
955
        out     0x70, al
773
        call    fsReadCMOS
956
        in      al, 0x71
-
 
957
        call    bcd2bin
-
 
958
        add     ax, 20  ;because CMOS return only the two last
-
 
959
                        ;digit (eg. 2000 -> 00 , 2001 -> 01) and we
774
        add     ax, 20
Line 960... Line -...
960
        rol     eax, 9  ;need the difference with 1980 (eg. 2001-1980)
-
 
961
        ret
775
        rol     eax, 9
962
 
-
 
963
 
776
        ret
964
get_time_for_file:
777
 
965
;-----------------------------------------------------
778
get_time_for_file:
966
; Get time from CMOS and pack hour,minute,second in AX
779
;   out in ax:
967
; TIME   bits  0..4   : second (the low bit is lost)
-
 
968
;              5..10  : minute 0..59
-
 
969
;              11..15 : hour 0..23
-
 
970
;-----------------------------------------------------
780
; bits 0-4 = second (the low bit is lost)
971
        mov     al, 0x0 ;second
781
; bits 5-10 = minute
972
        out     0x70, al
782
; bits 11-15 = hour
973
        in      al, 0x71
-
 
974
        call    bcd2bin
-
 
975
        ror     eax, 6
-
 
976
 
783
        mov     al, 0
977
        mov     al, 0x2 ;minute
784
        call    fsReadCMOS
978
        out     0x70, al
785
        ror     eax, 6
979
        in      al, 0x71
-
 
980
        call    bcd2bin
-
 
981
        ror     eax, 6
-
 
982
 
786
        mov     al, 2
983
        mov     al, 0x4 ;hour
787
        call    fsReadCMOS
984
        out     0x70, al
788
        ror     eax, 6
985
        in      al, 0x71
789
        mov     al, 4
Line 986... Line -...
986
        call    bcd2bin
-
 
987
        rol     eax, 11
-
 
988
        ret
-
 
989
 
-
 
990
 
-
 
991
set_current_time_for_entry:
-
 
992
;-----------------------------------------------------
-
 
993
; Set current time/date for file entry
-
 
994
; input  : ebx = file entry pointer
-
 
995
;-----------------------------------------------------
-
 
996
        push    eax
-
 
997
        call    get_time_for_file; update files date/time
-
 
998
        mov     [ebx+22], ax
-
 
999
        call    get_date_for_file
-
 
1000
        mov     [ebx+24], ax
-
 
1001
        pop     eax
-
 
1002
        ret
790
        call    fsReadCMOS
1003
 
-
 
1004
 
791
        rol     eax, 11
1005
 
-
 
1006
add_disk_free_space:
-
 
1007
;-----------------------------------------------------
-
 
1008
; input  : ecx = cluster count
792
        ret
1009
; Note   : negative = remove clusters from free space
793
 
1010
;          positive = add clusters to free space
794
add_disk_free_space:
1011
;-----------------------------------------------------
795
; in: ecx = cluster count (signed)
1012
        test    ecx, ecx        ; no change
-
 
1013
        je      add_dfs_no
796
        test    ecx, ecx
1014
        cmp     [ebp+FAT.fs_type], 32  ; free disk space only used by fat32
797
        je      .ret
1015
        jne     add_dfs_no
798
        cmp     [ebp+FAT.fs_type], 32
1016
 
799
        jne     .ret
1017
        push    eax ebx
800
        push    eax ebx
1018
        mov     eax, [ebp+FAT.ADR_FSINFO]
801
        mov     eax, [ebp+FAT.ADR_FSINFO]
1019
        lea     ebx, [ebp+FAT.fsinfo_buffer]
-
 
1020
        call    fs_read32_sys
802
        lea     ebx, [ebp+FAT.fsinfo_buffer]
1021
        test    eax, eax
803
        call    fs_read32_sys
1022
        jnz     add_not_fs
-
 
1023
 
804
        test    eax, eax
1024
        cmp     dword [ebx+0x1fc], 0xaa550000; check sector id
805
        jnz     @f
1025
        jne     add_not_fs
806
        cmp     dword [ebx+0x1fc], 0xaa550000   ; check sector id
1026
 
807
        jne     @f
1027
        add     [ebx+0x1e8], ecx
808
        add     [ebx+0x1e8], ecx
1028
        push    [ebp+FAT.fatStartScan]
-
 
1029
        pop     dword [ebx+0x1ec]
809
        push    [ebp+FAT.fatStartScan]
1030
        mov     eax, [ebp+FAT.ADR_FSINFO]
-
 
1031
        call    fs_write32_sys
810
        pop     dword [ebx+0x1ec]
1032
;    jc    add_not_fs
-
 
1033
 
811
        mov     eax, [ebp+FAT.ADR_FSINFO]
1034
  add_not_fs:
812
        call    fs_write32_sys
Line 1035... Line -...
1035
        pop     ebx eax
-
 
1036
 
-
 
1037
  add_dfs_no:
813
@@:
1038
        ret
-
 
1039
 
814
        pop     ebx eax
1040
 
-
 
1041
 
815
.ret:
1042
clear_cluster_chain:
816
        ret
1043
;-----------------------------------------------------
817
 
1044
; input  : eax = first cluster
-
 
1045
;-----------------------------------------------------
818
clear_cluster_chain:
1046
        push    eax ecx edx
819
; in: eax = first cluster
1047
        xor     ecx, ecx        ; cluster count
820
        push    eax ecx edx
1048
 
821
        xor     ecx, ecx        ; cluster count
1049
  clean_new_chain:
822
@@:
1050
        cmp     eax, [ebp+FAT.LAST_CLUSTER]; end of file
823
        cmp     eax, [ebp+FAT.LAST_CLUSTER]
1051
        ja      delete_OK
-
 
1052
        cmp     eax, 2          ; unfinished fat chain or zero length file
824
        ja      @f
1053
        jb      delete_OK
825
        cmp     eax, 2
1054
        cmp     eax, [ebp+FAT.ROOT_CLUSTER]; don't remove root cluster
826
        jb      @f
1055
        jz      delete_OK
-
 
1056
 
827
        cmp     eax, [ebp+FAT.ROOT_CLUSTER]
1057
        xor     edx, edx
828
        jz      @f
1058
        call    set_FAT         ; clear fat entry
829
        xor     edx, edx
Line 1059... Line 830...
1059
        jc      access_denied_01
830
        call    set_FAT
1060
 
831
        jc      .ret
1061
        inc     ecx             ; update cluster count
832
        inc     ecx
1062
        mov     eax, edx        ; old cluster
833
        mov     eax, edx
1063
        jmp     clean_new_chain
834
        jmp     @b
1064
 
835
 
Line 1065... Line -...
1065
  delete_OK:
-
 
1066
        call    add_disk_free_space; add clusters to free disk space
-
 
1067
        clc
-
 
1068
  access_denied_01:
-
 
1069
        pop     edx ecx eax
-
 
1070
        ret
-
 
1071
 
-
 
1072
 
-
 
1073
if 0
-
 
1074
get_hd_info:
-
 
1075
;-----------------------------------------------------------
-
 
1076
; output : eax = 0 - ok
-
 
1077
;                3 - unknown FS
-
 
1078
;               10 - access denied
-
 
1079
;          edx = cluster size in bytes
-
 
1080
;          ebx = total clusters on disk
-
 
1081
;          ecx = free clusters on disk
-
 
1082
;-----------------------------------------------------------
-
 
1083
        cmp     [ebp+FAT.fs_type], 16
-
 
1084
        jz      info_fat_ok
-
 
1085
        cmp     [ebp+FAT.fs_type], 32
-
 
1086
        jz      info_fat_ok
-
 
1087
        xor     edx, edx
-
 
1088
        xor     ebx, ebx
-
 
1089
        xor     ecx, ecx
-
 
1090
        mov     eax, ERROR_UNKNOWN_FS
-
 
1091
        ret
-
 
1092
 
-
 
1093
  info_fat_ok:
-
 
1094
;    call  reserve_hd1
-
 
1095
 
-
 
1096
        xor     ecx, ecx        ; count of free clusters
-
 
1097
        mov     eax, 2
-
 
1098
        mov     ebx, [ebp+FAT.LAST_CLUSTER]
-
 
1099
 
-
 
1100
  info_cluster:
-
 
1101
        push    eax
-
 
1102
        call    get_FAT         ; get cluster info
-
 
1103
        jc      info_access_denied
-
 
1104
 
-
 
1105
        test    eax, eax        ; is it free?
-
 
1106
        jnz     info_used       ; no
-
 
1107
        inc     ecx
-
 
1108
 
-
 
1109
  info_used:
-
 
1110
        pop     eax
-
 
1111
        inc     eax
-
 
1112
        cmp     eax, ebx        ; is above last cluster?
-
 
1113
        jbe     info_cluster    ; no. test next cluster
-
 
1114
 
-
 
1115
        dec     ebx             ; cluster count
-
 
1116
        imul    edx, [ebp+FAT.SECTORS_PER_CLUSTER], 512; cluster size in bytes
-
 
1117
        xor     eax, eax
-
 
1118
        ret
-
 
1119
 
-
 
1120
  info_access_denied:
-
 
1121
        add     esp, 4
-
 
1122
        xor     edx, edx
836
@@:
1123
        xor     ebx, ebx
837
        call    add_disk_free_space
1124
        xor     ecx, ecx
838
        clc
1125
        mov     eax, ERROR_ACCESS_DENIED
839
.ret:
1126
        ret
840
        pop     edx ecx eax
1127
end if
-
 
1128
 
-
 
1129
update_disk:
-
 
1130
        cmp     [ebp+FAT.fat_change], 0 ; is fat changed?
-
 
1131
        je      upd_no_change
841
        ret
1132
        cmp     [ebp+FAT.fs_type], 12
-
 
1133
        jz      .fat12
842
 
1134
;-----------------------------------------------------------
-
 
1135
; write changed fat and cache to disk
-
 
1136
;-----------------------------------------------------------
-
 
1137
 
-
 
1138
        call    write_fat_sector
843
update_disk:
1139
        jc      update_disk_acces_denied
-
 
1140
        jmp     upd_no_change
-
 
1141
.fat12:
844
        cmp     [ebp+FAT.fat_change], 0
1142
        call    restorefatchain
845
        jz      .noChange
1143
        mov     [ebp+FAT.fat_change], 0
846
        cmp     [ebp+FAT.fs_type], 12
1144
 
-
 
1145
  upd_no_change:
847
        jz      .fat12
Line -... Line 848...
-
 
848
        call    write_fat_sector
-
 
849
        jc      .ret
-
 
850
.noChange:
-
 
851
        mov     esi, [ebp+PARTITION.Disk]
-
 
852
        call    disk_sync
-
 
853
.ret:
-
 
854
        ret
-
 
855
 
-
 
856
.fat12:
-
 
857
        mov     esi, [ebp+FAT.fat12_unpacked_ptr]
-
 
858
        mov     edi, [ebp+FAT.fat_cache_ptr]
-
 
859
        mov     edx, [ebp+FAT.LAST_CLUSTER]
-
 
860
        and     edx, not 7
-
 
861
        lea     edx, [esi+(edx+8)*2]
-
 
862
@@:
-
 
863
        mov     eax, dword [esi]
-
 
864
        mov     ebx, dword [esi+4]
-
 
865
        shl     ax, 4
-
 
866
        shl     eax, 4
-
 
867
        shl     bx, 4
-
 
868
        shr     ebx, 4
-
 
869
        shrd    eax, ebx, 8
-
 
870
        shr     ebx, 8
-
 
871
        mov     dword [edi], eax
-
 
872
        mov     word [edi+4], bx
-
 
873
        add     edi, 6
-
 
874
        add     esi, 8
-
 
875
        cmp     esi, edx
-
 
876
        jb      @b
-
 
877
        mov     esi, [ebp+FAT.NUMBER_OF_FATS]
-
 
878
        mov     edx, [ebp+FAT.LAST_CLUSTER]
-
 
879
        lea     edx, [(edx+1)*3 + 512*2-1]
-
 
880
        shr     edx, 10
-
 
881
        push    [ebp+FAT.FAT_START]
-
 
882
.write_fats:
-
 
883
        xor     eax, eax
-
 
884
        mov     ebx, [ebp+FAT.fat_cache_ptr]
-
 
885
.loop1:
-
 
886
        push    eax
-
 
887
        add     eax, [esp+4]
-
 
888
        call    fs_write32_sys
-
 
889
        test    eax, eax
-
 
890
        pop     eax
-
 
891
        jnz     @f
-
 
892
        add     ebx, 512
-
 
893
        inc     eax
-
 
894
        cmp     eax, edx
-
 
895
        jb      .loop1
-
 
896
        pop     eax
-
 
897
        add     eax, [ebp+FAT.SECTORS_PER_FAT]
1146
 
898
        push    eax
1147
        push    esi
899
        dec     esi
1148
        mov     esi, [ebp+PARTITION.Disk]
900
        jnz     .write_fats
-
 
901
@@:
1149
        call    disk_sync
902
        pop     eax
1150
        pop     esi
903
        mov     [ebp+FAT.fat_change], 0
1151
  update_disk_acces_denied:
904
        jmp     .noChange
Line 1152... Line 905...
1152
        ret
905
 
Line 1522... Line 1275...
1522
        ret
1275
        ret
Line 1523... Line 1276...
1523
 
1276
 
1524
;----------------------------------------------------------------
1277
;----------------------------------------------------------------
1525
fat_Read:
1278
fat_Read:
1526
        call    fat_lock
-
 
1527
        push    edi
1279
        call    fat_lock
1528
        call    hd_find_lfn
1280
        call    hd_find_lfn
1529
        jc      .notFound
1281
        jc      .notFound
1530
        test    byte [edi+11], 0x10     ; do not allow read directories
1282
        test    byte [edi+11], 0x10     ; do not allow read directories
1531
        jnz     .noaccess
1283
        jnz     .noaccess
Line 1572... Line 1324...
1572
 
1324
 
1573
.endOfFile:
1325
.endOfFile:
1574
        push    ERROR_END_OF_FILE
1326
        push    ERROR_END_OF_FILE
1575
.ret:
1327
.ret:
1576
        call    fat_unlock
1328
        call    fat_unlock
1577
        pop     eax edi
1329
        pop     eax
1578
        xor     ebx, ebx
1330
        xor     ebx, ebx
Line 1579... Line 1331...
1579
        ret
1331
        ret
1580
 
1332
 
Line 1689... Line 1441...
1689
        pop     eax
1441
        pop     eax
1690
.noaccess2:
1442
.noaccess2:
1691
        mov     byte [esp], ERROR_DEVICE
1443
        mov     byte [esp], ERROR_DEVICE
1692
.done:
1444
.done:
1693
        call    fat_unlock
1445
        call    fat_unlock
1694
        pop     eax edx edi
1446
        pop     eax edx
1695
        sub     ebx, edx
1447
        sub     ebx, edx
1696
        ret
1448
        ret
Line 1697... Line 1449...
1697
 
1449
 
1698
.fileEnd:
1450
.fileEnd:
Line 1712... Line 1464...
1712
 
1464
 
1713
;----------------------------------------------------------------
1465
;----------------------------------------------------------------
1714
fat_ReadFolder:
1466
fat_ReadFolder:
1715
        call    fat_lock
1467
        call    fat_lock
1716
        mov     eax, [ebp+FAT.ROOT_CLUSTER]
-
 
1717
        push    edi
1468
        mov     eax, [ebp+FAT.ROOT_CLUSTER]
1718
        cmp     byte [esi], 0
1469
        cmp     byte [esi], 0
1719
        jz      .doit
1470
        jz      .doit
1720
        call    hd_find_lfn
1471
        call    hd_find_lfn
1721
        jnc     .found
-
 
1722
        pop     edi
-
 
1723
        push    eax
-
 
1724
        call    fat_unlock
-
 
1725
        pop     eax
-
 
1726
        or      ebx, -1
-
 
1727
        ret
-
 
1728
.found:
1472
        jc      .error
1729
        test    byte [edi+11], 0x10     ; do not allow read files
1473
        test    byte [edi+11], 0x10     ; do not allow read files
1730
        jnz     .found_dir
-
 
1731
        pop     edi
-
 
1732
        call    fat_unlock
-
 
1733
        or      ebx, -1
-
 
1734
        mov     eax, ERROR_ACCESS_DENIED
-
 
1735
        ret
-
 
1736
.found_dir:
1474
        jz      .accessDenied
1737
        mov     eax, [edi+20-2]
1475
        mov     eax, [edi+20-2]
1738
        mov     ax, [edi+26]    ; eax=cluster
1476
        mov     ax, [edi+26]    ; eax=cluster
1739
.doit:
1477
.doit:
1740
        push    esi
1478
        push    esi
Line 1760... Line 1498...
1760
        jz      .notfound
1498
        jz      .notfound
1761
        mov     eax, [ebp+FAT.ROOT_START]
1499
        mov     eax, [ebp+FAT.ROOT_START]
1762
        push    [ebp+FAT.ROOT_SECTORS]
1500
        push    [ebp+FAT.ROOT_SECTORS]
1763
        push    ebx
1501
        push    ebx
1764
        jmp     .new_sector
1502
        jmp     .new_sector
-
 
1503
 
1765
@@:
1504
@@:
1766
        dec     eax
1505
        dec     eax
1767
        dec     eax
1506
        dec     eax
1768
        imul    eax, [ebp+FAT.SECTORS_PER_CLUSTER]
1507
        imul    eax, [ebp+FAT.SECTORS_PER_CLUSTER]
1769
        push    [ebp+FAT.SECTORS_PER_CLUSTER]
1508
        push    [ebp+FAT.SECTORS_PER_CLUSTER]
Line 1855... Line 1594...
1855
        mov     [esp+8], eax
1594
        mov     [esp+8], eax
1856
        pop     eax
1595
        pop     eax
1857
        pop     ebx
1596
        pop     ebx
1858
        add     esp, 4
1597
        add     esp, 4
1859
        jmp     .new_cluster
1598
        jmp     .new_cluster
-
 
1599
 
1860
.notfound2:
1600
.notfound2:
1861
        add     esp, 8
1601
        add     esp, 8
1862
.notfound:
1602
.notfound:
1863
        add     esp, 262*2+4
1603
        add     esp, 262*2+8
1864
        pop     esi edi
-
 
1865
        mov     ebx, [edx+4]
-
 
1866
        call    fat_unlock
-
 
1867
        mov     eax, ERROR_DEVICE
1604
        push    ERROR_DEVICE
1868
        ret
1605
        jmp     @f
-
 
1606
 
1869
.done:
1607
.done:
1870
        add     esp, 262*2+4+8
1608
        add     esp, 262*2+16
1871
        mov     ebx, [edx+4]
-
 
1872
        xor     eax, eax
1609
        pushd   0
1873
        dec     ecx
1610
        dec     ecx
1874
        js      @f
1611
        js      @f
1875
        mov     al, ERROR_END_OF_FILE
1612
        mov     byte [esp], ERROR_END_OF_FILE
1876
@@:
1613
@@:
1877
        push    eax
1614
        mov     ebx, [edx+4]
-
 
1615
.ret:
1878
        call    fat_unlock
1616
        call    fat_unlock
1879
        pop     eax
1617
        pop     eax
1880
        pop     esi edi
-
 
1881
        ret
1618
        ret
Line -... Line 1619...
-
 
1619
 
-
 
1620
.error:
-
 
1621
        push    eax
-
 
1622
        xor     ebx, ebx
-
 
1623
        jmp     .ret
-
 
1624
 
-
 
1625
.accessDenied:
-
 
1626
        push    ERROR_ACCESS_DENIED
-
 
1627
        xor     ebx, ebx
-
 
1628
        jmp     .ret
1882
 
1629
 
1883
fat1x_root_next:
1630
fat1x_root_next:
1884
        push    ecx
1631
        push    ecx
1885
        lea     ecx, [ebp+FAT.buffer+0x200-0x20]
1632
        lea     ecx, [ebp+FAT.buffer+0x200-0x20]
1886
        cmp     edi, ecx
1633
        cmp     edi, ecx
1887
        jae     fat1x_root_next_sector
-
 
1888
        pop     ecx
1634
        jae     fat1x_root_next_sector
-
 
1635
        add     edi, 0x20
-
 
1636
@@:
-
 
1637
        pop     ecx
-
 
1638
        ret
-
 
1639
 
-
 
1640
fat1x_root_next_write:
-
 
1641
        push    ecx
-
 
1642
        lea     ecx, [ebp+FAT.buffer+0x200]
1889
        add     edi, 0x20
1643
        cmp     edi, ecx
-
 
1644
        jc      @b
1890
        ret     ; CF=0
1645
        call    fat1x_root_end_write
1891
fat1x_root_next_sector:
-
 
1892
; read next sector
1646
fat1x_root_next_sector:
1893
        push    [ebp+FAT.longname_sec2]
1647
        push    [ebp+FAT.longname_sec2]
1894
        pop     [ebp+FAT.longname_sec1]
1648
        pop     [ebp+FAT.longname_sec1]
1895
        mov     ecx, [eax+4]
1649
        mov     ecx, [eax+4]
1896
        push    ecx
1650
        push    ecx
1897
        add     ecx, [ebp+FAT.ROOT_START]
1651
        add     ecx, [ebp+FAT.ROOT_START]
1898
        mov     [ebp+FAT.longname_sec2], ecx
1652
        mov     [ebp+FAT.longname_sec2], ecx
1899
        pop     ecx
1653
        pop     ecx
1900
        inc     ecx
1654
        inc     ecx
1901
        mov     [eax+4], ecx
1655
        mov     [eax+4], ecx
-
 
1656
        cmp     ecx, [ebp+FAT.ROOT_SECTORS]
1902
        cmp     ecx, [ebp+FAT.ROOT_SECTORS]
1657
        jnc     fat_notroot_next_err
1903
        pop     ecx
-
 
1904
        jb      fat1x_root_first
-
 
1905
        mov     eax, ERROR_FILE_NOT_FOUND
-
 
1906
        stc
-
 
1907
        ret
1658
        pop     ecx
1908
fat1x_root_first:
1659
fat1x_root_first:
1909
        mov     eax, [eax+4]
1660
        mov     eax, [eax+4]
1910
        add     eax, [ebp+FAT.ROOT_START]
1661
        add     eax, [ebp+FAT.ROOT_START]
1911
        push    ebx
1662
        push    ebx
1912
        lea     edi, [ebp+FAT.buffer]
1663
        lea     edi, [ebp+FAT.buffer]
1913
        mov     ebx, edi
1664
        mov     ebx, edi
1914
        call    fs_read32_sys
1665
        call    fs_read32_sys
1915
        pop     ebx
1666
        pop     ebx
1916
        test    eax, eax
-
 
1917
        jnz     .readerr
1667
        test    eax, eax
1918
        ret     ; CF=0
-
 
1919
.readerr:
1668
        jz      @f
1920
        mov     eax, ERROR_DEVICE
-
 
1921
        stc
-
 
1922
        ret
-
 
1923
.notfound:
-
 
1924
        mov     eax, ERROR_FILE_NOT_FOUND
1669
        movi    eax, ERROR_DEVICE
-
 
1670
        stc
1925
        stc
1671
@@:
-
 
1672
        ret
1926
        ret
1673
 
1927
fat1x_root_begin_write:
1674
fat1x_root_begin_write:
1928
        push    edi eax
1675
        push    edi eax
1929
        call    fat1x_root_first
1676
        call    fat1x_root_first
1930
        pop     eax edi
1677
        pop     eax edi
-
 
1678
        ret
1931
        ret
1679
 
1932
fat1x_root_end_write:
1680
fat1x_root_end_write:
1933
        pusha
1681
        pusha
1934
        mov     eax, [eax+4]
1682
        mov     eax, [eax+4]
1935
        add     eax, [ebp+FAT.ROOT_START]
1683
        add     eax, [ebp+FAT.ROOT_START]
1936
        lea     ebx, [ebp+FAT.buffer]
1684
        lea     ebx, [ebp+FAT.buffer]
1937
        call    fs_write32_sys
1685
        call    fs_write32_sys
1938
        popa
1686
        popa
1939
        ret
-
 
1940
fat1x_root_next_write:
-
 
1941
        push    ecx
-
 
1942
        lea     ecx, [ebp+FAT.buffer+0x200]
-
 
1943
        cmp     edi, ecx
-
 
1944
        jae     @f
-
 
1945
        pop     ecx
-
 
1946
        ret
-
 
1947
@@:
-
 
1948
        call    fat1x_root_end_write
-
 
1949
        jmp     fat1x_root_next_sector
-
 
1950
fat1x_root_extend_dir:
-
 
1951
        stc
-
 
Line 1952... Line 1687...
1952
        ret
1687
        ret
1953
 
1688
 
1954
fat_notroot_next:
1689
fat_notroot_next:
1955
        push    ecx
1690
        push    ecx
1956
        lea     ecx, [ebp+FAT.buffer+0x200-0x20]
1691
        lea     ecx, [ebp+FAT.buffer+0x200-0x20]
1957
        cmp     edi, ecx
-
 
1958
        jae     fat_notroot_next_sector
1692
        cmp     edi, ecx
-
 
1693
        jae     fat_notroot_next_sector
-
 
1694
        add     edi, 0x20
-
 
1695
@@:
-
 
1696
        pop     ecx
-
 
1697
        ret
-
 
1698
 
-
 
1699
fat_notroot_next_write:
-
 
1700
        push    ecx
1959
        pop     ecx
1701
        lea     ecx, [ebp+FAT.buffer+0x200]
-
 
1702
        cmp     edi, ecx
-
 
1703
        jc      @b
-
 
1704
        push    eax
1960
        add     edi, 0x20
1705
        call    fat_notroot_end_write
1961
        ret     ; CF=0
1706
        pop     eax
1962
fat_notroot_next_sector:
1707
fat_notroot_next_sector:
1963
        push    [ebp+FAT.longname_sec2]
1708
        push    [ebp+FAT.longname_sec2]
1964
        pop     [ebp+FAT.longname_sec1]
1709
        pop     [ebp+FAT.longname_sec1]
Line 1970... Line 1715...
1970
        inc     ecx
1715
        inc     ecx
1971
        cmp     ecx, [ebp+FAT.SECTORS_PER_CLUSTER]
1716
        cmp     ecx, [ebp+FAT.SECTORS_PER_CLUSTER]
1972
        jae     fat_notroot_next_cluster
1717
        jae     fat_notroot_next_cluster
1973
        mov     [eax+4], ecx
1718
        mov     [eax+4], ecx
1974
        jmp     @f
1719
        jmp     @f
-
 
1720
 
-
 
1721
fat_notroot_next_err:
-
 
1722
        pop     ecx
-
 
1723
        movi    eax, ERROR_FILE_NOT_FOUND
-
 
1724
fat1x_root_extend_dir:
-
 
1725
        stc
-
 
1726
        ret
-
 
1727
 
1975
fat_notroot_next_cluster:
1728
fat_notroot_next_cluster:
1976
        push    eax
1729
        push    eax
1977
        mov     eax, [eax]
1730
        mov     eax, [eax]
1978
        call    get_FAT
1731
        call    get_FAT
1979
        mov     ecx, eax
1732
        mov     ecx, eax
Line 2001... Line 1754...
2001
        pop     ecx
1754
        pop     ecx
2002
        mov     eax, ERROR_DEVICE
1755
        mov     eax, ERROR_DEVICE
2003
        stc
1756
        stc
2004
.ret:
1757
.ret:
2005
        ret
1758
        ret
2006
fat_notroot_next_err:
-
 
2007
        pop     ecx
-
 
2008
        mov     eax, ERROR_FILE_NOT_FOUND
-
 
2009
        stc
-
 
2010
        ret
1759
 
2011
fat_notroot_begin_write:
1760
fat_notroot_begin_write:
2012
        push    eax edi
1761
        push    eax edi
2013
        call    fat_notroot_first
1762
        call    fat_notroot_first
2014
        pop     edi eax
1763
        pop     edi eax
2015
        ret
1764
        ret
-
 
1765
 
2016
fat_notroot_end_write:
1766
fat_notroot_end_write:
2017
        call    fat_get_sector
1767
        call    fat_get_sector
2018
        push    ebx
1768
        push    ebx
2019
        lea     ebx, [ebp+FAT.buffer]
1769
        lea     ebx, [ebp+FAT.buffer]
2020
        call    fs_write32_sys
1770
        call    fs_write32_sys
2021
        pop     ebx
1771
        pop     ebx
2022
        ret
1772
        ret
-
 
1773
 
2023
fat_notroot_next_write:
1774
fat_notroot_extend_dir.writeerr:
2024
        push    ecx
-
 
2025
        lea     ecx, [ebp+FAT.buffer+0x200]
-
 
2026
        cmp     edi, ecx
-
 
2027
        jae     @f
-
 
2028
        pop     ecx
1775
        pop     edx
2029
        ret
-
 
2030
@@:
1776
@@:
2031
        push    eax
-
 
2032
        call    fat_notroot_end_write
-
 
2033
        pop     eax
1777
        pop     eax
2034
        jmp     fat_notroot_next_sector
1778
        ret
-
 
1779
 
2035
fat_notroot_extend_dir:
1780
fat_notroot_extend_dir:
2036
        push    eax
1781
        push    eax
2037
        call    get_free_FAT
1782
        call    get_free_FAT
2038
        jnc     .found
-
 
2039
        pop     eax
1783
        jc      @b
2040
        ret     ; CF=1
-
 
2041
.found:
-
 
2042
        push    edx
1784
        push    edx
2043
        mov     edx, [ebp+FAT.fatEND]
1785
        mov     edx, [ebp+FAT.fatEND]
2044
        call    set_FAT
1786
        call    set_FAT
2045
        jc      .writeerr
1787
        jc      .writeerr
2046
        mov     edx, eax
1788
        mov     edx, eax
2047
        mov     eax, [esp+4]
1789
        mov     eax, [esp+4]
2048
        mov     eax, [eax]
1790
        mov     eax, [eax]
2049
        push    edx
1791
        push    edx
2050
        call    set_FAT
1792
        call    set_FAT
2051
        pop     edx
1793
        pop     edx
2052
        jnc     @f
-
 
2053
.writeerr:
-
 
2054
        pop     edx
1794
        jc      .writeerr
2055
        pop     eax
-
 
2056
        stc
-
 
2057
        ret
-
 
2058
@@:
-
 
2059
        push    ecx
1795
        push    ecx
2060
        or      ecx, -1
1796
        or      ecx, -1
2061
        call    add_disk_free_space
1797
        call    add_disk_free_space
2062
; zero new cluster
-
 
2063
        mov     ecx, 512/4
1798
        mov     ecx, 512/4
2064
        lea     edi, [ebp+FAT.buffer]
1799
        lea     edi, [ebp+FAT.buffer]
2065
        push    edi
1800
        push    edi
2066
        xor     eax, eax
1801
        xor     eax, eax
2067
        rep stosd
1802
        rep stosd
Line 2142... Line 1877...
2142
        push    edx
1877
        push    edx
2143
        push    fat1x_root_first
1878
        push    fat1x_root_first
2144
        push    fat1x_root_next
1879
        push    fat1x_root_next
2145
        jmp     .common1
1880
        jmp     .common1
Line -... Line 1881...
-
 
1881
 
-
 
1882
.retNotFound:
-
 
1883
        movi    eax, ERROR_FILE_NOT_FOUND
-
 
1884
        jmp     .ret1
-
 
1885
 
-
 
1886
.noAccess:
-
 
1887
        movi    eax, ERROR_ACCESS_DENIED
-
 
1888
.ret1:
-
 
1889
        mov     [esp+28], eax
-
 
1890
        call    fat_unlock
-
 
1891
        popad
-
 
1892
        xor     ebx, ebx
-
 
1893
        ret
-
 
1894
 
-
 
1895
.full:
-
 
1896
        movi    eax, ERROR_DISK_FULL
-
 
1897
        jmp     .ret1
2146
 
1898
 
2147
.noroot:
-
 
2148
        mov     eax, ERROR_ACCESS_DENIED
1899
.noroot:
2149
        cmp     byte [edi+1], 0
1900
        cmp     byte [edi+1], 0
2150
        jz      .ret1
1901
        jz      .noAccess
2151
; check existence
1902
; check existence
2152
        mov     byte [edi], 0
1903
        mov     byte [edi], 0
2153
        push    edi
1904
        push    edi
2154
        call    hd_find_lfn
1905
        call    hd_find_lfn
2155
        pop     esi
1906
        pop     esi
2156
        mov     byte [esi], '/'
-
 
2157
        jnc     @f
-
 
2158
.notfound0:
-
 
2159
        mov     eax, ERROR_FILE_NOT_FOUND
-
 
2160
.ret1:
-
 
2161
        mov     [esp+28], eax
1907
        mov     byte [esi], '/'
2162
        call    fat_unlock
-
 
2163
        popad
-
 
2164
        xor     ebx, ebx
-
 
2165
        ret
-
 
2166
 
-
 
2167
@@:
1908
        jc      .retNotFound
2168
        inc     esi
1909
        inc     esi
2169
        test    byte [edi+11], 0x10     ; must be directory
-
 
2170
        mov     eax, ERROR_ACCESS_DENIED
1910
        test    byte [edi+11], 0x10
2171
        jz      .ret1
1911
        jz      .noAccess   ; file
2172
        mov     edx, [edi+20-2]
1912
        mov     edx, [edi+20-2]
2173
        mov     dx, [edi+26]            ; ebp=cluster
1913
        mov     dx, [edi+26]
2174
        mov     eax, ERROR_FS_FAIL
1914
        movi    eax, ERROR_FS_FAIL
2175
        cmp     edx, 2
1915
        cmp     edx, 2
2176
        jb      .ret1
1916
        jb      .ret1
2177
.pushnotroot:
1917
.pushnotroot:
2178
        push    edx
1918
        push    edx
Line 2203... Line 1943...
2203
 
1943
 
2204
.exists_file:
1944
.exists_file:
2205
        cmp     byte [esp+36+28], 0
1945
        cmp     byte [esp+36+28], 0
2206
        jz      @f
1946
        jz      @f
2207
        add     esp, 36
-
 
2208
        call    fat_unlock
-
 
2209
        popad
-
 
2210
        mov     eax, ERROR_ACCESS_DENIED
1947
        add     esp, 36
2211
        xor     ebx, ebx
-
 
Line 2212... Line 1948...
2212
        ret
1948
        jmp     .noAccess
2213
 
1949
 
2214
@@: ; delete FAT chain
1950
@@: ; delete FAT chain
2215
        push    edi
1951
        push    edi
Line 2238... Line 1974...
2238
        pop     ecx edi esi
1974
        pop     ecx edi esi
2239
        call    fat_next_short_name
1975
        call    fat_next_short_name
2240
        jnc     .test_short_name_loop
1976
        jnc     .test_short_name_loop
2241
.disk_full:
1977
.disk_full:
2242
        add     esp, 12+36
1978
        add     esp, 12+36
2243
        call    fat_unlock
-
 
2244
        popa
-
 
2245
        mov     eax, ERROR_DISK_FULL
-
 
2246
        xor     ebx, ebx
1979
        jmp     .full
2247
        ret
-
 
Line 2248... Line 1980...
2248
 
1980
 
2249
.notfound:  ; generate short name
1981
.notfound:  ; generate short name
2250
        call    fat_name_is_legal
1982
        call    fat_name_is_legal
2251
        jc      @f
1983
        jc      @f
2252
        add     esp, 36
1984
        add     esp, 36
2253
        call    fat_unlock
-
 
2254
        popad
-
 
2255
        mov     eax, ERROR_FILE_NOT_FOUND
-
 
2256
        xor     ebx, ebx
-
 
Line 2257... Line 1985...
2257
        ret
1985
        jmp     .retNotFound
2258
 
1986
 
2259
@@:
1987
@@:
2260
        sub     esp, 12
1988
        sub     esp, 12
Line 2319... Line 2047...
2319
        call    dword [eax-4]
2047
        call    dword [eax-4]
2320
        pop     eax
2048
        pop     eax
2321
        jnc     .scan_dir
2049
        jnc     .scan_dir
2322
.fsfrfe3:
2050
.fsfrfe3:
2323
        add     esp, 12+8+12+36
2051
        add     esp, 12+8+12+36
2324
        call    fat_unlock
-
 
2325
        popad
-
 
2326
        mov     eax, ERROR_DEVICE
2052
        movi    eax, ERROR_DEVICE
2327
        xor     ebx, ebx
2053
        jmp     .ret1
2328
        ret
-
 
Line 2329... Line 2054...
2329
 
2054
 
2330
.scan_dir:
2055
.scan_dir:
2331
        cmp     byte [edi], 0
2056
        cmp     byte [edi], 0
2332
        jz      .free
2057
        jz      .free
Line 2346... Line 2071...
2346
        lea     eax, [esp+16+8+12+8]
2071
        lea     eax, [esp+16+8+12+8]
2347
        call    dword [eax+20]          ; extend directory
2072
        call    dword [eax+20]          ; extend directory
2348
        pop     eax
2073
        pop     eax
2349
        jnc     .scan_dir
2074
        jnc     .scan_dir
2350
        add     esp, 12+8+12+36
2075
        add     esp, 12+8+12+36
2351
        call    fat_unlock
-
 
2352
        popad
-
 
2353
        mov     eax, ERROR_DISK_FULL
-
 
2354
        xor     ebx, ebx
2076
        jmp     .full
2355
        ret
-
 
Line 2356... Line 2077...
2356
 
2077
 
2357
.free:
2078
.free:
2358
        test    ecx, ecx
2079
        test    ecx, ecx
2359
        jnz     @f
2080
        jnz     @f
Line 2517... Line 2238...
2517
; we can write directly from given buffer
2238
; we can write directly from given buffer
2518
        mov     ebx, esi
2239
        mov     ebx, esi
2519
        add     esi, ecx
2240
        add     esi, ecx
2520
        jmp     .writecommon
2241
        jmp     .writecommon
Line -... Line 2242...
-
 
2242
 
-
 
2243
.writedir:
-
 
2244
        push    512
-
 
2245
        lea     edi, [ebp+FAT.buffer]
-
 
2246
        mov     ebx, edi
-
 
2247
        mov     ecx, [ebp+FAT.SECTORS_PER_CLUSTER]
-
 
2248
        shl     ecx, 9
-
 
2249
        cmp     ecx, [esp+16]
-
 
2250
        jnz     .writedircont
-
 
2251
        dec     dword [esp+20]
-
 
2252
        push    esi
-
 
2253
        mov     ecx, 32/4
-
 
2254
        rep movsd
-
 
2255
        pop     esi
-
 
2256
        mov     dword [edi-32], '.   '
-
 
2257
        mov     dword [edi-32+4], '    '
-
 
2258
        mov     dword [edi-32+8], '    '
-
 
2259
        mov     byte [edi-32+11], 10h
-
 
2260
        push    esi
-
 
2261
        mov     ecx, 32/4
-
 
2262
        rep movsd
-
 
2263
        pop     esi
-
 
2264
        mov     dword [edi-32], '..  '
-
 
2265
        mov     dword [edi-32+4], '    '
-
 
2266
        mov     dword [edi-32+8], '    '
-
 
2267
        mov     byte [edi-32+11], 10h
-
 
2268
        mov     ecx, [esp+20+36]
-
 
2269
        cmp     ecx, [ebp+FAT.ROOT_CLUSTER]
-
 
2270
        jnz     @f
-
 
2271
        xor     ecx, ecx
-
 
2272
@@:
-
 
2273
        mov     word [edi-32+26], cx
-
 
2274
        shr     ecx, 16
-
 
2275
        mov     [edi-32+20], cx
-
 
2276
        jmp     .writedircont
2521
 
2277
 
2522
.writeshort:
2278
.writeshort:
2523
        mov     ecx, [esp+12]
2279
        mov     ecx, [esp+12]
2524
        push    ecx
2280
        push    ecx
2525
        lea     edi, [ebp+FAT.buffer]
2281
        lea     edi, [ebp+FAT.buffer]
Line 2597... Line 2353...
2597
        call    update_disk
2353
        call    update_disk
2598
        call    fat_unlock
2354
        call    fat_unlock
2599
        popad
2355
        popad
2600
        ret
2356
        ret
Line 2601... Line -...
2601
 
-
 
2602
.writedir:
-
 
2603
        push    512
-
 
2604
        lea     edi, [ebp+FAT.buffer]
-
 
2605
        mov     ebx, edi
-
 
2606
        mov     ecx, [ebp+FAT.SECTORS_PER_CLUSTER]
-
 
2607
        shl     ecx, 9
-
 
2608
        cmp     ecx, [esp+16]
-
 
2609
        jnz     .writedircont
-
 
2610
        dec     dword [esp+20]
-
 
2611
        push    esi
-
 
2612
        mov     ecx, 32/4
-
 
2613
        rep movsd
-
 
2614
        pop     esi
-
 
2615
        mov     dword [edi-32], '.   '
-
 
2616
        mov     dword [edi-32+4], '    '
-
 
2617
        mov     dword [edi-32+8], '    '
-
 
2618
        mov     byte [edi-32+11], 10h
-
 
2619
        push    esi
-
 
2620
        mov     ecx, 32/4
-
 
2621
        rep movsd
-
 
2622
        pop     esi
-
 
2623
        mov     dword [edi-32], '..  '
-
 
2624
        mov     dword [edi-32+4], '    '
-
 
2625
        mov     dword [edi-32+8], '    '
-
 
2626
        mov     byte [edi-32+11], 10h
-
 
2627
        mov     ecx, [esp+20+36]
-
 
2628
        cmp     ecx, [ebp+FAT.ROOT_CLUSTER]
-
 
2629
        jnz     @f
-
 
2630
        xor     ecx, ecx
-
 
2631
@@:
-
 
2632
        mov     word [edi-32+26], cx
-
 
2633
        shr     ecx, 16
-
 
2634
        mov     [edi-32+20], cx
-
 
2635
        jmp     .writedircont
-
 
2636
 
2357
 
2637
@@:
2358
@@:
2638
        or      eax, -1
2359
        or      eax, -1
2639
        rep stosw
2360
        rep stosw
Line 2652... Line 2373...
2652
        ret
2373
        ret
Line 2653... Line 2374...
2653
 
2374
 
2654
;----------------------------------------------------------------
2375
;----------------------------------------------------------------
2655
fat_Write:
2376
fat_Write:
2656
        call    fat_lock
-
 
2657
        push    edi
2377
        call    fat_lock
2658
        call    hd_find_lfn
2378
        call    hd_find_lfn
2659
        jc      .error
2379
        jc      .error
2660
        cmp     dword [ebx+8], 0
2380
        cmp     dword [ebx+8], 0
2661
        jnz     .eof    ; FAT does not support files larger than 4GB
2381
        jnz     .eof    ; FAT does not support files larger than 4GB
Line 2704... Line 2424...
2704
        pop     eax eax ecx edx
2424
        pop     eax eax ecx edx
2705
.error:
2425
.error:
2706
        push    eax
2426
        push    eax
2707
@@:
2427
@@:
2708
        call    fat_unlock
2428
        call    fat_unlock
2709
        pop     eax edi
2429
        pop     eax
2710
        ret
2430
        ret
Line 2711... Line 2431...
2711
 
2431
 
2712
.eof:
2432
.eof:
2713
        push    ERROR_END_OF_FILE
2433
        push    ERROR_END_OF_FILE
Line 2948... Line 2668...
2948
        pop     eax edx esi
2668
        pop     eax edx esi
2949
        movi    eax, ERROR_DISK_FULL
2669
        movi    eax, ERROR_DISK_FULL
2950
        stc
2670
        stc
2951
        ret
2671
        ret
Line 2952... Line -...
2952
 
-
 
2953
fat_update_datetime:
-
 
2954
        call    get_time_for_file
-
 
2955
        mov     [edi+22], ax            ; last write time
-
 
2956
        call    get_date_for_file
-
 
2957
        mov     [edi+24], ax            ; last write date
-
 
2958
        mov     [edi+18], ax            ; last access date
-
 
2959
        ret
-
 
2960
 
2672
 
2961
;----------------------------------------------------------------
2673
;----------------------------------------------------------------
2962
fat_SetFileEnd:
2674
fat_SetFileEnd:
2963
        call    fat_lock
-
 
2964
        push    edi
2675
        call    fat_lock
2965
        call    hd_find_lfn
2676
        call    hd_find_lfn
2966
        jc      .reteax
2677
        jc      .reteax
2967
; must not be directory
2678
; must not be directory
2968
        test    byte [edi+11], 10h
2679
        test    byte [edi+11], 10h
2969
        jnz     .access_denied
2680
        jnz     .access_denied
2970
; file size must not exceed 4 Gb
2681
; file size must not exceed 4 Gb
2971
        cmp     dword [ebx+8], 0
2682
        cmp     dword [ebx+8], 0
2972
        jnz     .endOfFile
2683
        jnz     .endOfFile
2973
        push    eax     ; save directory sector
2684
        push    eax     ; save directory sector
-
 
2685
; set file modification date/time to current
-
 
2686
        call    get_time_for_file
2974
; set file modification date/time to current
2687
        mov     [edi+22], ax    ; last write
-
 
2688
        call    get_date_for_file
-
 
2689
        mov     [edi+24], ax    ; last write
2975
        call    fat_update_datetime
2690
        mov     [edi+18], ax    ; last access
2976
        mov     eax, [ebx+4]
2691
        mov     eax, [ebx+4]
2977
        cmp     eax, [edi+28]
2692
        cmp     eax, [edi+28]
2978
        jb      .truncate
2693
        jb      .truncate
2979
        ja      .expand
2694
        ja      .expand
Line 3083... Line 2798...
3083
        pop     eax ecx ecx ebp ebx ecx
2798
        pop     eax ecx ecx ebp ebx ecx
3084
.reteax:
2799
.reteax:
3085
        push    eax
2800
        push    eax
3086
.ret:
2801
.ret:
3087
        call    fat_unlock
2802
        call    fat_unlock
3088
        pop     eax edi
2803
        pop     eax
3089
        ret
2804
        ret
Line 3090... Line 2805...
3090
 
2805
 
3091
.error_fat:
2806
.error_fat:
3092
        pop     eax
2807
        pop     eax
Line 3183... Line 2898...
3183
        rep stosb
2898
        rep stosb
3184
        pop     eax
2899
        pop     eax
3185
        call    fs_write32_app
2900
        call    fs_write32_app
3186
        pop     ebx
2901
        pop     ebx
3187
.truncate_done:
2902
.truncate_done:
3188
        pop     ecx eax edi
2903
        pop     ecx eax
3189
        call    update_disk
2904
        call    update_disk
3190
        call    fat_unlock
2905
        call    fat_unlock
3191
        xor     eax, eax
2906
        xor     eax, eax
3192
        ret
2907
        ret
Line 3196... Line 2911...
3196
        cmp     byte [esi], 0
2911
        cmp     byte [esi], 0
3197
        jnz     @f
2912
        jnz     @f
3198
        mov     eax, 2
2913
        mov     eax, 2
3199
        ret
2914
        ret
3200
@@:
2915
@@:
3201
        push    edi
-
 
3202
        call    fat_lock
2916
        call    fat_lock
3203
        call    hd_find_lfn
2917
        call    hd_find_lfn
3204
        jc      .error
2918
        jc      @f
3205
        push    ebp
2919
        push    ebp
3206
        xor     ebp, ebp
2920
        xor     ebp, ebp
3207
        mov     esi, [ebx+16]
2921
        mov     esi, [ebx+16]
3208
        mov     dword [esi+4], ebp
2922
        mov     dword [esi+4], ebp
3209
        call    fat_entry_to_bdfe2
2923
        call    fat_entry_to_bdfe2
3210
        pop     ebp
2924
        pop     ebp
3211
        call    fat_unlock
-
 
3212
        xor     eax, eax
2925
        xor     eax, eax
3213
        pop     edi
-
 
3214
        ret
-
 
3215
.error:
2926
@@:
3216
        push    eax
2927
        push    eax
3217
        call    fat_unlock
2928
        call    fat_unlock
3218
        pop     eax
2929
        pop     eax
3219
        pop     edi
-
 
3220
        ret
2930
        ret
Line 3221... Line 2931...
3221
 
2931
 
3222
;----------------------------------------------------------------
2932
;----------------------------------------------------------------
3223
fat_SetFileInfo:
-
 
3224
        push    edi
2933
fat_SetFileInfo:
3225
        call    fat_lock
2934
        call    fat_lock
3226
        call    hd_find_lfn
2935
        call    hd_find_lfn
3227
        jc      .error
2936
        jc      @f
3228
        push    eax
2937
        push    eax
3229
        mov     edx, [ebx+16]
2938
        mov     edx, [ebx+16]
3230
        call    bdfe_to_fat_entry
2939
        call    bdfe_to_fat_entry
3231
        pop     eax
2940
        pop     eax
3232
        lea     ebx, [ebp+FAT.buffer]
2941
        lea     ebx, [ebp+FAT.buffer]
3233
        call    fs_write32_sys
2942
        call    fs_write32_sys
3234
        call    update_disk
-
 
3235
        call    fat_unlock
-
 
3236
        pop     edi
2943
        call    update_disk
3237
        xor     eax, eax
-
 
3238
        ret
2944
        xor     eax, eax
3239
.error:
2945
@@:
3240
        push    eax
2946
        push    eax
3241
        call    fat_unlock
2947
        call    fat_unlock
3242
        pop     eax
-
 
3243
        pop     edi
2948
        pop     eax
Line 3244... Line 2949...
3244
        ret
2949
        ret
3245
 
2950
 
3246
;----------------------------------------------------------------
2951
;----------------------------------------------------------------
3247
fat_Delete:
2952
fat_Delete:
3248
        call    fat_lock
2953
        call    fat_lock
3249
        and     [ebp+FAT.longname_sec1], 0
-
 
3250
        and     [ebp+FAT.longname_sec2], 0
2954
        and     [ebp+FAT.longname_sec1], 0
3251
        push    edi
2955
        and     [ebp+FAT.longname_sec2], 0
3252
        call    hd_find_lfn
2956
        call    hd_find_lfn
3253
        jc      .notFound
2957
        jc      .notFound
3254
        cmp     dword [edi], '.   '
2958
        cmp     dword [edi], '.   '
Line 3305... Line 3009...
3305
        popad
3009
        popad
3306
.err2:
3010
.err2:
3307
        push    ERROR_DEVICE
3011
        push    ERROR_DEVICE
3308
.ret:
3012
.ret:
3309
        call    fat_unlock
3013
        call    fat_unlock
3310
        pop     eax edi
3014
        pop     eax
3311
        ret
3015
        ret
Line 3312... Line 3016...
3312
 
3016
 
3313
.notFound:
3017
.notFound:
3314
        push    ERROR_FILE_NOT_FOUND
3018
        push    ERROR_FILE_NOT_FOUND
Line 3375... Line 3079...
3375
; delete FAT chain
3079
; delete FAT chain
3376
        pop     eax
3080
        pop     eax
3377
        call    clear_cluster_chain
3081
        call    clear_cluster_chain
3378
        call    update_disk
3082
        call    update_disk
3379
        call    fat_unlock
3083
        call    fat_unlock
3380
        pop     edi
-
 
3381
        xor     eax, eax
3084
        xor     eax, eax
3382
        ret
3085
        ret