2018-03-16 20:40:39 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2018-03-16 20:40:39 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Jochen Hoenicke <hoenicke@gmail.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 __SUPERVISE_H__
|
|
|
|
#define __SUPERVISE_H__
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2019-03-29 16:20:01 +00:00
|
|
|
#if !EMULATOR
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
#define SVC_FLASH_UNLOCK 0
|
|
|
|
#define SVC_FLASH_ERASE 1
|
2018-03-16 20:40:39 +00:00
|
|
|
#define SVC_FLASH_PROGRAM 2
|
2019-03-29 16:10:31 +00:00
|
|
|
#define SVC_FLASH_LOCK 3
|
|
|
|
#define SVC_TIMER_MS 4
|
2021-02-09 07:29:36 +00:00
|
|
|
#define SVC_REBOOT_TO_BOOTLOADER 5
|
2018-03-16 20:40:39 +00:00
|
|
|
|
2021-02-09 13:39:32 +00:00
|
|
|
#define RETURN_TO_BOOTLOADER_FLAG_ADDR ((uint32_t *)0x20010000)
|
|
|
|
#define RETURN_TO_BOOTLOADER_FLAG 0x11
|
|
|
|
|
2018-03-16 20:40:39 +00:00
|
|
|
/* Unlocks flash. This function needs to be called before programming
|
|
|
|
* or erasing. Multiple calls of flash_program and flash_erase can
|
|
|
|
* follow and should be completed with flash_lock().
|
|
|
|
*/
|
|
|
|
inline void svc_flash_unlock(void) {
|
2019-03-29 16:10:31 +00:00
|
|
|
__asm__ __volatile__("svc %0" ::"i"(SVC_FLASH_UNLOCK) : "memory");
|
2018-03-16 20:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable flash write operations.
|
|
|
|
* @param program_size (8-bit, 16-bit, 32-bit or 64-bit)
|
|
|
|
* should be one of the FLASH_CR_PROGRAM_X.. constants
|
|
|
|
*/
|
|
|
|
inline void svc_flash_program(uint32_t program_size) {
|
2019-03-29 16:10:31 +00:00
|
|
|
register uint32_t r0 __asm__("r0") = program_size;
|
|
|
|
__asm__ __volatile__("svc %0" ::"i"(SVC_FLASH_PROGRAM), "r"(r0) : "memory");
|
2018-03-16 20:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Erase a flash sector.
|
2019-03-29 16:10:31 +00:00
|
|
|
* @param sector sector number 0..11
|
2018-03-16 20:40:39 +00:00
|
|
|
* (this only allows erasing meta sectors 2 and 3 though).
|
|
|
|
*/
|
|
|
|
inline void svc_flash_erase_sector(uint8_t sector) {
|
2019-03-29 16:10:31 +00:00
|
|
|
register uint32_t r0 __asm__("r0") = sector;
|
|
|
|
__asm__ __volatile__("svc %0" ::"i"(SVC_FLASH_ERASE), "r"(r0) : "memory");
|
2018-03-16 20:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock flash after programming or erasing.
|
|
|
|
* @return flash status register (FLASH_SR)
|
|
|
|
*/
|
|
|
|
inline uint32_t svc_flash_lock(void) {
|
2019-03-29 16:10:31 +00:00
|
|
|
register uint32_t r0 __asm__("r0");
|
|
|
|
__asm__ __volatile__("svc %1" : "=r"(r0) : "i"(SVC_FLASH_LOCK) : "memory");
|
|
|
|
return r0;
|
2018-03-16 20:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t svc_timer_ms(void) {
|
2019-03-29 16:10:31 +00:00
|
|
|
register uint32_t r0 __asm__("r0");
|
|
|
|
__asm__ __volatile__("svc %1" : "=r"(r0) : "i"(SVC_TIMER_MS) : "memory");
|
|
|
|
return r0;
|
2018-03-16 20:40:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 07:29:36 +00:00
|
|
|
/* Reboot to bootloader.
|
2021-02-09 13:39:32 +00:00
|
|
|
* Sets a flag in memory and reboots. When bootloader runs, it will recognize
|
2021-02-09 07:29:36 +00:00
|
|
|
* flag and won't run firmware, but enter bootloader mode.
|
|
|
|
*/
|
|
|
|
inline void svc_reboot_to_bootloader(void) {
|
|
|
|
__asm__ __volatile__("svc %0" ::"i"(SVC_REBOOT_TO_BOOTLOADER) : "memory");
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:02:50 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
extern void svc_flash_unlock(void);
|
|
|
|
extern void svc_flash_program(uint32_t program_size);
|
|
|
|
extern void svc_flash_erase_sector(uint16_t sector);
|
|
|
|
extern uint32_t svc_flash_lock(void);
|
|
|
|
extern uint32_t svc_timer_ms(void);
|
|
|
|
|
|
|
|
#endif
|
2018-03-16 20:40:39 +00:00
|
|
|
|
|
|
|
#endif
|