mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-22 22:38:08 +00:00
bootloader/loader: refactor PRINT(LN) macros to common.h
This commit is contained in:
parent
4f9f351a16
commit
cb8e957379
@ -8,12 +8,6 @@
|
|||||||
#include "sdcard.h"
|
#include "sdcard.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
|
||||||
#define BOOTLOADER_FGCOLOR 0xFFFF
|
|
||||||
#define BOOTLOADER_BGCOLOR 0x0000
|
|
||||||
|
|
||||||
#define BOOTLOADER_PRINT(X) do { display_print(X, -1); display_print_out(BOOTLOADER_FGCOLOR, BOOTLOADER_BGCOLOR); } while(0)
|
|
||||||
#define BOOTLOADER_PRINTLN(X) do { display_print(X "\n", -1); display_print_out(BOOTLOADER_FGCOLOR, BOOTLOADER_BGCOLOR); } while(0)
|
|
||||||
|
|
||||||
#define IMAGE_MAGIC 0x4C5A5254 // TRZL
|
#define IMAGE_MAGIC 0x4C5A5254 // TRZL
|
||||||
#define IMAGE_MAXSIZE (1 * 64 * 1024 + 7 * 128 * 1024)
|
#define IMAGE_MAXSIZE (1 * 64 * 1024 + 7 * 128 * 1024)
|
||||||
|
|
||||||
@ -23,20 +17,20 @@ void pendsv_isr_handler(void) {
|
|||||||
|
|
||||||
bool check_sdcard(void)
|
bool check_sdcard(void)
|
||||||
{
|
{
|
||||||
BOOTLOADER_PRINTLN("checking for SD card");
|
DPRINTLN("checking for SD card");
|
||||||
|
|
||||||
if (!sdcard_is_present()) {
|
if (!sdcard_is_present()) {
|
||||||
BOOTLOADER_PRINTLN("no SD card found");
|
DPRINTLN("no SD card found");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOTLOADER_PRINTLN("SD card found");
|
DPRINTLN("SD card found");
|
||||||
|
|
||||||
sdcard_power_on();
|
sdcard_power_on();
|
||||||
|
|
||||||
uint64_t cap = sdcard_get_capacity_in_bytes();
|
uint64_t cap = sdcard_get_capacity_in_bytes();
|
||||||
if (cap < 1024 * 1024) {
|
if (cap < 1024 * 1024) {
|
||||||
BOOTLOADER_PRINTLN("SD card too small");
|
DPRINTLN("SD card too small");
|
||||||
sdcard_power_off();
|
sdcard_power_off();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -48,10 +42,10 @@ bool check_sdcard(void)
|
|||||||
sdcard_power_off();
|
sdcard_power_off();
|
||||||
|
|
||||||
if (image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, NULL)) {
|
if (image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, NULL)) {
|
||||||
BOOTLOADER_PRINTLN("SD card header is valid");
|
DPRINTLN("SD card header is valid");
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
BOOTLOADER_PRINTLN("SD card header is invalid");
|
DPRINTLN("SD card header is invalid");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,7 +53,7 @@ bool check_sdcard(void)
|
|||||||
bool copy_sdcard(void)
|
bool copy_sdcard(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
BOOTLOADER_PRINT("erasing flash ");
|
DPRINT("erasing flash ");
|
||||||
|
|
||||||
// erase flash (except bootloader)
|
// erase flash (except bootloader)
|
||||||
HAL_FLASH_Unlock();
|
HAL_FLASH_Unlock();
|
||||||
@ -74,14 +68,14 @@ bool copy_sdcard(void)
|
|||||||
EraseInitStruct.Sector = i;
|
EraseInitStruct.Sector = i;
|
||||||
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
|
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
|
||||||
HAL_FLASH_Lock();
|
HAL_FLASH_Lock();
|
||||||
BOOTLOADER_PRINTLN(" failed");
|
DPRINTLN(" failed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
BOOTLOADER_PRINT(".");
|
DPRINT(".");
|
||||||
}
|
}
|
||||||
BOOTLOADER_PRINTLN(" done");
|
DPRINTLN(" done");
|
||||||
|
|
||||||
BOOTLOADER_PRINTLN("copying new loader from SD card");
|
DPRINTLN("copying new loader from SD card");
|
||||||
|
|
||||||
sdcard_power_on();
|
sdcard_power_on();
|
||||||
|
|
||||||
@ -91,7 +85,7 @@ bool copy_sdcard(void)
|
|||||||
|
|
||||||
image_header hdr;
|
image_header hdr;
|
||||||
if (!image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
|
if (!image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
|
||||||
BOOTLOADER_PRINTLN("invalid header");
|
DPRINTLN("invalid header");
|
||||||
sdcard_power_off();
|
sdcard_power_off();
|
||||||
HAL_FLASH_Lock();
|
HAL_FLASH_Lock();
|
||||||
return false;
|
return false;
|
||||||
@ -101,7 +95,7 @@ bool copy_sdcard(void)
|
|||||||
sdcard_read_blocks((uint8_t *)buf, i, 1);
|
sdcard_read_blocks((uint8_t *)buf, i, 1);
|
||||||
for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) {
|
for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) {
|
||||||
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, LOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]) != HAL_OK) {
|
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, LOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]) != HAL_OK) {
|
||||||
BOOTLOADER_PRINTLN("copy failed");
|
DPRINTLN("copy failed");
|
||||||
sdcard_power_off();
|
sdcard_power_off();
|
||||||
HAL_FLASH_Lock();
|
HAL_FLASH_Lock();
|
||||||
return false;
|
return false;
|
||||||
@ -112,36 +106,36 @@ bool copy_sdcard(void)
|
|||||||
sdcard_power_off();
|
sdcard_power_off();
|
||||||
HAL_FLASH_Lock();
|
HAL_FLASH_Lock();
|
||||||
|
|
||||||
BOOTLOADER_PRINTLN("done");
|
DPRINTLN("done");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void check_and_jump(void)
|
void check_and_jump(void)
|
||||||
{
|
{
|
||||||
BOOTLOADER_PRINTLN("checking loader");
|
DPRINTLN("checking loader");
|
||||||
|
|
||||||
image_header hdr;
|
image_header hdr;
|
||||||
|
|
||||||
if (image_parse_header((const uint8_t *)LOADER_START, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
|
if (image_parse_header((const uint8_t *)LOADER_START, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
|
||||||
BOOTLOADER_PRINTLN("valid loader header");
|
DPRINTLN("valid loader header");
|
||||||
} else {
|
} else {
|
||||||
BOOTLOADER_PRINTLN("invalid loader header");
|
DPRINTLN("invalid loader header");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (image_check_signature((const uint8_t *)LOADER_START, &hdr, NULL)) {
|
if (image_check_signature((const uint8_t *)LOADER_START, &hdr, NULL)) {
|
||||||
BOOTLOADER_PRINTLN("valid loader signature");
|
DPRINTLN("valid loader signature");
|
||||||
|
|
||||||
// TODO: remove debug wait
|
// TODO: remove debug wait
|
||||||
BOOTLOADER_PRINTLN("waiting 1 second");
|
DPRINTLN("waiting 1 second");
|
||||||
HAL_Delay(1000);
|
HAL_Delay(1000);
|
||||||
// end
|
// end
|
||||||
BOOTLOADER_PRINTLN("JUMP!");
|
DPRINTLN("JUMP!");
|
||||||
jump_to(LOADER_START + HEADER_SIZE);
|
jump_to(LOADER_START + HEADER_SIZE);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
BOOTLOADER_PRINTLN("invalid loader signature");
|
DPRINTLN("invalid loader signature");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,9 +150,9 @@ int main(void)
|
|||||||
display_clear();
|
display_clear();
|
||||||
display_backlight(255);
|
display_backlight(255);
|
||||||
|
|
||||||
BOOTLOADER_PRINTLN("TREZOR Bootloader " VERSION_STR);
|
DPRINTLN("TREZOR Bootloader " VERSION_STR);
|
||||||
BOOTLOADER_PRINTLN("=================");
|
DPRINTLN("=================");
|
||||||
BOOTLOADER_PRINTLN("starting bootloader");
|
DPRINTLN("starting bootloader");
|
||||||
|
|
||||||
if (check_sdcard()) {
|
if (check_sdcard()) {
|
||||||
if (!copy_sdcard()) {
|
if (!copy_sdcard()) {
|
||||||
|
@ -8,12 +8,6 @@
|
|||||||
#include "touch.h"
|
#include "touch.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
|
||||||
#define LOADER_FGCOLOR 0xFFFF
|
|
||||||
#define LOADER_BGCOLOR 0x0000
|
|
||||||
|
|
||||||
#define LOADER_PRINT(X) do { display_print(X, -1); display_print_out(LOADER_FGCOLOR, LOADER_BGCOLOR); } while(0)
|
|
||||||
#define LOADER_PRINTLN(X) do { display_print(X "\n", -1); display_print_out(LOADER_FGCOLOR, LOADER_BGCOLOR); } while(0)
|
|
||||||
|
|
||||||
#define IMAGE_MAGIC 0x465A5254 // TRZF
|
#define IMAGE_MAGIC 0x465A5254 // TRZF
|
||||||
#define IMAGE_MAXSIZE (7 * 128 * 1024)
|
#define IMAGE_MAXSIZE (7 * 128 * 1024)
|
||||||
|
|
||||||
@ -47,43 +41,43 @@ void display_vendor(const uint8_t *vimg, const char *vstr, uint32_t vstr_len, ui
|
|||||||
|
|
||||||
void check_and_jump(void)
|
void check_and_jump(void)
|
||||||
{
|
{
|
||||||
LOADER_PRINTLN("checking vendor header");
|
DPRINTLN("checking vendor header");
|
||||||
|
|
||||||
vendor_header vhdr;
|
vendor_header vhdr;
|
||||||
if (vendor_parse_header((const uint8_t *)FIRMWARE_START, &vhdr)) {
|
if (vendor_parse_header((const uint8_t *)FIRMWARE_START, &vhdr)) {
|
||||||
LOADER_PRINTLN("valid vendor header");
|
DPRINTLN("valid vendor header");
|
||||||
} else {
|
} else {
|
||||||
LOADER_PRINTLN("invalid vendor header");
|
DPRINTLN("invalid vendor header");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vendor_check_signature((const uint8_t *)FIRMWARE_START, &vhdr)) {
|
if (vendor_check_signature((const uint8_t *)FIRMWARE_START, &vhdr)) {
|
||||||
LOADER_PRINTLN("valid vendor header signature");
|
DPRINTLN("valid vendor header signature");
|
||||||
} else {
|
} else {
|
||||||
LOADER_PRINTLN("invalid vendor header signature");
|
DPRINTLN("invalid vendor header signature");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOADER_PRINTLN("checking firmware header");
|
DPRINTLN("checking firmware header");
|
||||||
|
|
||||||
image_header hdr;
|
image_header hdr;
|
||||||
if (image_parse_header((const uint8_t *)(FIRMWARE_START + vhdr.hdrlen), IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
|
if (image_parse_header((const uint8_t *)(FIRMWARE_START + vhdr.hdrlen), IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
|
||||||
LOADER_PRINTLN("valid firmware header");
|
DPRINTLN("valid firmware header");
|
||||||
} else {
|
} else {
|
||||||
LOADER_PRINTLN("invalid firmware header");
|
DPRINTLN("invalid firmware header");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (image_check_signature((const uint8_t *)(FIRMWARE_START + vhdr.hdrlen), &hdr, &vhdr)) {
|
if (image_check_signature((const uint8_t *)(FIRMWARE_START + vhdr.hdrlen), &hdr, &vhdr)) {
|
||||||
LOADER_PRINTLN("valid firmware signature");
|
DPRINTLN("valid firmware signature");
|
||||||
|
|
||||||
display_vendor(vhdr.vimg, (const char *)vhdr.vstr, vhdr.vstr_len, hdr.version);
|
display_vendor(vhdr.vimg, (const char *)vhdr.vstr, vhdr.vstr_len, hdr.version);
|
||||||
HAL_Delay(1000); // TODO: remove?
|
HAL_Delay(1000); // TODO: remove?
|
||||||
LOADER_PRINTLN("JUMP!");
|
DPRINTLN("JUMP!");
|
||||||
jump_to(FIRMWARE_START + vhdr.hdrlen + HEADER_SIZE);
|
jump_to(FIRMWARE_START + vhdr.hdrlen + HEADER_SIZE);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
LOADER_PRINTLN("invalid firmware signature");
|
DPRINTLN("invalid firmware signature");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,9 +97,9 @@ int main(void)
|
|||||||
display_clear();
|
display_clear();
|
||||||
display_backlight(255);
|
display_backlight(255);
|
||||||
|
|
||||||
LOADER_PRINTLN("TREZOR Loader " VERSION_STR);
|
DPRINTLN("TREZOR Loader " VERSION_STR);
|
||||||
LOADER_PRINTLN("=============");
|
DPRINTLN("=============");
|
||||||
LOADER_PRINTLN("starting loader");
|
DPRINTLN("starting loader");
|
||||||
|
|
||||||
if (touch_read() != 0) {
|
if (touch_read() != 0) {
|
||||||
mainloop();
|
mainloop();
|
||||||
|
@ -16,4 +16,9 @@ void __attribute__((noreturn)) __fatal_error(const char *msg);
|
|||||||
|
|
||||||
void jump_to(uint32_t address);
|
void jump_to(uint32_t address);
|
||||||
|
|
||||||
|
// common helper macros
|
||||||
|
|
||||||
|
#define DPRINT(X) do { display_print(X, -1); display_print_out(0xFFFF, 0x0000); } while(0)
|
||||||
|
#define DPRINTLN(X) do { display_print(X "\n", -1); display_print_out(0xFFFF, 0x0000); } while(0)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user