1*4a5d661aSToomas Soome#- 2*4a5d661aSToomas Soome# Copyright (c) 2007 Yahoo!, Inc. 3*4a5d661aSToomas Soome# All rights reserved. 4*4a5d661aSToomas Soome# Written by: John Baldwin <jhb@FreeBSD.org> 5*4a5d661aSToomas Soome# 6*4a5d661aSToomas Soome# Redistribution and use in source and binary forms, with or without 7*4a5d661aSToomas Soome# modification, are permitted provided that the following conditions 8*4a5d661aSToomas Soome# are met: 9*4a5d661aSToomas Soome# 1. Redistributions of source code must retain the above copyright 10*4a5d661aSToomas Soome# notice, this list of conditions and the following disclaimer. 11*4a5d661aSToomas Soome# 2. Redistributions in binary form must reproduce the above copyright 12*4a5d661aSToomas Soome# notice, this list of conditions and the following disclaimer in the 13*4a5d661aSToomas Soome# documentation and/or other materials provided with the distribution. 14*4a5d661aSToomas Soome# 3. Neither the name of the author nor the names of any co-contributors 15*4a5d661aSToomas Soome# may be used to endorse or promote products derived from this software 16*4a5d661aSToomas Soome# without specific prior written permission. 17*4a5d661aSToomas Soome# 18*4a5d661aSToomas Soome# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19*4a5d661aSToomas Soome# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*4a5d661aSToomas Soome# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*4a5d661aSToomas Soome# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22*4a5d661aSToomas Soome# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23*4a5d661aSToomas Soome# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24*4a5d661aSToomas Soome# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25*4a5d661aSToomas Soome# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26*4a5d661aSToomas Soome# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27*4a5d661aSToomas Soome# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28*4a5d661aSToomas Soome# SUCH DAMAGE. 29*4a5d661aSToomas Soome# 30*4a5d661aSToomas Soome# $FreeBSD$ 31*4a5d661aSToomas Soome# 32*4a5d661aSToomas Soome# Partly from: src/sys/boot/i386/mbr/mbr.s 1.7 33*4a5d661aSToomas Soome 34*4a5d661aSToomas Soome# A 512 byte PMBR boot manager to read a boot program and run it. 35*4a5d661aSToomas Soome# The embedded MBR is set up for PMBR and default bootblock sector 36*4a5d661aSToomas Soome# is hardcoded to 256 and size 1. The actual values are supposed to be 37*4a5d661aSToomas Soome# updated by installboot. 38*4a5d661aSToomas Soome 39*4a5d661aSToomas Soome .set LOAD,0x7c00 # Load address 40*4a5d661aSToomas Soome .set EXEC,0x600 # Execution address 41*4a5d661aSToomas Soome .set MAGIC,0xaa55 # Magic: bootable 42*4a5d661aSToomas Soome .set SECSIZE,0x200 # Size of a single disk sector 43*4a5d661aSToomas Soome .set DISKSIG,440 # Disk signature offset 44*4a5d661aSToomas Soome .set STACK,EXEC+SECSIZE*4 # Stack address 45*4a5d661aSToomas Soome .set DPBUF,STACK 46*4a5d661aSToomas Soome 47*4a5d661aSToomas Soome .set NHRDRV,0x475 # Number of hard drives 48*4a5d661aSToomas Soome 49*4a5d661aSToomas Soome .globl start # Entry point 50*4a5d661aSToomas Soome .code16 51*4a5d661aSToomas Soome .text 52*4a5d661aSToomas Soome 53*4a5d661aSToomas Soomestart: jmp real_code 54*4a5d661aSToomas Soome .fill 0x3c,0x1,0x90 # fill with nop to ease disasm 55*4a5d661aSToomas Soome# 56*4a5d661aSToomas Soome# BIOS Parameter Block. Reserved space from 0xb to 0x3e, the FAT32 BPB 57*4a5d661aSToomas Soome# is 60 (3Ch) bytes. 58*4a5d661aSToomas Soome# 59*4a5d661aSToomas Soome . = start + 0x3e 60*4a5d661aSToomas Soome 61*4a5d661aSToomas Soome# 62*4a5d661aSToomas Soome# Setup the segment registers for flat addressing and setup the stack. 63*4a5d661aSToomas Soome# 64*4a5d661aSToomas Soomereal_code: cld # String ops inc 65*4a5d661aSToomas Soome xorw %ax,%ax # Zero 66*4a5d661aSToomas Soome movw %ax,%es # Address 67*4a5d661aSToomas Soome movw %ax,%ds # data 68*4a5d661aSToomas Soome movw %ax,%ss # Set up 69*4a5d661aSToomas Soome movw $STACK,%sp # stack 70*4a5d661aSToomas Soome# 71*4a5d661aSToomas Soome# Relocate ourself to a lower address so that we have more room to load 72*4a5d661aSToomas Soome# other sectors. 73*4a5d661aSToomas Soome# 74*4a5d661aSToomas Soome movw $main-EXEC+LOAD,%si # Source 75*4a5d661aSToomas Soome movw $main,%di # Destination 76*4a5d661aSToomas Soome movw $SECSIZE-(main-start),%cx # Byte count 77*4a5d661aSToomas Soome rep # Relocate 78*4a5d661aSToomas Soome movsb # code 79*4a5d661aSToomas Soome# 80*4a5d661aSToomas Soome# Jump to the relocated code. 81*4a5d661aSToomas Soome# 82*4a5d661aSToomas Soome jmp main-LOAD+EXEC # To relocated code 83*4a5d661aSToomas Soome# 84*4a5d661aSToomas Soome# Validate drive number in %dl. 85*4a5d661aSToomas Soome# 86*4a5d661aSToomas Soomemain: cmpb $0x80,%dl # Drive valid? 87*4a5d661aSToomas Soome jb main.1 # No 88*4a5d661aSToomas Soome movb NHRDRV,%dh # Calculate the highest 89*4a5d661aSToomas Soome addb $0x80,%dh # drive number available 90*4a5d661aSToomas Soome cmpb %dh,%dl # Within range? 91*4a5d661aSToomas Soome jb main.2 # Yes 92*4a5d661aSToomas Soomemain.1: movb $0x80,%dl # Assume drive 0x80 93*4a5d661aSToomas Soome# 94*4a5d661aSToomas Soome# Load stage2 and start it. location and size is written by installboot 95*4a5d661aSToomas Soome# and if size is 0, we can not do anything... 96*4a5d661aSToomas Soome# 97*4a5d661aSToomas Soomemain.2: movw stage2_size, %ax 98*4a5d661aSToomas Soome cmpw $0, %ax 99*4a5d661aSToomas Soome je err_noboot # the stage2 size is not set 100*4a5d661aSToomas Soome pushw %dx # save drive 101*4a5d661aSToomas Soome movb $0x41, %ah # check extensions 102*4a5d661aSToomas Soome movw $0x55aa, %bx 103*4a5d661aSToomas Soome int $0x13 104*4a5d661aSToomas Soome popw %dx # restore drive 105*4a5d661aSToomas Soome jc err_rd # need lba mode for now 106*4a5d661aSToomas Soome cmpw $0xaa55, %bx # chs support is not 107*4a5d661aSToomas Soome jne err_rd # implemented. 108*4a5d661aSToomas Soome movw $stage2_sector, %si # pointer to lba 109*4a5d661aSToomas Soome movw $LOAD/16,%bx # set buffer segment 110*4a5d661aSToomas Soome movw %bx,%es 111*4a5d661aSToomas Soome xorw %bx,%bx # and offset 112*4a5d661aSToomas Soomeload_boot: push %si # Save %si 113*4a5d661aSToomas Soome call read 114*4a5d661aSToomas Soome pop %si # Restore 115*4a5d661aSToomas Soome decw stage2_size # stage2_size-- 116*4a5d661aSToomas Soome jnz next_boot 117*4a5d661aSToomas Soomeboot: mov %bx,%es # Reset %es to zero 118*4a5d661aSToomas Soome jmp LOAD # Jump to boot code 119*4a5d661aSToomas Soomenext_boot: incl (%si) # Next LBA 120*4a5d661aSToomas Soome adcl $0,4(%si) 121*4a5d661aSToomas Soome mov %es,%ax # Adjust segment for next 122*4a5d661aSToomas Soome addw $SECSIZE/16,%ax # sector 123*4a5d661aSToomas Soome mov %ax,%es # 124*4a5d661aSToomas Soome jmp load_boot 125*4a5d661aSToomas Soome# 126*4a5d661aSToomas Soome# Load a sector (64-bit LBA at %si) from disk %dl into %es:%bx by creating 127*4a5d661aSToomas Soome# a EDD packet on the stack and passing it to the BIOS. Trashes %ax and %si. 128*4a5d661aSToomas Soome# 129*4a5d661aSToomas Soomeread: pushl 0x4(%si) # Set the LBA 130*4a5d661aSToomas Soome pushl 0x0(%si) # address 131*4a5d661aSToomas Soome pushw %es # Set the address of 132*4a5d661aSToomas Soome pushw %bx # the transfer buffer 133*4a5d661aSToomas Soome pushw $0x1 # Read 1 sector 134*4a5d661aSToomas Soome pushw $0x10 # Packet length 135*4a5d661aSToomas Soome movw %sp,%si # Packer pointer 136*4a5d661aSToomas Soome movw $0x4200,%ax # BIOS: LBA Read from disk 137*4a5d661aSToomas Soome int $0x13 # Call the BIOS 138*4a5d661aSToomas Soome add $0x10,%sp # Restore stack 139*4a5d661aSToomas Soome jc err_rd # If error 140*4a5d661aSToomas Soome ret 141*4a5d661aSToomas Soome# 142*4a5d661aSToomas Soome# Various error message entry points. 143*4a5d661aSToomas Soome# 144*4a5d661aSToomas Soomeerr_rd: movw $msg_rd,%si # "I/O error loading 145*4a5d661aSToomas Soome jmp putstr # boot loader" 146*4a5d661aSToomas Soome 147*4a5d661aSToomas Soomeerr_noboot: movw $msg_noboot,%si # "Missing boot 148*4a5d661aSToomas Soome jmp putstr # loader" 149*4a5d661aSToomas Soome# 150*4a5d661aSToomas Soome# Output an ASCIZ string to the console via the BIOS. 151*4a5d661aSToomas Soome# 152*4a5d661aSToomas Soomeputstr.0: movw $0x7,%bx # Page:attribute 153*4a5d661aSToomas Soome movb $0xe,%ah # BIOS: Display 154*4a5d661aSToomas Soome int $0x10 # character 155*4a5d661aSToomas Soomeputstr: lodsb # Get character 156*4a5d661aSToomas Soome testb %al,%al # End of string? 157*4a5d661aSToomas Soome jnz putstr.0 # No 158*4a5d661aSToomas Soomeputstr.1: jmp putstr.1 # Await reset 159*4a5d661aSToomas Soome 160*4a5d661aSToomas Soomemsg_rd: .asciz "I/O error" 161*4a5d661aSToomas Soomemsg_noboot: .asciz "No boot loader" 162*4a5d661aSToomas Soome 163*4a5d661aSToomas Soome nop 164*4a5d661aSToomas Soomembr_version: .byte 1, 1 # 1.1 165*4a5d661aSToomas Soome .align 4 166*4a5d661aSToomas Soomestage2_size: .word 1 # bootblock size in sectors 167*4a5d661aSToomas Soomestage2_sector: .quad 256 # lba of bootblock 168*4a5d661aSToomas Soomedisk_uuid: .quad 0 # uuid 169*4a5d661aSToomas Soome .quad 0 170*4a5d661aSToomas Soome 171*4a5d661aSToomas Soome# this is the end of the code block we can use, next is space for 172*4a5d661aSToomas Soome# signature, partition table 4 entries and signature. 173*4a5d661aSToomas Soome .org DISKSIG,0x1b8 # 174*4a5d661aSToomas Soomesig: .long 0 # OS Disk Signature 175*4a5d661aSToomas Soome .word 0 # "Unknown" in PMBR 176*4a5d661aSToomas Soome 177*4a5d661aSToomas Soomepartbl: .byte 0x00 # non-bootable 178*4a5d661aSToomas Soome .byte 0x00 # head 0 179*4a5d661aSToomas Soome .byte 0x02 # sector 180*4a5d661aSToomas Soome .byte 0x00 # cylinder 181*4a5d661aSToomas Soome .byte 0xEE # ID 182*4a5d661aSToomas Soome .byte 0xFF # ending head 183*4a5d661aSToomas Soome .byte 0xFF # ending sector 184*4a5d661aSToomas Soome .byte 0xFF # ending cylinder 185*4a5d661aSToomas Soome .long 0x00000001 # starting LBA 186*4a5d661aSToomas Soome .long 0xFFFFFFFF # size 187*4a5d661aSToomas Soome .fill 0x10,0x3,0x0 # other 3 entries 188*4a5d661aSToomas Soome .word MAGIC # Magic number 189