#
# arch/alpha/miniboot/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) 1994 by Linus Torvalds
#
KSRC	= /usr/src/linux

include $(KSRC)/.config

LINKFLAGS =	-non_shared -N 
CFLAGS	= -g -O -Wall -mno-fp-regs -DMINI_SERIAL_ECHO
LIBS	= $(KSRC)/arch/alpha/lib/lib.a $(KSRC)/lib/lib.a $(KSRC)/arch/alpha/lib/lib.a
NM	= nm

#
#  Where do things go in memory?  Be careful with this as you need to have things
#  like relocate.S and miniboot.ld agree with these numbers.
#
RELOC_AT =       0xfffffc00001fff80
PALCODE_AT =     0xfffffc0000200000
LOADER_AT =      0xfffffc0000210000
LINKFLAGS := 	 $(LINKFLAGS) -T miniboot.ld

#
#  ...and how big are they?
#
PALCODE_SIZE =	0x10000
LOADER_SIZE =	0x20000
ifdef MINI_LINUX_IN_MEMORY
LINUX_SIZE =	0x200000
LINUX_AT = 	 0xfffffc0000230000
endif

#
#  The main config utility tells us what sort of system it is.  Here we derive
#  some more information from this, like where's the PALcode?  Some of these control the
#  things that get built later on.
#
ifdef CONFIG_ALPHA_NONAME
MACHINE_TYPE 	=	-DDC21066
PALCODE_NH  	= palcode/noname/osfpal.nh
RELOCATE_MINI   = 1
SYSTEM_SPECIFIC = -DMINI_NVRAM
ALL		= miniloader mboot bootm updateflash
endif

ifdef CONFIG_ALPHA_CABRIOLET
MACHINE_TYPE =	-DDC21064
PALCODE_NH  = palcode/eb64p/osfpal.nh
SYSTEM_SPECIFIC = 
ALL		= miniloader 
endif


ifdef CONFIG_ALPHA_EB64
MACHINE_TYPE =	-DDC21064
PALCODE_NH  = palcode/eb64/osfpal.nh
SYSTEM_SPECIFIC = 
ALL		= miniloader
endif

ifdef CONFIG_ALPHA_EB66
MACHINE_TYPE =	-DDC21066
PALCODE_NH  = palcode/eb66/osfpal.nh
SYSTEM_SPECIFIC = 
ALL		= miniloader
endif

ifdef CONFIG_ALPHA_EB64P
MACHINE_TYPE =	-DDC21064
PALCODE_NH  = palcode/eb64p/osfpal.nh
SYSTEM_SPECIFIC = 
ALL		= miniloader
endif

ifdef CONFIG_ALPHA_EB66P
MACHINE_TYPE = -DDC21066
PALCODE_NH  = palcode/eb66p/osfpal.nh
BUILD_FLASH = 1
SYSTEM_SPECIFIC = 
ALL		= miniloader
endif

#
#  Set up the compile and build time constants that we need.
#
ifdef MINI_LINUX_IN_MEMORY
WHERE   =    	-DPALCODE_AT=$(PALCODE_AT) \
		-DLINUX_AT=$(LINUX_AT) \
		-DLOADER_AT=$(LOADER_AT) 
else
WHERE   =    	-DPALCODE_AT=$(PALCODE_AT) \
		-DLOADER_AT=$(LOADER_AT) 
endif

ifdef MINI_LINUX_IN_MEMORY
SIZES   =	-DPALCODE_SIZE=$(PALCODE_SIZE) \
		-DLOADER_SIZE=$(LOADER_SIZE) \
		-DLINUX_SIZE=$(LINUX_SIZE)    
else
SIZES   =	-DPALCODE_SIZE=$(PALCODE_SIZE) \
		-DLOADER_SIZE=$(LOADER_SIZE) 
endif

DEFINES = $(WHERE) $(SIZES)  \
	$(MACHINE_TYPE) -D__KERNEL__  \
	$(SYSTEM_SPECIFIC)

INCLUDES = -I$(KSRC)/include

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

OBJECTS = miniboot.o boot_main.o env.o nvram.o kbd.o printf.o cpu.o uart.o  boot_e2lib.o  timer.o devices.o vga.o 

ZIPOBJECTS = zip/misc.o zip/inflate.o zip/unzip.o

FLASH_OBJECTS = flash_entry.o flash.o printf.o uart.o vga.o

#
#  What objects/libraries that we need from the Linux kernel depends on what we're building.
#
ifdef CONFIG_PCI
ifdef MINI_LINUX_IN_MEMORY

OBJECTS := $(OBJECTS) $(KSRC)/arch/alpha/kernel/irq.o 

ifdef CONFIG_ALPHA_LCA
OBJECTS := $(OBJECTS) $(KSRC)/arch/alpha/kernel/lca.o 
endif

ifdef CONFIG_ALPHA_APECS
OBJECTS := $(OBJECTS) $(KSRC)/arch/alpha/kernel/apecs.o 
endif

else

OBJECTS := $(OBJECTS) $(ZIPOBJECTS) $(KSRC)/drivers/pci/pci.a $(KSRC)/arch/alpha/kernel/irq.o \
	$(KSRC)/arch/alpha/kernel/bios32.o

ifdef CONFIG_ALPHA_LCA
OBJECTS := $(OBJECTS) $(KSRC)/arch/alpha/kernel/lca.o 
FLASH_OBJECTS := $(FLASH_OBJECTS) $(KSRC)/arch/alpha/kernel/lca.o 
endif

ifdef CONFIG_ALPHA_APECS
OBJECTS := $(OBJECTS) $(KSRC)/arch/alpha/kernel/apecs.o 
FLASH_OBJECTS := $(FLASH_OBJECTS) $(KSRC)/arch/alpha/kernel/apecs.o 
endif

endif

ifdef CONFIG_SCSI
OBJECTS := $(OBJECTS) $(KSRC)/kernel/resource.o $(KSRC)/drivers/scsi/scsi.a
OBJECTS := $(OBJECTS) $(KSRC)/drivers/block/block.a 
endif

ifdef CONFIG_FLOPPY
ifndef CONFIG_SCSI
OBJECTS := $(OBJECTS) $(KSRC)/drivers/block/block.a
endif
endif

endif

#
#  The set of targets for this directory depends on what system we're building the
#  miniloader for.
#
all:	$(ALL)
	@echo now build a rom, burn some flash or load an image!

msb:	all
	@echo dummy action for miniloader

# ----------------------------------------------------------------------------------------------------
#	The miniloader itself
# ----------------------------------------------------------------------------------------------------

vmlinux.stripped:   $(KSRC)/vmlinux
	quickstrip $(KSRC)/vmlinux vmlinux.stripped

ifdef MINI_LINUX_IN_MEMORY
  ifdef RELOCATE_MINI
miniloader:	relocate.nh $(PALCODE_NH) miniboot.nh vmlinux.stripped tools/sysgen 
	tools/sysgen \
		-s -e$(RELOC_AT)   relocate.nh \
		-s -e$(PALCODE_AT) $(PALCODE_NH) \
		-s -e$(LOADER_AT)  miniboot.nh \
		-s -e$(LINUX_AT)   vmlinux.stripped \
		-o miniloader
  else
miniloader:	$(PALCODE_NH) miniboot.nh vmlinux.stripped tools/sysgen 
	tools/sysgen \
		-s -e$(PALCODE_AT) $(PALCODE_NH) \
		-s -e$(LOADER_AT)  miniboot.nh \
		-s -e$(LINUX_AT)   vmlinux.stripped \
		-o miniloader
endif
else
ifdef RELOCATE_MINI
miniloader:	relocate.nh $(PALCODE_NH) miniboot.nh tools/sysgen 
	tools/sysgen \
		-s -e$(RELOC_AT)   relocate.nh \
		-s -e$(PALCODE_AT) $(PALCODE_NH) \
		-s -e$(LOADER_AT) miniboot.nh \
		-o miniloader
else
miniloader:	$(PALCODE_NH) miniboot.nh tools/sysgen 
	tools/sysgen \
		-s -e$(PALCODE_AT) $(PALCODE_NH) \
		-s -e$(LOADER_AT) miniboot.nh \
		-o miniloader
endif
endif

miniboot: $(OBJECTS) 
	$(LD) $(LINKFLAGS) \
		$(OBJECTS) \
		$(LIBS) \
		-o miniboot || \
		(rm -f miniboot && exit 1)
	$(NM) miniboot | grep -v '\(compiled\)\|\(\.o$$\)\|\( a \)' | sort > miniboot.map

miniboot.nh:	miniboot tools/cstrip
	tools/cstrip -va miniboot miniboot.nh

# ----------------------------------------------------------------------------------------------------
#	relocation code                           
# ----------------------------------------------------------------------------------------------------
ifdef RELOCATE_MINI

relocate.o:	relocate.S

relocate.nh:	relocate.o tools/cstrip
	tools/cstrip -va relocate.o relocate.nh

endif
# ----------------------------------------------------------------------------------------------------
#	disk boot block loadable miniloader image.
# ----------------------------------------------------------------------------------------------------

mboot:	miniloader tools/build
	tools/build -v -b miniloader mboot

bootm:	miniloader tools/build
	tools/build -v miniloader bootm

# ----------------------------------------------------------------------------------------------------
#	Arc loadable miniloader image.
# ----------------------------------------------------------------------------------------------------

arcload.exe:	miniloader tools/arc
	tools/arc -v -b$(PALCODE_AT) -e216480 miniloader arcload.exe

# ----------------------------------------------------------------------------------------------------
#	flash update tool
# ----------------------------------------------------------------------------------------------------
flash.h:	tools/data miniloader
	tools/data -v miniloader flash.h

flash.o:	flash.h

updateflash: $(FLASH_OBJECTS) flash.h
	$(LD) -non_shared -T updateflash.ld -N \
		$(FLASH_OBJECTS) \
		$(LIBS) \
		-o updateflash || \
		(rm -f updateflash && exit 1)
	$(NM) updateflash | grep -v '\(compiled\)\|\(\.o$$\)\|\( a \)' | sort > updateflash.map
	quickstrip updateflash updateflash

# ----------------------------------------------------------------------------------------------------
#	The tools that we need to build the miniloader.
# ----------------------------------------------------------------------------------------------------

tools/sysgen:	tools/sysgen.c
	$(HOSTCC) -o tools/sysgen tools/sysgen.c

tools/data:	tools/data.c
	$(HOSTCC) -o tools/data tools/data.c

tools/arc:	tools/arc.c
	$(HOSTCC) -o tools/arc tools/arc.c

tools/cstrip:	tools/cstrip.c
	$(HOSTCC) -o tools/cstrip tools/cstrip.c

tools/build:	tools/build.c
	$(HOSTCC) -o tools/build tools/build.c

clean:
	rm -f $(ALL) *.nh *.map *.lis *.exe *.o

dep:
