1 #------------------------------------------------------------------------------
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
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.
16 #------------------------------------------------------------------------------
18 .equ BLOCK_SIZE, 0x0200
19 .equ BLOCK_MASK, 0x01ff
22 # ****************************************************************************
23 # Code loaded by BIOS at 0x0000:0x7C00
24 # ****************************************************************************
29 # ****************************************************************************
31 # ****************************************************************************
37 leaw %cs:StartString, %si
43 # ****************************************************************************
45 # ****************************************************************************
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
66 movsb # Copy 0x0200 bytes from 0x7C00 to 0x0600
67 retl # JMP 0x0000:0x0600 + Offset(RelocatedStart)
69 # ****************************************************************************
70 # Code relocated to 0x0000:0x0600
71 # ****************************************************************************
74 # ****************************************************************************
75 # Get Driver Parameters to 0x0000:0x7BFC
76 # ****************************************************************************
78 xorw %ax, %ax # AX = 0
79 movw %ax, %ss # SS = 0
83 movw $0x7c00, %sp # SP = 0x7c00
84 movw %sp, %bp # BP = 0x7c00
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
97 # ****************************************************************************
98 # Read Target DBR from hard disk to 0x0000:0x7C00
99 # ****************************************************************************
102 movb MbrPartitionIndicator(%bp), %al # AX = MbrPartitionIndex
103 cmpb $0xff, %al # 0xFF means do legacy MBR boot
106 movl $0x0000600, %eax # Assume LegacyMBR is backuped in Sector 6
107 jmp StartReadTo7C00 # EAX = Header/Sector/Tracker/Zero
110 cmpb $4, %al # MbrPartitionIndex should < 4
112 shlw $4, %ax # AX = MBREntrySize * Index
113 addw $0x1be, %ax # AX = MBREntryOffset
114 movw %ax, %di # DI = MBREntryOffset
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
121 movl %es:8(%bp,%di,), %eax # Start LBA
123 shrl $16, %edx # DX:AX = Start LBA
124 # = Ci * (H * S) + Hi * S + (Si - 1)
126 # Calculate C/H/S according to LBA
128 divw 2(%bp) # AX = Hi + H*Ci
131 pushw %dx # 0000:7bfa = Si <----
132 xorw %dx, %dx # DX:AX = Hi + H*Ci
133 divw 4(%bp) # AX = Ci <----
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
146 movb PhysicalDrive(%bp), %dl # Drive number
152 # ****************************************************************************
153 # Transfer control to BootSector - Jump to 0x0000:0x7C00
154 # ****************************************************************************
156 pushw %ax # PUSH 0x0000 - Segment
158 pushw %di # PUSH 0x7C00 - Offset
159 retl # JMP 0x0000:0x7C00
161 # ****************************************************************************
163 # ****************************************************************************
171 leaw %cs:ErrorString, %si
184 .byte 'M', 0x0c, 'B', 0x0c, 'R', 0x0c, ' ', 0x0c, 'S', 0x0c, 't', 0x0c, 'a', 0x0c, 'r', 0x0c, 't', 0x0c, '!', 0x0c
186 .byte 'M', 0x0c, 'B', 0x0c, 'R', 0x0c, ' ', 0x0c, 'E', 0x0c, 'r', 0x0c, 'r', 0x0c, ':', 0x0c, '?', 0x0c, '?', 0x0c
188 # ****************************************************************************
189 # A2C - convert Ascii code stored in AH to character stored in AX
190 # ****************************************************************************
210 # ****************************************************************************
211 # PhysicalDrive - Used to indicate which disk to be boot
212 # Can be patched by tool
213 # ****************************************************************************
215 PhysicalDrive: .byte 0x80
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 # ****************************************************************************
223 MbrPartitionIndicator: .byte 0
225 # ****************************************************************************
226 # Unique MBR signature
227 # ****************************************************************************
231 # ****************************************************************************
233 # ****************************************************************************
237 # ****************************************************************************
238 # MBR Entry - To be patched
239 # ****************************************************************************
249 # ****************************************************************************
251 # ****************************************************************************
255 .word 0xaa55 # Boot Sector Signature