# 
#  Copyright 2000 Compaq Computer Corporation.                       
#                                                                    
#  Copying or modifying this code for any purpose is permitted,        
#  provided that this copyright notice is preserved in its entirety     
#  in all copies or modifications.  COMPAQ COMPUTER CORPORATION          
#  MAKES NO WARRANTIES, EXPRESSED OR IMPLIED, AS TO THE USEFULNESS        
#  OR CORRECTNESS OF THIS CODE OR ITS FITNESS FOR ANY PARTICULAR           
#  PURPOSE.                                                                 
# 

#
# Compaq Personal Server Monitor Makefile
# David Panariti -- port to NetBSD
# Jamey Hicks -- PCI configuration setup
# Chris McKillop -- port to QNX
#

VERSION_MAJOR = 2
VERSION_MINOR = 14
VERSION_MICRO = 8

OS_NAME := $(shell uname -s | tr '[A-Z]' '[a-z]' )

#
# These are OS neutral settings.
#
include config.mk

#
# These are OS specific settings - "ln -s os_<os>.mk os.mk"
# Where <os> is one of linux, qnx, or netbsd.
#
include os_$(OS_NAME).mk

#
# Local Overrides - no error if not present.
#  - Use this file to override anything in os.mk or config.mk.
#
-include config.local.mk


#
# Normally you need not worry about the stuff below.
#

VER_DEFS = -DVERSION_MAJOR=$(VERSION_MAJOR) 
VER_DEFS += -DVERSION_MINOR=$(VERSION_MINOR) 
VER_DEFS += -DVERSION_MICRO=$(VERSION_MICRO) 

GLOBAL_DEFS = -DBOOTLDR -DDATE=\"`date +%y-%m-%d_%H:%M`\" 

INCLUDES = $(OS_INCLUDES) $(LOCAL_INCLUDES) -I.
ALL_DEFS = $(ARCH_DEFS) $(VER_DEFS) $(FLASH_DEFS) $(GLOBAL_DEFS) $(OS_DEFS) 
ALL_DEFS += $(LOCAL_DEFS)
CFLAGS = $(ALL_DEFS) $(OS_CFLAGS) $(LOCAL_CFLAGS) $(INCLUDES)
ASMFLAGS = $(ALL_DEFS) $(OS_ASMFLAGS) $(LOCAL_ASMFLAGS)
CLIBS = $(OS_CLIBS) $(LOCAL_CLIBS) -lc -lz

.PHONY : mk_date_code_

all: bootldr wince-bootldr

HDRS=bootconfig.h bootldr.h btflash.h btpci.h sa1100.h cyclone_boot.h heap.h pcireg.h regs-21285.h bsdsum.h modem.h splashz.h aux_micro.h boot_flags.h lcd.h

# BOOTO = boot.o
ifeq ($(CONFIG_ARCH), bitsy)
  BOOTO = boot-sa1100.o mmu-strongarm.o
endif
ifeq ($(CONFIG_ARCH), jornada720)
  BOOTO = boot-sa1100.o mmu-strongarm.o
endif
ifeq ($(CONFIG_ARCH), skiff)
  BOOTO = boot.o mmu-strongarm.o 
endif
ifeq ($(CONFIG_ARCH), assabet)
  BOOTO = boot-sa1100.o mmu-strongarm.o
endif
ifeq ($(CONFIG_ARCH), neponset)
  BOOTO = boot-sa1100.o mmu-strongarm.o
endif
OTHEROBJS = bootldr.o \
	btflash.o \
	heap.o \
	xmodem.o \
        bsdsum.o \
        modem.o \
	getcmd.o \
	$(AUX_MICRO_O) \
	lcd.o 

ifeq ($(CONFIG_BIG_KERNEL),y)
ARCH_DEFS += -DCONFIG_BIG_KERNEL
VERS_BK = -bigkernel-mono
else
VERS_BK = -mono
endif


LDFLAGS_C002 = -Twinceld.ld -Bstatic 
LDFLAGS = -Tbootld.ld -Bstatic 
ifeq ($(CONFIG_POWERMGR),y)
ARCH_DEFS += -DCONFIG_POWERMGR
endif

ifeq ($(CONFIG_ARCH), assabet)
  ARCH_DEFS += -DCONFIG_ASSABET=1 -DCONFIG_INTEL_FLASH=1
endif
ifeq ($(CONFIG_ARCH), bitsy)
  LDBASE_C002 = 0xc0022000
  # LDFLAGS_C002 = -Ttext=0xc0022000 -Tdata=0xc0032700 -Bstatic 
  LDFLAGS_C002 = -Twinceld.ld -Bstatic 
  # LDFLAGS = -Ttext=0x0 -Tdata=0x10700 -Bstatic 
  LDFLAGS = -Tbootld.ld -Bstatic 
  LDBASE_0000 = 0x00
  LDFLAGS_HI = -Ttext=0x00020000 -Tdata=0x00030700 -Bstatic 
  ARCH_DEFS += -DCONFIG_BITSY=1 -DCONFIG_INTEL_FLASH=1
  AUX_MICRO_O = aux_micro.o
endif
ifeq ($(CONFIG_ARCH), jornada720)
  ARCH_DEFS += -DCONFIG_JORNADA720=1 -DCONFIG_INTEL_FLASH=1
endif
ifeq ($(CONFIG_ARCH), neponset)
  ARCH_DEFS += -DCONFIG_NEPONSET=1 -DCONFIG_INTEL_FLASH=1
endif
ifeq ($(CONFIG_ARCH), skiff)
  LDFLAGS = -Ttext=0x41000000 -Tdata=0x4100d000 -Bstatic 
  ARCH_DEFS += -DCONFIG_SKIFF=1 -DCONFIG_AMD_FLASH=1
  CONFIG_PCI = y
endif

ifeq ($(CONFIG_PCI), y)
  ARCH_DEFS += -DCONFIG_PCI=1
  OTHEROBJS += btpci.o
endif
ifeq ($(CONFIG_BZIP), yes)
  CLIBS += -lbzip
endif
ifeq ($(CONFIG_GZIP), 1)
  CFLAGS += -DCONFIG_GZIP
  CLIBS += -lz
endif


OBJS = $(BOOTO) $(OTHEROBJS)

VER_STR=$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_MICRO)$(VERS_BK)

#
# patch the a.out header with a branch around itself.
# needs to be done for the first download of a new
# a.out bootldr.
# The a.out bootldr will patch the header iff it detects an ARM
# ZMAGIC magic number.
#

bootldr: $(HDRS) $(OBJS) bootld.ld zbsdchksum
	$(MK_DATE_CODE)
	$(CROSS_COMPILE)$(LD) -v $(LDFLAGS) -o bootldr-elf $(BOOTO) $(OTHEROBJS) $(mak_DC_OBJ) $(CLIBS)
	$(CROSS_COMPILE)$(NM) -v -l bootldr-elf > bootldr.nm
	$(CROSS_COMPILE)$(OBJCOPY) -O binary -S bootldr-elf bootldr $(OBJCOPYFLAGS)
	./zbsdchksum 
	cp bootldr bootldr-0000-$(VER_STR)
	$(MD5SUM) bootldr-0000-$(VER_STR) > bootldr-0000-$(VER_STR).md5sum

winceld.ld:	bootldr.ld.in Makefile
	sed s/@LOAD_BASE_ADDR@/$(LDBASE_C002)/g bootldr.ld.in > $@

bootld.ld:	bootldr.ld.in Makefile
	sed s/@LOAD_BASE_ADDR@/$(LDBASE_0000)/g bootldr.ld.in > $@

wince-bootldr: $(HDRS) $(OBJS) winceld.ld zbsdchksum
	$(CROSS_COMPILE)$(LD) -v $(LDFLAGS_C002) -o wince-bootldr-elf $(BOOTO) $(OTHEROBJS) $(mak_DC_OBJ) $(CLIBS)
	$(CROSS_COMPILE)$(NM) -v -l wince-bootldr-elf > wince-bootldr.nm
	$(CROSS_COMPILE)$(OBJCOPY) -O binary -S wince-bootldr-elf wince-bootldr $(OBJCOPYFLAGS)
	./zbsdchksum $@
	cp wince-bootldr bootldr-c000-$(VER_STR)
	$(MD5SUM) bootldr-c000-$(VER_STR) > bootldr-c000-$(VER_STR).md5sum

bootldr.hex: bootldr
	hexdump -v -e '"0x%x\n"' bootldr > bootldr.hex

updateosl: wince-bootldr bootldr 
	echo "/* last extracted on `date` by $(USER) on $(HOST) */" > wince-bootldr.struct
	echo "/* compiled to run at address 0xc0022000 */" >> wince-bootldr.struct
	echo " " >> wince-bootldr.struct
	echo "EMBEDDED_IMAGE ram_bootldr_image = {" >> wince-bootldr.struct 
	echo "        0x0665d3e0, 0x21b93596, 0xc2213f2c, 0xf7b740ea, /* signature */ " >> wince-bootldr.struct
	echo "        0x3,      /* structure version */" >> wince-bootldr.struct
	echo "        MAX_IMAGE_SIZE,      /* max image size */" >> wince-bootldr.struct
	echo "        0xC0022000,          /* physical address of image */" >> wince-bootldr.struct
	echo "        `ls -l ./wince-bootldr | awk '{print $$5}'`, /*  $@ image size */" >> wince-bootldr.struct
	echo "        `date -r wince-bootldr +%s`,                   /* last modified time as time_t 0=unknown*/" >> wince-bootldr.struct
	echo "        $(VERSION_MAJOR),    /* $@ major version */" >> wince-bootldr.struct
	echo "        $(VERSION_MINOR),    /* $@ minor version */" >> wince-bootldr.struct
	echo "        $(VERSION_MICRO),    /* $@ micro version */" >> wince-bootldr.struct
	hexdump -v -e '"        0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x,\n"' wince-bootldr >> wince-bootldr.struct
	echo "};" >> wince-bootldr.struct
	echo " " >> wince-bootldr.struct
	echo "/* last extracted on `date` by $(USER) on $(HOST) */" > flash-bootldr.struct
	echo "/* compiled to run at address 0x00000000 */" >> flash-bootldr.struct
	echo " " >> flash-bootldr.struct
	echo "EMBEDDED_IMAGE flash_bootldr_image = {" >> flash-bootldr.struct 
	echo "        0xce83a165, 0x4ed944c6, 0x493ff54a, 0x520596d3, /* signature */ " >> flash-bootldr.struct
	echo "        0x3,      /* structure version */" >> flash-bootldr.struct
	echo "        MAX_IMAGE_SIZE,      /* max image size */" >> flash-bootldr.struct
	echo "        0x00000000,          /* physical address of image */" >> flash-bootldr.struct
	echo "        `ls -l ./bootldr | awk '{print $$5}'`, /*  flash-bootldr image size */" >> flash-bootldr.struct
	echo "        `date -r bootldr +%s`,                   /* last modified time as time_t 0=unknown*/" >> flash-bootldr.struct
	echo "        $(VERSION_MAJOR),    /* flash-bootldr major version */" >> flash-bootldr.struct
	echo "        $(VERSION_MINOR),    /* flash-bootldr minor version */" >> flash-bootldr.struct
	echo "        $(VERSION_MICRO),    /* flash-bootldr micro version */" >> flash-bootldr.struct
	hexdump -v -e '"        0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x,\n"' bootldr >> flash-bootldr.struct
	echo "};" >> flash-bootldr.struct
	echo " " >> flash-bootldr.struct
	cat wince-bootldr.struct flash-bootldr.struct > wce-temp.struct
	cp wce-temp.struct wince-bootldr.struct
	echo '/:+++/+1,/:---/-1d' > bootldr_image.ed
	echo 'w'                 >> bootldr_image.ed
	echo '/:+++/r wince-bootldr.struct' >> bootldr_image.ed
	echo '/:+++/+1,/:---/-1s/0x        ,//g' >> bootldr_image.ed
	echo 'w'                 >> bootldr_image.ed
	echo 'q'                 >> bootldr_image.ed
	ed - ../windowsce/osloader/bootldr_image.cpp < bootldr_image.ed
	rm wince-bootldr.struct bootldr_image.ed wce-temp.struct flash-bootldr.struct

boot.o: boot.s $(HDRS)
	$(CROSS_COMPILE)$(ASM) $(ASMFLAGS) boot.s

boot-sa1100.o: boot-sa1100.s $(HDRS)
	$(CROSS_COMPILE)$(ASM) $(ASMFLAGS) boot-sa1100.s

btpci.o: btpci.c $(HDRS)
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c btpci.c

btusb.o: btusb.c $(HDRS) btusb.h
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c btusb.c

btflash.o: btflash.c $(HDRS)
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c btflash.c

heap.o: heap.c $(HDRS)
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c heap.c

xmodem.o: xmodem.c $(HDRS)
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c xmodem.c

modem.o: modem.c $(HDRS)
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c modem.c

bsdsum.o: bsdsum.c $(HDRS)
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c bsdsum.c

bootldr.o: bootldr.c $(HDRS)
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c bootldr.c

getcmd.o: getcmd.c $(HDRS)
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c getcmd.c

aux_micro.o: aux_micro.c $(HDRS)
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c aux_micro.c

lcd.o: lcd.c $(HDRS)
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c lcd.c

mmu-strongarm.o: mmu-strongarm.c $(HDRS)
	$(CROSS_COMPILE)$(CC) $(CFLAGS) -c mmu-strongarm.c

zerosum: zerosum.c
	$(CC) -o zerosum zerosum.c

zbsdchksum: zbsdchksum.c
	$(CC) -o zbsdchksum zbsdchksum.c

splashz.h:
	./zpnm.py $(CONFIG_GIMP_SPLASH_PNM) | \
		./bin2h.py splash_zimg > \
			splashz.h

clean:
	rm -f $(OBJS) bootldr-elf bootldr bootldr.nm \
		wince-bootldr wince-bootldr-elf wince-bootldr.nm \
                zbsdchksum generated_date_code.h \
                generated_date_code.c generated_date_code.o \
                zbsdchksum \
		bootldr-[0c]000-*

distclean: clean
	rm -f bootldr-000* bootldr-c000*

ifeq ($(CONFIG_USE_DATE_CODE),y)

CFLAGS += -DCONFIG_USE_DATE_CODE

mak_DC_FILE = generated_date_code
mak_DC_H_FILE = $(mak_DC_FILE).h
mak_DC_C_FILE = $(mak_DC_FILE).c
mak_DC_OBJ = $(mak_DC_FILE).o
DATE_CODE_VAR := _bootldr_date_code
DATE_CODE_DEF	:= BOOTLDR_DATE_CODE
MK_DATE_CODE = $(MAKE) mk_date_code_
mk_date_code_:
	rm -f $(mak_DC_C_FILE)
	rm -f $(mak_DC_H_FILE)
	echo "/*" > $(mak_DC_H_FILE)
	echo " * Generated file.  DO NOT EDIT." >> $(mak_DC_H_FILE)
	echo " * Well, you can if you want to, but, why?" >> $(mak_DC_H_FILE)
	echo " */" >> $(mak_DC_H_FILE)
	echo "#define $(DATE_CODE_DEF) \"`date`\"" >> $(mak_DC_H_FILE)
	echo "/*" > $(mak_DC_C_FILE)
	echo " * Generated file.  DO NOT EDIT." >> $(mak_DC_C_FILE)
	echo " * Well, you can if you want to, but, why?" >> $(mak_DC_C_FILE)
	echo " */" >> $(mak_DC_C_FILE)
	echo "#include \"$(mak_DC_H_FILE)\"" >> $(mak_DC_C_FILE)
	echo "char $(DATE_CODE_VAR)[]=$(DATE_CODE_DEF);" >> $(mak_DC_C_FILE)
	cc -c -o $(mak_DC_OBJ) $(mak_DC_C_FILE)
else
mak_DC_OBJ=
MK_DATE_CODE =
endif
