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