Tools: Generate SPM configurations in script

The manifest tool has two places (config_impl.h and config_impl.cmake)
to check SFN/IPC Partitions numbers.
This doubles the maintainance effort.

This patch moves those checks forward to the manifest script.
This patch also adds back all the SPM configurations to config_impl.h
so that all sources need them only have to include the head file but
no need to add compiler definitions.

This patch removes the backend.c and builds the backend_ipc/sfn.c
according to the configurations.

Change-Id: I8da72bccc547df6ee4b2503869daf490b04bcf8b
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 5dfcaee..7bb5a00 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -133,6 +133,7 @@
     COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tfm_parse_manifest_list.py
                                   -m ${COMBINED_LIST}
                                   -f ${GENERATED_FILE_LISTS}
+                                  -l ${TFM_ISOLATION_LEVEL}
                                   -o ${CMAKE_BINARY_DIR}/generated
                                   ${PARSE_MANIFEST_QUIET_FLAG}
     DEPENDS ${TEMPLATE_FILES} ${MANIFEST_FILES}
@@ -146,6 +147,7 @@
     COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tfm_parse_manifest_list.py
                                   -m ${COMBINED_LIST}
                                   -f ${GENERATED_FILE_LISTS}
+                                  -l ${TFM_ISOLATION_LEVEL}
                                   -o ${CMAKE_BINARY_DIR}/generated
                                   ${PARSE_MANIFEST_QUIET_FLAG}
     RESULT_VARIABLE RET
diff --git a/tools/config_impl.cmake.template b/tools/config_impl.cmake.template
index 4c9dbcc..5aeb360 100644
--- a/tools/config_impl.cmake.template
+++ b/tools/config_impl.cmake.template
@@ -9,29 +9,12 @@
 ########{{utilities.donotedit_warning}}########
 
 if(TFM_PSA_API)
-{% if partition_statistics['ipc_partition_num'] > 0
-    and partition_statistics['sfn_partition_num'] == 0 %}
-    set(CONFIG_TFM_SPM_BACKEND_IPC ON PARENT_SCOPE)
+    set(CONFIG_TFM_SPM_BACKEND_SFN {{config_impl['CONFIG_TFM_SPM_BACKEND_SFN']}} PARENT_SCOPE)
+    set(CONFIG_TFM_SPM_BACKEND_IPC {{config_impl['CONFIG_TFM_SPM_BACKEND_IPC']}} PARENT_SCOPE)
 
-    if(TFM_ISOLATION_LEVEL GREATER 1)
-        set(CONFIG_TFM_PSA_API_SUPERVISOR_CALL ON PARENT_SCOPE)
-    else()
-        set(CONFIG_TFM_PSA_API_CROSS_CALL ON PARENT_SCOPE)
-    endif()
-{% elif partition_statistics['sfn_partition_num'] > 0
-    and partition_statistics['ipc_partition_num'] == 0 %}
-    set(CONFIG_TFM_SPM_BACKEND_SFN ON PARENT_SCOPE)
-    set(CONFIG_TFM_PSA_API_SFN_CALL ON PARENT_SCOPE)
-
-    if(TFM_ISOLATION_LEVEL GREATER 1)
-        message(FATAL_ERROR "High isolation level SFN model is not supported.")
-    endif()
-{% elif partition_statistics['sfn_partition_num'] > 0
-    and partition_statistics['ipc_partition_num'] > 0 %}
-    message(FATAL_ERROR "IPC and SFN co-work not supported yet.")
-{% else %}
-    message(FATAL_ERROR "Invalid partition number input, check configurations.")
-{% endif %}
+    set(CONFIG_TFM_PSA_API_SFN_CALL        {{config_impl['CONFIG_TFM_PSA_API_SFN_CALL']}}        PARENT_SCOPE)
+    set(CONFIG_TFM_PSA_API_CROSS_CALL      {{config_impl['CONFIG_TFM_PSA_API_CROSS_CALL']}}      PARENT_SCOPE)
+    set(CONFIG_TFM_PSA_API_SUPERVISOR_CALL {{config_impl['CONFIG_TFM_PSA_API_SUPERVISOR_CALL']}} PARENT_SCOPE)
 
     if((CONFIG_TFM_FP GREATER 0) AND CONFIG_TFM_SPM_BACKEND_SFN)
         message(FATAL_ERROR "FP is not supported for SFN model.")
diff --git a/tools/tfm_parse_manifest_list.py b/tools/tfm_parse_manifest_list.py
index b6a687e..32b06dd 100644
--- a/tools/tfm_parse_manifest_list.py
+++ b/tools/tfm_parse_manifest_list.py
@@ -117,7 +117,7 @@
 
     return partition_manifest
 
-def process_partition_manifests(manifest_lists):
+def process_partition_manifests(manifest_lists, isolation_level):
     """
     Parse the input manifest lists, generate the data base for genereated files
     and generate manifest header files.
@@ -147,6 +147,14 @@
         'ipc_partition_num': 0,
         'sfn_partition_num': 0
     }
+    config_impl = {
+        'CONFIG_TFM_SPM_BACKEND_SFN'              : '0',
+        'CONFIG_TFM_SPM_BACKEND_IPC'              : '0',
+        'CONFIG_TFM_PSA_API_SFN_CALL'             : '0',
+        'CONFIG_TFM_PSA_API_CROSS_CALL'           : '0',
+        'CONFIG_TFM_PSA_API_SUPERVISOR_CALL'      : '0',
+        'CONFIG_TFM_CONNECTION_BASED_SERVICE_API' : '0'
+    }
 
     # Get all the manifests information as a dictionary
     for i, item in enumerate(manifest_lists):
@@ -256,8 +264,34 @@
         all_manifests[idx]['pid'] = pid
         pid_list.append(pid)
 
+    # Set up configurations
+    if partition_statistics['ipc_partition_num'] == 0 and \
+        partition_statistics['sfn_partition_num'] > 0:
+        if isolation_level > 1:
+            print('High isolation level SFN model is not supported.')
+            exit(1)
+        config_impl['CONFIG_TFM_SPM_BACKEND_SFN'] = '1'
+        config_impl['CONFIG_TFM_PSA_API_SFN_CALL'] = '1'
+    elif partition_statistics['ipc_partition_num'] > 0 and \
+        partition_statistics['sfn_partition_num'] == 0:
+        config_impl['CONFIG_TFM_SPM_BACKEND_IPC'] = '1'
+        if isolation_level > 1:
+            config_impl['CONFIG_TFM_PSA_API_SUPERVISOR_CALL'] = '1'
+        else:
+            config_impl['CONFIG_TFM_PSA_API_CROSS_CALL'] = '1'
+    elif partition_statistics['ipc_partition_num'] > 0 and \
+        partition_statistics['sfn_partition_num'] > 0:
+        print('IPC and SFN co-work not supported yet.')
+        exit(1)
+    else:
+        print('Invalid partition number input, check configurations.')
+        exit(1)
+
+    if partition_statistics['connection_based_srv_num'] > 0:
+        config_impl['CONFIG_TFM_CONNECTION_BASED_SERVICE_API'] = 1
+
     context['partitions'] = partition_list
-    context['partition_statistics'] = partition_statistics
+    context['config_impl'] = config_impl
     context['stateless_services'] = process_stateless_services(partition_list)
 
     return context
@@ -485,6 +519,14 @@
                         , required=True
                         , metavar='file-list'
                         , help='These files descripe the file list to generate')
+
+    parser.add_argument('-l', '--isolation-level'
+                        , dest='isolation_level'
+                        , required=True
+                        , choices=['1', '2', '3']
+                        , metavar='isolation-level'
+                        , help='The isolation level')
+
     parser.add_argument('-q', '--quiet'
                         , dest='quiet'
                         , required=False
@@ -537,7 +579,7 @@
     """
     os.chdir(os.path.join(sys.path[0], '..'))
 
-    context = process_partition_manifests(manifest_lists)
+    context = process_partition_manifests(manifest_lists, int(args.isolation_level))
 
     utilities = {}
     utilities['donotedit_warning'] = donotedit_warning