+#------------------------------------------------------------------------------\r
+#*\r
+#* Copyright 2006 - 2007, Intel Corporation \r
+#* All rights reserved. This program and the accompanying materials \r
+#* are licensed and made available under the terms and conditions of the BSD License \r
+#* which accompanies this distribution. The full text of the license may be found at \r
+#* http://opensource.org/licenses/bsd-license.php \r
+#* \r
+#* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
+#* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+#* \r
+#* gpt.asm\r
+#* \r
+#* Abstract:\r
+#*\r
+#------------------------------------------------------------------------------\r
+\r
+ #.MODEL small\r
+# .dosseg\r
+ .stack: \r
+ .486p: \r
+ .code: \r
+\r
+.equ BLOCK_SIZE, 0x0200\r
+.equ BLOCK_MASK, 0x01ff\r
+.equ BLOCK_SHIFT, 9\r
+\r
+# ****************************************************************************\r
+# Code loaded by BIOS at 0x0000:0x7C00\r
+# ****************************************************************************\r
+\r
+ .org 0x00\r
+Start: \r
+\r
+# ****************************************************************************\r
+# Start Print\r
+# ****************************************************************************\r
+\r
+ movw $0xb800, %ax\r
+ movw %ax, %es\r
+ movw $0x7c0, %ax\r
+ movw %ax, %ds\r
+ leaw %cs:StartString, %si\r
+ movw $10, %cx\r
+ movw $160, %di\r
+ rep\r
+ movsw\r
+\r
+# ****************************************************************************\r
+# Print over\r
+# ****************************************************************************\r
+\r
+# ****************************************************************************\r
+# Initialize segment registers and copy code at 0x0000:0x7c00 to 0x0000:0x0600\r
+# ****************************************************************************\r
+ xorw %ax, %ax # AX = 0x0000 \r
+ movw $0x7c00, %bx # BX = 0x7C00\r
+ movw $0x600, %bp # BP = 0x0600\r
+ movw RelocatedStart, %si # SI = Offset(RelocatedStart)\r
+ movw $0x200, %cx # CX = 0x0200\r
+ subw %si, %cx # CS = 0x0200 - Offset(RelocatedStart)\r
+ leaw (%bp,%si,), %di # DI = 0x0600 + Offset(RelocatedStart)\r
+ leaw (%bx,%si,), %si # BX = 0x7C00 + Offset(RelocatedStart)\r
+ movw %ax, %ss # SS = 0x0000\r
+ movw %bx, %sp # SP = 0x7C00\r
+ movw %ax, %es # ES = 0x0000\r
+ movw %ax, %ds # DS = 0x0000\r
+ pushw %ax # PUSH 0x0000\r
+ pushw %di # PUSH 0x0600 + Offset(RelocatedStart)\r
+ cld # Clear the direction flag\r
+ rep\r
+ movsb # Copy 0x0200 bytes from 0x7C00 to 0x0600\r
+ retl # JMP 0x0000:0x0600 + Offset(RelocatedStart)\r
+\r
+# ****************************************************************************\r
+# Code relocated to 0x0000:0x0600\r
+# ****************************************************************************\r
+\r
+RelocatedStart: \r
+# ****************************************************************************\r
+# Get Driver Parameters to 0x0000:0x7BFC\r
+# ****************************************************************************\r
+ xorw %ax, %ax # ax = 0\r
+ movw %ax, %ss # ss = 0\r
+ addw $0x1000, %ax\r
+ movw %ax, %ds\r
+\r
+ movw $0x7c00, %sp # sp = 0x7c00\r
+ movw %sp, %bp # bp = 0x7c00\r
+\r
+ movb $8, %ah # ah = 8 - Get Drive Parameters Function\r
+ movb %dl, PhysicalDrive(%bp) # BBS defines that BIOS would pass the booting driver number to the loader through DL\r
+ int $0x13 # Get Drive Parameters\r
+ xorw %ax, %ax # ax = 0\r
+ movb %dh, %al # al = dh\r
+ incb %al # MaxHead = al + 1\r
+ pushw %ax # 0000:7bfe = MaxHead\r
+ movb %cl, %al # al = cl\r
+ andb $0x3f, %al # MaxSector = al & 0x3f\r
+ pushw %ax # 0000:7bfc = MaxSector\r
+\r
+# ****************************************************************************\r
+# Read GPT Header from hard disk to 0x0000:0x0800\r
+# ****************************************************************************\r
+ xorw %ax, %ax\r
+ movw %ax, %es # Read to 0x0000:0x0800\r
+ movw $0x800, %di # Read to 0x0000:0x0800\r
+ movl $1, %eax # Read LBA #1\r
+ movl $0, %edx # Read LBA #1\r
+ movw $1, %bx # Read 1 Block\r
+ pushw %es\r
+ call ReadBlocks\r
+ popw %es\r
+\r
+# ****************************************************************************\r
+# Read Target GPT Entry from hard disk to 0x0000:0x0A00\r
+# ****************************************************************************\r
+ cmpl $0x20494645, %es:(%di) # Check for "EFI "\r
+ jne BadGpt\r
+ cmpl $0x54524150, %es:4(%di) # Check for "PART"\r
+ jne BadGpt\r
+ cmpl $0x00010000, %es:8(%di) # Check Revision - 0x10000\r
+ jne BadGpt\r
+\r
+ movl %es:84(%di), %eax # EAX = SizeOfPartitionEntry\r
+ mulb GptPartitionIndicator(%bp) # EAX = SizeOfPartitionEntry * GptPartitionIndicator\r
+ movl %eax, %edx # EDX = SizeOfPartitionEntry * GptPartitionIndicator\r
+ shrl $BLOCK_SHIFT, %eax # EAX = (SizeOfPartitionEntry * GptPartitionIndicator) / BLOCK_SIZE\r
+ andl $BLOCK_MASK, %edx # EDX = Targer PartitionEntryLBA Offset\r
+ # = (SizeOfPartitionEntry * GptPartitionIndicator) % BLOCK_SIZE\r
+ pushl %edx\r
+ movl %es:72(%di), %ecx # ECX = PartitionEntryLBA (Low)\r
+ movl %es:76(%di), %ebx # EBX = PartitionEntryLBA (High)\r
+ addl %ecx, %eax # EAX = Target PartitionEntryLBA (Low)\r
+ # = (PartitionEntryLBA + \r
+ # (SizeOfPartitionEntry * GptPartitionIndicator) / BLOCK_SIZE)\r
+ adcl %ebx, %edx # EDX = Target PartitionEntryLBA (High)\r
+\r
+ movw $0xA00, %di # Read to 0x0000:0x0A00\r
+ movw $1, %bx # Read 1 Block\r
+ pushw %es\r
+ call ReadBlocks\r
+ popw %es\r
+\r
+# ****************************************************************************\r
+# Read Target DBR from hard disk to 0x0000:0x7C00\r
+# ****************************************************************************\r
+ popl %edx # EDX = (SizeOfPartitionEntry * GptPartitionIndicator) % BLOCK_SIZE\r
+ addw %dx, %di # DI = Targer PartitionEntryLBA Offset\r
+ cmpl $0xC12A7328, %es:(%di) # Check for EFI System Partition "C12A7328-F81F-11d2-BA4B-00A0C93EC93B"\r
+ jne BadGpt\r
+ cmpl $0x11d2F81F, %es:4(%di) # \r
+ jne BadGpt\r
+ cmpl $0xA0004BBA, %es:8(%di) # \r
+ jne BadGpt\r
+ cmpl $0x3BC93EC9, %es:0xc(%di) # \r
+ jne BadGpt\r
+\r
+ movl %es:32(%di), %eax # EAX = StartingLBA (Low)\r
+ movl %es:36(%di), %edx # EDX = StartingLBA (High)\r
+ movw $0x7C00, %di # Read to 0x0000:0x7C00\r
+ movw $1, %bx # Read 1 Block\r
+ call ReadBlocks\r
+\r
+# ****************************************************************************\r
+# Transfer control to BootSector - Jump to 0x0000:0x7C00\r
+# ****************************************************************************\r
+ xorw %ax, %ax\r
+ pushw %ax # PUSH 0x0000\r
+ movw $0x7c00, %di\r
+ pushw %di # PUSH 0x7C00\r
+ retl # JMP 0x0000:0x7C00\r
+\r
+# ****************************************************************************\r
+# ReadBlocks - Reads a set of blocks from a block device\r
+#\r
+# EDX:EAX = Start LBA\r
+# BX = Number of Blocks to Read (must < 127)\r
+# ES:DI = Buffer to store sectors read from disk\r
+# ****************************************************************************\r
+\r
+# si = DiskAddressPacket\r
+\r
+ReadBlocks: \r
+ pushal\r
+ pushw %ds\r
+ xorw %cx, %cx\r
+ movw %cx, %ds\r
+ movw $0x600, %bp # bp = 0x600\r
+ leaw AddressPacket(%bp), %si\r
+ movb %bl, %ds:2(%si) # 02 = Number Of Block transfered\r
+ movw %di, %ds:4(%si) # 04 = Transfer Buffer Offset\r
+ movw %es, %ds:6(%si) # 06 = Transfer Buffer Segment\r
+ movl %eax, %ds:8(%si) # 08 = Starting LBA (Low)\r
+ movl %edx, %ds:0xc(%si) # 0C = Starting LBA (High)\r
+ movb $0x42, %ah # ah = Function 42\r
+ movb PhysicalDrive(%bp), %dl # dl = Drive Number\r
+ int $0x13\r
+ jc BadGpt\r
+ popw %ds\r
+ popal\r
+ ret\r
+\r
+# ****************************************************************************\r
+# Address Packet used by ReadBlocks\r
+# ****************************************************************************\r
+AddressPacket: \r
+ .byte 0x10 # Size of address packet\r
+ .byte 0x0 # Reserved. Must be 0\r
+ .byte 0x1 # Read blocks at a time (To be fixed each times)\r
+ .byte 0x0 # Reserved. Must be 0\r
+ .word 0x000 # Destination Address offset (To be fixed each times)\r
+ .word 0x000 # Destination Address segment (To be fixed each times)\r
+AddressPacketLba: \r
+ .long 0x0,0x0 # Start LBA (To be fixed each times)\r
+AddressPacketEnd: \r
+\r
+# ****************************************************************************\r
+# ERROR Condition:\r
+# ****************************************************************************\r
+\r
+BadGpt: \r
+ movw $0xb800, %ax\r
+ movw %ax, %es\r
+ movw $0x60, %ax\r
+ movw %ax, %ds\r
+ leaw %cs:ErrorString, %si\r
+ movw $10, %cx\r
+ movw $320, %di\r
+ rep\r
+ movsw\r
+Halt: \r
+ jmp Halt\r
+\r
+StartString: \r
+ .byte 'G', 0x0c, 'P', 0x0c, 'T', 0x0c, ' ', 0x0c, 'S', 0x0c, 't', 0x0c, 'a', 0x0c, 'r', 0x0c, 't', 0x0c, '!', 0x0c\r
+ErrorString: \r
+ .byte 'G', 0x0c, 'P', 0x0c, 'T', 0x0c, ' ', 0x0c, 'E', 0x0c, 'r', 0x0c, 'r', 0x0c, 'o', 0x0c, 'r', 0x0c, '!', 0x0c\r
+\r
+# ****************************************************************************\r
+# PhysicalDrive - Used to indicate which disk to be boot\r
+# Can be patched by tool\r
+# ****************************************************************************\r
+ .org 0x01B6\r
+PhysicalDrive: .byte 0x80\r
+\r
+# ****************************************************************************\r
+# GptPartitionIndicator - Used to indicate which GPT partition to be boot\r
+# Can be patched by tool\r
+# ****************************************************************************\r
+ .org 0x01B7\r
+GptPartitionIndicator: .byte 0\r
+\r
+# ****************************************************************************\r
+# Unique MBR signature\r
+# ****************************************************************************\r
+ .org 0x01B8\r
+ .ascii "DUET"\r
+\r
+# ****************************************************************************\r
+# Unknown\r
+# ****************************************************************************\r
+ .org 0x01BC\r
+ .word 0\r
+\r
+# ****************************************************************************\r
+# PMBR Entry - Can be patched by tool\r
+# ****************************************************************************\r
+ .org 0x01BE\r
+ .byte 0 # Boot Indicator\r
+ .byte 0xff # Start Header\r
+ .byte 0xff # Start Sector\r
+ .byte 0xff # Start Track\r
+ .byte 0xee # OS Type\r
+ .byte 0xff # End Header\r
+ .byte 0xff # End Sector\r
+ .byte 0xff # End Track\r
+ .long 1 # Starting LBA\r
+ .long 0xFFFFFFFF # End LBA\r
+\r
+ .org 0x01CE\r
+ .long 0,0,0,0\r
+ .org 0x01DE\r
+ .long 0,0,0,0\r
+ .org 0x01EE\r
+ .long 0,0,0,0\r
+\r
+# ****************************************************************************\r
+# Sector Signature\r
+# ****************************************************************************\r
+\r
+ .org 0x01FE\r
+SectorSignature: \r
+ .word 0xaa55 # Boot Sector Signature\r
+\r
+\r
+\r