-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAIN.ASM
228 lines (170 loc) · 5.29 KB
/
MAIN.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
;FOR FAT32 FILE SYSTEM ON HARD DISK
;CAUSION: THE SECTOR SIZE MUST BE 512
[ORG 0X7C00]
[BITS 16]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;BPB RESERVED AREA, WOULD NOT BE COPIED WITH DD (USE "SKIP" IN ORDER TO SKIP THOSE)
%INCLUDE "FAT32HEADERS.INC"
;=====GLOBAL=MACROS=====
;ADDRESS-RELATIVE
%DEFINE STACKBASE 0X7C00
%INCLUDE "LDR_CFG.INC"
;ROOT DIRECTORY: 0X7E00~0X100000 (ABLE TO STORE 1985 SECTORS)
%DEFINE ROOTDIRBASE 0X7E00
%DEFINE FATBASE 0X7E00
%DEFINE FATCHAINBASE 0X700
;FAT32-RELATIVE
;LOAD THE ROOTDIR, GET THE FIRST CLUSTER OF THE GIVEN
;FILE, THAN OVERWRITE THE ROOTDIR REGION WITH FAT TABLE,
;AND WRITE THE LINKED LIST OF SECTOR NUMBER TO "TMPBASE" IN LOW MEMORY
;0X500, ONCETHOSE STEPS ARE DONE, LOAD THE GIVEN FILE INFO "LOADERBASE"
;, WHICH IS 0X7E00, RIGHT AFTER THE BOOT SECTOR REGION
;FIRSTDATASECTOR: DD 0
;instead of using space in data section, i prefer overwriting the FAT32 filesystem metadata "BS_FILSYSTYPE"
;because it is useless now(and loader may use the spce from 0x7c00 to 0x7e00)
%define BS_FILSYSTYPE_ADDR (0X7C00+82)
%define FIRSTDATASECTOR (BS_FILSYSTYPE_ADDR)
%DEFINE SFNSIZE 32
%DEFINE FILENAME_LENGTH 11
;=======================
[SECTION .CODE]
;=========================
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;INITIALIZE EVERYTHING
INIT:
;I THINK IT IS BETTER TO USE FLAT MEMORY MODEL
;THEREFORE NOT SETTING THE DS, ES, SS TO 0X7C0
MOV AX, CS
MOV DS, AX
MOV ES, AX
MOV SS, AX
MOV SP, STACKBASE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CALCULATE_FIRSTDATASECTOR:
;FAT32:
;FIRSTDATASECTOR = BPB_RESVDSECCNT + (BPB_NUMFATS * FATSZ32)
;(BPB_NUMFATS * FATSZ32)
MOV ECX, DWORD [BPB_FATSZ32]
MOVZX EAX, BYTE [BPB_NUMFATS]
MUL ECX
;+BPB_RESVDSECCNT
MOVZX EDX, WORD [BPB_RESVDSECCNT]
ADD EAX, EDX
;THIS WILL RAISE A "MISMATCH IN OPERAND SIZES" EXCEPTION ON COMPILE TIME
;ADD EAX, WORD [BPB_RESVDSECCNT]
MOV DWORD [FIRSTDATASECTOR], EAX
LOAD_ROOTDIR:
;EAX IS NOW FIRSTDATASECTOR, THEREFORE NOT SET AGAIN
.LOAD_INTO_MEM:
MOV EBX, ROOTDIRBASE
MOVZX ECX, BYTE [BPB_SECPERCLUS]
CALL READ_ATA
;CX IS NOW BPB_SECPERCLUS
;CHECKOUT FILES IN THE FIRST CLUSTER (NOTE:ONLY SEARCH FOR THE FIRST ROOTDIRECTORY CLUSTER)
.COMPARE_FILENAME:
%DEFINE SFNSHIFT 5
.SETUP:
MOV ESI, ROOTDIRBASE
.COMPARE_LOOP:
MOV ECX, FILENAME_LENGTH
MOV EDI, LOADER_FILENAME
MOV EDX, ESI
;THE ASSEMBLER I USE NOW (NASM) SEEMS NOT RECONIZING THE FOLLWING INSTRUCTION, SO I USE
;HEX VALUE TO DO THE SAME THING
DB 0X66, 0XF3 ,0X67 ,0XA6 ; REPE CMPSB BYTE PTR [ESI], BYTE PTR ES:[EDI]
JECXZ FILE_FOUND
;ELSE: (IF FILE NOT FOUND...)
MOV ESI, EDX
ADD ESI, SFNSIZE
MOVZX EDX, BYTE [BPB_SECPERCLUS]
ADD EDX, ROOTDIRBASE
CMP EDX, ESI
;JE FILE_LOST
JE $
JMP .COMPARE_LOOP
FILE_FOUND: ;IF FILE FOUND:LOAD THE FAT TABLE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;GETTING THE FIRST CLUSTER OF THE GIVEN FILE HERE;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;ESI IS NOW THE FILE ENTRY
;THE START OF THE SFN ENTRY (OFFSET 0)=ESI-11
;DIR_FSTCLUSHI STARTS FROM OFFSET 20, SIZE 2 (16BITS)
;DIR_FSTCLUSLO STARTS FROM OFFSET 26, SIZE 2 (16BITS)
;ALL IN ALL:
;DIR_FSTCLUSHI=ESI-11+20=ESI+9
;DIR_FSTCLUSLO=ESI-11+26=ESI+15
MOV ESI, EDX
MOV DX, WORD [ESI+20]
SHL EDX, 16
MOVZX ECX, WORD [ESI+26]
ADD EDX, ECX
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LOAD_FATTABLE:
;IF THE FILE IS FOUND, THAN THE FAT TABLE SHOULD BE LOADED, OTHERWISE, THERE IS NO NEED TO LOAD THE TABLE
MOVZX EAX, WORD [BPB_RESVDSECCNT]
MOV EBX, FATBASE
;UNFORTUNATELY, THE INTURRUPT FUNCTION ONLY PROVIDES 16-BIT SECTOR COUNT(WHICH IS STORE IN CX REGISTER)
;SO, IF THE FAT TABLE IS LARGE, THE UPPER PART WILL NOT BE LOADED INSIDE MEMORY
;PLUS, THE CONVENTIONAL MEMORY (0X7E00~0X100000) IS ONLY ABLE TO HELD 1985 SECTORS
;CHECK THE FATSZ(NOTE:IN SECTOR OF 512)
CMP EAX, 1985
JBE .SMALL_FAT
;ELSE: THE FAT IS BIGGER THAN 1985 SECTORS
;THEN: LOAD THE FIRST 1985 SECTORS FROM THE DISK
MOV EAX, 1985
.SMALL_FAT:
MOV ECX, DWORD [BPB_FATSZ32]
CALL READ_ATA
%DEFINE FAT_ENTRY_SIZE 4
;I CALLED IT "FATCHAIN"
;THE CLUSTER NUMBER ARE STORED INSIDE FATCHAIN
LINK_FATCHAIN:
MOV SI, FATCHAINBASE ;SI IS ENOUGH FOR THE LOWER 1MB
MOV EBP, LOAD_LOADER
;EDX IS NOW THE FIRST CLUSTER OF THE FILE, NOT SET AGAIN NOW
MOV BP, WORD PREPARE_LOAD_LOADER ;IF THE CHAIN HITS EOF, THAN GO TO THE NEXT STEP "LOAD_LOADER"
MOV EAX, EDX ;BOTH CMP_STOP AND NXT_CLUSTER TAKE EAX AS ARGUMENT
.CREATE_CHAIN:
SUB EAX, DWORD [BPB_ROOTCLUS]
MOV DWORD [SI], EAX
ADD SI, FAT_ENTRY_SIZE
;mov ebx, eax
ADD EAX, DWORD [BPB_ROOTCLUS]
CALL NXT_CLUSTER
CALL CMP_STOP
JMP .CREATE_CHAIN
;NOTE: THE SECTOR NUMBER TO BE READ=FIRSTDATASECTOR+(VALUE_IN_FATCHAIN*BPB_SECPERCLUS)
PREPARE_LOAD_LOADER:
MOV DWORD [SI], 0XFFFFFFFF
;FINISH THE FATCHAIN, SET A 'STOP' SIGN THERE
MOV EBX, LOADERBASE
MOV SI, FATCHAINBASE
LOAD_LOADER:
MOV EAX, DWORD [SI]
CALL CLUSTER2SECTOR
MOVZX CX, BYTE [BPB_SECPERCLUS]
CALL READ_ATA
ADD SI, 4
MOV EAX, DWORD [SI]
MOV BP, WORD LOADERBASE
;TWEAKED VERSION
;THE "TWEAKED" VERSION RECOGNIZE THE VALUE 0XFFFFFFFF AS EOF
CMP EAX, 0XFFFFFFFF
JNE .2
.1:
;JMP BP
PUSH BP
RET
.2:
;IF NOT EOF...
;POINTER TO CURRENT BUFFER+=(BPB_SECPERCLUS*512)
MOVZX EBP, BYTE [BPB_SECPERCLUS]
SHL EBP, 9 ;*512(SECTOR SIZE IN BYTES)
ADD EBX, EBP
JMP LOAD_LOADER
;=========================
[SECTION .DATA]
LOADER_FILENAME: DB "LOADER SYS"
[SECTION SUBROUTINE]
%INCLUDE "MAIN.INC"