summaryrefslogtreecommitdiff
path: root/lib/romlib
diff options
context:
space:
mode:
authorRoberto Vargas <roberto.vargas@arm.com>2018-05-22 16:05:42 +0100
committerRoberto Vargas <roberto.vargas@arm.com>2018-08-03 11:31:42 +0100
commit5accce5bcc211c08e80c638acbd01099af81cc37 (patch)
tree923fe8d7c1cd69eccef6eeb9ed5c875f65f9b5c8 /lib/romlib
parent6c3733456706809d5c9fb78a9746bf2fa484fb91 (diff)
Add support for romlib in the build system
Romlib is a new image that is stored in ROM and contains the code of several libraries that can be shared between different images. All the functions within in the library are accessed using a jump table which allows to update the romlib image whithout changing the binary compatibility. This jump table can be also stored in RAM and it can allow to patch a romlib with potential bugs fixes.. Change-Id: If980ccdaca24b7aaca900e32acc68baf6f94ab35 Signed-off-by: Roberto Vargas <roberto.vargas@arm.com>
Diffstat (limited to 'lib/romlib')
-rw-r--r--lib/romlib/Makefile71
-rwxr-xr-xlib/romlib/gentbl.sh40
-rwxr-xr-xlib/romlib/genvar.sh36
-rwxr-xr-xlib/romlib/genwrappers.sh52
-rw-r--r--lib/romlib/init.s30
-rw-r--r--lib/romlib/jmptbl.i35
-rw-r--r--lib/romlib/romlib.ld.S44
7 files changed, 308 insertions, 0 deletions
diff --git a/lib/romlib/Makefile b/lib/romlib/Makefile
new file mode 100644
index 00000000..46b92068
--- /dev/null
+++ b/lib/romlib/Makefile
@@ -0,0 +1,71 @@
+#
+# Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+AS = $(CROSS_COMPILE)as
+LD = $(CROSS_COMPILE)ld
+OC = $(CROSS_COMPILE)objcopy
+CPP = $(CROSS_COMPILE)cpp
+BUILD_DIR = ../../$(BUILD_PLAT)/romlib
+LIB_DIR = ../../$(BUILD_PLAT)/lib
+WRAPPER_DIR = ../../$(BUILD_PLAT)/libwrapper
+LIBS = -lmbedtls -lfdt -lc
+INC = $(INCLUDES:-I%=-I../../%)
+PPFLAGS = $(INC) $(DEFINES) -P -D__ASSEMBLY__ -D__LINKER__ -MD -MP -MT $(BUILD_DIR)/romlib.ld
+OBJS = $(BUILD_DIR)/jmptbl.o $(BUILD_DIR)/init.o
+
+V ?= 0
+ifeq ($(V),0)
+ Q := @
+else
+ Q :=
+endif
+
+ifeq ($(DEBUG),1)
+ CFLAGS := -g
+ LDFLAGS := -g
+endif
+
+
+.PHONY: all clean distclean
+
+all: $(BUILD_DIR)/romlib.bin $(LIB_DIR)/libwrappers.a
+
+%.o: %.s
+ @echo " AS $@"
+ $(Q)$(AS) $(ASFLAGS) -o $@ $<
+
+$(BUILD_DIR)/%.o: %.s
+ @echo " AS $@"
+ $(Q)$(AS) $(ASFLAGS) -o $@ $<
+
+$(BUILD_DIR)/romlib.ld: romlib.ld.S
+ @echo " PP $@"
+ $(Q)$(CPP) $(PPFLAGS) -o $@ romlib.ld.S
+
+$(BUILD_DIR)/romlib.elf: $(OBJS) $(BUILD_DIR)/romlib.ld
+ @echo " LD $@"
+ $(Q)$(LD) -T $(BUILD_DIR)/romlib.ld -L$(LIB_DIR) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
+
+$(BUILD_DIR)/romlib.bin: $(BUILD_DIR)/romlib.elf
+ @echo " BIN $@"
+ $(Q)$(OC) -O binary $(BUILD_DIR)/romlib.elf $@
+
+$(WRAPPER_DIR)/jmpvar.s: $(BUILD_DIR)/romlib.elf
+ @echo " VAR $@"
+ $(Q)./genvar.sh -o $@ $(BUILD_DIR)/romlib.elf
+
+$(LIB_DIR)/libwrappers.a: jmptbl.i $(WRAPPER_DIR)/jmpvar.o
+ @echo " AR $@"
+ $(Q)./genwrappers.sh -b $(WRAPPER_DIR) -o $@ jmptbl.i
+
+$(BUILD_DIR)/jmptbl.s: jmptbl.i
+ @echo " TBL $@"
+ $(Q)./gentbl.sh -o $@ jmptbl.i
+
+clean:
+ @rm -f $(BUILD_DIR)/*
+
+-include $(BUILD_DIR)/romlib.d
diff --git a/lib/romlib/gentbl.sh b/lib/romlib/gentbl.sh
new file mode 100755
index 00000000..0695f6e4
--- /dev/null
+++ b/lib/romlib/gentbl.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+# Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+set -e
+
+output=jmptbl.s
+
+for i
+do
+ case $i in
+ -o)
+ output=$2
+ shift 2
+ ;;
+ --)
+ shift
+ break
+ ;;
+ -*)
+ echo usage: gentbl.sh [-o output] file ... >&2
+ exit 1
+ ;;
+ esac
+done
+
+tmp=`mktemp`
+trap "rm -f $tmp" EXIT INT QUIT
+
+rm -f $output
+
+awk -v OFS="\n" '
+BEGIN {print "\t.text",
+ "\t.globl\tjmptbl",
+ "jmptbl:"}
+ {sub(/[:blank:]*#.*/,"")}
+!/^$/ {print "\tb\t" $3}' "$@" > $tmp
+
+mv $tmp $output
diff --git a/lib/romlib/genvar.sh b/lib/romlib/genvar.sh
new file mode 100755
index 00000000..a3e2cdf6
--- /dev/null
+++ b/lib/romlib/genvar.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+# Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+set -e
+
+output=jmpvar.s
+for i
+do
+ case $i in
+ -o)
+ output=$2
+ shift 2
+ ;;
+ --)
+ shift
+ break
+ ;;
+ -*)
+ echo usage: genvar.sh [-o output] file... >&2
+ ;;
+ esac
+done
+
+tmp=`mktemp`
+trap "rm -f $tmp" EXIT INT QUIT
+
+nm -a "$@" |
+awk -v OFS="\n" '
+$3 == ".text" {print "\t.data",
+ "\t.globl\tjmptbl",
+ "\t.align\t4",
+ "jmptbl:\t.quad\t0x" $1}' > $tmp
+
+mv $tmp $output
diff --git a/lib/romlib/genwrappers.sh b/lib/romlib/genwrappers.sh
new file mode 100755
index 00000000..bcf670b9
--- /dev/null
+++ b/lib/romlib/genwrappers.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+# Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+set -e
+
+build=.
+out=output.a
+
+for i
+do
+ case $i in
+ -o)
+ out=$2
+ shift 2
+ ;;
+ -b)
+ build=$2
+ shift 2
+ ;;
+ --)
+ shift
+ break
+ ;;
+ -*)
+ echo usage: genwrappers.sh [-o output] [-b dir] file ... >&2
+ exit 1
+ ;;
+ esac
+done
+
+awk '{sub(/[:blank:]*#.*/,"")}
+!/^$/ {print $1*4, $2, $3}' "$@" |
+while read idx lib sym
+do
+ file=$build/${lib}_$sym
+
+ cat <<EOF > $file.s
+ .globl $sym
+$sym:
+ ldr x17, =jmptbl
+ ldr x17, [x17]
+ mov x16, $idx
+ add x16, x16, x17
+ br x16
+EOF
+
+ ${CROSS_COMPILE}as -o $file.o $file.s
+done
+
+${CROSS_COMPILE}ar -rc $out $build/*.o
diff --git a/lib/romlib/init.s b/lib/romlib/init.s
new file mode 100644
index 00000000..5cf2aca0
--- /dev/null
+++ b/lib/romlib/init.s
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+ .globl rom_lib_init
+ .extern __DATA_RAM_START__, __DATA_ROM_START__, __DATA_SIZE__
+ .extern memset, memcpy
+
+rom_lib_init:
+ cmp w0, #1
+ mov w0, #0
+ b.le 1f
+ ret
+
+1: stp x29, x30, [sp, #-16]!
+ adrp x0, __DATA_RAM_START__
+ ldr x1,= __DATA_ROM_START__
+ ldr x2, =__DATA_SIZE__
+ bl memcpy
+
+ ldr x0, =__BSS_START__
+ mov x1, #0
+ ldr x2, =__BSS_SIZE__
+ bl memset
+ ldp x29, x30, [sp], #16
+
+ mov w0, #1
+ ret
diff --git a/lib/romlib/jmptbl.i b/lib/romlib/jmptbl.i
new file mode 100644
index 00000000..338cd8a7
--- /dev/null
+++ b/lib/romlib/jmptbl.i
@@ -0,0 +1,35 @@
+#
+# Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+0 rom rom_lib_init
+1 fdt fdt_getprop_namelen
+2 fdt fdt_setprop_inplace
+3 fdt fdt_check_header
+4 fdt fdt_node_offset_by_compatible
+5 mbedtls mbedtls_asn1_get_alg
+6 mbedtls mbedtls_asn1_get_alg_null
+7 mbedtls mbedtls_asn1_get_bitstring_null
+8 mbedtls mbedtls_asn1_get_bool
+9 mbedtls mbedtls_asn1_get_int
+10 mbedtls mbedtls_asn1_get_tag
+11 mbedtls mbedtls_free
+12 mbedtls mbedtls_md
+13 mbedtls mbedtls_md_get_size
+14 mbedtls mbedtls_memory_buffer_alloc_init
+15 mbedtls mbedtls_oid_get_md_alg
+16 mbedtls mbedtls_oid_get_numeric_string
+17 mbedtls mbedtls_oid_get_pk_alg
+18 mbedtls mbedtls_oid_get_sig_alg
+19 mbedtls mbedtls_pk_free
+20 mbedtls mbedtls_pk_init
+21 mbedtls mbedtls_pk_parse_subpubkey
+22 mbedtls mbedtls_pk_verify_ext
+23 mbedtls mbedtls_platform_set_snprintf
+24 mbedtls mbedtls_x509_get_rsassa_pss_params
+25 mbedtls mbedtls_x509_get_sig_alg
+26 mbedtls mbedtls_md_info_from_type
+27 c exit
+28 c atexit
diff --git a/lib/romlib/romlib.ld.S b/lib/romlib/romlib.ld.S
new file mode 100644
index 00000000..8f0bc62b
--- /dev/null
+++ b/lib/romlib/romlib.ld.S
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <platform_def.h>
+#include <xlat_tables_defs.h>
+
+MEMORY {
+ ROM (rx): ORIGIN = ROMLIB_RO_BASE, LENGTH = ROMLIB_RO_LIMIT - ROMLIB_RO_BASE
+ RAM (rwx): ORIGIN = ROMLIB_RW_BASE, LENGTH = ROMLIB_RW_END - ROMLIB_RW_BASE
+}
+
+OUTPUT_FORMAT(PLATFORM_LINKER_FORMAT)
+OUTPUT_ARCH(PLATFORM_LINKER_ARCH)
+ENTRY(jmptbl)
+
+SECTIONS
+{
+ . = ROMLIB_RO_BASE;
+ .text : {
+ *jmptbl.o(.text)
+ *(.text*)
+ *(.rodata*)
+ } >ROM
+
+ __DATA_ROM_START__ = LOADADDR(.data);
+
+ .data : {
+ __DATA_RAM_START__ = .;
+ *(.data*)
+ __DATA_RAM_END__ = .;
+ } >RAM AT>ROM
+
+ __DATA_SIZE__ = SIZEOF(.data);
+
+ .bss : {
+ __BSS_START__ = .;
+ *(.bss*)
+ __BSS_END__ = .;
+ } >RAM
+ __BSS_SIZE__ = SIZEOF(.bss);
+}