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