-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAIN.INC
120 lines (89 loc) · 2.57 KB
/
MAIN.INC
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
%INCLUDE "FAT32HEADERS_ADDR.INC"
READ_ATA:
;ACCESS DISK VIA INT 0X13
;SAVE THE VALUE
PUSHAD
;THE DISK PACKET WILL BE LOCATED AT 0X500 (RIGHT AFTER EBDR DATA REGION)
%DEFINE PACKETBASE 0X500
;WORD 0X00 (RESERVED)
;PACKET SIZE (0X10)
;0X00, 0X10 -> 0X1000 IN LSB
%DEFINE PACKET_HEADER 0X1000
;HOW MANY SECTORS TO READ
%DEFINE SECTOR_COUNT (2)
;LINEAR ADDRESS WILL BE BREAK UP INTO SEGMENT:OFFSET FORM
%DEFINE BUFFER_OFFSET (4)
%DEFINE BUFFER_SEG (6)
;LOGIC BLOCK ADDRESS OFFSET (HIGH, LOW)
%DEFINE LOW_LBA_NUMBER (8)
%DEFINE HIGH_LBA_NUMBER (12)
.SETUP_PACKET:
;CREATE THE PACKET STRUCTURE
;USE AX AS INDEX IN ORDER TO SHRINK THE SIZE OF THE PROGRAM
MOV EBP, EBX
MOV BX, PACKETBASE
MOV WORD [BX], PACKET_HEADER
MOV DWORD [BX+HIGH_LBA_NUMBER], 0
;CONVERT LINEAR ADDRESS INTO SEGMENT AND OFFSET
MOV WORD [BX+BUFFER_OFFSET],BP
SHR EBP, 16
MOV WORD [BX+BUFFER_SEG],BP
MOV WORD [BX+SECTOR_COUNT],CX
;BX=HIGHER PART OF THE ADDRESS(SEGMENT NUMBER)
;THE EXTENDED REGISTER IS ONLY ABLE TO ADDRESS 32 BIT LBA
MOV DWORD [BX+LOW_LBA_NUMBER], EAX
MOV DWORD [BX+HIGH_LBA_NUMBER], 0
;ISSUE AN 0X13 INTERRUPT
MOV AH, 0X42
;MOV DL, 0X80 ;DRIVE NUMBER
;LET'S NOT HARD CODED THE DRIVE NUMBER INTO THE PROGRAM
MOV DL, BYTE [BS_DRVNUM]
MOV SI, PACKETBASE
INT 0X13
;IF THE CARRY FLAG IS SET, THERE MUST BE AN ERROR, HALT
;JC $
POPAD
RET
NXT_CLUSTER:
;CLUSTER NUMBER STARTS FROM 2
;THAT IS TO SAY, THE FIRST DATA SECTOR IS CLUSTER 2, AND SO ON.
;BUT IN THIS FUNCTION, ASSUME CLUSTER NUMBER STARTS FROM 0 (THE FIRST DATA SECTOR IS CLUSTER 0)
;STEP 1: IF THE FIRST DATA SECTOR IS CLUSTER 0, THAN PLUS 2
;ADD EAX, DWORD [BPB_ROOTCLUS]
;STEP 2: LOCATE THE NXTCLUS ENTRY IN FAT TABLE
;NOW, THE EAX REGISTER IS THE CLUSTER NUMBER (STARTS FROM 2)
;STEP 2.1: MUL EAX BY 4 ((OPERATOR *4) = (OPERATOR << 2))
SHL EAX, 2
;STEP 2.2: EAX = EAX + FATBASE
ADD EAX, FATBASE
;STEP 3:EXTRACT THE VALUE OF "NXTCLUSTER", WRITEBACK INTO EAX
MOV EAX, DWORD [EAX]
;SUB EAX, DWORD [BPB_ROOTCLUS]
RET
CLUSTER2SECTOR:
PUSH ECX
PUSH EDX
XOR EDX, EDX
;EAX IS PASSED BY THE CALLER TO CALLEE
MOVZX ECX, BYTE [BPB_SECPERCLUS]
MUL CX
ADD EAX, DWORD [FIRSTDATASECTOR]
POP EDX
POP ECX
RET
CMP_STOP:
;IS IT ORDINARY CLUSTER?
CMP EAX, 0X00_00_00_02
JGE .IN_VALID_CLUSTERNUM_RANGE
.IN_VALID_CLUSTERNUM_RANGE:CMP EAX, 0X0F_FF_FF_EF
JBE .RETURN_LABEL ;FINISH READING, RETURN
;HAVE IT REACHED EOF?
.NOT_VALID_COMPARE_EOF: CMP EAX, 0X0F_FF_FF_F8
JGE .IN_EOF_VALUE_RANGE
.IN_EOF_VALUE_RANGE: CMP EAX, 0X0F_FF_FF_FF
JNBE .RETURN_LABEL
;IF (EOF)
;JMP BP
PUSH BP
.RETURN_LABEL:
RET