# Copyright (c) 1992, 1993, 1994, 1995 Berkeley Software Design, Inc.
# All rights reserved.
# The Berkeley Software Design Inc. software License Agreement specifies
# the terms and conditions for redistribution.
#
#	BSDI $Id: Makefile,v 2.6 1995/12/12 19:35:06 donn Exp $
#
# from:
#	@(#)Makefile    8.1 (Berkeley) 6/11/93


DESTDIR=
STAND= ../../stand
INCPATH= -I../.. -I. -I..
VPATH=	${STAND}
STANDDIR= ${DESTDIR}/stand 
MDEC= ${DESTDIR}/usr/bootstraps

CPP=	cpp ${INCPATH} -DLOCORE -DSTANDALONE -DAT386

# LOWMEM
#	Memory from zero to LOWMEM is loaded indirectly by /boot. Any
#	Text segment data that would end up here is loaded in a buffer and
#	copied into low memory just before the new program is started. This
#	preserves both BIOS low memory and the code used to call the BIOS.
# FIRSTLD
#	This is were the BIOS loads block-0 (first stage) boot straps.
#	When using FDISK partitions, first-stage boot straps from the
#	partition are also loaded at this address.  This code also runs
#	at this address. There is a gdt in this code which must not be
#	overwritten while in use. This gdt is in use until /boot starts
#	execution.
# SECONDLD
#	Second stage boot straps are loaded an run at SECONDLD.
#	The BIOS boot actually uses 0x200 below this address to read
#	from the beginning of the partition.  The range SECONDLD - 0x200
#	to SECONDLD + 15*0x200 must not cross a 64K address boundary.
# BOOTLD
#	/boot loads at this address.  This must be above LOWMEM for /boot
#	to use the BIOS, and the boot code must not overwrite the gdt
#	within the fist-stage boot block.
# BOOTREL
#	/boot relocates itself and runs at BOOTREL.
# BIOSCALL
#	Memory needed to do BIOS calls starts at this address. There
#	are buffers for 0x1100 and then code.  The code is less the
#	0x100. 
# BOOT
#	Means that bootxx or boot is being built, rather than
#	a standalone application.
#

# all values in hex:
LOWMEM=4000
FIRSTLD=7c00
SECONDLD=50200
BOOTLD=8000
BOOTREL=1E0000
UTILLD=8000
# BIOSCALL=1000	from Makefile.common 

DEFAULTBOOT="/bsd"
CFLAGS=	-DSTANDALONE -DAT386 '-DDEFAULTBOOT=${DEFAULTBOOT}' -O ${INCPATH}

SRCS=	ahaboot.s boot.c bootxx.c \
	eahaboot.s fdboot.s srt0.s wdboot.s
SRCS+=	cat.c copy.c ls.c		# ??? should be elsewhere?

LIBS=	libsa/libsa.a libunzip/libunzip.a
SMLIBS=	libsmsa/libsmsa.a

BOOTBLOCKS= wdboot bootwd biosboot fdboot fdboot.debug fd5boot \
	bootfd bootfd.debug ahaboot bootaha eahaboot booteaha bootbios

DISKUTILS= boot cat copy ls
ALL=	${BOOTBLOCKS} ${DISKUTILS}

all: machine ${ALL}

machine:
	ln -s ../include machine

libsa/libsa.a::
	cd libsa; ${MAKE} ${MFLAGS}
libsmsa/libsmsa.a::
	cd libsmsa; ${MAKE} ${MFLAGS}
libunzip/libunzip.a::
	cd libunzip; ${MAKE}

# startups

srt0.o: srt0.s
	${CPP} -DLOADADR=0x${UTILLD} srt0.s | \
	    ${AS} -o srt0.o


wsrt0.o: srt0.s
	${CPP} -DSMALL -DLOADADR=0x${SECONDLD} -DBOOT srt0.s | \
	    ${AS} -o wsrt0.o

wsrt0.debug.o: srt0.s
	${CPP} -DDEBUG -DSMALL -DLOADADR=0x${SECONDLD} -DBOOT srt0.s | \
	    ${AS} -o wsrt0.debug.o

relsrt0.o: srt0.s
	${CPP} -DLOADADR=0x${BOOTLD} -DRELOCADR=0x${BOOTREL} -DBOOT srt0.s | \
	    ${AS} -o relsrt0.o

# block 0 boots

wdboot.o: wdboot.s
	${CPP} -DSECONDLD=0x${SECONDLD} wdboot.s | ${AS} -o $@

fdboot.o: fdboot.s 
	${CPP} -DSECONDLD=0x${SECONDLD} fdboot.s | ${AS} -o $@

fdboot.debug.o: fdboot.s 
	${CPP} -DFDDEBUG -DSECONDLD=0x${SECONDLD} fdboot.s | ${AS} -o $@

fd5boot.o: fdboot.s 
	${CPP} -DSECONDLD=0x${SECONDLD} -DFD5BOOT fdboot.s | ${AS} -o $@

ahaboot.o: ahaboot.s 
	${CPP} -DSECONDLD=0x${SECONDLD} ahaboot.s | ${AS} -o $@

# BOOTCD is ((target << 4) | partition);
# here decodes to boot from target 1 partition 6.
ahacdboot.o: ahaboot.s 
	${CPP} -DBOOTCD=0x16 -DSECONDLD=0x${SECONDLD} ahaboot.s | ${AS} -o $@

eahaboot.o: eahaboot.s
	${CPP} -DSECONDLD=0x${SECONDLD} eahaboot.s | ${AS} -o $@

biosboot.o: biosboot.s
	${CPP} -DLOCORE -DSECONDLD=0x${SECONDLD} biosboot.s | ${AS} -o $@


# bootable from floppy or real disks

boot:	boot.o relsrt0.o ${LIBS}
	ld -e __start -N -T ${BOOTREL} -o $@ relsrt0.o boot.o ${LIBS}

boot.kbd: boot.o relsrt0.o kbd_reset.kbd.o ${LIBS}
	ld -e __start -N -T ${BOOTREL} -o $@ relsrt0.o boot.o ${LIBS}

kbd_reset.kbd.o: kbd_reset.c
	${CC} -c -DIBM_THINKPAD_KBD_BOTCH ${CFLAGS} kbd_reset.c -o $@

boot.o: boot.c
	${CC} -c ${CFLAGS} -DLOWMEM=0x${LOWMEM} -DBOOTREL=0x${BOOTREL} boot.c

# boot for floppy that allows disk change after loading

# utilities

cat:	cat.o srt0.o ${LIBS}
	ld -N -T${UTILLD} -o $@ srt0.o cat.o ${LIBS}

ls:	ls.o srt0.o ${LIBS}
	ld -e entry -N -T${UTILLD} -o $@ srt0.o ls.o ${LIBS}

copy:	copy.o srt0.o ${LIBS}
	ld -N -T${UTILLD} -o $@ srt0.o copy.o ${LIBS}

# getting booted from disc

wdboot: wdboot.o
	ld -N -T ${FIRSTLD} wdboot.o
	cp a.out wdb
	rm -f wdboot
	strip a.out; dd if=a.out of=wdboot ibs=32 skip=1; rm -f a.out
	ls -l wdboot

bootxx.o: machine

bootwd: wsrt0.o bootxx.o ${SMLIBS} libsmsa/wd.o
	ld -N -T ${SECONDLD} wsrt0.o bootxx.o libsmsa/wd.o ${SMLIBS}
	size a.out
	cp a.out bwd
	rm -f bootwd
	strip a.out; dd if=a.out of=bootwd ibs=32 skip=1; rm -f a.out
	ls -l bootwd

bootbios: wsrt0.o bootxx.o ${SMLIBS} libsmsa/bios.o
	ld -N -T ${SECONDLD} wsrt0.o bootxx.o libsmsa/bios.o ${SMLIBS}
	size a.out
	cp a.out bbios
	rm -f bootbios
	strip a.out; dd if=a.out of=bootbios ibs=32 skip=1; rm -f a.out
	ls -l bootbios

fdboot: fdboot.o
	ld -N -T ${FIRSTLD} fdboot.o
	cp a.out fdb
	rm -f fdboot
	strip a.out; dd if=a.out of=fdboot ibs=32 skip=1; rm -f a.out
	ls -l fdboot

fdboot.debug: fdboot.debug.o
	ld -N -T ${FIRSTLD} fdboot.debug.o
	cp a.out fdb
	rm -f fdboot.debug
	strip a.out; dd if=a.out of=fdboot.debug ibs=32 skip=1; rm -f a.out
	ls -l fdboot.debug

fd5boot: fd5boot.o
	ld -N -T ${FIRSTLD} fd5boot.o
	cp a.out fd5b
	rm -f fd5boot
	strip a.out; dd if=a.out of=fd5boot ibs=32 skip=1; rm -f a.out
	ls -l fd5boot

bootfd: wsrt0.o bootxx.o ${SMLIBS} libsmsa/fd.o
	ld -N -T ${SECONDLD} wsrt0.o bootxx.o libsmsa/fd.o ${SMLIBS}
	size a.out
	cp a.out bfd
	rm -f bootfd
	strip a.out; dd if=a.out of=bootfd ibs=32 skip=1; rm -f a.out
	ls -l bootfd

sm_fd.debug.o: fd.c
	rm -f sm_fd.debug.c
	ln -s fd.c sm_fd.debug.c
	${CC} -c -DFDDEBUG=1 -DSMALL ${CFLAGS} sm_fd.debug.c
	rm -f sm_fd.debug.c

bootfd.debug: wsrt0.debug.o bootxx.o sm_fd.debug.o ${SMLIBS}
	ld -N -T ${SECONDLD} wsrt0.debug.o bootxx.o sm_fd.debug.o ${SMLIBS}
	size a.out
	cp a.out bfd.debug
	rm -f bootfd.debug
	strip a.out; dd if=a.out of=bootfd.debug ibs=32 skip=1; rm -f a.out
	ls -l bootfd.debug

ahaboot: ahaboot.o
	ld -N -T ${FIRSTLD} ahaboot.o
	cp a.out ahab
	rm -f ahaboot
	strip a.out; dd if=a.out of=ahaboot ibs=32 skip=1; rm -f a.out
	ls -l ahaboot

ahacdboot: ahacdboot.o
	ld -N -T ${FIRSTLD} ahacdboot.o
	cp a.out ahacdb
	rm -f ahacdboot
	strip a.out; dd if=a.out of=ahacdboot ibs=32 skip=1; rm -f a.out
	ls -l ahacdboot


bootaha: wsrt0.o bootxx.o ${SMLIBS} libsmsa/aha.o libsmsa/sd.o
	ld -N -T ${SECONDLD} wsrt0.o bootxx.o libsmsa/aha.o libsmsa/sd.o \
	    ${SMLIBS}
	size a.out
	cp a.out baha
	rm -f bootaha
	strip a.out; dd if=a.out of=bootaha ibs=32 skip=1; rm -f a.out
	ls -l bootaha

eahaboot: eahaboot.o
	ld -N -T ${FIRSTLD} eahaboot.o
	cp a.out eahab
	rm -f eahaboot
	strip a.out; dd if=a.out of=eahaboot ibs=32 skip=1; rm -f a.out
	ls -l eahaboot

booteaha: wsrt0.o bootxx.o ${SMLIBS} libsmsa/eaha.o libsmsa/sd.o
	ld -N -T ${SECONDLD} wsrt0.o bootxx.o libsmsa/eaha.o libsmsa/sd.o \
	    ${SMLIBS}
	size a.out
	cp a.out beaha
	rm -f booteaha
	strip a.out; dd if=a.out of=booteaha ibs=32 skip=1; rm -f a.out
	ls -l booteaha

biosboot: biosboot.o
	${LD} -N -T ${FIRSTLD} biosboot.o
	@echo "check: bootbase == 7c00; ebootblkcode <= 7dfe; edata == 7e00"
	@nm -p a.out | grep bootbase
	@nm -p a.out | grep ebootblkcode
	@nm -p a.out | grep edata
	@nm -p a.out | grep ' _end'
	cp a.out biosb
	rm -f biosboot
	strip a.out; dd if=a.out of=biosboot ibs=32 skip=1; rm -f a.out
	ls -l biosboot

clean:
	rm -f *.o *.exe *.i
	rm -f a.out fd fdb fd5b wdb ahab eahab biosb ${ALL}
	rm -f bbios baha beaha bfd bfd.debug bwd
	rm -f boot[a-wyz]? boot[a-wyz]??
	rm -f format core sboot
	cd libsa; ${MAKE} ${MFLAGS} clean
	cd libsmsa; ${MAKE} ${MFLAGS} clean
	cd libunzip; ${MAKE} ${MFLAGS} clean

cleandir: clean
	rm -f ${MAN} machine tags .depend
	cd libsa; ${MAKE} ${MFLAGS} cleandir
	cd libsmsa; ${MAKE} ${MFLAGS} cleandir
	cd libunzip; ${MAKE} ${MFLAGS} cleandir

depend: machine .depend
	cd libsa; ${MAKE} ${MFLAGS} depend
	cd libsmsa; ${MAKE} ${MFLAGS} depend
	cd libunzip; ${MAKE} ${MFLAGS} depend

.depend: ${SRCS}
	mkdep ${INCPATH} -DSTANDALONE ${.ALLSRC}

install: ${ALL}
	test -d ${MDEC} || mkdir -p ${MDEC}
	test -d ${STANDDIR} || mkdir -p ${STANDDIR}
	install -c -s boot ${DESTDIR}
	cp ${BOOTBLOCKS} ${MDEC}
	cp ${DISKUTILS} ${STANDDIR}
