1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00

trezorhal: use sectrue where possible

This commit is contained in:
Pavol Rusnak 2017-10-27 03:32:26 +02:00
parent efa918a2f9
commit 4d36d0e1c1
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
10 changed files with 31 additions and 30 deletions

View File

@ -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

View File

@ -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,

View File

@ -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;
}

View File

@ -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;

View File

@ -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");
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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++;

View File

@ -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);

View File

@ -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;
}