# $Id: Makefile,v 1.4 2002/03/02 23:11:46 tmm Exp $

# Location of the tree to be burned onto CD-ROM
ISOTREE?=	/home/tmm/cdfs/isotree
# Location of the src/sys tree to be used for building the boot block
SYSTREE?=	/mnt/tmm/sparc64/sys
BOOT1DIR=	${SYSTREE}/boot/sparc64/bootblock
BOOT1=		boot1
LOADERDIR=	${SYSTREE}/boot/sparc64/loader
LOADER=		loader
# Size of the boot file system in 512-byte blocks, including a disk label
BOOTFSSZ?=	4096
MOUNTPT=	fsmnt

ALLTARGETS=	boot1 loader boot.sun4u bootslice.sun4u sparc64.iso

all:	sparc64.iso

boot1:
	cd ${BOOT1DIR} && ${MAKE} all
	cp ${BOOT1DIR}/${BOOT1} $@

# XXX: non-standard options, dependency is bogus, so always do a full rebuild
# for now.
loader:
	cd ${LOADERDIR} && \
	${MAKE} clean all LOADER_DISK_SUPPORT=yes LOADER_NET_SUPPORT=no \
	    LOADER_NFS_SUPPORT=no LOADER_TFTP_SUPPORT=no \
	    LOADER_CD9660_SUPPORT=yes
	cp ${LOADERDIR}/${LOADER} $@

# Build the boot file system. This needs to be done natively to get the correct
# FS endianess!
bootfs.sun4u:	loader
	dd if=/dev/zero of=$@ bs=512 count=${BOOTFSSZ}
	-mkdir ${MOUNTPT}
	MD=$$(mdconfig -a -t vnode -f $@) ; \
	DEV=/dev/$${MD}c ; \
	disklabel -w -r $$MD auto ; \
	newfs $$DEV ; \
	mount $$DEV ${MOUNTPT} ; \
	mkdir ${MOUNTPT}/boot ; \
	cp loader ${MOUNTPT}/boot/ ; \
	umount ${MOUNTPT} ; \
	mdconfig -d -u $${MD#md}

# The UFS slice on the CD-ROM is prepended by a pseudo disk label. The first
# (512-byte) block is zeroed out on Sun disks, so we do so, too.
# The next 15 sectors contain the boot code.
# Since a FreeBSD disk label is also 16 sectors in size, we can just overwrite
# it with with our boot1, which has the disk label portions zeroed out and is of
# the size of a label, leaving the file system that follows it intact.
bootslice.sun4u:	bootfs.sun4u boot1
	cp  bootfs.sun4u $@
	dd if=boot1 of=$@ bs=512 conv=notrunc

# Create the ISO image, including the boot slice generated above.
# To make sure that the kernel will use the cd9660 file system as root fs
# there should be a /boot/loader.conf with boot_cdrom set to "YES".
sparc64.iso: bootslice.sun4u
	mkisofs -B ,,,,bootslice.sun4u -r -o $@ ${ISOTREE}

clean:
	-rm ${ALLTARGETS}
