+++ /dev/null
-#------------------------------------------------------------------------------\r
-#*\r
-#* Copyright (c) 2006 - 2007, Intel Corporation. All rights reserved.<BR>\r
-#* 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
-#* bootsect.S\r
-#* \r
-#* bootsect.S is built as 16-bit binary file in 512 bytes and patched to disk/partition's \r
-#* first section - boot sector. \r
-#*\r
-#* The startup sequence for DUET disk boot sector is:\r
-#*\r
-#* 1, LegacyBios check 0xAA55 signature at boot sectore offset 0x1FE to judget \r
-#* whether disk/partition is bootable.\r
-#* 2, LegacyBios will load boot sector to 0x7c00 in real mode, pass BPB data and\r
-#* hand off control to 0x7c00 code.\r
-#* 3, boot sector code simply parse FAT format in boot disk and find EfiLdr binary file \r
-#* and EfiVar.bin if exists. For first boot, EfiVar.bin does not exist.\r
-#* 4, boot sector load the first sector of EfiLdr binary which is start.com to\r
-#* 0x2000:0x0000 address.\r
-#* 5, boot sector handoff control to 0x2000:0x0000 for start.com binary.\r
-#*\r
-#------------------------------------------------------------------------------\r
-\r
- .stack: \r
- .486p: \r
- .code16\r
-\r
-.equ FAT_DIRECTORY_ENTRY_SIZE, 0x020\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, 0x04c494645 # "EFIL"\r
-.equ LOADER_FILENAME_PART2, 0x020205244 # "DR__"\r
-.equ LOADER_FILENAME_PART3, 0x020202020 # "____"\r
-\r
- .org 0x0\r
-.global _start\r
-_start:\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
-PhysicalDrive: .byte 0 # PhysicalDriveNumber - 8 bits - ignored\r
-CurrentHead: .byte 0 # Current Head - 8 bits\r
-Signature: .byte 0 # Signature - 8 bits - ignored\r
-VolId: .ascii " " # Volume Serial Number- 4 bytes\r
-FatLabel: .ascii " " # Label - 11 bytes\r
-SystemId: .ascii "FAT12 " # SystemId - 8 bytes\r
-\r
-BootSectorEntryPoint: \r
- #ASSUME ds:@code\r
- #ASSUME ss:@code\r
-\r
-# ****************************************************************************\r
-# Start Print\r
-# ****************************************************************************\r
- movw $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 SectorsPerFat(%bp) # ax = NoFats * SectorsPerFat\r
- addw ReservedSectors(%bp), %ax # ax = NoFats * SectorsPerFat + ReservedSectors = RootLBA\r
- pushw %ds\r
- popw %es\r
- xorw %di, %di # Store directory in es:di = 1000:0000\r
- call ReadBlocks # Read entire Root Directory\r
- addw %bx, %ax # ax = NoFats * SectorsPerFat + ReservedSectors + RootDirSectors = FirstClusterLBA (FirstDataSector)\r
- movw %ax, (%bp) # Save FirstClusterLBA (FirstDataSector) for later use\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) # Compare to "EFIL"\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
- movw $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 # Comment it for pass build. Should optimise code size. \r
-LBAOffsetForBootSector: \r
- .long 0x0\r
-\r
-# ****************************************************************************\r
-# Sector Signature\r
-# ****************************************************************************\r
-\r
- .org 0x01fe # Comment it for pass build.\r
-SectorSignature: \r
- .word 0xaa55 # Boot Sector Signature\r
-\r
-\r
-\r