--- /dev/null
+#------------------------------------------------------------------------------\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
+#* bs32.asm\r
+#* \r
+#* Abstract:\r
+#*\r
+#------------------------------------------------------------------------------\r
+\r
+ #.MODEL small\r
+ .stack: \r
+ .486p: \r
+ .code: \r
+\r
+.equ FAT_DIRECTORY_ENTRY_SIZE, 0x0020\r
+.equ FAT_DIRECTORY_ENTRY_SHIFT, 5\r
+.equ BLOCK_SIZE, 0x0200\r
+.equ BLOCK_MASK, 0x01ff\r
+.equ BLOCK_SHIFT, 9\r
+ # "EFILDR_____"\r
+.equ LOADER_FILENAME_PART1, 0x4c494645 # "EFIL"\r
+.equ LOADER_FILENAME_PART2, 0x30325244 # "DR20"\r
+.equ LOADER_FILENAME_PART3, 0x20202030 # "0___"\r
+\r
+ .org 0x00\r
+Ia32Jump: \r
+ jmp BootSectorEntryPoint # JMP inst - 3 bytes\r
+ nop\r
+\r
+OemId: .ascii "INTEL " # OemId - 8 bytes\r
+# BPB data below will be fixed by tool\r
+SectorSize: .word 0 # Sector Size - 16 bits\r
+SectorsPerCluster: .byte 0 # Sector Per Cluster - 8 bits\r
+ReservedSectors: .word 0 # Reserved Sectors - 16 bits\r
+NoFats: .byte 0 # Number of FATs - 8 bits\r
+RootEntries: .word 0 # Root Entries - 16 bits\r
+Sectors: .word 0 # Number of Sectors - 16 bits\r
+Media: .byte 0 # Media - 8 bits - ignored\r
+SectorsPerFat: .word 0 # Sectors Per FAT - 16 bits\r
+SectorsPerTrack: .word 0 # Sectors Per Track - 16 bits - ignored\r
+Heads: .word 0 # Heads - 16 bits - ignored\r
+HiddenSectors: .long 0 # Hidden Sectors - 32 bits - ignored\r
+LargeSectors: .long 0 # Large Sectors - 32 bits \r
+\r
+#******************************************************************************\r
+#\r
+#The structure for FAT32 starting at offset 36 of the boot sector. (At this point, \r
+#the BPB/boot sector for FAT12 and FAT16 differs from the BPB/boot sector for FAT32.)\r
+#\r
+#******************************************************************************\r
+\r
+SectorsPerFat32: .long 0 # Sectors Per FAT for FAT32 - 4 bytes\r
+ExtFlags: .word 0 # Mirror Flag - 2 bytes\r
+FSVersion: .word 0 # File System Version - 2 bytes\r
+RootCluster: .long 0 # 1st Cluster Number of Root Dir - 4 bytes\r
+FSInfo: .word 0 # Sector Number of FSINFO - 2 bytes\r
+BkBootSector: .word 0 # Sector Number of Bk BootSector - 2 bytes\r
+Reserved: .fill 12, 1, 0 # Reserved Field - 12 bytes\r
+PhysicalDrive: .byte 0 # Physical Drive Number - 1 byte\r
+Reserved1: .byte 0 # Reserved Field - 1 byte\r
+Signature: .byte 0 # Extended Boot Signature - 1 byte\r
+VolId: .ascii " " # Volume Serial Number - 4 bytes\r
+FatLabel: .ascii " " # Volume Label - 11 bytes\r
+FileSystemType: .ascii "FAT32 " # File System Type - 8 bytes\r
+BootSectorEntryPoint: \r
+ #ASSUME ds:@code\r
+ #ASSUME ss:@code\r
+\r
+# ****************************************************************************\r
+# Start Print\r
+# ****************************************************************************\r
+ leaw %cs:StartString, %si\r
+ call PrintString\r
+\r
+# ****************************************************************************\r
+# Print over\r
+# ****************************************************************************\r
+\r
+ movw %cs, %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
+ cmpw $0xaa55, SectorSignature(%bp) # Verify Boot Sector Signature\r
+ jne BadBootSector\r
+ movw RootEntries(%bp), %cx # cx = RootEntries\r
+ shlw $FAT_DIRECTORY_ENTRY_SHIFT, %cx # cx = cx * 32 = cx * sizeof(FAT_DIRECTORY_ENTRY) = Size of Root Directory in bytes\r
+ movw %cx, %bx # bx = size of the Root Directory in bytes\r
+ andw $BLOCK_MASK, %bx # See if it is an even number of sectors long\r
+ jne BadBootSector # If is isn't, then the boot sector is bad.\r
+ movw %cx, %bx # bx = size of the Root Directory in bytes\r
+ shrw $BLOCK_SHIFT, %bx # bx = size of Root Directory in sectors\r
+ movb NoFats(%bp), %al # al = NoFats\r
+ xorb %ah, %ah # ah = 0 ==> ax = NoFats\r
+ mulw SectorsPerFat32(%bp) # ax = NoFats * SectorsPerFat\r
+ addw ReservedSectors(%bp), %ax # ax = NoFats * SectorsPerFat + ReservedSectors = RootLBA\r
+ addw %bx, %ax # ax = NoFats * SectorsPerFat + ReservedSectors + RootDirSectors = FirstClusterLBA\r
+ movw %ax, (%bp) # Save FirstClusterLBA for later use\r
+\r
+ movw RootCluster(%bp), %ax # ax = StartCluster of Root Directory\r
+ subw $2, %ax # ax = StartCluster - 2\r
+ xorb %bh, %bh\r
+ movb SectorsPerCluster(%bp), %bl # bx = SectorsPerCluster\r
+ mulw %bx # ax = (StartCluster - 2) * SectorsPerCluster\r
+ addw (%bp), %ax # ax = FirstClusterLBA + (StartCluster-2)*SectorsPerCluster\r
+ pushw %ds\r
+ popw %es\r
+ xorw %di, %di # Store directory in es:di = 1000:0000\r
+ call ReadBlocks # Read StartCluster of Root Directory\r
+\r
+ # dx - variable storage (initial value is 0)\r
+ # bx - loader (initial value is 0)\r
+ xorw %dx, %dx\r
+ xorw %bx, %bx\r
+\r
+FindEFILDR: \r
+ cmpl $LOADER_FILENAME_PART1, (%di)\r
+ jne FindVARSTORE\r
+ cmpl $LOADER_FILENAME_PART2, 4(%di)\r
+ jne FindVARSTORE\r
+ cmpl $LOADER_FILENAME_PART3, 7(%di)\r
+ jne FindVARSTORE\r
+ movw 26(%di), %bx # bx = Start Cluster for EFILDR <----------------------------------\r
+ testw %dx, %dx\r
+ je FindNext # Efivar.bin is not loaded\r
+ jmp FoundAll\r
+\r
+FindVARSTORE: \r
+ # if the file is not loader file, see if it's "EFIVAR BIN"\r
+ cmpl $0x56494645, (%di) # Compare to "EFIV"\r
+ jne FindNext\r
+ cmpl $0x20205241, 4(%di) # Compare to "AR "\r
+ jne FindNext\r
+ cmpl $0x4e494220, 7(%di) # Compare to " BIN"\r
+ jne FindNext\r
+ movw %di, %dx # dx = Offset of Start Cluster for Efivar.bin <---------------------\r
+ addw $26, %dx\r
+ testw %bx, %bx\r
+ je FindNext # Efildr is not loaded\r
+ jmp FoundAll\r
+\r
+FindNext: \r
+ # go to next find\r
+ addw $FAT_DIRECTORY_ENTRY_SIZE, %di # Increment di\r
+ subw $FAT_DIRECTORY_ENTRY_SIZE, %cx # Decrement cx\r
+ # TODO: jump to FindVarStore if ...\r
+ jne FindEFILDR\r
+ jmp NotFoundAll\r
+\r
+FoundAll: \r
+FoundEFILDR: \r
+ movw %bx, %cx # cx = Start Cluster for EFILDR <----------------------------------\r
+ movw %cs, %ax # Destination = 2000:0000\r
+ addw $0x2000, %ax\r
+ movw %ax, %es\r
+ xorw %di, %di\r
+ReadFirstClusterOfEFILDR: \r
+ movw %cx, %ax # ax = StartCluster\r
+ subw $2, %ax # ax = StartCluster - 2\r
+ xorb %bh, %bh\r
+ movb SectorsPerCluster(%bp), %bl # bx = SectorsPerCluster\r
+ pushw %dx\r
+ mulw %bx\r
+ popw %dx # ax = (StartCluster - 2) * SectorsPerCluster\r
+ addw (%bp), %ax # ax = FirstClusterLBA + (StartCluster-2)*SectorsPerCluster\r
+ xorb %bh, %bh\r
+ movb SectorsPerCluster(%bp), %bl # bx = Number of Sectors in a cluster\r
+ pushw %es\r
+ call ReadBlocks\r
+ popw %ax\r
+JumpIntoFirstSectorOfEFILDR: \r
+ movw %ax, JumpSegment(%bp)\r
+JumpFarInstruction: \r
+ .byte 0xea\r
+JumpOffset: \r
+ .word 0x000\r
+JumpSegment: \r
+ .word 0x2000\r
+\r
+\r
+PrintString: \r
+ movw $0xb800, %ax\r
+ movw %ax, %es\r
+ movw $0x7c0, %ax\r
+ movw %ax, %ds\r
+ movw $7, %cx\r
+ movw $160, %di\r
+ rep\r
+ movsw\r
+ ret\r
+# ****************************************************************************\r
+# ReadBlocks - Reads a set of blocks from a block device\r
+#\r
+# AX = Start LBA\r
+# BX = Number of Blocks to Read\r
+# ES:DI = Buffer to store sectors read from disk\r
+# ****************************************************************************\r
+\r
+# cx = Blocks\r
+# bx = NumberOfBlocks\r
+# si = StartLBA\r
+\r
+ReadBlocks: \r
+ pusha\r
+ addl LBAOffsetForBootSector(%bp), %eax # Add LBAOffsetForBootSector to Start LBA\r
+ addl HiddenSectors(%bp), %eax # Add HiddenSectors to Start LBA\r
+ movl %eax, %esi # esi = Start LBA\r
+ movw %bx, %cx # cx = Number of blocks to read\r
+ReadCylinderLoop: \r
+ movw $0x7bfc, %bp # bp = 0x7bfc\r
+ movl %esi, %eax # eax = Start LBA\r
+ xorl %edx, %edx # edx = 0\r
+ movzwl (%bp), %ebx # bx = MaxSector\r
+ divl %ebx # ax = StartLBA / MaxSector\r
+ incw %dx # dx = (StartLBA % MaxSector) + 1\r
+ subw %dx, %bx # bx = MaxSector - Sector\r
+ incw %bx # bx = MaxSector - Sector + 1\r
+ cmpw %bx, %cx # Compare (Blocks) to (MaxSector - Sector + 1)\r
+ jg LimitTransfer\r
+ movw %cx, %bx # bx = Blocks\r
+LimitTransfer: \r
+ pushw %cx\r
+ movb %dl, %cl # cl = (StartLBA % MaxSector) + 1 = Sector\r
+ xorw %dx, %dx # dx = 0\r
+ divw 2(%bp) # ax = ax / (MaxHead + 1) = Cylinder \r
+ # dx = ax % (MaxHead + 1) = Head\r
+\r
+ pushw %bx # Save number of blocks to transfer\r
+ movb %dl, %dh # dh = Head\r
+ movw $0x7c00, %bp # bp = 0x7c00\r
+ movb PhysicalDrive(%bp), %dl # dl = Drive Number\r
+ movb %al, %ch # ch = Cylinder\r
+ movb %bl, %al # al = Blocks\r
+ movb $2, %ah # ah = Function 2\r
+ movw %di, %bx # es:bx = Buffer address\r
+ int $0x13\r
+ jc DiskError\r
+ popw %bx\r
+ popw %cx\r
+ movzwl %bx, %ebx\r
+ addl %ebx, %esi # StartLBA = StartLBA + NumberOfBlocks\r
+ subw %bx, %cx # Blocks = Blocks - NumberOfBlocks\r
+ movw %es, %ax\r
+ shlw $(BLOCK_SHIFT-4), %bx\r
+ addw %bx, %ax\r
+ movw %ax, %es # es:di = es:di + NumberOfBlocks*BLOCK_SIZE\r
+ cmpw $0, %cx\r
+ jne ReadCylinderLoop\r
+ popa\r
+ ret\r
+\r
+# ****************************************************************************\r
+# ERROR Condition:\r
+# ****************************************************************************\r
+NotFoundAll: \r
+ # if we found EFILDR, continue\r
+ testw %bx, %bx\r
+ jne FoundEFILDR\r
+BadBootSector: \r
+DiskError: \r
+ leaw %cs:ErrorString, %si\r
+ call PrintString\r
+Halt: \r
+ jmp Halt\r
+\r
+StartString: \r
+ .byte 'B', 0x0c, 'S', 0x0c, 't', 0x0c, 'a', 0x0c, 'r', 0x0c, 't', 0x0c, '!', 0x0c\r
+ErrorString: \r
+ .byte 'B', 0x0c, 'E', 0x0c, 'r', 0x0c, 'r', 0x0c, 'o', 0x0c, 'r', 0x0c, '!', 0x0c\r
+\r
+# ****************************************************************************\r
+# LBA Offset for BootSector, need patched by tool for HD boot.\r
+# ****************************************************************************\r
+\r
+ .org 0x01fa\r
+LBAOffsetForBootSector: \r
+ .long 0x0\r
+\r
+# ****************************************************************************\r
+# Sector Signature\r
+# ****************************************************************************\r
+\r
+ .org 0x01fe\r
+SectorSignature: \r
+ .word 0xaa55 # Boot Sector Signature\r
+\r
+\r
+\r