1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

refactor(core/embed): move&rename display terminal functions

[no changelog]
This commit is contained in:
cepetr 2024-01-17 15:29:49 +01:00 committed by cepetr
parent 139701fb1a
commit 446c3b77b4
16 changed files with 225 additions and 164 deletions

View File

@ -63,7 +63,9 @@ SOURCE_MOD += [
'embed/lib/fonts/font_bitmap.c',
'embed/lib/image.c',
'embed/lib/mini_printf.c',
'embed/lib/terminal.c',
'vendor/trezor-storage/flash_common.c',
]
SOURCE_BOARDLOADER = [

View File

@ -87,6 +87,7 @@ SOURCE_MOD += [
'embed/lib/fonts/fonts.c',
'embed/lib/fonts/font_bitmap.c',
'embed/lib/image.c',
'embed/lib/terminal.c',
'embed/extmod/modtrezorcrypto/rand.c',
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',

View File

@ -75,6 +75,7 @@ CPPPATH_MOD += [
]
SOURCE_MOD += [
'embed/lib/display.c',
'embed/lib/terminal.c',
'embed/lib/colors.c',
'embed/lib/fonts/fonts.c',
'embed/lib/fonts/font_bitmap.c',

View File

@ -84,6 +84,7 @@ SOURCE_MOD += [
'embed/lib/fonts/fonts.c',
'embed/lib/fonts/font_bitmap.c',
'embed/lib/image.c',
'embed/lib/terminal.c',
'embed/lib/touch.c',
'embed/extmod/modtrezorcrypto/rand.c',
'vendor/micropython/lib/uzlib/adler32.c',

View File

@ -194,6 +194,7 @@ SOURCE_MOD += [
'embed/lib/fonts/fonts.c',
'embed/lib/fonts/font_bitmap.c',
'embed/lib/image.c',
'embed/lib/terminal.c',
'embed/extmod/modtrezorui/modtrezorui.c',
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',

View File

@ -85,6 +85,7 @@ SOURCE_MOD += [
'embed/lib/fonts/font_bitmap.c',
'embed/lib/image.c',
'embed/lib/mini_printf.c',
'embed/lib/terminal.c',
'embed/lib/qr-code-generator/qrcodegen.c',
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',

View File

@ -60,6 +60,7 @@ SOURCE_MOD += [
'embed/lib/fonts/font_bitmap.c',
'embed/lib/image.c',
'embed/lib/mini_printf.c',
'embed/lib/terminal.c',
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',
'vendor/micropython/lib/uzlib/tinflate.c',

View File

@ -194,6 +194,7 @@ SOURCE_MOD += [
'embed/lib/fonts/fonts.c',
'embed/lib/fonts/font_bitmap.c',
'embed/lib/image.c',
'embed/lib/terminal.c',
'embed/extmod/modtrezorui/modtrezorui.c',
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',

View File

@ -28,6 +28,7 @@
#include "image.h"
#include "model.h"
#include "rng.h"
#include "terminal.h"
#ifdef USE_SD_CARD
#include "sdcard.h"
#endif
@ -116,43 +117,43 @@ static uint32_t check_sdcard(void) {
return 0;
}
static void progress_callback(int pos, int len) { display_printf("."); }
static void progress_callback(int pos, int len) { term_printf("."); }
static secbool copy_sdcard(void) {
display_backlight(255);
display_printf("Trezor Boardloader\n");
display_printf("==================\n\n");
term_printf("Trezor Boardloader\n");
term_printf("==================\n\n");
display_printf("bootloader found on the SD card\n\n");
display_printf("applying bootloader in 10 seconds\n\n");
display_printf("unplug now if you want to abort\n\n");
term_printf("bootloader found on the SD card\n\n");
term_printf("applying bootloader in 10 seconds\n\n");
term_printf("unplug now if you want to abort\n\n");
uint32_t codelen;
for (int i = 10; i >= 0; i--) {
display_printf("%d ", i);
term_printf("%d ", i);
hal_delay(1000);
codelen = check_sdcard();
if (0 == codelen) {
display_printf("\n\nno SD card, aborting\n");
term_printf("\n\nno SD card, aborting\n");
return secfalse;
}
}
display_printf("\n\nerasing flash:\n\n");
term_printf("\n\nerasing flash:\n\n");
// erase all flash (except boardloader)
if (sectrue != flash_area_erase(&ALL_WIPE_AREA, progress_callback)) {
display_printf(" failed\n");
term_printf(" failed\n");
return secfalse;
}
display_printf(" done\n\n");
term_printf(" done\n\n");
ensure(flash_unlock_write(), NULL);
// copy bootloader from SD card to Flash
display_printf("copying new bootloader from SD card\n\n");
term_printf("copying new bootloader from SD card\n\n");
ensure(sdcard_power_on(), NULL);
@ -171,8 +172,8 @@ static secbool copy_sdcard(void) {
sdcard_power_off();
ensure(flash_lock_write(), NULL);
display_printf("\ndone\n\n");
display_printf("Unplug the device and remove the SD card\n");
term_printf("\ndone\n\n");
term_printf("Unplug the device and remove the SD card\n");
return sectrue;
}

View File

@ -190,112 +190,6 @@ bool display_toif_info(const uint8_t *data, uint32_t len, uint16_t *out_w,
return true;
}
#ifndef TREZOR_PRINT_DISABLE
#define DISPLAY_PRINT_COLS (DISPLAY_RESX / 6)
#define DISPLAY_PRINT_ROWS (DISPLAY_RESY / 8)
static char display_print_buf[DISPLAY_PRINT_ROWS][DISPLAY_PRINT_COLS];
static uint16_t display_print_fgcolor = COLOR_WHITE,
display_print_bgcolor = COLOR_BLACK;
// set colors for display_print function
void display_print_color(uint16_t fgcolor, uint16_t bgcolor) {
display_print_fgcolor = fgcolor;
display_print_bgcolor = bgcolor;
}
// display text using bitmap font
void display_print(const char *text, int textlen) {
static uint8_t row = 0, col = 0;
// determine text length if not provided
if (textlen < 0) {
textlen = strlen(text);
}
// print characters to internal buffer (display_print_buf)
for (int i = 0; i < textlen; i++) {
switch (text[i]) {
case '\r':
break;
case '\n':
row++;
col = 0;
break;
default:
display_print_buf[row][col] = text[i];
col++;
break;
}
if (col >= DISPLAY_PRINT_COLS) {
col = 0;
row++;
}
if (row >= DISPLAY_PRINT_ROWS) {
for (int j = 0; j < DISPLAY_PRINT_ROWS - 1; j++) {
memcpy(display_print_buf[j], display_print_buf[j + 1],
DISPLAY_PRINT_COLS);
}
memzero(display_print_buf[DISPLAY_PRINT_ROWS - 1], DISPLAY_PRINT_COLS);
row = DISPLAY_PRINT_ROWS - 1;
}
}
// render buffer to display
display_set_window(0, 0, DISPLAY_RESX - 1, DISPLAY_RESY - 1);
for (int i = 0; i < DISPLAY_RESX * DISPLAY_RESY; i++) {
int x = (i % DISPLAY_RESX);
int y = (i / DISPLAY_RESX);
const int j = y % 8;
y /= 8;
const int k = x % 6;
x /= 6;
char c = 0;
if (x < DISPLAY_PRINT_COLS && y < DISPLAY_PRINT_ROWS) {
c = display_print_buf[y][x] & 0x7F;
// char invert = display_print_buf[y][x] & 0x80;
} else {
c = ' ';
}
if (c < ' ') {
c = ' ';
}
const uint8_t *g = Font_Bitmap + (5 * (c - ' '));
if (k < 5 && (g[k] & (1 << j))) {
PIXELDATA(display_print_fgcolor);
} else {
PIXELDATA(display_print_bgcolor);
}
}
display_pixeldata_dirty();
display_refresh();
}
#ifdef TREZOR_EMULATOR
#define mini_vsnprintf vsnprintf
#include <stdio.h>
#else
#include "mini_printf.h"
#endif
// variadic display_print
void display_printf(const char *fmt, ...) {
if (!strchr(fmt, '%')) {
display_print(fmt, strlen(fmt));
} else {
va_list va;
va_start(va, fmt);
char buf[256] = {0};
int len = mini_vsnprintf(buf, sizeof(buf), fmt, va);
display_print(buf, len);
va_end(va);
}
}
#endif // TREZOR_PRINT_DISABLE
#ifdef FRAMEBUFFER
static void display_text_render(int x, int y, const char *text, int textlen,
int font, uint16_t fgcolor, uint16_t bgcolor) {

View File

@ -45,13 +45,6 @@ void display_bar(int x, int y, int w, int h, uint16_t c);
bool display_toif_info(const uint8_t *buf, uint32_t len, uint16_t *out_w,
uint16_t *out_h, toif_format_t *out_format);
#ifndef TREZOR_PRINT_DISABLE
void display_print_color(uint16_t fgcolor, uint16_t bgcolor);
void display_print(const char *text, int textlen);
void display_printf(const char *fmt, ...)
__attribute__((__format__(__printf__, 1, 2)));
#endif
void display_text(int x, int y, const char *text, int textlen, int font,
uint16_t fgcolor, uint16_t bgcolor);
void display_text_center(int x, int y, const char *text, int textlen, int font,

131
core/embed/lib/terminal.c Normal file
View File

@ -0,0 +1,131 @@
/*
* 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/>.
*/
#include "terminal.h"
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include "display.h"
#include TREZOR_BOARD
#ifndef TREZOR_PRINT_DISABLE
#define TERMINAL_COLS (DISPLAY_RESX / 6)
#define TERMINAL_ROWS (DISPLAY_RESY / 8)
static char terminal_fb[TERMINAL_ROWS][TERMINAL_COLS];
static uint16_t terminal_fgcolor = COLOR_WHITE;
static uint16_t terminal_bgcolor = COLOR_BLACK;
// set colors for display_print function
void term_set_color(uint16_t fgcolor, uint16_t bgcolor) {
terminal_fgcolor = fgcolor;
terminal_bgcolor = bgcolor;
}
// display text using bitmap font
void term_print(const char *text, int textlen) {
static uint8_t row = 0, col = 0;
// determine text length if not provided
if (textlen < 0) {
textlen = strlen(text);
}
// print characters to internal buffer (terminal_fb)
for (int i = 0; i < textlen; i++) {
switch (text[i]) {
case '\r':
break;
case '\n':
row++;
col = 0;
break;
default:
terminal_fb[row][col] = text[i];
col++;
break;
}
if (col >= TERMINAL_COLS) {
col = 0;
row++;
}
if (row >= TERMINAL_ROWS) {
for (int j = 0; j < TERMINAL_ROWS - 1; j++) {
memcpy(terminal_fb[j], terminal_fb[j + 1], TERMINAL_COLS);
}
memset(terminal_fb[TERMINAL_ROWS - 1], 0, TERMINAL_COLS);
row = TERMINAL_ROWS - 1;
}
}
// render buffer to display
display_set_window(0, 0, DISPLAY_RESX - 1, DISPLAY_RESY - 1);
for (int i = 0; i < DISPLAY_RESX * DISPLAY_RESY; i++) {
int x = (i % DISPLAY_RESX);
int y = (i / DISPLAY_RESX);
const int j = y % 8;
y /= 8;
const int k = x % 6;
x /= 6;
char c = 0;
if (x < TERMINAL_COLS && y < TERMINAL_ROWS) {
c = terminal_fb[y][x] & 0x7F;
// char invert = terminal_fb[y][x] & 0x80;
} else {
c = ' ';
}
if (c < ' ') {
c = ' ';
}
const uint8_t *g = Font_Bitmap + (5 * (c - ' '));
if (k < 5 && (g[k] & (1 << j))) {
PIXELDATA(terminal_fgcolor);
} else {
PIXELDATA(terminal_bgcolor);
}
}
display_pixeldata_dirty();
display_refresh();
}
#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));
} 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);
va_end(va);
}
}
#endif // TREZOR_PRINT_DISABLE

32
core/embed/lib/terminal.h Normal file
View File

@ -0,0 +1,32 @@
/*
* 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/>.
*/
#ifndef LIB_TERMINAL_H
#define LIB_TERMINAL_H
#include "colors.h"
#ifndef TREZOR_PRINT_DISABLE
void term_set_color(uint16_t fgcolor, uint16_t bgcolor);
void term_print(const char *text, int textlen);
void term_printf(const char *fmt, ...)
__attribute__((__format__(__printf__, 1, 2)));
#endif
#endif // LIB_TERMINAL_H

View File

@ -34,7 +34,7 @@
#include "secbool.h"
#include "touch.h"
static void progress_callback(int pos, int len) { display_printf("."); }
static void progress_callback(int pos, int len) { term_printf("."); }
static void flash_from_sdcard(const flash_area_t* area, uint32_t source,
uint32_t length) {
@ -46,7 +46,7 @@ static void flash_from_sdcard(const flash_area_t* area, uint32_t source,
"length not a multiple of block size");
for (uint32_t i = 0; i < length / SDCARD_BLOCK_SIZE; i++) {
display_printf("read %d\n", (unsigned int)(i + source / SDCARD_BLOCK_SIZE));
term_printf("read %d\n", (unsigned int)(i + source / SDCARD_BLOCK_SIZE));
ensure(sdcard_read_blocks(buf, i + source / SDCARD_BLOCK_SIZE, 1),
"sdcard_read_blocks");
@ -70,15 +70,15 @@ int main(void) {
ensure(sdcard_is_present(), "sdcard_is_present");
display_printf("updating boardloader + bootloader\n");
term_printf("updating boardloader + bootloader\n");
display_printf("erasing sectors");
term_printf("erasing sectors");
ensure(flash_area_erase(&BOARDLOADER_AREA, progress_callback),
"flash_erase_sectors");
ensure(flash_area_erase(&BOOTLOADER_AREA, progress_callback),
"flash_erase_sectors");
display_printf("\n");
display_printf("erased\n");
term_printf("\n");
term_printf("erased\n");
ensure(flash_unlock_write(), NULL);
@ -92,7 +92,7 @@ int main(void) {
flash_from_sdcard(&BOOTLOADER_AREA, BOARDLOADER_TOTAL_SIZE,
BOOTLOADER_TOTAL_SIZE);
display_printf("done\n");
term_printf("done\n");
sdcard_power_off();
ensure(flash_lock_write(), NULL);

View File

@ -24,6 +24,7 @@
#include "common.h"
#include "display.h"
#include "model.h"
#include "terminal.h"
#ifdef FANCY_FATAL_ERROR
#include "rust_ui.h"
@ -72,15 +73,15 @@ error_uni(const char *label, const char *msg, const char *footer) {
screen_fatal_error_rust(label, msg, "PLEASE VISIT\nTREZOR.IO/RSOD");
display_refresh();
#else
display_print_color(COLOR_WHITE, COLOR_FATAL_ERROR);
term_set_color(COLOR_WHITE, COLOR_FATAL_ERROR);
if (label) {
display_printf("%s\n", label);
term_printf("%s\n", label);
}
if (msg) {
display_printf("%s\n", msg);
term_printf("%s\n", msg);
}
if (footer) {
display_printf("\n%s\n", footer);
term_printf("\n%s\n", footer);
}
#endif
display_backlight(255);
@ -101,26 +102,26 @@ __fatal_error(const char *expr, const char *msg, const char *file, int line,
"PLEASE VISIT\nTREZOR.IO/RSOD");
display_refresh();
#else
display_print_color(COLOR_WHITE, COLOR_FATAL_ERROR);
display_printf("\nINTERNAL ERROR:\n");
term_set_color(COLOR_WHITE, COLOR_FATAL_ERROR);
term_printf("\nINTERNAL ERROR:\n");
if (expr) {
display_printf("expr: %s\n", expr);
term_printf("expr: %s\n", expr);
}
if (msg) {
display_printf("msg : %s\n", msg);
term_printf("msg : %s\n", msg);
}
if (file) {
display_printf("file: %s:%d\n", file, line);
term_printf("file: %s:%d\n", file, line);
}
if (func) {
display_printf("func: %s\n", func);
term_printf("func: %s\n", func);
}
#ifdef SCM_REVISION
const uint8_t *rev = (const uint8_t *)SCM_REVISION;
display_printf("rev : %02x%02x%02x%02x%02x\n", rev[0], rev[1], rev[2], rev[3],
term_printf("rev : %02x%02x%02x%02x%02x\n", rev[0], rev[1], rev[2], rev[3],
rev[4]);
#endif
display_printf("\nPlease contact Trezor support.\n");
term_printf("\nPlease contact Trezor support.\n");
#endif
trezor_shutdown();
}
@ -134,14 +135,14 @@ error_shutdown(const char *label, const char *msg) {
screen_fatal_error_rust(label, msg, "PLEASE VISIT\nTREZOR.IO/RSOD");
display_refresh();
#else
display_print_color(COLOR_WHITE, COLOR_FATAL_ERROR);
term_set_color(COLOR_WHITE, COLOR_FATAL_ERROR);
if (label) {
display_printf("%s\n", label);
term_printf("%s\n", label);
}
if (msg) {
display_printf("%s\n", msg);
term_printf("%s\n", msg);
}
display_printf("\nPLEASE VISIT TREZOR.IO/RSOD\n");
term_printf("\nPLEASE VISIT TREZOR.IO/RSOD\n");
#endif
display_backlight(255);
trezor_shutdown();

View File

@ -56,15 +56,15 @@ error_uni(const char *label, const char *msg, const char *footer) {
screen_fatal_error_rust(label, msg, "PLEASE VISIT\nTREZOR.IO/RSOD");
display_refresh();
#else
display_print_color(COLOR_WHITE, COLOR_FATAL_ERROR);
term_set_color(COLOR_WHITE, COLOR_FATAL_ERROR);
if (label) {
display_printf("%s\n", label);
term_printf("%s\n", label);
}
if (msg) {
display_printf("%s\n", msg);
term_printf("%s\n", msg);
}
if (footer) {
display_printf("\n%s\n", footer);
term_printf("\n%s\n", footer);
}
#endif
display_backlight(255);
@ -93,33 +93,33 @@ __fatal_error(const char *expr, const char *msg, const char *file, int line,
display_refresh();
#else
display_print_color(COLOR_WHITE, COLOR_FATAL_ERROR);
display_printf("\nINTERNAL ERROR:\n");
term_set_color(COLOR_WHITE, COLOR_FATAL_ERROR);
term_printf("\nINTERNAL ERROR:\n");
printf("\nINTERNAL ERROR:\n");
if (expr) {
display_printf("expr: %s\n", expr);
term_printf("expr: %s\n", expr);
printf("expr: %s\n", expr);
}
if (msg) {
display_printf("msg : %s\n", msg);
term_printf("msg : %s\n", msg);
printf("msg : %s\n", msg);
}
if (file) {
display_printf("file: %s:%d\n", file, line);
term_printf("file: %s:%d\n", file, line);
printf("file: %s:%d\n", file, line);
}
if (func) {
display_printf("func: %s\n", func);
term_printf("func: %s\n", func);
printf("func: %s\n", func);
}
#ifdef SCM_REVISION
const uint8_t *rev = (const uint8_t *)SCM_REVISION;
display_printf("rev : %02x%02x%02x%02x%02x\n", rev[0], rev[1], rev[2], rev[3],
term_printf("rev : %02x%02x%02x%02x%02x\n", rev[0], rev[1], rev[2], rev[3],
rev[4]);
printf("rev : %02x%02x%02x%02x%02x\n", rev[0], rev[1], rev[2], rev[3],
rev[4]);
#endif
display_printf("\n\n\nHint:\nIsn't the emulator already running?\n");
term_printf("\n\n\nHint:\nIsn't the emulator already running?\n");
printf("Hint:\nIsn't the emulator already running?\n");
#endif
hal_delay(3000);