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 # ****************************************************************************
31 # ****************************************************************************
33 # ****************************************************************************
39 leaw %cs:StartString, %si
45 # ****************************************************************************
47 # ****************************************************************************
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
68 movsb # Copy 0x0200 bytes from 0x7C00 to 0x0600
69 retl # JMP 0x0000:0x0600 + Offset(RelocatedStart)
71 # ****************************************************************************
72 # Code relocated to 0x0000:0x0600
73 # ****************************************************************************
76 # ****************************************************************************
77 # Get Driver Parameters to 0x0000:0x7BFC
78 # ****************************************************************************
80 xorw %ax, %ax # AX = 0
81 movw %ax, %ss # SS = 0
85 movw $0x7c00, %sp # SP = 0x7c00
86 movw %sp, %bp # BP = 0x7c00
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
99 # ****************************************************************************
100 # Read Target DBR from hard disk to 0x0000:0x7C00
101 # ****************************************************************************
104 movb MbrPartitionIndicator(%bp), %al # AX = MbrPartitionIndex
105 cmpb $0xff, %al # 0xFF means do legacy MBR boot
108 movl $0x0000600, %eax # Assume LegacyMBR is backuped in Sector 6
109 jmp StartReadTo7C00 # EAX = Header/Sector/Tracker/Zero
112 cmpb $4, %al # MbrPartitionIndex should < 4
114 shlw $4, %ax # AX = MBREntrySize * Index
115 addw $0x1be, %ax # AX = MBREntryOffset
116 movw %ax, %di # DI = MBREntryOffset
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
123 movl %es:8(%bp,%di,), %eax # Start LBA
125 shrl $16, %edx # DX:AX = Start LBA
126 # = Ci * (H * S) + Hi * S + (Si - 1)
128 # Calculate C/H/S according to LBA
130 divw 2(%bp) # AX = Hi + H*Ci
133 pushw %dx # 0000:7bfa = Si <----
134 xorw %dx, %dx # DX:AX = Hi + H*Ci
135 divw 4(%bp) # AX = Ci <----
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
148 movb PhysicalDrive(%bp), %dl # Drive number
154 # ****************************************************************************
155 # Transfer control to BootSector - Jump to 0x0000:0x7C00
156 # ****************************************************************************
158 pushw %ax # PUSH 0x0000 - Segment
160 pushw %di # PUSH 0x7C00 - Offset
161 retl # JMP 0x0000:0x7C00
163 # ****************************************************************************
165 # ****************************************************************************
173 leaw %cs:ErrorString, %si
186 .byte 'M', 0x0c, 'B', 0x0c, 'R', 0x0c, ' ', 0x0c, 'S', 0x0c, 't', 0x0c, 'a', 0x0c, 'r', 0x0c, 't', 0x0c, '!', 0x0c
188 .byte 'M', 0x0c, 'B', 0x0c, 'R', 0x0c, ' ', 0x0c, 'E', 0x0c, 'r', 0x0c, 'r', 0x0c, ':', 0x0c, '?', 0x0c, '?', 0x0c
190 # ****************************************************************************
191 # A2C - convert Ascii code stored in AH to character stored in AX
192 # ****************************************************************************
212 # ****************************************************************************
213 # PhysicalDrive - Used to indicate which disk to be boot
214 # Can be patched by tool
215 # ****************************************************************************
217 PhysicalDrive: .byte 0x80
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 # ****************************************************************************
225 MbrPartitionIndicator: .byte 0
227 # ****************************************************************************
228 # Unique MBR signature
229 # ****************************************************************************
233 # ****************************************************************************
235 # ****************************************************************************
239 # ****************************************************************************
240 # MBR Entry - To be patched
241 # ****************************************************************************
251 # ****************************************************************************
253 # ****************************************************************************
257 .word 0xaa55 # Boot Sector Signature