bbstart.s revision 1.12.20.1       1  1.12.20.1  uebayasi /* $NetBSD: bbstart.s,v 1.12.20.1 2010/08/17 06:43:58 uebayasi Exp $ */
      2        1.4        is 
      3        1.4        is /*-
      4        1.5        is  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5        1.4        is  * All rights reserved.
      6        1.1        is  *
      7        1.4        is  * This code is derived from software contributed to The NetBSD Foundation
      8        1.4        is  * by Ignatios Souvatzis.
      9        1.1        is  *
     10        1.1        is  * Redistribution and use in source and binary forms, with or without
     11        1.1        is  * modification, are permitted provided that the following conditions
     12        1.1        is  * are met:
     13        1.1        is  * 1. Redistributions of source code must retain the above copyright
     14        1.1        is  *    notice, this list of conditions and the following disclaimer.
     15        1.1        is  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1        is  *    notice, this list of conditions and the following disclaimer in the
     17        1.1        is  *    documentation and/or other materials provided with the distribution.
     18        1.1        is  *
     19        1.4        is  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.4        is  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.4        is  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.4        is  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.4        is  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.4        is  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.4        is  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.4        is  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.4        is  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.4        is  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.4        is  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1        is  */
     31        1.1        is 
     32        1.6        is #include <machine/asm.h>
     33  1.12.20.1  uebayasi #include "elf2bb.h"
     34        1.1        is 
     35        1.1        is #define LVOAllocMem	-0x0c6
     36        1.1        is #define LVODoIO		-0x1c8
     37        1.1        is #define LVOCacheClearU	-0x27c
     38        1.1        is 
     39        1.1        is #define IOcmd	28
     40        1.1        is #define IOerr	31
     41        1.1        is #define IOlen	36
     42        1.1        is #define IObuf	40
     43        1.1        is #define IOoff	44
     44        1.1        is 
     45        1.1        is #define Cmd_Rd	2
     46        1.1        is 
     47        1.1        is 	.text
     48       1.10    mhitch #if defined(_PRIMARY_BOOT) || defined(AUTOLOAD)
     49        1.1        is Lzero:	.asciz "DOS"			| "DOS type"
     50       1.10    mhitch #else
     51       1.10    mhitch Lzero:	.ascii	"BOOT"			| Secondary Boot
     52       1.10    mhitch #endif
     53        1.1        is 	/*
     54  1.12.20.1  uebayasi 	 * We put the relocator version here, for elf2bb, which replaces
     55        1.1        is 	 * it with the bootblock checksum.
     56        1.1        is 	 */
     57        1.1        is Chksum:	.long RELVER_RELATIVE_BYTES_FORWARD
     58        1.1        is Filesz:	.long 8192			| dummy
     59        1.1        is 
     60       1.11   aymeric /*
     61        1.1        is  * Entry point from Kickstart.
     62        1.1        is  * A1 points to an IOrequest, A6 points to ExecBase, we have a stack.
     63        1.1        is  * _must_ be at offset 12.
     64        1.1        is  */
     65        1.6        is ENTRY_NOPROFILE(start)
     66        1.1        is #ifdef AUTOLOAD
     67        1.1        is 	jra	Lautoload
     68        1.1        is #else
     69        1.1        is 	jra	Lrelocate
     70        1.1        is #endif
     71        1.1        is 
     72        1.1        is Lreltab:
     73  1.12.20.1  uebayasi 	.word 0			| elf2bb puts the reloc table address here
     74        1.9        is 
     75        1.9        is 	.globl _C_LABEL(default_command)
     76        1.9        is _C_LABEL(default_command):
     77        1.9        is 	.asciz	"netbsd -ASn2"
     78        1.9        is 	.org	(_C_LABEL(default_command)+32)
     79        1.1        is 
     80        1.1        is #ifdef AUTOLOAD
     81        1.1        is /*
     82        1.1        is  * autoload
     83        1.1        is  */
     84        1.1        is Lautoload:
     85        1.8    mhitch 	movl	%a6,%sp@-			|SysBase
     86        1.8    mhitch 	movl	%a1,%sp@-			|IORequest
     87        1.1        is 
     88        1.7        is 	movl	#AUTOLOAD,%d0		|Howmuch
     89        1.8    mhitch 	movl	%d0,%a1@(IOlen)		| for the actual read...
     90        1.7        is 	movl	#0x10001,%d1		|MEMF_CLEAR|MEMF_PUBLIC
     91        1.7        is 	jsr	%a6@(LVOAllocMem)
     92        1.7        is 	movl	%sp@+,%a1			|IORequest
     93        1.7        is 	movl	%sp@+,%a6			|SysBase
     94        1.8    mhitch 	orl	%d0,%d0
     95        1.1        is 	jne	Lgotmem
     96        1.7        is 	movql	#1,%d0
     97        1.1        is 	rts
     98        1.1        is 
     99        1.1        is Lgotmem:
    100        1.8    mhitch 	movl	%d0,%sp@-			|Address
    101        1.7        is 	movl	%a1@(IOoff),%sp@-		|Old offset
    102        1.8    mhitch 	movl	%a1,%sp@-
    103        1.8    mhitch 	movl	%a6,%sp@-
    104        1.1        is 
    105        1.1        is /* we've set IOlen above */
    106        1.8    mhitch 	movl	%d0,%a1@(IObuf)
    107        1.7        is 	movw	#Cmd_Rd,%a1@(IOcmd)
    108        1.7        is 	jsr	%a6@(LVODoIO)
    109        1.7        is 
    110        1.7        is 	movl	%sp@+,%a6
    111        1.7        is 	movl	%sp@+,%a1
    112        1.7        is 	movl	%sp@+,%a1@(IOoff)
    113        1.1        is 
    114        1.7        is 	tstb	%a1@(IOerr)
    115        1.1        is 	jne	Lioerr
    116        1.7        is 	addl	#Lrelocate-Lzero,%sp@
    117        1.1        is 
    118        1.8    mhitch 	movl	%a6,%sp@-
    119        1.7        is 	jsr	%a6@(LVOCacheClearU)
    120        1.7        is 	movl	%sp@+,%a6
    121        1.1        is 	rts
    122        1.1        is Lioerr:
    123        1.7        is 	movql	#1,%d0
    124        1.7        is 	addql	#4,%sp
    125        1.1        is 	rts
    126        1.1        is #endif
    127        1.1        is 
    128        1.1        is /*
    129        1.1        is  * Relocate ourselves, at the same time clearing the relocation table
    130        1.1        is  * (in case it overlaps with BSS).
    131        1.1        is  *
    132        1.1        is  * Register usage:
    133        1.1        is  * A2: points into the reloc table, located at our end.
    134        1.1        is  * A0: pointer to the longword to relocate.
    135        1.1        is  * D0: word offset of longword to relocate
    136        1.1        is  * D1: points to our start.
    137        1.1        is  *
    138        1.1        is  * Table has relative byte offsets, if a byte offset is zero, it is
    139        1.1        is  * followed by an absolute word offset. If this is zero, too, table
    140        1.1        is  * end is reached.
    141        1.1        is  */
    142       1.11   aymeric 
    143        1.1        is Lrelocate:
    144        1.7        is 	lea	%pc@(Lzero),%a0
    145        1.8    mhitch 	movl	%a0,%d1
    146        1.7        is 	movw	%pc@(Lreltab),%a2
    147        1.8    mhitch 	addl	%d1,%a2
    148        1.1        is 	jra	Loopend
    149        1.1        is 
    150        1.1        is Loopw:
    151        1.7        is 	clrw	%a2@+
    152        1.8    mhitch 	movl	%d1,%a0	| for a variant with relative words, erase this line
    153        1.1        is Loopb:
    154        1.8    mhitch 	addl	%d0,%a0
    155        1.8    mhitch 	addl	%d1,%a0@
    156        1.1        is Loopend:
    157        1.7        is 	movq	#0,%d0
    158        1.7        is 	movb	%a2@,%d0
    159        1.7        is 	clrb	%a2@+	| bfclr %a2@+{0:8} is still two shorts
    160        1.7        is 	tstb	%d0	| we could save one short by using casb %d0,d0,%a2@+
    161        1.1        is 	jne	Loopb
    162        1.1        is 
    163        1.7        is 	movw	%a2@,%d0
    164        1.1        is 	jne	Loopw
    165        1.1        is 
    166        1.1        is Lendtab:
    167        1.8    mhitch 	movl	%a6,%sp@-
    168        1.7        is 	jsr	%a6@(LVOCacheClearU)
    169        1.7        is 	movl	%sp@+,%a6
    170        1.1        is 
    171        1.1        is /* We are relocated. Now it is safe to initialize _SysBase: */
    172        1.1        is 
    173        1.8    mhitch 	movl	%a6,_C_LABEL(SysBase)
    174        1.1        is 
    175       1.10    mhitch #ifndef _PRIMARY_BOOT
    176       1.10    mhitch 	movl	%a5,%sp@-		| Console info
    177       1.10    mhitch #endif
    178        1.8    mhitch 	movl	%a1,%sp@-
    179        1.6        is 	bsr	_C_LABEL(pain)
    180       1.10    mhitch #ifdef _PRIMARY_BOOT
    181       1.10    mhitch 	addql	#4,%sp
    182       1.10    mhitch #else
    183       1.10    mhitch 	addql	#8,%sp
    184       1.10    mhitch #endif
    185        1.1        is 
    186        1.1        is Lerr:
    187        1.7        is 	movq	#1,%d0
    188        1.1        is 	rts
    189        1.1        is 
    190        1.6        is 	.comm _C_LABEL(SysBase),4
    191