Home | History | Annotate | Download | only in BootSector
      1 #------------------------------------------------------------------------------
      2 #*
      3 #*   Copyright (c) 2006 - 2007, 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
      8 #*
      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.
     11 #*
     12 #*   bootsect.S
     13 #*
     14 #*   bootsect.S is built as 16-bit binary file in 512 bytes and patched to disk/partition's
     15 #*   first section - boot sector.
     16 #*
     17 #*   The startup sequence for DUET disk boot sector is:
     18 #*
     19 #*   1, LegacyBios check 0xAA55 signature at boot sectore offset 0x1FE to judget
     20 #*      whether disk/partition is bootable.
     21 #*   2, LegacyBios will load boot sector to 0x7c00 in real mode, pass BPB data and
     22 #*      hand off control to 0x7c00 code.
     23 #*   3, boot sector code simply parse FAT format in boot disk and find EfiLdr binary file
     24 #*      and EfiVar.bin if exists. For first boot, EfiVar.bin does not exist.
     25 #*   4, boot sector load the first sector of EfiLdr binary which is start.com to
     26 #*      0x2000:0x0000 address.
     27 #*   5, boot sector handoff control to 0x2000:0x0000 for start.com binary.
     28 #*
     29 #------------------------------------------------------------------------------
     30 
     31         .stack:
     32         .486p:
     33         .code16
     34 
     35 .equ                      FAT_DIRECTORY_ENTRY_SIZE, 0x020
     36 .equ                      FAT_DIRECTORY_ENTRY_SHIFT, 5
     37 .equ                      BLOCK_SIZE, 0x0200
     38 .equ                      BLOCK_MASK, 0x01ff
     39 .equ                      BLOCK_SHIFT, 9
     40                                                # "EFILDR_____"
     41 .equ                      LOADER_FILENAME_PART1, 0x04c494645    # "EFIL"
     42 .equ                      LOADER_FILENAME_PART2, 0x020205244    # "DR__"
     43 .equ                      LOADER_FILENAME_PART3, 0x020202020    # "____"
     44 
     45         .org 0x0
     46 .global _start
     47 _start:
     48 Ia32Jump:
     49   jmp   BootSectorEntryPoint  # JMP inst                  - 3 bytes
     50   nop
     51 
     52 OemId:              .ascii   "INTEL   "       # OemId               - 8 bytes
     53 # BPB data below will be fixed by tool
     54 SectorSize:         .word  0                  # Sector Size         - 16 bits
     55 SectorsPerCluster:  .byte  0                  # Sector Per Cluster  - 8 bits
     56 ReservedSectors:    .word  0                  # Reserved Sectors    - 16 bits
     57 NoFats:             .byte  0                  # Number of FATs      - 8 bits
     58 RootEntries:        .word  0                  # Root Entries        - 16 bits
     59 Sectors:            .word  0                  # Number of Sectors   - 16 bits
     60 Media:              .byte  0                  # Media               - 8 bits  - ignored
     61 SectorsPerFat:      .word  0                  # Sectors Per FAT     - 16 bits
     62 SectorsPerTrack:    .word  0                  # Sectors Per Track   - 16 bits - ignored
     63 Heads:              .word  0                  # Heads               - 16 bits - ignored
     64 HiddenSectors:      .long  0                  # Hidden Sectors      - 32 bits - ignored
     65 LargeSectors:       .long  0                  # Large Sectors       - 32 bits
     66 PhysicalDrive:      .byte  0                  # PhysicalDriveNumber - 8 bits  - ignored
     67 CurrentHead:        .byte  0                  # Current Head        - 8 bits
     68 Signature:          .byte  0                  # Signature           - 8 bits  - ignored
     69 VolId:              .ascii "    "             # Volume Serial Number- 4 bytes
     70 FatLabel:           .ascii "           "      # Label               - 11 bytes
     71 SystemId:           .ascii "FAT12   "         # SystemId            - 8 bytes
     72 
     73 BootSectorEntryPoint:
     74         #ASSUME ds:@code
     75         #ASSUME ss:@code
     76 
     77 # ****************************************************************************
     78 # Start Print
     79 # ****************************************************************************
     80   movw $StartString, %si
     81   call PrintString
     82 
     83 # ****************************************************************************
     84 # Print over
     85 # ****************************************************************************
     86 
     87   movw  %cs, %ax      # ax = 0
     88   movw  %ax, %ss      # ss = 0
     89   addw  $0x1000, %ax
     90   movw  %ax, %ds
     91 
     92   movw  $0x7c00, %sp  # sp = 0x7c00
     93   movw  %sp, %bp      # bp = 0x7c00
     94 
     95   movb  $8, %ah                             # ah = 8 - Get Drive Parameters Function
     96   movb  %dl, PhysicalDrive(%bp)             # BBS defines that BIOS would pass the booting driver number to the loader through DL
     97   int   $0x13                               # Get Drive Parameters
     98   xorw  %ax, %ax                # ax = 0
     99   movb  %dh, %al                # al = dh
    100   incb  %al                     # MaxHead = al + 1
    101   pushw %ax                     # 0000:7bfe = MaxHead
    102   movb  %cl, %al                # al = cl
    103   andb  $0x3f, %al              # MaxSector = al & 0x3f
    104   pushw %ax                     # 0000:7bfc = MaxSector
    105 
    106   cmpw  $0xaa55, SectorSignature(%bp)         # Verify Boot Sector Signature
    107   jne   BadBootSector
    108   movw  RootEntries(%bp), %cx             # cx = RootEntries
    109   shlw  $FAT_DIRECTORY_ENTRY_SHIFT, %cx   # cx = cx * 32 = cx * sizeof(FAT_DIRECTORY_ENTRY) = Size of Root Directory in bytes
    110   movw  %cx, %bx                          # bx = size of the Root Directory in bytes
    111   andw  $BLOCK_MASK, %bx                  # See if it is an even number of sectors long
    112   jne   BadBootSector                     # If is isn't, then the boot sector is bad.
    113   movw  %cx, %bx                          # bx = size of the Root Directory in bytes
    114   shrw  $BLOCK_SHIFT, %bx                 # bx = size of Root Directory in sectors
    115   movb  NoFats(%bp), %al                  # al = NoFats
    116   xorb  %ah, %ah                          # ah = 0  ==> ax = NoFats
    117   mulw  SectorsPerFat(%bp)                # ax = NoFats * SectorsPerFat
    118   addw  ReservedSectors(%bp), %ax         # ax = NoFats * SectorsPerFat + ReservedSectors = RootLBA
    119   pushw %ds
    120   popw  %es
    121   xorw  %di, %di                          # Store directory in es:di = 1000:0000
    122   call  ReadBlocks                        # Read entire Root Directory
    123   addw  %bx, %ax                          # ax = NoFats * SectorsPerFat + ReservedSectors + RootDirSectors = FirstClusterLBA (FirstDataSector)
    124   movw  %ax, (%bp)                        # Save FirstClusterLBA (FirstDataSector) for later use
    125 
    126   # dx - variable storage (initial value is 0)
    127   # bx - loader (initial value is 0)
    128   xorw  %dx, %dx
    129   xorw  %bx, %bx
    130 
    131 FindEFILDR:
    132   cmpl   $LOADER_FILENAME_PART1, (%di)        # Compare to "EFIL"
    133   jne   FindVARSTORE
    134   cmpl   $LOADER_FILENAME_PART2, 4(%di)
    135   jne   FindVARSTORE
    136   cmpl   $LOADER_FILENAME_PART3, 7(%di)
    137   jne   FindVARSTORE
    138   movw  26(%di), %bx                      # bx = Start Cluster for EFILDR  <----------------------------------
    139   testw %dx, %dx
    140   je    FindNext                          # Efivar.bin is not loaded
    141   jmp   FoundAll
    142 
    143 FindVARSTORE:
    144   ## if the file is not loader file, see if it's "EFIVAR  BIN"
    145   cmpl  $0x56494645, (%di)                # Compare to "EFIV"
    146   jne   FindNext
    147   cmpl  $0x20205241, 4(%di)               # Compare to "AR  "
    148   jne   FindNext
    149   cmpl  $0x4e494220, 7(%di)               # Compare to " BIN"
    150   jne   FindNext
    151   movw  %di, %dx                          # dx = Offset of Start Cluster for Efivar.bin <---------------------
    152   addw  $26, %dx
    153   testw %bx, %bx
    154   je    FindNext                          # Efildr is not loaded
    155   jmp   FoundAll
    156 
    157 FindNext:
    158   # go to next find
    159   addw  $FAT_DIRECTORY_ENTRY_SIZE, %di    # Increment di
    160   subw  $FAT_DIRECTORY_ENTRY_SIZE, %cx    # Decrement cx
    161   # TODO: jump to FindVarStore if ...
    162   jne   FindEFILDR
    163   jmp   NotFoundAll
    164 
    165 FoundAll:
    166 FoundEFILDR:
    167   movw    %bx, %cx                            # cx = Start Cluster for EFILDR  <----------------------------------
    168   movw    %cs, %ax                            # Destination = 2000:0000
    169   addw    $0x2000, %ax
    170   movw    %ax, %es
    171   xorw    %di, %di
    172 ReadFirstClusterOfEFILDR:
    173   movw    %cx, %ax                            # ax = StartCluster
    174   subw    $2, %ax                             # ax = StartCluster - 2
    175   xorb    %bh, %bh
    176   movb    SectorsPerCluster(%bp), %bl         # bx = SectorsPerCluster
    177   pushw   %dx
    178   mulw    %bx
    179   popw    %dx                                 # ax = (StartCluster - 2) * SectorsPerCluster
    180   addw    (%bp), %ax                          # ax = FirstClusterLBA + (StartCluster-2)*SectorsPerCluster
    181   xorb    %bh, %bh
    182   movb    SectorsPerCluster(%bp), %bl         # bx = Number of Sectors in a cluster
    183   pushw   %es
    184   call    ReadBlocks
    185   popw    %ax
    186 JumpIntoFirstSectorOfEFILDR:
    187   movw    %ax, JumpSegment(%bp)
    188 JumpFarInstruction:
    189   .byte   0xea
    190 JumpOffset:
    191   .word   0x000
    192 JumpSegment:
    193   .word   0x2000
    194 
    195 
    196 PrintString:
    197   movw $0xb800, %ax
    198   movw %ax, %es
    199   movw $0x7c0, %ax
    200   movw %ax, %ds
    201   movw $7, %cx
    202   movw $160, %di
    203   rep
    204   movsw
    205   ret
    206 # ****************************************************************************
    207 # ReadBlocks - Reads a set of blocks from a block device
    208 #
    209 # AX    = Start LBA
    210 # BX    = Number of Blocks to Read
    211 # ES:DI = Buffer to store sectors read from disk
    212 # ****************************************************************************
    213 
    214 # cx = Blocks
    215 # bx = NumberOfBlocks
    216 # si = StartLBA
    217 
    218 ReadBlocks:
    219   pusha
    220   addl    LBAOffsetForBootSector(%bp), %eax            # Add LBAOffsetForBootSector to Start LBA
    221   addl    HiddenSectors(%bp), %eax            # Add HiddenSectors to Start LBA
    222   movl    %eax, %esi                          # esi = Start LBA
    223   movw    %bx, %cx                            # cx = Number of blocks to read
    224 ReadCylinderLoop:
    225   movw    $0x7bfc, %bp                        # bp = 0x7bfc
    226   movl    %esi, %eax                          # eax = Start LBA
    227   xorl    %edx, %edx                          # edx = 0
    228   movzwl  (%bp), %ebx                         # bx = MaxSector
    229   divl    %ebx                                # ax = StartLBA / MaxSector
    230   incw    %dx                                 # dx = (StartLBA % MaxSector) + 1
    231   subw    %dx, %bx                            # bx = MaxSector - Sector
    232   incw    %bx                                 # bx = MaxSector - Sector + 1
    233   cmpw    %bx, %cx                            # Compare (Blocks) to (MaxSector - Sector + 1)
    234   jg      LimitTransfer
    235   movw    %cx, %bx                            # bx = Blocks
    236 LimitTransfer:
    237   pushw   %cx
    238   movb    %dl, %cl                            # cl = (StartLBA % MaxSector) + 1 = Sector
    239   xorw    %dx, %dx                            # dx = 0
    240   divw    2(%bp)                              # ax = ax / (MaxHead + 1) = Cylinder
    241                                               # dx = ax % (MaxHead + 1) = Head
    242 
    243   pushw   %bx                                 # Save number of blocks to transfer
    244   movb    %dl, %dh                            # dh = Head
    245   movw    $0x7c00, %bp                        # bp = 0x7c00
    246   movb    PhysicalDrive(%bp), %dl             # dl = Drive Number
    247   movb    %al, %ch                            # ch = Cylinder
    248   movb    %bl, %al                            # al = Blocks
    249   movb    $2, %ah                             # ah = Function 2
    250   movw    %di, %bx                            # es:bx = Buffer address
    251   int     $0x13
    252   jc      DiskError
    253   popw    %bx
    254   popw    %cx
    255   movzwl  %bx, %ebx
    256   addl    %ebx, %esi                          # StartLBA = StartLBA + NumberOfBlocks
    257   subw    %bx, %cx                            # Blocks = Blocks - NumberOfBlocks
    258   movw    %es, %ax
    259   shlw    $(BLOCK_SHIFT-4), %bx
    260   addw    %bx, %ax
    261   movw    %ax, %es                            # es:di = es:di + NumberOfBlocks*BLOCK_SIZE
    262   cmpw    $0, %cx
    263   jne     ReadCylinderLoop
    264   popa
    265   ret
    266 
    267 # ****************************************************************************
    268 # ERROR Condition:
    269 # ****************************************************************************
    270 NotFoundAll:
    271   ## if we found EFILDR, continue
    272   testw %bx, %bx
    273   jne  FoundEFILDR
    274 BadBootSector:
    275 DiskError:
    276   movw $ErrorString, %si
    277   call PrintString
    278 Halt:
    279   jmp   Halt
    280 
    281 StartString:
    282   .byte 'B', 0x0c, 'S', 0x0c, 't', 0x0c, 'a', 0x0c, 'r', 0x0c, 't', 0x0c, '!', 0x0c
    283 ErrorString:
    284   .byte 'B', 0x0c, 'E', 0x0c, 'r', 0x0c, 'r', 0x0c, 'o', 0x0c, 'r', 0x0c, '!', 0x0c
    285 
    286 # ****************************************************************************
    287 # LBA Offset for BootSector, need patched by tool for HD boot.
    288 # ****************************************************************************
    289 
    290   .org 0x01fa   # Comment it for pass build. Should optimise code size.
    291 LBAOffsetForBootSector:
    292   .long     0x0
    293 
    294 # ****************************************************************************
    295 # Sector Signature
    296 # ****************************************************************************
    297 
    298   .org 0x01fe    # Comment it for pass build.
    299 SectorSignature:
    300   .word     0xaa55      # Boot Sector Signature
    301 
    302 
    303 
    304