1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-12 18:49:07 +00:00

bootloader/loader: refactor PRINT(LN) macros to common.h

This commit is contained in:
Pavol Rusnak 2017-04-06 17:25:14 +02:00
parent 4f9f351a16
commit cb8e957379
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
3 changed files with 43 additions and 50 deletions

View File

@ -8,12 +8,6 @@
#include "sdcard.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_MAXSIZE (1 * 64 * 1024 + 7 * 128 * 1024)
@ -23,20 +17,20 @@ void pendsv_isr_handler(void) {
bool check_sdcard(void)
{
BOOTLOADER_PRINTLN("checking for SD card");
DPRINTLN("checking for SD card");
if (!sdcard_is_present()) {
BOOTLOADER_PRINTLN("no SD card found");
DPRINTLN("no SD card found");
return false;
}
BOOTLOADER_PRINTLN("SD card found");
DPRINTLN("SD card found");
sdcard_power_on();
uint64_t cap = sdcard_get_capacity_in_bytes();
if (cap < 1024 * 1024) {
BOOTLOADER_PRINTLN("SD card too small");
DPRINTLN("SD card too small");
sdcard_power_off();
return false;
}
@ -48,10 +42,10 @@ bool check_sdcard(void)
sdcard_power_off();
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;
} else {
BOOTLOADER_PRINTLN("SD card header is invalid");
DPRINTLN("SD card header is invalid");
return false;
}
}
@ -59,7 +53,7 @@ bool check_sdcard(void)
bool copy_sdcard(void)
{
BOOTLOADER_PRINT("erasing flash ");
DPRINT("erasing flash ");
// erase flash (except bootloader)
HAL_FLASH_Unlock();
@ -74,14 +68,14 @@ bool copy_sdcard(void)
EraseInitStruct.Sector = i;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
HAL_FLASH_Lock();
BOOTLOADER_PRINTLN(" failed");
DPRINTLN(" failed");
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();
@ -91,7 +85,7 @@ bool copy_sdcard(void)
image_header hdr;
if (!image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
BOOTLOADER_PRINTLN("invalid header");
DPRINTLN("invalid header");
sdcard_power_off();
HAL_FLASH_Lock();
return false;
@ -101,7 +95,7 @@ bool copy_sdcard(void)
sdcard_read_blocks((uint8_t *)buf, i, 1);
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) {
BOOTLOADER_PRINTLN("copy failed");
DPRINTLN("copy failed");
sdcard_power_off();
HAL_FLASH_Lock();
return false;
@ -112,36 +106,36 @@ bool copy_sdcard(void)
sdcard_power_off();
HAL_FLASH_Lock();
BOOTLOADER_PRINTLN("done");
DPRINTLN("done");
return true;
}
void check_and_jump(void)
{
BOOTLOADER_PRINTLN("checking loader");
DPRINTLN("checking loader");
image_header 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 {
BOOTLOADER_PRINTLN("invalid loader header");
DPRINTLN("invalid loader header");
return;
}
if (image_check_signature((const uint8_t *)LOADER_START, &hdr, NULL)) {
BOOTLOADER_PRINTLN("valid loader signature");
DPRINTLN("valid loader signature");
// TODO: remove debug wait
BOOTLOADER_PRINTLN("waiting 1 second");
DPRINTLN("waiting 1 second");
HAL_Delay(1000);
// end
BOOTLOADER_PRINTLN("JUMP!");
DPRINTLN("JUMP!");
jump_to(LOADER_START + HEADER_SIZE);
} else {
BOOTLOADER_PRINTLN("invalid loader signature");
DPRINTLN("invalid loader signature");
}
}
@ -156,9 +150,9 @@ int main(void)
display_clear();
display_backlight(255);
BOOTLOADER_PRINTLN("TREZOR Bootloader " VERSION_STR);
BOOTLOADER_PRINTLN("=================");
BOOTLOADER_PRINTLN("starting bootloader");
DPRINTLN("TREZOR Bootloader " VERSION_STR);
DPRINTLN("=================");
DPRINTLN("starting bootloader");
if (check_sdcard()) {
if (!copy_sdcard()) {

View File

@ -8,12 +8,6 @@
#include "touch.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_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)
{
LOADER_PRINTLN("checking vendor header");
DPRINTLN("checking vendor header");
vendor_header vhdr;
if (vendor_parse_header((const uint8_t *)FIRMWARE_START, &vhdr)) {
LOADER_PRINTLN("valid vendor header");
DPRINTLN("valid vendor header");
} else {
LOADER_PRINTLN("invalid vendor header");
DPRINTLN("invalid vendor header");
return;
}
if (vendor_check_signature((const uint8_t *)FIRMWARE_START, &vhdr)) {
LOADER_PRINTLN("valid vendor header signature");
DPRINTLN("valid vendor header signature");
} else {
LOADER_PRINTLN("invalid vendor header signature");
DPRINTLN("invalid vendor header signature");
return;
}
LOADER_PRINTLN("checking firmware header");
DPRINTLN("checking firmware header");
image_header 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 {
LOADER_PRINTLN("invalid firmware header");
DPRINTLN("invalid firmware header");
return;
}
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);
HAL_Delay(1000); // TODO: remove?
LOADER_PRINTLN("JUMP!");
DPRINTLN("JUMP!");
jump_to(FIRMWARE_START + vhdr.hdrlen + HEADER_SIZE);
} else {
LOADER_PRINTLN("invalid firmware signature");
DPRINTLN("invalid firmware signature");
}
}
@ -103,9 +97,9 @@ int main(void)
display_clear();
display_backlight(255);
LOADER_PRINTLN("TREZOR Loader " VERSION_STR);
LOADER_PRINTLN("=============");
LOADER_PRINTLN("starting loader");
DPRINTLN("TREZOR Loader " VERSION_STR);
DPRINTLN("=============");
DPRINTLN("starting loader");
if (touch_read() != 0) {
mainloop();

View File

@ -16,4 +16,9 @@ void __attribute__((noreturn)) __fatal_error(const char *msg);
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