Fix warnings generated by GCC.
[edk2.git] / DuetPkg / BootSector / Mbr.S
bloba2431e19319bf0a831bfad3aa8db0bf977a9906c
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
18 .equ                      BLOCK_SIZE, 0x0200\r
19 .equ                      BLOCK_MASK, 0x01ff\r
20 .equ                      BLOCK_SHIFT, 9\r
22 # ****************************************************************************\r
23 # Code loaded by BIOS at 0x0000:0x7C00\r
24 # ****************************************************************************\r
26 .org 0x0\r
28 .global _start\r
29 _start:\r
31 # ****************************************************************************\r
32 # Start Print\r
33 # ****************************************************************************\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
45 # ****************************************************************************\r
46 # Print over\r
47 # ****************************************************************************\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
71 # ****************************************************************************\r
72 # Code relocated to 0x0000:0x0600\r
73 # ****************************************************************************\r
75 RelocatedStart: \r
76 # ****************************************************************************\r
77 # Get Driver Parameters to 0x0000:0x7BFC\r
78 # ****************************************************************************\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
85         movw  $0x7c00, %sp                        # SP = 0x7c00\r
86         movw  %sp, %bp                            # BP = 0x7c00\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
99 # ****************************************************************************\r
100 # Read Target DBR from hard disk to 0x0000:0x7C00\r
101 # ****************************************************************************\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
107 LegacyMbr: \r
108         movl  $0x0000600, %eax                    # Assume LegacyMBR is backuped in Sector 6\r
109         jmp   StartReadTo7C00                     # EAX = Header/Sector/Tracker/Zero\r
111 EfiDbr: \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
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
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
138 StartReadTo7C00: \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
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
163 # ****************************************************************************\r
164 # ERROR Condition:\r
165 # ****************************************************************************\r
167 BadDbr: \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
182 Halt: \r
183     jmp   Halt\r
185 StartString: \r
186 .byte 'M', 0x0c, 'B', 0x0c, 'R', 0x0c, ' ', 0x0c, 'S', 0x0c, 't', 0x0c, 'a', 0x0c, 'r', 0x0c, 't', 0x0c, '!', 0x0c\r
187 ErrorString: \r
188 .byte 'M', 0x0c, 'B', 0x0c, 'R', 0x0c, ' ', 0x0c, 'E', 0x0c, 'r', 0x0c, 'r', 0x0c, ':', 0x0c, '?', 0x0c, '?', 0x0c\r
190 # ****************************************************************************\r
191 # A2C - convert Ascii code stored in AH to character stored in AX\r
192 # ****************************************************************************\r
193 A2C: \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
200     cmpb '9', %ah\r
201     jle  A2C_L1\r
202     addb $7, %ah\r
203 A2C_L1: \r
205     cmpb '9', %al\r
206     jle A2C_L2\r
207     addb $7, %al\r
208 A2C_L2: \r
209     ret\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
217 PhysicalDrive:        .byte 0x80\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
225 MbrPartitionIndicator: .byte 0\r
227 # ****************************************************************************\r
228 # Unique MBR signature\r
229 # ****************************************************************************\r
230 .org   0x01B8\r
231     .ascii "DUET"\r
233 # ****************************************************************************\r
234 # Unknown\r
235 # ****************************************************************************\r
236 .org   0x01BC\r
237     .word 0\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
251 # ****************************************************************************\r
252 # Sector Signature\r
253 # ****************************************************************************\r
255 .org 0x01FE\r
256 SectorSignature: \r
257   .word     0xaa55      # Boot Sector Signature\r