1 #------------------------------------------------------------------------------
3 #* Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
4 #* 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 #------------------------------------------------------------------------------
20 .equ BLOCK_SIZE, 0x0200
21 .equ BLOCK_MASK, 0x01ff
24 # ****************************************************************************
25 # Code loaded by BIOS at 0x0000:0x7C00
26 # ****************************************************************************
33 # ****************************************************************************
35 # ****************************************************************************
41 leaw %cs:StartString, %si
47 # ****************************************************************************
49 # ****************************************************************************
51 # ****************************************************************************
52 # Initialize segment registers and copy code at 0x0000:0x7c00 to 0x0000:0x0600
53 # ****************************************************************************
54 xorw %ax, %ax # AX = 0x0000
55 movw $0x7c00, %bx # BX = 0x7C00
56 movw $0x600, %bp # BP = 0x0600
57 movw $RelocatedStart, %si # SI = Offset(RelocatedStart)
58 movw $0x200, %cx # CX = 0x0200
59 subw %si, %cx # CS = 0x0200 - Offset(RelocatedStart)
60 leaw (%bp,%si,), %di # DI = 0x0600 + Offset(RelocatedStart)
61 leaw (%bx,%si,), %si # BX = 0x7C00 + Offset(RelocatedStart)
62 movw %ax, %ss # SS = 0x0000
63 movw %bx, %sp # SP = 0x7C00
64 movw %ax, %es # ES = 0x0000
65 movw %ax, %ds # DS = 0x0000
66 pushw %ax # PUSH 0x0000
67 pushw %di # PUSH 0x0600 + Offset(RelocatedStart)
68 cld # Clear the direction flag
70 movsb # Copy 0x0200 bytes from 0x7C00 to 0x0600
71 retl # JMP 0x0000:0x0600 + Offset(RelocatedStart)
73 # ****************************************************************************
74 # Code relocated to 0x0000:0x0600
75 # ****************************************************************************
78 # ****************************************************************************
79 # Get Driver Parameters to 0x0000:0x7BFC
80 # ****************************************************************************
82 xorw %ax, %ax # AX = 0
83 movw %ax, %ss # SS = 0
87 movw $0x7c00, %sp # SP = 0x7c00
88 movw %sp, %bp # BP = 0x7c00
90 movb $8, %ah # AH = 8 - Get Drive Parameters Function
91 movb %dl, PhysicalDrive(%bp) # BBS defines that BIOS would pass the booting driver number to the loader through DL
92 int $0x13 # Get Drive Parameters
93 xorw %ax, %ax # AX = 0
94 movb %dh, %al # AL = DH
95 incb %al # MaxHead = AL + 1
96 pushw %ax # 0000:7bfe = MaxHead
97 movb %cl, %al # AL = CL
98 andb $0x3f, %al # MaxSector = AL & 0x3f
99 pushw %ax # 0000:7bfc = MaxSector
101 # ****************************************************************************
102 # Read Target DBR from hard disk to 0x0000:0x7C00
103 # ****************************************************************************
106 movb MbrPartitionIndicator(%bp), %al # AX = MbrPartitionIndex
107 cmpb $0xff, %al # 0xFF means do legacy MBR boot
110 movl $0x0000600, %eax # Assume LegacyMBR is backuped in Sector 6
111 jmp StartReadTo7C00 # EAX = Header/Sector/Tracker/Zero
114 cmpb $4, %al # MbrPartitionIndex should < 4
116 shlw $4, %ax # AX = MBREntrySize * Index
117 addw $0x1be, %ax # AX = MBREntryOffset
118 movw %ax, %di # DI = MBREntryOffset
120 # Here we don't use the C/H/S information provided by Partition table
121 # but calculate C/H/S from LBA ourselves
122 # Ci: Cylinder number
125 movl %es:8(%bp,%di,), %eax # Start LBA
127 shrl $16, %edx # DX:AX = Start LBA
128 # = Ci * (H * S) + Hi * S + (Si - 1)
130 # Calculate C/H/S according to LBA
132 divw 2(%bp) # AX = Hi + H*Ci
135 pushw %dx # 0000:7bfa = Si <----
136 xorw %dx, %dx # DX:AX = Hi + H*Ci
137 divw 4(%bp) # AX = Ci <----
143 movb %al, %ch # Ci[0-7]
144 orb %ah, %cl # Ci[8,9]
145 movw $0x7c00, %bx # ES:BX = 0000:7C00h
146 movb $0x2, %ah # Function 02h
147 movb $1, %al # 1 Sector
150 movb PhysicalDrive(%bp), %dl # Drive number
156 # ****************************************************************************
157 # Transfer control to BootSector - Jump to 0x0000:0x7C00
158 # ****************************************************************************
160 pushw %ax # PUSH 0x0000 - Segment
162 pushw %di # PUSH 0x7C00 - Offset
163 retl # JMP 0x0000:0x7C00
165 # ****************************************************************************
167 # ****************************************************************************
175 leaw %cs:ErrorString, %si
188 .byte 'M', 0x0c, 'B', 0x0c, 'R', 0x0c, ' ', 0x0c, 'S', 0x0c, 't', 0x0c, 'a', 0x0c, 'r', 0x0c, 't', 0x0c, '!', 0x0c
190 .byte 'M', 0x0c, 'B', 0x0c, 'R', 0x0c, ' ', 0x0c, 'E', 0x0c, 'r', 0x0c, 'r', 0x0c, ':', 0x0c, '?', 0x0c, '?', 0x0c
192 # ****************************************************************************
193 # A2C - convert Ascii code stored in AH to character stored in AX
194 # ****************************************************************************
214 # ****************************************************************************
215 # PhysicalDrive - Used to indicate which disk to be boot
216 # Can be patched by tool
217 # ****************************************************************************
219 PhysicalDrive: .byte 0x80
221 # ****************************************************************************
222 # MbrPartitionIndicator - Used to indicate which MBR partition to be boot
223 # Can be patched by tool
224 # OxFF means boot to legacy MBR. (LBA OFFSET 6)
225 # ****************************************************************************
227 MbrPartitionIndicator: .byte 0
229 # ****************************************************************************
230 # Unique MBR signature
231 # ****************************************************************************
235 # ****************************************************************************
237 # ****************************************************************************
241 # ****************************************************************************
242 # MBR Entry - To be patched
243 # ****************************************************************************
253 # ****************************************************************************
255 # ****************************************************************************
259 .word 0xaa55 # Boot Sector Signature