1*58a2b000SEvgeniy Ivanov/* $NetBSD: biosboot.S,v 1.8 2011/01/05 23:13:01 jakllsch Exp $ */ 2*58a2b000SEvgeniy Ivanov 3*58a2b000SEvgeniy Ivanov/*- 4*58a2b000SEvgeniy Ivanov * Copyright (c) 2003 The NetBSD Foundation, Inc. 5*58a2b000SEvgeniy Ivanov * All rights reserved. 6*58a2b000SEvgeniy Ivanov * 7*58a2b000SEvgeniy Ivanov * This code is derived from software contributed to The NetBSD Foundation 8*58a2b000SEvgeniy Ivanov * by David Laight. 9*58a2b000SEvgeniy Ivanov * 10*58a2b000SEvgeniy Ivanov * Redistribution and use in source and binary forms, with or without 11*58a2b000SEvgeniy Ivanov * modification, are permitted provided that the following conditions 12*58a2b000SEvgeniy Ivanov * are met: 13*58a2b000SEvgeniy Ivanov * 1. Redistributions of source code must retain the above copyright 14*58a2b000SEvgeniy Ivanov * notice, this list of conditions and the following disclaimer. 15*58a2b000SEvgeniy Ivanov * 2. Redistributions in binary form must reproduce the above copyright 16*58a2b000SEvgeniy Ivanov * notice, this list of conditions and the following disclaimer in the 17*58a2b000SEvgeniy Ivanov * documentation and/or other materials provided with the distribution. 18*58a2b000SEvgeniy Ivanov * 19*58a2b000SEvgeniy Ivanov * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20*58a2b000SEvgeniy Ivanov * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21*58a2b000SEvgeniy Ivanov * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22*58a2b000SEvgeniy Ivanov * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23*58a2b000SEvgeniy Ivanov * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24*58a2b000SEvgeniy Ivanov * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25*58a2b000SEvgeniy Ivanov * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26*58a2b000SEvgeniy Ivanov * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27*58a2b000SEvgeniy Ivanov * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28*58a2b000SEvgeniy Ivanov * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29*58a2b000SEvgeniy Ivanov * POSSIBILITY OF SUCH DAMAGE. 30*58a2b000SEvgeniy Ivanov */ 31*58a2b000SEvgeniy Ivanov 32*58a2b000SEvgeniy Ivanov#include <machine/asm.h> 33*58a2b000SEvgeniy Ivanov#include <sys/bootblock.h> 34*58a2b000SEvgeniy Ivanov 35*58a2b000SEvgeniy Ivanov/* 36*58a2b000SEvgeniy Ivanov * Code linked to 0x1000:0 and (usually) read from /boot by bootxx code 37*58a2b000SEvgeniy Ivanov * 38*58a2b000SEvgeniy Ivanov * On entry: 39*58a2b000SEvgeniy Ivanov * %dl BIOS drive number 40*58a2b000SEvgeniy Ivanov * %ecx:%ebx Sector number of NetBSD partition 41*58a2b000SEvgeniy Ivanov * %ds:%si Boot parameter block (patched by installboot) 42*58a2b000SEvgeniy Ivanov * %cs 0x1000 43*58a2b000SEvgeniy Ivanov * %ds, %es, %ss All zero 44*58a2b000SEvgeniy Ivanov * %sp near 0xfffc 45*58a2b000SEvgeniy Ivanov */ 46*58a2b000SEvgeniy Ivanov 47*58a2b000SEvgeniy Ivanov .text 48*58a2b000SEvgeniy Ivanov .code16 49*58a2b000SEvgeniy IvanovENTRY(boot_start) 50*58a2b000SEvgeniy Ivanov jmp boot_start_1 51*58a2b000SEvgeniy Ivanov .balign 4 52*58a2b000SEvgeniy IvanovENTRY(boot_magic) 53*58a2b000SEvgeniy Ivanov .long X86_BOOT_MAGIC_2 /* checked for by bootxx code */ 54*58a2b000SEvgeniy IvanovENTRY(boot_params) 55*58a2b000SEvgeniy Ivanov .long boot_start_1 - boot_params 56*58a2b000SEvgeniy Ivanov#include <boot_params.S> 57*58a2b000SEvgeniy Ivanov . = boot_start + 0x80 /* space for patchable variables */ 58*58a2b000SEvgeniy Ivanovboot_start_1: 59*58a2b000SEvgeniy Ivanov 60*58a2b000SEvgeniy Ivanov#if 0 61*58a2b000SEvgeniy Ivanov /* Allow for boot_start not being %cs:0 */ 62*58a2b000SEvgeniy Ivanov call 2f 63*58a2b000SEvgeniy Ivanov2: pop %cx 64*58a2b000SEvgeniy Ivanov sub $2b, %cx /* %ax is offset */ 65*58a2b000SEvgeniy Ivanov test $0xf, %cx /* check code seg aligned */ 66*58a2b000SEvgeniy Ivanov jz 3f 67*58a2b000SEvgeniy Ivanov lret /* not playing if not */ 68*58a2b000SEvgeniy Ivanov3: mov %cs, %ax 69*58a2b000SEvgeniy Ivanov shr $4, %cx 70*58a2b000SEvgeniy Ivanov add %cx, %ax /* segment staring at boot_start */ 71*58a2b000SEvgeniy Ivanov push %ax 72*58a2b000SEvgeniy Ivanov push $4f 73*58a2b000SEvgeniy Ivanov lret 74*58a2b000SEvgeniy Ivanov4: 75*58a2b000SEvgeniy Ivanov#endif 76*58a2b000SEvgeniy Ivanov 77*58a2b000SEvgeniy Ivanov mov %cs, %ax 78*58a2b000SEvgeniy Ivanov mov %ax, %es 79*58a2b000SEvgeniy Ivanov 80*58a2b000SEvgeniy Ivanov movl %ecx, %ebp /* move LBA out of the way */ 81*58a2b000SEvgeniy Ivanov 82*58a2b000SEvgeniy Ivanov /* Grab boot_params patched into bootxx by installboot */ 83*58a2b000SEvgeniy Ivanov cmpl $X86_BOOT_MAGIC_1,-4(%si) /* sanity check ptr */ 84*58a2b000SEvgeniy Ivanov jne 2f 85*58a2b000SEvgeniy Ivanov mov $boot_params, %di 86*58a2b000SEvgeniy Ivanov movl (%si),%ecx 87*58a2b000SEvgeniy Ivanov cmp $boot_start_1 - boot_params, %cx 88*58a2b000SEvgeniy Ivanov jbe 1f 89*58a2b000SEvgeniy Ivanov mov $boot_start_1 - boot_params, %cx 90*58a2b000SEvgeniy Ivanov1: cld 91*58a2b000SEvgeniy Ivanov rep 92*58a2b000SEvgeniy Ivanov movsb 93*58a2b000SEvgeniy Ivanov2: 94*58a2b000SEvgeniy Ivanov 95*58a2b000SEvgeniy Ivanov mov %ax, %ds 96*58a2b000SEvgeniy Ivanov movl $_end, %eax /* top of bss */ 97*58a2b000SEvgeniy Ivanov shr $4, %eax /* as a segment */ 98*58a2b000SEvgeniy Ivanov add $0x1001, %ax /* and + 64k */ 99*58a2b000SEvgeniy Ivanov mov %ax, %ss /* for stack */ 100*58a2b000SEvgeniy Ivanov mov $0xfffc, %sp /* %sp at top of it */ 101*58a2b000SEvgeniy Ivanov 102*58a2b000SEvgeniy Ivanov call gdt_fixup 103*58a2b000SEvgeniy Ivanov 104*58a2b000SEvgeniy Ivanov calll real_to_prot 105*58a2b000SEvgeniy Ivanov .code32 106*58a2b000SEvgeniy Ivanov 107*58a2b000SEvgeniy Ivanov movl $_end, %ecx /* zero bss */ 108*58a2b000SEvgeniy Ivanov movl $__bss_start, %edi 109*58a2b000SEvgeniy Ivanov subl %edi, %ecx 110*58a2b000SEvgeniy Ivanov shr $2, %ecx /* _end and __bss_start are aligned */ 111*58a2b000SEvgeniy Ivanov xor %eax, %eax 112*58a2b000SEvgeniy Ivanov rep 113*58a2b000SEvgeniy Ivanov stosl 114*58a2b000SEvgeniy Ivanov 115*58a2b000SEvgeniy Ivanov testb $X86_BP_FLAGS_LBA64VALID, boot_params+4 116*58a2b000SEvgeniy Ivanov jnz 1f 117*58a2b000SEvgeniy Ivanov xorl %ebp, %ebp /* high part of LBA is not valid */ 118*58a2b000SEvgeniy Ivanov1: 119*58a2b000SEvgeniy Ivanov 120*58a2b000SEvgeniy Ivanov movzbl %dl, %edx 121*58a2b000SEvgeniy Ivanov push %ebp /* high 32 bits of first sector */ 122*58a2b000SEvgeniy Ivanov push %ebx /* first sector of bios partition */ 123*58a2b000SEvgeniy Ivanov push %edx /* bios disk */ 124*58a2b000SEvgeniy Ivanov call _C_LABEL(boot2) /* C bootstrap code */ 125*58a2b000SEvgeniy Ivanov addl $12, %esp 126*58a2b000SEvgeniy Ivanov call prot_to_real 127*58a2b000SEvgeniy Ivanov .code16 128*58a2b000SEvgeniy Ivanov 129*58a2b000SEvgeniy Ivanovboot_fail: 130*58a2b000SEvgeniy Ivanov push %ax 131*58a2b000SEvgeniy Ivanov movw $1f, %si 132*58a2b000SEvgeniy Ivanov call message 133*58a2b000SEvgeniy Ivanov pop %si 134*58a2b000SEvgeniy Ivanov call message 135*58a2b000SEvgeniy Ivanov jmp loopstop 136*58a2b000SEvgeniy Ivanov1: .asciz "Boot2 failed: " 137*58a2b000SEvgeniy Ivanov 138*58a2b000SEvgeniy IvanovENTRY(_rtt) 139*58a2b000SEvgeniy Ivanov .code32 140*58a2b000SEvgeniy Ivanov call prot_to_real 141*58a2b000SEvgeniy Ivanov .code16 142*58a2b000SEvgeniy Ivanovloopstop: 143*58a2b000SEvgeniy Ivanov movb $0x86, %ah /* delay for about a second */ 144*58a2b000SEvgeniy Ivanov movw $16, %cx 145*58a2b000SEvgeniy Ivanov int $0x15 146*58a2b000SEvgeniy Ivanov int $0x18 /* might be a boot fail entry */ 147*58a2b000SEvgeniy Ivanov1: sti 148*58a2b000SEvgeniy Ivanov hlt 149*58a2b000SEvgeniy Ivanov jmp 1b 150