# arch/ppc/boot/sandpoint/Makefile
# 
# Makefile for Motorola SPS Sandpoint bootloader
#
# Author: Mark A. Greer
#	  mgreer@mvista.com
#
# Copyright 2001 MontaVista Software Inc.
#
# This program is free software; you can redistribute  it and/or modify it
# under  the terms of  the GNU General  Public License as published by the
# Free Software Foundation;  either version 2 of the  License, or (at your
# option) any later version.

.c.s:
	$(CC) $(CFLAGS) -S -o $*.s $<
.s.o:
	$(AS) -o $*.o $<
.c.o:
	$(CC) $(CFLAGS) -D__BOOTER__ -c -o $*.o $<
.S.s:
	$(CPP) $(AFLAGS) -traditional -o $*.o $<
.S.o:
	$(CC) $(AFLAGS) -traditional -c -o $*.o $<

BOOTADDRESS  = 0x00800000
CORDADDRESS  = 0xff000000
IMAGEADDRESS = 0xff006000
STACKADDRESS = 0x00900000

ZLINKFLAGS = -T ../ld.script -Ttext $(BOOTADDRESS)
BLINKFLAGS = -T ../ld.script -Ttext $(BOOTADDRESS)
OBJECTS := head.o ../common/misc-common.o ../common/misc-simple.o \
		  ../common/ns16550.o ../common/string.o ../common/bootinfo.o

BOOT_OBJECTS := boot_head.o ../common/misc-common.o ../common/boot_misc-simple.o \
		  ../common/ns16550.o ../common/string.o  ../common/bootinfo.o

OBJCOPY_ARGS = -O elf32-powerpc
OBJCOPY_ARGS_BIN = -I elf32-powerpc -O binary

LIBS := ../lib/zlib.a

OFFSET     := ../utils/offset
SIZE       := ../utils/size
SETSUM     := ../utils/setsum/setsum
SETBOOTSUM := ../utils/setsum/setbootsum

ifdef CONFIG_LAN
CFLAGS     := $(CFLAGS) -DCONFIG_LAN
AFLAGS     := $(AFLAGS) -DCONFIG_LAN
endif

ifdef CONFIG_HLAN
CFLAGS     := $(CFLAGS) -DCONFIG_HLAN
AFLAGS     := $(AFLAGS) -DCONFIG_HLAN
endif

ifdef CONFIG_HGLAN
CFLAGS     := $(CFLAGS) -DCONFIG_HGLAN
AFLAGS     := $(AFLAGS) -DCONFIG_HGLAN
endif

ifdef CONFIG_HTGL
CFLAGS     := $(CFLAGS) -DCONFIG_HTGL
AFLAGS     := $(AFLAGS) -DCONFIG_HTGL
endif

all: zImage

../common/misc-simple.o: ../common/misc-simple.c
	rm -f ../common/boot_misc-simple.o
	$(CC) $(CFLAGS) -DINITRD_OFFSET=0 -DINITRD_SIZE=0 -DZIMAGE_OFFSET=0 \
		-DZIMAGE_SIZE=0 -c -o $@ $*.c

zvmlinux.initrd: $(OBJECTS) $(LIBS) ../images/vmlinux.gz
	$(LD) $(ZLINKFLAGS) -o $@.tmp $(OBJECTS) $(LIBS)
	$(OBJCOPY) $(OBJCOPY_ARGS) -R .comment \
		--add-section=initrd=../images/ramdisk.image.gz \
		--add-section=image=../images/vmlinux.gz \
		--set-section-flags=image=alloc,load,readonly,data \
		--set-section-flags=initrd=alloc,load,readonly,data \
		$@.tmp $@
	$(CC) $(CFLAGS) -DINITRD_OFFSET=`sh $(OFFSET) $(OBJDUMP) $@ initrd` \
		-DINITRD_SIZE=`sh $(SIZE) $(OBJDUMP) $@ initrd` \
		-DZIMAGE_OFFSET=`sh $(OFFSET) $(OBJDUMP) $@ image` \
		-DZIMAGE_SIZE=`sh $(SIZE) $(OBJDUMP) $@ image` \
		-c -o ../common/misc-simple.o ../common/misc-simple.c
	$(LD) $(ZLINKFLAGS) -o $@.tmp $(OBJECTS) $(LIBS)
	$(OBJCOPY) $(OBJCOPY_ARGS) -R .comment \
		--add-section=initrd=../images/ramdisk.image.gz \
		--add-section=image=../images/vmlinux.gz \
		--set-section-flags=image=alloc,load,readonly,data \
		--set-section-flags=initrd=alloc,load,readonly,data \
		$@.tmp $@
	rm -f $@.tmp

zImage: zvmlinux bootcode
	$(SETSUM) ../../../../firminfo.txt ../images/vmlinux.gz -o ../images/kernimg.bin
	mv zvmlinux ../images/$@.sandpoint
	rm ../images/vmlinux.gz

zImage.initrd: zvmlinux.initrd bootcode
	$(SETSUM) ../../../../firminfo.txt ../images/vmlinux.gz -r ../images/ramdisk.image.gz -o ../images/firmimg.bin
	mv zvmlinux.initrd ../images/$@.sandpoint
	mv bootcode.bin ../images/bootcode.bin
	rm ../images/vmlinux.gz
	rm ./bootcode

zvmlinux: $(OBJECTS) $(LIBS) ../images/vmlinux.gz

#
# build the boot loader image and then compute the offset into it
# for the kernel image
#
	$(LD) $(ZLINKFLAGS) -o $@.tmp $(OBJECTS) $(LIBS)
	$(OBJCOPY) $(OBJCOPY_ARGS) -R .comment \
		--add-section=image=../images/vmlinux.gz \
		--set-section-flags=image=alloc,load,readonly,data \
		$@.tmp $@
#
# then with the offset rebuild the bootloader so we know where the kernel is
#
	$(CC) $(CFLAGS) -DINITRD_OFFSET=0 -DINITRD_SIZE=0 \
		-DZIMAGE_OFFSET=`sh $(OFFSET) $(OBJDUMP) $@ image` \
		-DZIMAGE_SIZE=`sh $(SIZE) $(OBJDUMP) $@ image` \
		-D__BOOTER__ \
		-c -o ../common/misc-simple.o ../common/misc-simple.c
	$(LD) $(ZLINKFLAGS) -o $@.tmp $(OBJECTS) $(LIBS)
	$(OBJCOPY) $(OBJCOPY_ARGS) -R .comment \
		--add-section=image=../images/vmlinux.gz \
		--set-section-flags=image=alloc,load,readonly,data \
		$@.tmp $@
	rm -f $@.tmp

# BUFFALO Boot code image from Flash ROM -------------------
boot_head.o: head.S
	$(CC) $(AFLAGS) -traditional -c -o boot_head.o head.S \
		-DFLASH_BOOT_IMAGE=1 

../common/boot_misc-simple.o: ../common/misc-simple.c
	$(CC) $(CFLAGS) -DFLASH_BOOT_IMAGE=1 -DINITRD_SIZE=0 \
		-DZIMAGE_OFFSET=0 -DZIMAGE_SIZE=0 -c -o $@ ../common/misc-simple.c

bootcode: $(BOOT_OBJECTS) $(LIBS) ../images/vmlinux.gz
	$(LD) $(BLINKFLAGS) -Tbss $(STACKADDRESS) -o $@.tmp $(BOOT_OBJECTS) $(LIBS)
	$(OBJCOPY) $(OBJCOPY_ARGS) -R .comment \
		$@.tmp $@
	$(OBJCOPY) $(OBJCOPY_ARGS_BIN)   $@ $@.bin
	rm -f $@.tmp

clean:
	rm -f head.o misc-sample.o ../common/misc-simple.o ../common/misc-common.o

include $(TOPDIR)/Rules.make
