Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix emulator build on MacOS #4242

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions core/SConscript.kernel
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,9 @@ else:

CPPDEFINES_MOD += [
'TRANSLATIONS',
'RSOD_IN_COREAPP',
]

if TREZOR_MODEL not in ('1', ):
CPPDEFINES_MOD += [
'FANCY_FATAL_ERROR',
]

CPPDEFINES_MOD += ['USE_SVC_SHUTDOWN']

if FEATURE_FLAGS["RDI"]:
CPPDEFINES_MOD += ['RDI']

Expand Down
4 changes: 0 additions & 4 deletions core/embed/bootloader/bootui.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
#include "display_draw.h"
#include "display_utils.h"
#include "fonts/fonts.h"
#ifdef TREZOR_EMULATOR
#include "emulator.h"
#else
#include "mini_printf.h"
#endif
#include "rust_ui.h"
#include "version.h"

Expand Down
1 change: 0 additions & 1 deletion core/embed/bootloader/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define __EMULATOR_H__

#define CLOCK_180_MHZ 0
#define mini_snprintf snprintf

#undef FIRMWARE_START

Expand Down
2 changes: 1 addition & 1 deletion core/embed/bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ void real_jump_to_firmware(void) {
__attribute__((noreturn)) void jump_to_fw_through_reset(void) {
display_fade(display_backlight(-1), 0, 200);

reboot();
reboot_device();
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion core/embed/extmod/modtrezorutils/modtrezorutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ STATIC mp_obj_t mod_trezorutils_reboot_to_bootloader(size_t n_args,
}
} else {
// Just reboot and go through the normal boot sequence
reboot();
reboot_device();
}

#endif
Expand Down
2 changes: 1 addition & 1 deletion core/embed/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static void coreapp_init(applet_t *applet) {

// Shows RSOD (Red Screen of Death)
static void show_rsod(const systask_postmortem_t *pminfo) {
#ifdef FANCY_FATAL_ERROR
#ifdef RSOD_IN_COREAPP
applet_t coreapp;
coreapp_init(&coreapp);

Expand Down
4 changes: 3 additions & 1 deletion core/embed/lib/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include "image.h"
#include "model.h"

_Static_assert(VENDOR_HEADER_MAX_SIZE + IMAGE_HEADER_SIZE <= IMAGE_CHUNK_SIZE);
_Static_assert(VENDOR_HEADER_MAX_SIZE + IMAGE_HEADER_SIZE <= IMAGE_CHUNK_SIZE,
"The size of the firmware headers must be less than or equal to "
"IMAGE_CHUNK_SIZE");

const uint8_t BOOTLOADER_KEY_M = 2;
const uint8_t BOOTLOADER_KEY_N = 3;
Expand Down
65 changes: 32 additions & 33 deletions core/embed/lib/mini_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,43 +99,38 @@ mini_itoa(int value, unsigned int radix, unsigned int uppercase, unsigned int un
return len;
}

inline static void _putc(char **cursor, char *const buffer_end, char ch)
{
if (*cursor < buffer_end)
*((*cursor)++) = ch;
}

inline static void _puts(char **cursor, char *const buffer_end, char *s, unsigned int len)
{
unsigned int i;

if (buffer_end - *cursor < len)
len = buffer_end - *cursor;

/* Copy to buffer at cursor */
for (i = 0; i < len; i++)
*((*cursor)++) = s[i];
}

int
mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va)
{
char *pbuffer = buffer;
char *const buffer_end = buffer + buffer_len;
char bf[24];
char ch;

int _putc(char ch)
{
if ((unsigned int)((pbuffer - buffer) + 1) >= buffer_len)
return 0;
*(pbuffer++) = ch;
*(pbuffer) = '\0';
return 1;
}

int _puts(char *s, unsigned int len)
{
unsigned int i;

if (buffer_len - (pbuffer - buffer) - 1 < len)
len = buffer_len - (pbuffer - buffer) - 1;

/* Copy to buffer */
for (i = 0; i < len; i++)
*(pbuffer++) = s[i];
*(pbuffer) = '\0';

return len;
}

while ((ch=*(fmt++))) {
if ((unsigned int)((pbuffer - buffer) + 1) >= buffer_len)
if (pbuffer + 1 >= buffer_end)
break;
if (ch!='%')
_putc(ch);
else {
if (ch!='%') {
_putc(&pbuffer, buffer_end, ch);
} else {
char zero_pad = 0;
char *ptr;
unsigned int len;
Expand All @@ -159,31 +154,35 @@ mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list v
case 'u':
case 'd':
len = mini_itoa(va_arg(va, unsigned int), 10, 0, (ch=='u'), bf, zero_pad);
_puts(bf, len);
_puts(&pbuffer, buffer_end, bf, len);
break;

case 'x':
case 'X':
len = mini_itoa(va_arg(va, unsigned int), 16, (ch=='X'), 1, bf, zero_pad);
_puts(bf, len);
_puts(&pbuffer, buffer_end, bf, len);
break;

case 'c' :
_putc((char)(va_arg(va, int)));
_putc(&pbuffer, buffer_end, (char)(va_arg(va, int)));
break;

case 's' :
ptr = va_arg(va, char*);
_puts(ptr, mini_strlen(ptr));
_puts(&pbuffer, buffer_end, ptr, mini_strlen(ptr));
break;

default:
_putc(ch);
_putc(&pbuffer, buffer_end, ch);
break;
}
}
}
end:
if (pbuffer < buffer_end)
*pbuffer = '\0';
else if (buffer_len > 0)
buffer[buffer_len - 1] = '\0';
return pbuffer - buffer;
}

Expand Down
6 changes: 3 additions & 3 deletions core/embed/lib/rsod.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void rsod_terminal(const systask_postmortem_t* pminfo) {

#endif // KERNEL_MODE

#if (defined(FIRMWARE) || defined(BOOTLOADER)) && defined(FANCY_FATAL_ERROR)
#ifdef FANCY_FATAL_ERROR

#include "rust_ui.h"

Expand Down Expand Up @@ -139,7 +139,7 @@ void rsod_gui(const systask_postmortem_t* pminfo) {
display_rsod_rust(title, message, footer);
}

#endif
#endif // FANCY_FATAL_ERROR

#ifdef KERNEL_MODE

Expand All @@ -153,7 +153,7 @@ static void init_and_show_rsod(const systask_postmortem_t* pminfo) {
// Initialize necessary drivers
display_init(DISPLAY_RESET_CONTENT);

#if (defined(FIRMWARE) || defined(BOOTLOADER)) && defined(FANCY_FATAL_ERROR)
#ifdef FANCY_FATAL_ERROR
// Show the RSOD using Rust GUI
rsod_gui(pminfo);
#else
Expand Down
21 changes: 5 additions & 16 deletions core/embed/lib/terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "terminal.h"
#include TREZOR_BOARD

#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include "display.h"
#include TREZOR_BOARD

#include "display.h"
#include "fonts/fonts.h"
#include "gfx_draw.h"
#include "mini_printf.h"
#include "terminal.h"

#define TERMINAL_COLS (DISPLAY_RESX / 6)
#define TERMINAL_ROWS (DISPLAY_RESY / 8)
Expand Down Expand Up @@ -169,29 +171,16 @@ void term_print(const char *text, int textlen) {
#endif
}

#ifdef TREZOR_EMULATOR
#define mini_vsnprintf vsnprintf
#include <stdio.h>
#else
#include "mini_printf.h"
#endif

// variadic term_print
void term_printf(const char *fmt, ...) {
if (!strchr(fmt, '%')) {
term_print(fmt, strlen(fmt));
#ifdef TREZOR_EMULATOR
printf("%s", fmt);
#endif
} else {
va_list va;
va_start(va, fmt);
char buf[256] = {0};
int len = mini_vsnprintf(buf, sizeof(buf), fmt, va);
term_print(buf, len);
#ifdef TREZOR_EMULATOR
vprintf(fmt, va);
#endif
va_end(va);
}
}
2 changes: 1 addition & 1 deletion core/embed/prodtest/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ static void test_otp_write_device_variant(const char *args) {
vcp_println("OK");
}

static void test_reboot(void) { reboot(); }
static void test_reboot(void) { reboot_device(); }

void cpuid_read(void) {
uint32_t cpuid[3];
Expand Down
2 changes: 1 addition & 1 deletion core/embed/trezorhal/bootutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <stdint.h>

// Immediately resets the device and initiates the normal boot sequence.
void __attribute__((noreturn)) reboot(void);
void __attribute__((noreturn)) reboot_device(void);

// Resets the device and enters the bootloader,
// halting there and waiting for further user instructions.
Expand Down
2 changes: 1 addition & 1 deletion core/embed/trezorhal/stm32f4/bootutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void reboot_and_upgrade(const uint8_t hash[32]) {
reboot_with_args(BOOT_COMMAND_INSTALL_UPGRADE, hash, 32);
}

void reboot(void) {
void reboot_device(void) {
bootargs_set(BOOT_COMMAND_NONE, NULL, 0);

#ifdef STM32U5
Expand Down
4 changes: 2 additions & 2 deletions core/embed/trezorhal/stm32f4/syscall_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ __attribute((no_stack_protector)) void syscall_handler(uint32_t *args,
case SYSCALL_SECURE_SHUTDOWN:
secure_shutdown();
break;
case SYSCALL_REBOOT:
reboot();
case SYSCALL_REBOOT_DEVICE:
reboot_device();
break;
case SYSCALL_REBOOT_TO_BOOTLOADER:
reboot_to_bootloader();
Expand Down
2 changes: 1 addition & 1 deletion core/embed/trezorhal/stm32f4/syscall_numbers.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef enum {
SYSCALL_SYSTICK_US_TO_CYCLES,

SYSCALL_SECURE_SHUTDOWN,
SYSCALL_REBOOT,
SYSCALL_REBOOT_DEVICE,
SYSCALL_REBOOT_TO_BOOTLOADER,
SYSCALL_REBOOT_AND_UPGRADE,

Expand Down
4 changes: 2 additions & 2 deletions core/embed/trezorhal/stm32f4/syscall_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ void reboot_and_upgrade(const uint8_t hash[32]) {
;
}

void reboot(void) {
syscall_invoke0(SYSCALL_REBOOT);
void reboot_device(void) {
syscall_invoke0(SYSCALL_REBOOT_DEVICE);
while (1)
;
}
Expand Down
4 changes: 2 additions & 2 deletions core/embed/trezorhal/stm32u5/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static secbool secret_present(uint32_t offset, uint32_t len) {
// read access to it.
static void secret_bhk_load(void) {
if (sectrue == secret_bhk_locked()) {
reboot();
reboot_device();
}

uint32_t secret[SECRET_BHK_LEN / sizeof(uint32_t)] = {0};
Expand Down Expand Up @@ -355,7 +355,7 @@ void secret_prepare_fw(secbool allow_run_with_secret, secbool trust_all) {

void secret_init(void) {
if (secret_bhk_locked() == sectrue) {
reboot();
reboot_device();
}

secret_ensure_initialized();
Expand Down
2 changes: 1 addition & 1 deletion core/embed/trezorhal/unix/display_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ void display_copy_mono1p(const gfx_bitblt_t *bb) {
gfx_bitblt_t bb_new = *bb;
bb_new.dst_row =
(uint8_t *)drv->buffer->pixels + (drv->buffer->pitch * bb_new.dst_y);
bb_new.dst_stride = DISPLAY_RESX;
bb_new.dst_stride = drv->buffer->pitch;

gfx_rgb565_copy_mono1p(&bb_new);
}
Expand Down
13 changes: 7 additions & 6 deletions core/embed/unix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@
#include <sys/types.h>
#include <unistd.h>

#include "common.h"
#include "display.h"
#include "extmod/misc.h"
#include "extmod/vfs_posix.h"
#include "flash.h"
#include "flash_otp.h"
#include "genhdr/mpversion.h"
#include "input.h"
#include "rsod.h"
#include "system.h"
#include "systimer.h"
#include "touch.h"

#include "py/builtin.h"
#include "py/compile.h"
#include "py/gc.h"
Expand All @@ -53,10 +59,6 @@
#include "py/repl.h"
#include "py/runtime.h"
#include "py/stackctrl.h"
#include "systimer.h"
#include "touch.h"

#include "common.h"

// Command line options, with their defaults
STATIC bool compile_only = false;
Expand Down Expand Up @@ -487,8 +489,7 @@ MP_NOINLINE int main_(int argc, char **argv) {

pre_process_options(argc, argv);

systick_init();
systimer_init();
system_init(&rsod_panic_handler);

display_init(DISPLAY_RESET_CONTENT);

Expand Down
Loading