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