mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-26 17:38:39 +00:00
trezorhal: use sectrue where possible
This commit is contained in:
parent
efa918a2f9
commit
4d36d0e1c1
1
Makefile
1
Makefile
@ -170,6 +170,7 @@ vendorheader_sl: ## construct SatoshiLabs vendor header
|
|||||||
binctl: ## print info about binary files
|
binctl: ## print info about binary files
|
||||||
./tools/binctl $(BOOTLOADER_BUILD_DIR)/bootloader.bin
|
./tools/binctl $(BOOTLOADER_BUILD_DIR)/bootloader.bin
|
||||||
./tools/binctl embed/firmware/vendorheader.bin
|
./tools/binctl embed/firmware/vendorheader.bin
|
||||||
|
./tools/binctl $(PRODTEST_BUILD_DIR)/prodtest.bin
|
||||||
./tools/binctl $(FIRMWARE_BUILD_DIR)/firmware.bin
|
./tools/binctl $(FIRMWARE_BUILD_DIR)/firmware.bin
|
||||||
|
|
||||||
bloaty: ## run bloaty size profiler
|
bloaty: ## run bloaty size profiler
|
||||||
|
@ -29,7 +29,7 @@ static const uint8_t * const BOARDLOADER_KEYS[] = {
|
|||||||
|
|
||||||
static uint32_t check_sdcard(void)
|
static uint32_t check_sdcard(void)
|
||||||
{
|
{
|
||||||
if (!sdcard_is_present()) {
|
if (sectrue != sdcard_is_present()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ static secbool copy_sdcard(void)
|
|||||||
display_printf("%d ", i);
|
display_printf("%d ", i);
|
||||||
hal_delay(1000);
|
hal_delay(1000);
|
||||||
codelen = check_sdcard();
|
codelen = check_sdcard();
|
||||||
if (!codelen) {
|
if (0 == codelen) {
|
||||||
display_printf("\n\nno SD card, aborting\n");
|
display_printf("\n\nno SD card, aborting\n");
|
||||||
return secfalse;
|
return secfalse;
|
||||||
}
|
}
|
||||||
@ -109,13 +109,13 @@ static secbool copy_sdcard(void)
|
|||||||
FLASH_SECTOR_FIRMWARE_EXTRA_END,
|
FLASH_SECTOR_FIRMWARE_EXTRA_END,
|
||||||
FLASH_SECTOR_PIN_AREA,
|
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");
|
display_printf(" failed\n");
|
||||||
return secfalse;
|
return secfalse;
|
||||||
}
|
}
|
||||||
display_printf(" done\n\n");
|
display_printf(" done\n\n");
|
||||||
|
|
||||||
if (!flash_unlock()) {
|
if (sectrue != flash_unlock()) {
|
||||||
display_printf("could not unlock flash\n");
|
display_printf("could not unlock flash\n");
|
||||||
return secfalse;
|
return secfalse;
|
||||||
}
|
}
|
||||||
@ -129,7 +129,7 @@ static secbool copy_sdcard(void)
|
|||||||
for (int i = 0; i < (IMAGE_HEADER_SIZE + codelen) / SDCARD_BLOCK_SIZE; i++) {
|
for (int i = 0; i < (IMAGE_HEADER_SIZE + codelen) / SDCARD_BLOCK_SIZE; i++) {
|
||||||
sdcard_read_blocks((uint8_t *)buf, i, 1);
|
sdcard_read_blocks((uint8_t *)buf, i, 1);
|
||||||
for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) {
|
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");
|
display_printf("copy failed\n");
|
||||||
sdcard_power_off();
|
sdcard_power_off();
|
||||||
flash_lock();
|
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
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if PRODUCTION
|
#if PRODUCTION
|
||||||
flash_set_option_bytes();
|
flash_set_option_bytes();
|
||||||
if (!flash_check_option_bytes()) {
|
if (sectrue != flash_check_option_bytes()) {
|
||||||
uint8_t sectors[] = {
|
uint8_t sectors[] = {
|
||||||
FLASH_SECTOR_STORAGE_1,
|
FLASH_SECTOR_STORAGE_1,
|
||||||
FLASH_SECTOR_STORAGE_2,
|
FLASH_SECTOR_STORAGE_2,
|
||||||
|
@ -207,7 +207,7 @@ secbool bootloader_loop(secbool firmware_present)
|
|||||||
ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
|
ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
|
||||||
uint16_t msg_id;
|
uint16_t msg_id;
|
||||||
uint32_t msg_size;
|
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
|
// invalid header -> discard
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -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]);
|
uint8_t offset = mp_obj_get_int(args[2]);
|
||||||
mp_buffer_info_t data;
|
mp_buffer_info_t data;
|
||||||
mp_get_buffer_raise(args[3], &data, MP_BUFFER_READ);
|
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");
|
mp_raise_ValueError("write failed");
|
||||||
}
|
}
|
||||||
return mp_const_none;
|
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]);
|
uint8_t offset = mp_obj_get_int(args[2]);
|
||||||
mp_buffer_info_t data;
|
mp_buffer_info_t data;
|
||||||
mp_get_buffer_raise(args[3], &data, MP_BUFFER_WRITE);
|
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");
|
mp_raise_ValueError("read failed");
|
||||||
}
|
}
|
||||||
return mp_const_none;
|
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) {
|
STATIC mp_obj_t mod_trezorio_FlashOTP_lock(mp_obj_t self, mp_obj_t block) {
|
||||||
uint8_t b = mp_obj_get_int(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");
|
mp_raise_ValueError("lock failed");
|
||||||
}
|
}
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
|
@ -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)) {
|
if (MP_OBJ_IS_TYPE(iface, &mod_trezorio_HID_type)) {
|
||||||
mp_obj_HID_t *hid = MP_OBJ_TO_PTR(iface);
|
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();
|
usb_deinit();
|
||||||
mp_raise_msg(&mp_type_RuntimeError, "failed to add HID interface");
|
mp_raise_msg(&mp_type_RuntimeError, "failed to add HID interface");
|
||||||
}
|
}
|
||||||
} else if (MP_OBJ_IS_TYPE(iface, &mod_trezorio_VCP_type)) {
|
} else if (MP_OBJ_IS_TYPE(iface, &mod_trezorio_VCP_type)) {
|
||||||
mp_obj_VCP_t *vcp = MP_OBJ_TO_PTR(iface);
|
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();
|
usb_deinit();
|
||||||
mp_raise_msg(&mp_type_RuntimeError, "failed to add VCP interface");
|
mp_raise_msg(&mp_type_RuntimeError, "failed to add VCP interface");
|
||||||
}
|
}
|
||||||
|
@ -199,25 +199,25 @@ static void test_sd(void)
|
|||||||
static uint8_t buf1[8 * 1024];
|
static uint8_t buf1[8 * 1024];
|
||||||
static uint8_t buf2[8 * 1024];
|
static uint8_t buf2[8 * 1024];
|
||||||
|
|
||||||
if (!sdcard_is_present()) {
|
if (sectrue != sdcard_is_present()) {
|
||||||
vcp_printf("ERROR NOCARD");
|
vcp_printf("ERROR NOCARD");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
sdcard_power_on();
|
sdcard_power_on();
|
||||||
if (!sdcard_read_blocks(buf1, 0, 0)) {
|
if (sectrue != sdcard_read_blocks(buf1, 0, 0)) {
|
||||||
vcp_printf("ERROR sdcard_read_blocks");
|
vcp_printf("ERROR sdcard_read_blocks");
|
||||||
goto power_off;
|
goto power_off;
|
||||||
}
|
}
|
||||||
if (!sdcard_write_blocks(buf1, 0, 0)) {
|
if (sectrue != sdcard_write_blocks(buf1, 0, 0)) {
|
||||||
vcp_printf("ERROR sdcard_write_blocks");
|
vcp_printf("ERROR sdcard_write_blocks");
|
||||||
goto power_off;
|
goto power_off;
|
||||||
}
|
}
|
||||||
if (!sdcard_read_blocks(buf2, 0, 0)) {
|
if (sectrue != sdcard_read_blocks(buf2, 0, 0)) {
|
||||||
vcp_printf("ERROR sdcard_read_blocks");
|
vcp_printf("ERROR sdcard_read_blocks");
|
||||||
goto power_off;
|
goto power_off;
|
||||||
}
|
}
|
||||||
if (memcmp(buf1, buf2, sizeof(buf1)) != 0) {
|
if (0 != memcmp(buf1, buf2, sizeof(buf1))) {
|
||||||
vcp_printf("ERROR DATA MISMATCH");
|
vcp_printf("ERROR DATA MISMATCH");
|
||||||
goto power_off;
|
goto power_off;
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ secbool flash_lock(void)
|
|||||||
|
|
||||||
secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len))
|
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;
|
return secfalse;
|
||||||
}
|
}
|
||||||
FLASH_EraseInitTypeDef EraseInitStruct;
|
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) {
|
if (block >= FLASH_OTP_NUM_BLOCKS || offset + datalen > FLASH_OTP_BLOCK_SIZE) {
|
||||||
return secfalse;
|
return secfalse;
|
||||||
}
|
}
|
||||||
if (!flash_unlock()) {
|
if (sectrue != flash_unlock()) {
|
||||||
return secfalse;
|
return secfalse;
|
||||||
}
|
}
|
||||||
secbool ret = secfalse;
|
secbool ret = secfalse;
|
||||||
@ -127,7 +127,7 @@ secbool flash_otp_lock(uint8_t block)
|
|||||||
if (block >= FLASH_OTP_NUM_BLOCKS) {
|
if (block >= FLASH_OTP_NUM_BLOCKS) {
|
||||||
return secfalse;
|
return secfalse;
|
||||||
}
|
}
|
||||||
if (!flash_unlock()) {
|
if (sectrue != flash_unlock()) {
|
||||||
return secfalse;
|
return secfalse;
|
||||||
}
|
}
|
||||||
HAL_StatusTypeDef ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, FLASH_OTP_LOCK_BASE + block, 0x00);
|
HAL_StatusTypeDef ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, FLASH_OTP_LOCK_BASE + block, 0x00);
|
||||||
|
@ -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)
|
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;
|
if (sig_m > sig_n) return secfalse;
|
||||||
|
|
||||||
// discard bits higher than sig_n
|
// 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);
|
blake2s_Final(&ctx, hash, BLAKE2S_DIGEST_LENGTH);
|
||||||
|
|
||||||
ed25519_public_key pub;
|
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));
|
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);
|
blake2s_Final(&ctx, hash, BLAKE2S_DIGEST_LENGTH);
|
||||||
|
|
||||||
ed25519_public_key pub;
|
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));
|
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)
|
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;
|
return secfalse;
|
||||||
}
|
}
|
||||||
const void *data = (const void *)(FLASH_SECTOR_TABLE[sectors[0]] + firstskip);
|
const void *data = (const void *)(FLASH_SECTOR_TABLE[sectors[0]] + firstskip);
|
||||||
int remaining = hdr->codelen;
|
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;
|
return secfalse;
|
||||||
}
|
}
|
||||||
int block = 1;
|
int block = 1;
|
||||||
@ -156,7 +156,7 @@ secbool check_image_contents(const image_header * const hdr, uint32_t firstskip,
|
|||||||
return secfalse;
|
return secfalse;
|
||||||
}
|
}
|
||||||
data = (const void *)FLASH_SECTOR_TABLE[sectors[block]];
|
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;
|
return secfalse;
|
||||||
}
|
}
|
||||||
block++;
|
block++;
|
||||||
|
@ -71,7 +71,7 @@ secbool sdcard_is_present(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
secbool sdcard_power_on(void) {
|
secbool sdcard_power_on(void) {
|
||||||
if (!sdcard_is_present()) {
|
if (sectrue != sdcard_is_present()) {
|
||||||
return secfalse;
|
return secfalse;
|
||||||
}
|
}
|
||||||
if (sd_handle.Instance) {
|
if (sd_handle.Instance) {
|
||||||
@ -109,7 +109,7 @@ error:
|
|||||||
}
|
}
|
||||||
|
|
||||||
secbool sdcard_power_off(void) {
|
secbool sdcard_power_off(void) {
|
||||||
if (!sd_handle.Instance) {
|
if (NULL == sd_handle.Instance) {
|
||||||
return sectrue;
|
return sectrue;
|
||||||
}
|
}
|
||||||
HAL_SD_DeInit(&sd_handle);
|
HAL_SD_DeInit(&sd_handle);
|
||||||
|
@ -43,7 +43,7 @@ static const USBD_DescriptorsTypeDef usb_descriptors;
|
|||||||
static const USBD_ClassTypeDef usb_class;
|
static const USBD_ClassTypeDef usb_class;
|
||||||
|
|
||||||
static secbool check_desc_str(const uint8_t *s) {
|
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;
|
if (strlen((const char *)s) > USB_MAX_STR_SIZE) return secfalse;
|
||||||
return sectrue;
|
return sectrue;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user