Skip to content
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
16 changes: 11 additions & 5 deletions core/embed/emmc_wrapper/emmc_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,18 @@ static int check_file_contents(uint8_t iface_num, const uint8_t* buffer, uint32_
}
);

if ( file_hdr.codelen - (FIRMWARE_IMAGE_INNER_SIZE - (file_vhdr.hdrlen + file_hdr.hdrlen)) >
FMC_SDRAM_FIRMWARE_P2_LEN )
{
send_failure(iface_num, FailureType_Failure_ProcessError, "Firmware file P2 too big!");
return -1;
}

// check file firmware hash
ExecuteCheck_MSGS_ADV(
check_image_contents_ADV(
&file_vhdr, &file_hdr, p_data + file_vhdr.hdrlen + file_hdr.hdrlen, 0, file_hdr.codelen
&file_vhdr, &file_hdr, p_data + file_vhdr.hdrlen + file_hdr.hdrlen, 0, file_hdr.codelen,
true
),
sectrue,
{
Expand All @@ -614,9 +622,7 @@ static int check_file_contents(uint8_t iface_num, const uint8_t* buffer, uint32_

// check file size
ExecuteCheck_MSGS_ADV(
(file_vhdr.hdrlen + file_hdr.hdrlen + file_hdr.codelen <=
FIRMWARE_SECTORS_COUNT * FLASH_FIRMWARE_SECTOR_SIZE),
true,
(file_vhdr.hdrlen + file_hdr.hdrlen + file_hdr.codelen <= FIRMWARE_IMAGE_MAXSIZE), true,
{
send_failure(iface_num, FailureType_Failure_ProcessError, "Firmware file is too big!");
return -1;
Expand Down Expand Up @@ -844,7 +850,7 @@ int check_bootloader_update(image_header* file_hdr)
) )
return -1;

if ( !check_image_contents_ADV(NULL, file_hdr, buffer + file_hdr->hdrlen, 0, file_hdr->codelen) )
if ( !check_image_contents_ADV(NULL, file_hdr, buffer + file_hdr->hdrlen, 0, file_hdr->codelen, true) )
return -1;

// check header stated size matchs file size
Expand Down
51 changes: 8 additions & 43 deletions core/embed/extmod/modtrezorutils/modtrezorutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
#include "mini_printf.h"
#endif

static void ui_progress(mp_obj_t ui_wait_callback, uint32_t current,
uint32_t total) {
if (mp_obj_is_callable(ui_wait_callback)) {
mp_call_function_2_protected(ui_wait_callback, mp_obj_new_int(current),
mp_obj_new_int(total));
}
}
// static void ui_progress(mp_obj_t ui_wait_callback, uint32_t current,
// uint32_t total) {
// if (mp_obj_is_callable(ui_wait_callback)) {
// mp_call_function_2_protected(ui_wait_callback, mp_obj_new_int(current),
// mp_obj_new_int(total));
// }
// }

/// def consteq(sec: bytes, pub: bytes) -> bool:
/// """
Expand Down Expand Up @@ -162,44 +162,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_reset_obj,
/// """
STATIC mp_obj_t mod_trezorutils_firmware_hash(size_t n_args,
const mp_obj_t *args) {
BLAKE2S_CTX ctx;
mp_buffer_info_t chal = {0};
if (n_args > 0 && args[0] != mp_const_none) {
mp_get_buffer_raise(args[0], &chal, MP_BUFFER_READ);
}

if (chal.len != 0) {
if (blake2s_InitKey(&ctx, BLAKE2S_DIGEST_LENGTH, chal.buf, chal.len) != 0) {
mp_raise_msg(&mp_type_ValueError, "Invalid challenge.");
}
} else {
blake2s_Init(&ctx, BLAKE2S_DIGEST_LENGTH);
}

mp_obj_t ui_wait_callback = mp_const_none;
if (n_args > 1 && args[1] != mp_const_none) {
ui_wait_callback = args[1];
}

ui_progress(ui_wait_callback, 0, FIRMWARE_SECTORS_COUNT);
for (int i = 0; i < FIRMWARE_SECTORS_COUNT; i++) {
uint8_t sector = FIRMWARE_SECTORS[i];
uint32_t size = flash_sector_size(sector);
const void *data = flash_get_address(sector, 0, size);
if (data == NULL) {
mp_raise_msg(&mp_type_RuntimeError, "Failed to read firmware.");
}
blake2s_Update(&ctx, data, size);
ui_progress(ui_wait_callback, i + 1, FIRMWARE_SECTORS_COUNT);
}

// we use onekey firmware hash
vstr_t vstr = {0};
vstr_init_len(&vstr, BLAKE2S_DIGEST_LENGTH);
if (blake2s_Final(&ctx, vstr.buf, vstr.len) != 0) {
vstr_clear(&vstr);
mp_raise_msg(&mp_type_RuntimeError, "Failed to finalize firmware hash.");
}

return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorutils_firmware_hash_obj, 0,
Expand Down
2 changes: 1 addition & 1 deletion core/embed/firmware/header.S
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ g_header:
.byte ONEKEY_VERSION_MINOR // onekey_minor
.byte ONEKEY_VERSION_PATCH // onekey_patch
.byte ONEKEY_VERSION_BUILD // onekey_build
. = . + 4 // reserved
.word 0 // hash block size
. = . + 512 // hash1 ... hash16

#if !defined TREZOR_MODEL_1
Expand Down
4 changes: 2 additions & 2 deletions core/embed/firmware/memory_H.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ ENTRY(reset_handler)

MEMORY {
FLASH (rx) : ORIGIN = 0x08060000, LENGTH = 1536K
FLASH2 (r) : ORIGIN = 0x90000000, LENGTH = 2048K
FLASH2 (r) : ORIGIN = 0x90000000, LENGTH = 4096K
AXIRAM (wal) : ORIGIN = 0x24000000, LENGTH = 512K
SRAM (wal) : ORIGIN = 0x20000000, LENGTH = 128K
SRAM1 (wal) : ORIGIN = 0x30000000, LENGTH = 128K
SRAM2 (wal) : ORIGIN = 0x30020000, LENGTH = 128K
/* SRAM3 is used for DMA */
SRAM3 (wal) : ORIGIN = 0x30040000, LENGTH = 32K
EXRAM (wal) : ORIGIN = 0xD1C00000, LENGTH = 2048K
EXRAM (wal) : ORIGIN = 0xD1C00000, LENGTH = 4096K
}

main_stack_base = ORIGIN(SRAM) + LENGTH(SRAM); /* 8-byte aligned full descending stack */
Expand Down
Loading
Loading