tools: manifest_compiler: Add non_critical_app to mgmt_flags

If non_critical_app is set apps can exit with a non-0 exit code
(exit(0), abort() or fault) and not cause a panic. To restart the app
after that, use one of the existing manifest start/restart flags.

Bug: 147698516
Change-Id: Ibd0c6acc1632079bfe4fc87ddf559a5e33ba4e47
diff --git a/tools/manifest_compiler.py b/tools/manifest_compiler.py
index 630e067..0dbff3c 100755
--- a/tools/manifest_compiler.py
+++ b/tools/manifest_compiler.py
@@ -36,7 +36,9 @@
         "mem_map": [{"id": 1, "addr": "0x70000000", "size": "0x1000"}, \
                 {"id": 2, "addr": "0x70010000", "size": "0x100"}, \
                 {"id": 3, "addr": "0x70020000", "size": "0x4"}],
-        "mgmt_flags": {"restart_on_exit": true, "deferred_start": false}
+        "mgmt_flags": {"restart_on_exit": true, \
+                "deferred_start": false, \
+                "non_critical_app": false},
         "start_ports": [{"name": "LOADABLE_START_PORT", \
                 "flags": {"allow_ta_connect": true, "allow_ns_connect": false}}]
    }
@@ -77,6 +79,7 @@
 MGMT_FLAGS = "mgmt_flags"
 MGMT_FLAG_RESTART_ON_EXIT = "restart_on_exit"
 MGMT_FLAG_DEFERRED_START = "deferred_start"
+MGMT_FLAG_NON_CRITICAL_APP = "non_critical_app"
 START_PORTS = "start_ports"
 START_PORT_FLAGS = "flags"
 START_PORT_NAME = "name"
@@ -108,6 +111,7 @@
 TRUSTY_APP_MGMT_FLAGS_NONE = 0
 TRUSTY_APP_MGMT_FLAGS_RESTART_ON_EXIT = 1 << 0
 TRUSTY_APP_MGMT_FLAGS_DEFERRED_START = 1 << 1
+TRUSTY_APP_MGMT_FLAGS_NON_CRITICAL_APP = 1 << 2
 
 # START_PORT flags
 # These values need to be kept in sync with user/base/include/user/trusty_ipc.h
@@ -153,9 +157,10 @@
 
 
 class MgmtFlags(object):
-    def __init__(self, restart_on_exit, deferred_start):
+    def __init__(self, restart_on_exit, deferred_start, non_critical_app):
         self.restart_on_exit = restart_on_exit
         self.deferred_start = deferred_start
+        self.non_critical_app = non_critical_app
 
 
 '''
@@ -480,7 +485,8 @@
 
     mgmt_flags = MgmtFlags(
             get_boolean(flags, MGMT_FLAG_RESTART_ON_EXIT, constants, log, optional=True),
-            get_boolean(flags, MGMT_FLAG_DEFERRED_START, constants, log, optional=True))
+            get_boolean(flags, MGMT_FLAG_DEFERRED_START, constants, log, optional=True),
+            get_boolean(flags, MGMT_FLAG_NON_CRITICAL_APP, constants, log, optional=True))
 
     if flags:
         log.error("Unknown atributes in mgmt_flags entries in manifest: {} "
@@ -549,7 +555,8 @@
             get_dict(manifest_dict, MGMT_FLAGS, log, optional=True,
                      default={
                              MGMT_FLAG_RESTART_ON_EXIT: False,
-                             MGMT_FLAG_DEFERRED_START: False}),
+                             MGMT_FLAG_DEFERRED_START: False,
+                             MGMT_FLAG_NON_CRITICAL_APP: False}),
                      constants, log)
 
     # START_PORTS
@@ -587,6 +594,8 @@
         flags |= TRUSTY_APP_MGMT_FLAGS_RESTART_ON_EXIT
     if mgmt_flags.deferred_start:
         flags |= TRUSTY_APP_MGMT_FLAGS_DEFERRED_START
+    if mgmt_flags.non_critical_app:
+        flags |= TRUSTY_APP_MGMT_FLAGS_NON_CRITICAL_APP
 
     return flags
 
@@ -725,12 +734,15 @@
                     "I", packed_data[:4]), packed_data[4:]
             mgmt_flag = {
                     MGMT_FLAG_RESTART_ON_EXIT: False,
-                    MGMT_FLAG_DEFERRED_START: False
+                    MGMT_FLAG_DEFERRED_START: False,
+                    MGMT_FLAG_NON_CRITICAL_APP: False
             }
             if flag & TRUSTY_APP_MGMT_FLAGS_RESTART_ON_EXIT:
                 mgmt_flag[MGMT_FLAG_RESTART_ON_EXIT] = True
             if flag &  TRUSTY_APP_MGMT_FLAGS_DEFERRED_START:
                 mgmt_flag[MGMT_FLAG_DEFERRED_START] = True
+            if flag & TRUSTY_APP_MGMT_FLAGS_NON_CRITICAL_APP:
+                mgmt_flag[MGMT_FLAG_NON_CRITICAL_APP] = True
             manifest[MGMT_FLAGS] = mgmt_flag
         elif tag == TRUSTY_APP_CONFIG_KEY_START_PORT:
             if START_PORTS not in manifest:
diff --git a/tools/test_manifest_compiler.py b/tools/test_manifest_compiler.py
index 039a68a..cea3171 100755
--- a/tools/test_manifest_compiler.py
+++ b/tools/test_manifest_compiler.py
@@ -592,10 +592,12 @@
         constants = {}
         mgmt_flags_ref_data = {
                 manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: True,
-                manifest_compiler.MGMT_FLAG_DEFERRED_START: True}
+                manifest_compiler.MGMT_FLAG_DEFERRED_START: True,
+                manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: True}
         mgmt_flags_data = {
                 manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: True,
-                manifest_compiler.MGMT_FLAG_DEFERRED_START: True}
+                manifest_compiler.MGMT_FLAG_DEFERRED_START: True,
+                manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: True}
 
         log = manifest_compiler.Log()
         mgmt_flags = manifest_compiler.parse_mgmt_flags(
@@ -607,6 +609,9 @@
         self.assertEqual(mgmt_flags.deferred_start,
                          mgmt_flags_ref_data[
                              manifest_compiler.MGMT_FLAG_DEFERRED_START])
+        self.assertEqual(mgmt_flags.non_critical_app,
+                         mgmt_flags_ref_data[
+                             manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP])
 
     '''
     Test with a valid management flags
@@ -615,10 +620,12 @@
         constants = {}
         mgmt_flags_ref_data = {
                 manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: False,
-                manifest_compiler.MGMT_FLAG_DEFERRED_START: False}
+                manifest_compiler.MGMT_FLAG_DEFERRED_START: False,
+                manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: False}
         mgmt_flags_data = {
                 manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: False,
-                manifest_compiler.MGMT_FLAG_DEFERRED_START: False}
+                manifest_compiler.MGMT_FLAG_DEFERRED_START: False,
+                manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: False}
 
         log = manifest_compiler.Log()
         mgmt_flags = manifest_compiler.parse_mgmt_flags(
@@ -630,6 +637,9 @@
         self.assertEqual(mgmt_flags.deferred_start,
                          mgmt_flags_ref_data[
                              manifest_compiler.MGMT_FLAG_DEFERRED_START])
+        self.assertEqual(mgmt_flags.non_critical_app,
+                         mgmt_flags_ref_data[
+                             manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP])
 
     '''
     Test with a valid management flags
@@ -638,10 +648,12 @@
         constants = {}
         mgmt_flags_ref_data = {
                 manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: False,
-                manifest_compiler.MGMT_FLAG_DEFERRED_START: True}
+                manifest_compiler.MGMT_FLAG_DEFERRED_START: True,
+                manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: False}
         mgmt_flags_data = {
                 manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: False,
-                manifest_compiler.MGMT_FLAG_DEFERRED_START: True}
+                manifest_compiler.MGMT_FLAG_DEFERRED_START: True,
+                manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: False}
 
         log = manifest_compiler.Log()
         mgmt_flags = manifest_compiler.parse_mgmt_flags(
@@ -653,6 +665,9 @@
         self.assertEqual(mgmt_flags.deferred_start,
                          mgmt_flags_ref_data[
                              manifest_compiler.MGMT_FLAG_DEFERRED_START])
+        self.assertEqual(mgmt_flags.non_critical_app,
+                         mgmt_flags_ref_data[
+                             manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP])
 
     '''
     Test with a management flags missing
@@ -668,6 +683,7 @@
         self.assertFalse(log.error_occurred())
         self.assertIsNone(mgmt_flags.restart_on_exit)
         self.assertTrue(mgmt_flags.deferred_start)
+        self.assertIsNone(mgmt_flags.non_critical_app)
 
     '''
     Test with a empty management flags"
@@ -682,6 +698,7 @@
         self.assertFalse(log.error_occurred())
         self.assertIsNone(mgmt_flags.restart_on_exit)
         self.assertIsNone(mgmt_flags.deferred_start)
+        self.assertIsNone(mgmt_flags.non_critical_app)
 
     '''
     Test with a mgmt_flags as array of flags
@@ -690,7 +707,8 @@
         constants = {}
         config_data = {manifest_compiler.MGMT_FLAGS: [{
                 manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: True,
-                manifest_compiler.MGMT_FLAG_DEFERRED_START: True}]}
+                manifest_compiler.MGMT_FLAG_DEFERRED_START: True,
+                manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: True}]}
 
         log = manifest_compiler.Log()
         mgmt_flags = manifest_compiler.parse_mgmt_flags(
@@ -887,6 +905,7 @@
             self.assertEqual(memio_map.size, int(size, 0))
         self.assertFalse(manifest.mgmt_flags.restart_on_exit)
         self.assertFalse(manifest.mgmt_flags.deferred_start)
+        self.assertFalse(manifest.mgmt_flags.non_critical_app)
 
     '''
     Test with invalid value in config,
@@ -1192,7 +1211,8 @@
                 manifest_compiler.MIN_STACK: 4096,
                 manifest_compiler.MGMT_FLAGS: {
                         manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: False,
-                        manifest_compiler.MGMT_FLAG_DEFERRED_START: False
+                        manifest_compiler.MGMT_FLAG_DEFERRED_START: False,
+                        manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: False
                 }
         }
 
@@ -1237,7 +1257,8 @@
                         {"id": 3, "addr": "0x70020000", "size": "0x4"}],
                 manifest_compiler.MGMT_FLAGS: {
                         manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: False,
-                        manifest_compiler.MGMT_FLAG_DEFERRED_START: False
+                        manifest_compiler.MGMT_FLAG_DEFERRED_START: False,
+                        manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: False
                 }
         }
 
@@ -1282,7 +1303,8 @@
                 manifest_compiler.MIN_STACK: 4096,
                 manifest_compiler.MGMT_FLAGS: {
                         manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: True,
-                        manifest_compiler.MGMT_FLAG_DEFERRED_START: False
+                        manifest_compiler.MGMT_FLAG_DEFERRED_START: False,
+                        manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: False
                 }
         }
 
@@ -1327,7 +1349,8 @@
                 ],
                 manifest_compiler.MGMT_FLAGS: {
                         manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: False,
-                        manifest_compiler.MGMT_FLAG_DEFERRED_START: False
+                        manifest_compiler.MGMT_FLAG_DEFERRED_START: False,
+                        manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: False
                 }
         }
 
@@ -1406,7 +1429,8 @@
                 ],
                 manifest_compiler.MGMT_FLAGS: {
                         manifest_compiler.MGMT_FLAG_RESTART_ON_EXIT: False,
-                        manifest_compiler.MGMT_FLAG_DEFERRED_START: False
+                        manifest_compiler.MGMT_FLAG_DEFERRED_START: False,
+                        manifest_compiler.MGMT_FLAG_NON_CRITICAL_APP: False
                 }
         }