From fdef0575b17843a3f125f36a8cae179f8821cadd Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 5 Apr 2017 17:41:10 +0200 Subject: [PATCH] loader: show firmware version in loader, remove image_options.h --- micropython/bootloader/image_options.h | 2 -- micropython/bootloader/main.c | 9 ++++++--- micropython/loader/image_options.h | 2 -- micropython/loader/main.c | 17 +++++++++++++---- micropython/trezorhal/image.c | 12 ++++++++---- micropython/trezorhal/image.h | 8 ++++---- vendor/norcow | 2 +- 7 files changed, 32 insertions(+), 20 deletions(-) delete mode 100644 micropython/bootloader/image_options.h delete mode 100644 micropython/loader/image_options.h diff --git a/micropython/bootloader/image_options.h b/micropython/bootloader/image_options.h deleted file mode 100644 index 6dae0934c4..0000000000 --- a/micropython/bootloader/image_options.h +++ /dev/null @@ -1,2 +0,0 @@ -#define IMAGE_MAGIC 0x4C5A5254 // TRZL -#define IMAGE_MAXSIZE (1 * 64 * 1024 + 7 * 128 * 1024) diff --git a/micropython/bootloader/main.c b/micropython/bootloader/main.c index 3af709d6db..bf43942b11 100644 --- a/micropython/bootloader/main.c +++ b/micropython/bootloader/main.c @@ -13,6 +13,9 @@ #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) + void pendsv_isr_handler(void) { __fatal_error("pendsv"); } @@ -43,7 +46,7 @@ bool check_sdcard(void) sdcard_power_off(); - if (image_parse_header((const uint8_t *)buf, NULL)) { + if (image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, NULL)) { BOOTLOADER_PRINTLN("SD card header is valid"); return true; } else { @@ -86,7 +89,7 @@ bool copy_sdcard(void) sdcard_read_blocks((uint8_t *)buf, 0, 1); image_header hdr; - if (!image_parse_header((const uint8_t *)buf, &hdr)) { + if (!image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) { BOOTLOADER_PRINTLN("invalid header"); sdcard_power_off(); HAL_FLASH_Lock(); @@ -119,7 +122,7 @@ void check_and_jump(void) image_header hdr; - if (image_parse_header((const uint8_t *)LOADER_START, &hdr)) { + if (image_parse_header((const uint8_t *)LOADER_START, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) { BOOTLOADER_PRINTLN("valid loader header"); } else { BOOTLOADER_PRINTLN("invalid loader header"); diff --git a/micropython/loader/image_options.h b/micropython/loader/image_options.h deleted file mode 100644 index 079d94f708..0000000000 --- a/micropython/loader/image_options.h +++ /dev/null @@ -1,2 +0,0 @@ -#define IMAGE_MAGIC 0x465A5254 // TRZF -#define IMAGE_MAXSIZE (7 * 128 * 1024) diff --git a/micropython/loader/main.c b/micropython/loader/main.c index 50dfe934fc..70290bf446 100644 --- a/micropython/loader/main.c +++ b/micropython/loader/main.c @@ -12,13 +12,15 @@ #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) + void pendsv_isr_handler(void) { __fatal_error("pendsv"); } void display_vendor(const uint8_t *vimg, const char *vstr, uint32_t vstr_len, uint32_t fw_version) { - (void)fw_version; display_clear(); if (memcmp(vimg, "TOIf", 4) != 0) { return; @@ -29,8 +31,15 @@ void display_vendor(const uint8_t *vimg, const char *vstr, uint32_t vstr_len, ui return; } uint32_t datalen = *(uint32_t *)(vimg + 8); - display_image((DISPLAY_RESX - w) / 2, (DISPLAY_RESY - h) / 2, w, h, vimg + 12, datalen); - display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY * 3 / 4 + 20, vstr, vstr_len, FONT_BOLD, 0xFFFF, 0x0000); + display_image(60, 32, w, h, vimg + 12, datalen); + display_text_center(120, 192, vstr, vstr_len, FONT_BOLD, 0xFFFF, 0x0000); + char ver_str[] = "v0.0.0.0"; + // TODO: fixme - the following does not work for values >= 10 + ver_str[1] += fw_version & 0xFF; + ver_str[3] += (fw_version >> 8) & 0xFF; + ver_str[5] += (fw_version >> 16) & 0xFF; + ver_str[7] += (fw_version >> 24) & 0xFF; + display_text_center(120, 215, ver_str, -1, FONT_NORMAL, 0x7BEF, 0x0000); display_refresh(); } @@ -56,7 +65,7 @@ void check_and_jump(void) LOADER_PRINTLN("checking firmware header"); image_header hdr; - if (image_parse_header((const uint8_t *)(FIRMWARE_START + vhdr.hdrlen), &hdr)) { + if (image_parse_header((const uint8_t *)(FIRMWARE_START + vhdr.hdrlen), IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) { LOADER_PRINTLN("valid firmware header"); } else { LOADER_PRINTLN("invalid firmware header"); diff --git a/micropython/trezorhal/image.c b/micropython/trezorhal/image.c index 653e998214..325b895a21 100644 --- a/micropython/trezorhal/image.c +++ b/micropython/trezorhal/image.c @@ -51,7 +51,7 @@ static bool compute_pubkey(const vendor_header *vhdr, uint8_t sigmask, ed25519_p return 0 == ed25519_cosi_combine_publickeys(res, keys, vsig_m); } -bool image_parse_header(const uint8_t *data, image_header *hdr) +bool image_parse_header(const uint8_t *data, uint32_t magic, uint32_t maxsize, image_header *hdr) { if (!hdr) { image_header h; @@ -59,7 +59,7 @@ bool image_parse_header(const uint8_t *data, image_header *hdr) } memcpy(&hdr->magic, data, 4); - if (hdr->magic != IMAGE_MAGIC) return false; + if (hdr->magic != magic) return false; memcpy(&hdr->hdrlen, data + 4, 4); if (hdr->hdrlen != HEADER_SIZE) return false; @@ -69,7 +69,7 @@ bool image_parse_header(const uint8_t *data, image_header *hdr) memcpy(&hdr->codelen, data + 12, 4); if (hdr->hdrlen + hdr->codelen < 4 * 1024) return false; - if (hdr->hdrlen + hdr->codelen > IMAGE_MAXSIZE) return false; + if (hdr->hdrlen + hdr->codelen > maxsize) return false; if ((hdr->hdrlen + hdr->codelen) % 512 != 0) return false; memcpy(&hdr->version, data + 16, 4); @@ -121,10 +121,14 @@ bool vendor_parse_header(const uint8_t *data, vendor_header *vhdr) memcpy(&vhdr->vsig_m, data + 14, 1); memcpy(&vhdr->vsig_n, data + 15, 1); + if (vhdr->vsig_n > MAX_VENDOR_PUBLIC_KEYS) { + return false; + } + for (int i = 0; i < vhdr->vsig_n; i++) { vhdr->vpub[i] = data + 16 + i * 32; } - for (int i = vhdr->vsig_n; i < 8; i++) { + for (int i = vhdr->vsig_n; i < MAX_VENDOR_PUBLIC_KEYS; i++) { vhdr->vpub[i] = 0; } diff --git a/micropython/trezorhal/image.h b/micropython/trezorhal/image.h index 45b969d056..5e4ee4e828 100644 --- a/micropython/trezorhal/image.h +++ b/micropython/trezorhal/image.h @@ -4,8 +4,6 @@ #include #include -#include "image_options.h" - typedef struct { uint32_t magic; uint32_t hdrlen; @@ -17,6 +15,8 @@ typedef struct { uint8_t sig[64]; } image_header; +#define MAX_VENDOR_PUBLIC_KEYS 8 + typedef struct { uint32_t magic; uint32_t hdrlen; @@ -24,7 +24,7 @@ typedef struct { uint16_t version; uint8_t vsig_m; uint8_t vsig_n; - const uint8_t *vpub[8]; + const uint8_t *vpub[MAX_VENDOR_PUBLIC_KEYS]; uint8_t vstr_len; const uint8_t *vstr; const uint8_t *vimg; @@ -32,7 +32,7 @@ typedef struct { uint8_t sig[64]; } vendor_header; -bool image_parse_header(const uint8_t *data, image_header *hdr); +bool image_parse_header(const uint8_t *data, uint32_t magic, uint32_t maxsize, image_header *hdr); bool image_check_signature(const uint8_t *data, const image_header *hdr, const vendor_header *vhdr); diff --git a/vendor/norcow b/vendor/norcow index ea6d2d03d0..81c00e0a53 160000 --- a/vendor/norcow +++ b/vendor/norcow @@ -1 +1 @@ -Subproject commit ea6d2d03d07ca9c51ff22a1521ce35dfda9fc12e +Subproject commit 81c00e0a53792c5d2c29895824c861e5d4341737