1/*- 2 * Copyright (c) 2007 Yahoo!, Inc. 3 * All rights reserved. 4 * Written by: John Baldwin <jhb@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the author nor the names of any co-contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * Partly from: src/sys/boot/i386/boot2/boot1.S 1.31 31 */ 32 33/* Memory Locations */ 34 .set MEM_REL,0x700 # Relocation address 35 .set MEM_ARG,0x900 # Arguments 36 .set MEM_ORG,0x7c00 # Origin 37 .set MEM_BUF,0x8cec # Load area 38 .set MEM_BTX,0x9000 # BTX start 39 .set MEM_JMP,0x9010 # BTX entry point 40 .set MEM_USR,0xa000 # Client start 41 .set BDA_BOOT,0x472 # Boot howto flag 42 43/* Misc. Constants */ 44 .set SIZ_PAG,0x1000 # Page size 45 .set SIZ_SEC,0x200 # Sector size 46 .set COPY_BLKS,0x8 # Number of blocks 47 # to copy for boot2 (<= 15) 48 .set COPY_BLK_SZ,0x8000 # Copy in 32k blocks; must be 49 # a multiple of 16 bytes 50 51 .globl start 52 .code16 53 54/* 55 * Copy BTX and boot2 to the right locations and start it all up. 56 */ 57 58/* 59 * Setup the segment registers to flat addressing (segment 0) and setup the 60 * stack to end just below the start of our code. 61 */ 62start: xor %cx,%cx # Zero 63 mov %cx,%es # Address 64 mov %cx,%ds # data 65 mov %cx,%ss # Set up 66 mov $start,%sp # stack 67 68/* 69 * BTX is right after us at 'end'. We read the length of BTX out of 70 * its header to find boot2. We need to copy boot2 to MEM_USR and BTX 71 * to MEM_BTX. Since those might overlap, we have to copy boot2 72 * backwards first and then copy BTX. We aren't sure exactly how long 73 * boot2 is, but it's currently under 256kB so we'll copy 8 blocks of 32kB 74 * each; this can be adjusted via COPY_BLK and COPY_BLK_SZ above. 75 */ 76 mov $end,%bx # BTX 77 mov 0xa(%bx),%si # Get BTX length and set 78 add %bx,%si # %si to start of boot2 79 dec %si # Set %ds:%si to point at the 80 mov %si,%ax # last byte we want to copy 81 shr $4,%ax # from boot2, with %si made as 82 add $(COPY_BLKS*COPY_BLK_SZ/16),%ax # small as possible. 83 and $0xf,%si # 84 mov %ax,%ds # 85 mov $MEM_USR/16,%ax # Set %es:(-1) to point at 86 add $(COPY_BLKS*COPY_BLK_SZ/16),%ax # the last byte we 87 mov %ax,%es # want to copy boot2 into. 88 mov $COPY_BLKS,%bx # Copy COPY_BLKS 32k blocks 89copyloop: 90 add $COPY_BLK_SZ,%si # Adjust %ds:%si to point at 91 mov %ds,%ax # the end of the next 32k to 92 sub $COPY_BLK_SZ/16,%ax # copy from boot2 93 mov %ax,%ds 94 mov $COPY_BLK_SZ-1,%di # Adjust %es:%di to point at 95 mov %es,%ax # the end of the next 32k into 96 sub $COPY_BLK_SZ/16,%ax # which we want boot2 copied 97 mov %ax,%es 98 mov $COPY_BLK_SZ,%cx # Copy 32k 99 std 100 rep movsb 101 dec %bx 102 jnz copyloop 103 mov %cx,%ds # Reset %ds and %es 104 mov %cx,%es 105 mov $end,%bx # BTX 106 mov 0xa(%bx),%cx # Get BTX length and set 107 mov %bx,%si # %si to end of BTX 108 mov $MEM_BTX,%di # %di -> end of BTX at 109 add %cx,%si # MEM_BTX 110 add %cx,%di 111 dec %si 112 dec %di 113 rep movsb # Move BTX 114 cld # String ops inc 115/* 116 * Enable A20 so we can access memory above 1 meg. 117 * Use the zero-valued %cx as a timeout for embedded hardware which do not 118 * have a keyboard controller. 119 */ 120seta20: cli # Disable interrupts 121seta20.1: dec %cx # Timeout? 122 jz seta20.3 # Yes 123 inb $0x64,%al # Get status 124 testb $0x2,%al # Busy? 125 jnz seta20.1 # Yes 126 movb $0xd1,%al # Command: Write 127 outb %al,$0x64 # output port 128seta20.2: inb $0x64,%al # Get status 129 testb $0x2,%al # Busy? 130 jnz seta20.2 # Yes 131 movb $0xdf,%al # Enable 132 outb %al,$0x60 # A20 133seta20.3: sti # Enable interrupts 134 135/* 136 * Save drive number from BIOS so boot2 can see it and start BTX. 137 */ 138 movb %dl,MEM_ARG 139 jmp start+MEM_JMP-MEM_ORG # Start BTX 140end: 141