2014-04-29 12:26:51 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2014-04-29 12:26:51 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
*
|
|
|
|
* This library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_H_
|
|
|
|
#define __UTIL_H_
|
|
|
|
|
2018-03-11 14:58:11 +00:00
|
|
|
#include <setup.h>
|
2019-03-29 16:10:31 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2017-12-13 18:38:51 +00:00
|
|
|
#if !EMULATOR
|
2017-07-01 19:08:26 +00:00
|
|
|
#include <libopencm3/cm3/scb.h>
|
|
|
|
#include <libopencm3/cm3/vector.h>
|
2019-02-24 11:23:49 +00:00
|
|
|
#include "timer.h"
|
2017-12-13 18:38:51 +00:00
|
|
|
#endif
|
2017-07-01 19:08:26 +00:00
|
|
|
|
2017-12-09 13:30:15 +00:00
|
|
|
// Statement expressions make these macros side-effect safe
|
2019-03-29 16:10:31 +00:00
|
|
|
#define MIN_8bits(a, b) \
|
|
|
|
({ \
|
|
|
|
typeof(a) _a = (a); \
|
|
|
|
typeof(b) _b = (b); \
|
|
|
|
_a < _b ? (_a & 0xFF) : (_b & 0xFF); \
|
|
|
|
})
|
|
|
|
#define MIN(a, b) \
|
|
|
|
({ \
|
|
|
|
typeof(a) _a = (a); \
|
|
|
|
typeof(b) _b = (b); \
|
|
|
|
_a < _b ? _a : _b; \
|
|
|
|
})
|
|
|
|
#define MAX(a, b) \
|
|
|
|
({ \
|
|
|
|
typeof(a) _a = (a); \
|
|
|
|
typeof(b) _b = (b); \
|
|
|
|
_a > _b ? _a : _b; \
|
|
|
|
})
|
2017-12-09 13:30:15 +00:00
|
|
|
|
2016-02-15 14:29:19 +00:00
|
|
|
void delay(uint32_t wait);
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
// converts uint32 to hexa (8 digits)
|
|
|
|
void uint32hex(uint32_t num, char *str);
|
|
|
|
|
|
|
|
// converts data to hexa
|
|
|
|
void data2hex(const void *data, uint32_t len, char *str);
|
|
|
|
|
2019-02-20 07:52:42 +00:00
|
|
|
// defined in startup.s (or setup.c for emulator)
|
2018-03-22 17:53:54 +00:00
|
|
|
extern void __attribute__((noreturn)) shutdown(void);
|
|
|
|
|
2017-12-13 18:38:51 +00:00
|
|
|
#if !EMULATOR
|
2017-08-08 10:59:39 +00:00
|
|
|
// defined in memory.ld
|
|
|
|
extern uint8_t _ram_start[], _ram_end[];
|
2017-07-01 19:08:26 +00:00
|
|
|
|
2017-08-08 10:59:39 +00:00
|
|
|
// defined in startup.s
|
|
|
|
extern void memset_reg(void *start, void *stop, uint32_t val);
|
2017-07-01 19:08:26 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
#define FW_SIGNED 0x5A3CA5C3
|
|
|
|
#define FW_UNTRUSTED 0x00000000
|
|
|
|
|
|
|
|
static inline void __attribute__((noreturn))
|
2019-04-01 15:02:32 +00:00
|
|
|
jump_to_firmware(const vector_table_t *ivt, int trust) {
|
|
|
|
if (FW_SIGNED == trust) { // trusted signed firmware
|
|
|
|
SCB_VTOR = (uint32_t)ivt; // * relocate vector table
|
2019-03-29 16:10:31 +00:00
|
|
|
// Set stack pointer
|
2019-04-01 15:02:32 +00:00
|
|
|
__asm__ volatile("msr msp, %0" ::"r"(ivt->initial_sp_value));
|
2019-03-29 16:10:31 +00:00
|
|
|
} else { // untrusted firmware
|
|
|
|
timer_init();
|
|
|
|
mpu_config_firmware(); // * configure MPU for the firmware
|
|
|
|
__asm__ volatile("msr msp, %0" ::"r"(_stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jump to address
|
2019-04-01 15:02:32 +00:00
|
|
|
ivt->reset();
|
2019-03-29 16:10:31 +00:00
|
|
|
|
|
|
|
// Prevent compiler from generating stack protector code (which causes CPU
|
|
|
|
// fault because the stack is moved)
|
|
|
|
for (;;)
|
|
|
|
;
|
2017-07-01 19:08:26 +00:00
|
|
|
}
|
2018-02-13 18:18:19 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static inline void set_mode_unprivileged(void) {
|
|
|
|
// http://infocenter.arm.com/help/topic/com.arm.doc.dui0552a/CHDBIBGJ.html
|
|
|
|
__asm__ volatile("msr control, %0" ::"r"(0x1));
|
2018-02-13 18:18:19 +00:00
|
|
|
}
|
2018-06-21 12:28:39 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static inline bool is_mode_unprivileged(void) {
|
|
|
|
uint32_t r0;
|
|
|
|
__asm__ volatile("mrs %0, control" : "=r"(r0));
|
|
|
|
return r0 & 1;
|
2018-06-21 12:28:39 +00:00
|
|
|
}
|
2018-06-21 12:52:17 +00:00
|
|
|
|
|
|
|
#else /* EMULATOR */
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static inline bool is_mode_unprivileged(void) { return true; }
|
2017-12-13 18:38:51 +00:00
|
|
|
#endif
|
2017-07-01 19:08:26 +00:00
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
#endif
|