]> git.proxmox.com Git - mirror_edk2.git/blame - DuetPkg/BootSector/bootsect.S
Add some comments in BdsDxe INF file.
[mirror_edk2.git] / DuetPkg / BootSector / bootsect.S
CommitLineData
819958c6 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#* bootsect.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, 0x020\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, 0x04c494645 # "EFIL"\r
30.equ LOADER_FILENAME_PART2, 0x020205244 # "DR__"\r
31.equ LOADER_FILENAME_PART3, 0x020202020 # "____"\r
32\r
33 .org 0x0\r
34Ia32Jump: \r
35 jmp BootSectorEntryPoint # JMP inst - 3 bytes\r
36 nop\r
37\r
38OemId: .ascii "INTEL " # OemId - 8 bytes\r
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
52PhysicalDrive: .byte 0 # PhysicalDriveNumber - 8 bits - ignored\r
53CurrentHead: .byte 0 # Current Head - 8 bits\r
54Signature: .byte 0 # Signature - 8 bits - ignored\r
55VolId: .ascii " " # Volume Serial Number- 4 bytes\r
56FatLabel: .ascii " " # Label - 11 bytes\r
57SystemId: .ascii "FAT12 " # SystemId - 8 bytes\r
58\r
59BootSectorEntryPoint: \r
60 #ASSUME ds:@code\r
61 #ASSUME ss:@code\r
62\r
63# ****************************************************************************\r
64# Start Print\r
65# ****************************************************************************\r
66 leaw %cs:StartString, %si\r
67 call PrintString\r
68\r
69# ****************************************************************************\r
70# Print over\r
71# ****************************************************************************\r
72\r
73 movw %cs, %ax # ax = 0\r
74 movw %ax, %ss # ss = 0\r
75 addw $0x1000, %ax\r
76 movw %ax, %ds\r
77\r
78 movw $0x7c00, %sp # sp = 0x7c00\r
79 movw %sp, %bp # bp = 0x7c00\r
80\r
81 movb $8, %ah # ah = 8 - Get Drive Parameters Function\r
82 movb %dl, PhysicalDrive(%bp) # BBS defines that BIOS would pass the booting driver number to the loader through DL\r
83 int $0x13 # Get Drive Parameters\r
84 xorw %ax, %ax # ax = 0\r
85 movb %dh, %al # al = dh\r
86 incb %al # MaxHead = al + 1\r
87 pushw %ax # 0000:7bfe = MaxHead\r
88 movb %cl, %al # al = cl\r
89 andb $0x3f, %al # MaxSector = al & 0x3f\r
90 pushw %ax # 0000:7bfc = MaxSector\r
91\r
92 cmpw $0xaa55, SectorSignature(%bp) # Verify Boot Sector Signature\r
93 jne BadBootSector\r
94 movw RootEntries(%bp), %cx # cx = RootEntries\r
95 shlw $FAT_DIRECTORY_ENTRY_SHIFT, %cx # cx = cx * 32 = cx * sizeof(FAT_DIRECTORY_ENTRY) = Size of Root Directory in bytes\r
96 movw %cx, %bx # bx = size of the Root Directory in bytes\r
97 andw $BLOCK_MASK, %bx # See if it is an even number of sectors long\r
98 jne BadBootSector # If is isn't, then the boot sector is bad.\r
99 movw %cx, %bx # bx = size of the Root Directory in bytes\r
100 shrw $BLOCK_SHIFT, %bx # bx = size of Root Directory in sectors\r
101 movb NoFats(%bp), %al # al = NoFats\r
102 xorb %ah, %ah # ah = 0 ==> ax = NoFats\r
103 mulw SectorsPerFat(%bp) # ax = NoFats * SectorsPerFat\r
104 addw ReservedSectors(%bp), %ax # ax = NoFats * SectorsPerFat + ReservedSectors = RootLBA\r
105 pushw %ds\r
106 popw %es\r
107 xorw %di, %di # Store directory in es:di = 1000:0000\r
108 call ReadBlocks # Read entire Root Directory\r
109 addw %bx, %ax # ax = NoFats * SectorsPerFat + ReservedSectors + RootDirSectors = FirstClusterLBA (FirstDataSector)\r
110 movw %ax, (%bp) # Save FirstClusterLBA (FirstDataSector) for later use\r
111\r
112 # dx - variable storage (initial value is 0)\r
113 # bx - loader (initial value is 0)\r
114 xorw %dx, %dx\r
115 xorw %bx, %bx\r
116\r
117FindEFILDR: \r
118 cmpl $LOADER_FILENAME_PART1, (%di) # Compare to "EFIL"\r
119 jne FindVARSTORE\r
120 cmpl $LOADER_FILENAME_PART2, 4(%di) \r
121 jne FindVARSTORE\r
122 cmpl $LOADER_FILENAME_PART3, 7(%di) \r
123 jne FindVARSTORE\r
124 movw 26(%di), %bx # bx = Start Cluster for EFILDR <----------------------------------\r
125 testw %dx, %dx\r
126 je FindNext # Efivar.bin is not loaded\r
127 jmp FoundAll\r
128\r
129FindVARSTORE: \r
130 ## if the file is not loader file, see if it's "EFIVAR BIN"\r
131 cmpl $0x56494645, (%di) # Compare to "EFIV"\r
132 jne FindNext\r
133 cmpl $0x20205241, 4(%di) # Compare to "AR "\r
134 jne FindNext\r
135 cmpl $0x4e494220, 7(%di) # Compare to " BIN"\r
136 jne FindNext\r
137 movw %di, %dx # dx = Offset of Start Cluster for Efivar.bin <---------------------\r
138 addw $26, %dx\r
139 testw %bx, %bx\r
140 je FindNext # Efildr is not loaded\r
141 jmp FoundAll\r
142\r
143FindNext: \r
144 # go to next find\r
145 addw $FAT_DIRECTORY_ENTRY_SIZE, %di # Increment di\r
146 subw $FAT_DIRECTORY_ENTRY_SIZE, %cx # Decrement cx\r
147 # TODO: jump to FindVarStore if ...\r
148 jne FindEFILDR\r
149 jmp NotFoundAll\r
150\r
151FoundAll: \r
152FoundEFILDR: \r
153 movw %bx, %cx # cx = Start Cluster for EFILDR <----------------------------------\r
154 movw %cs, %ax # Destination = 2000:0000\r
155 addw $0x2000, %ax\r
156 movw %ax, %es\r
157 xorw %di, %di\r
158ReadFirstClusterOfEFILDR: \r
159 movw %cx, %ax # ax = StartCluster\r
160 subw $2, %ax # ax = StartCluster - 2\r
161 xorb %bh, %bh\r
162 movb SectorsPerCluster(%bp), %bl # bx = SectorsPerCluster\r
163 pushw %dx\r
164 mulw %bx\r
165 popw %dx # ax = (StartCluster - 2) * SectorsPerCluster\r
166 addw (%bp), %ax # ax = FirstClusterLBA + (StartCluster-2)*SectorsPerCluster\r
167 xorb %bh, %bh\r
168 movb SectorsPerCluster(%bp), %bl # bx = Number of Sectors in a cluster\r
169 pushw %es\r
170 call ReadBlocks\r
171 popw %ax\r
172JumpIntoFirstSectorOfEFILDR: \r
173 movw %ax, JumpSegment(%bp)\r
174JumpFarInstruction: \r
175 .byte 0xea\r
176JumpOffset: \r
177 .word 0x000\r
178JumpSegment: \r
179 .word 0x2000\r
180\r
181\r
182PrintString: \r
183 movw $0xb800, %ax\r
184 movw %ax, %es\r
185 movw $0x7c0, %ax\r
186 movw %ax, %ds\r
187 movw $7, %cx\r
188 movw $160, %di\r
189 rep\r
190 movsw\r
191 ret\r
192# ****************************************************************************\r
193# ReadBlocks - Reads a set of blocks from a block device\r
194#\r
195# AX = Start LBA\r
196# BX = Number of Blocks to Read\r
197# ES:DI = Buffer to store sectors read from disk\r
198# ****************************************************************************\r
199\r
200# cx = Blocks\r
201# bx = NumberOfBlocks\r
202# si = StartLBA\r
203\r
204ReadBlocks: \r
205 pusha\r
206 addl LBAOffsetForBootSector(%bp), %eax # Add LBAOffsetForBootSector to Start LBA\r
207 addl HiddenSectors(%bp), %eax # Add HiddenSectors to Start LBA\r
208 movl %eax, %esi # esi = Start LBA\r
209 movw %bx, %cx # cx = Number of blocks to read\r
210ReadCylinderLoop: \r
211 movw $0x7bfc, %bp # bp = 0x7bfc\r
212 movl %esi, %eax # eax = Start LBA\r
213 xorl %edx, %edx # edx = 0\r
214 movzwl (%bp), %ebx # bx = MaxSector\r
215 divl %ebx # ax = StartLBA / MaxSector\r
216 incw %dx # dx = (StartLBA % MaxSector) + 1\r
217 subw %dx, %bx # bx = MaxSector - Sector\r
218 incw %bx # bx = MaxSector - Sector + 1\r
219 cmpw %bx, %cx # Compare (Blocks) to (MaxSector - Sector + 1)\r
220 jg LimitTransfer\r
221 movw %cx, %bx # bx = Blocks\r
222LimitTransfer: \r
223 pushw %cx\r
224 movb %dl, %cl # cl = (StartLBA % MaxSector) + 1 = Sector\r
225 xorw %dx, %dx # dx = 0\r
226 divw 2(%bp) # ax = ax / (MaxHead + 1) = Cylinder \r
227 # dx = ax % (MaxHead + 1) = Head\r
228\r
229 pushw %bx # Save number of blocks to transfer\r
230 movb %dl, %dh # dh = Head\r
231 movw $0x7c00, %bp # bp = 0x7c00\r
232 movb PhysicalDrive(%bp), %dl # dl = Drive Number\r
233 movb %al, %ch # ch = Cylinder\r
234 movb %bl, %al # al = Blocks\r
235 movb $2, %ah # ah = Function 2\r
236 movw %di, %bx # es:bx = Buffer address\r
237 int $0x13\r
238 jc DiskError\r
239 popw %bx\r
240 popw %cx\r
241 movzwl %bx, %ebx\r
242 addl %ebx, %esi # StartLBA = StartLBA + NumberOfBlocks\r
243 subw %bx, %cx # Blocks = Blocks - NumberOfBlocks\r
244 movw %es, %ax\r
245 shlw $(BLOCK_SHIFT-4), %bx\r
246 addw %bx, %ax\r
247 movw %ax, %es # es:di = es:di + NumberOfBlocks*BLOCK_SIZE\r
248 cmpw $0, %cx\r
249 jne ReadCylinderLoop\r
250 popa\r
251 ret\r
252\r
253# ****************************************************************************\r
254# ERROR Condition:\r
255# ****************************************************************************\r
256NotFoundAll: \r
257 ## if we found EFILDR, continue\r
258 testw %bx, %bx\r
259 jne FoundEFILDR\r
260BadBootSector: \r
261DiskError: \r
262 leaw %cs:ErrorString, %si\r
263 call PrintString\r
264Halt: \r
265 jmp Halt\r
266\r
267StartString: \r
268 .byte 'B', 0x0c, 'S', 0x0c, 't', 0x0c, 'a', 0x0c, 'r', 0x0c, 't', 0x0c, '!', 0x0c\r
269ErrorString: \r
270 .byte 'B', 0x0c, 'E', 0x0c, 'r', 0x0c, 'r', 0x0c, 'o', 0x0c, 'r', 0x0c, '!', 0x0c\r
271\r
272# ****************************************************************************\r
273# LBA Offset for BootSector, need patched by tool for HD boot.\r
274# ****************************************************************************\r
275\r
276 # .org 0x01fa # Comment it for pass build. Should optimise code size. \r
277LBAOffsetForBootSector: \r
278 .long 0x0\r
279\r
280# ****************************************************************************\r
281# Sector Signature\r
282# ****************************************************************************\r
283\r
284 # .org 0x01fe # Comment it for pass build.\r
285SectorSignature: \r
286 .word 0xaa55 # Boot Sector Signature\r
287\r
288\r
289\r