From 4d36d0e1c108926d97b174d604356d9bc7932350 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 27 Oct 2017 03:32:26 +0200 Subject: [PATCH] trezorhal: use sectrue where possible --- Makefile | 1 + embed/boardloader/main.c | 14 +++++++------- embed/bootloader/main.c | 2 +- embed/extmod/modtrezorio/modtrezorio-flash.h | 6 +++--- embed/extmod/modtrezorio/modtrezorio-msg.h | 4 ++-- embed/prodtest/main.c | 10 +++++----- embed/trezorhal/flash.c | 6 +++--- embed/trezorhal/image.c | 12 ++++++------ embed/trezorhal/sdcard.c | 4 ++-- embed/trezorhal/usb.c | 2 +- 10 files changed, 31 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 5dbfb58e..afbdf66b 100644 --- a/Makefile +++ b/Makefile @@ -170,6 +170,7 @@ vendorheader_sl: ## construct SatoshiLabs vendor header binctl: ## print info about binary files ./tools/binctl $(BOOTLOADER_BUILD_DIR)/bootloader.bin ./tools/binctl embed/firmware/vendorheader.bin + ./tools/binctl $(PRODTEST_BUILD_DIR)/prodtest.bin ./tools/binctl $(FIRMWARE_BUILD_DIR)/firmware.bin bloaty: ## run bloaty size profiler diff --git a/embed/boardloader/main.c b/embed/boardloader/main.c index 672074f1..fd641419 100644 --- a/embed/boardloader/main.c +++ b/embed/boardloader/main.c @@ -29,7 +29,7 @@ static const uint8_t * const BOARDLOADER_KEYS[] = { static uint32_t check_sdcard(void) { - if (!sdcard_is_present()) { + if (sectrue != sdcard_is_present()) { return 0; } @@ -77,7 +77,7 @@ static secbool copy_sdcard(void) display_printf("%d ", i); hal_delay(1000); codelen = check_sdcard(); - if (!codelen) { + if (0 == codelen) { display_printf("\n\nno SD card, aborting\n"); return secfalse; } @@ -109,13 +109,13 @@ static secbool copy_sdcard(void) FLASH_SECTOR_FIRMWARE_EXTRA_END, FLASH_SECTOR_PIN_AREA, }; - if (!flash_erase_sectors(sectors, 2 + 1 + 6 + 4 + 7 + 1, progress_callback)) { + if (sectrue != flash_erase_sectors(sectors, 2 + 1 + 6 + 4 + 7 + 1, progress_callback)) { display_printf(" failed\n"); return secfalse; } display_printf(" done\n\n"); - if (!flash_unlock()) { + if (sectrue != flash_unlock()) { display_printf("could not unlock flash\n"); return secfalse; } @@ -129,7 +129,7 @@ static secbool copy_sdcard(void) for (int i = 0; i < (IMAGE_HEADER_SIZE + codelen) / SDCARD_BLOCK_SIZE; i++) { sdcard_read_blocks((uint8_t *)buf, i, 1); for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) { - if (!flash_write_word(BOOTLOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j])) { + if (sectrue != flash_write_word(BOOTLOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j])) { display_printf("copy failed\n"); sdcard_power_off(); flash_lock(); @@ -151,13 +151,13 @@ int main(void) { periph_init(); // need the systick timer running before the production flash (and many other HAL) operations - if (!reset_flags_init()) { + if (sectrue != reset_flags_init()) { return 1; } #if PRODUCTION flash_set_option_bytes(); - if (!flash_check_option_bytes()) { + if (sectrue != flash_check_option_bytes()) { uint8_t sectors[] = { FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2, diff --git a/embed/bootloader/main.c b/embed/bootloader/main.c index 0b69f089..5a316568 100644 --- a/embed/bootloader/main.c +++ b/embed/bootloader/main.c @@ -207,7 +207,7 @@ secbool bootloader_loop(secbool firmware_present) ensure(sectrue * (r == USB_PACKET_SIZE), NULL); uint16_t msg_id; uint32_t msg_size; - if (!msg_parse_header(buf, &msg_id, &msg_size)) { + if (sectrue != msg_parse_header(buf, &msg_id, &msg_size)) { // invalid header -> discard continue; } diff --git a/embed/extmod/modtrezorio/modtrezorio-flash.h b/embed/extmod/modtrezorio/modtrezorio-flash.h index ddadef4b..2003f450 100644 --- a/embed/extmod/modtrezorio/modtrezorio-flash.h +++ b/embed/extmod/modtrezorio/modtrezorio-flash.h @@ -39,7 +39,7 @@ STATIC mp_obj_t mod_trezorio_FlashOTP_write(size_t n_args, const mp_obj_t *args) uint8_t offset = mp_obj_get_int(args[2]); mp_buffer_info_t data; mp_get_buffer_raise(args[3], &data, MP_BUFFER_READ); - if (!flash_otp_write(block, offset, data.buf, data.len)) { + if (sectrue != flash_otp_write(block, offset, data.buf, data.len)) { mp_raise_ValueError("write failed"); } return mp_const_none; @@ -55,7 +55,7 @@ STATIC mp_obj_t mod_trezorio_FlashOTP_read(size_t n_args, const mp_obj_t *args) uint8_t offset = mp_obj_get_int(args[2]); mp_buffer_info_t data; mp_get_buffer_raise(args[3], &data, MP_BUFFER_WRITE); - if (!flash_otp_read(block, offset, data.buf, data.len)) { + if (sectrue != flash_otp_read(block, offset, data.buf, data.len)) { mp_raise_ValueError("read failed"); } return mp_const_none; @@ -68,7 +68,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorio_FlashOTP_read_obj, 4, 4, /// ''' STATIC mp_obj_t mod_trezorio_FlashOTP_lock(mp_obj_t self, mp_obj_t block) { uint8_t b = mp_obj_get_int(block); - if (!flash_otp_lock(b)) { + if (sectrue != flash_otp_lock(b)) { mp_raise_ValueError("lock failed"); } return mp_const_none; diff --git a/embed/extmod/modtrezorio/modtrezorio-msg.h b/embed/extmod/modtrezorio/modtrezorio-msg.h index 78a31b95..066da7a5 100644 --- a/embed/extmod/modtrezorio/modtrezorio-msg.h +++ b/embed/extmod/modtrezorio/modtrezorio-msg.h @@ -449,13 +449,13 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) { if (MP_OBJ_IS_TYPE(iface, &mod_trezorio_HID_type)) { mp_obj_HID_t *hid = MP_OBJ_TO_PTR(iface); - if (!usb_hid_add(&hid->info)) { + if (sectrue != usb_hid_add(&hid->info)) { usb_deinit(); mp_raise_msg(&mp_type_RuntimeError, "failed to add HID interface"); } } else if (MP_OBJ_IS_TYPE(iface, &mod_trezorio_VCP_type)) { mp_obj_VCP_t *vcp = MP_OBJ_TO_PTR(iface); - if (!usb_vcp_add(&vcp->info)) { + if (sectrue != usb_vcp_add(&vcp->info)) { usb_deinit(); mp_raise_msg(&mp_type_RuntimeError, "failed to add VCP interface"); } diff --git a/embed/prodtest/main.c b/embed/prodtest/main.c index 1e4a540b..4ef6268b 100644 --- a/embed/prodtest/main.c +++ b/embed/prodtest/main.c @@ -199,25 +199,25 @@ static void test_sd(void) static uint8_t buf1[8 * 1024]; static uint8_t buf2[8 * 1024]; - if (!sdcard_is_present()) { + if (sectrue != sdcard_is_present()) { vcp_printf("ERROR NOCARD"); return; } sdcard_power_on(); - if (!sdcard_read_blocks(buf1, 0, 0)) { + if (sectrue != sdcard_read_blocks(buf1, 0, 0)) { vcp_printf("ERROR sdcard_read_blocks"); goto power_off; } - if (!sdcard_write_blocks(buf1, 0, 0)) { + if (sectrue != sdcard_write_blocks(buf1, 0, 0)) { vcp_printf("ERROR sdcard_write_blocks"); goto power_off; } - if (!sdcard_read_blocks(buf2, 0, 0)) { + if (sectrue != sdcard_read_blocks(buf2, 0, 0)) { vcp_printf("ERROR sdcard_read_blocks"); goto power_off; } - if (memcmp(buf1, buf2, sizeof(buf1)) != 0) { + if (0 != memcmp(buf1, buf2, sizeof(buf1))) { vcp_printf("ERROR DATA MISMATCH"); goto power_off; } diff --git a/embed/trezorhal/flash.c b/embed/trezorhal/flash.c index f9421949..6cc5785a 100644 --- a/embed/trezorhal/flash.c +++ b/embed/trezorhal/flash.c @@ -48,7 +48,7 @@ secbool flash_lock(void) secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len)) { - if (!flash_unlock()) { + if (sectrue != flash_unlock()) { return secfalse; } FLASH_EraseInitTypeDef EraseInitStruct; @@ -108,7 +108,7 @@ secbool flash_otp_write(uint8_t block, uint8_t offset, const uint8_t *data, uint if (block >= FLASH_OTP_NUM_BLOCKS || offset + datalen > FLASH_OTP_BLOCK_SIZE) { return secfalse; } - if (!flash_unlock()) { + if (sectrue != flash_unlock()) { return secfalse; } secbool ret = secfalse; @@ -127,7 +127,7 @@ secbool flash_otp_lock(uint8_t block) if (block >= FLASH_OTP_NUM_BLOCKS) { return secfalse; } - if (!flash_unlock()) { + if (sectrue != flash_unlock()) { return secfalse; } HAL_StatusTypeDef ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, FLASH_OTP_LOCK_BASE + block, 0x00); diff --git a/embed/trezorhal/image.c b/embed/trezorhal/image.c index bac82381..74cee048 100644 --- a/embed/trezorhal/image.c +++ b/embed/trezorhal/image.c @@ -9,7 +9,7 @@ static secbool compute_pubkey(uint8_t sig_m, uint8_t sig_n, const uint8_t * const *pub, uint8_t sigmask, ed25519_public_key res) { - if (!sig_m || !sig_n) return secfalse; + if (0 == sig_m || 0 == sig_n) return secfalse; if (sig_m > sig_n) return secfalse; // discard bits higher than sig_n @@ -68,7 +68,7 @@ secbool load_image_header(const uint8_t * const data, const uint32_t magic, cons blake2s_Final(&ctx, hash, BLAKE2S_DIGEST_LENGTH); ed25519_public_key pub; - if (!compute_pubkey(key_m, key_n, keys, hdr->sigmask, pub)) return secfalse; + if (sectrue != compute_pubkey(key_m, key_n, keys, hdr->sigmask, pub)) return secfalse; return sectrue * (0 == ed25519_sign_open(hash, BLAKE2S_DIGEST_LENGTH, pub, *(const ed25519_signature *)hdr->sig)); } @@ -125,7 +125,7 @@ secbool load_vendor_header(const uint8_t * const data, uint8_t key_m, uint8_t ke blake2s_Final(&ctx, hash, BLAKE2S_DIGEST_LENGTH); ed25519_public_key pub; - if (!compute_pubkey(key_m, key_n, keys, vhdr->sigmask, pub)) return secfalse; + if (sectrue != compute_pubkey(key_m, key_n, keys, vhdr->sigmask, pub)) return secfalse; return sectrue * (0 == ed25519_sign_open(hash, BLAKE2S_DIGEST_LENGTH, pub, *(const ed25519_signature *)vhdr->sig)); } @@ -141,12 +141,12 @@ static secbool check_hash(const uint8_t * const hash, const uint8_t * const data secbool check_image_contents(const image_header * const hdr, uint32_t firstskip, const uint8_t *sectors, int blocks) { - if (!sectors || blocks < 1) { + if (0 == sectors || blocks < 1) { return secfalse; } const void *data = (const void *)(FLASH_SECTOR_TABLE[sectors[0]] + firstskip); int remaining = hdr->codelen; - if (!check_hash(hdr->hashes, data, MIN(remaining, IMAGE_CHUNK_SIZE - firstskip))) { + if (sectrue != check_hash(hdr->hashes, data, MIN(remaining, IMAGE_CHUNK_SIZE - firstskip))) { return secfalse; } int block = 1; @@ -156,7 +156,7 @@ secbool check_image_contents(const image_header * const hdr, uint32_t firstskip, return secfalse; } data = (const void *)FLASH_SECTOR_TABLE[sectors[block]]; - if (!check_hash(hdr->hashes + block * 32, data, MIN(remaining, IMAGE_CHUNK_SIZE))) { + if (sectrue != check_hash(hdr->hashes + block * 32, data, MIN(remaining, IMAGE_CHUNK_SIZE))) { return secfalse; } block++; diff --git a/embed/trezorhal/sdcard.c b/embed/trezorhal/sdcard.c index 49e2d29f..dd5f0edb 100644 --- a/embed/trezorhal/sdcard.c +++ b/embed/trezorhal/sdcard.c @@ -71,7 +71,7 @@ secbool sdcard_is_present(void) { } secbool sdcard_power_on(void) { - if (!sdcard_is_present()) { + if (sectrue != sdcard_is_present()) { return secfalse; } if (sd_handle.Instance) { @@ -109,7 +109,7 @@ error: } secbool sdcard_power_off(void) { - if (!sd_handle.Instance) { + if (NULL == sd_handle.Instance) { return sectrue; } HAL_SD_DeInit(&sd_handle); diff --git a/embed/trezorhal/usb.c b/embed/trezorhal/usb.c index 0d55e454..f055aae4 100644 --- a/embed/trezorhal/usb.c +++ b/embed/trezorhal/usb.c @@ -43,7 +43,7 @@ static const USBD_DescriptorsTypeDef usb_descriptors; static const USBD_ClassTypeDef usb_class; static secbool check_desc_str(const uint8_t *s) { - if (!s) return secfalse; + if (NULL == s) return secfalse; if (strlen((const char *)s) > USB_MAX_STR_SIZE) return secfalse; return sectrue; }