1/*	$NetBSD: fdboot.S,v 1.1.4.3 2004/09/21 13:14:19 skrll Exp $	*/
2
3/*
4 * Copyright (c) 1995 Waldi Ravens.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *        This product includes software developed by Waldi Ravens.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "xxboot.h"
34
35#ifdef __ELF__
36	.globl	_start, main, fill
37
38	.text
39
40_start:	bras	main
41#else
42	.globl	start, main, fill
43
44	.text
45
46start:	bras	main
47#endif
48/*
49 * Fake gemdos-fs bootsector, to keep TOS away.
50 */
51	.ascii	"NetBSD"	| oem
52	.byte	0,0,0		| serial
53	.word	0		| bps
54	.byte	0		| spc
55	.word	0		| res
56	.byte	0		| fat
57	.word	0		| ndirs
58	.word	0		| sec
59	.byte	0		| media
60	.word	0		| spf
61	.word	0		| spt
62	.word	0		| side
63	.word	0		| hid
64	.even
65/*
66 * ROM loader does not save any register!
67 */
68main:	movml	%d1-%d7/%a0-%a6,%sp@-
69
70	lea	%pc@(m_bot),%a3
71	movl	_membot:w,%d3
72	lea	MAXBOT,%a6
73	cmpl	%a6,%d3
74	bhis	exit			| membot > MAXBOT
75
76	lea	%pc@(m_top),%a3
77	movl	_memtop:w,%d3
78	cmpl	#MINTOP,%d3
79	blts	exit			| memtop < MINTOP
80
81	andw	#-4,%d3
82	movl	%d3,%a0
83	movl	%sp,%a0@-
84	movl	%a0,%sp			| set new stack
85/*
86 * Determine the number of sectors per cylinder.
87 */
88	movq	#0,%d3
890:	movl	%a6,%a3			| buffer
90	movq	#1,%d0			| count
91	movq	#0,%d1			| side
92	movl	%d1,%d2			| track
93	addw	%d0,%d3			| sector
94	bsr	rds0
95	beqs	0b
96	subqw	#1,%d3
97	addw	%d3,%d3
98	lea	%pc@(secpercyl),%a0
99	movw	%d3,%a0@
100/*
101 * Load secondary boot loader and disklabel.
102 */
103	movq	#NSEC,%d5		| # of sectors
104	movq	#1,%d4			| first sector
105	movl	%a6,%a3			| load address
106	bsr	rds1
107	lea	%pc@(m_rds),%a3
108	bnes	0f			| I/O error
109/*
110 * int bootxx(readsector, disklabel, autoboot)
111 */
112	clrl	%sp@-			| no autoboot
113	pea	%a6@(LBLST-MAXBOT)	| disklabel
114	pea	%pc@(rds2)		| readsector
115	jsr	%a6@(BXXST-MAXBOT)
116	lea	%pc@(m_sbl),%a3		| NetBSD not booted
117	lea	%sp@(12),%sp
1180:	movl	%sp@,%sp			| restore BIOS stack
119	movl	%d0,%d3
120
121exit:	bsrs	puts			| display error
122	lea	%pc@(m_key),%a3
123	bsrs	puts			| wait for key
1240:	movml	%sp@+,%d1-%d7/%a0-%a6
125	movq	#0,%d0
126	rts
127/*
128 * puts (in: a3, d3)
129 */
1300:	cmpw	#35,%d0		| '#'
131	bnes	1f
132	bsrs	puti
133	bras	puts
1341:	cmpw	#64,%d0		| '@'
135	bnes	2f
136	movw	#2,%sp@-
137	movw	#Bconin,%sp@-
138	trap	#BIOS
139	addql	#4,%sp
140	bras	puts
1412:	bsrs	putc
142puts:	movq	#0,%d0
143	movb	%a3@+,%d0
144	bnes	0b
145	rts
146
147puti:	swap	%d3
148	bsrs	0f
149	swap	%d3
1500:	rorw	#8,%d3
151	bsrs	1f
152	rorw	#8,%d3
1531:	rorb	#4,%d3
154	bsrs	2f
155	rorb	#4,%d3
1562:	movw	%d3,%d0
157	andw	#15,%d0
158	addw	#48,%d0
159	cmpw	#58,%d0
160	bcss	putc
161	addw	#39,%d0
162putc:	movw	%d0,%sp@-
163	movw	#2,%sp@-
164	movw	#Bconout,%sp@-
165	trap	#BIOS
166	addql	#6,%sp
167	rts
168/*
169 * int readsec (void *buffer, u_int offset, u_int count);
170 */
171rds2:	movml	%d2-%d5/%a2-%a3,%sp@-
172	movl	%sp@(28),%a3		| buffer
173	movl	%sp@(32),%d4		| offset
174	movl	%sp@(36),%d5		| count
175	bsrs	rds1
176	movml	%sp@+,%d2-%d5/%a2-%a3
177	rts
178
179rds1:	bsrs	1f
180	bnes	0f
181	tstl	%d5
182	bnes	rds1
1830:	rts
184
1851:	movq	#0,%d0
186	movw	%pc@(secpercyl),%d0
187	movl	%d4,%d3
188	divuw	%d0,%d3
189	movw	%d3,%d2		| track
190	clrw	%d3
191	swap	%d3
192	lsrw	#1,%d0
193	divuw	%d0,%d3
194	movw	%d3,%d1		| side
195	swap	%d3
196	subw	%d3,%d0
197	addqw	#1,%d3		| sector
198	cmpl	%d0,%d5
199	bccs	rds0
200	movw	%d5,%d0
201
202rds0:	movw	%d0,%sp@-		| count
203	movw	%d1,%sp@-		| side
204	movw	%d2,%sp@-		| track
205	movw	%d3,%sp@-		| sector
206	movw	_bootdev:w,%sp@-	| device
207	clrl	%sp@-		| filler
208	movl	%a3,%sp@-		| buffer
209	addl	%d0,%d4
210	subl	%d0,%d5
211	lsll	#8,%d0
212	addl	%d0,%d0
213	addl	%d0,%a3
214	movw	#Floprd,%sp@-
215	trap	#XBIOS
216	lea	%sp@(20),%sp
217	tstl	%d0
218	rts
219
220secpercyl:
221	.word	0
222
223m_bot:	.asciz	"fdboot: membot == 0x#\r\n"
224m_top:	.asciz	"fdboot: memtop == 0x#\r\n"
225m_sbl:	.asciz	"fdboot: bootxx => 0x#\r\n"
226m_rds:	.asciz	"fdboot: Floprd => 0x#\r\n"
227m_key:	.asciz	"\007\r\npress any key... @\r\n"
228
229fill:	.space	22		| 510-(fill-start)
230	.word	0		| checksum
231end:
232