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