forked from andyvand/CloverNTFSBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntfsbootalt.asm
2772 lines (2122 loc) · 77.2 KB
/
ntfsbootalt.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
page ,132
title ntfsboot - NTFS boot loader
name ntfsboot
; The ROM in the IBM PC starts the boot process by performing a hardware
; initialization and a verification of all external devices. If all goes
; well, it will then load from the boot drive the sector from track 0, head 0,
; sector 1. This sector is placed at physical address 07C00h.
;
; The boot code's sole resposiblity is to find NTLDR, load it at
; address 2000:0000, and then jump to it.
;
; The boot code understands the structure of the NTFS root directory,
; and is capable of reading files. There is no contiguity restriction.
;
MASM equ 1
.xlist
.286
A_DEFINED EQU 1
include ntfs.inc
DoubleWord struc
lsw dw ?
msw dw ?
DoubleWord ends
;
; The following are various segments used by the boot loader. The first
; two are the segments where the boot sector is initially loaded and where
; the boot sector is relocated to. The third is the static location
; where the NTLDR is loaded.
;
BootSeg segment at 07c0h ; this is where the ROM loads us initially.
BootSeg ends
NewSeg segment at 0d00h ; this is where we'll relocate to.
NewSeg ends ; enough for 16 boot sectors +
; 4-sector scratch
; below where we'll load NTLDR.
LdrSeg segment at 2000h ; we want to load the loader at 2000:0000
LdrSeg ends
;/********************** START OF SPECIFICATIONS ************************/
;/* */
;/* SUBROUTINE NAME: ntfsboot */
;/* */
;/* DESCRIPTIVE NAME: Bootstrap loader */
;/* */
;/* FUNCTION: To load NTLDR into memory. */
;/* */
;/* NOTES: ntfsboot is loaded by the ROM BIOS (Int 19H) at */
;/* physical memory location 0000:7C00H. */
;/* ntfsboot runs in real mode. */
;/* This boot record is for NTFS volumes only. */
;/* */
;/* ENTRY POINT: ntfsboot */
;/* LINKAGE: Jump (far) from Int 19H */
;/* */
;/* INPUT: CS:IP = 0000:7C00H */
;/* SS:SP = 0030:00FAH (CBIOS dependent) */
;/* */
;/* EXIT-NORMAL: DL = INT 13 drive number we booted from */
;/* Jmp to main in NTLDR */
;/* */
;/* EXIT-ERROR: None */
;/* */
;/* EFFECTS: NTLDR is loaded into the physical memory */
;/* location 00020000H */
;/* */
;/* MESSAGES: A disk read error occurred. */
;/* The file NTLDR cannot be found. */
;/* Insert a system diskette and restart the system. */
;/* */
;/*********************** END OF SPECIFICATIONS *************************/
BootCode segment ;would like to use BootSeg here, but LINK flips its lid
assume cs:BootCode,ds:nothing,es:nothing,ss:nothing
org 0 ; start at beginning of segment, not 0100h.
public cstart
cstart proc far
jmp start
.errnz ($-cstart) GT (3),<FATAL PROBLEM: JMP is more than three bytes>
org 3
;
; This is a template BPB--anyone who writes boot code to disk
; should either preserve the existing BPB and NTFS information
; or create it anew.
;
Version db "NTFS " ; Must be 8 characters
BPB label byte
BytesPerSector dw 0 ; Size of a physical sector
SectorsPerCluster db 0 ; Sectors per allocation unit
ReservedSectors dw 0 ; Number of reserved sectors
Fats db 0 ; Number of fats
DirectoryEntries dw 0 ; Number of directory entries
Sectors dw 0 ; No. of sectors - no. of hidden sectors
Media db 0 ; Media byte
FatSectors dw 0 ; Number of fat sectors
SectorsPerTrack dw 0 ; Sectors per track
Heads dw 0 ; Number of surfaces
HiddenSectors dd 0 ; Number of hidden sectors
SectorsLong dd 0 ; Number of sectors iff Sectors = 0
;
; The following is the rest of the NTFS Sector Zero information.
; The position and order of DriveNumber and CurrentHead are especially
; important, since those two variables are loaded into a single 16-bit
; register for the BIOS with one instruction.
;
DriveNumber db 80h ; Physical drive number (0 or 80h)
CurrentHead db ? ; Variable to store current head no.
SectorZeroPad1 dw 0
SectorsOnVolume db (size LARGE_INTEGER) dup (0)
MftStartLcn db (size LARGE_INTEGER) dup (0)
Mft2StartLcn db (size LARGE_INTEGER) dup (0)
ClustersPerFrs dd 0
DefClustersPerBuf dd 0
SerialNumber db (size LARGE_INTEGER) dup (0)
CheckSum dd 0
;
; The following variables are not part of the Extended BPB; they're just
; scratch variables for the boot code.
;
SectorBase dd ? ; next sector to read
CurrentTrack dw ? ; current track
CurrentSector db ? ; current sector
SectorCount dw ? ; number of sectors to read
;****************************************************************************
start:
;
; First of all, set up the segments we need (stack and data).
;
cli
xor ax, ax ; Set up the stack to just before
mov ss, ax ; this code. It'll be moved after
mov sp, 7c00h ; we relocate.
sti
mov ax, Bootseg ; Address our BPB with DS.
mov ds, ax
assume ds:BootCode
;
; Now read the 16-sector boot block into memory. Then jump to that
; new version of the boot block, starting in the second sector
; (after the bootrecord sig).
;
mov SectorBase.lsw, 0 ; read sector zero.
mov SectorBase.msw, 0
mov word ptr [SectorCount], 16 ; read boot area
mov ax, NewSeg ; read it at NewSeg.
mov es, ax
sub bx, bx ; at NewSeg:0000.
call DoReadLL ; Call low-level DoRead routine
;
push NewSeg ; we'll jump to NewSeg:0200h.
push offset mainboot ; (the second sector).
ret ; "return" to the second sector.
cstart endp
;*******************************************************************************
;
; Low-level read routine that doesn't work across a 64k addr boundary.
;
; Read SectorCount sectors (starting at SectorBase) to es:bx.
;
; As a side effect, SectorBase is updated (but es:bx are not)
; and SectorCount is reduced to zero.
;
DoReadLL proc
push ax ; save important registers
push bx
push cx
push dx
push es
DoRead$Loop:
.386
mov eax, SectorBase
add eax, HiddenSectors
xor edx,edx
;EDX:EAX = absolute sector number
movzx ecx,word ptr SectorsPerTrack ; get into 32 bit value
div ecx ; (EDX) = sector within track, (EAX)=track
inc dl ; sector numbers are 1-based, not 0
mov CurrentSector, dl
mov edx,eax
shr edx,16
.286
div Heads ; (DX) = head no., (AX) = cylinder
mov CurrentHead, dl
mov CurrentTrack, ax
; CurrentHead is the head for this next disk request
; CurrentTrack is the track for this next request
; CurrentSector is the beginning sector number for this request
;
; Compute the number of sectors that we may be able to read in a single ROM
; request.
;
mov ax, SectorsPerTrack ; could read up to this much
sub al, CurrentSector ; offset within this track
inc ax ; CurrentSector was 1-based
;
; AX is the number of sectors that we may read.
;
cmp ax, SectorCount ; do we need to read whole trk?
jbe DoRead$FullTrack ; yes we do.
mov ax, SectorCount ; no, read a partial track.
;
; AX is now the number of sectors that we SHOULD read.
;
DoRead$FullTrack:
push ax ; save sector count for later calc.
mov ah, 2 ; "read sectors"
mov dx, CurrentTrack ; at this cylinder
mov cl, 6
shl dh, cl ; high 2 bits of DH = bits 8,9 of DX
or dh, CurrentSector ; (DH)=cyl bits | 6-bit sector no.
mov cx, dx ; (CX)=cylinder/sector no. combination
xchg ch, cl ; in the right order
mov dh, CurrentHead
mov dl, 80h ; should be DriveNumber, but...
int 13h ; call BIOS.
pop ax
jb BootErr$he ; If errors report
add SectorBase.lsw, ax ; increment logical sector position
adc SectorBase.msw, 0
sub SectorCount, ax ; exhausted entire sector run?
jbe DoRead$Exit ; yes, we're all done.
shl ax, 9 - 4 ; (AX)=paragraphs read from last track
mov dx, es ; (DX)=segment we last read at
add dx, ax ; (DX)=segment right after last read
mov es, dx ; (ES)=segment to read next track at
jmp DoRead$Loop
;
DoRead$Exit:
pop es
pop dx
pop cx
pop bx
pop ax
ret
DoReadLL endp
;****************************************************************************
;
; BootErr - print error message and hang the system.
;
BootErr proc
BootErr$fnf:
mov si,offset TXT_MSG_SYSINIT_FILE_NOT_FD +2
jmp short BootErr2
BootErr$ntc:
mov si,offset TXT_MSG_SYSINIT_NTLDR_CMPRS +2
jmp short BootErr2
BootErr$he:
mov si,offset TXT_MSG_SYSINIT_BOOT_ERROR +2
BootErr2:
call BootErr$print
mov si,offset TXT_MSG_SYSINIT_INSER_DK +2
call BootErr$print
sti
jmp $ ;Wait forever
BootErr$print:
lodsb ; Get next character
cmp al, 0
je BootErr$Done
mov ah,14 ; Write teletype
mov bx,7 ; Attribute
int 10h ; Print it
jmp BootErr$print
BootErr$Done:
ret
BootErr endp
;****************************************************************************
include ntfsboot.inc ;suck in the message text
ReservedForFuture DB 2 dup(?) ;reserve remaining bytes to prevent NLS
;messages from using them
.errnz ($-cstart) GT (512-2),<FATAL PROBLEM: first sector is too large>
org 512-2
db 55h,0aah
; Name we look for. ntldr_length is the number of characters,
; ntldr_name is the name itself. Note that it is not NULL
; terminated, and doesn't need to be.
;
ntldr_name_length dw 4
ntldr_name dw 'B', 'O', 'O', 'T'
ntldr_alt db 0
db 0
; Predefined name for index-related attributes associated with an
; index over $FILE_NAME
;
index_name_length dw 4
index_name dw '$', 'I', '3', '0'
; Global variables. These offsets are all relative to NewSeg.
;
AttrList dd 0e000h; Offset of buffer to hold attribute list
MftFrs dd 3000h; Offset of first MFT FRS
SegmentsInMft dd ? ; number of FRS's with MFT Data attribute records
RootIndexFrs dd ? ; Offset of Root Index FRS
AllocationIndexFrs dd ? ; Offset of Allocation Index FRS ; KPeery
BitmapIndexFrs dd ? ; Offset of Bitmap Index FRS ; KPeery
IndexRoot dd ? ; Offset of Root Index $INDEX_ROOT attribute
IndexAllocation dd ? ; Offset of Root Index $INDEX_ALLOCATION attribute
IndexBitmap dd ? ; Offset of Root Index $BITMAP attribute
NtldrFrs dd ? ; Offset of NTLDR FRS
NtldrData dd ? ; Offset of NTLDR $DATA attribute
IndexBlockBuffer dd ? ; Offset of current index buffer
IndexBitmapBuffer dd ? ; Offset of index bitmap buffer
NextBuffer dd ? ; Offset of next free byte in buffer space
BytesPerCluster dd ? ; Bytes per cluster
BytesPerFrs dd ? ; Bytes per File Record Segment
SectorsPerFrs dd ? ; Sectors per File Record Segment
BytesPerIndexBlock dd ? ; Bytes per index alloc block in root index
ClustersPerIndexBlock dd ? ; Clusters per index alloc block in root index
SectorsPerIndexBlock dd ? ; Sectors per index block in root index
.386
SAVE_ALL macro
push es
push ds
pushad
endm
RESTORE_ALL macro
popad
nop
pop ds
pop es
endm
;****************************************************************************
;
; mainboot -
;
;
mainboot proc far
; Get the new ds and the new stack. Note that ss is zero.
;
mov ax, cs ; Set DS to CS
mov ds, ax
shl ax, 4 ; convert to an offset.
cli
mov sp, ax ; load new stack, just before boot code.
sti
; Set up the FRS buffers. The MFT buffer is in a fixed
; location, and the other three come right after it. The
; buffer for index allocation blocks comes after that.
;
; Compute the useful constants associated with the volume
;
movzx eax, BytesPerSector ; eax = Bytes per Sector
movzx ebx, SectorsPerCluster ; ebx = Sectors Per Cluster
mul ebx ; eax = Bytes per Cluster
mov BytesPerCluster, eax
mov ecx, ClustersPerFrs ; ecx = clusters per frs
cmp cl, 0 ; is ClustersPerFrs less than zero?
jg mainboot$1
; If the ClustersPerFrs field is negative, we calculate the number
; of bytes per FRS by negating the value and using that as a shif count.
;
neg cl
mov eax, 1
shl eax, cl ; eax = bytes per frs
jmp mainboot$2
mainboot$1:
; Otherwise if ClustersPerFrs was positive, we multiply by bytes
; per cluster.
mov eax, BytesPerCluster
mul ecx ; eax = bytes per frs
mainboot$2:
mov BytesPerFrs, eax
movzx ebx, BytesPerSector
xor edx, edx ; zero high part of dividend
div ebx ; eax = sectors per frs
mov SectorsPerFrs, eax
; Set up the MFT FRS's---this will read all the $DATA attribute
; records for the MFT.
;
call SetupMft
; Set up the remaining FRS buffers. The RootIndex FRS comes
; directly after the last MFT FRS, followed by the NTLdr FRS
; and the Index Block buffer.
;
mov ecx, NextBuffer
mov RootIndexFrs, ecx
add ecx, BytesPerFrs ; AllocationFrs may be different
mov AllocationIndexFrs, ecx ; from RootIndexFrs - KPeery
add ecx, BytesPerFrs ; BitmapFrs may be different
mov BitmapIndexFrs, ecx ; from RootIndexFrs - KPeery
add ecx, BytesPerFrs
mov NtldrFrs, ecx
add ecx, BytesPerFrs
mov IndexBlockBuffer, ecx
;
; Read the root index, allocation index and bitmap FRS's and locate
; the interesting attributes.
;
mov eax, $INDEX_ROOT
mov ecx, RootIndexFrs
call LoadIndexFrs
or eax, eax
jz BootErr$he
mov IndexRoot, eax ; offset in Frs buffer
mov eax, $INDEX_ALLOCATION ; Attribute type code
mov ecx, AllocationIndexFrs ; FRS to search
call LoadIndexFrs
mov IndexAllocation, eax
mov eax, $BITMAP ; Attribute type code
mov ecx, BitmapIndexFrs ; FRS to search
call LoadIndexFrs
mov IndexBitmap, eax
; Consistency check: the index root must exist, and it
; must be resident.
;
mov eax, IndexRoot
or eax, eax
jz BootErr$he
cmp [eax].ATTR_FormCode, RESIDENT_FORM
jne BootErr$he
; Determine the size of the index allocation buffer based
; on information in the $INDEX_ROOT attribute. The index
; bitmap buffer comes immediately after the index block buffer.
;
; eax -> $INDEX_ROOT attribute record
;
lea edx, [eax].ATTR_FormUnion ; edx -> resident info
add ax, [edx].RES_ValueOffset ; eax -> value of $INDEX_ROOT
movzx ecx, [eax].IR_ClustersPerBuffer
mov ClustersPerIndexBlock, ecx
mov ecx, [eax].IR_BytesPerBuffer
mov BytesPerIndexBlock, ecx
mov eax, BytesPerIndexBlock
movzx ecx, BytesPerSector
xor edx, edx
div ecx ; eax = sectors per index block
mov SectorsPerIndexBlock, eax
mov eax, IndexBlockBuffer
add eax, BytesPerIndexBlock
mov IndexBitmapBuffer, eax
; Next consistency check: if the $INDEX_ALLOCATION attribute
; exists, the $INDEX_BITMAP attribute must also exist.
;
cmp IndexAllocation, 0
je mainboot30
cmp IndexBitmap, 0 ; since IndexAllocation exists, the
je BootErr$he ; bitmap must exist, too.
; Since the bitmap exists, we need to read it into the bitmap
; buffer. If it's resident, we can just copy the data.
;
mov ebx, IndexBitmap ; ebx -> index bitmap attribute
push ds
pop es
mov edi, IndexBitmapBuffer ; es:edi -> index bitmap buffer
call ReadWholeAttribute
mainboot30:
;
; OK, we've got the index-related attributes.
;
mov cx, 2000 ; loop counter = max 2000 miliseconds in total
.loop:
mov ah, 01 ; int 0x16, Func 0x01 - get keyboard status/preview key
int 16h
jz .wait ; no keypress - wait and loop again
xor ah, ah ; read the char from buffer to spend it
int 16h
; have a key - ASCII is in al - put it to file name /boot<pressed key>
mov ntldr_alt, al
cmp al, 0
jz .bootFileSet
mov ntldr_name_length, 5
jmp .bootFileSet ; try to boot
.wait:
; waith for 1 ms: int 0x15, Func 0x86 (wait for cx:dx microseconds)
push cx ; save loop counter
xor cx, cx
mov dx, 1000
mov ah, 86h
int 15h
pop cx ; restore loop counter
loop .loop
; no keypress so far
; change filename to /boot by putting space as 5th char
; and try to load
; mov BYTE [gFileName + 4], ' '
.bootFileSet:
movzx ecx, ntldr_name_length ; ecx = name length in characters
mov eax, offset ntldr_name ; eax -> name
call FindFile
or eax, eax
jz BootErr$fnf
; Read the FRS for NTLDR and find its data attribute.
;
; eax -> Index Entry for NTLDR.
;
mov eax, [eax].IE_FileReference.REF_LowPart
push ds
pop es ; es:edi = target buffer
mov edi, NtldrFrs
call ReadFrs
mov eax, NtldrFrs ; pointer to FRS
mov ebx, $DATA ; requested attribute type
mov ecx, 0 ; attribute name length in characters
mov edx, 0 ; attribute name (NULL if none)
call LocateAttributeRecord
; eax -> $DATA attribute for NTLDR
;
or eax, eax ; if eax is zero, attribute not found.
jz BootErr$fnf
; Get the attribute record header flags, and make sure none of the
; `compressed' bits are set
movzx ebx, [eax].ATTR_Flags
and ebx, ATTRIBUTE_FLAG_COMPRESSION_MASK
jnz BootErr$ntc
mov ebx, eax ; ebx -> $DATA attribute for NTLDR
push LdrSeg
pop es ; es = segment addres to read into
sub edi, edi ; es:edi = buffer address
call ReadWholeAttribute
;
; We've loaded NTLDR--jump to it.
;
; Before we go to NTLDR, set up the registers the way it wants them:
; DL = INT 13 drive number we booted from
;
mov dl, DriveNumber
mov ax,1000
mov es, ax ; we don't really need this
lea si, BPB
sub ax,ax
push LdrSeg
push ax
retf ; "return" to NTLDR.
mainboot endp
;****************************************************************************
;
; DoRead - read SectorCount sectors into ES:BX starting from sector
; SectorBase.
;
; NOTE: This code WILL NOT WORK if ES:BX does not point to an address whose
; physical address (ES * 16 + BX) MOD 512 != 0.
;
; DoRead adds to ES rather than BX in the main loop so that runs longer than
; 64K can be read with a single call to DoRead.
;
; Note that DoRead (unlike DoReadLL) saves and restores SectorCount
; and SectorBase
;
.286
DoRead proc
push ax ; save important registers
push bx
push cx
push dx
push es
push SectorCount ; save state variables too
push SectorBase.lsw
push SectorBase.msw
;
; Calculate how much we can read into what's left of the current 64k
; physical address block, and read it.
;
;
mov ax,bx
shr ax,4
mov cx,es
add ax,cx ; ax = paragraph addr
;
; Now calc maximum number of paragraphs that we can read safely:
; 4k - ( ax mod 4k )
;
and ax,0fffh
sub ax,1000h
neg ax
;
; Calc CX = number of paragraphs to be read
;
mov cx,SectorCount ; convert SectorCount to paragraph cnt
shl cx,9-4
DoRead$Loop64:
push cx ; save cpRead
cmp ax,cx ; ax = min(cpReadSafely, cpRead)
jbe @F
mov ax,cx
@@:
push ax
;
; Calculate new SectorCount from amount we can read
;
shr ax,9-4
mov SectorCount,ax
call DoReadLL
pop ax ; ax = cpActuallyRead
pop cx ; cx = cpRead
sub cx,ax ; Any more to read?
jbe DoRead$Exit64 ; Nope.
;
; Adjust ES:BX by amount read
;
mov dx,es
add dx,ax
mov es,dx
;
; Since we're now reading on a 64k byte boundary, cpReadSafely == 4k.
;
mov ax,01000h ; 16k paragraphs per 64k segment
jmp short DoRead$Loop64 ; and go read some more.
DoRead$Exit64:
pop SectorBase.msw ; restore all this crap
pop SectorBase.lsw
pop SectorCount
pop es
pop dx
pop cx
pop bx
pop ax
ret
DoRead endp
.386
;****************************************************************************
;
; ReadClusters - Reads a run of clusters from the disk.
;
; ENTRY: eax == LCN to read
; edx == clusters to read
; es:edi -> Target buffer
;
; USES: none (preserves all registers)
;
ReadClusters proc near
SAVE_ALL
mov ebx, edx ; ebx = clusters to read.
movzx ecx, SectorsPerCluster ; ecx = cluster factor
mul ecx ; Convert LCN to sectors (wipes out edx!)
mov SectorBase, eax ; Store starting sector in SectorBase
mov eax, ebx ; eax = number of clusters
mul ecx ; Convert EAX to sectors (wipes out edx!)
mov SectorCount, ax ; Store number of sectors in SectorCount
; Note that ReadClusters gets its target buffer in es:edi but calls
; the DoRead worker function that takes a target in es:bx--we need
; to normalize es:edi so that we don't overflow bx.
;
mov bx, di
and bx, 0Fh
mov ax, es
shr edi, 4
add ax, di ; ax:bx -> target buffer
push ax
pop es ; es:bx -> target buffer
call DoRead
RESTORE_ALL
ret
ReadClusters endp
;
;****************************************************************************
;
; LocateAttributeRecord -- Find an attribute record in an FRS.
;
; ENTRY: EAX -- pointer to FRS
; EBX -- desired attribute type code
; ECX -- length of attribute name in characters
; EDX -- pointer to attribute name
;
; EXIT: EAX points at attribute record (0 indicates not found)
;
; USES: All
;
LocateAttributeRecord proc near
; get the first attribute record.
;
add ax, word ptr[eax].FRS_FirstAttribute
; eax -> next attribute record to investigate.
; ebx == desired type
; ecx == name length
; edx -> pointer to name
;
lar10:
cmp [eax].ATTR_TypeCode, 0ffffffffh
je lar99
cmp dword ptr[eax].ATTR_TypeCode, ebx
jne lar80
; this record is a potential match. Compare the names:
;
; eax -> candidate record
; ebx == desired type
; ecx == name length
; edx -> pointer to name
;
or ecx, ecx ; Did the caller pass in a name length?
jnz lar20
; We want an attribute with no name--the current record is
; a match if and only if it has no name.
;
cmp [eax].ATTR_NameLength, 0
jne lar80 ; Not a match.
; It's a match, and eax is set up correctly, so return.
;
ret
; We want a named attribute.
;
; eax -> candidate record
; ebx == desired type
; ecx == name length
; edx -> pointer to name
;
lar20:
cmp cl, [eax].ATTR_NameLength
jne lar80 ; Not a match.
; Convert name in current record to uppercase.
;
mov esi, eax
add si, word ptr[eax].ATTR_NameOffset
call UpcaseName
; eax -> candidate record
; ebx == desired type
; ecx == name length
; edx -> pointer to name
; esi -> Name in current record (upcased)
;
push ecx ; save cx
push ds ; Copy data segment into es
pop es
mov edi, edx ; note that esi is already set up.
repe cmpsw ; zero flag is set if equal
pop ecx ; restore cx
jnz lar80 ; not a match
; eax points at a matching record.
;
ret
;
; This record doesn't match; go on to the next.
;
; eax -> rejected candidate attribute record
; ebx == desired type
; ecx == Name length
; edx -> desired name
;
lar80: cmp [eax].ATTR_RecordLength, 0 ; if the record length is zero
je lar99 ; the FRS is corrupt.
add eax, [eax].ATTR_RecordLength; Go to next record
jmp lar10 ; and try again
; Didn't find it.
;
lar99: sub eax, eax
ret
LocateAttributeRecord endp
;****************************************************************************
;
; LocateIndexEntry -- Find an index entry in a file name index
;
; ENTRY: EAX -> pointer to index header
; EBX -> file name to find
; ECX == length of file name in characters
;
; EXIT: EAX points at index entry. NULL to indicate failure.
;
; USES: All
;
LocateIndexEntry proc near
; Convert the input name to upper-case
;
mov esi, ebx
call UpcaseName
; DEBUG CODE
;
; call PrintName
; call Debug2
;
; END DEBUG CODE
add eax, [eax].IH_FirstIndexEntry
; EAX -> current entry
; EBX -> file name to find
; ECX == length of file name in characters
;
lie10: test [eax].IE_Flags, INDEX_ENTRY_END ; Is it the end entry?
jnz lie99
lea edx, [eax].IE_Value ; edx -> FILE_NAME attribute value
; DEBUG CODE -- list file names as they are examined
;
; SAVE_ALL
;
; call Debug3
; movzx ecx, [edx].FN_FileNameLength ; ecx = chars in name
; lea esi, [edx].FN_FileName ; esi -> name
; call PrintName
;
; RESTORE_ALL
;
; END DEBUG CODE
; EAX -> current entry
; EBX -> file name to find
; ECX == length of file name in characters
; EDX -> FILE_NAME attribute
;
cmp cl, [edx].FN_FileNameLength ; Is name the right length?
jne lie80
lea esi, [edx].FN_FileName ; Get name from FILE_NAME structure
call UpcaseName
push ecx ; save ecx
push ds
pop es ; copy data segment into es for cmpsw
mov edi, ebx ; edi->search name (esi already set up)
repe cmpsw ; zero flag is set if they're equal
pop ecx ; restore ecx
jnz lie80
; the current entry matches the search name, and eax points at it.
;
ret
; The current entry is not a match--get the next one.
; EAX -> current entry
; EBX -> file name to find
; ECX == length of file name in characters
;
lie80: cmp [eax].IE_Length, 0 ; If the entry length is zero
je lie99 ; then the index block is corrupt.
add ax, [eax].IE_Length ; Get the next entry.
jmp lie10
; Name not found in this block. Set eax to zero and return
;
lie99: xor eax, eax
ret
LocateIndexEntry endp
;****************************************************************************
;
; ReadWholeAttribute - Read an entire attribute value
;
; ENTRY: ebx -> attribute
; es:edi -> target buffer
;
; USES: ALL
;
ReadWholeAttribute proc near
cmp [ebx].ATTR_FormCode, RESIDENT_FORM
jne rwa10
; The attribute is resident.
; ebx -> attribute
; es:edi -> target buffer
;
SAVE_ALL