From 8652988d62b19631e498b62a3800f0decb5e743a Mon Sep 17 00:00:00 2001
From: "henryk@ploetzli.ch"
 <henryk@ploetzli.ch@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Date: Thu, 27 Aug 2009 04:25:34 +0000
Subject: [PATCH] Merge linker scripts in bootrom to have a single linker
 script for the bootloader proper (previously known as bootrom-merged.s19)
 Remove the now unnecessary files (merge-srec.pl, ldscript-ram-jtag) Note that
 this drops the dependency on perl for bootrom build, so end-users who don't
 touch the FPGA bitstream will not need perl anymore

---
 armsrc/Makefile           |  8 +++----
 bootrom/Makefile          | 22 ++++++-------------
 bootrom/flash-reset.s     | 10 ++++-----
 bootrom/fromflash.c       |  2 +-
 bootrom/ldscript-flash    | 46 +++++++++++++++++++++++++++++++++------
 bootrom/ldscript-ram-jtag | 10 ---------
 bootrom/ram-reset.s       | 10 ++++-----
 common/Makefile.common    |  4 ++--
 tools/merge-srec.pl       | 23 --------------------
 9 files changed, 63 insertions(+), 72 deletions(-)
 delete mode 100644 bootrom/ldscript-ram-jtag
 delete mode 100644 tools/merge-srec.pl

diff --git a/armsrc/Makefile b/armsrc/Makefile
index ce1ad087..567c8d12 100644
--- a/armsrc/Makefile
+++ b/armsrc/Makefile
@@ -49,8 +49,8 @@ clean:
 help:
 	@echo Multi-OS Makefile, you are running on $(DETECTED_OS)
 	@echo Possible targets:
-	@echo +	all           - Make both:
-	@echo +	osimage.s19   - The OS image
-	@echo +	fpgaimage.s19 - The FPGA image
-	@echo +	clean         - Clean $(OBJDIR)
+	@echo +	all               - Make both:
+	@echo +	$(OBJDIR)/osimage.s19   - The OS image
+	@echo +	$(OBJDIR)/fpgaimage.s19 - The FPGA image
+	@echo +	clean             - Clean $(OBJDIR)
 
diff --git a/bootrom/Makefile b/bootrom/Makefile
index 2ab91286..43e2533f 100644
--- a/bootrom/Makefile
+++ b/bootrom/Makefile
@@ -1,36 +1,28 @@
 # Makefile for bootrom, see ../common/Makefile.common for common settings
 
-OBJJTAG = $(OBJDIR)/bootrom.o $(OBJDIR)/ram-reset.o $(OBJDIR)/usb.o
-OBJFLASH = $(OBJDIR)/flash-reset.o $(OBJDIR)/fromflash.o
-
-THUMBSRC = usb.c fromflash.c bootrom.c
-ASMSRC   = ram-reset.s flash-reset.s
+# DO NOT use thumb mode in the phase 1 bootloader since that generates a section with glue code
+ARMSRC = fromflash.c 
+THUMBSRC = usb.c bootrom.c
+ASMSRC = ram-reset.s flash-reset.s
 
 # Do not move this inclusion before the definition of {THUMB,ASM,ARM}{OBJ,SRC}
 include ../common/Makefile.common
 
-all: bootrom-merged.s19
-
-bootrom-merged.s19: $(OBJDIR)/bootrom.s19 $(OBJDIR)/bootrom-forjtag.s19
-	perl ../tools/merge-srec.pl $(OBJDIR)/bootrom.s19 $(OBJDIR)/bootrom-forjtag.s19 > bootrom-merged.s19
+all: $(OBJDIR)/bootrom.s19
 
-$(OBJDIR)/bootrom.elf: $(OBJFLASH)
+$(OBJDIR)/bootrom.elf: $(ASMOBJ) $(ARMOBJ) $(THUMBOBJ)
 	$(LD) -g -Tldscript-flash --oformat elf32-littlearm -Map=$(patsubst %.elf,%.map,$@) -o $@ $^
 
-$(OBJDIR)/bootrom-forjtag.elf: $(OBJJTAG)
-	$(LD) -g -Tldscript-ram-jtag --oformat elf32-littlearm -Map=$(patsubst %.elf,%.map,$@) -o $@ $^
-
 clean:
 	$(DELETE) $(OBJDIR)$(PATHSEP)*.o
 	$(DELETE) $(OBJDIR)$(PATHSEP)*.elf
 	$(DELETE) $(OBJDIR)$(PATHSEP)*.s19
 	$(DELETE) $(OBJDIR)$(PATHSEP)*.map
 	$(DELETE) $(OBJDIR)$(PATHSEP)*.d
-	$(DELETE) bootrom-merged.s19
 
 .PHONY: all clean help
 help:
 	@echo Multi-OS Makefile, you are running on $(DETECTED_OS)
 	@echo Possible targets:
-	@echo +	all   - Make bootrom-merged.s19, the main bootrom
+	@echo +	all   - Make $(OBJDIR)/bootrom.s19, the main bootrom
 	@echo +	clean - Clean $(OBJDIR)
diff --git a/bootrom/flash-reset.s b/bootrom/flash-reset.s
index afb658a4..48c111a7 100644
--- a/bootrom/flash-reset.s
+++ b/bootrom/flash-reset.s
@@ -1,11 +1,11 @@
 .extern CopyBootToRAM
     
-.text
-.code 32
-.align 0
+.section .startup,"ax"
+         .code 32
+         .align 0
 
-.global start
-start:
+.global flashstart
+flashstart:
     b       Reset
     b       UndefinedInstruction
     b       SoftwareInterrupt
diff --git a/bootrom/fromflash.c b/bootrom/fromflash.c
index e6868092..f6d5237c 100644
--- a/bootrom/fromflash.c
+++ b/bootrom/fromflash.c
@@ -1,6 +1,6 @@
 #include <proxmark3.h>
 
-void CopyBootToRAM(void)
+void __attribute__((section("bootphase1"))) CopyBootToRAM(void)
 {
 	int i;
 
diff --git a/bootrom/ldscript-flash b/bootrom/ldscript-flash
index 0d5d7325..fa6fb26e 100644
--- a/bootrom/ldscript-flash
+++ b/bootrom/ldscript-flash
@@ -1,11 +1,43 @@
+MEMORY 
+{
+	/* AT91SAM7S256 has 256k Flash and 64k RAM */
+	/* Important note: the correct ORIGIN for bootphase1 is 0x00100000 and for bootphase2 is 0x00100200
+	   However, this will confuse the currently deployed flash code which expects logical and and not
+	   physical addresses and performs no sanity checks at all. If confronted with physical addresses, 
+	   it will happily erase everything and brick the device. So for the time being pretend these addresses
+	   to start at 0x0 while updating all the flash code with proper sanity checks, then come back later and
+	   fix the addresses. -- Henryk Plötz <henryk@ploetzli.ch> 2009-08-27 */
+        bootphase1 : ORIGIN = 0x00000000, LENGTH = 0x200 /* Phase 1 bootloader: Copies real bootloader to RAM */
+        bootphase2 : ORIGIN = 0x00000200, LENGTH = 0x2000 - 0x200 /* Main bootloader code, stored in Flash, executed from RAM */
+        ram     : ORIGIN = 0x00200000, LENGTH = 32K
+}
+
+
 SECTIONS
 {
-    . = 0x00000000;
-    .text : { obj/flash-reset.o(.text) *(.text) }
-    .rodata : { *(.rodata) }
-    . = 0x00200000;
-    .data : { *(.data) }
-    __bss_start__ = .;
-    .bss : { *(.bss) }
+    . = 0;
+    
+    bootphase1 : {
+    	*(.startup) 
+    	*(.bootphase1)
+    } >bootphase1
+    
+    bootphase2 : {
+    	__bootphase2_start__ = .;
+    	*(.startphase2)
+    	*(.text)
+    	*(.glue_7)
+    	*(.rodata)
+        *(.data)
+    	. = ALIGN( 32 / 8 );
+    	__bootphase2_end__ = .;
+    } >ram AT>bootphase2
+    
+    .bss : {
+    	__bss_start__ = .; 
+    	*(.bss)
+    } >ram
+    
+    . = ALIGN( 32 / 8 );
     __bss_end__ = .;
 }
diff --git a/bootrom/ldscript-ram-jtag b/bootrom/ldscript-ram-jtag
deleted file mode 100644
index 5dd57061..00000000
--- a/bootrom/ldscript-ram-jtag
+++ /dev/null
@@ -1,10 +0,0 @@
-SECTIONS
-{
-    . = 0x00200000;
-    .text : { obj/ram-reset.o(.text) *(.text) }
-    .rodata : { *(.rodata) }
-    .data : { *(.data) }
-    __bss_start__ = .;
-    .bss : { *(.bss) }
-    __bss_end__ = .;
-}
diff --git a/bootrom/ram-reset.s b/bootrom/ram-reset.s
index 56bf36e3..6155b002 100644
--- a/bootrom/ram-reset.s
+++ b/bootrom/ram-reset.s
@@ -1,10 +1,10 @@
 .extern BootROM
     
-.text
-.code 32
-.align 0
+.section .startphase2,"ax"
+         .code 32
+         .align 0
 
-.global start
-start:
+.global ramstart
+ramstart:
     ldr     sp,     = 0x0020FFF8
     bl      BootROM
diff --git a/common/Makefile.common b/common/Makefile.common
index c4da75e1..eab96080 100644
--- a/common/Makefile.common
+++ b/common/Makefile.common
@@ -76,8 +76,8 @@ DEPENDENCY_FILES = $(patsubst %.c,$(OBJDIR)/%.d,$(notdir $(THUMBSRC))) \
 
 $(DEPENDENCY_FILES): Makefile ../common/Makefile.common
 $(OBJDIR)/%.d: %.c
-	$(CC) -MM -MT "$(@) $(@:.d=.o)" $(CFLAGS) $< > $@
+	@$(CC) -MM -MT "$(@) $(@:.d=.o)" $(CFLAGS) $< > $@
 $(OBJDIR)/%.d: %.s
-	$(CC) -MM -MT "$(@) $(@:.d=.o)" $(CFLAGS) $< > $@
+	@$(CC) -MM -MT "$(@) $(@:.d=.o)" $(CFLAGS) $< > $@
 
 -include $(DEPENDENCY_FILES)
diff --git a/tools/merge-srec.pl b/tools/merge-srec.pl
deleted file mode 100644
index 683e6d3c..00000000
--- a/tools/merge-srec.pl
+++ /dev/null
@@ -1,23 +0,0 @@
-# merge the code that initially executes out of flash with the RAM image
-
-($flashFile, $ramFile) = @ARGV;
-
-open(FLASH, $flashFile) or die "$flashFile: $!\n";
-
-while(<FLASH>) {
-	print if /^S3/;
-	$EOF_record = $_ if /^S[789]/;
-}
-
-open(RAM, $ramFile) or die "$ramFile: $!\n";
-
-while(<RAM>) {
-	if(/^S3(..)(........)(.*)([0-9a-fA-F]{2})/) {
-		$addr = sprintf('%08X', hex($2) - 0x00200000 + 0x200);
-		$line = "$1$addr$3";
-		$checksum = 0;
-		$checksum += $_ foreach map(hex, unpack("a2"x40, $line));
-		print "S3$line", sprintf("%02X", ($checksum%256)^0xff ), "\n";
-	}
-}
-print $EOF_record;
-- 
2.39.5