From 86211dfc7b228eedc9ac7279dab724cf9a06f6ce Mon Sep 17 00:00:00 2001
From: Craig Comstock <craig_comstock@yahoo.com>
Date: Fri, 8 May 2015 01:18:29 +0000
Subject: [PATCH 2/2] Add support for e86 flash and loader configs as well as
 simple jumper app which replaces menu and simply jumps
 to the second flash page on e.g. c139 phone.

---
 src/target/firmware/Makefile                    |   17 ++-
 src/target/firmware/apps/jumper/main.c          |   30 +++++
 src/target/firmware/board/compal_e86/flash.lds  |  134 +++++++++++++++++++++
 src/target/firmware/board/compal_e86/loader.lds |  147 +++++++++++++++++++++++
 4 files changed, 323 insertions(+), 5 deletions(-)
 create mode 100644 src/target/firmware/apps/jumper/main.c
 create mode 100644 src/target/firmware/board/compal_e86/flash.lds
 create mode 100644 src/target/firmware/board/compal_e86/loader.lds

diff --git a/src/target/firmware/Makefile b/src/target/firmware/Makefile
index 0f4120f..54d557b 100644
--- a/src/target/firmware/Makefile
+++ b/src/target/firmware/Makefile
@@ -18,6 +18,12 @@ ENV_e88loader_OBJS=board/compal/start.rom.o board/compal/header.o board/compal/e
 ENV_e88flash_LDS=board/compal_e88/flash.lds
 ENV_e88flash_OBJS=board/compal/start.rom.o board/compal/header.o board/compal/exceptions_redirected.o board/compal/handlers.o
 
+ENV_e86loader_LDS=board/compal_e86/loader.lds
+ENV_e86loader_OBJS=board/compal/start.rom.o board/compal/header.o board/compal/exceptions_redirect.o
+
+ENV_e86flash_LDS=board/compal_e86/flash.lds
+ENV_e86flash_OBJS=board/compal/start.rom.o board/compal/header.o board/compal/exceptions_redirected.o board/compal/handlers.o
+
 
 #
 # Boards
@@ -66,7 +72,7 @@ BOARD_compal_e88_ENVIRONMENTS=$(compal_COMMON_ENVIRONMENTS) e88loader e88flash
 BOARD_compal_e86_OBJS=$(calypso_COMMON_OBJS) board/compal_e86/init.o \
 	board/compal_e86/rffe_dualband_e86.o board/compal/rf_power.o \
 	battery/dummy.o $(FB_e86_OBJS)
-BOARD_compal_e86_ENVIRONMENTS=$(compal_COMMON_ENVIRONMENTS)
+BOARD_compal_e86_ENVIRONMENTS=$(compal_COMMON_ENVIRONMENTS) e86loader e86flash
 
 # Compal E99
 BOARD_compal_e99_OBJS=$(compal_COMMON_OBJS) board/compal_e99/init.o \
@@ -84,12 +90,13 @@ BOARD_se_j100_ENVIRONMENTS=$(compal_COMMON_ENVIRONMENTS)
 #
 
 # List of all applications (meant to be overridden on command line)
-APPLICATIONS?=hello_world compal_dsp_dump layer1 loader rssi menu
+APPLICATIONS?=hello_world compal_dsp_dump layer1 loader rssi menu jumper
 
 # Applications specific env requirements
 APP_loader_ENVIRONMENTS=compalram highram
 APP_rssi_ENVIRONMENTS=* -compalram
 APP_menu_ENVIRONMENTS=* -highram
+APP_jumper_ENVIRONMENTS=e86loader e88loader
 
 # Various objects that are currently linked into all applications
 FLASH_OBJS=flash/cfi_flash.o
@@ -128,10 +135,10 @@ INCLUDES=-Iinclude/ -I../../../include -I../../shared/libosmocore/include
 #
 
 # Uncomment this line if you want to enable Tx (Transmit) Support.
-#CFLAGS += -DCONFIG_TX_ENABLE
+CFLAGS += -DCONFIG_TX_ENABLE
 
 # Uncomment this line if you want to write to flash.
-#CFLAGS += -DCONFIG_FLASH_WRITE
+CFLAGS += -DCONFIG_FLASH_WRITE
 
 # Uncomment this line if you want to write to flash, including the bootloader.
-#CFLAGS += -DCONFIG_FLASH_WRITE_LOADER
+CFLAGS += -DCONFIG_FLASH_WRITE_LOADER
diff --git a/src/target/firmware/apps/jumper/main.c b/src/target/firmware/apps/jumper/main.c
new file mode 100644
index 0000000..b9b9376
--- /dev/null
+++ b/src/target/firmware/apps/jumper/main.c
@@ -0,0 +1,30 @@
+/* Simple Jumper to jump to an address */
+
+/* (C) 2015 by Craig Comstock <craig_comstock@yahoo.com>
+ *
+ * All Rights Reserved
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#include <asm/system.h>
+
+void 
+main(void)
+{
+	void (*f) (void) = (void (*)(void))0x10000;
+	local_irq_disable();
+	f();
+}
diff --git a/src/target/firmware/board/compal_e86/flash.lds b/src/target/firmware/board/compal_e86/flash.lds
new file mode 100644
index 0000000..c28769d
--- /dev/null
+++ b/src/target/firmware/board/compal_e86/flash.lds
@@ -0,0 +1,134 @@
+/*
+ * Linker script for flashed applications on the Compal E86 (modified E88 script)
+ *
+ * This script creates a binary that can be linked at 0xFFFF, starting
+ * with the second flash page. This is what a phone application or
+ * pure layer1 device uses.
+ *
+ * XXX: interrupts?
+ *
+ */
+OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
+OUTPUT_ARCH(arm)
+ENTRY(_start)
+MEMORY
+{
+	LOADR (rx) : ORIGIN = 0x00000000, LENGTH = 0x10000
+    /* 4 MBytes of external flash memory (minus loader) */
+    FLASH (rx) : ORIGIN = 0x00010000, LENGTH = 0x3F0000
+    /* 256 kBytes of internal zero-waitstate sram */
+    IRAM  (rw) : ORIGIN = 0x00800000, LENGTH = 0x040000
+    /* 256 kBytes of external slow sram */
+    ERAM  (rw) : ORIGIN = 0x01000000, LENGTH = 0x040000
+}
+SECTIONS
+{
+    /* entrypoint */
+    .text.start : {
+        PROVIDE(_start = .);
+        KEEP(*(.text.start))
+        *(.text.start)
+    } > FLASH
+
+    /* exception vectors from 0x80001c to 0x800034 */
+    .text.exceptions 0x80001c : {
+        KEEP(*(.text.exceptions))
+        * (.text.exceptions)
+        . = ALIGN(4);
+    } > IRAM AT> FLASH
+    PROVIDE(_exceptions = LOADADDR(.text.exceptions));
+
+    /* code */
+    .text : {
+        /* regular code */
+        *(.text*)
+        /* gcc voodoo */
+        *(.glue_7t) *(.glue_7) *(.vfp11_veneer) *(.v4_bx)
+    } > FLASH
+    PROVIDE(_text_start = ADDR(.text));
+    PROVIDE(_text_end = ADDR(.text) + SIZEOF(.text));
+
+    /* constructor pointers */
+    .ctors : {
+        /* ctor count */
+        LONG(SIZEOF(.ctors) / 4 - 2)
+        /* ctor pointers */
+        KEEP(*(SORT(.ctors)))
+        /* end of list */
+        LONG(0)
+    } > FLASH
+    PROVIDE(_ctor_start = LOADADDR(.ctors));
+    PROVIDE(_ctor_end = LOADADDR(.ctors) + SIZEOF(.ctors));
+
+    /* destructor pointers */
+    .dtors : {
+        /* dtor count */
+        LONG(SIZEOF(.dtors) / 4 - 2)
+        /* dtor pointers */
+        KEEP(*(SORT(.dtors)))
+        /* end of list */
+        LONG(0)
+    } > FLASH
+    PROVIDE(_dtor_start = LOADADDR(.dtors));
+    PROVIDE(_dtor_end = LOADADDR(.dtors) + SIZEOF(.dtors));
+
+    /* read-only data */
+    .rodata : {
+        *(.rodata*)
+    } > FLASH
+    PROVIDE(_rodata_start = ADDR(.rodata));
+    PROVIDE(_rodata_end = ADDR(.rodata) + SIZEOF(.rodata));
+
+    /* pic offset tables */
+    .got : {
+        . = ALIGN(4);
+        *(.got)
+        *(.got.plt) *(.igot.plt) *(.got) *(.igot)
+        . = ALIGN(4);
+    } > FLASH
+    PROVIDE(_got_start = ADDR(.got));
+    PROVIDE(_got_end = ADDR(.got) + SIZEOF(.got));
+
+    /* reserved ram  */
+    .compal.reservedram 0x800000 (NOLOAD) : {
+        . = 0xff;
+    } > IRAM
+
+    /* initialized data */
+    .data : AT (LOADADDR(.got) + SIZEOF(.got)) {
+        . = ALIGN(4);
+        *(.data)
+        . = ALIGN(4);
+    } > IRAM
+    PROVIDE(__data_start = LOADADDR(.data));
+    PROVIDE(__data_end = LOADADDR(.data) + SIZEOF(.data));
+    PROVIDE(_data_start = ADDR(.data));
+    PROVIDE(_data_end = ADDR(.data) + SIZEOF(.data));
+
+    /* ram code */
+    .ramtext : AT (LOADADDR(.data) + SIZEOF(.data)) {
+        . = ALIGN(4);
+        *(.ramtext)
+        . = ALIGN(4);
+    } > IRAM
+    PROVIDE(__ramtext_start = LOADADDR(.ramtext));
+    PROVIDE(__ramtext_end = LOADADDR(.ramtext) + SIZEOF(.ramtext));
+    PROVIDE(_ramtext_start = ADDR(.ramtext));
+    PROVIDE(_ramtext_end = ADDR(.ramtext) + SIZEOF(.ramtext));
+
+    /* uninitialized data */
+    .bss (NOLOAD) : {
+        . = ALIGN(4);
+        *(.bss)
+        . = ALIGN(4);
+    } > IRAM
+    PROVIDE(__bss_start = ADDR(.bss));
+    PROVIDE(__bss_end = ADDR(.bss) + SIZEOF(.bss));
+    PROVIDE(_bss_start = __bss_start);
+    PROVIDE(_bss_end = __bss_end);
+
+    /* end of image */
+    . = ALIGN(4);
+    _end = .;
+    PROVIDE(end = .);
+}
diff --git a/src/target/firmware/board/compal_e86/loader.lds b/src/target/firmware/board/compal_e86/loader.lds
new file mode 100644
index 0000000..68ab4e9
--- /dev/null
+++ b/src/target/firmware/board/compal_e86/loader.lds
@@ -0,0 +1,147 @@
+/*
+ * Linker script for flashed loader on the Compal E86 (copy/modified from E88)
+ *
+ * This script creates a binary that can replace a standard firmware
+ * located at 0x2000. It works in conjunction with the compal ramloader.
+ *
+ * The interrupt vectors and start address are at known, fixed offsets.
+ *
+ */
+OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
+OUTPUT_ARCH(arm)
+ENTRY(_start)
+MEMORY
+{
+    /* 4 MBytes of external flash memory */
+    FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x400000
+    /* 256 kBytes of internal zero-waitstate sram */
+    IRAM  (rw) : ORIGIN = 0x00800000, LENGTH = 0x040000
+    /* 256 kBytes of external slow sram */
+    ERAM  (rw) : ORIGIN = 0x01000000, LENGTH = 0x040000
+}
+SECTIONS
+{
+    /* Provide symbols for the compal loader */
+    .compal.loader 0x00000000 (NOLOAD) : {
+        _compal_loader_start = .;
+        . = 0x2000;
+        _compal_loader_end = .;
+    } > FLASH
+
+    /* Compal-style image header */
+    .compal.header 0x00002000 : {
+        _compal_header_start = .;
+        KEEP(*(.compal.header))
+        *(.compal.header)
+        . = 0xA0;
+        _compal_header_end = .;
+    } > FLASH
+
+    /* Compal-style vector table */
+    .compal.vectors 0x000020A0 : {
+        PROVIDE(_exceptions = .);
+        KEEP(*(.text.exceptions))
+        *(.text.exceptions)
+    } > FLASH
+
+    /* Compal-style entry point */
+    .text.start 0x000020F8 : {
+        PROVIDE(_start = .);
+        KEEP(*(.text.start))
+        *(.text.start)
+    } > FLASH
+
+    /* code */
+    .text : {
+        /* regular code */
+        *(.text*)
+        /* gcc voodoo */
+        *(.glue_7t) *(.glue_7) *(.vfp11_veneer) *(.v4_bx)
+    } > FLASH
+    PROVIDE(_text_start = ADDR(.text));
+    PROVIDE(_text_end = ADDR(.text) + SIZEOF(.text));
+
+    /* constructor pointers */
+    .ctors : {
+        /* ctor count */
+        LONG(SIZEOF(.ctors) / 4 - 2)
+        /* ctor pointers */
+        KEEP(*(SORT(.ctors)))
+        /* end of list */
+        LONG(0)
+    } > FLASH
+    PROVIDE(_ctor_start = LOADADDR(.ctors));
+    PROVIDE(_ctor_end = LOADADDR(.ctors) + SIZEOF(.ctors));
+
+    /* destructor pointers */
+    .dtors : {
+        /* dtor count */
+        LONG(SIZEOF(.dtors) / 4 - 2)
+        /* dtor pointers */
+        KEEP(*(SORT(.dtors)))
+        /* end of list */
+        LONG(0)
+    } > FLASH
+    PROVIDE(_dtor_start = LOADADDR(.dtors));
+    PROVIDE(_dtor_end = LOADADDR(.dtors) + SIZEOF(.dtors));
+
+    /* read-only data */
+    .rodata : {
+        *(.rodata*)
+    } > FLASH
+    PROVIDE(_rodata_start = ADDR(.rodata));
+    PROVIDE(_rodata_end = ADDR(.rodata) + SIZEOF(.rodata));
+
+    /* pic offset tables */
+    .got : {
+        . = ALIGN(4);
+        *(.got)
+        *(.got.plt) *(.igot.plt) *(.got) *(.igot)
+        . = ALIGN(4);
+    } > FLASH
+    PROVIDE(_got_start = ADDR(.got));
+    PROVIDE(_got_end = ADDR(.got) + SIZEOF(.got));
+
+    /* reserved ram  */
+    .compal.reservedram 0x800000 (NOLOAD) : {
+        . = 0xff;
+    } > IRAM
+
+    /* initialized data */
+    .data : AT (LOADADDR(.got) + SIZEOF(.got)) {
+        . = ALIGN(4);
+        *(.data)
+        . = ALIGN(4);
+    } > IRAM
+    PROVIDE(__data_start = LOADADDR(.data));
+    PROVIDE(__data_end = LOADADDR(.data) + SIZEOF(.data));
+    PROVIDE(_data_start = ADDR(.data));
+    PROVIDE(_data_end = ADDR(.data) + SIZEOF(.data));
+
+    /* ram code */
+    .ramtext : AT (LOADADDR(.data) + SIZEOF(.data)) {
+        . = ALIGN(4);
+        *(.ramtext)
+        . = ALIGN(4);
+    } > IRAM
+    PROVIDE(__ramtext_start = LOADADDR(.ramtext));
+    PROVIDE(__ramtext_end = LOADADDR(.ramtext) + SIZEOF(.ramtext));
+    PROVIDE(_ramtext_start = ADDR(.ramtext));
+    PROVIDE(_ramtext_end = ADDR(.ramtext) + SIZEOF(.ramtext));
+
+    /* uninitialized data */
+    .bss (NOLOAD) : {
+        . = ALIGN(4);
+        *(.bss)
+        . = ALIGN(4);
+    } > IRAM
+    PROVIDE(__bss_start = ADDR(.bss));
+    PROVIDE(__bss_end = ADDR(.bss) + SIZEOF(.bss));
+    PROVIDE(_bss_start = __bss_start);
+    PROVIDE(_bss_end = __bss_end);
+
+    /* end of image */
+    . = ALIGN(4);
+    _end = .;
+    PROVIDE(end = .);
+}
-- 
1.7.10.4

