From 6d65551b827115c2fc7e927f032e60e045eaab2b Mon Sep 17 00:00:00 2001 From: Jochen Hoenicke Date: Mon, 29 Aug 2016 23:22:49 +0200 Subject: [PATCH] More alignment fixes --- firmware/ethereum.c | 8 ++++++-- firmware/fsm.c | 10 +++++----- firmware/storage.c | 18 +++++++++++------- firmware/u2f.c | 17 +++++++++-------- firmware/usb.c | 2 +- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/firmware/ethereum.c b/firmware/ethereum.c index 70738710c8..4b714ca190 100644 --- a/firmware/ethereum.c +++ b/firmware/ethereum.c @@ -180,11 +180,15 @@ static void ethereumFormatAmount(bignum256 *val, char buffer[25]) char value[25] = {0}; char *value_ptr = value; + // convert val into base 1000 for easy printing. uint16_t num[26]; uint8_t last_used = 0; for (int i = 0; i < 26; i++) { - bn_divmod1000(val, (uint32_t *)&(num[i])); - if (num[i] > 0) { + uint32_t limb; + bn_divmod1000(val, &limb); + // limb is < 1000. + num[i] = (uint16_t) limb; + if (limb > 0) { last_used = i; } } diff --git a/firmware/fsm.c b/firmware/fsm.c index c54439b3d4..93c3639203 100644 --- a/firmware/fsm.c +++ b/firmware/fsm.c @@ -51,9 +51,9 @@ // message methods -static uint8_t msg_resp[MSG_OUT_SIZE]; +static uint8_t msg_resp[MSG_OUT_SIZE] __attribute__ ((aligned)); -#define RESP_INIT(TYPE) TYPE *resp = (TYPE *)msg_resp; \ +#define RESP_INIT(TYPE) TYPE *resp = (TYPE *) (void *) msg_resp; \ _Static_assert(sizeof(msg_resp) >= sizeof(TYPE), #TYPE " is too large"); \ memset(resp, 0, sizeof(TYPE)); @@ -1142,10 +1142,10 @@ void fsm_msgDebugLinkMemoryWrite(DebugLinkMemoryWrite *msg) if (msg->flash) { flash_clear_status_flags(); flash_unlock(); - uint32_t* src = (uint32_t *) msg->memory.bytes; for (unsigned int i = 0; i < length; i += 4) { - flash_program_word(msg->address + i, *src); - src++; + uint32_t word; + memcpy(&word, msg->memory.bytes + i, 4); + flash_program_word(msg->address + i, word); } flash_lock(); } else { diff --git a/firmware/storage.c b/firmware/storage.c index 2cc9204ae5..0eaa8554a9 100644 --- a/firmware/storage.c +++ b/firmware/storage.c @@ -42,7 +42,7 @@ Storage storage; -uint8_t storage_uuid[12]; +uint32_t storage_uuid[12/sizeof(uint32_t)]; char storage_uuid_str[25]; /* @@ -77,6 +77,7 @@ be added to the storage u2f_counter to get the real counter value. #define FLASH_STORAGE_U2FAREA (FLASH_STORAGE_PINAREA + FLASH_STORAGE_PINAREA_LEN) #define FLASH_STORAGE_U2FAREA_LEN (0x100) #define FLASH_STORAGE_REALLEN (4 + sizeof(storage_uuid) + sizeof(Storage)) + _Static_assert(FLASH_STORAGE_START + FLASH_STORAGE_REALLEN <= FLASH_STORAGE_PINAREA, "Storage struct is too large for TREZOR flash"); _Static_assert((sizeof(storage_uuid) & 3) == 0, "storage uuid unaligned"); _Static_assert((sizeof(storage) & 3) == 0, "storage unaligned"); @@ -87,6 +88,9 @@ _Static_assert((sizeof(storage) & 3) == 0, "storage unaligned"); */ static uint32_t storage_u2f_offset; +/* magic constant to check validity of storage block */ +static const uint32_t storage_magic = 0x726f7473; // 'stor' as uint32_t + static bool sessionSeedCached, sessionSeedUsesPassphrase; static uint8_t sessionSeed[64]; @@ -114,7 +118,7 @@ void storage_check_flash_errors(void) bool storage_from_flash(void) { - if (memcmp((void *)FLASH_STORAGE_START, "stor", 4) != 0) { + if (memcmp((void *)FLASH_STORAGE_START, &storage_magic, 4) != 0) { // wrong magic return false; } @@ -182,7 +186,7 @@ void storage_init(void) void storage_reset_uuid(void) { // set random uuid - random_buffer(storage_uuid, sizeof(storage_uuid)); + random_buffer((uint8_t *)storage_uuid, sizeof(storage_uuid)); data2hex(storage_uuid, sizeof(storage_uuid), storage_uuid_str); } @@ -216,7 +220,7 @@ static uint32_t storage_flash_words(uint32_t addr, uint32_t *src, int nwords) { static void storage_commit_locked(void) { - uint8_t meta_backup[FLASH_META_DESC_LEN]; + uint32_t meta_backup[FLASH_META_DESC_LEN/4]; // backup meta memcpy(meta_backup, (uint8_t*)FLASH_META_START, FLASH_META_DESC_LEN); @@ -225,11 +229,11 @@ static void storage_commit_locked(void) flash_erase_sector(FLASH_META_SECTOR_FIRST, FLASH_CR_PROGRAM_X32); // copy meta uint32_t flash = FLASH_META_START; - flash = storage_flash_words(flash, (uint32_t *)meta_backup, FLASH_META_DESC_LEN/4); + flash = storage_flash_words(flash, meta_backup, FLASH_META_DESC_LEN/4); // copy storage - flash_program_word(flash, *(uint32_t *) "stor"); + flash_program_word(flash, storage_magic); flash += 4; - flash = storage_flash_words(flash, (uint32_t *)&storage_uuid, sizeof(storage_uuid)/4); + flash = storage_flash_words(flash, storage_uuid, sizeof(storage_uuid)/4); flash = storage_flash_words(flash, (uint32_t *)&storage, sizeof(storage)/4); // fill remainder with zero for future extensions while (flash < FLASH_STORAGE_PINAREA) { diff --git a/firmware/u2f.c b/firmware/u2f.c index 76eb80f511..22eb3e1d95 100644 --- a/firmware/u2f.c +++ b/firmware/u2f.c @@ -297,7 +297,7 @@ void u2fhid_init(const U2FHID_FRAME *in) { const U2FHID_INIT_REQ *init_req = (const U2FHID_INIT_REQ *)&in->init.data; U2FHID_FRAME f; - U2FHID_INIT_RESP *resp = (U2FHID_INIT_RESP *)f.init.data; + U2FHID_INIT_RESP resp; debugLog(0, "", "u2fhid_init"); @@ -312,13 +312,14 @@ void u2fhid_init(const U2FHID_FRAME *in) f.init.bcnth = 0; f.init.bcntl = U2FHID_INIT_RESP_SIZE; - memcpy(resp->nonce, init_req->nonce, sizeof(init_req->nonce)); - resp->cid = in->cid == CID_BROADCAST ? next_cid() : in->cid; - resp->versionInterface = U2FHID_IF_VERSION; - resp->versionMajor = VERSION_MAJOR; - resp->versionMinor = VERSION_MINOR; - resp->versionBuild = VERSION_PATCH; - resp->capFlags = CAPFLAG_WINK; + memcpy(resp.nonce, init_req->nonce, sizeof(init_req->nonce)); + resp.cid = in->cid == CID_BROADCAST ? next_cid() : in->cid; + resp.versionInterface = U2FHID_IF_VERSION; + resp.versionMajor = VERSION_MAJOR; + resp.versionMinor = VERSION_MINOR; + resp.versionBuild = VERSION_PATCH; + resp.capFlags = CAPFLAG_WINK; + memcpy(&f.init.data, &resp, sizeof(resp)); queue_u2f_pkt(&f); } diff --git a/firmware/usb.c b/firmware/usb.c index 6466f4ae2b..56504deee8 100644 --- a/firmware/usb.c +++ b/firmware/usb.c @@ -342,7 +342,7 @@ static void hid_u2f_rx_callback(usbd_device *dev, uint8_t ep) debugLog(0, "", "hid_u2f_rx_callback"); if ( usbd_ep_read_packet(dev, ENDPOINT_ADDRESS_U2F_OUT, buf, 64) != 64) return; - u2fhid_read(tiny, (const U2FHID_FRAME *)buf); + u2fhid_read(tiny, (const U2FHID_FRAME *) (void*) buf); } #if DEBUG_LINK