summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/checkpatch.sh42
-rw-r--r--scripts/checkpatch_inc.sh29
-rwxr-xr-xscripts/gen_hashed_bin.py148
-rw-r--r--scripts/gen_ld_rodata_sects.awk55
-rw-r--r--scripts/gen_ld_text_sects.awk55
-rw-r--r--scripts/mem_usage.awk129
-rwxr-xr-xscripts/pem_to_pub_c.py79
-rwxr-xr-xscripts/render_font.py159
-rwxr-xr-xscripts/sign.py80
-rwxr-xr-xscripts/tee_bin_parser.py68
10 files changed, 844 insertions, 0 deletions
diff --git a/scripts/checkpatch.sh b/scripts/checkpatch.sh
new file mode 100755
index 0000000..94d8c4f
--- /dev/null
+++ b/scripts/checkpatch.sh
@@ -0,0 +1,42 @@
+#!/bin/bash
+
+DIR="${BASH_SOURCE%/*}"
+
+source "$DIR/checkpatch_inc.sh"
+
+hash $CHECKPATCH 2>/dev/null ||
+ { echo >&2 "Could not find checkpatch.pl, aborting"; exit 1; }
+
+usage() {
+ SCR=$(basename "$0")
+ echo "Usage: $SCR [--working] Check working area"
+ echo " $SCR <commit> Check specific commit"
+ echo " $SCR --diff <commit1> <commit2> Check diff commit1...commit2"
+ echo " $SCR --cached Check staging area"
+ echo " $SCR --help This help"
+ exit 1
+}
+
+op=${1:---working}
+case "$op" in
+ --cached)
+ echo "Checking staging area: "
+ checkstaging
+ ;;
+ --diff)
+ echo "Checking diff (diff $1...$2)"
+ checkdiff "$2" "$3"
+ ;;
+ --working)
+ echo "Checking working area: "
+ checkworking
+ ;;
+ --help|-h)
+ usage
+ ;;
+ *)
+ echo "Checking commit: "
+ checkpatch "$1"
+ ;;
+
+esac
diff --git a/scripts/checkpatch_inc.sh b/scripts/checkpatch_inc.sh
new file mode 100644
index 0000000..487b60e
--- /dev/null
+++ b/scripts/checkpatch_inc.sh
@@ -0,0 +1,29 @@
+#!/bin/bash
+
+CHECKPATCH="${CHECKPATCH:-checkpatch.pl}"
+# checkpatch.pl will ignore the following paths
+CHECKPATCH_IGNORE=$(echo core/lib/lib{fdt,tomcrypt} lib/lib{png,utils,zlib})
+_CP_EXCL=$(for p in $CHECKPATCH_IGNORE; do echo ":(exclude)$p" ; done)
+
+function _checkpatch() {
+ $CHECKPATCH --quiet --ignore FILE_PATH_CHANGES \
+ --ignore GERRIT_CHANGE_ID --no-tree -
+}
+
+function checkpatch() {
+ git show --oneline --no-patch $1
+ git format-patch -1 $1 --stdout -- $_CP_EXCL . | _checkpatch
+}
+
+function checkstaging() {
+ git diff --cached -- . $_CP_EXCL | _checkpatch
+}
+
+function checkworking() {
+ git diff -- . $_CP_EXCL | _checkpatch
+}
+
+function checkdiff() {
+ git diff $1...$2 -- . $_CP_EXCL | _checkpatch
+}
+
diff --git a/scripts/gen_hashed_bin.py b/scripts/gen_hashed_bin.py
new file mode 100755
index 0000000..e39cc13
--- /dev/null
+++ b/scripts/gen_hashed_bin.py
@@ -0,0 +1,148 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2014, Linaro Limited
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+import argparse
+import sys
+import shutil
+import os
+import struct
+import hashlib
+
+arch_id = {'arm32': 0, 'arm64': 1}
+
+def write_header(outf, init_size, args, paged_size):
+ magic = 0x4554504f # 'OPTE'
+ version = 1;
+ outf.write(struct.pack('<IBBHIIIII', \
+ magic, version, arch_id[args.arch], args.flags, init_size, \
+ args.init_load_addr_hi, args.init_load_addr_lo, \
+ args.init_mem_usage, paged_size))
+
+
+def append_to(outf, start_offs, in_fname, max_bytes=0xffffffff):
+ #print "Appending %s@0x%x 0x%x bytes at position 0x%x" % \
+ #( in_fname, start_offs, max_bytes, int(outf.tell()) )
+ inf = open(in_fname, 'rb');
+ inf.seek(start_offs)
+ while True :
+ nbytes = min(16 * 1024, max_bytes)
+ if nbytes == 0 :
+ break
+ #print "Reading %s %d bytes" % (in_fname, nbytes)
+ buf = inf.read(nbytes)
+ if not buf :
+ break
+ outf.write(buf)
+ max_bytes -= len(buf)
+ inf.close()
+
+def append_hashes(outf, in_fname):
+ page_size = 4 * 1024
+
+ inf = open(in_fname, 'r')
+ while True :
+ page = inf.read(page_size)
+ if len(page) == page_size :
+ #print "Writing hash at position 0x%x" % \
+ #int(outf.tell())
+ outf.write(hashlib.sha256(page).digest())
+ elif len(page) == 0 :
+ break
+ else :
+ print("Error: short read, got " + repr(len(page)))
+ sys.exit(1)
+
+ inf.close()
+
+def int_parse(str):
+ return int(str, 0)
+
+def get_args():
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--arch', required=True, \
+ choices=arch_id.keys(), \
+ help='Architecture')
+
+ parser.add_argument('--flags', \
+ type=int, default=0, \
+ help='Flags, currently none defined')
+
+ parser.add_argument('--init_size', \
+ required=True, type=int_parse, \
+ help='Size of initialization part of binary')
+
+ parser.add_argument('--init_load_addr_hi', \
+ type=int_parse, default=0, \
+ help='Upper 32 bits of load address of binary')
+
+ parser.add_argument('--init_load_addr_lo', \
+ required=True, type=int_parse, \
+ help='Lower 32 bits of load address of binary')
+
+ parser.add_argument('--init_mem_usage', \
+ required=True, type=int_parse, \
+ help='Total amount of used memory when initializing');
+
+ parser.add_argument('--tee_pager_bin', \
+ required=True, \
+ help='The input tee_pager.bin')
+
+ parser.add_argument('--tee_pageable_bin', \
+ required=True, \
+ help='The input tee_pageable.bin')
+
+ parser.add_argument('--out', \
+ required=True, type=argparse.FileType('wb'), \
+ help='The output tee.bin')
+
+ return parser.parse_args();
+
+def main():
+ args = get_args()
+ init_bin_size = args.init_size
+ tee_pager_fname = args.tee_pager_bin
+ tee_pageable_fname = args.tee_pageable_bin
+ outf = args.out
+
+ write_header(outf, 0, args, 0)
+ header_size = outf.tell();
+ append_to(outf, 0, tee_pager_fname)
+ append_to(outf, 0, tee_pageable_fname, init_bin_size)
+ append_hashes(outf, tee_pageable_fname)
+ init_size = outf.tell() - header_size;
+ append_to(outf, init_bin_size, tee_pageable_fname)
+ paged_size = outf.tell() - init_size - header_size;
+
+ outf.seek(0)
+ #print "init_size 0x%x" % init_size
+ write_header(outf, init_size, args, paged_size)
+
+ outf.close()
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/gen_ld_rodata_sects.awk b/scripts/gen_ld_rodata_sects.awk
new file mode 100644
index 0000000..04e5992
--- /dev/null
+++ b/scripts/gen_ld_rodata_sects.awk
@@ -0,0 +1,55 @@
+#
+# Copyright (c) 2014, Linaro Limited
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+BEGIN {
+ in_shdr = 0;
+}
+
+/Section Headers:/ {
+ in_shdr = 1;
+ next;
+}
+
+/Key to Flags:/ {
+ in_shdr = 0;
+ next;
+}
+
+{
+ if (in_shdr) {
+ if ($1 == "[")
+ name_offs = 3;
+ else
+ name_offs = 2;
+
+ name = $(name_offs);
+ type = $(name_offs + 1);
+ flags = $(name_offs + 6);
+ if (name ~ /^\.rodata\./ && type == "PROGBITS")
+ printf "\t*(%s)\n", name;
+ }
+}
diff --git a/scripts/gen_ld_text_sects.awk b/scripts/gen_ld_text_sects.awk
new file mode 100644
index 0000000..8b25ff7
--- /dev/null
+++ b/scripts/gen_ld_text_sects.awk
@@ -0,0 +1,55 @@
+#
+# Copyright (c) 2014, Linaro Limited
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+BEGIN {
+ in_shdr = 0;
+}
+
+/Section Headers:/ {
+ in_shdr = 1;
+ next;
+}
+
+/Key to Flags:/ {
+ in_shdr = 0;
+ next;
+}
+
+{
+ if (in_shdr) {
+ if ($1 == "[")
+ name_offs = 3;
+ else
+ name_offs = 2;
+
+ name = $(name_offs);
+ type = $(name_offs + 1);
+ flags = $(name_offs + 6);
+ if (name ~ /^\.text\./ && type == "PROGBITS")
+ printf "\t*(%s)\n", name;
+ }
+}
diff --git a/scripts/mem_usage.awk b/scripts/mem_usage.awk
new file mode 100644
index 0000000..c5a8478
--- /dev/null
+++ b/scripts/mem_usage.awk
@@ -0,0 +1,129 @@
+#
+# Copyright (c) 2014, Linaro Limited
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+BEGIN {
+ in_shdr = 0;
+ num_sects = 0;
+}
+
+/Section Headers:/ {
+ in_shdr = 1;
+ next;
+}
+
+/Key to Flags:/ {
+ in_shdr = 0;
+ next;
+}
+
+function add_section(_name, _addr, _offs, _size)
+{
+ sects[num_sects]["name"] = _name;
+ sects[num_sects]["addr"] = strtonum("0x" _addr);
+ sects[num_sects]["offs"] = strtonum("0x" _offs);
+ sects[num_sects]["size"] = strtonum("0x" _size);
+ num_sects++;
+}
+
+{
+ if (in_shdr) {
+ if ($1 == "[")
+ name_offs = 3;
+ else
+ name_offs = 2;
+
+ name = $(name_offs);
+ addr = $(name_offs + 2);
+ offs = $(name_offs + 3);
+ size = $(name_offs + 4);
+ flags = $(name_offs + 6);
+
+ if (flags == "AX" || flags == "WA" || flags == "A" ||
+ flags == "AL") {
+ add_section(name, addr, offs, size);
+ }
+ }
+}
+
+function print_sect(_name, _addr, _size, _round_up, _print_num_pages,
+ #local variables
+ _size_kib, _num_pages)
+{
+ if (_size == 0) {
+ _size_kib = 0;
+ _num_pages = 0;
+ } else {
+ if (_round_up) {
+ _size_kib = (_size - 1) / 1024 + 1;
+ } else {
+ _size_kib = _size / 1024;
+ }
+ _num_pages = (_size - 1) / 4096 + 1;
+ }
+
+
+
+ printf "%-16s", _name;
+ printf " %.8X - %.8X", _addr, _addr + _size;
+ printf " size %.8X %3d KiB", _size, _size_kib
+
+ if (_print_num_pages)
+ printf " %d pages", _num_pages;
+ printf "\n";
+}
+
+END {
+ for (i = 0; i < num_sects; i++ ) {
+ addr = sects[i]["addr"];
+
+ if (addr != 0) {
+ first_addr = addr;
+ break;
+ }
+ }
+
+ last_addr = sects[num_sects - 1]["addr"];
+ last_size = sects[num_sects - 1]["size"];
+ ram_usage = last_addr + last_size - first_addr;
+ print_sect("RAM Usage", first_addr, ram_usage, 1, 1);
+
+ last_addr = 0;
+ last_size = 0;
+
+ for (i = 0; i < num_sects; i++ ) {
+ name = sects[i]["name"];
+ addr = sects[i]["addr"];
+ size = sects[i]["size"];
+
+ if (last_addr != 0 && addr != last_addr + last_size)
+ print_sect("*hole*", last_addr + last_size,
+ addr - (last_addr + last_size), 0, 0);
+ print_sect(name, addr, size, 0, 0);
+ last_addr = addr;
+ last_size = size;
+ }
+}
diff --git a/scripts/pem_to_pub_c.py b/scripts/pem_to_pub_c.py
new file mode 100755
index 0000000..47c004d
--- /dev/null
+++ b/scripts/pem_to_pub_c.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2015, Linaro Limited
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+def get_args():
+ import argparse
+
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--prefix', required=True, \
+ help='Prefix for the public key exponent and modulus in c file')
+
+ parser.add_argument('--out', required=True, \
+ help='Name of c file for the public key')
+
+ parser.add_argument('--key', required=True, help='Name of key file')
+
+ return parser.parse_args()
+
+def main():
+ import array
+ from Crypto.PublicKey import RSA
+ from Crypto.Util.number import long_to_bytes
+
+ args = get_args();
+
+ f = open(args.key, 'r')
+ key = RSA.importKey(f.read())
+ f.close
+
+ f = open(args.out, 'w')
+
+ f.write("#include <stdint.h>\n");
+ f.write("#include <stddef.h>\n\n");
+
+ f.write("const uint32_t " + args.prefix + "_exponent = " +
+ str(key.publickey().e) + ";\n\n")
+
+ f.write("const uint8_t " + args.prefix + "_modulus[] = {\n")
+ i = 0;
+ for x in array.array("B", long_to_bytes(key.publickey().n)):
+ f.write("0x" + '{0:02x}'.format(x) + ",")
+ i = i + 1;
+ if i % 8 == 0:
+ f.write("\n");
+ else:
+ f.write(" ");
+ f.write("};\n");
+
+ f.write("const size_t " + args.prefix + "_modulus_size = sizeof(" + \
+ args.prefix + "_modulus);\n")
+
+ f.close()
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/render_font.py b/scripts/render_font.py
new file mode 100755
index 0000000..c6e5510
--- /dev/null
+++ b/scripts/render_font.py
@@ -0,0 +1,159 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2016, Linaro Limited
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+from wand.image import Image
+from wand.drawing import Drawing
+from wand.color import Color
+import sys
+
+def get_args():
+ from argparse import ArgumentParser
+
+ parser = ArgumentParser()
+ parser.add_argument('--font_file', required=True, \
+ help='Name of font file')
+ parser.add_argument('--font_size', type=int, default=40, \
+ help='Size of font')
+ parser.add_argument('--font_name', required=True, \
+ help='Name of font in program')
+ parser.add_argument('--out_dir', required=True, \
+ help='Out directory')
+ parser.add_argument('--verbose', default=False, \
+ help='Print informational messages')
+ return parser.parse_args()
+
+def c_hex_print(f, str):
+ n = 0
+ s = ""
+ for x in str:
+ if n % 8 == 0:
+ s += "\t"
+ else:
+ s += " "
+ # Hack to satisfy both Python 2.x and 3.x. In 2.x the variable x
+ # is string, while it in Python 3.x it's considered as a class
+ # int.
+ if sys.version_info > (3, 0):
+ s += "0x%02x," % x
+ else:
+ s += "0x%02x," % ord(x)
+ n = n + 1
+ if n % 8 == 0:
+ f.write(s + "\n")
+ s = ""
+ if n % 8 != 0:
+ f.write(s + "\n")
+ s = ""
+
+def write_comment(f):
+ f.write("/*\n * This file is auto generated with\n")
+ f.write(" *")
+ for x in sys.argv:
+ f.write(" " + x);
+ f.write("\n * do not edit.\n */\n")
+
+
+def main():
+ args = get_args()
+
+ draw = Drawing()
+ draw.font = args.font_file
+ draw.font_size = args.font_size
+
+ font_name = args.font_name
+ out_dir = args.out_dir
+
+ img_ref = Image(width=1000, height=1000)
+
+ if args.verbose:
+ print("Writing " + out_dir + "/" + font_name + ".c")
+ f = open(out_dir + "/" + font_name + ".c", 'w+')
+ write_comment(f)
+ f.write("#include \"font.h\"\n\n")
+
+ font_height = 0
+ range_first = 0x20
+ range_last = 0x7d
+ font_width = []
+ max_width = 0
+ for x in range(range_first, range_last + 1):
+ letter = chr(x)
+ metrics = draw.get_font_metrics(img_ref, letter)
+ text_height = int(round(metrics.text_height + 2))
+ if font_height == 0:
+ font_height = text_height
+ assert (font_height == text_height), "font height changed!"
+ if max_width == 0:
+ max_width = metrics.maximum_horizontal_advance + 2
+ assert (max_width == metrics.maximum_horizontal_advance + 2), \
+ "font advance width changed!"
+ text_width = int(round(metrics.text_width + 2))
+ font_width.append(text_width)
+ img = Image(width=text_width, height=text_height)
+ d = draw.clone()
+ d.text(0, int(metrics.ascender), letter)
+ d(img)
+
+ img.depth = 1;
+
+ f.write("static const unsigned char ")
+ f.write("letter_" + str(hex(x)[2:]) + "[] = {\n")
+ c_hex_print(f, img.make_blob(format='A'))
+ f.write("};\n\n")
+ img.close()
+
+ f.write("static const struct font_letter letters[] = {\n")
+ for x in range(range_first, range_last + 1):
+ letter_var_name = "letter_" + str(hex(x)[2:])
+ f.write("\t{ " + letter_var_name + ", ")
+ f.write("sizeof(" + letter_var_name + "), ")
+ f.write(str(font_width[x - range_first]) + "},\n")
+ f.write("};\n\n")
+
+ f.write("const struct font font_" + font_name + " = {\n")
+ f.write("\t.first = " + str(hex(range_first)) + ",\n")
+ f.write("\t.last = " + str(hex(range_last)) + ",\n")
+ f.write("\t.letters = letters,\n")
+ f.write("\t.height = " + str(font_height) + ",\n")
+ f.write("\t.max_width = " + str(max_width) + ",\n")
+ f.write("};\n")
+ f.close()
+
+ if args.verbose:
+ print("Writing " + out_dir + "/" + font_name + ".h")
+ f = open(out_dir + "/" + font_name + ".h", 'w+')
+ write_comment(f)
+ f.write("#ifndef __" + font_name.upper() + "_H\n");
+ f.write("#define __" + font_name.upper() + "_H\n");
+ f.write("#include \"font.h\"\n")
+ f.write("extern const struct font font_" + font_name + ";\n")
+ f.write("#endif /*__" + font_name.upper() + "_H*/\n");
+ f.close()
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/sign.py b/scripts/sign.py
new file mode 100755
index 0000000..f407f3b
--- /dev/null
+++ b/scripts/sign.py
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2015, Linaro Limited
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+def get_args():
+ from argparse import ArgumentParser
+
+ parser = ArgumentParser()
+ parser.add_argument('--key', required=True, help='Name of key file')
+ parser.add_argument('--in', required=True, dest='inf', \
+ help='Name of in file')
+ parser.add_argument('--out', required=True, help='Name of out file')
+ return parser.parse_args()
+
+def main():
+ from Crypto.Signature import PKCS1_v1_5
+ from Crypto.Hash import SHA256
+ from Crypto.PublicKey import RSA
+ import struct
+
+ args = get_args()
+
+ f = open(args.key, 'rb')
+ key = RSA.importKey(f.read())
+ f.close()
+
+ f = open(args.inf, 'rb')
+ img = f.read()
+ f.close()
+
+ signer = PKCS1_v1_5.new(key)
+ h = SHA256.new()
+
+ digest_len = h.digest_size
+ sig_len = len(signer.sign(h))
+ img_size = len(img)
+
+ magic = 0x4f545348 # SHDR_MAGIC
+ img_type = 0 # SHDR_TA
+ algo = 0x70004830 # TEE_ALG_RSASSA_PKCS1_V1_5_SHA256
+ shdr = struct.pack('<IIIIHH', \
+ magic, img_type, img_size, algo, digest_len, sig_len)
+
+ h.update(shdr)
+ h.update(img)
+ sig = signer.sign(h)
+
+ f = open(args.out, 'wb')
+ f.write(shdr)
+ f.write(h.digest())
+ f.write(sig)
+ f.write(img)
+ f.close()
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/tee_bin_parser.py b/scripts/tee_bin_parser.py
new file mode 100755
index 0000000..e2031cc
--- /dev/null
+++ b/scripts/tee_bin_parser.py
@@ -0,0 +1,68 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2016, Linaro Limited
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+import struct
+
+def main():
+ with open ("../out/arm/core/tee.bin", "rb") as f:
+ data = f.read(4)
+ magic = struct.unpack('<I', data)
+ print("Magic: \t\t0x%08x" % magic)
+
+ data = f.read(1)
+ version = struct.unpack('<B', data)
+ print("Version: \t0x%02x" % version)
+
+ data = f.read(1)
+ arch_id = struct.unpack('<B', data)
+ print("ArchID: \t0x%02x" % arch_id)
+
+ data = f.read(2)
+ flags = struct.unpack('<H', data)
+ print("Arch Flags: \t0x%04x" % arch_id)
+
+ data = f.read(4)
+ init_size = struct.unpack('<I', data)
+ print("Init size: \t0x%04x" % init_size)
+
+ data = f.read(4)
+ laddr_h = struct.unpack('<I', data)
+ print("Load addr high:\t0x%04x" % laddr_h)
+
+ data = f.read(4)
+ laddr_l = struct.unpack('<I', data)
+ print("Load addr low: \t0x%04x" % laddr_l)
+
+ data = f.read(4)
+ mem_usage = struct.unpack('<I', data)
+ print("Mem usage: \t0x%04x" % mem_usage)
+
+ data = f.read(4)
+ pgd_size = struct.unpack('<I', data)
+ print("Pages size: \t0x%04x" % pgd_size)
+
+if __name__ == "__main__":
+ main()