]> git.proxmox.com Git - mirror_edk2.git/blame - DuetPkg/BootSector/bs32.S
syntax checked
[mirror_edk2.git] / DuetPkg / BootSector / bs32.S
CommitLineData
32f9b796 1#------------------------------------------------------------------------------\r
2#*\r
3#* Copyright 2006 - 2007, Intel Corporation \r
4#* All rights reserved. This program and the accompanying materials \r
5#* are licensed and made available under the terms and conditions of the BSD License \r
6#* which accompanies this distribution. The full text of the license may be found at \r
7#* http://opensource.org/licenses/bsd-license.php \r
8#* \r
9#* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10#* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11#* \r
12#* bs32.asm\r
13#* \r
14#* Abstract:\r
15#*\r
16#------------------------------------------------------------------------------\r
17\r
18 #.MODEL small\r
19 .stack: \r
20 .486p: \r
21 .code: \r
22\r
23.equ FAT_DIRECTORY_ENTRY_SIZE, 0x0020\r
24.equ FAT_DIRECTORY_ENTRY_SHIFT, 5\r
25.equ BLOCK_SIZE, 0x0200\r
26.equ BLOCK_MASK, 0x01ff\r
27.equ BLOCK_SHIFT, 9\r
28 # "EFILDR_____"\r
29.equ LOADER_FILENAME_PART1, 0x4c494645 # "EFIL"\r
30.equ LOADER_FILENAME_PART2, 0x30325244 # "DR20"\r
31.equ LOADER_FILENAME_PART3, 0x20202030 # "0___"\r
32\r
33 .org 0x00\r
34Ia32Jump: \r
35 jmp BootSectorEntryPoint # JMP inst - 3 bytes\r
36 nop\r
37\r
9ffd16fb 38OemId: .ascii "INTEL " # OemId - 8 bytes\r
32f9b796 39# BPB data below will be fixed by tool\r
40SectorSize: .word 0 # Sector Size - 16 bits\r
41SectorsPerCluster: .byte 0 # Sector Per Cluster - 8 bits\r
42ReservedSectors: .word 0 # Reserved Sectors - 16 bits\r
43NoFats: .byte 0 # Number of FATs - 8 bits\r
44RootEntries: .word 0 # Root Entries - 16 bits\r
45Sectors: .word 0 # Number of Sectors - 16 bits\r
46Media: .byte 0 # Media - 8 bits - ignored\r
47SectorsPerFat: .word 0 # Sectors Per FAT - 16 bits\r
48SectorsPerTrack: .word 0 # Sectors Per Track - 16 bits - ignored\r
49Heads: .word 0 # Heads - 16 bits - ignored\r
50HiddenSectors: .long 0 # Hidden Sectors - 32 bits - ignored\r
51LargeSectors: .long 0 # Large Sectors - 32 bits \r
52\r
53#******************************************************************************\r
54#\r
55#The structure for FAT32 starting at offset 36 of the boot sector. (At this point, \r
56#the BPB/boot sector for FAT12 and FAT16 differs from the BPB/boot sector for FAT32.)\r
57#\r
58#******************************************************************************\r
59\r
60SectorsPerFat32: .long 0 # Sectors Per FAT for FAT32 - 4 bytes\r
61ExtFlags: .word 0 # Mirror Flag - 2 bytes\r
62FSVersion: .word 0 # File System Version - 2 bytes\r
63RootCluster: .long 0 # 1st Cluster Number of Root Dir - 4 bytes\r
64FSInfo: .word 0 # Sector Number of FSINFO - 2 bytes\r
65BkBootSector: .word 0 # Sector Number of Bk BootSector - 2 bytes\r
66Reserved: .fill 12, 1, 0 # Reserved Field - 12 bytes\r
67PhysicalDrive: .byte 0 # Physical Drive Number - 1 byte\r
68Reserved1: .byte 0 # Reserved Field - 1 byte\r
69Signature: .byte 0 # Extended Boot Signature - 1 byte\r
70VolId: .ascii " " # Volume Serial Number - 4 bytes\r
71FatLabel: .ascii " " # Volume Label - 11 bytes\r
72FileSystemType: .ascii "FAT32 " # File System Type - 8 bytes\r
73BootSectorEntryPoint: \r
74 #ASSUME ds:@code\r
75 #ASSUME ss:@code\r
76\r
77# ****************************************************************************\r
78# Start Print\r
79# ****************************************************************************\r
80 leaw %cs:StartString, %si\r
81 call PrintString\r
82\r
83# ****************************************************************************\r
84# Print over\r
85# ****************************************************************************\r
86\r
87 movw %cs, %ax # ax = 0\r
88 movw %ax, %ss # ss = 0\r
89 addw $0x1000, %ax\r
90 movw %ax, %ds\r
91\r
92 movw $0x7c00, %sp # sp = 0x7c00\r
93 movw %sp, %bp # bp = 0x7c00\r
94\r
95 movb $8, %ah # ah = 8 - Get Drive Parameters Function\r
96 movb %dl, PhysicalDrive(%bp) # BBS defines that BIOS would pass the booting driver number to the loader through DL\r
97 int $0x13 # Get Drive Parameters\r
98 xorw %ax, %ax # ax = 0\r
99 movb %dh, %al # al = dh\r
100 incb %al # MaxHead = al + 1\r
101 pushw %ax # 0000:7bfe = MaxHead\r
102 movb %cl, %al # al = cl\r
103 andb $0x3f, %al # MaxSector = al & 0x3f\r
104 pushw %ax # 0000:7bfc = MaxSector\r
105\r
106 cmpw $0xaa55, SectorSignature(%bp) # Verify Boot Sector Signature\r
107 jne BadBootSector\r
108 movw RootEntries(%bp), %cx # cx = RootEntries\r
109 shlw $FAT_DIRECTORY_ENTRY_SHIFT, %cx # cx = cx * 32 = cx * sizeof(FAT_DIRECTORY_ENTRY) = Size of Root Directory in bytes\r
110 movw %cx, %bx # bx = size of the Root Directory in bytes\r
111 andw $BLOCK_MASK, %bx # See if it is an even number of sectors long\r
112 jne BadBootSector # If is isn't, then the boot sector is bad.\r
113 movw %cx, %bx # bx = size of the Root Directory in bytes\r
114 shrw $BLOCK_SHIFT, %bx # bx = size of Root Directory in sectors\r
115 movb NoFats(%bp), %al # al = NoFats\r
116 xorb %ah, %ah # ah = 0 ==> ax = NoFats\r
117 mulw SectorsPerFat32(%bp) # ax = NoFats * SectorsPerFat\r
118 addw ReservedSectors(%bp), %ax # ax = NoFats * SectorsPerFat + ReservedSectors = RootLBA\r
119 addw %bx, %ax # ax = NoFats * SectorsPerFat + ReservedSectors + RootDirSectors = FirstClusterLBA\r
120 movw %ax, (%bp) # Save FirstClusterLBA for later use\r
121\r
122 movw RootCluster(%bp), %ax # ax = StartCluster of Root Directory\r
123 subw $2, %ax # ax = StartCluster - 2\r
124 xorb %bh, %bh\r
125 movb SectorsPerCluster(%bp), %bl # bx = SectorsPerCluster\r
126 mulw %bx # ax = (StartCluster - 2) * SectorsPerCluster\r
127 addw (%bp), %ax # ax = FirstClusterLBA + (StartCluster-2)*SectorsPerCluster\r
128 pushw %ds\r
129 popw %es\r
130 xorw %di, %di # Store directory in es:di = 1000:0000\r
131 call ReadBlocks # Read StartCluster of Root Directory\r
132\r
133 # dx - variable storage (initial value is 0)\r
134 # bx - loader (initial value is 0)\r
135 xorw %dx, %dx\r
136 xorw %bx, %bx\r
137\r
138FindEFILDR: \r
139 cmpl $LOADER_FILENAME_PART1, (%di)\r
140 jne FindVARSTORE\r
141 cmpl $LOADER_FILENAME_PART2, 4(%di)\r
142 jne FindVARSTORE\r
143 cmpl $LOADER_FILENAME_PART3, 7(%di)\r
144 jne FindVARSTORE\r
145 movw 26(%di), %bx # bx = Start Cluster for EFILDR <----------------------------------\r
146 testw %dx, %dx\r
147 je FindNext # Efivar.bin is not loaded\r
148 jmp FoundAll\r
149\r
150FindVARSTORE: \r
151 # if the file is not loader file, see if it's "EFIVAR BIN"\r
152 cmpl $0x56494645, (%di) # Compare to "EFIV"\r
153 jne FindNext\r
154 cmpl $0x20205241, 4(%di) # Compare to "AR "\r
155 jne FindNext\r
156 cmpl $0x4e494220, 7(%di) # Compare to " BIN"\r
157 jne FindNext\r
158 movw %di, %dx # dx = Offset of Start Cluster for Efivar.bin <---------------------\r
159 addw $26, %dx\r
160 testw %bx, %bx\r
161 je FindNext # Efildr is not loaded\r
162 jmp FoundAll\r
163\r
164FindNext: \r
165 # go to next find\r
166 addw $FAT_DIRECTORY_ENTRY_SIZE, %di # Increment di\r
167 subw $FAT_DIRECTORY_ENTRY_SIZE, %cx # Decrement cx\r
168 # TODO: jump to FindVarStore if ...\r
169 jne FindEFILDR\r
170 jmp NotFoundAll\r
171\r
172FoundAll: \r
173FoundEFILDR: \r
174 movw %bx, %cx # cx = Start Cluster for EFILDR <----------------------------------\r
175 movw %cs, %ax # Destination = 2000:0000\r
176 addw $0x2000, %ax\r
177 movw %ax, %es\r
178 xorw %di, %di\r
179ReadFirstClusterOfEFILDR: \r
180 movw %cx, %ax # ax = StartCluster\r
181 subw $2, %ax # ax = StartCluster - 2\r
182 xorb %bh, %bh\r
183 movb SectorsPerCluster(%bp), %bl # bx = SectorsPerCluster\r
184 pushw %dx\r
185 mulw %bx\r
186 popw %dx # ax = (StartCluster - 2) * SectorsPerCluster\r
187 addw (%bp), %ax # ax = FirstClusterLBA + (StartCluster-2)*SectorsPerCluster\r
188 xorb %bh, %bh\r
189 movb SectorsPerCluster(%bp), %bl # bx = Number of Sectors in a cluster\r
190 pushw %es\r
191 call ReadBlocks\r
192 popw %ax\r
193JumpIntoFirstSectorOfEFILDR: \r
194 movw %ax, JumpSegment(%bp)\r
195JumpFarInstruction: \r
196 .byte 0xea\r
197JumpOffset: \r
198 .word 0x000\r
199JumpSegment: \r
200 .word 0x2000\r
201\r
202\r
203PrintString: \r
204 movw $0xb800, %ax\r
205 movw %ax, %es\r
206 movw $0x7c0, %ax\r
207 movw %ax, %ds\r
208 movw $7, %cx\r
209 movw $160, %di\r
210 rep\r
211 movsw\r
212 ret\r
213# ****************************************************************************\r
214# ReadBlocks - Reads a set of blocks from a block device\r
215#\r
216# AX = Start LBA\r
217# BX = Number of Blocks to Read\r
218# ES:DI = Buffer to store sectors read from disk\r
219# ****************************************************************************\r
220\r
221# cx = Blocks\r
222# bx = NumberOfBlocks\r
223# si = StartLBA\r
224\r
225ReadBlocks: \r
226 pusha\r
227 addl LBAOffsetForBootSector(%bp), %eax # Add LBAOffsetForBootSector to Start LBA\r
228 addl HiddenSectors(%bp), %eax # Add HiddenSectors to Start LBA\r
229 movl %eax, %esi # esi = Start LBA\r
230 movw %bx, %cx # cx = Number of blocks to read\r
231ReadCylinderLoop: \r
232 movw $0x7bfc, %bp # bp = 0x7bfc\r
233 movl %esi, %eax # eax = Start LBA\r
234 xorl %edx, %edx # edx = 0\r
235 movzwl (%bp), %ebx # bx = MaxSector\r
236 divl %ebx # ax = StartLBA / MaxSector\r
237 incw %dx # dx = (StartLBA % MaxSector) + 1\r
238 subw %dx, %bx # bx = MaxSector - Sector\r
239 incw %bx # bx = MaxSector - Sector + 1\r
240 cmpw %bx, %cx # Compare (Blocks) to (MaxSector - Sector + 1)\r
241 jg LimitTransfer\r
242 movw %cx, %bx # bx = Blocks\r
243LimitTransfer: \r
244 pushw %cx\r
245 movb %dl, %cl # cl = (StartLBA % MaxSector) + 1 = Sector\r
246 xorw %dx, %dx # dx = 0\r
247 divw 2(%bp) # ax = ax / (MaxHead + 1) = Cylinder \r
248 # dx = ax % (MaxHead + 1) = Head\r
249\r
250 pushw %bx # Save number of blocks to transfer\r
251 movb %dl, %dh # dh = Head\r
252 movw $0x7c00, %bp # bp = 0x7c00\r
253 movb PhysicalDrive(%bp), %dl # dl = Drive Number\r
254 movb %al, %ch # ch = Cylinder\r
255 movb %bl, %al # al = Blocks\r
256 movb $2, %ah # ah = Function 2\r
257 movw %di, %bx # es:bx = Buffer address\r
258 int $0x13\r
259 jc DiskError\r
260 popw %bx\r
261 popw %cx\r
262 movzwl %bx, %ebx\r
263 addl %ebx, %esi # StartLBA = StartLBA + NumberOfBlocks\r
264 subw %bx, %cx # Blocks = Blocks - NumberOfBlocks\r
265 movw %es, %ax\r
266 shlw $(BLOCK_SHIFT-4), %bx\r
267 addw %bx, %ax\r
268 movw %ax, %es # es:di = es:di + NumberOfBlocks*BLOCK_SIZE\r
269 cmpw $0, %cx\r
270 jne ReadCylinderLoop\r
271 popa\r
272 ret\r
273\r
274# ****************************************************************************\r
275# ERROR Condition:\r
276# ****************************************************************************\r
277NotFoundAll: \r
278 # if we found EFILDR, continue\r
279 testw %bx, %bx\r
280 jne FoundEFILDR\r
281BadBootSector: \r
282DiskError: \r
283 leaw %cs:ErrorString, %si\r
284 call PrintString\r
285Halt: \r
286 jmp Halt\r
287\r
288StartString: \r
289 .byte 'B', 0x0c, 'S', 0x0c, 't', 0x0c, 'a', 0x0c, 'r', 0x0c, 't', 0x0c, '!', 0x0c\r
290ErrorString: \r
291 .byte 'B', 0x0c, 'E', 0x0c, 'r', 0x0c, 'r', 0x0c, 'o', 0x0c, 'r', 0x0c, '!', 0x0c\r
292\r
293# ****************************************************************************\r
294# LBA Offset for BootSector, need patched by tool for HD boot.\r
295# ****************************************************************************\r
296\r
297 .org 0x01fa\r
298LBAOffsetForBootSector: \r
299 .long 0x0\r
300\r
301# ****************************************************************************\r
302# Sector Signature\r
303# ****************************************************************************\r
304\r
305 .org 0x01fe\r
306SectorSignature: \r
307 .word 0xaa55 # Boot Sector Signature\r
308\r
309\r
310\r