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/>.
|
|
|
|
*/
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "supervise.h"
|
2018-03-16 20:40:39 +00:00
|
|
|
#include <libopencm3/stm32/flash.h>
|
|
|
|
#include <stdint.h>
|
2021-02-10 12:18:36 +00:00
|
|
|
#if !EMULATOR
|
2021-02-10 12:02:58 +00:00
|
|
|
#include <vendor/libopencm3/include/libopencmsis/core_cm3.h>
|
2021-02-10 12:18:36 +00:00
|
|
|
#endif
|
2018-03-16 20:40:39 +00:00
|
|
|
#include "memory.h"
|
|
|
|
|
2018-03-26 17:02:50 +00:00
|
|
|
#if !EMULATOR
|
|
|
|
|
2018-03-16 20:40:39 +00:00
|
|
|
static void svhandler_flash_unlock(void) {
|
2019-03-29 16:10:31 +00:00
|
|
|
flash_wait_for_last_operation();
|
|
|
|
flash_clear_status_flags();
|
|
|
|
flash_unlock();
|
2018-03-16 20:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void svhandler_flash_program(uint32_t psize) {
|
2019-03-29 16:10:31 +00:00
|
|
|
/* Wait for any write operation to complete. */
|
|
|
|
flash_wait_for_last_operation();
|
|
|
|
/* check program size argument */
|
|
|
|
if (psize != FLASH_CR_PROGRAM_X8 && psize != FLASH_CR_PROGRAM_X16 &&
|
|
|
|
psize != FLASH_CR_PROGRAM_X32 && psize != FLASH_CR_PROGRAM_X64)
|
|
|
|
return;
|
|
|
|
FLASH_CR = (FLASH_CR & ~(FLASH_CR_PROGRAM_MASK << FLASH_CR_PROGRAM_SHIFT)) |
|
|
|
|
(psize << FLASH_CR_PROGRAM_SHIFT);
|
|
|
|
FLASH_CR |= FLASH_CR_PG;
|
2018-03-16 20:40:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 09:41:35 +00:00
|
|
|
static void svhandler_flash_erase_sector(uint8_t sector) {
|
2019-03-29 16:10:31 +00:00
|
|
|
/* we only allow erasing storage sectors 2 and 3. */
|
|
|
|
if (sector < FLASH_STORAGE_SECTOR_FIRST ||
|
|
|
|
sector > FLASH_STORAGE_SECTOR_LAST) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
flash_erase_sector(sector, FLASH_CR_PROGRAM_X32);
|
2018-03-16 20:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t svhandler_flash_lock(void) {
|
2019-03-29 16:10:31 +00:00
|
|
|
/* Wait for any write operation to complete. */
|
|
|
|
flash_wait_for_last_operation();
|
|
|
|
/* Disable writes to flash. */
|
|
|
|
FLASH_CR &= ~FLASH_CR_PG;
|
|
|
|
/* lock flash register */
|
|
|
|
FLASH_CR |= FLASH_CR_LOCK;
|
|
|
|
/* return flash status register */
|
|
|
|
return FLASH_SR;
|
2018-03-16 20:40:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 07:29:36 +00:00
|
|
|
static void __attribute__((noreturn)) svhandler_reboot_to_bootloader(void) {
|
2021-02-11 23:26:59 +00:00
|
|
|
*STAY_IN_BOOTLOADER_FLAG_ADDR = STAY_IN_BOOTLOADER_FLAG;
|
2021-02-09 07:29:36 +00:00
|
|
|
scb_reset_system();
|
|
|
|
}
|
|
|
|
|
2018-03-16 20:40:39 +00:00
|
|
|
extern volatile uint32_t system_millis;
|
|
|
|
|
|
|
|
void svc_handler_main(uint32_t *stack) {
|
2019-03-29 16:10:31 +00:00
|
|
|
uint8_t svc_number = ((uint8_t *)stack[6])[-2];
|
|
|
|
switch (svc_number) {
|
|
|
|
case SVC_FLASH_UNLOCK:
|
|
|
|
svhandler_flash_unlock();
|
|
|
|
break;
|
|
|
|
case SVC_FLASH_PROGRAM:
|
|
|
|
svhandler_flash_program(stack[0]);
|
|
|
|
break;
|
|
|
|
case SVC_FLASH_ERASE:
|
|
|
|
svhandler_flash_erase_sector(stack[0]);
|
|
|
|
break;
|
|
|
|
case SVC_FLASH_LOCK:
|
|
|
|
stack[0] = svhandler_flash_lock();
|
|
|
|
break;
|
|
|
|
case SVC_TIMER_MS:
|
|
|
|
stack[0] = system_millis;
|
|
|
|
break;
|
2021-02-09 07:29:36 +00:00
|
|
|
case SVC_REBOOT_TO_BOOTLOADER:
|
|
|
|
svhandler_reboot_to_bootloader();
|
|
|
|
break;
|
2019-03-29 16:10:31 +00:00
|
|
|
default:
|
|
|
|
stack[0] = 0xffffffff;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-16 20:40:39 +00:00
|
|
|
}
|
2018-03-26 17:02:50 +00:00
|
|
|
|
|
|
|
#endif
|