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