#
# aboot/Makefile
#
# This file is subject to the terms and conditions of the GNU General Public
# License.  See the file "COPYING" in the main directory of this archive
# for more details.
#
# Copyright (c) 1995, 1996 by David Mosberger (davidm@cs.arizona.edu)
#

# location of linux kernel sources (must be absolute path):
KSRC		= /usr/src/linux
VMLINUX		= $(KSRC)/vmlinux
VMLINUXGZ	= $(KSRC)/arch/alpha/boot/vmlinux.gz

#CFGDEFS	= -DDEBUG

root		=
bindir		= $(root)/sbin
bootdir		= $(root)/boot

#
# There shouldn't be any need to change anything below this line.
#
LOADADDR	= 20000000

ifeq ($(shell uname),Linux)
 elf=$(shell if $(LD) --help | grep elf64alpha >/dev/null; then echo yes; fi)
 ifeq ($(elf),yes)
   LINKFLAGS = -static -Ttext 0xfffffc0000310000 -N
   ABOOT_LDFLAGS = -static -N -Ttext $(LOADADDR)	# for Linux/ELF
 else
   ABOOT_LDFLAGS = -static -N -Taboot.lds		# for Linux/ECOFF
 endif
else
 ABOOT_LDFLAGS	= -non_shared -N -T $(LOADADDR)	# for DEC Unix
endif

CC		= gcc
OBJSTRIP	= $(KSRC)/arch/alpha/boot/tools/objstrip
TOP		= $(shell pwd)
CPPFLAGS	= $(CFGDEFS) -I$(TOP)/include -I$(KSRC)/include
CFLAGS		= $(CPPFLAGS) -D__KERNEL__ -O2 -Wall -mno-fp-regs
ASFLAGS		= $(CPPFLAGS)

.c.s:
	$(CC) $(CFLAGS) -S -o $*.s $<
.s.o:
	$(AS) -o $*.o $<
.c.o:
	$(CC) $(CFLAGS) -c -o $*.o $<
.S.s:
	$(CC) $(ASFLAGS) -D__ASSEMBLY__ -traditional -E -o $*.o $<
.S.o:
	$(CC) $(ASFLAGS) -D__ASSEMBLY__ -traditional -c -o $*.o $<

NET_OBJS = net.o
DISK_OBJS = disk.o fs/ext2.o fs/ufs.o fs/dummy.o fs/iso.o
ABOOT_OBJS = \
	head.o aboot.o cons.o utils.o \
	zip/misc.o zip/unzip.o zip/inflate.o
LIBS	= lib/libaboot.a

all:	diskboot

diskboot:	bootlx sdisklabel/sdisklabel sdisklabel/swriteboot \
		tools/e2writeboot tools/isomarkboot tools/abootconf \
		tools/elfencap

netboot: vmlinux.bootp

bootlx:	aboot $(OBJSTRIP)
	$(OBJSTRIP) -vb aboot bootlx

install: tools/abootconf tools/e2writeboot tools/isomarkboot \
	sdisklabel/swriteboot
	install -d -g 0 -o 0 $(bindir) $(bootdir)
	install -c -s -g 0 -o 0 tools/abootconf $(bindir)
	install -c -s -g 0 -o 0 tools/e2writeboot $(bindir)
	install -c -s -g 0 -o 0 tools/isomarkboot $(bindir)
	install -c -s -g 0 -o 0 sdisklabel/swriteboot $(bindir)
	install -c -g 0 -o 0 bootlx $(bootdir)

installondisk:	bootlx sdisklabel/swriteboot
	sdisklabel/swriteboot -vf0 /dev/sda bootlx vmlinux.gz

aboot:	$(ABOOT_OBJS) $(DISK_OBJS) $(LIBS)
	$(LD) $(ABOOT_LDFLAGS) $(ABOOT_OBJS) $(DISK_OBJS) -o $@ $(LIBS)
	strip $@ # this is needed to work around an ELF bug:

vmlinux.bootp: net_aboot.nh $(VMLINUXGZ) net_pad
	cat net_aboot.nh $(VMLINUXGZ) net_pad > $@

net_aboot.nh: net_aboot $(OBJSTRIP)
	$(OBJSTRIP) -vb net_aboot $@

net_aboot: $(ABOOT_OBJS) $(ABOOT_OBJS) $(NET_OBJS) $(LIBS)
	$(LD) $(ABOOT_LDFLAGS) $(ABOOT_OBJS) $(NET_OBJS) -o $@ $(LIBS)

net_pad:
	dd if=/dev/zero of=$@ bs=512 count=1

clean:	sdisklabel/clean tools/clean lib/clean
	rm -f aboot abootconf net_aboot net_aboot.nh net_pad vmlinux.bootp \
		$(ABOOT_OBJS) $(DISK_OBJS) $(NET_OBJS) bootlx \
		include/ksize.h vmlinux.nh

distclean: clean
	find . -name \*~ | xargs rm -f

lib/%:
	make -C lib $* CPPFLAGS="$(CPPFLAGS)"

tools/%:
	make -C tools $* CPPFLAGS="$(CPPFLAGS)"

sdisklabel/%:
	make -C sdisklabel $* CPPFLAGS="$(CPPFLAGS)"

disk.o: include/ksize.h

vmlinux.nh include/ksize.h: $(OBJSTRIP)
	cp $(VMLINUX) vmlinux.strip
	strip vmlinux.strip	# work around ELF binutils bug
	echo "#define KERNEL_SIZE `$(OBJSTRIP) -p vmlinux.strip /dev/null`" \
	 > include/ksize.h
	$(OBJSTRIP) -vb vmlinux.strip vmlinux.nh
	rm -rf vmlinux.strip

dep:
