199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2007 Yahoo!, Inc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Written by: John Baldwin <jhb@FreeBSD.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 3. Neither the name of the author nor the names of any co-contributors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * may be used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * $FreeBSD$
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Partly from: src/sys/boot/i386/boot2/boot1.S 1.31
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Memory Locations */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set MEM_REL,0x700 # Relocation address
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set MEM_ARG,0x900 # Arguments
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set MEM_ORG,0x7c00 # Origin
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set MEM_BUF,0x8cec # Load area
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set MEM_BTX,0x9000 # BTX start
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set MEM_JMP,0x9010 # BTX entry point
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set MEM_USR,0xa000 # Client start
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set BDA_BOOT,0x472 # Boot howto flag
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Misc. Constants */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set SIZ_PAG,0x1000 # Page size
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set SIZ_SEC,0x200 # Sector size
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set COPY_BLKS,0x4 # Number of blocks
199767f8919635c4928607450d9e0abb932109ceToomas Soome # to copy for boot2
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set COPY_BLK_SZ,0x8000 # Copy in 32k blocks; must be
199767f8919635c4928607450d9e0abb932109ceToomas Soome # a multiple of 16 bytes
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .globl start
199767f8919635c4928607450d9e0abb932109ceToomas Soome .code16
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copy BTX and boot2 to the right locations and start it all up.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Setup the segment registers to flat addressing (segment 0) and setup the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * stack to end just below the start of our code.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestart: xor %cx,%cx # Zero
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %cx,%es # Address
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %cx,%ds # data
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %cx,%ss # Set up
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov $start,%sp # stack
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * BTX is right after us at 'end'. We read the length of BTX out of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * its header to find boot2. We need to copy boot2 to MEM_USR and BTX
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to MEM_BTX. Since those might overlap, we have to copy boot2
199767f8919635c4928607450d9e0abb932109ceToomas Soome * backwards first and then copy BTX. We aren't sure exactly how long
199767f8919635c4928607450d9e0abb932109ceToomas Soome * boot2 is, but it's currently under 128kB so we'll copy 4 blocks of 32kB
199767f8919635c4928607450d9e0abb932109ceToomas Soome * each; this can be adjusted via COPY_BLK and COPY_BLK_SZ above.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov $end,%bx # BTX
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov 0xa(%bx),%si # Get BTX length and set
199767f8919635c4928607450d9e0abb932109ceToomas Soome add %bx,%si # %si to start of boot2
199767f8919635c4928607450d9e0abb932109ceToomas Soome dec %si # Set %ds:%si to point at the
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %si,%ax # last byte we want to copy
199767f8919635c4928607450d9e0abb932109ceToomas Soome shr $4,%ax # from boot2, with %si made as
199767f8919635c4928607450d9e0abb932109ceToomas Soome add $(COPY_BLKS*COPY_BLK_SZ/16),%ax # small as possible.
199767f8919635c4928607450d9e0abb932109ceToomas Soome and $0xf,%si #
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %ax,%ds #
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov $MEM_USR/16,%ax # Set %es:(-1) to point at
199767f8919635c4928607450d9e0abb932109ceToomas Soome add $(COPY_BLKS*COPY_BLK_SZ/16),%ax # the last byte we
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %ax,%es # want to copy boot2 into.
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov $COPY_BLKS,%bx # Copy COPY_BLKS 32k blocks
199767f8919635c4928607450d9e0abb932109ceToomas Soomecopyloop:
199767f8919635c4928607450d9e0abb932109ceToomas Soome add $COPY_BLK_SZ,%si # Adjust %ds:%si to point at
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %ds,%ax # the end of the next 32k to
199767f8919635c4928607450d9e0abb932109ceToomas Soome sub $COPY_BLK_SZ/16,%ax # copy from boot2
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %ax,%ds
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov $COPY_BLK_SZ-1,%di # Adjust %es:%di to point at
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %es,%ax # the end of the next 32k into
199767f8919635c4928607450d9e0abb932109ceToomas Soome sub $COPY_BLK_SZ/16,%ax # which we want boot2 copied
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %ax,%es
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov $COPY_BLK_SZ,%cx # Copy 32k
199767f8919635c4928607450d9e0abb932109ceToomas Soome std
199767f8919635c4928607450d9e0abb932109ceToomas Soome rep movsb
199767f8919635c4928607450d9e0abb932109ceToomas Soome dec %bx
199767f8919635c4928607450d9e0abb932109ceToomas Soome jnz copyloop
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %cx,%ds # Reset %ds and %es
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %cx,%es
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov $end,%bx # BTX
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov 0xa(%bx),%cx # Get BTX length and set
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov %bx,%si # %si to end of BTX
199767f8919635c4928607450d9e0abb932109ceToomas Soome mov $MEM_BTX,%di # %di -> end of BTX at
199767f8919635c4928607450d9e0abb932109ceToomas Soome add %cx,%si # MEM_BTX
199767f8919635c4928607450d9e0abb932109ceToomas Soome add %cx,%di
199767f8919635c4928607450d9e0abb932109ceToomas Soome dec %si
199767f8919635c4928607450d9e0abb932109ceToomas Soome dec %di
199767f8919635c4928607450d9e0abb932109ceToomas Soome rep movsb # Move BTX
199767f8919635c4928607450d9e0abb932109ceToomas Soome cld # String ops inc
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Enable A20 so we can access memory above 1 meg.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Use the zero-valued %cx as a timeout for embedded hardware which do not
199767f8919635c4928607450d9e0abb932109ceToomas Soome * have a keyboard controller.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeseta20: cli # Disable interrupts
199767f8919635c4928607450d9e0abb932109ceToomas Soomeseta20.1: dec %cx # Timeout?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jz seta20.3 # Yes
199767f8919635c4928607450d9e0abb932109ceToomas Soome inb $0x64,%al # Get status
199767f8919635c4928607450d9e0abb932109ceToomas Soome testb $0x2,%al # Busy?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jnz seta20.1 # Yes
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0xd1,%al # Command: Write
199767f8919635c4928607450d9e0abb932109ceToomas Soome outb %al,$0x64 # output port
199767f8919635c4928607450d9e0abb932109ceToomas Soomeseta20.2: inb $0x64,%al # Get status
199767f8919635c4928607450d9e0abb932109ceToomas Soome testb $0x2,%al # Busy?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jnz seta20.2 # Yes
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0xdf,%al # Enable
199767f8919635c4928607450d9e0abb932109ceToomas Soome outb %al,$0x60 # A20
199767f8919635c4928607450d9e0abb932109ceToomas Soomeseta20.3: sti # Enable interrupts
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Save drive number from BIOS so boot2 can see it and start BTX.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb %dl,MEM_ARG
199767f8919635c4928607450d9e0abb932109ceToomas Soome jmp MEM_JMP # Start BTX
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * make sure the end is aligned for concatenated next module
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .align 4
199767f8919635c4928607450d9e0abb932109ceToomas Soomeend: