]> git.proxmox.com Git - mirror_edk2.git/blame - DuetPkg/BootSector/Mbr.S
Use .p2align directive instead of ambiguous .align directive.
[mirror_edk2.git] / DuetPkg / BootSector / Mbr.S
CommitLineData
f3eb2131 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#* Mbr.asm\r
13#*\r
14#* Abstract:\r
15#*\r
16#------------------------------------------------------------------------------\r
17\r
18.equ BLOCK_SIZE, 0x0200\r
19.equ BLOCK_MASK, 0x01ff\r
20.equ BLOCK_SHIFT, 9\r
21\r
22# ****************************************************************************\r
23# Code loaded by BIOS at 0x0000:0x7C00\r
24# ****************************************************************************\r
25\r
26.org 0x0\r
27\r
ae3d1e91 28.global _start\r
f3eb2131 29_start:\r
30\r
31# ****************************************************************************\r
32# Start Print\r
33# ****************************************************************************\r
34\r
35 movw $0xb800, %ax\r
36 movw %ax, %es\r
37 movw $0x7c0, %ax\r
38 movw %ax, %ds\r
39 leaw %cs:StartString, %si\r
40 movw $10, %cx\r
41 movw $160, %di\r
42 rep \r
43 movsw\r
44\r
45# ****************************************************************************\r
46# Print over\r
47# ****************************************************************************\r
48\r
49# ****************************************************************************\r
50# Initialize segment registers and copy code at 0x0000:0x7c00 to 0x0000:0x0600\r
51# ****************************************************************************\r
52 xorw %ax, %ax # AX = 0x0000\r
53 movw $0x7c00, %bx # BX = 0x7C00\r
54 movw $0x600, %bp # BP = 0x0600\r
55 movw RelocatedStart, %si # SI = Offset(RelocatedStart)\r
56 movw $0x200, %cx # CX = 0x0200\r
57 subw %si, %cx # CS = 0x0200 - Offset(RelocatedStart)\r
58 leaw (%bp,%si,), %di # DI = 0x0600 + Offset(RelocatedStart)\r
59 leaw (%bx,%si,), %si # BX = 0x7C00 + Offset(RelocatedStart)\r
60 movw %ax, %ss # SS = 0x0000\r
61 movw %bx, %sp # SP = 0x7C00\r
62 movw %ax, %es # ES = 0x0000\r
63 movw %ax, %ds # DS = 0x0000\r
64 pushw %ax # PUSH 0x0000\r
65 pushw %di # PUSH 0x0600 + Offset(RelocatedStart)\r
66 cld # Clear the direction flag\r
67 rep\r
68 movsb # Copy 0x0200 bytes from 0x7C00 to 0x0600\r
69 retl # JMP 0x0000:0x0600 + Offset(RelocatedStart)\r
70\r
71# ****************************************************************************\r
72# Code relocated to 0x0000:0x0600\r
73# ****************************************************************************\r
74\r
75RelocatedStart: \r
76# ****************************************************************************\r
77# Get Driver Parameters to 0x0000:0x7BFC\r
78# ****************************************************************************\r
79\r
80 xorw %ax, %ax # AX = 0\r
81 movw %ax, %ss # SS = 0\r
82 addw $0x1000, %ax\r
83 movw %ax, %ds\r
84\r
85 movw $0x7c00, %sp # SP = 0x7c00\r
86 movw %sp, %bp # BP = 0x7c00\r
87\r
88 movb $8, %ah # AH = 8 - Get Drive Parameters Function\r
89 movb %dl, PhysicalDrive(%bp) # BBS defines that BIOS would pass the booting driver number to the loader through DL\r
90 int $0x13 # Get Drive Parameters\r
91 xorw %ax, %ax # AX = 0\r
92 movb %dh, %al # AL = DH\r
93 incb %al # MaxHead = AL + 1\r
94 pushw %ax # 0000:7bfe = MaxHead\r
95 movb %cl, %al # AL = CL\r
96 andb $0x3f, %al # MaxSector = AL & 0x3f\r
97 pushw %ax # 0000:7bfc = MaxSector\r
98\r
99# ****************************************************************************\r
100# Read Target DBR from hard disk to 0x0000:0x7C00\r
101# ****************************************************************************\r
102\r
103 xorw %ax, %ax\r
104 movb MbrPartitionIndicator(%bp), %al # AX = MbrPartitionIndex\r
105 cmpb $0xff, %al # 0xFF means do legacy MBR boot\r
106 jnz EfiDbr\r
107LegacyMbr: \r
108 movl $0x0000600, %eax # Assume LegacyMBR is backuped in Sector 6\r
109 jmp StartReadTo7C00 # EAX = Header/Sector/Tracker/Zero\r
110\r
111EfiDbr: \r
112 cmpb $4, %al # MbrPartitionIndex should < 4\r
113 jae BadDbr\r
114 shlw $4, %ax # AX = MBREntrySize * Index\r
115 addw $0x1be, %ax # AX = MBREntryOffset\r
116 movw %ax, %di # DI = MBREntryOffset\r
117\r
118 # Here we don't use the C/H/S information provided by Partition table\r
119 # but calculate C/H/S from LBA ourselves\r
120 # Ci: Cylinder number\r
121 # Hi: Header number\r
122 # Si: Sector number\r
123 movl %es:8(%bp,%di,), %eax # Start LBA\r
124 movl %eax, %edx\r
125 shrl $16, %edx # DX:AX = Start LBA\r
126 # = Ci * (H * S) + Hi * S + (Si - 1)\r
127\r
128 # Calculate C/H/S according to LBA\r
129 movw $0x7bfa, %bp\r
130 divw 2(%bp) # AX = Hi + H*Ci\r
131 # DX = Si - 1\r
132 incw %dx # DX = Si\r
133 pushw %dx # 0000:7bfa = Si <----\r
134 xorw %dx, %dx # DX:AX = Hi + H*Ci\r
135 divw 4(%bp) # AX = Ci <----\r
136 # DX = Hi <----\r
137\r
138StartReadTo7C00: \r
139\r
140 movb (%bp), %cl # Si\r
141 movb %al, %ch # Ci[0-7]\r
142 orb %ah, %cl # Ci[8,9]\r
143 movw $0x7c00, %bx # ES:BX = 0000:7C00h\r
144 movb $0x2, %ah # Function 02h\r
145 movb $1, %al # 1 Sector\r
146 movb %dl, %dh # Hi\r
147 movw $0x600, %bp\r
148 movb PhysicalDrive(%bp), %dl # Drive number\r
149 int $0x13\r
150 jc BadDbr\r
151\r
152\r
153\r
154# ****************************************************************************\r
155# Transfer control to BootSector - Jump to 0x0000:0x7C00\r
156# ****************************************************************************\r
157 xorw %ax, %ax\r
158 pushw %ax # PUSH 0x0000 - Segment\r
159 movw $0x7c00, %di\r
160 pushw %di # PUSH 0x7C00 - Offset\r
161 retl # JMP 0x0000:0x7C00\r
162\r
163# ****************************************************************************\r
164# ERROR Condition:\r
165# ****************************************************************************\r
166\r
167BadDbr: \r
168 pushw %ax\r
169 movw $0xb800, %ax\r
170 movw %ax, %es\r
171 movw $0x60, %ax\r
172 movw %ax, %ds\r
173 leaw %cs:ErrorString, %si\r
174 movw $320, %di\r
175 popw %ax\r
176 call A2C\r
177 movb %ah, 16(%si)\r
178 movb %al, 18(%si)\r
179 movw $10, %cx\r
180 rep\r
181 movsw\r
182Halt: \r
183 jmp Halt\r
184\r
185StartString: \r
186.byte 'M', 0x0c, 'B', 0x0c, 'R', 0x0c, ' ', 0x0c, 'S', 0x0c, 't', 0x0c, 'a', 0x0c, 'r', 0x0c, 't', 0x0c, '!', 0x0c\r
187ErrorString: \r
188.byte 'M', 0x0c, 'B', 0x0c, 'R', 0x0c, ' ', 0x0c, 'E', 0x0c, 'r', 0x0c, 'r', 0x0c, ':', 0x0c, '?', 0x0c, '?', 0x0c\r
189\r
190# ****************************************************************************\r
191# A2C - convert Ascii code stored in AH to character stored in AX\r
192# ****************************************************************************\r
193A2C: \r
194 movb %ah, %al\r
195 shrb $4, %ah\r
196 andb $0xF, %al\r
197 addb '0', %ah\r
198 addb '0', %al\r
199\r
200 cmpb '9', %ah\r
201 jle A2C_L1\r
202 addb $7, %ah\r
203A2C_L1: \r
204\r
205 cmpb '9', %al\r
206 jle A2C_L2\r
207 addb $7, %al\r
208A2C_L2: \r
209 ret\r
210\r
211\r
212# ****************************************************************************\r
213# PhysicalDrive - Used to indicate which disk to be boot\r
214# Can be patched by tool\r
215# ****************************************************************************\r
216.org 0x01B6\r
217PhysicalDrive: .byte 0x80\r
218\r
219# ****************************************************************************\r
220# MbrPartitionIndicator - Used to indicate which MBR partition to be boot\r
221# Can be patched by tool\r
222# OxFF means boot to legacy MBR. (LBA OFFSET 6)\r
223# ****************************************************************************\r
224.org 0x01B7\r
225MbrPartitionIndicator: .byte 0\r
226\r
227# ****************************************************************************\r
228# Unique MBR signature\r
229# ****************************************************************************\r
230.org 0x01B8\r
231 .ascii "DUET"\r
232\r
233# ****************************************************************************\r
234# Unknown\r
235# ****************************************************************************\r
236.org 0x01BC\r
237 .word 0\r
238\r
239# ****************************************************************************\r
240# MBR Entry - To be patched\r
241# ****************************************************************************\r
242.org 0x01BE\r
243 .long 0,0,0,0\r
244.org 0x01CE\r
245 .long 0,0,0,0\r
246.org 0x01DE\r
247 .long 0,0,0,0\r
248.org 0x01EE\r
249 .long 0,0,0,0\r
250\r
251# ****************************************************************************\r
252# Sector Signature\r
253# ****************************************************************************\r
254\r
255.org 0x01FE\r
256SectorSignature: \r
257 .word 0xaa55 # Boot Sector Signature\r
258\r
259\r
260\r