/* First stage loader bootstrap for SUNW,Ultra's IEEE 1275 PROM
   
   Copyright (C) 1996 Jakub Jelinek
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#define BLOCK_START	0x1dc
#define promvec		%l5
#define buffer		%l0
#define fd		%l6
#define stdouth		%l7
#define dest		%l4
#define PUTCHAR		jmpl	buffer + (putchar - 0x4000), %i7
#define PROM11		jmpl	buffer + (prom11 - 0x4000), %i7
#define GETPROP		jmpl	buffer + (getprop - 0x4000), %i7
#define	PROM31		jmpl	buffer + (prom31 - 0x4000), %i7
#define RWPROM31	jmpl	buffer + (rwprom31 - 0x4000), %i7

	.text
	.align 4
	.global putchar, bootmain, _start, digit_here, letter_here

_start:
bootmain:
	mov	%o4, promvec
	sethi	%hi(0x4000), buffer
	sethi	%hi(0x10000), dest

	add	buffer, %lo(chosen), %o1
	PROM11
	 add	buffer, %lo(finddevice), %o0
	ldx	[dest + 32], fd
	GETPROP
	 add	buffer, %lo(stdout), %o0
	lduw	[dest + 256], stdouth
	PUTCHAR
	 mov 	'S', %o0
	GETPROP
	 add	buffer, %lo(bootpath), %o0
	add	dest, 256, %o0
	mov	':', %o4
2:
	ldub	[%o0], %o3
	tst	%o3
	be	3f
	 cmp	%o3, %o4

	bne,a	2b
	 inc	%o0
3:
	stb	%o4, [%o0]
letter_here:
	mov	0xDD, %o4
	stb	%o4, [%o0 + 1]
	stb	%g0, [%o0 + 2]
	add	dest, 256, %o1
	PROM11
	 add	buffer, %lo(open), %o0

	ldx	[dest + 32], fd
	or	buffer, BLOCK_START, %l1
	mov	%l1, %l2
5:
	lduw	[%l2], %l3
	add	%l2, 4, %l2
	tst	%l3
	be	7f
	 sll	%l3, 9, %o3
	srl	%l3, 23, %o2
	RWPROM31
	 add	buffer, %lo(seek), %o0
	lduw	[dest + 48], %o0
	cmp	%o0, -1
	be	9f
	 mov	512, %o3
	mov	%l1, %o2
	RWPROM31
	 add	buffer, %lo(read), %o0
	lduw	[dest + 48], %o0
	cmp	%o0, 512
0:
	bne	9f
	 cmp	%l1, %i5

	bne	5b
	 add	%l1, 512, %l1

	PUTCHAR
	 mov	'I', %o0
	b	5b
	 add	dest, 1024, %l1
7:
	mov	fd, %o1
	PROM11
	 add	buffer, %lo(close), %o0
	ldub	[dest + 1024 + 0x28], %o0
	cmp	%o0, 'L'
	bne	9f
	 nop

	PUTCHAR
	 nop

	jmpl	dest + 1024, %g0
	 mov	promvec, %o4
9:
	PROM11
	 add	buffer, %lo(exit), %o0

putchar:
	stb 	%o0, [buffer]
	mov	stdouth, %o1
	mov	buffer, %o2
	mov	1, %o3
	b	prom31
	 add	buffer, %lo(write), %o0

getprop:
	stx	%o0, [dest + 32]
	add	dest, 256, %o0
	stx	%o0, [dest + 40]
	mov	1024, %o0
	stx	%o0, [dest + 48]
	mov	fd, %o1
	add	buffer, %lo(getprops), %o0
	mov	4, %o2
	stx	%o2, [dest + 8]
	b	1f
prom11:
	mov	1, %o2
	stx	%o2, [dest + 8]
1:
	stx	%o0, [dest]
	stx	%o2, [dest + 16]
	stx	%o1, [dest + 24]
	mov	dest, %o6
	call	promvec
	 mov	%i7, %o7

rwprom31:
	mov	fd, %o1
prom31:
	mov	3, %o4
	stx	%o4, [dest + 8]
	stx	%o2, [dest + 32]
	stx	%o3, [dest + 40]
	b	1b
	 mov	1, %o2

finddevice:
	.asciz	"finddevice"
getprops:
	.asciz	"getprop"
bootpath:
	.asciz	"bootpath"
stdout:
	.asciz	"stdout"
open:
	.asciz	"open"
close:
	.asciz	"close"
seek:
	.asciz	"seek"
read:
	.asciz	"read"
write:
	.asciz	"write"
chosen:
	.asciz	"/chosen"
exit:
	.asciz	"exit"
