Build: Follow the 'source_structure.rst'

This is the first patch to follow the first commit of source structure
document. The items under the 'secure_fw' folder are re-organized:

- Create/Move some folders/files to follow document.
- Rename some folders to foll, for example, 'secure_fw/services' to
  'secure_fw/partitions'.
- Update affected files to make it work.

This is a big change, to make the structure meet the basic shape of
the structure document defined, and make it easier to be understood
for users. Staging changes are not applicable so they are combined
into one - and because it is not the final shape yet, so:

- Upcoming updates on the 'secure_fw' folder would follow up soon.
- Fine-tune about the 'source_structure.rst' would come, too.

Change-Id: I5c11175e0a4579cd9b42d3e3519dbffb87334d0b
Signed-off-by: Ken Liu <ken.liu@arm.com>
diff --git a/secure_fw/spm/init/tfm_boot_data.c b/secure_fw/spm/init/tfm_boot_data.c
new file mode 100644
index 0000000..2dfbbc4
--- /dev/null
+++ b/secure_fw/spm/init/tfm_boot_data.c
@@ -0,0 +1,242 @@
+/*
+ * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include <stdint.h>
+#include "bl2/include/tfm_boot_status.h"
+#include "region_defs.h"
+#include "tfm_memory_utils.h"
+#include "tfm_internal.h"
+#include "tfm_api.h"
+#include "tfm_core_utils.h"
+#include "tfm/spm_api.h"
+#include "tfm/spm_partition_defs.h"
+#ifdef TFM_PSA_API
+#include "tfm_internal_defines.h"
+#include "tfm_utils.h"
+#include "psa/service.h"
+#include "tfm_thread.h"
+#include "tfm_wait.h"
+#include "tfm_message_queue.h"
+#include "tfm_spm_hal.h"
+#include "tfm/spm_db.h"
+#endif
+
+/*!
+ * \def BOOT_DATA_VALID
+ *
+ * \brief Indicates that shared data between bootloader and runtime firmware was
+ *        passed the sanity check with success.
+ */
+#define BOOT_DATA_VALID (1u)
+
+/*!
+ * \def BOOT_DATA_INVALID
+ *
+ * \brief Indicates that shared data between bootloader and runtime firmware was
+ *        failed on sanity check.
+ */
+#define BOOT_DATA_INVALID (0u)
+
+/*!
+ * \var is_boot_data_valid
+ *
+ * \brief Indicates the status of shared data between bootloader and runtime
+ *        firmware
+ */
+static uint32_t is_boot_data_valid = BOOT_DATA_INVALID;
+
+/*!
+ * \struct boot_data_access_policy
+ *
+ * \brief Defines the access policy of secure partitions to data items in shared
+ *        data area (between bootloader and runtime firmware).
+ */
+struct boot_data_access_policy {
+    uint32_t partition_id;
+    uint32_t major_type;
+};
+
+/*!
+ * \var access_policy_table
+ *
+ * \brief Contains the partition_id and major_type assignments. This describes
+ *        which secure partition is allowed to access which data item
+ *        (identified by major_type).
+ */
+static const struct boot_data_access_policy access_policy_table[] = {
+    {TFM_SP_INITIAL_ATTESTATION, TLV_MAJOR_IAS},
+};
+
+/*!
+ * \brief Verify the access right of the active secure partition to the
+ *        specified data type in the shared data area.
+ *
+ * \param[in]  major_type  Data type identifier.
+ *
+ * \return  Returns 0 in case of success, otherwise -1.
+ */
+static int32_t tfm_core_check_boot_data_access_policy(uint8_t major_type)
+{
+    uint32_t partition_id;
+    uint32_t i;
+    int32_t rc = -1;
+    const uint32_t array_size =
+            sizeof(access_policy_table) / sizeof(access_policy_table[0]);
+
+#ifndef TFM_PSA_API
+    uint32_t partition_idx = tfm_spm_partition_get_running_partition_idx();
+
+    partition_id = tfm_spm_partition_get_partition_id(partition_idx);
+#else
+    partition_id = tfm_spm_partition_get_running_partition_id();
+#endif
+
+    for (i = 0; i < array_size; ++i) {
+        if (partition_id == access_policy_table[i].partition_id) {
+            if (major_type == access_policy_table[i].major_type) {
+                rc = 0;
+                break;
+            }
+        }
+    }
+
+    return rc;
+}
+
+/* Compile time check to verify that shared data region is not overlapping with
+ * non-secure data area.
+ */
+#if ((BOOT_TFM_SHARED_DATA_BASE  >= NS_DATA_START && \
+      BOOT_TFM_SHARED_DATA_BASE  <= NS_DATA_LIMIT) || \
+     (BOOT_TFM_SHARED_DATA_LIMIT >= NS_DATA_START && \
+      BOOT_TFM_SHARED_DATA_LIMIT <= NS_DATA_LIMIT))
+#error "Shared data area and non-secure data area is overlapping"
+#endif
+
+void tfm_core_validate_boot_data(void)
+{
+#ifdef BOOT_DATA_AVAILABLE
+    struct tfm_boot_data *boot_data;
+
+    boot_data = (struct tfm_boot_data *)BOOT_TFM_SHARED_DATA_BASE;
+
+    if (boot_data->header.tlv_magic == SHARED_DATA_TLV_INFO_MAGIC) {
+        is_boot_data_valid = BOOT_DATA_VALID;
+    }
+#else
+    is_boot_data_valid = BOOT_DATA_VALID;
+#endif /* BOOT_DATA_AVAILABLE */
+}
+
+void tfm_core_get_boot_data_handler(uint32_t args[])
+{
+    uint8_t  tlv_major = (uint8_t)args[0];
+    uint8_t *buf_start = (uint8_t *)args[1];
+    uint16_t buf_size  = (uint16_t)args[2];
+    struct tfm_boot_data *boot_data;
+#ifdef BOOT_DATA_AVAILABLE
+    uint8_t *ptr;
+    struct shared_data_tlv_entry tlv_entry;
+    uintptr_t tlv_end, offset;
+#endif /* BOOT_DATA_AVAILABLE */
+#ifndef TFM_PSA_API
+    uint32_t running_partition_idx =
+                tfm_spm_partition_get_running_partition_idx();
+    uint32_t res;
+#else
+    struct spm_partition_desc_t *partition = NULL;
+    uint32_t privileged;
+#endif
+
+#ifndef TFM_PSA_API
+    /*
+     * Make sure that the output pointer points to a memory area that is owned
+     * by the partition. And check 4 bytes alignment.
+     */
+    res = tfm_spm_check_buffer_access(running_partition_idx,
+                                      (void *)buf_start,
+                                      buf_size,
+                                      2);
+    if (!res) {
+        /* Not in accessible range, return error */
+        args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
+        return;
+    }
+#else
+    partition = tfm_spm_get_running_partition();
+    if (!partition) {
+        tfm_core_panic();
+    }
+    privileged =
+        tfm_spm_partition_get_privileged_mode(partition->static_data->
+                                              partition_flags);
+
+    if (tfm_memory_check(buf_start, buf_size, false, TFM_MEMORY_ACCESS_RW,
+        privileged) != IPC_SUCCESS) {
+        /* Not in accessible range, return error */
+        args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
+        return;
+    }
+#endif
+
+    if (is_boot_data_valid != BOOT_DATA_VALID) {
+        args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
+        return;
+    }
+
+    /* Check whether caller has access right to given tlv_major_type */
+    if (tfm_core_check_boot_data_access_policy(tlv_major)) {
+        args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
+        return;
+    }
+
+#ifdef BOOT_DATA_AVAILABLE
+    /* Get the boundaries of TLV section */
+    boot_data = (struct tfm_boot_data *)BOOT_TFM_SHARED_DATA_BASE;
+    tlv_end = BOOT_TFM_SHARED_DATA_BASE + boot_data->header.tlv_tot_len;
+    offset  = BOOT_TFM_SHARED_DATA_BASE + SHARED_DATA_HEADER_SIZE;
+#endif /* BOOT_DATA_AVAILABLE */
+
+    /* Add header to output buffer as well */
+    if (buf_size < SHARED_DATA_HEADER_SIZE) {
+        args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
+        return;
+    } else {
+        boot_data = (struct tfm_boot_data *)buf_start;
+        boot_data->header.tlv_magic   = SHARED_DATA_TLV_INFO_MAGIC;
+        boot_data->header.tlv_tot_len = SHARED_DATA_HEADER_SIZE;
+    }
+
+#ifdef BOOT_DATA_AVAILABLE
+    ptr = boot_data->data;
+    /* Iterates over the TLV section and copy TLVs with requested major
+     * type to the provided buffer.
+     */
+    for (; offset < tlv_end; offset += tlv_entry.tlv_len) {
+        /* Create local copy to avoid unaligned access */
+        (void)tfm_core_util_memcpy(&tlv_entry,
+                                   (const void *)offset,
+                                   SHARED_DATA_ENTRY_HEADER_SIZE);
+        if (GET_MAJOR(tlv_entry.tlv_type) == tlv_major) {
+            /* Check buffer overflow */
+            if (((ptr - buf_start) + tlv_entry.tlv_len) > buf_size) {
+                args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
+                return;
+            }
+
+            (void)tfm_core_util_memcpy(ptr, (const void *)offset,
+                                       tlv_entry.tlv_len);
+
+            ptr += tlv_entry.tlv_len;
+            boot_data->header.tlv_tot_len += tlv_entry.tlv_len;
+        }
+    }
+#endif /* BOOT_DATA_AVAILABLE */
+
+    args[0] = (uint32_t)TFM_SUCCESS;
+    return;
+}
diff --git a/secure_fw/spm/init/tfm_core.c b/secure_fw/spm/init/tfm_core.c
new file mode 100644
index 0000000..e1f0c14
--- /dev/null
+++ b/secure_fw/spm/init/tfm_core.c
@@ -0,0 +1,212 @@
+/*
+ * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "region.h"
+#include "tfm_core_topology.h"
+#include "tfm_internal.h"
+#include "tfm_irq_list.h"
+#include "tfm_nspm.h"
+#include "tfm_spm_hal.h"
+#include "tfm_version.h"
+#include "log/tfm_log.h"
+#include "tfm/spm_api.h"
+#include "tfm/spm_db.h"
+
+/*
+ * Avoids the semihosting issue
+ * FixMe: describe 'semihosting issue'
+ */
+#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
+__asm("  .global __ARM_use_no_argv\n");
+#endif
+
+#ifndef TFM_LVL
+#error TFM_LVL is not defined!
+#endif
+
+#ifdef TFM_PSA_API
+#if (TFM_LVL != 1) && (TFM_LVL != 2)
+#error Only TFM_LVL 1 and 2 are supported for IPC model!
+#endif
+#else
+#if (TFM_LVL != 1)
+#error Only TFM_LVL 1 is supported for library model!
+#endif
+#endif
+
+REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP,  $$ZI$$Base);
+
+static int32_t tfm_core_init(void)
+{
+    size_t i;
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+    enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE;
+
+    /* Enables fault handlers */
+    plat_err = tfm_spm_hal_enable_fault_handlers();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_ERROR_GENERIC;
+    }
+
+    /* Configures the system reset request properties */
+    plat_err = tfm_spm_hal_system_reset_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_ERROR_GENERIC;
+    }
+
+    /* Configures debug authentication */
+    plat_err = tfm_spm_hal_init_debug();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_ERROR_GENERIC;
+    }
+
+    /*
+     * Access to any peripheral should be performed after programming
+     * the necessary security components such as PPC/SAU.
+     */
+    plat_err = tfm_spm_hal_init_isolation_hw();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_ERROR_GENERIC;
+    }
+
+    /* Performs platform specific initialization */
+    plat_err = tfm_spm_hal_post_init();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_ERROR_GENERIC;
+    }
+
+    LOG_MSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
+
+#ifdef TFM_CORE_DEBUG
+    LOG_MSG("TF-M isolation level is: %d\r\n", TFM_LVL);
+#endif
+
+    tfm_core_validate_boot_data();
+
+    configure_ns_code();
+
+    /* Configures all interrupts to retarget NS state, except for
+     * secure peripherals
+     */
+    plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_ERROR_GENERIC;
+    }
+
+    for (i = 0; i < tfm_core_irq_signals_count; ++i) {
+        plat_err = tfm_spm_hal_set_secure_irq_priority(
+                                          tfm_core_irq_signals[i].irq_line,
+                                          tfm_core_irq_signals[i].irq_priority);
+        if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+            return TFM_ERROR_GENERIC;
+        }
+        irq_target_state = tfm_spm_hal_set_irq_target_state(
+                                          tfm_core_irq_signals[i].irq_line,
+                                          TFM_IRQ_TARGET_STATE_SECURE);
+        if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) {
+            return TFM_ERROR_GENERIC;
+        }
+    }
+
+    /* Enable secure peripherals interrupts */
+    plat_err = tfm_spm_hal_nvic_interrupt_enable();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_ERROR_GENERIC;
+    }
+
+    return TFM_SUCCESS;
+}
+
+static int32_t tfm_core_set_secure_exception_priorities(void)
+{
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
+    tfm_arch_prioritize_secure_exception();
+
+    /* Explicitly set Secure SVC priority to highest */
+    plat_err = tfm_spm_hal_set_secure_irq_priority(SVCall_IRQn, 0);
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_ERROR_GENERIC;
+    }
+
+    tfm_core_topology_set_pendsv_priority();
+
+    return TFM_SUCCESS;
+}
+
+int main(void)
+{
+    /* set Main Stack Pointer limit */
+    tfm_arch_set_msplim((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK_MSP,
+                                               $$ZI$$Base));
+
+    if (tfm_core_init() != TFM_SUCCESS) {
+        tfm_core_panic();
+    }
+    /* Print the TF-M version */
+    LOG_MSG("\033[1;34mBooting TFM v%d.%d %s\033[0m\r\n",
+            VERSION_MAJOR, VERSION_MINOR, VERSION_STRING);
+
+    if (tfm_spm_db_init() != SPM_ERR_OK) {
+        tfm_core_panic();
+    }
+
+#ifdef CONFIG_TFM_ENABLE_MEMORY_PROTECT
+    if (tfm_spm_hal_setup_isolation_hw() != TFM_PLAT_ERR_SUCCESS) {
+        tfm_core_panic();
+    }
+#endif /* CONFIG_TFM_ENABLE_MEMORY_PROTECT */
+
+#ifndef TFM_PSA_API
+    tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_RUNNING);
+
+    REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base)[];
+    uint32_t psp_stack_bottom =
+                      (uint32_t)REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
+
+    tfm_arch_set_psplim(psp_stack_bottom);
+
+    if (tfm_spm_partition_init() != SPM_ERR_OK) {
+        /* Certain systems might refuse to boot altogether if partitions fail
+         * to initialize. This is a placeholder for such an error handler
+         */
+    }
+
+    /*
+     * Prioritise secure exceptions to avoid NS being able to pre-empt
+     * secure SVC or SecureFault. Do it before PSA API initialization.
+     */
+    if (tfm_core_set_secure_exception_priorities() != TFM_SUCCESS) {
+        tfm_core_panic();
+    }
+
+    /* We close the TFM_SP_CORE_ID partition, because its only purpose is
+     * to be able to pass the state checks for the tests started from secure.
+     */
+    tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_CLOSED);
+    tfm_spm_partition_set_state(TFM_SP_NON_SECURE_ID,
+                                SPM_PARTITION_STATE_RUNNING);
+
+#ifdef TFM_CORE_DEBUG
+    /* Jumps to non-secure code */
+    LOG_MSG("\033[1;34mJumping to non-secure code...\033[0m\r\n");
+#endif
+
+    jump_to_ns_code();
+#else /* !defined(TFM_PSA_API) */
+    /*
+     * Prioritise secure exceptions to avoid NS being able to pre-empt
+     * secure SVC or SecureFault. Do it before PSA API initialization.
+     */
+    if (tfm_core_set_secure_exception_priorities() != TFM_SUCCESS) {
+        tfm_core_panic();
+    }
+
+    /* Move to handler mode for further SPM initialization. */
+    tfm_core_handler_mode();
+#endif /* !defined(TFM_PSA_API) */
+}