refactor(core): allow running coreapp emulator as an applet
What changed, and why it matters
This commit is a code cleanup that lets the Trezor firmware's emulator (used for testing on regular computers) run the core application as a separate 'applet' task, similar to how it runs on real hardware. It merges two setup functions into one and adds a Unix-specific implementation. There is no indication this fixes a security bug or introduces a vulnerability.
No security action required. Treat as routine refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors coreapp initialization: coreapp_init() and coreapp_reset() are merged into a single coreapp_init() function. On STM32, the behavior is functionally equivalent. A new core/embed/sys/task/unix/coreapp.c is added so the emulator can initialize coreapp as an applet using systask_init() and systask_push_call() to invoke coreapp_emu(). The build system is updated to include the new Unix applet files when the ‘applet’ feature is enabled. The commit is tagged ‘[no changelog]’ and contains no security-related content.
Changed components
core/embed/projects/kernel/main.ccore/embed/sys/task/inc/sys/applet.hcore/embed/sys/task/inc/sys/coreapp.hcore/embed/sys/task/stm32/coreapp.ccore/embed/sys/task/unix/coreapp.ccore/site_scons/models/unix_common.pyInspect captured patch +82 / −26
diff --git a/core/embed/projects/kernel/main.c b/core/embed/projects/kernel/main.c
index 4d48eb4d3..b6aafe78e 100644
--- a/core/embed/projects/kernel/main.c
+++ b/core/embed/projects/kernel/main.c
@@ -223,10 +223,9 @@ static void kernel_loop(applet_t *coreapp) {
static void show_rsod(const systask_postmortem_t *pminfo) {
#ifdef RSOD_IN_COREAPP
applet_t coreapp;
- coreapp_init(&coreapp);
// Reset and run the coreapp in RSOD mode
- if (coreapp_reset(&coreapp, 1, pminfo, sizeof(systask_postmortem_t))) {
+ if (coreapp_init(&coreapp, 1, pminfo, sizeof(systask_postmortem_t))) {
// Run the applet & wait for it to finish
applet_run(&coreapp);
// Loop until the coreapp is terminated
@@ -286,10 +285,9 @@ int main(void) {
// Initialize coreapp task
applet_t coreapp;
- coreapp_init(&coreapp);
// Reset and run the coreapp
- if (!coreapp_reset(&coreapp, 0, NULL, 0)) {
+ if (!coreapp_init(&coreapp, 0, NULL, 0)) {
error_shutdown("Cannot start coreapp");
}
diff --git a/core/embed/sys/task/inc/sys/applet.h b/core/embed/sys/task/inc/sys/applet.h
index 71acfc4b2..8188dc933 100644
--- a/core/embed/sys/task/inc/sys/applet.h
+++ b/core/embed/sys/task/inc/sys/applet.h
@@ -36,10 +36,8 @@ typedef struct {
applet_layout_t layout;
// Applet privileges
applet_privileges_t privileges;
-
// Applet task
systask_t task;
-
} applet_t;
// Initializes the applet structure
diff --git a/core/embed/sys/task/inc/sys/coreapp.h b/core/embed/sys/task/inc/sys/coreapp.h
index bb550a5a9..492ddf105 100644
--- a/core/embed/sys/task/inc/sys/coreapp.h
+++ b/core/embed/sys/task/inc/sys/coreapp.h
@@ -45,20 +45,31 @@ typedef struct {
void* saes_callback;
} coreapp_header_t;
-// Initializes the coreapp applet structure
-void coreapp_init(applet_t* applet);
+#ifdef TREZOR_EMULATOR
-// Resets the coreapp and prepares it for execution from its entry point.
+// Initializes the coreapp and prepares it for execution from its entry point.
//
// Coreapp does not start immediately, it needs to be run by
// `applet_run()` after calling this function.
//
-// Returns `true` if the applet was successfully reset.
-bool coreapp_reset(applet_t* applet, uint32_t cmd, const void* arg,
- size_t arg_size);
+// Returns `true` if the applet was successfully initialized.
+bool coreapp_init(applet_t* applet, int argc, char** argv);
+
+#else
+
+// Initializes the coreapp and prepares it for execution from its entry point.
+//
+// Coreapp does not start immediately, it needs to be run by
+// `applet_run()` after calling this function.
+//
+// Returns `true` if the applet was successfully initialized.
+bool coreapp_init(applet_t* applet, uint32_t cmd, const void* arg,
+ size_t arg_size);
mpu_area_t coreapp_get_code_area(void);
mpu_area_t coreapp_get_tls_area(void);
+#endif // TREZOR_EMULATOR
+
#endif // KERNEL_MODE
diff --git a/core/embed/sys/task/stm32/coreapp.c b/core/embed/sys/task/stm32/coreapp.c
index 48abf1422..5b2c071b3 100644
--- a/core/embed/sys/task/stm32/coreapp.c
+++ b/core/embed/sys/task/stm32/coreapp.c
@@ -36,8 +36,17 @@ static mpu_area_t coreapp_tls_area;
extern uint32_t _kernel_flash_end;
#define KERNEL_END ALIGN_UP((uint32_t) & _kernel_flash_end, COREAPP_ALIGNMENT)
-// Initializes coreapp applet
-void coreapp_init(applet_t* applet) {
+static void coreapp_clear_memory(applet_t* applet) {
+ if (applet->layout.data1.size > 0) {
+ memset((void*)applet->layout.data1.start, 0, applet->layout.data1.size);
+ }
+ if (applet->layout.data2.size > 0) {
+ memset((void*)applet->layout.data2.start, 0, applet->layout.data2.size);
+ }
+}
+
+bool coreapp_init(applet_t* applet, uint32_t cmd, const void* arg,
+ size_t arg_size) {
const uint32_t CODE1_START = KERNEL_END;
#ifdef FIRMWARE_P1_START
@@ -66,19 +75,7 @@ void coreapp_init(applet_t* applet) {
};
applet_init(applet, &coreapp_layout, &coreapp_privileges);
-}
-
-static void coreapp_clear_memory(applet_t* applet) {
- if (applet->layout.data1.size > 0) {
- memset((void*)applet->layout.data1.start, 0, applet->layout.data1.size);
- }
- if (applet->layout.data2.size > 0) {
- memset((void*)applet->layout.data2.start, 0, applet->layout.data2.size);
- }
-}
-bool coreapp_reset(applet_t* applet, uint32_t cmd, const void* arg,
- size_t arg_size) {
// Enable access to coreapp memory regions
mpu_set_active_applet(&applet->layout);
diff --git a/core/embed/sys/task/unix/coreapp.c b/core/embed/sys/task/unix/coreapp.c
new file mode 100644
index 000000000..b0368e1e5
--- /dev/null
+++ b/core/embed/sys/task/unix/coreapp.c
@@ -0,0 +1,48 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef KERNEL
+
+#include <trezor_rtl.h>
+
+#include <sys/applet.h>
+#include <sys/coreapp.h>
+#include <sys/systask.h>
+
+extern int coreapp_emu(int argc, char** argv);
+
+bool coreapp_init(applet_t* applet, int argc, char** argv) {
+ const applet_layout_t coreapp_layout = {0};
+ const applet_privileges_t coreapp_privileges = {0};
+
+ applet_init(applet, &coreapp_layout, &coreapp_privileges);
+
+ if (!systask_init(&applet->task, 0, 0, 0, applet)) {
+ return false;
+ }
+
+ if (!systask_push_call(&applet->task, (void*)coreapp_emu, (uintptr_t)argc,
+ (uintptr_t)argv, 0)) {
+ return false;
+ }
+
+ return true;
+}
+
+#endif // KERNEL
diff --git a/core/site_scons/models/unix_common.py b/core/site_scons/models/unix_common.py
index b02be0198..9e9879e50 100644
--- a/core/site_scons/models/unix_common.py
+++ b/core/site_scons/models/unix_common.py
@@ -98,4 +98,8 @@ def unix_common_files(env, features_wanted, defines, sources, paths):
defines += [("USE_IPC", "1")]
paths += ["embed/sys/ipc/inc"]
+ if "applet" in features_wanted:
+ sources += ["embed/sys/task/unix/applet.c"]
+ sources += ["embed/sys/task/unix/coreapp.c"]
+
return features_available
Why this scored 11/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.