hikey: uefi-tools: Update UEFI tools to match updated UEFI sources

Copied over from https://github.com/96boards-hikey/uefi-tools
SHA1: 42eac07beb4da42a182d2a87d6b2e928fc9a31cf

Change-Id: Iee58d079ee565bbb83849d357caac2b21b9be043
Signed-off-by: John Stultz <john.stultz@linaro.org>
diff --git a/uefi-tools/common-functions b/uefi-tools/common-functions
index 08f4fbf..9d01ae1 100644
--- a/uefi-tools/common-functions
+++ b/uefi-tools/common-functions
@@ -4,8 +4,6 @@
 RESULT_PASS_COUNT=0
 RESULT_FAIL_COUNT=0
 
-TOOLS_DIR="`dirname $0`"
-
 function result_log
 {
 	if [ $1 -eq 0 ]; then
@@ -34,6 +32,10 @@
 	        BUILD_ARCH=ARM;;
 	    aarch64*)
 	        BUILD_ARCH=AARCH64;;
+	    i?86*)
+		BUILD_ARCH=IA32;;
+	    x86_64*)
+		BUILD_ARCH=X64;;
 	    *)
 	        BUILD_ARCH=other;;
 	esac
@@ -45,42 +47,168 @@
 
 	echo "Target: $PLATFORM_ARCH"
 	echo "Build: $BUILD_ARCH"
-	if [ "$PLATFORM_ARCH" = "$BUILD_ARCH" ]; then
+ 	if [ "$PLATFORM_ARCH" = "$BUILD_ARCH" ]; then
 	    TEMP_CROSS_COMPILE=
-	elif [ "$PLATFORM_ARCH" == "AARCH64" ]; then
-	    if [ X"$CROSS_COMPILE_64" != X"" ]; then
-	        TEMP_CROSS_COMPILE="$CROSS_COMPILE_64"
-	    else
-	        TEMP_CROSS_COMPILE=aarch64-linux-gnu-
-	    fi
-	elif [ "$PLATFORM_ARCH" == "ARM" ]; then
-	    if [ X"$CROSS_COMPILE_32" != X"" ]; then
-	        TEMP_CROSS_COMPILE="$CROSS_COMPILE_32"
-	    else
-	        TEMP_CROSS_COMPILE=arm-linux-gnueabihf-
-	    fi
 	else
-	    echo "Unsupported target architecture '$PLATFORM_ARCH'!" >&2
+	    case "$PLATFORM_ARCH" in
+		AARCH64)
+                    if [ X"$CROSS_COMPILE_64" != X"" ]; then
+                        TEMP_CROSS_COMPILE="$CROSS_COMPILE_64"
+                    else
+                        TEMP_CROSS_COMPILE=aarch64-linux-gnu-
+                    fi
+                ;;
+		ARM)
+		    TEMP_CROSS_COMPILE=arm-linux-gnueabihf- ;;
+		IA32)
+		    TEMP_CROSS_COMPILE=x86_64-linux-gnu-
+		    for family in 6 5 4 3; do
+		        if i$family86-linux-gnu-as -version >/dev/null 2>&1;then
+		            TEMP_CROSS_COMPILE=i$family86-linux-gnu-
+		            break
+		        fi
+		    done
+		;;
+		X64)
+		    TEMP_CROSS_COMPILE=x86_64-linux-gnu- ;;
+		*)
+	            echo "Unsupported target architecture '$PLATFORM_ARCH'!" >&2
+		;;
+	    esac
 	fi
 }
 
 function get_gcc_version
 {
+	$1 -v >/dev/null 2>&1 || return 1
 	gcc_version=$($1 -dumpversion)
+	MAJVER=`echo $gcc_version | cut -d. -f1`
+
 	case $gcc_version in
-		4.6*|4.7*|4.8*|4.9*)
+		4*)
 			echo GCC$(echo ${gcc_version} | awk -F. '{print $1$2}')
 			;;
 		*)
-			echo "Unknown toolchain version '$gcc_version'" >&2
-			echo "Attempting to build using GCC49 profile." >&2
-			echo GCC49
+			if [ "$MAJVER" -ge 5 ]; then
+			    # We only have a GCC5 build profile for now, so...
+			    # echo GCC$MAJVER
+			    echo GCC5
+			else
+			    echo "Unknown toolchain version '$gcc_version'" >&2
+			    echo "Attempting to build using GCC49 profile." >&2
+			    echo GCC49
+			fi
 			;;
 	esac
+
+	return 0
 }
 
 function get_clang_version
 {
 	clang_version=`$1 --version | head -1 | sed 's/^.*version\s*\([0-9]*\).\([0-9]*\).*/\1\2/g'`
-	echo "CLANG$clang_version"
+	case $clang_version in
+		35*)
+			echo "CLANG$clang_version"
+			;;
+		*)
+			echo "Unknown toolchain version '$clang_version'" >&2
+			echo "Attempting to build using CLANG35 profile." >&2
+			echo CLANG35
+			;;
+	esac
+}
+
+function download_patch_openssl
+{
+    OPENSSL_VER=`ls EDKII_openssl-*.patch | sed 's/^.*-\([0-9.a-z]*\).patch/\1/'`
+    OPENSSL_TAR=openssl-${OPENSSL_VER}.tar.gz
+    OPENSSL_URL=http://www.openssl.org/source/${OPENSSL_TAR}
+    OPENSSL_DIR=openssl-${OPENSSL_VER}
+    OPENSSL_PATCH=EDKII_openssl-${OPENSSL_VER}.patch
+
+    if [ -e "$WORKSPACE"/CryptoPkg/Include/openssl/opensslconf.h ]; then
+        echo "OpenSSL already imported!"
+        return 0
+    fi
+
+    # Use cached copy if available
+    if [ -f "$WORKSPACE"/LinaroPkg/"$OPENSSL_TAR" ]; then
+	tar xzf "$WORKSPACE"/LinaroPkg/"$OPENSSL_TAR"
+    else
+	wget -O - -q ${OPENSSL_URL} | tar xzf -
+    fi
+
+    echo "Importing OpenSSL $OPENSSL_VER"
+    ( cd ${OPENSSL_DIR}; patch -p1 -i ../${OPENSSL_PATCH} )
+    ./Install.sh
+
+    if [ $? -eq 0 ]; then
+	OPENSSL_CONFIGURED=TRUE
+    else
+	echo "OpenSSL $OPENSSL_VER import failed!" >&2
+	rm -rf $OPENSSL_TAR $OPENSSL_DIR
+	return 1
+    fi
+}
+
+function clone_process_openssl
+{
+    if [ -e openssl/include/openssl/opensslconf.h ]; then
+        echo "OpenSSL already imported!"
+        return 0
+    fi
+
+    OPENSSL_VER=`git ls-remote --tags git://github.com/openssl/openssl.git | awk '{print $2;}' | sed 's-^refs/tags/--g' | grep -v '\^{}$' | grep '^OpenSSL' | grep -v '^OpenSSL_FIPS' | tail -1 | sed -n 's/^OpenSSL_\([0-9]*\)_\([0-9]*\)_\([0-9.a-z]*\)$/openssl-\1.\2.\3\n/p'`
+    OPENSSL_TAR="$OPENSSL_VER.tar.gz"
+    if [ -z "$OPENSSL_TAR" ]; then
+        return 1
+    fi
+    OPENSSL_URL=http://www.openssl.org/source/${OPENSSL_TAR}
+    if [ ! -f "$OPENSSL_TAR" ]; then
+        wget -q ${OPENSSL_URL}
+    else
+        rm -rf openssl
+    fi
+    tar xzf "$OPENSSL_TAR"
+    mv "$OPENSSL_VER" openssl
+    # perl process_files.pl # not needed and not currently working
+    return $?
+}
+
+function import_openssl
+{
+    # Don't re-import if already done
+    if [ "$OPENSSL_CONFIGURED" = "TRUE" ]; then
+	echo "Using existing OpenSSL $OPENSSL_VER"
+	return 0
+    fi
+
+    cd CryptoPkg/Library/OpensslLib/
+    if [ -f EDKII_openssl-*.patch ]; then
+        download_patch_openssl
+    else
+        clone_process_openssl
+    fi
+    RET=$?
+    cd - >/dev/null
+    return $RET
+}
+
+function search_packages_path
+{
+    file="$1"
+
+    IFS=:
+    for dir in $PACKAGES_PATH; do
+	if [ -e "$dir/$file" ]; then
+	    echo "$dir/$file"
+	    unset IFS
+	    return 0
+	fi
+    done
+
+    echo "$file not found in any directory on PACKAGES_PATH!" >&2
+    unset IFS
+    return 1
 }