##
## Linux Loader for SPARC
##
#
CC=gcc
LD=ld
STRIP=strip
RM=/bin/rm -f
DD=dd
ELFTOAOUT=/usr/bin/elftoaout
NM=nm
VERSION=0.67

# Linux
CFLAGS = -O2 -I../include -fomit-frame-pointer

# Relocate to 2.5MB. The Hitchhiker's Guide to Open Boot Rev 3 claims
# that 3MB are guaranteed to be mapped.
# Changing this value will require changing a bunch of other constants throughout the code
LDFLAGS=-N -Ttext 0x4000

.S.o:
	$(CC) $(CFLAGS) -c $*.S

ifeq (Linux,$(shell uname))
ifeq (sparc,$(shell uname -m))
all: first.b first.h

first.h: first
	sed -n 's/^00004\([0-9a-f][0-9a-f][0-9a-f]\) .*digit_here.*$$/#define DIGIT_OFFSET_TMP 0x\1/p;s/^00004\([0-9a-f][0-9a-f][0-9a-f]\) .*letter_here.*$$/#define LETTER_OFFSET_TMP 0x\1/p' < first.map > first.h
else
all:
	@echo First stage bootblock should be build under Linux/Sparc only

first.h:
	@if [ ! -f first.h ]; then echo First do make under Linux/Sparc (and do not make clean until compiled on this platform); exit 1; fi
endif
else
all:
	@echo First stage bootblock should be build under Linux/Sparc only

first.h:
	@if [ ! -f first.h ]; then echo First do make under Linux/Sparc (and do not make clean until compiled on this platform); exit 1; fi
endif

first.o: first.S
	$(CC) $(CFLAGS) -c first.S -o first.o

first: first.o
	$(LD) $(LDFLAGS) -o first first.o
	$(NM) first | grep -v '*ABS*' | sort > first.map
	$(STRIP) first

clean:
	$(RM) *.o *~ first first.b first.map first.h core

first.b: first
	$(ELFTOAOUT) -o first.b first
	echo -n 'SILO'$(VERSION) | $(DD) of=first.b bs=8 conv=notrunc seek=3 count=1
	dd if=/dev/zero of=first.b bs=4 count=1 seek=127
	ln -f first.b ../boot/first.b

