/* Second stage boot loader bootstrap
   
   Copyright (C) 1996 Pete A. Zaitcev
		 1996 Miguel de Icaza
		 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.  */

	.text
	.global _start, start, silo_conf, silo_conf_part, boot_part
_start:
start:
	! Set up a stack
	sethi %hi(start),%l0
	or %l0,%lo(start),%l0
	save %l0,-120,%o6

	/* Move ourselves up - there are three chunks to move. */
	sethi %hi(main_text_start),%l4
	or %l4,%lo(main_text_start),%l4		! %l4 - end address
	sethi %hi(start),%l1
	or %l1,%lo(start),%l1			! %l1 - new base
	sub %l4,%l1,%l2				! %l2 - length
callsaved:
	call 1f					! Our PC may float on sun4 vs. sun4m
	sub %o7, (callsaved - start), %l0	! %l0 - old base
	.byte 'O'				! This is for v2 primary loader to check if we are ok... :)
silo_conf_part:
	.byte  0
boot_part:
	.byte 0
	.align 4
silo_conf:
	.skip  256
2:
	ld [%l0 + %l2], %l3			! !%l3
	st %l3, [%l1 + %l2]
1:
	subcc %l2,%g0,%g0
	bne 2b
	sub %l2,4,%l2

	mov %l1,%l5
	sub %l4,%l1,%l2
	add %l0,%l2,%l0
	sethi %hi(main_rodata_start),%l4
	or %l4,%lo(main_rodata_start),%l4		! %l4 - end address
	sethi %hi(main_text_end),%l1
	or %l1,%lo(main_text_end),%l1		! %l1 - new base
	b 1f
	sub %l4,%l1,%l2				! %l2 - length
2:
	ld [%l0 + %l2], %l3			! !%l3
	st %l3, [%l1 + %l2]
1:
	subcc %l2,%g0,%g0
	bne 2b
	sub %l2,4,%l2

	sub %l4,%l1,%l2
	add %l0,%l2,%l0
	sethi %hi(main_data_start),%l4
	or %l4,%lo(main_data_start),%l4		! %l4 - end address
	sethi %hi(main_rodata_end),%l1
	or %l1,%lo(main_rodata_end),%l1		! %l1 - new base
	b 1f
	sub %l4,%l1,%l2				! %l2 - length
2:
	ld [%l0 + %l2], %l3			! !%l3
	st %l3, [%l1 + %l2]
1:
	subcc %l2,%g0,%g0
	bne 2b
	sub %l2,4,%l2

	sub %l4,%l1,%l2
	add %l0,%l2,%l0
	sethi %hi(__bss_start),%l4
	or %l4,%lo(__bss_start),%l4		! %l4 - bss address
	sethi %hi(main_data_end),%l1
	or %l1,%lo(main_data_end),%l1		! %l1 - new base
	b 1f
	sub %l4,%l1,%l2				! %l2 - length

2:
	ld [%l0 + %l2], %l3			! !%l3
	st %l3, [%l1 + %l2]
1:
	subcc %l2,%g0,%g0
	bne 2b
	sub %l2,4,%l2

	/* Jump to relocated code */
	jmpl %l5 + (jumphere - start), %g0
	nop
jumphere:

	/* Clear BSS */
	sethi %hi(_end), %l1			! %l1
	or %l1, %lo(_end), %l1
	ba 4f
	sub %l1, %l4, %l3			! %l3 - length
5:
	st %g0, [%l4 + %l3]
4:
	subcc %l3,%g0,%g0
	bne 5b
	sub %l3,4,%l3

	! Call gunzipmain() and return to PROM when to boot is impossible.
	call gunzipmain
	or %i0, %g0, %o0			! Prom vector address

	orcc %o0, %o0, %g1
	bne 6f
	or %i0, %g0, %o0

	jmpl %i7 + 8, %g0
	restore

6:
	jmpl %g1 + %g0, %g0
	restore
