/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

/* "%W% (Berkeley) %G%" */

#define PARTITION		/* Partition is in bits 12 to 15 of R5

/*
 * RP??/RM?? 1st level boot program: loads next 7.5Kbytes from
 * boot sector of file system and sets it up to run.
 * Reads from the controller and drive passed in from the boot
 * rom.
 *   R1:  address of the boot devices's adapter
 *   R2:  controller number of the boot device
 *   R3:  unit number of the boot device
 *   R5:  software boot control flags
 *   R6:  address of driver subroutine from ROM
 *   SP:  base address of usable memory + 0x200
 */
	.set	BOOTLAST,15		/* last boot sector */
	.set	RPBPSECT,512		/* bytes per sector */
	.set	RELOC,0x70000

init:
	.word	0			/* entry mask for DEC monitor */
	nop;nop;nop;nop;nop;nop;nop;nop	/* some no-ops for 750 boot to skip */
	nop;nop;
start:
	clrl	r10			/* major("/dev/hp0a") */
	extzv	$13,$2,r1,r4		/* get MBA number from R1 */
	insv	r4,$24,$8,r10		/* set MBA number */
	insv	r3,$16,$8,r10		/* drive number */
#ifdef PARTITION
	extzv	$12,$4,r5,r4		/* get partition from r5 */
	bicw2	$0xf000,r5		/* remove from r5 */
	insv	r4,$8,$4,r10		/* set partition */
#endif
	movl	r5,r11			/* Save software boot control flags */
	movl	$RELOC,sp
					/* Save register during move */
	movl	r1,r7			/* MBA adapter address */
	movl	r2,r8			/* controller number */
	movl	r3,r9			/* unit number */
	moval	init,r4
	movc3	$end,(r4),(sp)
					/* Restore registers */
	movl	r7,r1			/* MBA adapter address */
	movl	r8,r2			/* controller number */
	movl	r9,r3			/* unit number */
	jmp	*$RELOC+start2
/* now running relocated */
/* bring in the boot program */
start2:					/* running relocated */
	pushr	$0xffff			/* BEGIN FIREWALL */
	movl	$1,r4			/* first boot sector */
	clrl	r5			/* transfer address */
	clrl	-(sp)			/* transfer address */
1:
	movl	r4,r8			/* requested sector # */
	jsb	(r6)			/* call ROM-based driver */
	blbs	r0,2f
	halt				/* read error */
2:
	addl2	$RPBPSECT,r5		/* bump address */
	movl	r5,(sp)
	aobleq	$BOOTLAST,r4,1b

	.set	PROGSIZE,(BOOTLAST*RPBPSECT)
done:
	tstl	(sp)+			/* pop address */
	popr	$0xffff			/* END FIREWALL */

/* clear core and execute program */
	movl	$PROGSIZE,r4
clrcor:
	clrq	(r4)
	acbl	$RELOC,$8,r4,clrcor
/* run loaded program */
	calls	$0,*$0
	brw	start2

end:
