Core: Separate the partition db config to two parts

The partition db of TF-M contains fields that remain constant during
execution, and some of them are used to store dynamically changing
context information. This change groups the two kinds of fields in
separate structures to highlight the distinction.

Change-Id: I869504cb36010b54409a9963556f96ec1381bfbe
Signed-off-by: Mate Toth-Pal <mate.toth-pal@arm.com>
diff --git a/secure_fw/core/tfm_secure_api.c b/secure_fw/core/tfm_secure_api.c
index dd67eb6..55cd1b1 100644
--- a/secure_fw/core/tfm_secure_api.c
+++ b/secure_fw/core/tfm_secure_api.c
@@ -65,6 +65,7 @@
 static int32_t tfm_push_lock(struct tfm_sfn_req_s *desc_ptr, uint32_t lr)
 {
     uint32_t caller_partition_id = tfm_spm_partition_get_running_partition_id();
+    const struct spm_partition_runtime_data_t *curr_part_data;
 
     if (caller_partition_id >= TFM_SP_BASE &&
         /* Also check partition state consistency */
@@ -73,7 +74,10 @@
         register uint32_t partition_id = desc_ptr->ss_id;
         uint32_t psp = __get_PSP();
         uint32_t partition_psp, partition_psplim;
-        uint32_t partition_state = tfm_spm_partition_get_state(partition_id);
+        uint32_t partition_state;
+
+        curr_part_data = tfm_spm_partition_get_runtime_data(partition_id);
+        partition_state = curr_part_data->partition_state;
 
         if (partition_state == SPM_PARTITION_STATE_RUNNING ||
             partition_state == SPM_PARTITION_STATE_SUSPENDED ||
@@ -98,7 +102,7 @@
              */
             tfm_spm_partition_set_stack(caller_partition_id, psp);
         }
-        partition_psp = tfm_spm_partition_get_stack(partition_id);
+        partition_psp = curr_part_data->stack_ptr;
         partition_psplim = tfm_spm_partition_get_stack_bottom(partition_id);
 #endif
         /* Stack the context for the partition call */
@@ -163,8 +167,10 @@
 {
     uint32_t current_partition_id =
             tfm_spm_partition_get_running_partition_id();
+    const struct spm_partition_runtime_data_t *curr_part_data =
+            tfm_spm_partition_get_runtime_data(current_partition_id);
     uint32_t return_partition_id =
-            tfm_spm_partition_get_caller_partition_id(current_partition_id);
+            curr_part_data->caller_partition_id;
 
     if (current_partition_id < TFM_SP_BASE) {
         return TFM_SECURE_UNLOCK_FAILED;
@@ -182,21 +188,19 @@
             tfm_spm_partition_sandbox_config(return_partition_id);
             /* Restore share status */
             tfm_spm_partition_set_share(return_partition_id,
-                    tfm_spm_partition_get_share(return_partition_id));
+               tfm_spm_partition_get_runtime_data(return_partition_id)->share);
         }
 #endif
 
 #if TFM_LVL == 1
-        if (tfm_spm_partition_get_caller_partition_id(current_partition_id) ==
-                TFM_SP_NON_SECURE_ID) {
+        if (return_partition_id == TFM_SP_NON_SECURE_ID) {
             /* In TFM level 1 context restore is only done when
              * returning to NS
              */
             /* Restore caller PSP and LR ptr */
-            __set_PSP(tfm_spm_partition_get_orig_psp(current_partition_id));
-            __set_PSPLIM(
-                    tfm_spm_partition_get_orig_psplim(current_partition_id));
-            *lr_ptr = tfm_spm_partition_get_orig_lr(current_partition_id);
+            __set_PSP(curr_part_data->orig_psp);
+            __set_PSPLIM(curr_part_data->orig_psplim);
+            *lr_ptr = curr_part_data->orig_lr;
         }
 #else
         uint32_t psp = __get_PSP();
@@ -206,9 +210,9 @@
                     current_partition_id, psp + SVC_STACK_FRAME_SIZE);
 
         /* Restore caller PSP and LR ptr */
-        __set_PSP(tfm_spm_partition_get_orig_psp(current_partition_id));
-        __set_PSPLIM(tfm_spm_partition_get_orig_psplim(current_partition_id));
-        *lr_ptr = tfm_spm_partition_get_orig_lr(current_partition_id);
+        __set_PSP(curr_part_data->orig_psp);
+        __set_PSPLIM(curr_part_data->orig_psplim);
+        *lr_ptr = curr_part_data->orig_lr;
 #endif
 
         /* Clear the context entry in the context stack before returning */
@@ -460,6 +464,8 @@
     int32_t res = TFM_ERROR_GENERIC;
     uint32_t running_partition_id =
             tfm_spm_partition_get_running_partition_id();
+    const struct spm_partition_runtime_data_t *curr_part_data =
+            tfm_spm_partition_get_runtime_data(running_partition_id);
 
     if (running_partition_id == TFM_SP_NON_SECURE_ID)  {
         /* This handler shouldn't be called from outside partition context.
@@ -470,8 +476,7 @@
     }
 
     /* Store return value in r0 */
-    if (tfm_spm_partition_get_caller_partition_id(running_partition_id) !=
-            TFM_SP_NON_SECURE_ID) {
+    if (curr_part_data->caller_partition_id != TFM_SP_NON_SECURE_ID) {
         res = TFM_SUCCESS;
     }
     svc_args[0] = res;
@@ -487,6 +492,8 @@
     int32_t res;
     uint32_t running_partition_id =
             tfm_spm_partition_get_running_partition_id();
+    const struct spm_partition_runtime_data_t *curr_part_data =
+            tfm_spm_partition_get_runtime_data(running_partition_id);
 
     if ((running_partition_id == TFM_SP_NON_SECURE_ID) || (size == 0)) {
         /* This handler shouldn't be called from outside partition context.
@@ -498,8 +505,7 @@
 
     int32_t flags = 0;
 
-    if (tfm_spm_partition_get_share(running_partition_id) !=
-            TFM_BUFFER_SHARE_PRIV) {
+    if (curr_part_data->share != TFM_BUFFER_SHARE_PRIV) {
         flags |= CMSE_MPU_UNPRIV;
     }
 
@@ -687,8 +693,9 @@
     enum tfm_buffer_share_region_e share;
     uint32_t running_partition_id =
             tfm_spm_partition_get_running_partition_id();
-    uint32_t caller_partition_id =
-            tfm_spm_partition_get_caller_partition_id(running_partition_id);
+    const struct spm_partition_runtime_data_t *curr_part_data =
+            tfm_spm_partition_get_runtime_data(running_partition_id);
+    uint32_t caller_partition_id = curr_part_data->caller_partition_id;
 
      /* tfm_core_set_buffer_area() returns int32_t */
     int32_t *res_ptr = (int32_t *)&args[0];
diff --git a/secure_fw/spm/spm_api.c b/secure_fw/spm/spm_api.c
index a885645..642b577 100644
--- a/secure_fw/spm/spm_api.c
+++ b/secure_fw/spm/spm_api.c
@@ -39,20 +39,20 @@
  * returned.
  */
 static void tfm_spm_partition_err_handler(
-    struct spm_partition_region_t *partition,
+    struct spm_partition_desc_t *partition,
     ss_error_type_t err_type,
     int32_t err_code)
 {
 #ifdef TFM_CORE_DEBUG
     if (err_type == TFM_INIT_FAILURE) {
         printf("Partition init failed for partition id 0x%08X\r\n",
-                partition->partition_id);
+                partition->static_data.partition_id);
     } else {
         printf("Unknown partition error %d for partition id 0x%08X\r\n",
-            err_type, partition->partition_id);
+            err_type, partition->static_data.partition_id);
     }
 #endif
-    tfm_spm_partition_set_state(partition->partition_id,
+    tfm_spm_partition_set_state(partition->static_data.partition_id,
             SPM_PARTITION_STATE_CLOSED);
 }
 
@@ -182,7 +182,7 @@
 
 enum spm_err_t tfm_spm_partition_init(void)
 {
-    struct spm_partition_region_t *serv;
+    struct spm_partition_desc_t *part;
     int32_t fail_cnt = 0;
     uint32_t i;
 
@@ -192,22 +192,22 @@
      * proper isolation during init.
      */
     for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
-        serv = &g_spm_partition_db.partitions[i];
-        if (serv->periph_start) {
-            ppc_configure_to_secure(serv->periph_ppc_bank,
-                                    serv->periph_ppc_loc);
+        part = &g_spm_partition_db.partitions[i];
+        if (part->static_data.periph_start) {
+            ppc_configure_to_secure(part->static_data.periph_ppc_bank,
+                    part->static_data.periph_ppc_loc);
         }
-        if (serv->partition_init == NULL) {
-            tfm_spm_partition_set_state(serv->partition_id,
+        if (part->static_data.partition_init == NULL) {
+            tfm_spm_partition_set_state(part->static_data.partition_id,
                     SPM_PARTITION_STATE_IDLE);
         } else {
-            int32_t ret = serv->partition_init();
+            int32_t ret = part->static_data.partition_init();
 
             if (ret == TFM_SUCCESS) {
-                tfm_spm_partition_set_state(
-                        serv->partition_id, SPM_PARTITION_STATE_IDLE);
+                tfm_spm_partition_set_state(part->static_data.partition_id,
+                                            SPM_PARTITION_STATE_IDLE);
             } else {
-                tfm_spm_partition_err_handler(serv, TFM_INIT_FAILURE, ret);
+                tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, ret);
                 fail_cnt++;
             }
         }
@@ -227,7 +227,7 @@
      * SPM partition for that partition
      */
 
-    struct spm_partition_region_t *serv;
+    struct spm_partition_desc_t *part;
     struct mpu_armv8m_region_cfg_t region_cfg;
 
     if (!g_spm_partition_db.is_init) {
@@ -235,7 +235,7 @@
     }
 
     /*brute force id*/
-    serv = &g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)];
+    part = &g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)];
 
     mpu_armv8m_disable(&dev_mpu_s);
 
@@ -243,8 +243,8 @@
 
     /* RO region*/
     region_cfg.region_nr = PARTITION_REGION_RO;
-    region_cfg.region_base = serv->ro_start;
-    region_cfg.region_limit = serv->ro_limit;
+    region_cfg.region_base = part->static_data.ro_start;
+    region_cfg.region_limit = part->static_data.ro_limit;
     region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
     region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
     region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
@@ -253,25 +253,26 @@
 
     /* RW, ZI and stack as one region*/
     region_cfg.region_nr = PARTITION_REGION_RW_STACK;
-    region_cfg.region_base = serv->rw_start;
-    region_cfg.region_limit = serv->stack_top;
+    region_cfg.region_base = part->static_data.rw_start;
+    region_cfg.region_limit = part->static_data.stack_top;
     region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
     region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
     region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
 
     mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
 
-    if (serv->periph_start) {
+    if (part->static_data.periph_start) {
         /* Peripheral */
         region_cfg.region_nr = PARTITION_REGION_PERIPH;
-        region_cfg.region_base = serv->periph_start;
-        region_cfg.region_limit = serv->periph_limit;
+        region_cfg.region_base = part->static_data.periph_start;
+        region_cfg.region_limit = part->static_data.periph_limit;
         region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
         region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
         region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
         mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
 
-        ppc_en_secure_unpriv(serv->periph_ppc_bank, serv->periph_ppc_loc);
+        ppc_en_secure_unpriv(part->static_data.periph_ppc_bank,
+                             part->static_data.periph_ppc_loc);
     }
 
     mpu_armv8m_enable(&dev_mpu_s, 1, 1);
@@ -313,13 +314,14 @@
     __ISB();
 #endif
 
-    struct spm_partition_region_t *serv;
+    struct spm_partition_desc_t *part;
 
-    serv = &g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)];
+    part = &g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)];
 
-    if (serv->periph_start) {
+    if (part->static_data.periph_start) {
         /* Peripheral */
-        ppc_clr_secure_unpriv(serv->periph_ppc_bank, serv->periph_ppc_loc);
+        ppc_clr_secure_unpriv(part->static_data.periph_ppc_bank,
+                              part->static_data.periph_ppc_loc);
     }
 
     mpu_armv8m_disable(&dev_mpu_s);
@@ -332,70 +334,38 @@
     return SPM_ERR_OK;
 }
 
-uint32_t tfm_spm_partition_get_stack(uint32_t partition_id)
-{
-    return g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
-            stack_ptr;
-}
-
 uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_id)
 {
     return g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
-            stack_bottom;
+            static_data.stack_bottom;
 }
 
 uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_id)
 {
     return
-      g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].stack_top;
+      g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
+            static_data.stack_top;
 }
 
 void tfm_spm_partition_set_stack(uint32_t partition_id, uint32_t stack_ptr)
 {
-    g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].stack_ptr =
-            stack_ptr;
+    g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
+            runtime_data.stack_ptr = stack_ptr;
 }
 #endif
 
-uint32_t tfm_spm_partition_get_state(uint32_t partition_id)
-{
-    return g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
-            partition_state;
-}
 
-uint32_t tfm_spm_partition_get_caller_partition_id(uint32_t partition_id)
+const struct spm_partition_runtime_data_t *
+             tfm_spm_partition_get_runtime_data(uint32_t partition_id)
 {
-    return g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
-            caller_partition_id;
-}
-
-uint32_t tfm_spm_partition_get_orig_psp(uint32_t partition_id)
-{
-    return
-      g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].orig_psp;
-}
-
-uint32_t tfm_spm_partition_get_orig_psplim(uint32_t partition_id)
-{
-    return g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
-            orig_psplim;
-}
-
-uint32_t tfm_spm_partition_get_orig_lr(uint32_t partition_id)
-{
-    return g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
-            orig_lr;
-}
-
-uint32_t tfm_spm_partition_get_share(uint32_t partition_id)
-{
-    return g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].share;
+    return &(g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
+                runtime_data);
 }
 
 void tfm_spm_partition_set_state(uint32_t partition_id, uint32_t state)
 {
     g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
-                       partition_state = state;
+                       runtime_data.partition_state = state;
     if (state == SPM_PARTITION_STATE_RUNNING) {
         g_spm_partition_db.running_partition_id = partition_id;
     }
@@ -405,27 +375,27 @@
                                                uint32_t caller_partition_id)
 {
     g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
-            caller_partition_id = caller_partition_id;
+            runtime_data.caller_partition_id = caller_partition_id;
 }
 
 void tfm_spm_partition_set_orig_psp(uint32_t partition_id,
                                     uint32_t orig_psp)
 {
-    g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].orig_psp =
-            orig_psp;
+    g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
+            runtime_data.orig_psp = orig_psp;
 }
 
 void tfm_spm_partition_set_orig_psplim(uint32_t partition_id,
                                        uint32_t orig_psplim)
 {
-    g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].orig_psplim =
-            orig_psplim;
+    g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
+            runtime_data.orig_psplim = orig_psplim;
 }
 
 void tfm_spm_partition_set_orig_lr(uint32_t partition_id, uint32_t orig_lr)
 {
-    g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].orig_lr =
-            orig_lr;
+    g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
+            runtime_data.orig_lr = orig_lr;
 }
 
 enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_id,
@@ -439,8 +409,8 @@
 #endif
 
     if (ret == SPM_ERR_OK) {
-        g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].share =
-                share;
+        g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
+                runtime_data.share = share;
     }
     return ret;
 }
@@ -452,12 +422,11 @@
 
 void tfm_spm_partition_cleanup_context(uint32_t partition_id)
 {
-    struct spm_partition_region_t *partition =
+    struct spm_partition_desc_t *partition =
             &g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)];
-    partition->partition_state = 0;
-    partition->caller_partition_id = 0;
-    partition->orig_psp = 0;
-    partition->orig_psplim = 0;
-    partition->orig_lr = 0;
-    partition->share = 0;
+    partition->runtime_data.caller_partition_id = 0;
+    partition->runtime_data.orig_psp = 0;
+    partition->runtime_data.orig_psplim = 0;
+    partition->runtime_data.orig_lr = 0;
+    partition->runtime_data.share = 0;
 }
diff --git a/secure_fw/spm/spm_api.h b/secure_fw/spm/spm_api.h
index ca4994c..039506c 100644
--- a/secure_fw/spm/spm_api.h
+++ b/secure_fw/spm/spm_api.h
@@ -30,6 +30,21 @@
 };
 
 /**
+ * \brief Runtime context information of a partition
+ */
+struct spm_partition_runtime_data_t {
+    uint32_t partition_state;
+    uint32_t caller_partition_id;
+    uint32_t orig_psp;
+    uint32_t orig_psplim;
+    uint32_t orig_lr;
+    uint32_t share;
+#if TFM_LVL != 1
+    uint32_t stack_ptr;
+#endif
+};
+
+/**
  * \brief Configure isolated sandbox for a partition
  *
  * \param[in] partition_id     Partition id
@@ -52,17 +67,6 @@
 enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_id);
 
 /**
- * \brief Get saved stack pointer for a partition
- *
- * \param[in] partition_id     Partition id
- *
- * \return Stack pointer value
- *
- * \note This function doesn't check if partition_id is valid.
- */
-uint32_t tfm_spm_partition_get_stack(uint32_t partition_id);
-
-/**
  * \brief Get bottom of stack region for a partition
  *
  * \param[in] partition_id     Partition id
@@ -85,72 +89,16 @@
 uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_id);
 
 /**
- * \brief Get the current state of a partition
+ * \brief Get the current runtime data of a partition
  *
  * \param[in] partition_id     Partition id
  *
- * \return The state of the specified partition
- *
- * \note This function doesn't check if partition_id is valid.
- * \note The returned value has the value set of \ref spm_part_state_t.
- */
-uint32_t tfm_spm_partition_get_state(uint32_t partition_id);
-
-/**
- * \brief Get the Id of the caller of the partition given
- *
- * \param[in] partition_id     Partition id to get the caller of
- *
- * \return The Id of the caller partition
+ * \return The runtime data of the specified partition
  *
  * \note This function doesn't check if partition_id is valid.
  */
-uint32_t tfm_spm_partition_get_caller_partition_id(uint32_t partition_id);
-
-/**
- * \brief Get the original PSP of the partition
- *
- * \param[in] partition_id     Partition id
- *
- * \return The original PSP of the partition
- *
- * \note This function doesn't check if partition_id is valid.
- */
-uint32_t tfm_spm_partition_get_orig_psp(uint32_t partition_id);
-
-/**
- * \brief Get the original PSP limit of the partition
- *
- * \param[in] partition_id     Partition id
- *
- * \return The original PSP limit of the partition
- *
- * \note This function doesn't check if partition_id is valid.
- */
-uint32_t tfm_spm_partition_get_orig_psplim(uint32_t partition_id);
-
-/**
- * \brief Get the original link register value of the partition
- *
- * \param[in] partition_id     Partition id
- *
- * \return The original link register value of the partition
- *
- * \note This function doesn't check if partition_id is valid.
- */
-uint32_t tfm_spm_partition_get_orig_lr(uint32_t partition_id);
-
-/**
- * \brief Get the buffer share region of the partition
- *
- * \param[in] partition_id     Partition id
- *
- * \return The buffer share region of the partition
- *
- * \note This function doesn't check if partition_id is valid.
- * \note The returned value has the value set of \ref tfm_buffer_share_region_e
- */
-uint32_t tfm_spm_partition_get_share(uint32_t partition_id);
+const struct spm_partition_runtime_data_t *
+             tfm_spm_partition_get_runtime_data(uint32_t partition_id);
 
 /**
  * \brief Returns the id of the partition that has running state
diff --git a/secure_fw/spm/spm_db.h b/secure_fw/spm/spm_db.h
index 5c6d1b4..01aed35 100644
--- a/secure_fw/spm/spm_db.h
+++ b/secure_fw/spm/spm_db.h
@@ -20,31 +20,9 @@
 
 typedef int32_t(*sp_init_function)(void);
 
-#if TFM_LVL == 1
-struct spm_partition_region_t {
+struct spm_partition_static_data_t {
     uint32_t partition_id;
-    uint32_t partition_state;
-    uint32_t caller_partition_id;
-    uint32_t orig_psp;
-    uint32_t orig_psplim;
-    uint32_t orig_lr;
-    uint32_t share;
-    uint32_t stack_ptr;
-    uint32_t periph_start;
-    uint32_t periph_limit;
-    uint16_t periph_ppc_bank;
-    uint16_t periph_ppc_loc;
-    sp_init_function partition_init;
-};
-#else
-struct spm_partition_region_t {
-    uint32_t partition_id;
-    uint32_t partition_state;
-    uint32_t caller_partition_id;
-    uint32_t orig_psp;
-    uint32_t orig_psplim;
-    uint32_t orig_lr;
-    uint32_t share;
+#if TFM_LVL != 1
     uint32_t code_start;
     uint32_t code_limit;
     uint32_t ro_start;
@@ -55,20 +33,24 @@
     uint32_t zi_limit;
     uint32_t stack_bottom;
     uint32_t stack_top;
-    uint32_t stack_ptr;
+#endif
     uint32_t periph_start;
     uint32_t periph_limit;
     uint16_t periph_ppc_bank;
     uint16_t periph_ppc_loc;
     sp_init_function partition_init;
 };
-#endif
+
+struct spm_partition_desc_t {
+    struct spm_partition_static_data_t static_data;
+    struct spm_partition_runtime_data_t runtime_data;
+};
 
 struct spm_partition_db_t {
     uint32_t is_init;
     uint32_t partition_count;
     uint32_t running_partition_id;
-    struct spm_partition_region_t partitions[SPM_MAX_PARTITIONS];
+    struct spm_partition_desc_t partitions[SPM_MAX_PARTITIONS];
 };
 
 /* Macros to pick linker symbols and allow to form the partition data base */
@@ -78,6 +60,8 @@
 #define REGION_DECLARE(a, b, c)
 #else
 #define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c)
+#define PART_REGION_ADDR(partition, region) \
+    (uint32_t)&REGION_NAME(Image$$, partition, region)
 #endif
 
 #define PARTITION_DECLARE(partition)                       \
@@ -94,123 +78,134 @@
 
 
 #if TFM_LVL == 1
-#define PARTITION_ADD(partition) {                                  \
-     if (index >= max_partitions) {                                 \
-         return max_partitions;                                     \
-     }                                                              \
-     db_ptr = (uint32_t *)&(db->partitions[index]);                 \
-    *db_ptr++ = partition##_ID;                                     \
-    *db_ptr++ = SPM_PARTITION_STATE_UNINIT; /* partition_state  */  \
-    *db_ptr++ = 0U;     /* caller partition id */                   \
-    *db_ptr++ = 0U;     /* original psp */                          \
-    *db_ptr++ = 0U;     /* original psplim */                       \
-    *db_ptr++ = 0U;     /* original lr */                           \
-    *db_ptr++ = 0U;     /* share */                                 \
-    *db_ptr++ = 0U;     /* stack pointer on partition enter */      \
-    *db_ptr++ = 0U;     /* peripheral start */                      \
-    *db_ptr++ = 0U;     /* peripheral limit */                      \
-    *db_ptr++ = 0U;     /* uint16_t[2] peripheral bank/loc */       \
-    *db_ptr++ = 0U;     /* partition init function*/                \
-    index++;                                                        \
-    }
+#define PARTITION_INIT_STATIC_DATA(data, partition) \
+    data.partition_id    = partition##_ID;          \
+    data.periph_start    = 0U;                      \
+    data.periph_limit    = 0U;                      \
+    data.periph_ppc_bank = 0U;                      \
+    data.periph_ppc_loc  = 0U;                      \
+    data.partition_init  = 0U
 #else
-#define PARTITION_ADD(partition) {                                             \
-     if (index >= max_partitions) {                                            \
-         return max_partitions;                                                \
-     }                                                                         \
-     db_ptr = (uint32_t *)&(db->partitions[index]);                            \
-    *db_ptr++ = partition##_ID;                                                \
-    *db_ptr++ = SPM_PARTITION_STATE_UNINIT; /* partition_state  */             \
-    *db_ptr++ = 0U;     /* caller partition id */                              \
-    *db_ptr++ = 0U;     /* original psp */                                     \
-    *db_ptr++ = 0U;     /* original psplim */                                  \
-    *db_ptr++ = 0U;     /* original lr */                                      \
-    *db_ptr++ = 0U;     /* share */                                            \
-    *db_ptr++ = (uint32_t)&REGION_NAME(Image$$, partition, $$Base);            \
-    *db_ptr++ = (uint32_t)&REGION_NAME(Image$$, partition, $$Limit);           \
-    *db_ptr++ = (uint32_t)&REGION_NAME(Image$$, partition, $$RO$$Base);        \
-    *db_ptr++ = (uint32_t)&REGION_NAME(Image$$, partition, $$RO$$Limit);       \
-    *db_ptr++ = (uint32_t)&REGION_NAME(Image$$, partition, _DATA$$RW$$Base);   \
-    *db_ptr++ = (uint32_t)&REGION_NAME(Image$$, partition, _DATA$$RW$$Limit);  \
-    *db_ptr++ = (uint32_t)&REGION_NAME(Image$$, partition, _DATA$$ZI$$Base);   \
-    *db_ptr++ = (uint32_t)&REGION_NAME(Image$$, partition, _DATA$$ZI$$Limit);  \
-    *db_ptr++ = (uint32_t)&REGION_NAME(Image$$, partition, _STACK$$ZI$$Base);  \
-    *db_ptr++ = (uint32_t)&REGION_NAME(Image$$, partition, _STACK$$ZI$$Limit); \
-    *db_ptr++ = (uint32_t)&REGION_NAME(Image$$, partition, _STACK$$ZI$$Limit); \
-    *db_ptr++ = 0U;     /* peripheral start */                                 \
-    *db_ptr++ = 0U;     /* peripheral limit */                                 \
-    *db_ptr++ = 0U;     /* uint16_t[2] peripheral bank/loc */                  \
-    *db_ptr++ = 0U;     /* partition init function*/                           \
-    index++;                                                                   \
-}
+#define PARTITION_INIT_STATIC_DATA(data, partition)                        \
+    data.partition_id    = partition##_ID;                                 \
+    data.code_start      = PART_REGION_ADDR(partition, $$Base);            \
+    data.code_limit      = PART_REGION_ADDR(partition, $$Limit);           \
+    data.ro_start        = PART_REGION_ADDR(partition, $$RO$$Base);        \
+    data.ro_limit        = PART_REGION_ADDR(partition, $$RO$$Limit);       \
+    data.rw_start        = PART_REGION_ADDR(partition, _DATA$$RW$$Base);   \
+    data.rw_limit        = PART_REGION_ADDR(partition, _DATA$$RW$$Limit);  \
+    data.zi_start        = PART_REGION_ADDR(partition, _DATA$$ZI$$Base);   \
+    data.zi_limit        = PART_REGION_ADDR(partition, _DATA$$ZI$$Limit);  \
+    data.stack_bottom    = PART_REGION_ADDR(partition, _STACK$$ZI$$Base);  \
+    data.stack_top       = PART_REGION_ADDR(partition, _STACK$$ZI$$Limit); \
+    data.periph_start    = 0U;                                             \
+    data.periph_limit    = 0U;                                             \
+    data.periph_ppc_bank = 0U;                                             \
+    data.periph_ppc_loc  = 0U;                                             \
+    data.partition_init  = 0U
 #endif
 
 #if TFM_LVL == 1
-#define DUMMY_PARTITION_ADD(partition) {                            \
-     if (index >= max_partitions) {                                 \
-         return max_partitions;                                     \
-     }                                                              \
-     db_ptr = (uint32_t *)&(db->partitions[index]);                 \
-    *db_ptr++ = partition##_ID;                                     \
-    *db_ptr++ = SPM_PARTITION_STATE_UNINIT; /* partition_state  */  \
-    *db_ptr++ = 0U;     /* caller partition id */                   \
-    *db_ptr++ = 0U;     /* original psp */                          \
-    *db_ptr++ = 0U;     /* original psplim */                       \
-    *db_ptr++ = 0U;     /* original lr */                           \
-    *db_ptr++ = 0U;     /* share */                                 \
-    *db_ptr++ = 0U;     /* stack pointer on partition enter */      \
-    *db_ptr++ = 0U;     /* peripheral start */                      \
-    *db_ptr++ = 0U;     /* peripheral limit */                      \
-    *db_ptr++ = 0U;     /* uint16_t[2] peripheral bank/loc */       \
-    *db_ptr++ = 0U;     /* partition init function*/                \
-    index++;                                                        \
-    }
+#define DUMMY_PARTITION_INIT_STATIC_DATA(data, partition)                     \
+        /* In case of TFM_LVL1 the static data is initialised the same way */ \
+        PARTITION_INIT_STATIC_DATA(data, partition)
 #else
-#define DUMMY_PARTITION_ADD(partition) {                           \
-     if (index >= max_partitions) {                                \
-         return max_partitions;                                    \
-     }                                                             \
-     db_ptr = (uint32_t *)&(db->partitions[index]);                \
-    *db_ptr++ = partition##_ID;                                    \
-    *db_ptr++ = SPM_PARTITION_STATE_UNINIT; /* partition_state  */ \
-    *db_ptr++ = 0U;     /* caller partition id */                  \
-    *db_ptr++ = 0U;     /* original_psp */                         \
-    *db_ptr++ = 0U;     /* original_psplim */                      \
-    *db_ptr++ = 0U;     /* original_lr */                          \
-    *db_ptr++ = 0U;     /* share */                                \
-    *db_ptr++ = 0U;                                                \
-    *db_ptr++ = 0U;                                                \
-    *db_ptr++ = 0U;                                                \
-    *db_ptr++ = 0U;                                                \
-    *db_ptr++ = 0U;                                                \
-    *db_ptr++ = 0U;                                                \
-    *db_ptr++ = 0U;                                                \
-    *db_ptr++ = 0U;                                                \
-    *db_ptr++ = 0U;                                                \
-    *db_ptr++ = 0U;                                                \
-    *db_ptr++ = 0U;                                                \
-    *db_ptr++ = 0U;     /* peripheral start */                     \
-    *db_ptr++ = 0U;     /* peripheral limit */                     \
-    *db_ptr++ = 0U;     /* uint16_t[2] peripheral bank/loc */      \
-    *db_ptr++ = 0U;     /* partition init function*/               \
-    index++;                                                       \
-}
+#define DUMMY_PARTITION_INIT_STATIC_DATA(data, partition) \
+    data.partition_id    = partition##_ID;                \
+    data.code_start      = 0U;                            \
+    data.code_limit      = 0U;                            \
+    data.ro_start        = 0U;                            \
+    data.ro_limit        = 0U;                            \
+    data.rw_start        = 0U;                            \
+    data.rw_limit        = 0U;                            \
+    data.zi_start        = 0U;                            \
+    data.zi_limit        = 0U;                            \
+    data.stack_bottom    = 0U;                            \
+    data.stack_top       = 0U;                            \
+    data.periph_start    = 0U;                            \
+    data.periph_limit    = 0U;                            \
+    data.periph_ppc_bank = 0U;                            \
+    data.periph_ppc_loc  = 0U;                            \
+    data.partition_init  = 0U
 #endif
 
-#define PARTITION_ADD_PERIPHERAL(partition, start, limit, bank, loc) {         \
-        db_ptr =                                                               \
-               (uint32_t *)&(db->partitions[PARTITION_ID_GET(partition##_ID)]);\
-        ((struct spm_partition_region_t *)db_ptr)->periph_start = start;       \
-        ((struct spm_partition_region_t *)db_ptr)->periph_limit = limit;       \
-        ((struct spm_partition_region_t *)db_ptr)->periph_ppc_bank = bank;     \
-        ((struct spm_partition_region_t *)db_ptr)->periph_ppc_loc = loc;       \
+
+#if TFM_LVL == 1
+#define PARTITION_INIT_RUNTIME_DATA(data, partition)                           \
+    data.partition_state     = SPM_PARTITION_STATE_UNINIT;                     \
+    data.caller_partition_id = 0U;                                             \
+    data.orig_psp            = 0U;                                             \
+    data.orig_psplim         = 0U;                                             \
+    data.orig_lr             = 0U;                                             \
+    data.share               = 0U
+#else
+#define PARTITION_INIT_RUNTIME_DATA(data, partition)                           \
+    data.partition_state     = SPM_PARTITION_STATE_UNINIT;                     \
+    data.caller_partition_id = 0U;                                             \
+    data.orig_psp            = 0U;                                             \
+    data.orig_psplim         = 0U;                                             \
+    data.orig_lr             = 0U;                                             \
+    data.share               = 0U;                                             \
+    data.stack_ptr           = PART_REGION_ADDR(partition, _STACK$$ZI$$Limit)
+#endif
+
+#if TFM_LVL == 1
+#define DUMMY_PARTITION_INIT_RUNTIME_DATA(data, partition) \
+    data.partition_state     = SPM_PARTITION_STATE_UNINIT; \
+    data.caller_partition_id = 0U;                         \
+    data.orig_psp            = 0U;                         \
+    data.orig_psplim         = 0U;                         \
+    data.orig_lr             = 0U;                         \
+    data.share               = 0U
+#else
+#define DUMMY_PARTITION_INIT_RUNTIME_DATA(data, partition) \
+    data.partition_state     = SPM_PARTITION_STATE_UNINIT; \
+    data.caller_partition_id = 0U;                         \
+    data.orig_psp            = 0U;                         \
+    data.orig_psplim         = 0U;                         \
+    data.orig_lr             = 0U;                         \
+    data.share               = 0U;                         \
+    data.stack_ptr           = 0U
+#endif
+
+#define PARTITION_ADD(partition) {                                      \
+    if (index >= max_partitions) {                                      \
+        return max_partitions;                                          \
+    }                                                                   \
+    part_ptr = (struct spm_partition_desc_t *)&(db->partitions[index]); \
+    PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition);       \
+    PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition);     \
+    index++;                                                            \
     }
 
-#define PARTITION_ADD_INIT_FUNC(partition, init_func) {                        \
-        extern int32_t init_func(void);                                        \
-        db_ptr =                                                               \
-               (uint32_t *)&(db->partitions[PARTITION_ID_GET(partition##_ID)]);\
-        ((struct spm_partition_region_t *)db_ptr)->partition_init = init_func; \
+#define DUMMY_PARTITION_ADD(partition) {                                  \
+    if (index >= max_partitions) {                                        \
+        return max_partitions;                                            \
+    }                                                                     \
+    part_ptr = (struct spm_partition_desc_t *)&(db->partitions[index]);   \
+    DUMMY_PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition);   \
+    /* The runtime data is the same for the dummy partitions*/            \
+    DUMMY_PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition); \
+    index++;                                                              \
+    }
+
+#define PARTITION_ADD_PERIPHERAL(partition, start, limit, bank, loc) {  \
+        part_ptr = &(db->partitions[PARTITION_ID_GET(partition##_ID)]); \
+        ((struct spm_partition_desc_t *)part_ptr)->                     \
+               static_data.periph_start = start;                        \
+        ((struct spm_partition_desc_t *)part_ptr)->                     \
+               static_data.periph_limit = limit;                        \
+        ((struct spm_partition_desc_t *)part_ptr)->                     \
+               static_data.periph_ppc_bank = bank;                      \
+        ((struct spm_partition_desc_t *)part_ptr)->                     \
+               static_data.periph_ppc_loc = loc;                        \
+    }
+
+#define PARTITION_ADD_INIT_FUNC(partition, init_func) {                 \
+        extern int32_t init_func(void);                                 \
+        part_ptr = &(db->partitions[PARTITION_ID_GET(partition##_ID)]); \
+        ((struct spm_partition_desc_t *)part_ptr)->                     \
+               static_data.partition_init = init_func;                  \
     }
 
 /*This file is meant to be included twice*/
@@ -222,7 +217,7 @@
                                 uint32_t max_partitions)
 {
     uint32_t index = 0;
-    uint32_t *db_ptr;
+    struct spm_partition_desc_t *part_ptr;
 
 #include "user_service_defines.inc"