FIH: Refine the usage of FIH tool

1. Make it skip the build if tfm_s.axf exists. Users can alse
   re-build the source code if necessary.
2. Add the support of certain function test

Signed-off-by: Jianliang Shen <jianliang.shen@arm.com>
Change-Id: Id99cf6d9f65da3ea7fe8c3b167cd06ac53d053b9
diff --git a/fih_test_tool/Readme.rst b/fih_test_tool/Readme.rst
index 646b0bf..0209219 100644
--- a/fih_test_tool/Readme.rst
+++ b/fih_test_tool/Readme.rst
@@ -75,7 +75,13 @@
 cd <TFM_DIR>
 mkdir build
 cd build
-<Path to>/fih_test -f LOW
+<Path to>/fih_test -p LOW
+
+# Test with certain function
+<Path to>/fih_test -p LOW -l 2 -f "tfm_hal_set_up_static_boundaries"
+
+# Build the AXF file again if the source code has been changed
+<Path to>/fih_test -p LOW -l 2 -r
 ```
 
 Fault types
diff --git a/fih_test_tool/fih_test b/fih_test_tool/fih_test
index dafdf53..953f4af 100755
--- a/fih_test_tool/fih_test
+++ b/fih_test_tool/fih_test
@@ -1,11 +1,11 @@
 #!/usr/bin/env bash
-# Copyright (c) 2021, Arm Limited. All rights reserved.
+# Copyright (c) 2021-2022, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 
 usage()
 {
-    echo "$0 [-s source_dir] [-d build_dir] [-b <build_type>] [-c <compiler>] [-f <fih_profile>] [-l <tfm_level>]"
+    echo "$0 [-s source_dir] [-d build_dir] [-b <build_type>] [-c <compiler>] [-p <fih_profile>] [-l <tfm_level>] [-f <function>] [-r]"
 }
 
 # Parse arguments
@@ -31,7 +31,7 @@
         shift
         shift
         ;;
-        -f|--fih_profile)
+        -p|--fih_profile)
         FIH_PROFILE="$2"
         shift
         shift
@@ -41,6 +41,16 @@
         shift
         shift
         ;;
+        -f|--function)
+        FUNCTION="$2"
+        shift
+        shift
+        ;;
+        -r|--re-build)
+        RE_BUILD=1
+        shift
+        shift
+        ;;
         -h|--help)
         usage
         exit 0
diff --git a/fih_test_tool/fih_test_build_tfm.sh b/fih_test_tool/fih_test_build_tfm.sh
index d755853..d2961d5 100644
--- a/fih_test_tool/fih_test_build_tfm.sh
+++ b/fih_test_tool/fih_test_build_tfm.sh
@@ -13,19 +13,22 @@
 set -e
 
 mkdir -p ${BUILD_DIR}
-pushd ${SOURCE_DIR}
-cmake -S . -B ${BUILD_DIR} \
-    -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-    -DTFM_TOOLCHAIN_FILE=toolchain_${COMPILER}.cmake \
-    -DTFM_PLATFORM=mps2/an521 \
-    -DDEBUG_AUTHENTICATION=FULL \
-    -DTFM_ISOLATION_LEVEL=${TFM_LEVEL} \
-    -DTFM_FIH_PROFILE=${FIH_PROFILE}
-popd
 
-pushd ${BUILD_DIR}
-make clean
-make -j install
-popd
+if [ ! -f ${BUILD_DIR}/bin/tfm_s.axf ] || [ $RE_BUILD -eq 1 ];then
+    pushd ${SOURCE_DIR}
+    cmake -S . -B ${BUILD_DIR} \
+        -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
+        -DTFM_TOOLCHAIN_FILE=toolchain_${COMPILER}.cmake \
+        -DTFM_PLATFORM=mps2/an521 \
+        -DDEBUG_AUTHENTICATION=FULL \
+        -DTFM_ISOLATION_LEVEL=${TFM_LEVEL} \
+        -DTFM_FIH_PROFILE=${FIH_PROFILE}
+    popd
+
+    pushd ${BUILD_DIR}
+    make clean
+    make -j install
+    popd
+fi
 
 set +e
diff --git a/fih_test_tool/fih_test_make_manifest.sh b/fih_test_tool/fih_test_make_manifest.sh
index 3b42747..d1ba248 100644
--- a/fih_test_tool/fih_test_make_manifest.sh
+++ b/fih_test_tool/fih_test_make_manifest.sh
@@ -1,5 +1,5 @@
 #!/usr/bin/env bash
-# Copyright (c) 2021, Arm Limited. All rights reserved.
+# Copyright (c) 2021-2022, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 
@@ -34,4 +34,34 @@
 
 # Output in CSV format with a label
 echo "Address, Type" > ${BUILD_DIR}/fih_manifest.csv
-echo "$ADDRESSES"   >> ${BUILD_DIR}/fih_manifest.csv
+
+# Dump all objects that have a name containing FUNCTION
+if [ -n "$FUNCTION" ];then
+    if [ -n "$(echo "$ADDRESSES" | grep "$FUNCTION")" ];then
+        IFS_OLD=$IFS
+        IFS=$'\n'
+        next_line=0
+        for line in $ADDRESSES;
+        do
+            if [ -n "$(echo "$line" | grep "CRITICAL_POINT")" ] || [ -n "$(echo "$line" | grep "FAILURE_LOOP")" ];then
+                echo "$line" >> ${BUILD_DIR}/fih_manifest.csv
+                continue
+            fi
+            if [ -n "$(echo "$line" | grep "$FUNCTION")" ];then
+                echo "$line" >> ${BUILD_DIR}/fih_manifest.csv
+                next_line=1
+                continue
+            fi
+            if [ $next_line -eq 1 ];then
+                echo "$line" >> ${BUILD_DIR}/fih_manifest.csv
+                next_line=0
+            fi
+        done
+        IFS=$IFS_OLD
+    else
+        echo "Function $FUNCTION is not found in TF-M!"
+        exit
+    fi
+else
+    echo "$ADDRESSES" >> ${BUILD_DIR}/fih_manifest.csv
+fi